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: multi-entry expert dots - the resident-expert kernels 2.3x faster on iq3_xxs layers
maxfridbe27ca717e7549
diff --git a/sycl/include/strata/sycl_math.hpp b/sycl/include/strata/sycl_math.hppnew file mode 100644index 000000000..58ccd2cae--- /dev/null+++ b/sycl/include/strata/sycl_math.hpp@@ -0,0 +1,15 @@+// include/strata/sycl_math.hpp - the SYCL port's device math that dpct emulates but the hardware has.+#pragma once+#include <sycl/sycl.hpp>+#include <dpct/dpct.hpp>+#include <cstdint>++namespace strata {+// CUDA's __dp4a(int, int, int): four signed byte products summed into c. One place to change it: the byte+// unpack + multiply-add form is what IGC pattern-matches into the DP4A instruction (sycl::ext::oneapi::dot_acc is+// the same emulation and its header defines non-inline functions, which breaks the link across TUs).+template <typename A, typename B, typename C>+inline int32_t dp4a(A a, B b, C c) {+ return dpct::dp4a((int32_t) a, (int32_t) b, (int32_t) c);+}+} // namespace stratadiff --git a/sycl/src/kernels/cuda/iq_kernels.dp.cpp b/sycl/src/kernels/cuda/iq_kernels.dp.cppindex 4e629c998..2c7eab329 100644--- a/sycl/src/kernels/cuda/iq_kernels.dp.cpp+++ b/sycl/src/kernels/cuda/iq_kernels.dp.cpp@@ -7,6 +7,7 @@#define DPCT_PROFILING_ENABLED#include <sycl/sycl.hpp>#include <dpct/dpct.hpp>+#include "strata/sycl_math.hpp"#include "strata/sycl_queue.hpp"#include "strata/kernels/iq_kernels.hpp"@@ -67,7 +68,7 @@ __dpct_inline__ sycl::int2 get_int_from_table_16(const int &q4,return sycl::int2(dpct::byte_level_permute(tmp[0], tmp[1], 0x6420),dpct::byte_level_permute(tmp[0], tmp[1], 0x7531));}-#define ggml_cuda_dp4a(a, b, c) dpct::dp4a((a), (b), (c))+#define ggml_cuda_dp4a(a, b, c) strata::dp4a((a), (b), (c))// ---------------------------------------------------------------- the dot products (vecdotq.cuh)__dpct_inline__ float vec_dot_q2_0_q8_1(const void *__restrict__ vbq,@@ -403,6 +404,32 @@ __dpct_inline__ float warp_sum(float v) {return v;}+// SYCL port: lanes per row for the grouped expert kernels. A 2560-wide row is 80 (block, part) calls; over 32+// lanes that is 2.5 calls each and a 5-step reduction - the work per sub-group is too small for the fixed cost.+// STRATA_EXPERT_LANES (compile time) lanes share a row; 256 / that many rows per work-group.+#ifndef STRATA_EXPERT_LANES+#define STRATA_EXPERT_LANES 8+#endif+constexpr int kExpertLanes = STRATA_EXPERT_LANES;+constexpr int kExpertRows = 256 / kExpertLanes;+template <int LANES>+__dpct_inline__ float lanes_sum(float v) {+#pragma unroll+ for (int o = LANES / 2; o > 0; o >>= 1)+ v += dpct::experimental::permute_sub_group_by_xor(0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(), v, o);+ return v;+}+template <int TY, int LANES>+__dpct_inline__ float row_dot_lanes(const uint8_t *row, const block_q8_1 *x, int nb, int sub) {+ using F = Fmt<TY>;+ float s = 0.0f;+ for (int k = sub; k < nb * F::ipb; k += LANES) {+ const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb);+ s += F::dot(row, x + kbx * (F::qk / 32), kbx, iqs);+ }+ return lanes_sum<LANES>(s);+}+// One row against one q8_1 activation, the whole warp: call k = (block, part) is lane-strided.template <int TY>__dpct_inline__ float row_dot(const uint8_t *row, const block_q8_1 *x, int nb,@@ -440,6 +467,213 @@ mmvq_kernel(const uint8_t *__restrict__ w, size_t row_bytes,}// ---------------------------------------------------------------- grouped native experts++// ---------------------------------------------------------------- SYCL port: multi-entry dots+//+// A window routes the same expert for several tokens (up to 6). The single-entry dot above recomputes the+// dequantised weights - grid lookups, sign unpacking, the byte-wise compare/xor/subtract emulations - once per+// token; only the dp4a against the activation and the scale differ. Multi<TY> splits each format into `prep`+// (weights, once) and `acts` + `finish` (per token). NW packed int32s of weights per call.+struct MultiW { int w[8]; int a = 0, b = 0; float d = 0.f; };++template <int TY> struct Multi { static constexpr bool has = false; static constexpr int NW = 0; };++template <> struct Multi<18> { // iq3_xxs+ static constexpr bool has = true; static constexpr int NW = 8;+ __dpct_inline__ static void prep(const void* vbq, int kbx, int iqs, MultiW& m) {+ const block_iq3_xxs* bq3 = (const block_iq3_xxs*) vbq + kbx;+ const sycl::int2 q3_packed = sycl::int2(get_int_b2(bq3->qs, iqs), get_int_b2(bq3->qs, iqs + 1));+ const uint8_t* q3 = (const uint8_t*) &q3_packed;+ const uint32_t aux32 = get_int_b2(bq3->qs, QK_K / 16 + iqs / 2);+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const sycl::int2 grid_pos = sycl::int2(iq3xxs_grid[q3[l0 + 0]], iq3xxs_grid[q3[l0 + 1]]);+ const uint32_t signs = unpack_ksigns(aux32 >> (7 * l0 / 2));+ const int signs0 = dpct::vectorized_binary<sycl::uchar4>(signs & 0x08040201, 0, std::not_equal_to<>());+ const int signs1 = dpct::vectorized_binary<sycl::uchar4>(signs & 0x80402010, 0, std::not_equal_to<>());+ m.w[l0 + 0] = dpct::vectorized_binary<sycl::uchar4>(grid_pos.x() ^ signs0, signs0, std::minus<>());+ m.w[l0 + 1] = dpct::vectorized_binary<sycl::uchar4>(grid_pos.y() ^ signs1, signs1, std::minus<>());+ }+ m.a = (int) (aux32 >> 28);+ m.d = sycl::vec<sycl::half, 1>(bq3->d).convert<float, sycl::rounding_mode::automatic>()[0];+ }+ __dpct_inline__ static void acts(const block_q8_1* y, int iqs, int* u, float& ds) {+#pragma unroll+ for (int j = 0; j < 8; ++j) u[j] = get_int_b4(y[iqs / 2].qs, j);+ ds = y[iqs / 2].ds[0];+ }+ __dpct_inline__ static float finish(int s0, int s1, const MultiW& m, float ds) {+ const int sumi = s0 + s1;+ return m.d * ds * (float) ((m.a * sumi + sumi / 2) / 2);+ }+};++template <> struct Multi<22> { // iq2_s+ static constexpr bool has = true; static constexpr int NW = 8;+ __dpct_inline__ static void prep(const void* vbq, int kbx, int iqs, MultiW& m) {+ const block_iq2_s* bq2 = (const block_iq2_s*) vbq + kbx;+ const int qs_packed = get_int_b2(bq2->qs, iqs / 2);+ const uint8_t* qs = (const uint8_t*) &qs_packed;+ const int qh = bq2->qh[iqs / 2];+ const int signs_packed_32 = get_int_b2(bq2->qs, QK_K / 32 + iqs / 2);+ const uint8_t* sp = (const uint8_t*) &signs_packed_32;+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const int* grid_pos = (const int*) (iq2s_grid + (qs[l0 / 2] | ((qh << (8 - l0)) & 0x300)));+ const int signs0 = dpct::vectorized_binary<sycl::uchar4>(((sp[l0 / 2] & 0x03) << 7) | ((sp[l0 / 2] & 0x0C) << 21), 0, std::not_equal_to<>());+ const int signs1 = dpct::vectorized_binary<sycl::uchar4>(((sp[l0 / 2] & 0x30) << 3) | ((sp[l0 / 2] & 0xC0) << 17), 0, std::not_equal_to<>());+ m.w[l0 + 0] = dpct::vectorized_binary<sycl::uchar4>(grid_pos[0] ^ signs0, signs0, std::minus<>());+ m.w[l0 + 1] = dpct::vectorized_binary<sycl::uchar4>(grid_pos[1] ^ signs1, signs1, std::minus<>());+ }+ m.a = bq2->scales[iqs / 2] & 0x0F;+ m.b = bq2->scales[iqs / 2] >> 4;+ m.d = sycl::vec<sycl::half, 1>(bq2->d).convert<float, sycl::rounding_mode::automatic>()[0];+ }+ __dpct_inline__ static void acts(const block_q8_1* y, int iqs, int* u, float& ds) {+#pragma unroll+ for (int j = 0; j < 8; ++j) u[j] = get_int_b4(y[iqs / 2].qs, j);+ ds = y[iqs / 2].ds[0];+ }+ __dpct_inline__ static float finish(int s0, int s1, const MultiW& m, float ds) {+ return m.d * ds * (float) ((s0 * m.a + s1 * m.b + (s0 + s1) / 2) / 4);+ }+};++template <> struct Multi<21> { // iq3_s+ static constexpr bool has = true; static constexpr int NW = 8;+ __dpct_inline__ static void prep(const void* vbq, int kbx, int iqs, MultiW& m) {+ const block_iq3_s* bq3 = (const block_iq3_s*) vbq + kbx;+ const sycl::int2 qs_packed = sycl::int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1));+ const uint8_t* qs = (const uint8_t*) &qs_packed;+ const int qh = bq3->qh[iqs / 2];+ const int signs_packed_32 = get_int_b2(bq3->signs, iqs / 2);+ const uint8_t* sp = (const uint8_t*) &signs_packed_32;+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const sycl::int2 grid_pos = sycl::int2(iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],+ iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);+ const int signs0 = dpct::vectorized_binary<sycl::uchar4>(((sp[l0 / 2] & 0x03) << 7) | ((sp[l0 / 2] & 0x0C) << 21), 0, std::not_equal_to<>());+ const int signs1 = dpct::vectorized_binary<sycl::uchar4>(((sp[l0 / 2] & 0x30) << 3) | ((sp[l0 / 2] & 0xC0) << 17), 0, std::not_equal_to<>());+ m.w[l0 + 0] = dpct::vectorized_binary<sycl::uchar4>(grid_pos.x() ^ signs0, signs0, std::minus<>());+ m.w[l0 + 1] = dpct::vectorized_binary<sycl::uchar4>(grid_pos.y() ^ signs1, signs1, std::minus<>());+ }+ m.a = 1 + 2 * ((bq3->scales[iqs / 4] >> ((iqs << 1) & 0x04)) & 0x0F);+ m.d = sycl::vec<sycl::half, 1>(bq3->d).convert<float, sycl::rounding_mode::automatic>()[0];+ }+ __dpct_inline__ static void acts(const block_q8_1* y, int iqs, int* u, float& ds) {+#pragma unroll+ for (int j = 0; j < 8; ++j) u[j] = get_int_b4(y[iqs / 2].qs, j);+ ds = y[iqs / 2].ds[0];+ }+ __dpct_inline__ static float finish(int s0, int s1, const MultiW& m, float ds) {+ return m.d * ds * (float) ((s0 + s1) * m.a);+ }+};++template <> struct Multi<20> { // iq4_nl (down)+ static constexpr bool has = true; static constexpr int NW = 4;+ __dpct_inline__ static void prep(const void* vbq, int kbx, int iqs, MultiW& m) {+ const block_iq4_nl* bq4 = (const block_iq4_nl*) vbq + kbx;+#pragma unroll+ for (int l = 0; l < 2; ++l) {+ const int aux_q4 = get_int_b2(bq4->qs, iqs + l);+ const sycl::int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl);+ m.w[2 * l + 0] = v.x();+ m.w[2 * l + 1] = v.y();+ }+ m.d = sycl::vec<sycl::half, 1>(bq4->d).convert<float, sycl::rounding_mode::automatic>()[0];+ }+ __dpct_inline__ static void acts(const block_q8_1* y, int iqs, int* u, float& ds) {+ const int* q8 = (const int*) y->qs + iqs;+#pragma unroll+ for (int l = 0; l < 2; ++l) { u[2 * l + 0] = q8[l + 0]; u[2 * l + 1] = q8[l + 4]; }+ ds = y->ds[0];+ }+ __dpct_inline__ static float finish(int s0, int s1, const MultiW& m, float ds) { return m.d * ds * (float) (s0 + s1); }+};++template <> struct Multi<42> { // q2_0 (down)+ static constexpr bool has = true; static constexpr int NW = 8;+ __dpct_inline__ static void prep(const void* vbq, int kbx, int iqs, MultiW& m) {+ const block_q2_0* bq2_0 = (const block_q2_0*) vbq + kbx;+ const int16_t* qs = (const int16_t*) bq2_0->qs + iqs * 4;+#pragma unroll+ for (int j = 0; j < 4; ++j) {+ const int q = qs[j];+ const int qe = dpct::byte_level_permute(0x020100FF, 0x020100FF, q >> 0);+ const int qo = dpct::byte_level_permute(0x020100FF, 0x020100FF, q >> 2);+ m.w[2 * j + 0] = dpct::byte_level_permute(qe, qo, 0x5140);+ m.w[2 * j + 1] = dpct::byte_level_permute(qe, qo, 0x7362);+ }+ m.d = bq2_0->d;+ }+ __dpct_inline__ static void acts(const block_q8_1* y, int iqs, int* u, float& ds) {+ const block_q8_1* chunk = y + iqs;+#pragma unroll+ for (int j = 0; j < 4; ++j) { u[2 * j + 0] = get_int_b4(chunk->qs, j * 2 + 0); u[2 * j + 1] = get_int_b4(chunk->qs, j * 2 + 1); }+ ds = chunk->ds[0];+ }+ __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.+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) {+ using F = Fmt<TY>;+ using M = Multi<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);+ MultiW m;+ M::prep(row, kbx, iqs, m);+#pragma unroll+ for (int e = 0; e < E; ++e) {+ int u[8];+ float ds;+ M::acts(xs[e] + kbx * (F::qk / 32), iqs, u, ds);+ int s0 = 0, s1 = 0;+#pragma unroll+ for (int j = 0; j < M::NW / 2; ++j) s0 = ggml_cuda_dp4a(m.w[j], u[j], s0);+#pragma unroll+ for (int j = M::NW / 2; j < M::NW; ++j) s1 = ggml_cuda_dp4a(m.w[j], u[j], s1);+ s[e] += M::finish(s0, s1, m, ds);+ }+ }+#pragma unroll+ for (int e = 0; e < E; ++e) out[e] = lanes_sum<LANES>(s[e]);+}++// 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>+__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) {+ int e = e0;+ if constexpr (Multi<TY>::has) {+ 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};+ float o[4];+ row_dot_multi<TY, LANES, 4>(wr, xs, nb, sub, o);+ if (sub == 0)+#pragma unroll+ for (int i = 0; i < 4; ++i) dst[(size_t) (e + i) * dst_stride] = o[i];+ }+ if (e + 2 <= e1) {+ const block_q8_1* xs[2] = {x + (size_t) ent_idx[e] * x_stride, x + (size_t) ent_idx[e + 1] * x_stride};+ float o[2];+ row_dot_multi<TY, LANES, 2>(wr, xs, nb, sub, o);+ if (sub == 0) { dst[(size_t) e * dst_stride] = o[0]; dst[(size_t) (e + 1) * dst_stride] = o[1]; }+ e += 2;+ }+ }+ for (; e < e1; ++e) {+ const float v = row_dot_lanes<TY, LANES>(wr, x + (size_t) ent_idx[e] * x_stride, nb, sub);+ if (sub == 0) dst[(size_t) e * dst_stride] = v;+ }+}+constexpr int GU_ROWS = 8; // rows per block (one warp each)template <int TG>@@ -451,9 +685,9 @@ __dpct_inline__ void native_gu_kernel(auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();const int g = item_ct1.get_group(1);if (g >= *n_groups) return;- const int warp = item_ct1.get_local_id(2) >> 5,- lane = item_ct1.get_local_id(2) & 31;- const int row = item_ct1.get_group(2) * GU_ROWS + warp; // 0 .. 2*n_ff+ const int rib = item_ct1.get_local_id(2) / kExpertLanes,+ sub = item_ct1.get_local_id(2) % kExpertLanes;+ const int row = item_ct1.get_group(2) * kExpertRows + rib; // 0 .. 2*n_ffif (row >= 2 * L.n_ff) return;const bool is_up = row >= L.n_ff;const int r = is_up ? row - (int) L.n_ff : row;@@ -461,10 +695,7 @@ __dpct_inline__ void native_gu_kernel(const uint8_t* wr = blob + (is_up ? L.up_off : 0) + (size_t) r * L.gu_row;const int nb = (int) (L.n_embd / Fmt<TG>::qk), xb = (int) (L.n_embd / 32);const int e0 = grp_start[g], e1 = grp_start[g + 1];- for (int e = e0; e < e1; ++e) {- const float s = row_dot<TG>(wr, xq + (size_t)ent_tok[e] * xb, nb, lane);- if (lane == 0) (is_up ? up : gate)[(size_t) e * L.n_ff + r] = s;- }+ row_entries<TG, kExpertLanes>(wr, xq, xb, ent_tok, e0, e1, nb, sub, (is_up ? up : gate) + r, (size_t) L.n_ff);}__dpct_inline__ void swiglu_entries_kernel(const float *__restrict__ gate,@@ -488,18 +719,38 @@ __dpct_inline__ void native_down_kernel(auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();const int g = item_ct1.get_group(1);if (g >= *n_groups) return;- const int warp = item_ct1.get_local_id(2) >> 5,- lane = item_ct1.get_local_id(2) & 31;- const int r = item_ct1.get_group(2) * 8 + warp;+ const int rib = item_ct1.get_local_id(2) / kExpertLanes,+ sub = item_ct1.get_local_id(2) % kExpertLanes;+ const int r = item_ct1.get_group(2) * kExpertRows + rib;if (r >= L.n_embd) return;const uint8_t* blob = (const uint8_t*) grp_ptr[g];const uint8_t* wr = blob + L.down_off + (size_t) r * L.d_row;const int nb = (int) (L.n_ff / Fmt<TD>::qk), hb = (int) (L.n_ff / 32);const int e0 = grp_start[g], e1 = grp_start[g + 1];- for (int e = e0; e < e1; ++e) {- const float s =- row_dot<TD>(wr, hq + (size_t)e * hb, nb, lane);- if (lane == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = s;+ // down: entry e's activation is row e of hq (identity index), its output row is ent_dst[e]+ {+ int e = e0;+ if constexpr (Multi<TD>::has) {+ 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];+ row_dot_multi<TD, kExpertLanes, 4>(wr, xs, nb, sub, o);+ if (sub == 0)+#pragma unroll+ for (int i = 0; i < 4; ++i) out[(size_t) ent_dst[e + i] * L.n_embd + r] = o[i];+ }+ if (e + 2 <= e1) {+ const block_q8_1* xs[2] = {hq + (size_t) e * hb, hq + (size_t) (e + 1) * hb};+ float o[2];+ row_dot_multi<TD, kExpertLanes, 2>(wr, xs, nb, sub, o);+ if (sub == 0) { out[(size_t) ent_dst[e] * L.n_embd + r] = o[0]; out[(size_t) ent_dst[e + 1] * L.n_embd + r] = o[1]; }+ e += 2;+ }+ }+ for (; e < e1; ++e) {+ const float v = row_dot_lanes<TD, kExpertLanes>(wr, hq + (size_t) e * hb, nb, sub);+ if (sub == 0) out[(size_t) ent_dst[e] * L.n_embd + r] = v;+ }}}@@ -1150,7 +1401,7 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long longfloat* h = (float*) ((uint8_t*) scratch + 2 * fa);block_q8_1* hq = (block_q8_1*) ((uint8_t*) scratch + 3 * fa);const auto* X = (const block_q8_1*) x_q8_1;- const dpct::dim3 ggu((unsigned)((2 * L.n_ff + GU_ROWS - 1) / GU_ROWS),+ const dpct::dim3 ggu((unsigned)((2 * L.n_ff + kExpertRows - 1) / kExpertRows),(unsigned)cap_groups);switch (L.gu_type) {case 16: {@@ -1334,7 +1585,7 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long longquantize_q8_1_kernel(h, hq, nh);});}- const dpct::dim3 gd((unsigned)((L.n_embd + 7) / 8), (unsigned)cap_groups);+ const dpct::dim3 gd((unsigned)((L.n_embd + kExpertRows - 1) / kExpertRows), (unsigned)cap_groups);switch (L.d_type) {case 20: {diff --git a/sycl/src/kernels/cuda/native_mmvq.dp.cpp b/sycl/src/kernels/cuda/native_mmvq.dp.cppindex b30b47230..7bce59c22 100644--- a/sycl/src/kernels/cuda/native_mmvq.dp.cpp+++ b/sycl/src/kernels/cuda/native_mmvq.dp.cpp@@ -26,6 +26,7 @@#define DPCT_PROFILING_ENABLED#include <sycl/sycl.hpp>#include <dpct/dpct.hpp>+#include "strata/sycl_math.hpp"#include "strata/sycl_queue.hpp"#include "strata/kernels/native_mmvq.hpp"#include "strata/kernels/iq_kernels.hpp"@@ -187,9 +188,9 @@ q5_q8_dot_impl(const int *__restrict__ vl, const int *__restrict__ vh,const int v0i = vl0i | vh0i;const int v1i = vl1i | vh1i;const int dot1 =- dpct::dp4a(v0i, u[2 * i], dpct::dp4a(v1i, u[2 * i + 1], 0));- const int dot2 = dpct::dp4a(0x01010101, u[2 * i],- dpct::dp4a(0x01010101, u[2 * i + 1], 0));+ strata::dp4a(v0i, u[2 * i], strata::dp4a(v1i, u[2 * i + 1], 0));+ const int dot2 = strata::dp4a(0x01010101, u[2 * i],+ strata::dp4a(0x01010101, u[2 * i + 1], 0));sumf_d += d8[i] * (dot1 * sc[i]);sumf_m += d8[i] * (dot2 * m[i]);}@@ -317,8 +318,8 @@ __dpct_inline__ float q2_q8_dot(const Q20Block *__restrict__ w,const int qo = dpct::byte_level_permute(0x020100ff, 0x020100ff, q >> 2);const int qx = dpct::byte_level_permute(qe, qo, 0x5140);const int qy = dpct::byte_level_permute(qe, qo, 0x7362);- sumi = dpct::dp4a(u, qx, sumi);- sumi = dpct::dp4a(v, qy, sumi);+ sumi = strata::dp4a(u, qx, sumi);+ sumi = strata::dp4a(v, qy, sumi);}const float d8 = chunk->ds[0];return d2 * d8 * sumi;@@ -405,7 +406,7 @@ __dpct_inline__ float q3_q8_dot_impl(int vl, int vh, const int *__restrict__ u,const int vih = ((vh >> i) << 2) & 0x04040404;const int vi =dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());- sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc);+ sumf += d8[i] * (strata::dp4a(vi, u[i], 0) * sc);}return d3 * sumf;}@@ -517,8 +518,8 @@ __dpct_inline__ float iq4_xs_q8_dot(const IQ4XSBlock *__restrict__ w,const sycl::int2 v = iq4_table_lookup(aux_q4, iq4nl_values);const int u0 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j];const int u1 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j + 4];- sumi = dpct::dp4a(v.x(), u0, sumi);- sumi = dpct::dp4a(v.y(), u1, sumi);+ sumi = strata::dp4a(v.x(), u0, sumi);+ sumi = strata::dp4a(v.y(), u1, sumi);}const int ls = ((w->scales_l[iqs / 8] >> (iqs & 0x04)) & 0x0f) |(((w->scales_h >> (iqs / 2)) & 0x03) << 4);@@ -593,9 +594,9 @@ q4_q8_dot_impl(const int *__restrict__ v, const int *__restrict__ u,const int v0i = (v[0] >> (4 * i)) & 0x0f0f0f0f;const int v1i = (v[1] >> (4 * i)) & 0x0f0f0f0f;const int dot1 =- dpct::dp4a(v1i, u[2 * i + 1], dpct::dp4a(v0i, u[2 * i], 0));- const int dot2 = dpct::dp4a(0x01010101, u[2 * i + 1],- dpct::dp4a(0x01010101, u[2 * i], 0));+ strata::dp4a(v1i, u[2 * i + 1], strata::dp4a(v0i, u[2 * i], 0));+ const int dot2 = strata::dp4a(0x01010101, u[2 * i + 1],+ strata::dp4a(0x01010101, u[2 * i], 0));sumf_d += d8[i] * (dot1 * sc[i]);sumf_m += d8[i] * (dot2 * m[i]);}@@ -707,7 +708,7 @@ __dpct_inline__ float q6_q8_dot_impl(int vl, int vh, const int *__restrict__ u,const int vih = ((vh >> (4 * i)) << 4) & 0x30303030;const int vi = dpct::vectorized_binary<sycl::char4>(vil | vih, 0x20202020, dpct::sub_sat());- sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc);+ sumf += d8[i] * (strata::dp4a(vi, u[i], 0) * sc);}return d * sumf;}@@ -793,9 +794,9 @@ __dpct_inline__ float small_q8_dot(const Q40Block *__restrict__ w,const int v = load_int_b2(w->qs, iqs + i);const int vi0 = (v >> 0) & 0x0f0f0f0f;const int vi1 = (v >> 4) & 0x0f0f0f0f;- sumi = dpct::dp4a(vi0, reinterpret_cast<const int *>(x->qs)[iqs + i],+ sumi = strata::dp4a(vi0, reinterpret_cast<const int *>(x->qs)[iqs + i],sumi);- sumi = dpct::dp4a(+ sumi = strata::dp4a(vi1, reinterpret_cast<const int *>(x->qs)[iqs + i + 4], sumi);}const sycl::float2 ds =@@ -817,14 +818,14 @@ __dpct_inline__ float small_q8_dot(const Q50Block *__restrict__ w,vi0 |= (vh << 11) & 0x00001000;vi0 |= (vh << 18) & 0x00100000;vi0 |= (vh << 25) & 0x10000000;- sumi = dpct::dp4a(vi0, reinterpret_cast<const int *>(x->qs)[iqs + i],+ sumi = strata::dp4a(vi0, reinterpret_cast<const int *>(x->qs)[iqs + i],sumi);int vi1 = (vl >> 4) & 0x0f0f0f0f;vi1 |= (vh >> 12) & 0x00000010;vi1 |= (vh >> 5) & 0x00001000;vi1 |= (vh << 2) & 0x00100000;vi1 |= (vh << 9) & 0x10000000;- sumi = dpct::dp4a(+ sumi = strata::dp4a(vi1, reinterpret_cast<const int *>(x->qs)[iqs + i + 4], sumi);}const sycl::float2 ds =@@ -841,7 +842,7 @@ __dpct_inline__ float small_q8_dot(const Q80Block *__restrict__ w,for (int i = 0; i < 2; ++i) {const int v = load_int_b2(w->qs, iqs + i);const int u = reinterpret_cast<const int*>(x->qs)[iqs + i];- sumi = dpct::dp4a(v, u, sumi);+ sumi = strata::dp4a(v, u, sumi);}const float d0 = w->d;const float d1 = x->ds[0];@@ -857,8 +858,8 @@ __dpct_inline__ float small_q8_dot(const IQ4NLBlock *__restrict__ w,for (int i = 0; i < 2; ++i) {const sycl::int2 v =iq4_table_lookup(load_int_b2(w->qs, iqs + i), iq4nl_values);- sumi = dpct::dp4a(v.x(), q8[i], sumi);- sumi = dpct::dp4a(v.y(), q8[i + 4], sumi);+ sumi = strata::dp4a(v.x(), q8[i], sumi);+ sumi = strata::dp4a(v.y(), q8[i + 4], sumi);}const float d = sycl::vec<sycl::half, 1>(w->d).convert<float, sycl::rounding_mode::automatic>()[0] *@@ -1041,8 +1042,8 @@ struct Q20Traits {int sumi = 0;#pragma unrollfor (int j = 0; j < 4; ++j) {- sumi = dpct::dp4a(q8[j * 2], r.qx[j], sumi);- sumi = dpct::dp4a(q8[j * 2 + 1], r.qy[j], sumi);+ sumi = strata::dp4a(q8[j * 2], r.qx[j], sumi);+ sumi = strata::dp4a(q8[j * 2 + 1], r.qy[j], sumi);}const float d8 = chunk->ds[0];return r.d2 * d8 * sumi;@@ -1123,8 +1124,8 @@ struct IQ4XSTraits {for (int j = 0; j < 4; ++j) {const int u0 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j];const int u1 = reinterpret_cast<const int*>(x[iqs / 4].qs)[j + 4];- sumi = dpct::dp4a(r.v[j].x(), u0, sumi);- sumi = dpct::dp4a(r.v[j].y(), u1, sumi);+ sumi = strata::dp4a(r.v[j].x(), u0, sumi);+ sumi = strata::dp4a(r.v[j].y(), u1, sumi);}sumi *= r.ls - 32;const float d = r.dw * x[iqs / 4].ds[0];diff --git a/sycl/src/kernels/cuda/s2_expert_grouped.dp.cpp b/sycl/src/kernels/cuda/s2_expert_grouped.dp.cppindex e2f8804cf..7b1e587ba 100644--- a/sycl/src/kernels/cuda/s2_expert_grouped.dp.cpp+++ b/sycl/src/kernels/cuda/s2_expert_grouped.dp.cpp@@ -16,6 +16,7 @@#define DPCT_PROFILING_ENABLED#include <sycl/sycl.hpp>#include <dpct/dpct.hpp>+#include "strata/sycl_math.hpp"#include "strata/sycl_queue.hpp"#include "strata/kernels/s2_expert_grouped.hpp"@@ -92,8 +93,8 @@ row_dot_s2_q8(const uint8_t *__restrict__ codes,// it rather than reasoning about which cast happens to work.int xw;memcpy(&xw, xq + 4 * j, 4);- s = dpct::dp4a(cw, xw, s);- hx = dpct::dp4a(ones, xw, hx);+ s = strata::dp4a(cw, xw, s);+ hx = strata::dp4a(ones, xw, hx);}// One weight scale per 64 elements, so per TWO 32-element chunks.const float dw = f16_at(scales + (size_t) (c >> 1) * 2);@@ -250,7 +251,7 @@ __dpct_inline__ int dot4(const uint8_t *codes, const int8_t *q) {(((c >> 4) & 3u) << 16) | (((c >> 6) & 3u) << 24));int xw;memcpy(&xw, q, sizeof xw);- return dpct::dp4a(cw, xw, 0);+ return strata::dp4a(cw, xw, 0);}__dpct_inline__ float row_dot_cpu_order(const uint8_t *codes,@@ -885,8 +886,8 @@ __dpct_inline__ float chunk_dot(sycl::uint2 cb, const int *xw, float dw,const unsigned cbyte = cbytes[j];const int cw = (int) ((cbyte & 3u) | (((cbyte >> 2) & 3u) << 8) | (((cbyte >> 4) & 3u) << 16) |(((cbyte >> 6) & 3u) << 24));- s = dpct::dp4a(cw, xw[j], s);- hx = dpct::dp4a(ones, xw[j], hx);+ s = strata::dp4a(cw, xw[j], s);+ hx = strata::dp4a(ones, xw[j], hx);}return dw * dx * (float) (s - hx);}diff --git a/sycl/src/kernels/native_expert_parity.cpp b/sycl/src/kernels/native_expert_parity.cppindex 5552aceb0..2b064f29f 100644--- a/sycl/src/kernels/native_expert_parity.cpp+++ b/sycl/src/kernels/native_expert_parity.cpp@@ -246,6 +246,45 @@ int main(int argc, char** argv) {strata::kernels::native_expert_grouped(L, dptr, dstart, dn, ddst, dtok, 1, NT, dxq, dscr, dout, s);s->wait();s->memcpy(got_g.data(), dout, got_g.size() * 4).wait();+ if (std::getenv("NATIVE_BENCH") != nullptr) { // SYCL port: one layer's worth of hits, timed+ // G distinct experts (copies of this blob at different addresses), E entries each = a T-token window+ const int G = 10, E = 4, NE = G * E, iters = 20;+ std::vector<void*> blobs((size_t) G);+ std::vector<unsigned long long> ptrs((size_t) G);+ for (int g = 0; g < G; ++g) {+ blobs[(size_t) g] = sycl::malloc_device(blob.size(), *s);+ s->memcpy(blobs[(size_t) g], blob.data(), blob.size()).wait();+ ptrs[(size_t) g] = (unsigned long long) blobs[(size_t) g];+ }+ std::vector<int32_t> starts((size_t) G + 1), dst((size_t) NE), tokv((size_t) NE);+ for (int g = 0; g <= G; ++g) starts[(size_t) g] = g * E;+ for (int e = 0; e < NE; ++e) { dst[(size_t) e] = e; tokv[(size_t) e] = e % NT; }+ unsigned long long* bptr = sycl::malloc_device<unsigned long long>((size_t) G, *s);+ int32_t* bstart = sycl::malloc_device<int32_t>((size_t) G + 1, *s);+ int32_t* bn = sycl::malloc_device<int32_t>(1, *s);+ int32_t* bdst = sycl::malloc_device<int32_t>((size_t) NE, *s);+ int32_t* btok = sycl::malloc_device<int32_t>((size_t) NE, *s);+ void* bscr = sycl::malloc_device(strata::kernels::native_expert_scratch_bytes(NE, FF), *s);+ float* bout = sycl::malloc_device<float>((size_t) NE * H, *s);+ const int32_t gn = G;+ s->memcpy(bptr, ptrs.data(), (size_t) G * 8).wait();+ s->memcpy(bstart, starts.data(), ((size_t) G + 1) * 4).wait();+ s->memcpy(bn, &gn, 4).wait();+ s->memcpy(bdst, dst.data(), (size_t) NE * 4).wait();+ s->memcpy(btok, tokv.data(), (size_t) NE * 4).wait();+ strata::kernels::native_expert_grouped(L, bptr, bstart, bn, bdst, btok, G, NE, dxq, bscr, bout, s);+ s->wait();+ const auto b0 = std::chrono::steady_clock::now();+ for (int i = 0; i < iters; ++i)+ strata::kernels::native_expert_grouped(L, bptr, bstart, bn, bdst, btok, G, NE, dxq, bscr, bout, s);+ s->wait();+ const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - b0).count() / iters;+ std::printf(" GPU grouped: %d experts x %d entries: %.3f ms per layer call (%.1f GB/s of expert bytes)\n",+ G, E, ms, (double) G * (double) blob.size() / ms / 1e6);+ for (int g = 0; g < G; ++g) sycl::free(blobs[(size_t) g], *s);+ sycl::free(bptr, *s); sycl::free(bstart, *s); sycl::free(bn, *s); sycl::free(bdst, *s); sycl::free(btok, *s);+ sycl::free(bscr, *s); sycl::free(bout, *s);+ }sycl::free(dblob, *s); sycl::free(dx, *s); sycl::free(dxq, *s); sycl::free(dscr, *s); sycl::free(dout, *s); sycl::free(dptr, *s);sycl::free(dstart, *s); sycl::free(dn, *s); sycl::free(ddst, *s); sycl::free(dtok, *s);}diff --git a/sycl/tools/fixups.py b/sycl/tools/fixups.pyindex 96a5300ea..4d7d50928 100644--- a/sycl/tools/fixups.py+++ b/sycl/tools/fixups.py@@ -118,6 +118,16 @@ def wait_default_queue_copies(s):for rel in all_sources():edit(str(rel), wait_default_queue_copies)+# 7e. dpct::dp4a is emulation (eight integer ops); sycl::ext::oneapi::dot_acc is the DP4A instruction on Xe.+def native_dp4a(s):+ if "dpct::dp4a(" not in s: return s+ s = s.replace("dpct::dp4a(", "strata::dp4a(")+ if "sycl_math.hpp" not in s:+ s = s.replace("#include <dpct/dpct.hpp>\n", '#include <dpct/dpct.hpp>\n#include "strata/sycl_math.hpp"\n', 1)+ return s+for rel in all_sources():+ edit(str(rel), native_dp4a)+# 8. ggml-common.h has a SYCL declaration mode (sycl::half instead of cuda_fp16.h).edit("src/kernels/cuda/iq_kernels.dp.cpp", lambda s: s.replace("#define GGML_COMMON_DECL_CUDA", "#define GGML_COMMON_DECL_SYCL").replace("#define GGML_COMMON_IMPL_CUDA", "#define GGML_COMMON_IMPL_SYCL"))