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++

#2222 sm_75: two launches fewer per decode layer (bitwise)

Rafa88a1c23f61a3
diff --git a/include/strata/kernels/native_mmvq.hpp b/include/strata/kernels/native_mmvq.hpp
index fef71027d..658f2cffb 100644
--- a/include/strata/kernels/native_mmvq.hpp
+++ b/include/strata/kernels/native_mmvq.hpp
@@ -141,6 +141,8 @@ constexpr int native_q8_1_il_cp(int ncols) { return ncols <= 2 ? 2 : ncols <= 4
std::size_t native_q8_1_il_bytes(int n_in, int ncols);
// Writes the interleaved copy of 2..4 columns of plain q8_1 blocks (native_quantize_q8_1's, or a fused producer's)
// to x_il (native_q8_1_il_bytes): the same values in the same bytes.
+/// native_quantize_q8_1 and native_q8_1_interleave in one launch (CUDA): the same bytes in both buffers.
+void native_quantize_q8_1_il(const float* x, void* x_q8_1, void* x_il, int n_in, int ncols, void* stream);
void native_q8_1_interleave(const void* x_q8_1, void* x_il, int n_in, int ncols, void* stream);
// True when native_mmvq_il runs its own kernel for this call (IQ4_XS, Q4_K, Q5_K, Q6_K, 2-4 columns, the exact layout).
bool native_mmvq_il_supported(int ggml_type, int ncols, int n_out);
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index a8791cc67..74461e913 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -246,6 +246,25 @@ bool head_mix_multi_enabled() {
// copy_rows_from_mapped_after): same reads, same values, one kernel node fewer each. A dependent kernel costs ~2.4 us in a
// gfx1201 graph whatever it does; measured on gfx12 only, so it is on there by default. STRATA_FUSE_WAIT=0 / 1 forces it
// off / on (HIP builds; a CUDA build never fuses).
+// sm_75 (Turing) defaults of the CUDA build's switches below, read per device
+bool device_is_sm75() {
+#if defined(STRATA_USE_HIP)
+ return false;
+#else
+ static int per_dev[64]; // 0 unknown, 1 no, 2 yes
+ int dev = 0;
+ if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) return false;
+ if (!per_dev[dev]) {
+ int maj = 0, mnr = 0;
+ const bool ok = cudaDeviceGetAttribute(&maj, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess &&
+ cudaDeviceGetAttribute(&mnr, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess;
+ if (!ok) cudaGetLastError();
+ per_dev[dev] = ok && maj == 7 && mnr == 5 ? 2 : 1;
+ }
+ return per_dev[dev] == 2;
+#endif
+}
+
bool fuse_wait_on() {
#if defined(STRATA_USE_HIP)
static const int env = [] {
@@ -262,7 +281,21 @@ bool fuse_wait_on() {
}
return per_dev[dev] == 2;
#else
- return false;
+ // CUDA: STRATA_FUSE_WAIT=1 folds both waits (the plan's and the CPU rows'), =2 only the plan's, =0 neither. Turing
+ // default 2 (RTX 2080 Ti, Flash-Next Q2_0, two runs each: +0.6..+1.4 % decode); the rows' version makes every row
+ // block of the copy poll the flag over PCIe and was -1.5 % there, so it stays opt-in.
+ static const int env = [] { const char* v = std::getenv("STRATA_FUSE_WAIT"); return v != nullptr ? std::atoi(v) : -1; }();
+ if (env >= 0) return env == 1 || env == 2;
+ return device_is_sm75();
+#endif
+}
+// 143-amdk's rows version of the folded wait (copy_rows_from_mapped_after): HIP with fuse_wait_on, CUDA STRATA_FUSE_WAIT=1
+bool fuse_rows_on() {
+#if defined(STRATA_USE_HIP)
+ return fuse_wait_on();
+#else
+ static const bool on = [] { const char* v = std::getenv("STRATA_FUSE_WAIT"); return v != nullptr && std::atoi(v) == 1; }();
+ return on;
#endif
}
@@ -1118,6 +1151,22 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
stamp(l, 1, grp);
float* xm = mixed_ + tb * N;
bool il_ready = false; // xil_ holds xq_'s interleaved copy (reset whenever xq_ is rewritten)
+ // a projection input's q8_1 blocks and their interleaved copy in one launch (native_quantize_q8_1_il, the same
+ // bytes): Turing default (+0.5..+1 % decode, two launches fewer per GDN layer and per QSA layer); STRATA_QIL=0 / 1
+ // forces it off / on
+ auto quant_in = [&](const float* x, int n_in) {
+#if !defined(STRATA_USE_HIP)
+ static const int qil_env = [] { const char* v = std::getenv("STRATA_QIL"); return v != nullptr ? std::atoi(v) : -1; }();
+ const bool qil = qil_env >= 0 ? qil_env != 0 : device_is_sm75();
+ if (qil && n >= 2 && n <= 4 && g_mmvq_il()) {
+ strata::kernels::native_quantize_q8_1_il(x, xq_, xil_, n_in, n, cs);
+ il_ready = true;
+ return;
+ }
+#endif
+ native_quantize_q8_1(x, xq_, n_in, n, cs);
+ il_ready = false;
+ };
auto mm = [&](const WeightRef* w, float* out, int n_in, int n_out) {
if (g_mmvq_il() && strata::kernels::native_mmvq_il_supported(w->native_type, n, n_out)) {
if (!il_ready && n >= 2) { // one column reads the plain q8_1 blocks
@@ -1148,7 +1197,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
float* hb = h_L_ + (size_t) gi * MT * C;
float* gate = gate_L_ + (size_t) gi * MT * HV;
float* beta = beta_L_ + (size_t) gi * MT * HV;
- if (!q8_attn) native_quantize_q8_1(xm, xq_, (int) N, n, cs);
+ if (!q8_attn) quant_in(xm, (int) N);
if (br) { // STRATA_DF_BRANCH: a/b and z beside q/k/v and the conv; all of them read only this layer's input
fork(0);
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
@@ -1212,8 +1261,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
stamp(l, 6, grp);
// STRATA_QFUSE: done above by gdn_step_norm_multi - but a batch's per-slot recurrence passes it no q8_1
// destination (#1139: the out-projection read stale bytes), so that case quantizes here as without it
- if (!g_qfuse() || batch_rec_) native_quantize_q8_1(y_ + (size_t) tb * ZV, xq_, (int) ZV, n, cs);
- il_ready = false;
+ if (!g_qfuse() || batch_rec_) quant_in(y_ + (size_t) tb * ZV, (int) ZV);
+ else il_ready = false;
mm(wout, bo_ + tb * N, (int) ZV, (int) N);
} else {
// ======================= QSA =======================
@@ -1256,7 +1305,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
return e != nullptr && std::atoi(e) != 0;
}();
const bool qb = dec_batch && n > 1 && native_qsa_enabled() && native_rope_enabled() && (!st.kv_q4 || qb_q4);
- if (!q8_attn) native_quantize_q8_1(xm, xq_, (int) N, n, cs);
+ if (!q8_attn) quant_in(xm, (int) N);
// STRATA_DF_BRANCH: the query side (q and its norm/RoPE/rotation on side 0, the indexer query on side 1)
// beside the K/V side; the indexer query joins before the block scores, the query before attention
const bool qbr = br && qb;
@@ -1439,8 +1488,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
qsa_gate_apply_f32(attn_ + t * NH * HD, qfull_ + t * NH * 2 * HD, s, attn32_ + t * NH * HD, cs);
}
stamp(l, 14, grp);
- native_quantize_q8_1(attn32_ + tb * NH * HD, xq_, (int) (NH * HD), n, cs);
- il_ready = false;
+ quant_in(attn32_ + tb * NH * HD, (int) (NH * HD));
mm(wo, bo_ + tb * N, (int) (NH * HD), (int) N);
}
} catch (const std::exception& e) {
@@ -1659,7 +1707,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
copy_or_zero_from_mapped(parts_out, m_ymiss_ + (size_t) tb * K * N, (long long) n * K * N,
skip_ + grp, ring, cs);
} else {
- const bool fuse_rows = fuse_wait_on() && !prof_on_ && !remote_opt_ && dec_batch; // 143-amdk
+ const bool fuse_rows = fuse_rows_on() && !prof_on_ && !remote_opt_ && dec_batch; // 143-amdk
if (!fuse_rows) wait_flag_ge(m_flag_, ring, cs); // the CPU's share is in the mapped rows
stamp(l, 23, grp);
if (remote_opt_) // #578: the helper's rows come back reduced; skip them as well
diff --git a/src/kernels/cuda/native_mmvq.cu b/src/kernels/cuda/native_mmvq.cu
index 3c7271649..20f4aa505 100644
--- a/src/kernels/cuda/native_mmvq.cu
+++ b/src/kernels/cuda/native_mmvq.cu
@@ -1590,6 +1590,33 @@ __global__ void native_q8_1_interleave_kernel(const Q81Block* __restrict__ y, in
if (p == 0) dl[std::size_t(b) * cp + c] = __low2float(blk.ds);
}
+// native_quantize_q8_1_kernel and native_q8_1_interleave_kernel in one launch: the same q8_1 blocks, and the
+// interleaved copy's ints and scales (an int is four codes, gathered from lanes 4p..4p+3 with two shuffles).
+__global__ void native_quantize_q8_1_il_kernel(const float* __restrict__ x, Q81Block* __restrict__ y,
+ int* __restrict__ bm, int* __restrict__ pm, float* __restrict__ dl,
+ int n_in, int n_total, int cp) {
+ const int i = int(blockIdx.x) * QUANT_THREADS + int(threadIdx.x);
+ if (i >= n_total) return; // n_total is a multiple of 32: only whole warps return
+ const float xi = x[i];
+ const float amax = warp_max(fabsf(xi));
+ const float sum = warp_sum(xi);
+ const float d = q8_1_finite(amax / 127.0f);
+ const int8_t q = q8_1_quant(xi, d, amax);
+ y[i / Q8K].qs[i % Q8K] = q;
+ const auto ds = q8_1_ds(d, sum);
+ if (i % Q8K == 0) y[i / Q8K].ds = ds;
+ const int lane = int(threadIdx.x) & 31;
+ unsigned v = unsigned(uint8_t(q)) << (8 * (lane & 3));
+ v |= __shfl_xor_sync(0xffffffffu, v, 1);
+ v |= __shfl_xor_sync(0xffffffffu, v, 2);
+ const int nb = n_in / Q8K, c = i / n_in, b = (i - c * n_in) / Q8K, pp = lane >> 2;
+ if ((lane & 3) == 0) {
+ bm[(std::size_t(b) * 8 + pp) * cp + c] = int(v);
+ pm[(std::size_t(pp) * nb + b) * cp + c] = int(v);
+ }
+ if (lane == 0) dl[std::size_t(b) * cp + c] = __low2float(ds);
+}
+
#endif // !__HIPCC__
void validate_shape(int n_in, int ncols, int block_elems = Q8K) {
@@ -2651,6 +2678,23 @@ std::size_t native_q8_1_il_bytes(int n_in, int ncols) {
return std::size_t(n_in / Q8K) * std::size_t(native_q8_1_il_cp(ncols)) * (16 * sizeof(int) + sizeof(float));
}
+void native_quantize_q8_1_il(const float* x, void* x_q8_1, void* x_il, int n_in, int ncols, void* stream) {
+ validate_shape(n_in, ncols);
+ if (ncols < 2 || ncols > 4) throw std::invalid_argument("native_quantize_q8_1_il requires 2 <= ncols <= 4");
+ validate_pointer(x);
+ validate_pointer(x_q8_1);
+ validate_pointer(x_il);
+ validate_stream(stream);
+ const int cp = native_q8_1_il_cp(ncols);
+ const Q81IlParts parts = q8_1_il_parts(x_il, n_in, ncols);
+ const int n_total = n_in * ncols;
+ native_quantize_q8_1_il_kernel<<<unsigned((n_total + QUANT_THREADS - 1) / QUANT_THREADS), QUANT_THREADS, 0,
+ static_cast<cudaStream_t>(stream)>>>(
+ x, static_cast<Q81Block*>(x_q8_1), const_cast<int*>(parts.bm), const_cast<int*>(parts.pm),
+ const_cast<float*>(parts.d), n_in, n_total, cp);
+ launch_check();
+}
+
void native_q8_1_interleave(const void* x_q8_1, void* x_il, int n_in, int ncols, void* stream) {
validate_shape(n_in, ncols);
if (ncols < 2 || ncols > 4) throw std::invalid_argument("native_q8_1_interleave requires 2 <= ncols <= 4");
@@ -2901,6 +2945,9 @@ void native_mmvq_il(int ggml_type, const void* weights, const void* x_q8_1, cons
#else // HIP: no interleaved path
std::size_t native_q8_1_il_bytes(int n_in, int ncols) { return native_q8_1_bytes(n_in, ncols) * 2; }
+void native_quantize_q8_1_il(const float*, void*, void*, int, int, void*) {
+ throw std::invalid_argument("native_quantize_q8_1_il is CUDA only");
+}
void native_q8_1_interleave(const void*, void*, int, int, void*) {
throw std::invalid_argument("native_q8_1_interleave is CUDA only");
}