Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
SYCL: Unsloth's expert formats decode each weight word once per grouped call (KqSplit), bitwise
Niko1221e65858f84f9f
diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txtindex 15ef03dfc..316838b2b 100644--- a/sycl/CMakeLists.txt+++ b/sycl/CMakeLists.txt@@ -383,5 +383,8 @@ if(STRATA_SYCL_PARITY)add_executable(native_expert_parity ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/native_expert_parity.cpp)target_include_directories(native_expert_parity PRIVATE ${STRATA_ROOT}/third_party/ggml)target_link_libraries(native_expert_parity PRIVATE strata_kernels strata_kernels_cpu)+ # MODEL-UPDATE: the CPU pool's expert rows per format against the RAM bandwidth (as the top-level build's)+ add_executable(kq_cpu_bench ${STRATA_ROOT}/src/kernels/cpu/kq_cpu_bench.cpp)+ target_link_libraries(kq_cpu_bench PRIVATE strata_kernels_cpu)endif()endif()diff --git a/sycl/src/kernels/cuda/iq_kernels.dp.cpp b/sycl/src/kernels/cuda/iq_kernels.dp.cppindex f67de0d7a..564201390 100644--- a/sycl/src/kernels/cuda/iq_kernels.dp.cpp+++ b/sycl/src/kernels/cuda/iq_kernels.dp.cpp@@ -1751,10 +1751,188 @@ template <> struct Multi<42> { // q2_0 (down)__dpct_inline__ static float finish(int s0, int s1, const MultiW& m, float ds) { return m.d * ds * (float) (s0 + s1); }};-// One row against E activations at once, LANES lanes per row.+// MODEL-UPDATE (Intel): Unsloth's formats in the lane kernels' multi-entry path. Each Fmt<TY>::dot of the K-quants,+// Q5_1 and Q8_0 split into `load` (every read of the weight block and what is computed from it alone) and `apply`+// (the activation reads and the rest of the dot), the same statements in the same order as vec_dot_*_q8_1 above, so+// apply(load(...)) is the dot's value bit for bit; a group's 2-4 entries then read and decode each weight word once+// instead of once per entry. Every entry keeps its own lane-strided k order and lanes_sum: the outputs are bitwise+// those of row_dot_lanes (native_grouped_parity --kq checks it). STRATA_KQ_SPLIT=0: the per-entry kernels.+template <int TY> struct KqSplit { static constexpr bool has = false; };+template <> struct KqSplit<12> { // Q4_K (vec_dot_q4_K_q8_1)+ static constexpr bool has = true;+ struct W { int v[2]; uint16_t aux[2]; sycl::half2 dm; };+ __dpct_inline__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {+ const block_q4_K* b = (const block_q4_K*) vbq + kbx;+ W r;+ const int bq8_offset = QR4_K * ((iqs / 2) / (QI8_1 / 2));+ const int* q4 = (const int*) (b->qs + 16 * bq8_offset + 4 * ((iqs / 2) % 4));+ r.v[0] = q4[0];+ r.v[1] = q4[4];+ k_scale_min(b->scales, bq8_offset, r.aux);+ r.dm = b->dm;+ return r;+ }+ __dpct_inline__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {+ const int bq8_offset = QR4_K * ((iqs / 2) / (QI8_1 / 2));+ int u[2 * QR4_K];+ float d8[QR4_K];+#pragma unroll+ for (int i = 0; i < QR4_K; ++i) {+ const block_q8_1* bq8i = bq8_1 + bq8_offset + i;+ d8[i] = bq8i->ds[0];+ const int* q8 = (const int*) bq8i->qs + ((iqs / 2) % 4);+ u[2 * i + 0] = q8[0];+ u[2 * i + 1] = q8[4];+ }+ const uint8_t* sc = (const uint8_t*) r.aux;+ const uint8_t* m = sc + 2;+ return vec_dot_q4_K_q8_1_impl_vmmq(r.v, u, sc, m, r.dm, d8);+ }+};+template <> struct KqSplit<13> { // Q5_K (vec_dot_q5_K_q8_1)+ static constexpr bool has = true;+ struct W { int vl[2]; int vh[2]; uint16_t aux[2]; sycl::half2 dm; };+ __dpct_inline__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {+ const block_q5_K* b = (const block_q5_K*) vbq + kbx;+ W r;+ const int bq8_offset = QR5_K * ((iqs / 2) / (QI8_1 / 2));+ const int* ql = (const int*) (b->qs + 16 * bq8_offset + 4 * ((iqs / 2) % 4));+ const int* qh = (const int*) (b->qh + 4 * ((iqs / 2) % 4));+ r.vl[0] = ql[0];+ r.vl[1] = ql[4];+ r.vh[0] = qh[0] >> bq8_offset;+ r.vh[1] = qh[4] >> bq8_offset;+ k_scale_min(b->scales, bq8_offset, r.aux);+ r.dm = b->dm;+ return r;+ }+ __dpct_inline__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {+ const int bq8_offset = QR5_K * ((iqs / 2) / (QI8_1 / 2));+ int u[2 * QR5_K];+ float d8[QR5_K];+#pragma unroll+ for (int i = 0; i < QR5_K; ++i) {+ const block_q8_1* bq8i = bq8_1 + bq8_offset + i;+ d8[i] = bq8i->ds[0];+ const int* q8 = (const int*) bq8i->qs + ((iqs / 2) % 4);+ u[2 * i + 0] = q8[0];+ u[2 * i + 1] = q8[4];+ }+ const uint8_t* sc = (const uint8_t*) r.aux;+ const uint8_t* m = sc + 2;+ return vec_dot_q5_K_q8_1_impl_vmmq(r.vl, r.vh, u, sc, m, r.dm, d8);+ }+};+template <> struct KqSplit<14> { // Q6_K (vec_dot_q6_K_q8_1)+ static constexpr bool has = true;+ struct W { int vl, vh; int8_t sc[5]; float d; };+ __dpct_inline__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {+ const block_q6_K* bq6_K = (const block_q6_K*) vbq + kbx;+ const int scale_offset = (QI6_K / 4) * (iqs / (QI6_K / 2)) + (iqs % (QI6_K / 2)) / (QI6_K / 8);+ const int vh_shift = 2 * ((iqs % (QI6_K / 2)) / (QI6_K / 4));+ W r;+ r.vl = get_int_b2(bq6_K->ql, iqs);+ r.vh = get_int_b2(bq6_K->qh, (QI6_K / 4) * (iqs / (QI6_K / 2)) + iqs % (QI6_K / 4)) >> vh_shift;+ const int8_t* scales = bq6_K->scales + scale_offset;+ r.sc[0] = scales[0];+ r.sc[4] = scales[4]; // the impl reads scales[4 * i], i < QR6_K = 2+ r.d = sycl::vec<sycl::half, 1>(bq6_K->d).convert<float, sycl::rounding_mode::automatic>()[0];+ return r;+ }+ __dpct_inline__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {+ const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K / 2)) + (iqs % (QI6_K / 2)) / (QI6_K / 4);+ int u[QR6_K];+ float d8[QR6_K];+#pragma unroll+ for (int i = 0; i < QR6_K; ++i) {+ u[i] = get_int_b4(bq8_1[bq8_offset + 2 * i].qs, iqs % QI8_1);+ d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];+ }+ return vec_dot_q6_K_q8_1_impl_mmvq(r.vl, r.vh, u, r.sc, r.d, d8);+ }+};+template <> struct KqSplit<7> { // Q5_1 (vec_dot_q5_1_q8_1)+ static constexpr bool has = true;+ struct W { int vl[VDR_Q5_1]; int qh; sycl::half2 dm; };+ __dpct_inline__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {+ const block_q5_1* b = (const block_q5_1*) vbq + kbx;+ W r;+#pragma unroll+ for (int i = 0; i < VDR_Q5_1; ++i) r.vl[i] = get_int_b4(b->qs, iqs + i);+ r.qh = get_int_b4(b->qh, 0);+ r.dm = b->dm;+ return r;+ }+ __dpct_inline__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {+ int sumi = 0, sumu = 0;+#pragma unroll+ for (int i = 0; i < VDR_Q5_1; ++i) {+ const int vl = r.vl[i];+ const int vh = r.qh >> (4 * (iqs + i));+ const int u0 = get_int_b4(bq8_1->qs, iqs + i), u1 = get_int_b4(bq8_1->qs, iqs + i + QI5_1);+ int vi0 = (vl >> 0) & 0x0F0F0F0F;+ vi0 |= (vh << 4) & 0x00000010;+ vi0 |= (vh << 11) & 0x00001000;+ vi0 |= (vh << 18) & 0x00100000;+ vi0 |= (vh << 25) & 0x10000000;+ sumi = ggml_cuda_dp4a(vi0, u0, sumi);+ int vi1 = (vl >> 4) & 0x0F0F0F0F;+ vi1 |= (vh >> 12) & 0x00000010;+ vi1 |= (vh >> 5) & 0x00001000;+ vi1 |= (vh << 2) & 0x00100000;+ vi1 |= (vh << 9) & 0x10000000;+ sumi = ggml_cuda_dp4a(vi1, u1, sumi);+ sumu = ggml_cuda_dp4a(0x01010101, u1, ggml_cuda_dp4a(0x01010101, u0, sumu));+ }+ const sycl::float2 dm5 = (r.dm).template convert<float, sycl::rounding_mode::automatic>();+ const float d8 = bq8_1->ds[0];+ return sumi * (dm5.x() * d8) + sumu * (dm5.y() * d8);+ }+};+template <> struct KqSplit<8> { // Q8_0 (vec_dot_q8_0_q8_1)+ static constexpr bool has = true;+ struct W { int q[VDR_Q8_0]; float d; };+ __dpct_inline__ static W load(const void* __restrict__ vbq, int kbx, int iqs) {+ const block_q8_0* b = (const block_q8_0*) vbq + kbx;+ W r;+#pragma unroll+ for (int i = 0; i < VDR_Q8_0; ++i) r.q[i] = get_int_b2(b->qs, iqs + i);+ r.d = sycl::vec<sycl::half, 1>(b->d).convert<float, sycl::rounding_mode::automatic>()[0];+ return r;+ }+ __dpct_inline__ static float apply(const W& r, const block_q8_1* __restrict__ bq8_1, int iqs) {+ int sumi = 0;+#pragma unroll+ for (int i = 0; i < VDR_Q8_0; ++i) sumi = ggml_cuda_dp4a(r.q[i], get_int_b4(bq8_1->qs, iqs + i), sumi);+ const float d8_1 = bq8_1->ds[0];+ return r.d * d8_1 * ((float) sumi);+ }+};+// the lane kernels' multi-entry path: the IQ formats' Multi always, the K-quant splits unless KM is false+template <int TY, bool KM> inline constexpr bool kEntryMulti = Multi<TY>::has || (KM && KqSplit<TY>::has);++// One row against E activations at once, LANES lanes per row: the K-quant / Q5_1 / Q8_0 split (KqSplit).template <int TY, int LANES, int E>-__dpct_inline__ void row_dot_multi(const uint8_t* row, const block_q8_1* const* xs, int nb, int sub, float* out,- const void* grid = nullptr) {+__dpct_inline__ void row_dot_ksplit(const uint8_t* row, const block_q8_1* const* xs, int nb, int sub, float* out) {+ using F = Fmt<TY>;+ using K = KqSplit<TY>;+ float s[E];+#pragma unroll+ for (int e = 0; e < E; ++e) s[e] = 0.f;+ for (int k = sub; k < nb * F::ipb; k += LANES) {+ const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);+ const typename K::W w = K::load(row, kbx, iqs);+#pragma unroll+ for (int e = 0; e < E; ++e) s[e] += K::apply(w, xs[e] + kbx * (F::qk / 32), iqs);+ }+#pragma unroll+ for (int e = 0; e < E; ++e) out[e] = lanes_sum<LANES>(s[e]);+}++// One row against E activations at once, LANES lanes per row: the IQ formats' Multi.+template <int TY, int LANES, int E>+__dpct_inline__ void row_dot_multi_iq(const uint8_t* row, const block_q8_1* const* xs, int nb, int sub, float* out,+ const void* grid = nullptr) {using F = Fmt<TY>;using M = Multi<TY>;float s[E];@@ -1781,13 +1959,20 @@ __dpct_inline__ void row_dot_multi(const uint8_t* row, const block_q8_1* const*for (int e = 0; e < E; ++e) out[e] = lanes_sum<LANES>(s[e]);}+template <int TY, int LANES, int E>+__dpct_inline__ void row_dot_multi(const uint8_t* row, const block_q8_1* const* xs, int nb, int sub, float* out,+ const void* grid = nullptr) {+ if constexpr (KqSplit<TY>::has) row_dot_ksplit<TY, LANES, E>(row, xs, nb, sub, out);+ else row_dot_multi_iq<TY, LANES, E>(row, xs, nb, sub, out, grid);+}+// The entries e0..e1 of one row: chunks of 4 through the multi-entry dot, the rest one at a time.-template <int TY, int LANES>+template <int TY, int LANES, bool KM = true>__dpct_inline__ void row_entries(const uint8_t* wr, const block_q8_1* x, int x_stride, const int32_t* ent_idx,int e0, int e1, int nb, int sub, float* dst, size_t dst_stride,const void* grid = nullptr) {int e = e0;- if constexpr (Multi<TY>::has) {+ if constexpr (kEntryMulti<TY, KM>) {for (; e + 4 <= e1; e += 4) {const block_q8_1* xs[4] = {x + (size_t) ent_idx[e] * x_stride, x + (size_t) ent_idx[e + 1] * x_stride,x + (size_t) ent_idx[e + 2] * x_stride, x + (size_t) ent_idx[e + 3] * x_stride};@@ -1813,7 +1998,7 @@ __dpct_inline__ void row_entries(const uint8_t* wr, const block_q8_1* x, int x_sconstexpr int GU_ROWS = 8; // rows per block (one warp each)-template <int TG, int LN = kExpertLanes>+template <int TG, int LN = kExpertLanes, bool KM = true>__dpct_inline__ void native_gu_kernel(const unsigned long long *__restrict__ grp_ptr,const int32_t *__restrict__ grp_start, const int32_t *__restrict__ n_groups,@@ -1850,7 +2035,7 @@ __dpct_inline__ void native_gu_kernel(for (int g = (int) item_ct1.get_group(1); g < ng; g += (int) item_ct1.get_group_range(1)) {const uint8_t* wr = (const uint8_t*) grp_ptr[g] + off;const int e0 = grp_start[g], e1 = grp_start[g + 1];- row_entries<TG, LN>(wr, xq, xb, ent_tok, e0, e1, nb, sub, (is_up ? up : gate) + r, (size_t) L.n_ff, grid);+ row_entries<TG, LN, KM>(wr, xq, xb, ent_tok, e0, e1, nb, sub, (is_up ? up : gate) + r, (size_t) L.n_ff, grid);}}@@ -1982,7 +2167,7 @@ __dpct_inline__ void swiglu_entries_kernel(const float *__restrict__ gate,h[i] = (g / (1.0f + sycl::native::exp(-g))) * up[i];}-template <int TD, int LN = kExpertLanes>+template <int TD, int LN = kExpertLanes, bool KM = true>__dpct_inline__ void native_down_kernel(const unsigned long long *__restrict__ grp_ptr,const int32_t *__restrict__ grp_start, const int32_t *__restrict__ n_groups,@@ -2001,7 +2186,7 @@ __dpct_inline__ void native_down_kernel(const uint8_t* wr = (const uint8_t*) grp_ptr[g] + off;const int e0 = grp_start[g], e1 = grp_start[g + 1];int e = e0;- if constexpr (Multi<TD>::has) {+ if constexpr (kEntryMulti<TD, KM>) {for (; e + 4 <= e1; e += 4) {const block_q8_1* xs[4] = {hq + (size_t) e * hb, hq + (size_t) (e + 1) * hb, hq + (size_t) (e + 2) * hb, hq + (size_t) (e + 3) * hb};float o[4];@@ -4107,52 +4292,77 @@ inline int lanes_env(const char* name) {}inline int gu_lanes() { static const int v = lanes_env("STRATA_GU_LANES"); return v; }inline int down_lanes() { static const int v = lanes_env("STRATA_DOWN_LANES"); return v; }-template <int TG, int LN>+// STRATA_KQ_GU_LANES (opt-in): the lane split of the K-quant / Q8_0 gate/up rows alone (Unsloth's files), default STRATA_GU_LANES.+// B70, 4-token windows, split on: 16 lanes reads Q4_K/Q5_1 experts at 439 GB/s against 426 at 8; the reduction order differs+// (other bits), so it stays opt-in.+inline int kq_gu_lanes() { static const int v = std::getenv("STRATA_KQ_GU_LANES") ? lanes_env("STRATA_KQ_GU_LANES") : gu_lanes(); return v; }+template <int TG, int LN, bool KM = true>void launch_gu_port(unsigned groups, dpct::queue_ptr s, const unsigned long long *grp_ptr, const int32_t *grp_start,const int32_t *n_groups, const int32_t *ent_tok, const block_q8_1 *X,const NativeExpertLayout &L, float *gate, float *up) {constexpr int ROWS = 256 / LN;const unsigned gx = (unsigned) ((2 * L.n_ff + ROWS - 1) / ROWS);auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync};- s->parallel_for<dpct_kernel_name<class native_gu_port, dpct_kernel_scalar<TG>, dpct_kernel_scalar<LN>>>(+ s->parallel_for<dpct_kernel_name<class native_gu_port, dpct_kernel_scalar<TG>, dpct_kernel_scalar<LN>, dpct_kernel_scalar<KM>>>(sycl::nd_range<3>(sycl::range<3>(1, groups, (size_t) gx * 256), sycl::range<3>(1, 1, 256)), exp_props,[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {- native_gu_kernel<TG, LN>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);+ native_gu_kernel<TG, LN, KM>(grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);});}+// STRATA_KQ_SPLIT=0 (or native_expert_set_kq_split(false)): the K-quant / Q5_1 / Q8_0 lane kernels without the+// multi-entry split (one weight read per entry, as before); the outputs are the same bits either way+bool g_kq_split = [] { const char* v = std::getenv("STRATA_KQ_SPLIT"); return !(v && v[0] == '0'); }();+template <int TG, int LN>+void gu_port_km(unsigned groups, dpct::queue_ptr s, const unsigned long long *grp_ptr, const int32_t *grp_start,+ const int32_t *n_groups, const int32_t *ent_tok, const block_q8_1 *X, const NativeExpertLayout &L,+ float *gate, float *up) {+ if constexpr (KqSplit<TG>::has) {+ if (!g_kq_split) { launch_gu_port<TG, LN, false>(groups, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); return; }+ }+ launch_gu_port<TG, LN, true>(groups, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up);+}template <int TG>void launch_gu_lanes(dpct::dim3 grid, dpct::queue_ptr s, const unsigned long long *grp_ptr, const int32_t *grp_start,const int32_t *n_groups, const int32_t *ent_tok, const block_q8_1 *X,const NativeExpertLayout &L, float *gate, float *up) {- switch (gu_lanes()) {- case 4: launch_gu_port<TG, 4>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 16: launch_gu_port<TG, 16>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- case 32: launch_gu_port<TG, 32>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;- default: launch_gu_port<TG, 8>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;+ switch (KqSplit<TG>::has ? kq_gu_lanes() : gu_lanes()) {+ case 4: gu_port_km<TG, 4>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;+ case 16: gu_port_km<TG, 16>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;+ case 32: gu_port_km<TG, 32>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;+ default: gu_port_km<TG, 8>(grid.y, s, grp_ptr, grp_start, n_groups, ent_tok, X, L, gate, up); break;}}-template <int TD, int LN>+template <int TD, int LN, bool KM = true>void launch_down_port(unsigned groups, dpct::queue_ptr s, const unsigned long long *grp_ptr, const int32_t *grp_start,const int32_t *n_groups, const int32_t *ent_dst, const block_q8_1 *hq,const NativeExpertLayout &L, float *out) {constexpr int ROWS = 256 / LN;const unsigned gx = (unsigned) ((L.n_embd + ROWS - 1) / ROWS);auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync};- s->parallel_for<dpct_kernel_name<class native_down_port, dpct_kernel_scalar<TD>, dpct_kernel_scalar<LN>>>(+ s->parallel_for<dpct_kernel_name<class native_down_port, dpct_kernel_scalar<TD>, dpct_kernel_scalar<LN>, dpct_kernel_scalar<KM>>>(sycl::nd_range<3>(sycl::range<3>(1, groups, (size_t) gx * 256), sycl::range<3>(1, 1, 256)), exp_props,[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {- native_down_kernel<TD, LN>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);+ native_down_kernel<TD, LN, KM>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);});}+template <int TD, int LN>+void down_port_km(unsigned groups, dpct::queue_ptr s, const unsigned long long *grp_ptr, const int32_t *grp_start,+ const int32_t *n_groups, const int32_t *ent_dst, const block_q8_1 *hq, const NativeExpertLayout &L,+ float *out) {+ if constexpr (KqSplit<TD>::has) {+ if (!g_kq_split) { launch_down_port<TD, LN, false>(groups, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); return; }+ }+ launch_down_port<TD, LN, true>(groups, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out);+}template <int TD>void launch_down_lanes(dpct::dim3 grid, dpct::queue_ptr s, const unsigned long long *grp_ptr, const int32_t *grp_start,const int32_t *n_groups, const int32_t *ent_dst, const block_q8_1 *hq,const NativeExpertLayout &L, float *out) {switch (down_lanes()) {- case 4: launch_down_port<TD, 4>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;- case 16: launch_down_port<TD, 16>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;- case 32: launch_down_port<TD, 32>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;- default: launch_down_port<TD, 8>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;+ case 4: down_port_km<TD, 4>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;+ case 16: down_port_km<TD, 16>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;+ case 32: down_port_km<TD, 32>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;+ default: down_port_km<TD, 8>(grid.y, s, grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break;}}@@ -4225,6 +4435,7 @@ void launch_down(dpct::dim3 grid, dpct::queue_ptr s,} // namespacevoid iq_set_old_kernels(bool old) { g_old_kernels = old; }+void native_expert_set_kq_split(bool on) { g_kq_split = on; }bool iq_old_kernels() { return g_old_kernels; }bool iq_supported(int t) noexcept { return is_iq(t); }diff --git a/sycl/src/kernels/dequant_bench.cpp b/sycl/src/kernels/dequant_bench.cppindex 1e0127f99..5311690d4 100644--- a/sycl/src/kernels/dequant_bench.cpp+++ b/sycl/src/kernels/dequant_bench.cpp@@ -15,7 +15,9 @@namespace {struct Ty { int id; const char* name; int bytes_per_256; };const Ty kTypes[] = {{16, "IQ2_XXS", 66}, {17, "IQ2_XS", 74}, {22, "IQ2_S", 82}, {18, "IQ3_XXS", 98},- {21, "IQ3_S", 110}, {20, "IQ4_NL", 144}, {42, "Q2_0", 72}};+ {21, "IQ3_S", 110}, {20, "IQ4_NL", 144}, {42, "Q2_0", 72},+ // Unsloth's files (MODEL-UPDATE): gate/up Q4_K / Q5_K / Q6_K / Q8_0, down Q5_1 / Q8_0+ {12, "Q4_K", 144}, {13, "Q5_K", 176}, {14, "Q6_K", 210}, {7, "Q5_1", 192}, {8, "Q8_0", 272}};uint64_t fnv(const uint16_t* p, size_t n) {uint64_t h = 1469598103934665603ull;for (size_t i = 0; i < n; ++i) { h ^= p[i]; h *= 1099511628211ull; }diff --git a/sycl/src/kernels/native_grouped_parity.cpp b/sycl/src/kernels/native_grouped_parity.cppindex bad8a2b8b..60a61a3c7 100644--- a/sycl/src/kernels/native_grouped_parity.cpp+++ b/sycl/src/kernels/native_grouped_parity.cpp@@ -27,6 +27,11 @@#include <string>#include <vector>+namespace strata::kernels {+// SYCL port only (iq_kernels.dp.cpp): the K-quant / Q5_1 / Q8_0 lane kernels with (true, the default) or without the+// multi-entry split; STRATA_KQ_SPLIT=0 at startup is false+void native_expert_set_kq_split(bool on);+}namespace k = strata::kernels;namespace {@@ -85,7 +90,7 @@ std::vector<uint8_t> random_rows(int t, int64_t rows, int64_t n, std::mt19937& r// (Q4_K / Q5_K: 2^-14 .. 2^-11, their 6-bit sub-scales multiply it by up to 63)const int e = (t == 12 || t == 13) ? 1 + ex(rng) % 4 : ex(rng);const uint16_t h = (uint16_t) ((sgn(rng) ? 0x8000 : 0) | (e << 10) | man(rng));- std::memcpy(&w[o], &h, 2);+ std::memcpy(&w[o + (t == 14 ? 208 : 0)], &h, 2); // Q6_K keeps its fp16 scale last (ql, qh, scales, d)if (t == 12 || t == 13 || t == 7) { // Q4_K / Q5_K: dmin, Q5_1: m - the second fp16, as smallconst int em = (t == 7) ? ex(rng) : 1 + ex(rng) % 4;const uint16_t m = (uint16_t) ((sgn(rng) ? 0x8000 : 0) | (em << 10) | man(rng));@@ -109,6 +114,10 @@ struct Setup {size_t out_floats = 0;unsigned long long* ptr = nullptr;int32_t *start = nullptr, *n = nullptr, *dst = nullptr, *tok = nullptr;+ std::vector<std::vector<uint8_t>> hb; // host copies: the blobs, the q8_1 activations, the last plan+ std::vector<uint8_t> h_xq;+ std::vector<int> h_blob;+ std::vector<int32_t> h_start, h_tok, h_dst;Setup(int gu, int dt, int64_t H, int64_t FF, int tokens, int k_per_token,int blobs_n, std::mt19937 &rng, dpct::queue_ptr s) {@@ -121,6 +130,7 @@ struct Setup {auto blob = random_rows(gu, 2 * FF, H, rng);const auto down = random_rows(dt, H, FF, rng);blob.insert(blob.end(), down.begin(), down.end());+ hb.push_back(blob);if (blob.size() != L.bytes) { std::fprintf(stderr, "blob %zu != %zu bytes\n", blob.size(), L.bytes); std::exit(2); }/*DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,@@ -147,6 +157,8 @@ struct Setup {"x");xq = dalloc<uint8_t>((size_t) T * (H / 32) * 36);k::quantize_q8_1_rows(dx, T, H, xq, s);+ h_xq.resize((size_t) T * (H / 32) * 36);+ s->memcpy(h_xq.data(), xq, h_xq.size()).wait();ck(DPCT_CHECK_ERROR(s->wait()), "quantize");sycl::free(dx, dpct::get_in_order_queue());scr_bytes = k::native_expert_scratch_bytes(cap, FF);@@ -192,7 +204,9 @@ struct Setup {std::shuffle(order.begin(), order.end(), rng);std::vector<unsigned long long> p;std::vector<int32_t> st(1, base), tk_v, ds(cap);+ h_blob.clear();for (const int m : sizes) {+ h_blob.push_back(order[p.size() % order.size()]);p.push_back((unsigned long long) (blobs + slot * (size_t) order[p.size() % order.size()]));st.push_back(st.back() + m);}@@ -201,6 +215,7 @@ struct Setup {std::iota(ds.begin(), ds.end(), 0);std::shuffle(ds.begin(), ds.end(), rng); // scattered rows of `out`for (int e = 0; e < cap; ++e) tk_v.push_back(tk(rng));+ h_start = st; h_tok = tk_v; h_dst = ds; // host copies for the CPU reference/*DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,assuming in the original code the source host memory is pageable memory.@@ -299,6 +314,162 @@ void check(int gu, int dt, int64_t H, int64_t FF, dpct::queue_ptr s,g_fail += bad;}+// ------------------------------------------------------------------------------------------------ --kq+// MODEL-UPDATE (Intel): the K-quant / Q5_1 / Q8_0 multi-entry split (native_expert_set_kq_split) against the per-entry+// kernels, bitwise; and both against a CPU reference in float: ggml's dequantize_row_* of the weights, the q8_1+// activations' own values (d x q), SwiGLU in float (the GPU re-quantizes h to q8_1 for the down product, the reference+// does not: that rounding is the tolerance, 3e-2 of the output's largest magnitude).+float h2f(const uint8_t* p) {+ uint16_t h;+ std::memcpy(&h, p, 2);+ return (float) sycl::bit_cast<sycl::half>(h);+}+// ggml-quants.c dequantize_row_* (one block)+void dq_block(int t, const uint8_t* b, float* y) {+ switch (t) {+ case 8: { // Q8_0: d, qs[32]+ const float d = h2f(b);+ for (int i = 0; i < 32; ++i) y[i] = d * (float) (int8_t) b[2 + i];+ break;+ }+ case 7: { // Q5_1: d, m, qh[4], qs[16]+ const float d = h2f(b), m = h2f(b + 2);+ uint32_t qh;+ std::memcpy(&qh, b + 4, 4);+ const uint8_t* qs = b + 8;+ for (int j = 0; j < 16; ++j) {+ const uint8_t xh0 = ((qh >> j) << 4) & 0x10, xh1 = ((qh >> (j + 12))) & 0x10;+ y[j] = (float) ((qs[j] & 0x0F) | xh0) * d + m;+ y[j + 16] = (float) ((qs[j] >> 4) | xh1) * d + m;+ }+ break;+ }+ case 12: case 13: { // Q4_K: d, dmin, scales[12], qs[128]; Q5_K: d, dmin, scales[12], qh[32], qs[128]+ const float d = h2f(b), dmin = h2f(b + 2);+ const uint8_t* sc = b + 4;+ const uint8_t* qh = b + 16;+ const uint8_t* ql = t == 12 ? b + 16 : b + 48;+ auto smk = [&](int j, uint8_t& s, uint8_t& m) {+ if (j < 4) { s = sc[j] & 63; m = sc[j + 4] & 63; }+ else { s = (sc[j + 4] & 0xF) | ((sc[j - 4] >> 6) << 4); m = (sc[j + 4] >> 4) | ((sc[j] >> 6) << 4); }+ };+ int is = 0;+ uint8_t u1 = 1, u2 = 2;+ for (int j = 0; j < 256; j += 64) {+ uint8_t s, m;+ smk(is, s, m);+ const float d1 = d * s, m1 = dmin * m;+ smk(is + 1, s, m);+ const float d2 = d * s, m2 = dmin * m;+ for (int l = 0; l < 32; ++l)+ *y++ = d1 * (float) ((ql[l] & 0xF) + (t == 13 && (qh[l] & u1) ? 16 : 0)) - m1;+ for (int l = 0; l < 32; ++l)+ *y++ = d2 * (float) ((ql[l] >> 4) + (t == 13 && (qh[l] & u2) ? 16 : 0)) - m2;+ ql += 32; is += 2; u1 <<= 2; u2 <<= 2;+ }+ break;+ }+ case 14: { // Q6_K: ql[128], qh[64], scales[16], d+ const uint8_t* ql = b;+ const uint8_t* qh = b + 128;+ const int8_t* sc = (const int8_t*) (b + 192);+ const float d = h2f(b + 208);+ for (int n = 0; n < 256; n += 128) {+ for (int l = 0; l < 32; ++l) {+ const int is = l / 16;+ const int q1 = (int8_t) ((ql[l] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;+ const int q2 = (int8_t) ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;+ const int q3 = (int8_t) ((ql[l] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;+ const int q4 = (int8_t) ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;+ y[l] = d * sc[is] * q1;+ y[l + 32] = d * sc[is + 2] * q2;+ y[l + 64] = d * sc[is + 4] * q3;+ y[l + 96] = d * sc[is + 6] * q4;+ }+ y += 128; ql += 64; qh += 32; sc += 8;+ }+ break;+ }+ default: std::fprintf(stderr, "dq_block: type %d\n", t); std::exit(2);+ }+}+std::vector<float> dq_rows(int t, const uint8_t* w, int64_t rows, int64_t n) {+ const int bv = block_values(t);+ const size_t bs = k::iq_row_bytes(t, bv);+ std::vector<float> y((size_t) rows * n);+ for (size_t b = 0; b < y.size() / bv; ++b) dq_block(t, w + b * bs, &y[b * bv]);+ return y;+}+// the CPU reference of the last plan's call: out rows ent_dst[e] (n_embd floats each)+std::vector<float> reference(const Setup& S) {+ const int64_t H = S.L.n_embd, FF = S.L.n_ff;+ std::vector<float> ref(S.out_floats, std::nanf(""));+ std::vector<float> x(H);+ for (size_t g = 0; g + 1 < S.h_start.size(); ++g) {+ const auto& blob = S.hb[S.h_blob[g]];+ const auto G = dq_rows(S.L.gu_type, blob.data(), FF, H);+ const auto U = dq_rows(S.L.gu_type, blob.data() + S.L.up_off, FF, H);+ const auto D = dq_rows(S.L.d_type, blob.data() + S.L.down_off, H, FF);+ for (int e = S.h_start[g]; e < S.h_start[g + 1]; ++e) {+ const uint8_t* xb = &S.h_xq[(size_t) S.h_tok[e] * (H / 32) * 36];+ for (int64_t i = 0; i < H; ++i) x[i] = h2f(xb + (i / 32) * 36) * (float) (int8_t) xb[(i / 32) * 36 + 4 + i % 32];+ std::vector<float> h(FF);+ for (int64_t r = 0; r < FF; ++r) {+ double gs = 0, us = 0;+ for (int64_t i = 0; i < H; ++i) { gs += (double) G[r * H + i] * x[i]; us += (double) U[r * H + i] * x[i]; }+ h[r] = (float) (gs / (1.0 + std::exp(-gs)) * us);+ }+ for (int64_t r = 0; r < H; ++r) {+ double s = 0;+ for (int64_t i = 0; i < FF; ++i) s += (double) D[r * FF + i] * h[i];+ ref[(size_t) S.h_dst[e] * H + r] = (float) s;+ }+ }+ }+ return ref;+}+void check_kq(int gu, int dt, int64_t H, int64_t FF, dpct::queue_ptr s, std::mt19937& rng) {+ Setup S(gu, dt, H, FF, 6, 4, 9, rng, s); // groups of 1..6 entries: the split's chunks of 4 and 2 and the rest+ int calls = 0, bad = 0;+ double worst = 0;+ for (const int groups : {1, 2, 3, 5, 8}) {+ const int ng = S.plan(groups, 0, rng);+ k::native_expert_set_kq_split(false);+ const auto a = S.result(false, 0, 0x00, s);+ k::native_expert_set_kq_split(true);+ const auto b = S.result(false, 0, 0x7F, s);+ ++calls;+ size_t diff = 0;+ for (size_t i = 0; i < a.size(); ++i) diff += a[i] != b[i];+ if (diff) {+ std::printf(" %-6s/%-6s %d groups: split vs per-entry: %zu of %zu floats differ FAIL\n", name_of(gu), name_of(dt),+ ng, diff, a.size());+ ++bad;+ }+ const auto ref = reference(S);+ double amax = 0, emax = 0;+ size_t n = 0;+ for (size_t i = 0; i < ref.size(); ++i) {+ if (std::isnan(ref[i])) continue;+ float g;+ std::memcpy(&g, &b[i], 4);+ amax = std::max(amax, (double) std::fabs(ref[i]));+ emax = std::max(emax, (double) std::fabs(g - ref[i]));+ ++n;+ }+ const double rel = amax > 0 ? emax / amax : 1.0;+ worst = std::max(worst, rel);+ if (n == 0 || !(rel < 3e-2)) {+ std::printf(" %-6s/%-6s %d groups: GPU vs CPU reference: max err %.3g of %.3g (%zu values) FAIL\n", name_of(gu),+ name_of(dt), ng, emax, amax, n);+ ++bad;+ }+ }+ std::printf("%-6s/%-6s %5lld x %4lld %d calls: split %s, CPU reference worst %.2e of the largest output\n", name_of(gu),+ name_of(dt), (long long) H, (long long) FF, calls, bad ? "FAIL" : "bitwise equal to per-entry", worst);+ g_fail += bad;+}+// ------------------------------------------------------------------------------------------------ --bench// A window's 48 calls, one per layer, each with ITS OWN experts as the layers have: copies of the Setup's blobs in one// arena, so the weights stream from DRAM as in the engine. (48 calls re-reading the same 28 experts - 73 MB, about an@@ -440,13 +611,13 @@ void bench(dpct::queue_ptr s, std::mt19937 &rng) {dpct::destroy_event(e1);}-// ------------------------------------------------------------------------------------------------ --kqbench [NL]+// ------------------------------------------------------------------------------------------------ --kqbench [NL [host]]// Unsloth's formats against the IQ packs' at the model's shapes (2560 x 640): microseconds per grouped call and the// expert bytes it reads per microsecond. NL layers (default 16), each with its own experts, so the weights stream from// DRAM; a call reads each of its groups' whole blobs (gate + up + down) once. Shapes: one token (10 groups of 1), a// window of 2 tokens (16 groups, 20 entries) and of 4 (28 groups, 40 entries; native_grouped_parity's --bench).// STRATA_GU_LANES / STRATA_DOWN_LANES pick the lane split as in the engine.-void kq_bench(dpct::queue_ptr s, std::mt19937& rng, int nl) {+void kq_bench(dpct::queue_ptr s, std::mt19937& rng, int nl, bool host) {const int pairs[][2] = {{21, 20}, {23, 20}, {18, 42}, {12, 7}, {12, 8}, {13, 8},#ifdef STRATA_Q6K_EXPERTS{14, 8},@@ -458,12 +629,15 @@ void kq_bench(dpct::queue_ptr s, std::mt19937& rng, int nl) {const struct { const char* what; int T; const std::vector<int>* sizes; } shapes[] = {{"1 token, 10x1", 1, &w1}, {"2 tokens, 16 groups / 20", 2, &w2}, {"4 tokens, 28 groups / 40", 4, &w4}};dpct::event_ptr e0 = new sycl::event(), e1 = new sycl::event();- std::printf("\n--kqbench: %d layers per graph, each its own experts; us per call (median of 40), expert MB per call, GB/s\n", nl);+ std::printf("\n--kqbench: %d layers per graph, each its own experts (%s); us per call (median of 40), expert MB per call, GB/s\n",+ nl, host ? "pinned host memory" : "device memory");for (const auto& pr : pairs) {for (const auto& sh : shapes) {Setup S(pr[0], pr[1], 2560, 640, sh.T, 10, 28, rng, s);const int ng = S.plan_sizes(*sh.sizes, 0, rng);- uint8_t* arena = dalloc<uint8_t>(S.slot * (size_t) (nl * ng));+ // host: the arena in pinned host memory, read by the kernels over PCIe (the B70's mirrored experts)+ uint8_t* arena = host ? (uint8_t*) sycl::malloc_host(S.slot * (size_t) (nl * ng) + 256, dpct::get_in_order_queue())+ : dalloc<uint8_t>(S.slot * (size_t) (nl * ng));std::vector<unsigned long long*> lp;for (int l = 0; l < nl; ++l) {std::vector<unsigned long long> p;@@ -510,10 +684,18 @@ int main(int argc, char** argv) {"stream");std::mt19937 rng(316);if (argc > 1 && std::string(argv[1]) == "--kqbench") { // timings only (the checks: run without arguments)- kq_bench(s, rng, argc > 2 ? std::atoi(argv[2]) : 16);+ kq_bench(s, rng, argc > 2 ? std::atoi(argv[2]) : 16, argc > 3 && std::string(argv[3]) == "host");dpct::get_current_device().destroy_queue(s);return 0;}+ if (argc > 1 && std::string(argv[1]) == "--kq") { // the K-quant split and the CPU reference only+ const int pairs[][2] = {{12, 7}, {12, 8}, {13, 8}, {14, 8}, {8, 8}, {13, 7}};+ for (const auto& pr : pairs) check_kq(pr[0], pr[1], 2560, 640, s, rng); // the model's shapes+ for (const auto& pr : pairs) check_kq(pr[0], pr[1], 512, 256, s, rng);+ std::printf("native_grouped_parity --kq: %d failures\n", g_fail);+ dpct::get_current_device().destroy_queue(s);+ return g_fail ? 1 : 0;+ }for (int gu : {16, 17, 18, 21, 22, 23, 29, 42, 12, 13,#ifdef STRATA_Q6K_EXPERTS14,