Skip to content

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: clean exit, per-kernel profiling, Q6_K unpack hoisted; measured: the mmvq kernels run at 110-130 GB/s warm

maxfridbe2eab02c23193
diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt
index 2214bb01f..5b260f1e9 100644
--- a/sycl/CMakeLists.txt
+++ b/sycl/CMakeLists.txt
@@ -175,6 +175,8 @@ if(STRATA_SYCL_PARITY)
strata_resolve(_src src/kernels/qsa_prompt_attn_parity.cpp)
add_executable(qsa_prompt_attn_parity ${_src})
target_link_libraries(qsa_prompt_attn_parity PRIVATE strata_kernels)
+ add_executable(mmvq_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/mmvq_bench.cpp) # dense mmvq timing
+ target_link_libraries(mmvq_bench PRIVATE strata_kernels)
# one native expert three ways on real GGUF rows (ggml reference, CPU path, GPU native_expert_grouped):
# native_expert_parity <shard1.gguf> [layer ...]
if(STRATA_NATIVE_EXPERTS)
diff --git a/sycl/src/kernels/cuda/native_mmvq.dp.cpp b/sycl/src/kernels/cuda/native_mmvq.dp.cpp
index 7bce59c22..cc78c0c40 100644
--- a/sycl/src/kernels/cuda/native_mmvq.dp.cpp
+++ b/sycl/src/kernels/cuda/native_mmvq.dp.cpp
@@ -32,6 +32,7 @@
#include "strata/kernels/iq_kernels.hpp"
#include <cstdint>
+#include <cstdlib>
#include <limits>
#include <stdexcept>
#include <string>
@@ -482,8 +483,7 @@ __dpct_inline__ void native_q3_k_mmvq_kernel(const Q3KBlock *__restrict__ w,
// The pinned nonlinear IQ4 codebook and its CUDA two-stage byte lookup. The
// explicit alignment satisfies the four 32-bit table loads; values are unchanged.
-inline dpct::global_memory<int8_t, 1>
- iq4nl_values(sycl::range(16), {-127, -104, -83, -65, -49, -35, -22, -10, 1,
+inline dpct::global_memory<int8_t, 1>& iq4nl_values = *new dpct::global_memory<int8_t, 1>(sycl::range(16), {-127, -104, -83, -65, -49, -35, -22, -10, 1,
13, 25, 38, 53, 69, 89, 113});
static constexpr int8_t kIq4nlTable[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
@@ -1079,27 +1079,51 @@ struct Q6KTraits {
using Block = Q6KBlock;
static constexpr int DIV = 256, T = 32, KBY = 8, BPI = WARPS * WARP / 32;
static int kqs(int tid) { return tid % 32; }
- struct W { int vl, vh; float d; const int8_t* scales; int bq8_offset; };
+ // SYCL port: the weight unpack (shifts, masks, the saturating byte subtract) is column-independent, so it is
+ // done once in load(); apply() is two dp4a and two fmas per column.
+ struct W { int vi[2]; float dsc[2]; int bq8_offset; };
static W load(const Block* __restrict__ w, int iqs) {
W r;
r.bq8_offset = 4 * (iqs / 16) + (iqs % 16) / 8;
const int scale_offset = 8 * (iqs / 16) + (iqs % 16) / 4;
const int vh_shift = 2 * ((iqs % 16) / 8);
- r.vl = load_int_b2(w->ql, iqs);
- r.vh = load_int_b2(w->qh, 8 * (iqs / 16) + iqs % 8) >> vh_shift;
- r.scales = w->scales + scale_offset;
- r.d = w->d;
+ const int vl = load_int_b2(w->ql, iqs);
+ const int vh = load_int_b2(w->qh, 8 * (iqs / 16) + iqs % 8) >> vh_shift;
+ const float d = w->d;
+#pragma unroll
+ for (int i = 0; i < 2; ++i) {
+ const int vil = (vl >> (4 * i)) & 0x0f0f0f0f;
+ const int vih = ((vh >> (4 * i)) << 4) & 0x30303030;
+ r.vi[i] = dpct::vectorized_binary<sycl::char4>(vil | vih, 0x20202020, dpct::sub_sat());
+ r.dsc[i] = d * (float) w->scales[scale_offset + 4 * i];
+ }
return r;
}
static float apply(const W& r, const Q81Block* __restrict__ x, int iqs) {
- int u[2];
- float d8[2];
+ float sumf = 0.0f;
+#pragma unroll
+ for (int i = 0; i < 2; ++i) {
+ const int u = reinterpret_cast<const int*>(x[r.bq8_offset + 2 * i].qs)[iqs % 8];
+ sumf += x[r.bq8_offset + 2 * i].ds[0] * r.dsc[i] * (float) strata::dp4a(r.vi[i], u, 0);
+ }
+ return sumf;
+ }
+ // SYCL port, row-blocked kernel: the activation's part for this (block, lane), loaded once per column and
+ // reused across the warp's rows.
+ struct A { int u[2]; float ds[2]; };
+ static A acts(const Q81Block* __restrict__ x, int iqs) {
+ A a;
+ const int bq8_offset = 4 * (iqs / 16) + (iqs % 16) / 8;
#pragma unroll
for (int i = 0; i < 2; ++i) {
- u[i] = reinterpret_cast<const int*>(x[r.bq8_offset + 2 * i].qs)[iqs % 8];
- d8[i] = x[r.bq8_offset + 2 * i].ds[0];
+ a.u[i] = reinterpret_cast<const int*>(x[bq8_offset + 2 * i].qs)[iqs % 8];
+ a.ds[i] = x[bq8_offset + 2 * i].ds[0];
}
- return q6_q8_dot_impl(r.vl, r.vh, u, r.scales, r.d, d8);
+ return a;
+ }
+ static float dot(const W& r, const A& a) {
+ return a.ds[0] * r.dsc[0] * (float) strata::dp4a(r.vi[0], a.u[0], 0) +
+ a.ds[1] * r.dsc[1] * (float) strata::dp4a(r.vi[1], a.u[1], 0);
}
};
struct IQ4XSTraits {
@@ -1233,6 +1257,61 @@ native_mmvq_multi_kernel(const typename F::Block *__restrict__ w,
}
}
+// ---------------------------------------------------------------- SYCL port: row-blocked mmvq
+//
+// Measured on the B70 (mmvq_bench): the shared kernel above spends its time re-reading the activation - every
+// row's work-group loads all NCOLS columns of it again, and a 4-warp work-group per row ends in a barrier and an
+// SLM reduction for ten blocks of work. Here each warp owns RPW rows outright: per 256-block it loads the
+// activation parts once for all columns and the RPW weight parts once, does RPW x NCOLS dots, and reduces its
+// own lanes at the end. No barrier, no SLM. Only for traits with acts()/dot() (Q6_K so far).
+template <typename F, int NCOLS, int RPW>
+void native_mmvq_rowwarp_kernel(const typename F::Block* __restrict__ w, const Q81Block* __restrict__ x,
+ float* __restrict__ y, int n_in, int n_out) {
+ auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
+ const int lane = int(item.get_local_id(2)), warp = int(item.get_local_id(1));
+ const int row0 = (int(item.get_group(2)) * WARPS + warp) * RPW;
+ const int blocks_per_row = n_in / F::DIV, x_stride = n_in / Q8K;
+ const int kqs = F::kqs(lane);
+ float tmp[RPW][NCOLS] = {};
+ for (int kbx = lane / F::T; kbx < blocks_per_row; kbx += WARP / F::T) {
+ const int kby = kbx * F::KBY;
+ typename F::A a[NCOLS];
+#pragma unroll
+ for (int j = 0; j < NCOLS; ++j) a[j] = F::acts(x + std::size_t(j) * x_stride + kby, kqs);
+#pragma unroll
+ for (int r = 0; r < RPW; ++r) {
+ if (row0 + r >= n_out) break;
+ const typename F::W wv = F::load(w + std::size_t(row0 + r) * blocks_per_row + kbx, kqs);
+#pragma unroll
+ for (int j = 0; j < NCOLS; ++j) tmp[r][j] += F::dot(wv, a[j]);
+ }
+ }
+ auto sg = item.get_sub_group();
+#pragma unroll
+ for (int r = 0; r < RPW; ++r)
+#pragma unroll
+ for (int j = 0; j < NCOLS; ++j) {
+ float v = tmp[r][j];
+#pragma unroll
+ for (int o = WARP / 2; o > 0; o >>= 1) v += dpct::experimental::permute_sub_group_by_xor(0xffffffffu, sg, v, o);
+ if (lane == 0 && row0 + r < n_out) y[std::size_t(j) * n_out + row0 + r] = v;
+ }
+}
+
+template <typename F, int NCOLS, int RPW>
+void launch_rowwarp(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, dpct::queue_ptr s) {
+ const auto* w = static_cast<const typename F::Block*>(weights);
+ const auto* x = static_cast<const Q81Block*>(x_q8_1);
+ const unsigned blocks = unsigned((std::size_t(n_out) + WARPS * RPW - 1) / (WARPS * RPW));
+ s->parallel_for<dpct_kernel_name<class native_mmvq_rowwarp, F, dpct_kernel_scalar<NCOLS>, dpct_kernel_scalar<RPW>>>(
+ sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)),
+ [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_rowwarp_kernel<F, NCOLS, RPW>(w, x, y, n_in, n_out); });
+}
+inline int rowwarp_rpw() { // STRATA_MMVQ_RPW: 0 = the shared kernel, 1/2/4 = rows per warp (bench knob)
+ static const int v = std::getenv("STRATA_MMVQ_RPW") ? std::atoi(std::getenv("STRATA_MMVQ_RPW")) : 0; // default: the shared kernel (measured equal at warm clocks)
+ return v;
+}
+
template <typename F, int NCOLS>
void launch_multi_n(const void *weights, const void *x_q8_1, float *y, int n_in,
int n_out, dpct::queue_ptr s) {
@@ -1240,7 +1319,8 @@ void launch_multi_n(const void *weights, const void *x_q8_1, float *y, int n_in,
const auto* x = static_cast<const Q81Block*>(x_q8_1);
if (!g_multi_exact) {
constexpr int NW = NCOLS <= 4 ? 4 : 2;
- const unsigned blocks = unsigned((std::size_t(n_out) + 1) / 2);
+ constexpr int ROWS = 1; // SYCL port: one row per work-group (more groups: the kernel is latency-bound here)
+ const unsigned blocks = unsigned((std::size_t(n_out) + ROWS - 1) / ROWS);
/*
DPCT1049:207: The work-group size passed to the SYCL kernel may exceed
the limit. To get the device limit, query
@@ -1261,8 +1341,8 @@ void launch_multi_n(const void *weights, const void *x_q8_1, float *y, int n_in,
exp_props,
[=](sycl::nd_item<3> item_ct1)
[[sycl::reqd_sub_group_size(32)]] {
- native_mmvq_multi_kernel<F, NCOLS, NW, 2>(w, x, y, n_in,
- n_out);
+ native_mmvq_multi_kernel<F, NCOLS, NW, ROWS>(w, x, y, n_in,
+ n_out);
});
}
return;
@@ -1884,6 +1964,16 @@ void native_q4_k_f32(const void* weights, const float* x, void* scratch_q8_1,
void native_q6_k_mmvq(const void* weights, const void* x_q8_1, float* y,
int n_in, int n_out, int ncols, void* stream) {
+ // SYCL port: the row-blocked kernel for the decode shapes (ncols <= 4)
+ if (ncols >= 1 && ncols <= 4 && rowwarp_rpw() > 0 && n_in % 256 == 0) {
+ dpct::queue_ptr s = strata::q_of(stream);
+ const int rpw = rowwarp_rpw();
+#define STRATA_RW(NC, RP) if (ncols == NC && rpw == RP) { launch_rowwarp<Q6KTraits, NC, RP>(weights, x_q8_1, y, n_in, n_out, s); return; }
+ STRATA_RW(1, 1) STRATA_RW(1, 2) STRATA_RW(1, 4) STRATA_RW(2, 1) STRATA_RW(2, 2) STRATA_RW(2, 4)
+ STRATA_RW(3, 1) STRATA_RW(3, 2) STRATA_RW(3, 4) STRATA_RW(4, 1) STRATA_RW(4, 2) STRATA_RW(4, 4)
+#undef STRATA_RW
+ }
+
validate_shape(n_in, ncols, 256);
if (n_out <= 0) throw std::invalid_argument("native MMVQ requires n_out > 0");
validate_pointer(weights);
diff --git a/sycl/src/kernels/cuda/s2_gemv_fast.dp.cpp b/sycl/src/kernels/cuda/s2_gemv_fast.dp.cpp
index 4db6c40a7..4014fdfb6 100644
--- a/sycl/src/kernels/cuda/s2_gemv_fast.dp.cpp
+++ b/sycl/src/kernels/cuda/s2_gemv_fast.dp.cpp
@@ -41,7 +41,7 @@ constexpr int QK_S2 = 64;
constexpr int MAX_SHARED_HALVES = 4096; // 8 KB of shared for x; n_embd 2560 fits with room
// byte -> the four code values with the -1 bias already applied, in element order (bits 0,2,4,6).
-inline dpct::constant_memory<float, 2> c_codes(256, 4);
+inline dpct::constant_memory<float, 2>& c_codes = *new dpct::constant_memory<float, 2>(256, 4); // never freed: exit-order safe
bool g_lut_ready[64] = {}; // per device: __constant__ memory is per device (a layer split runs on two)
diff --git a/sycl/src/kernels/mmvq_bench.cpp b/sycl/src/kernels/mmvq_bench.cpp
new file mode 100644
index 000000000..828b3bae7
--- /dev/null
+++ b/sycl/src/kernels/mmvq_bench.cpp
@@ -0,0 +1,39 @@
+// sycl/src/kernels/mmvq_bench.cpp - SYCL port: time the dense native_mmvq kernels on random weights.
+// mmvq_bench [n_in] [n_out] [ncols] (weights are random bytes: dequant cost is data-independent)
+#include "strata/kernels/native_mmvq.hpp"
+#include <sycl/sycl.hpp>
+#include <dpct/dpct.hpp>
+#include <chrono>
+#include <cstdio>
+#include <cstdlib>
+#include <vector>
+int main(int argc, char** argv) {
+ const int n_in = argc > 1 ? std::atoi(argv[1]) : 2560, n_out = argc > 2 ? std::atoi(argv[2]) : 2560, ncols = argc > 3 ? std::atoi(argv[3]) : 4;
+ sycl::queue* s = &dpct::get_in_order_queue();
+ // Q6_K: 210 bytes per 256 weights
+ const size_t wbytes = (size_t) n_out * (n_in / 256) * 210;
+ std::vector<uint8_t> hw(wbytes);
+ for (size_t i = 0; i < wbytes; ++i) hw[i] = (uint8_t) (i * 2654435761u >> 13);
+ std::vector<float> hx((size_t) ncols * n_in);
+ for (size_t i = 0; i < hx.size(); ++i) hx[i] = (float) ((i * 7919) % 1000) / 500.f - 1.f;
+ void* w = sycl::malloc_device(wbytes, *s);
+ float* x = sycl::malloc_device<float>(hx.size(), *s);
+ void* xq = sycl::malloc_device((size_t) ncols * (n_in / 32) * 36, *s);
+ float* y = sycl::malloc_device<float>((size_t) ncols * n_out, *s);
+ s->memcpy(w, hw.data(), wbytes).wait(); s->memcpy(x, hx.data(), hx.size() * 4).wait();
+ strata::kernels::native_quantize_q8_1(x, xq, n_in, ncols, s);
+ { // warm up until the clocks are up (~300 ms of work), then time
+ const auto w0 = std::chrono::steady_clock::now();
+ while (std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - w0).count() < 300) {
+ for (int i = 0; i < 20; ++i) strata::kernels::native_q6_k_mmvq(w, xq, y, n_in, n_out, ncols, s);
+ s->wait();
+ }
+ }
+ const int it = 400;
+ const auto t0 = std::chrono::steady_clock::now();
+ for (int i = 0; i < it; ++i) strata::kernels::native_q6_k_mmvq(w, xq, y, n_in, n_out, ncols, s);
+ s->wait();
+ const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / it;
+ std::printf("q6_k mmvq %d x %d, %d cols: %.1f us (%.1f GB/s of weights)\n", n_in, n_out, ncols, us, wbytes / us / 1e3);
+ return 0;
+}
diff --git a/sycl/tools/fixups.py b/sycl/tools/fixups.py
index 4d7d50928..f46dc1507 100644
--- a/sycl/tools/fixups.py
+++ b/sycl/tools/fixups.py
@@ -128,6 +128,15 @@ def native_dp4a(s):
for rel in all_sources():
edit(str(rel), native_dp4a)
+# 7f. dpct's static global_memory/constant_memory objects destruct after the default queue at exit and segfault
+# in queue::get_device(). Heap-allocate them and never delete: the process is exiting anyway.
+edit("src/kernels/cuda/native_mmvq.dp.cpp", lambda s: s.replace(
+ "inline dpct::global_memory<int8_t, 1>\n iq4nl_values(sycl::range(16), {",
+ "inline dpct::global_memory<int8_t, 1>& iq4nl_values = *new dpct::global_memory<int8_t, 1>(sycl::range(16), {"))
+edit("src/kernels/cuda/s2_gemv_fast.dp.cpp", lambda s: s.replace(
+ "inline dpct::constant_memory<float, 2> c_codes(256, 4);",
+ "inline dpct::constant_memory<float, 2>& c_codes = *new dpct::constant_memory<float, 2>(256, 4); // never freed: exit-order safe"))
+
# 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"))