| 558 | |
| 559 | template <NativeArithmetic Arithmetic> |
| 560 | void native_gu_rows_impl(const NativeFmt& f, const uint8_t* blob, const void* const* act, int nt, float* const* ff, |
| 561 | int r0, int r1) { |
| 562 | // the multi-token kernels decode the weights once for all tokens: 2.0-2.4x ggml-cpu at three tokens, no faster |
| 563 | // at one (all are bound by the codebook lookups, ~5 GB/s per core), measured by native_expert_parity. AVX-512 |
| 564 | // first, then the AVX-2 one (Zen 2/3, Intel 12th-14th gen). STRATA_NO_IQ512 drops an AVX-512 CPU to the |
| 565 | // AVX-2 kernel, STRATA_NO_IQ256 drops the AVX-2 kernel; ggml-cpu's single-token vec_dot is reached only with |
| 566 | // both set (and on a CPU without AVX-512, STRATA_NO_IQ512 changes nothing). |
| 567 | // 0.2.12 (PR 5120): AVX-512 F/DQ/BW/VL + VNNI is what iq_avx512.cpp runs on (its object holds no VBMI instruction); VBMI is not required. |
| 568 | static const bool avx512 = cpu_vnni_ok() && std::getenv("STRATA_NO_IQ512") == nullptr; |
| 569 | // STRATA_NO_IQ256 drops the AVX-2 kernel. cpu_avx2_ok() is ALSO required: iq_avx2.cpp and kq_avx2.cpp |
| 570 | // are compiled /arch:AVX2 and use AVX2 and FMA3, so calling them on a CPU without either is an |
| 571 | // illegal instruction, not a slow path. The gate sat next to `avx512` above, which does test |
| 572 | // cpu_avx512_ok(), and that asymmetry is what let a pre-Haswell CPU reach iq256_gu_rows. |
| 573 | // Without it this falls through to ggml-cpu's vec_dot below, which is compiled for whatever |
| 574 | // baseline the build selected (AVX1 here) and covers the same types - IQ2_XXS and IQ2_S among |
| 575 | // them. That path loops over tokens itself, so it is correct for any `nt`, not just one. |
| 576 | static const bool avx2 = cpu_avx2_ok() && std::getenv("STRATA_NO_IQ256") == nullptr; |
| 577 | // The older-CPU tier (STRATA_ISA_FLOOR=avx builds): without it every expert on such a CPU runs on ggml-cpu's |
| 578 | // scalar _generic dots. Only where AVX2 is absent, so a modern CPU's rounding never moves (and |
| 579 | // STRATA_NO_IQ256=1 there still means ggml, as before). iq_avx1.cpp is compiled for AVX and gated on |
| 580 | // cpu_avx1_ok(). Measured on an E5-2470 v2 (iq_avx1_parity --bench): 1.2-3.1x ggml's dot from two tokens on, |
| 581 | // at one token ggml's auto-vectorized generic loop matches the 128-bit kernel - so the same #152 mt_min rule |
| 582 | // as the AVX-2 kernels, and STRATA_IQ_MT_MIN=1 takes the kernel for one token too (its rows are then |
| 583 | // independent of the drafting: iq_avx1_parity checks every width bit for bit). |
| 584 | static const bool avx1 = !cpu_avx2_ok() && cpu_avx1_ok() && std::getenv("STRATA_NO_IQ128") == nullptr; |
| 585 | // The exact rows are ggml-cpu's AVX2 single-token dot bit for bit, so they need ggml-cpu itself built for AVX2 and |
| 586 | // not AVX-512 (ggml_cpu_has_*): a STRATA_ISA_FLOOR build runs ggml's AVX1/SSE dots, a native build on an AVX-512 CPU |
| 587 | // run with STRATA_FORCE_AVX2=1 runs its AVX-512 one, and both round differently (#2215's guard). |
| 588 | static const bool exact_one = [] { |
| 589 | const char* v = std::getenv("STRATA_CPU_IQ_ALL_EXACT1"); |
| 590 | return v && v[0] == '1' && v[1] == 0 && flavour_avx2() && !flavour_avx512(); |
| 591 | }(); |
| 592 | // only where ggml's dot runs today (below the #152 multi-token rule): with STRATA_IQ_MT_MIN=1 a lone token keeps the |
| 593 | // multi-token kernel, so a token's rows still do not depend on how many tokens share its expert |
| 594 | if (exact_one && nt == 1 && nt < native_gu_mt_min(f.gu_type) && avx2 && !cpu_avx512_ok() && |
| 595 | (f.gu_type == 16 || f.gu_type == 17 || f.gu_type == 18 || f.gu_type == 21 || f.gu_type == 22)) { |
| 596 | static std::once_flag announced; |
| 597 | std::call_once(announced, [] { std::fprintf(stderr, "strata ab: GATE=STRATA_CPU_IQ_ALL_EXACT1 active=1\n"); }); |
| 598 | iq256_gu_rows_exact_one(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act[0], ff[0], r0, r1); |
| 599 | return; |
| 600 | } |
| 601 | // 0.2.5 (AVX-512 CPUs where a gather is cheap: Intel Sapphire Rapids and later): ONE token of an IQ3_XXS / IQ3_S expert on the |
| 602 | // 512-bit exact rows, ggml-cpu's single-token AVX2 dot bit for bit with the grid words gathered: 1.3x ggml's dot on a Xeon 8468 |
| 603 | // (x512_bench), the same output. Where the multi-token rule (#152) already sends one token to the multi-token kernel |
| 604 | // (STRATA_IQ_MT_MIN=1) that stays; STRATA_NO_IQ512X1=1 turns this off; the first use checks the bits against this ggml. |
| 605 | static const bool x512_one = avx512 && iq512_gather_on() && std::getenv("STRATA_NO_IQ512X1") == nullptr; |
| 606 | if (x512_one && nt == 1 && nt < native_gu_mt_min(f.gu_type) && f.tail_off == 0 && (f.gu_type == 18 || f.gu_type == 21) && |
| 607 | x512_one_ok(f.gu_type)) { |
| 608 | const int n1 = (int) f.n_embd; |
| 609 | float g[64], u[64]; |
| 610 | float* gp[1] = {g}; |
| 611 | float* up[1] = {u}; |
| 612 | for (int c0 = r0; c0 < r1; c0 += 64) { |
| 613 | const int c1 = std::min(r1, c0 + 64); |
| 614 | iq512x_rows(f.gu_type, blob + (size_t) c0 * f.gu_row, f.gu_row, n1, act, 1, gp, 0, c1 - c0); |
| 615 | iq512x_rows(f.gu_type, blob + f.up_off + (size_t) c0 * f.gu_row, f.gu_row, n1, act, 1, up, 0, c1 - c0); |
| 616 | for (int r = c0; r < c1; ++r) ff[0][r] = (g[r - c0] / (1.f + std::exp(-g[r - c0]))) * u[r - c0]; |
| 617 | } |
nothing calls this directly
no test coverage detected