MCPcopy Create free account
hub / github.com/Niko1221/Strata / native_down_rows_impl

Function native_down_rows_impl

engine/src/kernels/cpu/native_expert.cpp:793–894  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

791
792template <NativeArithmetic Arithmetic>
793void native_down_rows_impl(const NativeFmt& f, const uint8_t* blob, const void* const* hq, int nt, float* const* out,
794 int r0, int r1) {
795 // IQ4_NL down rows: the AVX-2 multi-token kernel decodes the nibbles and absolutises the weights once per
796 // block instead of once per token; ggml-cpu's dot is single-token. STRATA_NO_IQ4NL falls back to it.
797 static const bool iq4nl_mt = std::getenv("STRATA_NO_IQ4NL") == nullptr;
798 static const int mt_min = [] { const char* e = std::getenv("STRATA_IQ_MT_MIN"); return e ? std::atoi(e) : 2; }();
799 static const bool kq = [] { const char* v = std::getenv("STRATA_KQ256"); return v != nullptr && std::atoi(v) != 0; }();
800 // Both multi-token kernels below are /arch:AVX2 translation units (kq_avx2.cpp and iq_avx2.cpp),
801 // so a CPU without AVX2 has to reach ggml-cpu's vec_dot instead - same reasoning as the gate/up
802 // rows above, where `avx512` tested cpu_avx512_ok() and `avx2` did not.
803 if (tq_rows_own(f.d_type)) { // the trellis types, as in native_gu_rows
804 tq_rows(tq_isa(), f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
805 return;
806 }
807 // BW-4: Q5_1 / Q8_0 down rows in 256-bit VNNI lanes with the weights decoded once per row (kq_avx512.cpp), bit-exact (kq512_ok), as the Q4_K gate/up rows in native_gu_rows
808 static const bool kq512 = cpu_avx512_ok() && std::getenv("STRATA_NO_KQ512") == nullptr;
809 static const int kq512_min = [] { const char* e = std::getenv("STRATA_KQ512_MIN"); return e ? std::atoi(e) : 8; }();
810 static const bool kq512_q51_ok = kq512 && kq512_ok(7), kq512_q80_ok = kq512 && kq512_ok(8); // (checked once, see native_gu_rows)
811 if (((f.d_type == 7 && kq512_q51_ok) || (f.d_type == 8 && kq512_q80_ok)) && nt >= kq512_min && (Arithmetic == NativeArithmetic::Default || flavour_avx2())) {
812 kq512_rows(f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
813 return;
814 }
815 if (cpu_avx2_ok() && kq && nt >= 2 && (f.d_type == 7 || f.d_type == 8) &&
816 (Arithmetic == NativeArithmetic::Default || (flavour_avx2()))) { // Q5_1 / Q8_0 down: bit-exact, any group size
817 kq256_rows(f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
818 return;
819 }
820 // 0.2.12: Q5_0 down rows (ggml-cpu's dot, once a token) in 512-bit lanes, the same bits (q50_512_ok checks that on first use): the weights
821 // of a block pair are decoded once for all the tokens. STRATA_NO_Q50_512=1 keeps ggml's dot; STRATA_Q50_512_MIN=N: from N tokens on (default 2).
822 static const bool q50_512 = cpu_vnni_ok() && std::getenv("STRATA_NO_Q50_512") == nullptr;
823 static const int q50_min = [] { const char* e = std::getenv("STRATA_Q50_512_MIN"); return e ? std::atoi(e) : 2; }();
824 if (q50_512 && f.d_type == 6 && nt >= q50_min && f.tail_off == 0 && q50_512_ok()) {
825 q50_512_down_rows(blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
826 return;
827 }
828 if (cpu_avx2_ok() && nt >= mt_min && f.d_type == 20 && iq4nl_mt) { // #152: the same rule as the gate/up rows
829 if (Arithmetic == NativeArithmetic::Default || mt_min <= 1) {
830 iq4nl256_down_rows(blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
831 return;
832 }
833#if defined(__x86_64__) || defined(_M_X64)
834 if (flavour_avx2() && iq4nl_reference_ok()) {
835 iq4nl_reference_rows(blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
836 return;
837 }
838#endif
839 }
840 // the older-CPU tier: IQ4_NL and Q2_0 down rows in 128-bit (ggml-cpu's dots for both are scalar here)
841 static const bool avx1 = !cpu_avx2_ok() && cpu_avx1_ok() && std::getenv("STRATA_NO_IQ128") == nullptr;
842 if (avx1 && iq128_down_supported(f.d_type)) {
843 iq128_down_rows(f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
844 return;
845 }
846 const int n = (int) f.n_ff;
847 float sd = 1.f; // NVFP4: the expert's s_down (see native_gu_rows)
848 if (f.tail_off) std::memcpy(&sd, blob + f.tail_off + 2 * sizeof(float), sizeof sd);
849 static const bool nvfp4_512 = cpu_avx512_ok() && std::getenv("STRATA_NO_NVFP4_512") == nullptr;
850 if (f.d_type == kNvfp4Type && nvfp4_512 && nvfp4_512_fits(n)) {

Callers

nothing calls this directly

Calls 15

tq_rows_ownFunction · 0.85
tq_rowsFunction · 0.85
tq_isaFunction · 0.85
kq512_okFunction · 0.85
flavour_avx2Function · 0.85
cpu_vnni_okFunction · 0.85
q50_512_okFunction · 0.85
iq4nl_reference_okFunction · 0.85
iq4nl_reference_rowsFunction · 0.85
cpu_avx1_okFunction · 0.85
memcpyFunction · 0.85
dot_x4_modeFunction · 0.85

Tested by

no test coverage detected