Merge perf-PR group sycl (f-145-pp-sycl)

This commit is contained in:
Niko1221 committed 2026-10-16 10:43:56 +02:00
commit 926b0bbe6d
8 files changed
+487 -14

No files matched your search

+18
View File
@@ -481,6 +481,24 @@ Kernel level: IQ4_XS 107 -> 323 GB/s at 2 columns (61 -> 212 at 6), Q4_K/Q5_K 1.
`STRATA_MMVQ_WIDE_K=0`. (`iq_parity` reports 10 "missing fixture" failures with and without the change: the oracle
fixtures are not in the port's tree.)
`STRATA_IQ3S_WIDE=1` (opt-in, 2026-10-10) reads the dense IQ3_S matrices (attn_q, attn_gate, ssm_out, attn_k, the
shared experts' gate/up) with a wide kernel shaped like the Q4_K..IQ4_XS ones. unitrace, B70, IQ3_XXS, one card, T=1:
worst attn_k 77 -> 123 GB/s, best attn_q 114 -> 307 GB/s; these matrices take 0.60 ms of kernel time per token
instead of 1.36 ms. The greedy output parts from the default at token 198 of 256 (the sub-blocks are summed in
another order). Measured again for 0.1.45 (B70, IQ3_S, stock-like config, 5 interleaved pairs): decode 33.39 -> 33.29 tok/s
(-0.3%), 4K prompt 40.71 -> 40.79, so it stays opt-in; the gain the author reports is on two cards.
**Q6_K decode kernels (0.1.45, #2340 leadgtr7, #2207 Andy Slater).** The Q6_K wide kernel took its six-bit offset (v - 32)
through a per-byte saturating subtract; it is now four integer ops (bitwise equal), and the Q4_K / Q5_K / IQ4_XS / Q6_K
kernels load the Q8_1 activations as one dword-vector. No switch. mmvq_bench Q6_K, B70: 2560x10240 1 column 44.7 -> 26.5 us,
4 columns 64.5 -> 46.2; A750: 2560x10240 4 columns 251 -> 102 us. End to end on the B70 (IQ3_S, 5 interleaved pairs, answers
identical): decode 32.24 -> 33.37 tok/s on a story prompt (+3.5%), 39.11 -> 40.77 after a 4K prompt (+4.2%), prompt rate unchanged.
`STRATA_MMVQ_LARGE_GRF` (default on for Arc A-series only, `=1` forces it on, `=0` off) launches the 2-8 column wide kernels with
256 registers per thread, where they spilled at 128 (the same bytes are produced either way). `STRATA_Q6_PLANAR` (default on,
`=0` off) converts the Q6_K output head and the MTP draft head to ql / qh / scales / d planes at load (same bytes, no VRAM change,
bitwise equal): 2560x248320 on a B70 800 -> 662 us (1 column), 1230 -> 1020 us (4 columns); end to end it adds +0.3% on the B70
(33.16 -> 33.25 tok/s, within noise) on top of the kernel changes above.
**Two-speed runs, explained (2026-09-30).** Identical greedy runs decode at either ~45 or ~39 tok/s. A per-gather
trace of the PLE reader (`STRATA_PLE_TRACE=1`) shows the slow runs pay one 226 ms PLE read stall in the first
decode round, after the window graphs are captured; every other read and round matches the fast runs. Prompt time
@@ -1,6 +1,7 @@
#pragma once
#include <cstddef>
#include <cstdint>
namespace strata::kernels {
@@ -114,6 +115,17 @@ bool native_q6_k_packed_enabled();
bool native_q6_k_pack(const void* weights, int n_in, int n_out, const char* what);
void native_q6_k_unpack(const void* weights);
/// STRATA_Q6_PLANAR (SYCL port; =0 turns it off): the Q6_K output heads stored as planes - ql (128 B), qh (64 B), the scales
/// (16 B, each lane's pair adjacent), d (2 B) of every 210-byte block - so the decode kernel's weight loads are single
/// aligned 16-byte loads (the GGUF layout is 2-byte aligned: two loads and a shift per run). Same bytes, ~14% faster, outputs
/// bitwise equal. native_q6_k_planar_adopt converts a GGUF-layout matrix (the old allocation is freed, the returned pointer
/// replaces it and is what native_q6_k_mmvq is called with; free it like the old one, after native_q6_k_unpack);
/// native_q6_k_planar_gather takes rows of a planar matrix as a new planar one (nullptr: `planar` is not one).
bool native_q6_k_planar_enabled();
bool native_q6_k_is_planar(const void* weights);
void* native_q6_k_planar_adopt(void* weights, int n_in, int n_out, const char* what);
void* native_q6_k_planar_gather(const void* planar, int n_in, const int32_t* rows_dev, int64_t n_sel, const char* what);
void native_q8_0_f32(const void* weights, const float* x, void* scratch_q8_1,
float* y, int n_in, int n_out, int ncols, void* stream);
+17 -1
View File
@@ -759,7 +759,19 @@ bool MtpDrafter::bind(const WeightTable &wt, const NativeHead *head,
dpct::get_in_order_queue().memcpy(dvocab_, raw.data(), raw.size()).wait();
dvocab_host_.resize((size_t) n_dvocab_);
std::memcpy(dvocab_host_.data(), raw.data(), raw.size());
strata::kernels::gather_rows((const uint8_t*) head->weights(), row_bytes, dvocab_, n_dvocab_, dhead_, nullptr);
bool planar_gathered = false;
if (head->type() == 14 && strata::kernels::native_q6_k_is_planar(head->weights()) && !q4_head_) {
// STRATA_Q6_PLANAR: the LM head is stored as planes; the subset is gathered from them, planar
void* pd = strata::kernels::native_q6_k_planar_gather(head->weights(), (int) (row_bytes / 210) * 256, dvocab_,
n_dvocab_, "MTP draft head");
if (pd != nullptr) {
sycl::free(dhead_, dpct::get_in_order_queue());
dhead_ = (uint8_t*) pd;
planar_gathered = true;
}
}
if (!planar_gathered)
strata::kernels::gather_rows((const uint8_t*) head->weights(), row_bytes, dvocab_, n_dvocab_, dhead_, nullptr);
dpct::get_current_device().queues_wait_and_throw();
vram_ += (uint64_t) (n_dvocab_ * row_bytes) + raw.size();
std::fprintf(stderr, "strata mtp: draft head over %lld tokens (%.1f MiB)\n", (long long) n_dvocab_,
@@ -769,6 +781,10 @@ bool MtpDrafter::bind(const WeightTable &wt, const NativeHead *head,
if (dhead_type_ == 14 && strata::kernels::native_q6_k_packed_enabled()) // STRATA_Q6_PACKED=1
strata::kernels::native_q6_k_pack(dhead_, (int) (row_bytes / 210) * 256, (int) n_dvocab_,
"MTP draft head");
if (!planar_gathered && dhead_type_ == 14 && !q4_head_ && !strata::kernels::native_q6_k_packed_enabled() &&
strata::kernels::native_q6_k_planar_enabled()) // STRATA_Q6_PLANAR
dhead_ = (uint8_t*) strata::kernels::native_q6_k_planar_adopt(dhead_, (int) (row_bytes / 210) * 256,
(int) n_dvocab_, "MTP draft head");
}
}
if (coupled_draft_env() && cparams_ == nullptr && !setup_coupled(err)) return false;
+2
View File
@@ -91,6 +91,8 @@ bool NativeHead::load(const std::vector<std::string> &shards, int64_t n_in,
type_ = (int) tensor->type;
if (type_ == 14 && strata::kernels::native_q6_k_packed_enabled()) // STRATA_Q6_PACKED=1
strata::kernels::native_q6_k_pack(weights_, n_in_, n_out_, "output head");
if (type_ == 14 && !strata::kernels::native_q6_k_packed_enabled() && strata::kernels::native_q6_k_planar_enabled()) // STRATA_Q6_PLANAR: stored as planes (same bytes)
weights_ = strata::kernels::native_q6_k_planar_adopt(weights_, n_in_, n_out_, "output head");
return true;
} catch (const std::exception& error) {
err = std::string("native head: ") + error.what();
+4 -1
View File
@@ -798,7 +798,10 @@ bool Verifier::record_window(int T, dpct::queue_ptr cs, std::string &err) {
// combine and the answers are the same kernels on the same data.
static const bool lean_env = [] { const char* v = std::getenv("STRATA_VERIFY_LEAN"); return !(v && v[0] == '0'); }();
static const bool no_host_env = [] { const char* v = std::getenv("STRATA_VERIFY_NO_HOST"); return v && *v && std::strcmp(v, "0") != 0; }();
const bool lean = lean_env && no_host_env && device_plan_ && !step_hook_ && !step_hook2_ && !remote_opt_ && !always_publish_ && !g_stepped;
// Not for a batch window (batch_rec_): unless the stage is all-resident, run_slot_rows / batch_poll wait for each
// layer's doorbell, so a lean batch graph finishes with the ring silent ("verify batch: layer 0 never rang (graph
// finished)", then the engine dies). Batch windows keep the doorbell nodes; the solo window stays lean.
const bool lean = lean_env && no_host_env && device_plan_ && !batch_rec_ && !step_hook_ && !step_hook2_ && !remote_opt_ && !always_publish_ && !g_stepped;
const WeightTable& wt = *wt_;
SessionState& ss = *ss_;
const int64_t N = g.n_embd, HC = g.hc, K = ss.k, C = g.ssm_conv_channels, ZV = g.ssm_value_dim;
+86
View File
@@ -4504,11 +4504,97 @@ void quantize_q8_1_rows(const float* x, int64_t n_rows, int64_t n_cols, void* y,
check("quantize_q8_1_rows");
}
// SYCL port: a wide kernel for the dense IQ3_S matrices (attn_q, attn_gate, ssm_out, attn_k, the shared experts'
// gate/up), shaped like the Q4_K..IQ4_XS wide kernels: 8 lanes share a 256-block, lane g takes sub-block g, so a
// 32-lane sub-group has four blocks in flight. Each sub-block's value is vec_dot_iq3_s_q8_1's; only the order the
// sub-blocks are summed in differs from mmvq_multi_kernel, so the output differs in the last bits.
template <int NCOLS>
__dpct_inline__ void iq3s_wide_kernel(const uint8_t* __restrict__ w, size_t rb, const block_q8_1* __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 row = (int) item.get_group(2) * (int) item.get_local_range(1) + warp;
if (row >= n_out) return;
const int nb = n_in / QK_K, xs = n_in / QK8_1;
const int g = lane & 7, sub = lane >> 3;
const block_iq3_s* wr = (const block_iq3_s*) (w + (size_t) row * rb);
float acc[NCOLS];
#pragma unroll
for (int c = 0; c < NCOLS; ++c) acc[c] = 0.0f;
for (int kbx = sub; kbx < nb; kbx += 4) {
const block_iq3_s* b = wr + kbx;
const sycl::int2 qs_packed = load8_a2(b->qs + 8 * g);
const uint8_t* qs = (const uint8_t*) &qs_packed;
const int qh = b->qh[g];
const int signs_packed_32 = load4_a2(b->signs + 4 * g);
const uint8_t* signs_packed_8 = (const uint8_t*) &signs_packed_32;
const int scale = 1 + 2 * ((b->scales[g / 2] >> (4 * (g & 1))) & 0x0F);
const float d3 = sycl::vec<sycl::half, 1>(b->d).convert<float, sycl::rounding_mode::automatic>()[0];
int wv[8];
#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 = swar_ne4(((signs_packed_8[l0 / 2] & 0x03) << 7) | ((signs_packed_8[l0 / 2] & 0x0C) << 21));
const int signs1 = swar_ne4(((signs_packed_8[l0 / 2] & 0x30) << 3) | ((signs_packed_8[l0 / 2] & 0xC0) << 17));
wv[l0 + 0] = swar_sub4(grid_pos.x() ^ signs0, signs0);
wv[l0 + 1] = swar_sub4(grid_pos.y() ^ signs1, signs1);
}
#pragma unroll
for (int c = 0; c < NCOLS; ++c) {
const block_q8_1* xb = x + (size_t) c * xs + (size_t) kbx * (QK_K / QK8_1) + g;
const int* u = reinterpret_cast<const int*>(xb->qs);
int sumi = 0;
#pragma unroll
for (int l = 0; l < 8; ++l) sumi = ggml_cuda_dp4a(wv[l], u[l], sumi);
sumi *= scale;
const float d = d3 * xb->ds[0];
acc[c] += d * sumi;
}
}
auto sg = item.get_sub_group();
#pragma unroll
for (int c = 0; c < NCOLS; ++c) {
float v = acc[c];
#pragma unroll
for (int o = 16; o > 0; o >>= 1) v += sycl::permute_group_by_xor(sg, v, o);
if (lane == 0) y[(size_t) c * n_out + row] = v;
}
}
// STRATA_IQ3S_WIDE=1: the wide kernel; unset or 0: the multi kernel (default).
bool iq3s_wide_on() {
static const bool v = [] { const char* e = std::getenv("STRATA_IQ3S_WIDE"); return e && std::atoi(e) != 0; }();
return v;
}
template <int NCOLS>
void launch_iq3s_wide_t(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int n_in, int n_out, dpct::queue_ptr s) {
constexpr int WPG = 4; // sub-groups per work-group
const unsigned groups = unsigned((n_out + WPG - 1) / WPG);
s->parallel_for<dpct_kernel_name<class iq3s_wide_k, dpct_kernel_scalar<NCOLS>>>(
sycl::nd_range<3>(sycl::range(1, WPG, groups * 32), sycl::range(1, WPG, 32)),
[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { iq3s_wide_kernel<NCOLS>(W, rb, X, y, n_in, n_out); });
}
bool launch_iq3s_wide(const uint8_t* W, size_t rb, const block_q8_1* X, float* y, int n_in, int n_out, int ncols, dpct::queue_ptr s) {
if (!iq3s_wide_on() || n_in % QK_K != 0) return false;
switch (ncols) {
case 1: launch_iq3s_wide_t<1>(W, rb, X, y, n_in, n_out, s); return true;
case 2: launch_iq3s_wide_t<2>(W, rb, X, y, n_in, n_out, s); return true;
case 3: launch_iq3s_wide_t<3>(W, rb, X, y, n_in, n_out, s); return true;
case 4: launch_iq3s_wide_t<4>(W, rb, X, y, n_in, n_out, s); return true;
case 5: launch_iq3s_wide_t<5>(W, rb, X, y, n_in, n_out, s); return true;
case 6: launch_iq3s_wide_t<6>(W, rb, X, y, n_in, n_out, s); return true;
case 7: launch_iq3s_wide_t<7>(W, rb, X, y, n_in, n_out, s); return true;
case 8: launch_iq3s_wide_t<8>(W, rb, X, y, n_in, n_out, s); return true;
default: return false;
}
}
void iq_mmvq(int t, const void* w, const void* x_q8_1, float* y, int n_in, int n_out, int ncols, void* stream) {
const size_t rb = iq_row_bytes(t, n_in);
dpct::queue_ptr s = strata::q_of(stream);
const auto* W = (const uint8_t*) w;
const auto* X = (const block_q8_1*) x_q8_1;
if (t == 21 && launch_iq3s_wide(W, rb, X, y, n_in, n_out, ncols, s)) { check("iq_mmvq"); return; }
switch (t) {
#define STRATA_MMVQ(T) case T: launch_mmvq<T>(W, rb, X, y, n_in, n_out, ncols, s); break;
STRATA_MMVQ_FMTS(STRATA_MMVQ)
+300 -11
View File
@@ -25,6 +25,7 @@
#define DPCT_PROFILING_ENABLED
#include <sycl/sycl.hpp>
#include <sycl/ext/intel/experimental/grf_size_properties.hpp>
#include <dpct/dpct.hpp>
#include "strata/sycl_math.hpp"
#include "strata/sycl_queue.hpp"
@@ -1512,6 +1513,19 @@ void native_mmvq_rowwarp_kernel(const typename F::Block* __restrict__ w, const Q
// is 210 bytes, so its ql/qh runs start 2-byte aligned, and the B70 splits a misaligned vector load into pieces:
// the wide kernel streamed 145 GB/s that way against 640 GB/s for the same blocks repacked at a 16-byte stride.
// Both aligned loads stay inside the 210-byte block for the ql and qh runs (last byte read <= block + 207).
// SYCL port: a 4-int vector load from a 4-byte aligned address (a Q8_1 block is 36 bytes, its qs sit at offset 4): one dword-vector
// message instead of four scalar loads - the decode kernels issue 8 of these per column and 4-byte step.
__dpct_inline__ sycl::int4 ld_i4_a4(const int* p) {
sycl::int4 v;
v.load(0, sycl::address_space_cast<sycl::access::address_space::global_space, sycl::access::decorated::no>(p));
return v;
}
// Q6_K: per-byte (v - 32) of four 6-bit values (bytes 0..63): no byte can saturate, so this is the saturating char4 subtract's
// result exactly, in four integer ops (SWAR: no borrow crosses a byte) instead of the per-byte vector builtin.
__dpct_inline__ int q6_sub32(int x) {
const uint32_t u = (uint32_t) x;
return (int) (((u | 0x80808080u) - 0x20202020u) ^ (~u & 0x80808080u));
}
__dpct_inline__ sycl::int4 load16_a2(const void* p) {
const uintptr_t a = reinterpret_cast<uintptr_t>(p);
const sycl::int4* q = reinterpret_cast<const sycl::int4*>(a & ~uintptr_t(15));
@@ -1576,10 +1590,8 @@ void native_mmvq_q6k_wide_kernel(const Q6KBlock* __restrict__ w, const Q81Block*
for (int j = 0; j < NCOLS; ++j) {
const Q81Block* xb = x + std::size_t(j) * x_stride + kby + bq8_offset;
if constexpr (A2) { // Q8_1 qs sits at offset 4 of a 36-byte block: 4-byte aligned, so four int loads
const int* p0 = reinterpret_cast<const int*>(xb[0].qs) + 4 * u_int4;
const int* p1 = reinterpret_cast<const int*>(xb[2].qs) + 4 * u_int4;
u0[j] = sycl::int4(p0[0], p0[1], p0[2], p0[3]);
u1[j] = sycl::int4(p1[0], p1[1], p1[2], p1[3]);
u0[j] = ld_i4_a4(reinterpret_cast<const int*>(xb[0].qs) + 4 * u_int4);
u1[j] = ld_i4_a4(reinterpret_cast<const int*>(xb[2].qs) + 4 * u_int4);
} else {
u0[j] = reinterpret_cast<const sycl::int4*>(xb[0].qs)[u_int4];
u1[j] = reinterpret_cast<const sycl::int4*>(xb[2].qs)[u_int4];
@@ -1593,8 +1605,8 @@ void native_mmvq_q6k_wide_kernel(const Q6KBlock* __restrict__ w, const Q81Block*
int vi0[4], vi1[4];
#pragma unroll
for (int p = 0; p < 4; ++p) {
vi0[p] = dpct::vectorized_binary<sycl::char4>((vl[p] & 0x0f0f0f0f) | ((vh[p] << 4) & 0x30303030), 0x20202020, dpct::sub_sat());
vi1[p] = dpct::vectorized_binary<sycl::char4>(((vl[p] >> 4) & 0x0f0f0f0f) | (((vh[p] >> 4) << 4) & 0x30303030), 0x20202020, dpct::sub_sat());
vi0[p] = q6_sub32((vl[p] & 0x0f0f0f0f) | ((vh[p] << 4) & 0x30303030));
vi1[p] = q6_sub32(((vl[p] >> 4) & 0x0f0f0f0f) | (((vh[p] >> 4) << 4) & 0x30303030));
}
#pragma unroll
for (int j = 0; j < NCOLS; ++j) {
@@ -1661,8 +1673,8 @@ void native_mmvq_q6k_pf_kernel(const Q6KBlock* __restrict__ w, const Q81Block* _
int vi0[4], vi1[4];
#pragma unroll
for (int p = 0; p < 4; ++p) {
vi0[p] = dpct::vectorized_binary<sycl::char4>((vl[p] & 0x0f0f0f0f) | ((vh[p] << 4) & 0x30303030), 0x20202020, dpct::sub_sat());
vi1[p] = dpct::vectorized_binary<sycl::char4>(((vl[p] >> 4) & 0x0f0f0f0f) | (((vh[p] >> 4) << 4) & 0x30303030), 0x20202020, dpct::sub_sat());
vi0[p] = q6_sub32((vl[p] & 0x0f0f0f0f) | ((vh[p] << 4) & 0x30303030));
vi1[p] = q6_sub32(((vl[p] >> 4) & 0x0f0f0f0f) | (((vh[p] >> 4) << 4) & 0x30303030));
}
#pragma unroll
for (int j = 0; j < NCOLS; ++j) {
@@ -1703,6 +1715,19 @@ inline int q6k_wide_rpw() {
static const int v = std::getenv("STRATA_MMVQ_WIDE_RPW") ? std::atoi(std::getenv("STRATA_MMVQ_WIDE_RPW")) : 1;
return v;
}
// Large-GRF mode (256 registers per thread instead of 128) for the wide kernels (Q6_K a2 and the generic one) at 2-8
// columns: on the A770 they spill at 128, and mmvq_bench Q6_K 4 columns goes 70-80 -> 150-237 GB/s with the same
// checksum. Large GRF halves the threads an Xe core keeps resident, so it is the default on Arc A-series (Alchemist)
// only, where it was measured; Battlemage and anything else, and 1 column everywhere, launch the same kernels as before.
// STRATA_MMVQ_LARGE_GRF=1 forces it on, =0 off.
inline bool mmvq_large_grf(dpct::queue_ptr s) {
static const bool v = [s] {
const char* e = std::getenv("STRATA_MMVQ_LARGE_GRF");
if (e != nullptr) return std::atoi(e) != 0;
return strata::intel_gpu_gen(s->get_device()) == strata::IntelGpuGen::Alchemist;
}();
return v;
}
template <int NCOLS>
void launch_q6k_wide(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 Q6KBlock*>(weights);
@@ -1726,6 +1751,17 @@ void launch_q6k_wide(const void* weights, const void* x_q8_1, float* y, int n_in
}
if (rpw == 1 && q6k_a2()) {
const unsigned blocks = unsigned((std::size_t(n_out) + WARPS - 1) / WARPS);
if constexpr (NCOLS > 1) {
if (mmvq_large_grf(s)) {
s->parallel_for<dpct_kernel_name<class native_mmvq_q6k_wide_a2_grf256, dpct_kernel_scalar<NCOLS>>>(
sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)),
sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>},
[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {
native_mmvq_q6k_wide_kernel<NCOLS, 1, WARP, (int) sizeof(Q6KBlock), true>(w, x, y, n_in, n_out);
});
return;
}
}
s->parallel_for<dpct_kernel_name<class native_mmvq_q6k_wide_a2, dpct_kernel_scalar<NCOLS>>>(
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)]] {
@@ -1769,9 +1805,8 @@ void launch_q6k_wide(const void* weights, const void* x_q8_1, float* y, int n_in
// replace does 4-byte loads (Q4_K/Q5_K, 230-280 GB/s) or global-memory codebook lookups (IQ4_XS, ~100 GB/s).
// Lane g of a block: Q4_K/Q5_K take qs bytes [16g, 16g+16) = sub-blocks 2(g/2) (low nibbles) and 2(g/2)+1 (high
// nibbles) at positions 16(g%2)..+15; IQ4_XS takes sub-block g (16 bytes, low nibbles = positions 0-15).
__dpct_inline__ sycl::int4 ld_q8_16(const Q81Block* b, int half) { // Q8_1 qs is 4-byte aligned: four int loads
const int* q = reinterpret_cast<const int*>(b->qs) + 4 * half;
return sycl::int4(q[0], q[1], q[2], q[3]);
__dpct_inline__ sycl::int4 ld_q8_16(const Q81Block* b, int half) { // Q8_1 qs is 4-byte aligned: one dword-vector load
return ld_i4_a4(reinterpret_cast<const int*>(b->qs) + 4 * half);
}
__dpct_inline__ int dp4a4(const sycl::int4 a, const sycl::int4 b, int acc) {
acc = strata::dp4a(a.x(), b.x(), acc); acc = strata::dp4a(a.y(), b.y(), acc);
@@ -1959,6 +1994,15 @@ void launch_wide(const void* weights, const void* x_q8_1, float* y, int n_in, in
STRATA_WKR(2) STRATA_WKR(4)
#undef STRATA_WKR
const unsigned blocks = unsigned((std::size_t(n_out) + WARPS - 1) / WARPS);
if constexpr (NCOLS > 1) {
if (mmvq_large_grf(s)) {
s->parallel_for<dpct_kernel_name<class native_mmvq_wide_grf256, F, dpct_kernel_scalar<NCOLS>>>(
sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)),
sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>},
[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { native_mmvq_wide_kernel<F, NCOLS>(w, x, y, n_in, n_out); });
return;
}
}
s->parallel_for<dpct_kernel_name<class native_mmvq_wide, F, dpct_kernel_scalar<NCOLS>>>(
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_wide_kernel<F, NCOLS>(w, x, y, n_in, n_out); });
@@ -2817,6 +2861,157 @@ bool q6_packed_mmvq(const void* weights, const void* x_q8_1, float* y, int n_in,
launch_check();
return true;
}
// ============================ STRATA_Q6_PLANAR: the Q6_K heads as 16-byte-aligned planes (SYCL port) ============================
//
// native_mmvq_q6k_wide_kernel<..., A2 = true> reads each lane's ql / qh run as two aligned 16-byte loads and a funnel shift
// because a 210-byte block is only 2-byte aligned. The planar layout holds the same bytes as four planes (ql 128 B, qh 64 B,
// scales 16 B, d 2 B per block): a lane's runs are single aligned int4 loads, its two scales one 16-bit load (the pair
// scales[so], scales[so + 4] stored adjacent, at byte 2g). The kernel is the A2 kernel's, line for line, from the loads on:
// every output is bitwise equal, ~14% faster on the 521 MB LM head and the draft head. Same bytes, so a matrix is CONVERTED
// (planar allocation made, GGUF-layout one freed): no VRAM is spent. Only the output heads are converted - the prompt path
// reads the other projections in the GGUF layout.
struct Q6Planar {
void* base;
const uint8_t* ql; const uint8_t* qh; const uint8_t* sc; const sycl::half* d;
int n_in, n_out;
};
std::unordered_map<const void*, Q6Planar>& q6_planar_registry() {
static std::unordered_map<const void*, Q6Planar> registry;
return registry;
}
template <int NCOLS>
void native_mmvq_q6k_planar_kernel(const uint8_t* __restrict__ qlp, const uint8_t* __restrict__ qhp,
const uint8_t* __restrict__ scp, const sycl::half* __restrict__ dp,
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 row = int(item.get_group(2)) * WARPS + warp;
if (row >= n_out) return;
const int blocks_per_row = n_in / QK, x_stride = n_in / Q8K;
const int g = lane & 7, sub = lane >> 3;
const int vh_shift = 2 * ((g & 3) >> 1);
const int qh4_idx = 2 * (g >> 2) + (g & 1);
const int bq8_offset = 4 * (g >> 2) + ((g & 3) >> 1);
const int u_int4 = g & 1;
float acc[NCOLS] = {};
for (int kbx = sub; kbx < blocks_per_row; kbx += WARP / 8) {
const int kby = kbx * (QK / Q8K);
const std::size_t bi = std::size_t(row) * blocks_per_row + kbx;
const sycl::int4 ql4 = reinterpret_cast<const sycl::int4*>(qlp + bi * 128)[g];
const sycl::int4 qh4 = reinterpret_cast<const sycl::int4*>(qhp + bi * 64)[qh4_idx];
const uint16_t s2 = reinterpret_cast<const uint16_t*>(scp + bi * 16)[g];
const float d = dp[bi];
const float dsc0 = d * (float) (int8_t) (s2 & 0xff), dsc1 = d * (float) (int8_t) (s2 >> 8);
sycl::int4 u0[NCOLS], u1[NCOLS];
float ds0[NCOLS], ds1[NCOLS];
#pragma unroll
for (int j = 0; j < NCOLS; ++j) {
const Q81Block* xb = x + std::size_t(j) * x_stride + kby + bq8_offset;
u0[j] = ld_i4_a4(reinterpret_cast<const int*>(xb[0].qs) + 4 * u_int4);
u1[j] = ld_i4_a4(reinterpret_cast<const int*>(xb[2].qs) + 4 * u_int4);
ds0[j] = xb[0].ds[0]; ds1[j] = xb[2].ds[0];
}
const int vl[4] = {ql4.x(), ql4.y(), ql4.z(), ql4.w()};
const int vh[4] = {qh4.x() >> vh_shift, qh4.y() >> vh_shift, qh4.z() >> vh_shift, qh4.w() >> vh_shift};
int vi0[4], vi1[4];
#pragma unroll
for (int p = 0; p < 4; ++p) {
vi0[p] = q6_sub32((vl[p] & 0x0f0f0f0f) | ((vh[p] << 4) & 0x30303030));
vi1[p] = q6_sub32(((vl[p] >> 4) & 0x0f0f0f0f) | (((vh[p] >> 4) << 4) & 0x30303030));
}
#pragma unroll
for (int j = 0; j < NCOLS; ++j) {
int s0 = 0, s1 = 0;
s0 = strata::dp4a(vi0[0], u0[j].x(), s0); s0 = strata::dp4a(vi0[1], u0[j].y(), s0);
s0 = strata::dp4a(vi0[2], u0[j].z(), s0); s0 = strata::dp4a(vi0[3], u0[j].w(), s0);
s1 = strata::dp4a(vi1[0], u1[j].x(), s1); s1 = strata::dp4a(vi1[1], u1[j].y(), s1);
s1 = strata::dp4a(vi1[2], u1[j].z(), s1); s1 = strata::dp4a(vi1[3], u1[j].w(), s1);
acc[j] += ds0[j] * dsc0 * (float) s0 + ds1[j] * dsc1 * (float) s1;
}
}
auto sg = item.get_sub_group();
#pragma unroll
for (int j = 0; j < NCOLS; ++j) {
float v = acc[j];
#pragma unroll
for (int o = WARP / 2; o > 0; o >>= 1) v += sycl::permute_group_by_xor(sg, v, o);
if (lane == 0) y[std::size_t(j) * n_out + row] = v;
}
}
template <int NCOLS>
void q6_planar_launch(const Q6Planar& w, const Q81Block* x, float* y, dpct::queue_ptr s) {
const unsigned blocks = unsigned((std::size_t(w.n_out) + WARPS - 1) / WARPS);
const uint8_t *ql = w.ql, *qh = w.qh, *sc = w.sc;
const sycl::half* d = w.d;
const int n_in = w.n_in, n_out = w.n_out;
if constexpr (NCOLS > 1) { // same rule as the GGUF-layout wide kernel (#2207): the multi-column kernel spills at 128 GRF on Alchemist
if (mmvq_large_grf(s)) {
s->parallel_for<dpct_kernel_name<class native_mmvq_q6k_planar_grf256, dpct_kernel_scalar<NCOLS>>>(
sycl::nd_range<3>(sycl::range(1, 1, blocks) * sycl::range(1, WARPS, WARP), sycl::range(1, WARPS, WARP)),
sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>},
[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {
native_mmvq_q6k_planar_kernel<NCOLS>(ql, qh, sc, d, x, y, n_in, n_out);
});
return;
}
}
s->parallel_for<dpct_kernel_name<class native_mmvq_q6k_planar, dpct_kernel_scalar<NCOLS>>>(
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_q6k_planar_kernel<NCOLS>(ql, qh, sc, d, x, y, n_in, n_out);
});
}
// every registered matrix is served here whatever the column count (more than 8: in chunks of 8), so a converted matrix is
// never read through a GGUF-layout kernel
bool q6_planar_mmvq(const void* weights, const void* x_q8_1, float* y, int n_in, int n_out, int ncols, void* stream) {
auto& registry = q6_planar_registry();
if (registry.empty() || ncols < 1) return false;
const auto found = registry.find(weights);
if (found == registry.end() || found->second.n_in != n_in || found->second.n_out != n_out) return false;
validate_shape(n_in, std::min(ncols, 8), 256);
validate_pointer(x_q8_1);
validate_pointer(y);
validate_stream(stream);
const auto s = strata::q_of(stream);
const std::size_t x_col = std::size_t(n_in / Q8K); // Q8_1 blocks per column
for (int c0 = 0; c0 < ncols; c0 += 8) {
const int nc = std::min(8, ncols - c0);
const auto* x = static_cast<const Q81Block*>(x_q8_1) + std::size_t(c0) * x_col;
float* yy = y + std::size_t(c0) * n_out;
switch (nc) {
case 1: q6_planar_launch<1>(found->second, x, yy, s); break;
case 2: q6_planar_launch<2>(found->second, x, yy, s); break;
case 3: q6_planar_launch<3>(found->second, x, yy, s); break;
case 4: q6_planar_launch<4>(found->second, x, yy, s); break;
case 5: q6_planar_launch<5>(found->second, x, yy, s); break;
case 6: q6_planar_launch<6>(found->second, x, yy, s); break;
case 7: q6_planar_launch<7>(found->second, x, yy, s); break;
default: q6_planar_launch<8>(found->second, x, yy, s); break;
}
}
launch_check();
return true;
}
// plane offsets of a matrix of nb blocks in one allocation: ql | qh | scales | d, each 256-byte aligned
struct Q6PlanarLayout { std::size_t o_qh, o_sc, o_d, total; };
Q6PlanarLayout q6_planar_layout(std::size_t nb) {
Q6PlanarLayout l;
l.o_qh = (nb * 128 + 255) & ~std::size_t(255);
l.o_sc = (l.o_qh + nb * 64 + 255) & ~std::size_t(255);
l.o_d = (l.o_sc + nb * 16 + 255) & ~std::size_t(255);
l.total = l.o_d + nb * 2;
return l;
}
Q6Planar q6_planar_make(void* dev, std::size_t nb, int n_in, int n_out) {
const Q6PlanarLayout l = q6_planar_layout(nb);
auto* base = static_cast<uint8_t*>(dev);
return Q6Planar{dev, base, base + l.o_qh, base + l.o_sc, reinterpret_cast<const sycl::half*>(base + l.o_d), n_in, n_out};
}
} // namespace
bool native_q8_0_packed_enabled() {
@@ -2864,6 +3059,98 @@ bool native_q6_k_packed_enabled() {
return enabled;
}
bool native_q6_k_planar_enabled() {
static const bool enabled = [] {
const char* v = std::getenv("STRATA_Q6_PLANAR");
return !(v && v[0] == '0' && v[1] == '\0');
}();
return enabled;
}
bool native_q6_k_is_planar(const void* weights) { return q6_planar_registry().count(weights) != 0; }
// Converts a GGUF-layout Q6_K matrix (a device allocation of the caller's) to the planar layout: a new allocation holds the
// planes, the old one is freed, the returned pointer (the new allocation's, to be freed with sycl::free as the old one would
// have been, after native_q6_k_unpack) is the key native_q6_k_mmvq serves it under. Not eligible or no room: `weights` back.
void* native_q6_k_planar_adopt(void* weights, int n_in, int n_out, const char* what) try {
if (!weights || n_in <= 0 || n_in % 256 || n_out <= 0 || !native_q6_k_planar_enabled()) return weights;
if (q6_planar_registry().count(weights)) return weights;
auto& q = dpct::get_in_order_queue();
const std::size_t nb = std::size_t(n_in / 256) * std::size_t(n_out);
const Q6PlanarLayout l = q6_planar_layout(nb);
void* dev = nullptr;
try { dev = strata::malloc_device_guarded(l.total, q, what); } catch (...) { dev = nullptr; }
if (dev == nullptr) {
std::fprintf(stderr, "strata: STRATA_Q6_PLANAR: no VRAM for the %s's planar copy (+%.1f MiB), keeping the GGUF layout\n", what,
l.total / 1048576.0);
return weights;
}
const uint8_t* src = static_cast<const uint8_t*>(weights);
uint8_t* base = static_cast<uint8_t*>(dev);
const std::size_t o_qh = l.o_qh, o_sc = l.o_sc, o_d = l.o_d;
q.parallel_for(sycl::range<1>(nb), [=](sycl::id<1> id) {
const std::size_t b = id[0];
const uint16_t* s16 = reinterpret_cast<const uint16_t*>(src + b * 210);
uint16_t* qlo = reinterpret_cast<uint16_t*>(base + b * 128);
for (int i = 0; i < 64; ++i) qlo[i] = s16[i];
uint16_t* qho = reinterpret_cast<uint16_t*>(base + o_qh + b * 64);
for (int i = 0; i < 32; ++i) qho[i] = s16[64 + i];
const int8_t* sc = reinterpret_cast<const int8_t*>(src + b * 210 + 192);
uint8_t* sco = base + o_sc + b * 16;
for (int g = 0; g < 8; ++g) {
const int so = 8 * (g >> 2) + (g & 3);
sco[2 * g] = (uint8_t) sc[so]; sco[2 * g + 1] = (uint8_t) sc[so + 4];
}
reinterpret_cast<uint16_t*>(base + o_d)[b] = s16[104];
});
q.wait();
sycl::free(weights, q);
q6_planar_registry()[dev] = q6_planar_make(dev, nb, n_in, n_out);
std::fprintf(stderr, "strata: STRATA_Q6_PLANAR: the %s (%d x %d) is stored as 16-byte-aligned planes (same bytes)\n", what, n_out, n_in);
return dev;
}
catch (sycl::exception const& exc) {
std::fprintf(stderr, "strata: STRATA_Q6_PLANAR conversion failed: %s\n", exc.what());
return weights;
}
// A planar matrix's rows `rows_dev[0 .. n_sel)` as a new planar matrix (the draft head's token subset of the LM head).
void* native_q6_k_planar_gather(const void* planar, int n_in, const int32_t* rows_dev, int64_t n_sel, const char* what) try {
const auto found = q6_planar_registry().find(planar);
if (found == q6_planar_registry().end() || found->second.n_in != n_in || n_sel <= 0) return nullptr;
auto& q = dpct::get_in_order_queue();
const int nbr = n_in / 256;
const std::size_t nb = std::size_t(nbr) * std::size_t(n_sel);
const Q6PlanarLayout l = q6_planar_layout(nb);
void* dev = nullptr;
try { dev = strata::malloc_device_guarded(l.total, q, what); } catch (...) { dev = nullptr; }
if (dev == nullptr) return nullptr;
const Q6Planar src = found->second;
uint8_t* base = static_cast<uint8_t*>(dev);
const std::size_t o_qh = l.o_qh, o_sc = l.o_sc, o_d = l.o_d;
q.parallel_for(sycl::range<1>(nb), [=](sycl::id<1> id) {
const std::size_t b = id[0];
const std::size_t i = b / nbr, j = b - i * nbr;
const std::size_t sb = std::size_t(rows_dev[i]) * nbr + j;
const sycl::int4* sq = reinterpret_cast<const sycl::int4*>(src.ql + sb * 128);
sycl::int4* dq = reinterpret_cast<sycl::int4*>(base + b * 128);
for (int k = 0; k < 8; ++k) dq[k] = sq[k];
const sycl::int4* sh = reinterpret_cast<const sycl::int4*>(src.qh + sb * 64);
sycl::int4* dh = reinterpret_cast<sycl::int4*>(base + o_qh + b * 64);
for (int k = 0; k < 4; ++k) dh[k] = sh[k];
*reinterpret_cast<sycl::int4*>(base + o_sc + b * 16) = *reinterpret_cast<const sycl::int4*>(src.sc + sb * 16);
reinterpret_cast<uint16_t*>(base + o_d)[b] = reinterpret_cast<const uint16_t*>(src.d)[sb];
});
q.wait();
q6_planar_registry()[dev] = q6_planar_make(dev, nb, n_in, (int) n_sel);
std::fprintf(stderr, "strata: STRATA_Q6_PLANAR: the %s (%lld x %d) is gathered from the planar head\n", what, (long long) n_sel, n_in);
return dev;
}
catch (sycl::exception const& exc) {
std::fprintf(stderr, "strata: STRATA_Q6_PLANAR gather failed: %s\n", exc.what());
return nullptr;
}
bool native_q6_k_pack(const void *weights, int n_in, int n_out,
const char *what) try {
if (!weights || n_in <= 0 || n_in % 256 || n_in / 256 < WARPS * WARP / 32 || n_out < 2048) return false;
@@ -3040,6 +3327,7 @@ catch (sycl::exception const &exc) {
}
void native_q6_k_unpack(const void* weights) {
q6_planar_registry().erase(weights); // planar matrices are the caller's allocation (native_q6_k_planar_adopt): the caller frees it
auto& registry = q6_packed_registry();
const auto found = registry.find(weights);
if (found == registry.end()) return;
@@ -3522,6 +3810,7 @@ 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) {
if (q6_packed_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream)) return; // STRATA_Q6_PACKED=1 heads
if (q6_planar_mmvq(weights, x_q8_1, y, n_in, n_out, ncols, stream)) return; // STRATA_Q6_PLANAR copies
// SYCL port: 16-byte-load kernel for the decode shapes (ncols <= 4)
// With the aligned loads (load16_a2) the wide kernel wins on every Q6_K shape and window width: 2.6-3.4x the
// misaligned-load kernels in q6k_align_bench, decode 44.9 -> 54 tok/s on the Coder, output tokens identical.
+48 -1
View File
@@ -3,9 +3,11 @@
#include "strata/kernels/native_mmvq.hpp"
#include <sycl/sycl.hpp>
#include <dpct/dpct.hpp>
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
int main(int argc, char** argv) {
@@ -46,6 +48,51 @@ int main(int argc, char** argv) {
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);
std::vector<float> hy((size_t) ncols * n_out);
s->memcpy(hy.data(), y, hy.size() * 4).wait();
unsigned long long cs = 1469598103934665603ull;
for (float v : hy) { unsigned u; std::memcpy(&u, &v, 4); cs = (cs ^ u) * 1099511628211ull; }
std::printf("q6_k mmvq %d x %d, %d cols: %.1f us (%.1f GB/s of weights) checksum %016llx\n", n_in, n_out, ncols, us, wbytes / us / 1e3, cs);
// STRATA_BENCH_PLANAR=1 (#2340): the same matrix as STRATA_Q6_PLANAR planes, bitwise against the GGUF-layout result above; then a
// gathered row subset (the MTP draft head's case) against the same rows of the full result. Exit 1 on any difference.
const char* pe = std::getenv("STRATA_BENCH_PLANAR");
if (pe && std::atoi(pe) != 0) {
void* w2 = sycl::malloc_device(wbytes, *s);
s->memcpy(w2, hw.data(), wbytes).wait();
void* wp = strata::kernels::native_q6_k_planar_adopt(w2, n_in, n_out, "bench matrix");
if (!strata::kernels::native_q6_k_is_planar(wp)) { std::printf("planar: adopt declined\n"); return 1; }
float* y2 = sycl::malloc_device<float>((size_t) ncols * n_out, *s);
strata::kernels::native_q6_k_mmvq(wp, xq, y2, n_in, n_out, ncols, s); s->wait();
std::vector<float> hp((size_t) ncols * n_out);
s->memcpy(hp.data(), y2, hp.size() * 4).wait();
const bool same = std::memcmp(hp.data(), hy.data(), hp.size() * 4) == 0;
for (int i = 0; i < 20; ++i) strata::kernels::native_q6_k_mmvq(wp, xq, y2, n_in, n_out, ncols, s);
s->wait();
const auto p0 = std::chrono::steady_clock::now();
for (int i = 0; i < it; ++i) strata::kernels::native_q6_k_mmvq(wp, xq, y2, n_in, n_out, ncols, s);
s->wait();
const double pus = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - p0).count() / it;
std::printf("q6_k planar %d x %d, %d cols: %.1f us (%.1f GB/s of weights) bitwise %s\n", n_in, n_out, ncols, pus, wbytes / pus / 1e3,
same ? "EQUAL" : "DIFFERENT");
int rc = same ? 0 : 1;
const int nsel = std::max(1, n_out / 3);
std::vector<int32_t> rows(nsel);
for (int i = 0; i < nsel; ++i) rows[i] = (int32_t) (((uint64_t) i * 2654435761ull + 12345) % (uint64_t) n_out);
int32_t* rd = sycl::malloc_device<int32_t>(nsel, *s);
s->memcpy(rd, rows.data(), nsel * 4).wait();
void* wg = strata::kernels::native_q6_k_planar_gather(wp, n_in, rd, nsel, "bench subset");
if (!wg) { std::printf("planar gather: declined\n"); return 1; }
float* y3 = sycl::malloc_device<float>((size_t) ncols * nsel, *s);
strata::kernels::native_q6_k_mmvq(wg, xq, y3, n_in, nsel, ncols, s); s->wait();
std::vector<float> hg((size_t) ncols * nsel);
s->memcpy(hg.data(), y3, hg.size() * 4).wait();
size_t bad = 0;
for (int c = 0; c < ncols; ++c)
for (int i = 0; i < nsel; ++i)
if (std::memcmp(&hg[(size_t) c * nsel + i], &hy[(size_t) c * n_out + rows[i]], 4) != 0) ++bad;
std::printf("q6_k planar gather %d of %d rows, %d cols: %zu of %zu outputs differ\n", nsel, n_out, ncols, bad, hg.size());
if (bad) rc = 1;
return rc;
}
return 0;
}