/* * test_vq3_port.c — Verification test for the llama-modchip LUT-VQ3-64 port * (GGML_TYPE_VQ3_0 = 44 in this port) against the built ggml static libs. * * 1. type traits sanity (vq3_0 / 64 / 24 B / quantized) * 2. ggml_quantize_chunk(VQ3_0) round-trip SQNR / cosine on heavy-tailed data * 3. vec_dot(VQ3_0 x Q8_0) vs F32 reference (needs ggml_init() for the FP16 table!) * 4. scalar-fallback vs AVX-512 VBMI kernel byte-exact quantizer equivalence * * Build: * gcc -O2 -march=native -mf16c tests/test_vq3_port.c -I ../llama.cpp/ggml/include \ * -o test_vq3_port ../llama.cpp/build/ggml/src/libggml*.a -lm -lstdc++ -lgomp -lpthread */ #include #include #include #include #include #include "ggml.h" #include "ggml-cpu.h" static uint64_t rng_state = 0x9E3779B97F4A7C15ULL; static double frand(void) { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 7; rng_state ^= rng_state << 17; return (double)(rng_state >> 11) / 9007199254740992.0; } static double gauss(void) { double u1 = frand(), u2 = frand(); if (u1 < 1e-300) u1 = 1e-300; return sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2); } int main(void) { static char init_pool[1 << 20]; struct ggml_init_params ip = { sizeof(init_pool), init_pool, true }; struct ggml_context * ctx = ggml_init(ip); ggml_cpu_init(); /* REQUIRED: fills ggml_table_f32_f16 used by GGML_CPU_FP16_TO_FP32 (upstream forces LUT, issue #30342) */ const enum ggml_type vt = GGML_TYPE_VQ3_0; const struct ggml_type_traits * tt = ggml_get_type_traits(vt); printf("type_name=%s blck_size=%d type_size=%zu is_quantized=%d\n", tt->type_name, (int)tt->blck_size, tt->type_size, (int)tt->is_quantized); if (strcmp(tt->type_name, "vq3_0") || tt->blck_size != 64 || tt->type_size != 24 || !tt->is_quantized) { fprintf(stderr, "FAIL: type traits mismatch\n"); return 1; } const struct ggml_type_traits_cpu * tc = ggml_get_type_traits_cpu(vt); if (!tc->from_float || !tc->vec_dot) { fprintf(stderr, "FAIL: cpu traits missing\n"); return 1; } /* --- round trip ------------------------------------------------------- */ const int64_t n = 64 * 4096; float * x = malloc(n * sizeof(float)); for (int64_t i = 0; i < n; ++i) { double g = gauss() * 0.05; if (frand() < 0.02) g *= 4.0; x[i] = (float)g; } size_t nbytes = (n / tt->blck_size) * tt->type_size; void * q = malloc(nbytes); tc->from_float(x, q, n); float * xd = malloc(n * sizeof(float)); tt->to_float(q, xd, n); double sig = 0, err = 0, dxy = 0, dx2 = 0, dy2 = 0; for (int64_t i = 0; i < n; ++i) { double e = (double)x[i] - xd[i]; sig += (double)x[i] * x[i]; err += e * e; dxy += (double)x[i] * xd[i]; dx2 += (double)x[i] * x[i]; dy2 += (double)xd[i] * xd[i]; } double sqnr = 10.0 * log10(sig / err), cosim = dxy / (sqrt(dx2) * sqrt(dy2)); printf("round-trip: SQNR=%.2f dB cos=%.5f (%.3f bpw)\n", sqnr, cosim, 8.0 * (double)nbytes / n); int fail = (sqnr < 11.0 || cosim < 0.95); /* --- vec dot ---------------------------------------------------------- */ const int64_t nd = 64 * 16384; float * a = malloc(nd * sizeof(float)); float * b = malloc(nd * sizeof(float)); for (int64_t i = 0; i < nd; ++i) { a[i] = (float)(gauss() * 0.05 * ((frand() < 0.02) ? 4.0 : 1.0)); b[i] = (float)(gauss() * 0.05 * ((frand() < 0.02) ? 4.0 : 1.0)); } void * qa = malloc(nd / 64 * 24); void * qb8 = malloc(nd / 32 * 34); tc->from_float(a, qa, nd); ggml_get_type_traits_cpu(GGML_TYPE_Q8_0)->from_float(b, qb8, nd); float dot_q = 0, dot_ref = 0, dot_dq = 0; tc->vec_dot(nd, &dot_q, 0, qa, 0, qb8, 0, 1); for (int64_t i = 0; i < nd; ++i) dot_ref += a[i] * b[i]; float * ad = malloc(nd * sizeof(float)); tt->to_float(qa, ad, nd); for (int64_t i = 0; i < nd; ++i) dot_dq += ad[i] * b[i]; double rel = fabs((double)dot_q - dot_dq) / (fabs(dot_dq) + 1e-6); /* noise scale: sqrt(sum a^2 b^2) — the f32 ref dot of independent samples is a near-cancellation random walk, so normalize by the per-element noise scale */ double noise = 0; for (int64_t i = 0; i < nd; ++i) noise += (double)a[i] * a[i] * b[i] * b[i]; noise = sqrt(noise); double rel2 = fabs((double)dot_dq - dot_ref) / noise; printf("vec_dot: vq3xq8=%.10g dequant-f32=%.10g f32ref=%.10g |q8diff|/|dq|=%.5f |dequant-ref|/noise=%.4f\n", dot_q, dot_dq, dot_ref, rel, rel2); if (rel > 0.03) { fprintf(stderr, "FAIL: vec_dot deviates from dequant path beyond Q8 noise\n"); fail = 1; } if (rel2 > 0.30) { fprintf(stderr, "FAIL: dequant dot error vs f32 too large\n"); fail = 1; } if (!fail) printf("ALL OK\n"); else fprintf(stderr, "TEST FAILED\n"); ggml_free(ctx); free(x); free(q); free(xd); free(a); free(b); free(qa); free(qb8); free(ad); return fail; }