Values within a few ulps of the rounding boundaries of the Q8_K quantizer (nearest_int adds the 2^23 + 2^22 magic number to iscale * x) and of the Q4_0 requantizer (x * id + 8.5, truncated). A compiler that contracts the multiply and the add into one FMA gives another result on exactly these inputs, and ggml-base does so on arm64 under GCC (not on x86), so random data finds the mismatch only afte
| 523 | // requantizer (x * id + 8.5, truncated). A compiler that contracts the multiply and the add into one FMA gives another result on exactly these inputs, and |
| 524 | // ggml-base does so on arm64 under GCC (not on x86), so random data finds the mismatch only after millions of values: this finds it in a few thousand. |
| 525 | void test_boundary(int rows) { |
| 526 | Rng r(4242); |
| 527 | const sq::Arith ar = oracle_arith(); |
| 528 | const sq::ActKernel* k8 = sq::act_kernel(sq::Type::Q8_K, ar); |
| 529 | const ggml_type_traits_cpu* t8 = ggml_get_type_traits_cpu(GGML_TYPE_Q8_K); |
| 530 | auto jitter = [&r](float v) { |
| 531 | for (uint32_t t = r.next() % 4; t > 0; --t) v = std::nextafter(v, r.next() & 1 ? INFINITY : -INFINITY); |
| 532 | return v; |
| 533 | }; |
| 534 | if (k8 != nullptr && t8 != nullptr) { |
| 535 | Group& g = open_group("bnd", "q8_K", 256); |
| 536 | uint64_t h = kFnvBasis; |
| 537 | bool reported = false; |
| 538 | std::vector<float> x(256); |
| 539 | std::vector<uint8_t> a(sq::kBlockQ8_K), b(sq::kBlockQ8_K); |
| 540 | for (int it = 0; it < rows * 300; ++it) { |
| 541 | const float m = 0.5f + 0.5f * std::fabs(r.uni()); |
| 542 | const int e = (int) (r.next() % 14) - 7; |
| 543 | const float dd = std::ldexp(m, e); |
| 544 | const float x0 = (r.next() & 1 ? 127.f : -127.f) * dd; |
| 545 | const float step = -1.f / (-127.f / x0); |
| 546 | x[0] = x0; |
| 547 | for (int j = 1; j < 256; ++j) x[(size_t) j] = jitter(((int) (r.next() % 253) - 126 + 0.5f) * step); |
| 548 | std::memset(a.data(), 0, a.size()); |
| 549 | std::memset(b.data(), 0, b.size()); |
| 550 | t8->from_float(x.data(), a.data(), 256); |
| 551 | k8->fn(x.data(), b.data(), 256); |
| 552 | const uint64_t rin = golden_fnv1a64(x.data(), x.size() * 4); |
| 553 | g.in = golden_fnv1a64(x.data(), x.size() * 4, g.in); |
| 554 | g.ref = golden_fnv1a64(a.data(), a.size(), g.ref); |
| 555 | h = golden_fnv1a64(b.data(), b.size(), h); |
| 556 | ++g.rows; |
| 557 | dump_row(g, "ggml", it, rin, a.data(), a.size()); |
| 558 | dump_row(g, k8->name, it, rin, b.data(), b.size()); |
| 559 | ++g_checked; |
| 560 | if (a != b && !reported) { |
| 561 | reported = true; |
| 562 | live_diff("q8_K", "boundary values: the quantizer differs from ggml's (a fused multiply-add on one side), iteration " + std::to_string(it)); |
| 563 | } |
| 564 | } |
| 565 | close_group(g, {{k8->name, h}}); |
| 566 | } |
| 567 | Group& g = open_group("bnd", "q4_0", 32); |
| 568 | uint64_t h = kFnvBasis; |
| 569 | bool reported = false; |
| 570 | std::vector<float> x(32); |
| 571 | std::vector<uint8_t> a(18), b(18); |
| 572 | for (int it = 0; it < rows * 300; ++it) { |
| 573 | const float m = 0.5f + 0.5f * std::fabs(r.uni()); |
| 574 | const int e = (int) (r.next() % 14) - 7; |
| 575 | const float dd = std::ldexp(m, e); |
| 576 | x[0] = -8.f * dd; |
| 577 | for (int j = 1; j < 32; ++j) x[(size_t) j] = jitter(((int) (r.next() % 15) + 1 - 8.5f) * dd); |
| 578 | ggml_quantize_chunk(GGML_TYPE_Q4_0, x.data(), a.data(), 0, 1, 32, nullptr); |
| 579 | sq::quantize_q4_0_ref(x.data(), b.data(), 32); |
| 580 | const uint64_t rin = golden_fnv1a64(x.data(), x.size() * 4); |
| 581 | g.in = golden_fnv1a64(x.data(), x.size() * 4, g.in); |
| 582 | g.ref = golden_fnv1a64(a.data(), a.size(), g.ref); |
no test coverage detected