mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-27 20:04:35 +00:00
dd5ae06405
* iq2_xxs: basics * iq2_xxs: scalar and AVX2 dot products Needed to change Q8_K to have quants in the -127...127 range, else the IQ2_XXS AVX implementation becomes very awkward. The alternative would have been to use Q8_0 instead. Perhaps I'll change later, for now this is what we have. * iq2_xxs: ARM_NEON dot product Somehow strangely slow (112 ms/token). * iq2_xxs: WIP Metal Dequantize works, something is still wrong with the dot product. * iq2_xxs: Metal dot product now works We have PP-512 = 475 t/s TG-128 = 47.3 t/s Not the greatest performance, but not complete garbage either. * iq2_xxs: slighty faster dot product TG-128 is now 48.4 t/s * iq2_xxs: slighty faster dot product TG-128 is now 50.9 t/s * iq2_xxs: even faster Metal dot product TG-128 is now 54.1 t/s. Strangely enough, putting the signs lookup table into shared memory has a bigger impact than the grid values being in shared memory. * iq2_xxs: dequantize CUDA kernel - fix conflict with master * iq2_xxs: quantized CUDA dot product (MMVQ) We get TG-128 = 153.1 t/s * iq2_xxs: slightly faster CUDA dot product TG-128 is now at 155.1 t/s. * iq2_xxs: add to llama ftype enum * iq2_xxs: fix MoE on Metal * Fix missing MMQ ops when on hipBLAS I had put the ggml_supports_mmq call at the wrong place. * Fix bug in qequantize_row_iq2_xxs The 0.25f factor was missing. Great detective work by @ggerganov! * Fixing tests * PR suggestion --------- Co-authored-by: Iwan Kawrakow <iwan.kawrakow@gmail.com>
179 lines
5.9 KiB
C++
179 lines
5.9 KiB
C++
// Unit tests for quantization specific functions - quantize, dequantize and dot product
|
|
|
|
#include "ggml.h"
|
|
|
|
#undef NDEBUG
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#if defined(_MSC_VER)
|
|
#pragma warning(disable: 4244 4267) // possible loss of data
|
|
#endif
|
|
|
|
constexpr float MAX_QUANTIZATION_REFERENCE_ERROR = 0.0001f;
|
|
constexpr float MAX_QUANTIZATION_TOTAL_ERROR = 0.002f;
|
|
constexpr float MAX_QUANTIZATION_TOTAL_ERROR_2BITS = 0.0075f;
|
|
constexpr float MAX_QUANTIZATION_TOTAL_ERROR_3BITS = 0.0040f;
|
|
constexpr float MAX_DOT_PRODUCT_ERROR = 0.02f;
|
|
|
|
static const char* RESULT_STR[] = {"ok", "FAILED"};
|
|
|
|
|
|
// Generate synthetic data
|
|
static void generate_data(float offset, size_t n, float * dst) {
|
|
for (size_t i = 0; i < n; i++) {
|
|
dst[i] = 0.1 + 2*cosf(i + offset);
|
|
}
|
|
}
|
|
|
|
// Calculate RMSE between two float arrays
|
|
static float array_rmse(const float * a1, const float * a2, size_t n) {
|
|
double sum = 0;
|
|
for (size_t i = 0; i < n; i++) {
|
|
double diff = a1[i] - a2[i];
|
|
sum += diff * diff;
|
|
}
|
|
return sqrtf(sum) / n;
|
|
}
|
|
|
|
// Total quantization error on test data
|
|
static float total_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
|
|
std::vector<uint8_t> tmp_q(2*test_size);
|
|
std::vector<float> tmp_out(test_size);
|
|
|
|
qfns.from_float(test_data, tmp_q.data(), test_size);
|
|
qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
|
|
return array_rmse(test_data, tmp_out.data(), test_size);
|
|
}
|
|
|
|
// Total quantization error on test data
|
|
static float reference_quantization_error(ggml_type_traits_t & qfns, size_t test_size, const float * test_data) {
|
|
std::vector<uint8_t> tmp_q(2*test_size);
|
|
std::vector<float> tmp_out(test_size);
|
|
std::vector<float> tmp_out_ref(test_size);
|
|
|
|
qfns.from_float(test_data, tmp_q.data(), test_size);
|
|
qfns.to_float(tmp_q.data(), tmp_out.data(), test_size);
|
|
|
|
qfns.from_float_reference(test_data, tmp_q.data(), test_size);
|
|
qfns.to_float(tmp_q.data(), tmp_out_ref.data(), test_size);
|
|
|
|
return array_rmse(tmp_out.data(), tmp_out_ref.data(), test_size);
|
|
}
|
|
|
|
static float dot_product(const float * a1, const float * a2, size_t test_size) {
|
|
double sum = 0;
|
|
for (size_t i = 0; i < test_size; i++) {
|
|
sum += a1[i] * a2[i];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
// Total dot product error
|
|
static float dot_product_error(
|
|
ggml_type_traits_t & qfns, size_t test_size, const float * test_data1, const float *test_data2
|
|
) {
|
|
std::vector<uint8_t> tmp_q1(2*test_size);
|
|
std::vector<uint8_t> tmp_q2(2*test_size);
|
|
|
|
auto vdot = ggml_internal_get_type_traits(qfns.vec_dot_type);
|
|
|
|
qfns.from_float(test_data1, tmp_q1.data(), test_size);
|
|
vdot.from_float(test_data2, tmp_q2.data(), test_size);
|
|
|
|
float result = INFINITY;
|
|
qfns.vec_dot(test_size, &result, tmp_q1.data(), tmp_q2.data());
|
|
|
|
const float dot_ref = dot_product(test_data1, test_data2, test_size);
|
|
|
|
return fabsf(result - dot_ref) / test_size;
|
|
}
|
|
|
|
int main(int argc, char * argv[]) {
|
|
bool verbose = false;
|
|
const size_t test_size = 32 * 128;
|
|
|
|
std::string arg;
|
|
for (int i = 1; i < argc; i++) {
|
|
arg = argv[i];
|
|
|
|
if (arg == "-v") {
|
|
verbose = true;
|
|
} else {
|
|
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
std::vector<float> test_data(test_size);
|
|
std::vector<float> test_data2(test_size);
|
|
|
|
generate_data(0.0, test_data.size(), test_data.data());
|
|
generate_data(1.0, test_data2.size(), test_data2.data());
|
|
|
|
// Initialize GGML, ensures float conversion tables are initialized
|
|
struct ggml_init_params ggml_params = {
|
|
/* .mem_size = */ 1*1024,
|
|
/* .mem_buffer = */ NULL,
|
|
/* .no_alloc = */ true,
|
|
};
|
|
struct ggml_context * ctx = ggml_init(ggml_params);
|
|
|
|
int num_failed = 0;
|
|
bool failed = false;
|
|
|
|
for (int i = 0; i < GGML_TYPE_COUNT; i++) {
|
|
ggml_type type = (ggml_type) i;
|
|
ggml_type_traits_t qfns = ggml_internal_get_type_traits(type);
|
|
|
|
// deprecated - skip
|
|
if (qfns.blck_size == 0) {
|
|
continue;
|
|
}
|
|
|
|
if ((ggml_type)i == GGML_TYPE_IQ2_XXS) {
|
|
printf("Skip %s due to missing quantization functionality\n", ggml_type_name((ggml_type) i));
|
|
continue;
|
|
}
|
|
|
|
printf("Testing %s\n", ggml_type_name((ggml_type) i));
|
|
|
|
if (qfns.from_float && qfns.to_float) {
|
|
const float total_error = total_quantization_error(qfns, test_size, test_data.data());
|
|
const float max_quantization_error =
|
|
type == GGML_TYPE_Q2_K ? MAX_QUANTIZATION_TOTAL_ERROR_2BITS :
|
|
type == GGML_TYPE_Q3_K ? MAX_QUANTIZATION_TOTAL_ERROR_3BITS : MAX_QUANTIZATION_TOTAL_ERROR;
|
|
failed = !(total_error < max_quantization_error);
|
|
num_failed += failed;
|
|
if (failed || verbose) {
|
|
printf("%5s absolute quantization error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], total_error);
|
|
}
|
|
|
|
const float reference_error = reference_quantization_error(qfns, test_size, test_data.data());
|
|
failed = !(reference_error < MAX_QUANTIZATION_REFERENCE_ERROR);
|
|
num_failed += failed;
|
|
if (failed || verbose) {
|
|
printf("%5s reference implementation error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], reference_error);
|
|
}
|
|
|
|
const float vec_dot_error = dot_product_error(qfns, test_size, test_data.data(), test_data2.data());
|
|
failed = !(vec_dot_error < MAX_DOT_PRODUCT_ERROR);
|
|
num_failed += failed;
|
|
if (failed || verbose) {
|
|
printf("%5s dot product error: %s (%f)\n", ggml_type_name(type), RESULT_STR[failed], vec_dot_error);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (num_failed || verbose) {
|
|
printf("%d tests failed\n", num_failed);
|
|
}
|
|
|
|
ggml_free(ctx);
|
|
|
|
return num_failed > 0;
|
|
}
|