| 377 | } |
| 378 | |
| 379 | void test_act(sq::Type act, int rows) { |
| 380 | const ggml_type gt = (ggml_type) (int) act; |
| 381 | const ggml_type_traits_cpu* tr = ggml_get_type_traits_cpu(gt); |
| 382 | const sq::TypeInfo* ti = sq::type_info(act); |
| 383 | if (tr == nullptr || tr->from_float == nullptr || ti == nullptr) return; |
| 384 | const sq::Arith ar = oracle_arith(); |
| 385 | const sq::ActKernel* k = sq::act_kernel(act, ar); |
| 386 | if (k == nullptr || k->arith != ar) { |
| 387 | fail(ti->name, std::string("no activation quantizer of contract ") + sq::arith_name(ar)); |
| 388 | return; |
| 389 | } |
| 390 | Rng r(777 + (uint64_t) act); |
| 391 | for (int nbk = 1; nbk <= 6; ++nbk) { |
| 392 | const int n = ti->blck * nbk; |
| 393 | Group& g = open_group("act", ti->name, n); |
| 394 | uint64_t h = kFnvBasis; |
| 395 | bool reported = false; |
| 396 | std::vector<float> x((size_t) n); |
| 397 | std::vector<uint8_t> a(ti->bytes * nbk), b(ti->bytes * nbk); |
| 398 | for (int it = 0; it < rows * 4; ++it) { |
| 399 | for (float& v : x) v = r.gauss() * (it % 3 == 0 ? 1.f : (it % 3 == 1 ? 1e-3f : 300.f)); |
| 400 | if (it % 7 == 0) std::memset(x.data(), 0, x.size() * 4); // an all-zero block |
| 401 | if (it % 11 == 0) x[(size_t) (r.next() % n)] = 0.f; |
| 402 | // ties: a block whose absolute maximum is 127 (id = 1) with half-integers elsewhere, and its negation |
| 403 | if (it % 5 == 0) { |
| 404 | // one draw per statement (see Rng::gauss): two r.next() in one expression gave MSVC other inputs than GCC |
| 405 | for (int j = 0; j < n; ++j) { |
| 406 | const int q = (int) (r.next() % 255) - 127; |
| 407 | const float half = r.next() & 1 ? 0.f : 0.5f; |
| 408 | x[(size_t) j] = q * 0.5f + half; |
| 409 | } |
| 410 | for (int bI = 0; bI < nbk; ++bI) { |
| 411 | const uint32_t at = r.next() % ti->blck; |
| 412 | x[(size_t) bI * ti->blck + at] = r.next() & 1 ? 127.f : -127.f; |
| 413 | } |
| 414 | if (act == sq::Type::Q8_K) for (int bI = 0; bI < nbk; ++bI) x[(size_t) bI * ti->blck + r.next() % ti->blck] = 127.f; |
| 415 | } |
| 416 | // pre-filled: Q8_K leaves an all-zero block's 16 block sums unwritten (ggml and the layer alike); without this they carry an earlier row's bytes |
| 417 | std::memset(a.data(), 0, a.size()); |
| 418 | std::memset(b.data(), 0, b.size()); |
| 419 | tr->from_float(x.data(), a.data(), n); |
| 420 | k->fn(x.data(), b.data(), n); |
| 421 | const uint64_t rin = golden_fnv1a64(x.data(), x.size() * 4); |
| 422 | g.in = golden_fnv1a64(x.data(), x.size() * 4, g.in); |
| 423 | g.ref = golden_fnv1a64(a.data(), a.size(), g.ref); |
| 424 | h = golden_fnv1a64(b.data(), b.size(), h); |
| 425 | ++g.rows; |
| 426 | dump_row(g, "ggml", it, rin, a.data(), a.size()); |
| 427 | dump_row(g, k->name, it, rin, b.data(), b.size()); |
| 428 | ++g_checked; |
| 429 | if (std::memcmp(a.data(), b.data(), a.size()) != 0 && !reported) { |
| 430 | reported = true; |
| 431 | size_t at = 0; |
| 432 | while (a[at] == b[at]) ++at; |
| 433 | char buf[160]; |
| 434 | std::snprintf(buf, sizeof buf, "kernel %s n=%d iteration %d: first differing byte %zu (ggml %02x ours %02x)", k->name, n, it, at, a[at], b[at]); |
| 435 | live_diff(ti->name, buf); |
| 436 | } |
no test coverage detected