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

gfx12 decode: the HC up kernel owns whole q8_1 groups and writes its consumer's image itself (STRATA_QFUSE_HC, no atomics), moe_hit_add folded into the combine (STRATA_FUSE_HITS); STRATA_FUSE_CONVAB (conv + a/b in one launch, opt-in, measured slower)

Niko1221a9917a552429
diff --git a/include/strata/kernels/native_moe.hpp b/include/strata/kernels/native_moe.hpp
index 6fc81076b..2bb2923e7 100644
--- a/include/strata/kernels/native_moe.hpp
+++ b/include/strata/kernels/native_moe.hpp
@@ -16,6 +16,13 @@ bool native_moe_combine_enabled();
void native_moe_combine(const float* parts, const float* weights, const float* shared,
float* output, int64_t n_embd, int64_t k, void* stream);
/// n_tok rows (parts [n,k,N], weights [n,k], shared/output [n,N]) in one launch, each as the single call.
+/// 143-amdk: native_moe_combine_multi with moe_hit_add folded in: for the rows listed in dst[0, *count) (entry index = token * k + expert,
+/// relative to `parts` and `hits`) the expert's part is parts + hits (the add moe_hit_add made in place), then the combine as before.
+/// Only the k = 10 vec4 case; `native_moe_combine_hits_supported` says whether a call may use it.
+bool native_moe_combine_hits_supported(const float* parts, const float* shared, const float* output, int64_t n_embd, int64_t k, int n_tok);
+void native_moe_combine_multi_hits_added(const float* parts, const float* hits, const int32_t* dst, const int32_t* count,
+ const float* weights, const float* shared, float* output, int64_t n_embd, int64_t k,
+ int n_tok, void* stream);
void native_moe_combine_multi(const float* parts, const float* weights, const float* shared, float* output,
int64_t n_embd, int64_t k, int n_tok, void* stream);
/// STRATA_VERIFY_RESIDENT: the same, reading every part as `0.0f + hit[i]` - bitwise what a zeroed part row plus
diff --git a/include/strata/kernels/verify_kernels.hpp b/include/strata/kernels/verify_kernels.hpp
index f7d3fe5b2..2cbd89978 100644
--- a/include/strata/kernels/verify_kernels.hpp
+++ b/include/strata/kernels/verify_kernels.hpp
@@ -28,6 +28,11 @@ void gdn_conv_l2_multi(const float* history, const float* qkv, const float* conv
/// history <- the last 3 entries of [history | qkv_0 .. qkv_{n-1}], n = *n_keep (0 leaves it as it was).
void gdn_conv_commit(float* history, const float* qkv, int channels, const int32_t* n_keep, void* stream);
/// alpha/beta for T columns of x (T, n_embd): gate (T, h_v), beta (T, h_v). Bitwise `fused_gdn_ab` per column.
+/// gdn_conv_l2_multi and gdn_ab_multi in one launch (143-amdk)
+void gdn_conv_ab_multi(const float* history, const float* qkv, const float* conv_w, float* h, int channels, int qk_heads,
+ float eps, int n_tok, int t_begin, bool commit, const float* x, const uint16_t* w_alpha,
+ const uint16_t* w_beta, const float* dt, const float* ssm_a, float* gate, float* beta, int n_embd,
+ int h_v, void* stream);
void gdn_ab_multi(const float* x, const uint16_t* w_alpha, const uint16_t* w_beta, const float* dt, const float* ssm_a,
float* gate, float* beta, int n_embd, int h_v, int n_tok, void* stream);
/// The recurrence + output norm for T tokens (h = (T, conv_channels) as q|k|v, gate/beta (T, h_v), z/y
diff --git a/src/core/verify.cpp b/src/core/verify.cpp
index c9e576835..3e4ff3f74 100644
--- a/src/core/verify.cpp
+++ b/src/core/verify.cpp
@@ -137,6 +137,22 @@ inline bool g_qdedup() { static const bool on = [] { const char* v = std::getenv
// fork F4 (Eddoursul): 2-4 token dense projections read an interleaved copy of the q8_1 activations (bitwise the same
// outputs); STRATA_MMVQ_IL=0 keeps native_mmvq's multi-column kernels
inline bool g_mmvq_il() { static const bool on = [] { const char* v = std::getenv("STRATA_MMVQ_IL"); return v == nullptr || v[0] != '0'; }(); return on; }
+// 143-amdk: STRATA_QFUSE_HC - only the hyper-connection read writes its consumer's q8_1 image (not the GDN recurrence): gfx12 default (=0 / 1 forces off / on)
+inline bool g_qfuse_hc() {
+#if defined(STRATA_USE_HIP)
+ static const bool on = [] {
+ const char* v = std::getenv("STRATA_QFUSE_HC");
+ if (v != nullptr) return std::atoi(v) != 0;
+ int dev = 0;
+ cudaDeviceProp p;
+ return cudaGetDevice(&dev) == cudaSuccess && cudaGetDeviceProperties(&p, dev) == cudaSuccess &&
+ strata::kernels::gfx_arch_is_gfx12(p.gcnArchName);
+ }();
+ return on;
+#else
+ return false;
+#endif
+}
inline bool g_qfuse() { static const bool on = [] { const char* v = std::getenv("STRATA_QFUSE"); return v != nullptr && std::atoi(v) != 0; }(); return on; }
#define VDBG(...) do { if (g_dbg) { std::fprintf(stderr, "verify dbg: " __VA_ARGS__); std::fflush(stderr); } } while (0)
@@ -230,6 +246,34 @@ bool fuse_wait_on() {
#endif
}
+// 143-amdk: STRATA_FUSE_CONVAB=1 - the GDN conv and the a/b projections in one launch. Opt-in: measured -0.3% on gfx1201 (the a/b dot chain is the longer one).
+bool fuse_convab_on() {
+#if defined(STRATA_USE_HIP)
+ static const bool on = [] { const char* v = std::getenv("STRATA_FUSE_CONVAB"); return v != nullptr && std::atoi(v) != 0; }();
+ return on;
+#else
+ return false;
+#endif
+}
+
+// 143-amdk: STRATA_FUSE_HITS - moe_hit_add folded into the combine (gfx12 default; =0 / 1 forces off / on)
+bool fuse_hits_on() {
+#if defined(STRATA_USE_HIP)
+ static const int env = [] { const char* v = std::getenv("STRATA_FUSE_HITS"); return v != nullptr ? (std::atoi(v) != 0 ? 1 : 0) : -1; }();
+ if (env >= 0) return env != 0;
+ 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]) {
+ cudaDeviceProp p;
+ per_dev[dev] = cudaGetDeviceProperties(&p, dev) == cudaSuccess && strata::kernels::gfx_arch_is_gfx12(p.gcnArchName) ? 2 : 1;
+ }
+ return per_dev[dev] == 2;
+#else
+ return false;
+#endif
+}
+
bool one_token_self_commit() {
#if defined(STRATA_USE_HIP)
static const bool on = [] {
@@ -740,7 +784,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
device_plan_ = !all_resident_ && !always_publish_ && (v != nullptr && std::atoi(v) != 0);
}
// (halo's STRATA_VERIFY_RESIDENT=1 is this window's all_resident_ graph above, which 0.1.39 has on by default)
- if (g_qfuse()) { // S26: the HC read's q8_1 group counters, zeroed once (each launch leaves them zero)
+ if (g_qfuse() || g_qfuse_hc()) { // S26: the HC read's q8_1 group counters, zeroed once (each launch leaves them zero)
if (cudaMalloc((void**) &qcnt_, sizeof(unsigned) * (size_t) (g.n_embd / 32)) != cudaSuccess ||
cudaMemset(qcnt_, 0, sizeof(unsigned) * (size_t) (g.n_embd / 32)) != cudaSuccess) {
cudaGetLastError();
@@ -1077,6 +1121,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
native_mmvq(wg->native_type, wg->native_data, xq_, z_ + (size_t) tb * ZV, (int) N, (int) ZV, n,
df_side_[1]);
}
+ const bool fuse_ab = fuse_convab_on() && !br && !batch_rec_ && !prof_on_;
mm(wqkv, qkv + (size_t) tb * C, (int) N, (int) C);
stamp(l, 2, grp);
if (batch_rec_) { // contiguous rows may be proposals for the same slot
@@ -1091,6 +1136,12 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
}
} else
// a one-token window keeps its token: it advances the conv history and the state itself (commit)
+ if (fuse_ab) // 143-amdk: the conv and the a/b projections in one launch
+ gdn_conv_ab_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, tb, self_commit,
+ xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
+ (const float*) wsa->data, gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) N,
+ (int) HV, cs);
+ else
gdn_conv_l2_multi(conv, qkv, (const float*) wc->data, hb, (int) C, (int) (2 * HK), EPS, n, cs, tb,
self_commit);
stamp(l, 3, grp);
@@ -1098,6 +1149,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
join(0);
join(1);
} else {
+ if (!fuse_ab)
gdn_ab_multi(xm, (const uint16_t*) wa->data, (const uint16_t*) wb->data, (const float*) wdt->data,
(const float*) wsa->data, gate + (size_t) tb * HV, beta + (size_t) tb * HV, (int) N, (int) HV,
n, cs);
@@ -1532,6 +1584,7 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
hit_xs_ + (size_t) tb * (N / 32), hit_scratch_, dst_buf, cs);
}
};
+ bool hits_in_combine = false;
if (ar_on()) {
stamp(l, 19, grp);
grouped(p_ptr, p_start, p_counts, 0, parts_out);
@@ -1583,7 +1636,11 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
else
copy_from_mapped(parts_out, m_ymiss_ + (size_t) tb * K * N, (int64_t) n * K * N, cs);
}
- moe_hit_add(parts_out, hit_out, p_dst, p_counts + 1, cap, N, cs);
+ // 143-amdk: the add of the GPU's hit rows is done by the combine that follows (one launch fewer)
+ hits_in_combine = fuse_hits_on() && !prof_on_ && !remote_opt_ && !sg_gated_[grp] && dec_batch && n > 1 &&
+ native_moe_combine_enabled() && n <= 8 &&
+ native_moe_combine_hits_supported(parts_ + (size_t) tb * K * N, shared_ + tb * N, bo_ + tb * N, N, K, n);
+ if (!hits_in_combine) moe_hit_add(parts_out, hit_out, p_dst, p_counts + 1, cap, N, cs);
}
// Same condition as `sh_fork` above, which is per group and out of scope here: wait only when the fork
// actually ran (a wait on an event never recorded is a no-op, but saying it outright reads better).
@@ -1595,6 +1652,12 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
native_moe_combine_multi_hits_gated(parts_out, w_ + tb * K, shared_ + tb * N, sh_g_ + tb, bo_ + tb * N, N, K, n, cs);
} catch (const std::exception& e) { err = "verify combine: " + std::string(e.what()); return false; }
} else
+ if (hits_in_combine) {
+ try {
+ native_moe_combine_multi_hits_added(parts_ + (size_t) tb * K * N, hit_out, p_dst, p_counts + 1, w_ + tb * K,
+ shared_ + tb * N, bo_ + tb * N, N, K, n, cs);
+ } catch (const std::exception& e) { err = "verify combine: " + std::string(e.what()); return false; }
+ } else
if (dec_batch && n > 1 && native_moe_combine_enabled()) { // one launch for the window's rows
try {
native_moe_combine_multi(parts_ + (size_t) tb * K * N, w_ + tb * K, shared_ + tb * N, bo_ + tb * N, N, K, n, cs);
diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu
index a0479d3cb..766ac2ede 100644
--- a/src/kernels/cuda/fused_gr.cu
+++ b/src/kernels/cuda/fused_gr.cu
@@ -375,6 +375,84 @@ __global__ void __launch_bounds__(THREADS) gr_up_multi_kernel(GrMulti m) {
}
+// 143-amdk (STRATA_QFUSE_HC): gr_up_multi_kernel with 32 columns a block (512 threads, 80 blocks, the same 8 rows a warp), so a block owns a whole
+// q8_1 group of `mixed` and writes its image itself. STRATA_QFUSE's version (16 columns a block) needs a counter, two fences and a last block
+// to finish the group; this one needs none. Every row's math is gr_up_multi_kernel's. Needs q8_mixed set for every token.
+constexpr int Q32_COLS = 32, Q32_THREADS = 512, Q32_WARPS = Q32_THREADS / 32, Q32_BLOCKS = N / Q32_COLS;
+template <int MAX_T = kFusedGrMaxT, bool EXACT_T = false>
+__global__ void __launch_bounds__(Q32_THREADS) gr_up_q32_kernel(GrMulti m) {
+ __shared__ __align__(16) float lo[MAX_T][LR];
+ __shared__ float g[MAX_T][HC][Q32_COLS];
+ __shared__ float mixed_s[MAX_T][Q32_COLS];
+ const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
+ const int T = EXACT_T ? MAX_T : m.T;
+ const int d0 = blockIdx.x * Q32_COLS;
+ for (int i = t; i < T * LR; i += Q32_THREADS) lo[i / LR][i % LR] = m.a[i / LR].lo[i % LR];
+ __syncthreads();
+ for (int r = warp; r < HC * Q32_COLS; r += Q32_WARPS) {
+ const int c = r / Q32_COLS, dd = r - c * Q32_COLS, i = c * N + d0 + dd;
+ const uint4* w4 = reinterpret_cast<const uint4*>(m.a[0].w_up + (size_t) i * LR);
+ const Bf16x8 wa = unpack8(__ldg(w4 + lane));
+ const Bf16x8 wb = unpack8(lane < LR / 8 - 32 ? __ldg(w4 + 32 + lane) : make_uint4(0, 0, 0, 0));
+ // the epilogue inputs of this lane's token, fetched while the dots run
+ float rv = 0.0f, wn = 0.0f, rsc = 0.0f, bo = 0.0f, ip = 0.0f;
+ bool apply = false;
+ if (lane < T) {
+ const FusedGrArgs& a = m.a[lane];
+ rv = a.R[i];
+ wn = a.w_norm[i];
+ rsc = a.rs[c];
+ apply = a.apply;
+ if (apply) { bo = a.bo_prev[d0 + dd]; ip = a.inj_prev[c]; }
+ }
+ float mine = 0.0f;
+#pragma unroll
+ for (int k = 0; k < MAX_T; ++k) {
+ if (!EXACT_T && k >= T) break;
+ float acc = dot8u_ptr(wa, lo[k] + lane * 8);
+ if (lane < LR / 8 - 32) acc += dot8u_ptr(wb, lo[k] + (32 + lane) * 8);
+ acc = warp_sum(acc);
+ if (lane == k) mine = acc;
+ }
+ if (lane < T) {
+ if (apply) {
+ rv = fmaf(bo, 2.0f * sigmoidf_(ip / (float) HC), rv);
+ m.a[lane].R_out[i] = rv;
+ }
+ const float x = rv * wn * rsc;
+ g[lane][c][dd] = x * sigmoidf_(mine);
+ }
+ }
+ __syncthreads();
+ for (int i = t; i < T * Q32_COLS; i += Q32_THREADS) {
+ const int k = i / Q32_COLS, col = i - k * Q32_COLS;
+ float s = 0.0f;
+#pragma unroll
+ for (int c = 0; c < HC; ++c) s += g[k][c][col];
+ const float v = s / (float) HC;
+ m.a[k].mixed[d0 + col] = v;
+ mixed_s[k][col] = v;
+ }
+ __syncthreads();
+ // the block owns the whole q8_1 group (32 columns): quantize it here, no counter, no fence. One warp a token, the
+ // quantizer of gr_q8_tail (native_quantize_q8_1's: XOR-tree max and sum, d = amax / 127, roundf(x / d)).
+ if (warp < T) {
+ const float xi = mixed_s[warp][lane];
+ float amax = fabsf(xi), sum = xi;
+#pragma unroll
+ for (int o = 16; o > 0; o >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));
+#pragma unroll
+ for (int o = 16; o > 0; o >>= 1) sum += __shfl_xor_sync(0xffffffffu, sum, o);
+ const float d = q8_1_finite(amax / 127.0f);
+ const int8_t q = q8_1_quant(xi, d, amax);
+ GrQ81* y = reinterpret_cast<GrQ81*>(m.a[warp].q8_mixed) + d0 / 32;
+ y->qs[lane] = q;
+ if (lane == 0) y->ds = q8_1_ds(d, sum);
+ }
+}
+
+
+
// ================================ hc read v3 (opt-in: STRATA_GR_V3=1) - two kernels, stream-split ================
// The norm kernel runs on only T blocks (~16 us of pure latency per call) and `down` on 41 blocks (~280 GB/s).
// v3: `down` is split over (row group, stream[, column half]) = 164 or 328 blocks; each block stages its slice of
@@ -944,6 +1022,11 @@ bool norm_fast_on() {
#endif
}
+// STRATA_QFUSE_HC_LOCAL=0 puts the atomic-tail up kernel back under STRATA_QFUSE_HC (default: the 32-column kernel)
+bool qfuse_hc_local() {
+ static const bool on = [] { const char* e = std::getenv("STRATA_QFUSE_HC_LOCAL"); return e == nullptr || std::atoi(e) != 0; }();
+ return on;
+}
void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long long* stamp_buf, int stamp_i0) {
const int n_tok = m.T;
#if STRATA_GR_FAST_BUILD
@@ -1009,6 +1092,20 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long
#if STRATA_GR_FAST_BUILD
if (fast) gr_up_fast_kernel<<<UPM_BLOCKS, THREADS, 0, st>>>(m);
else
+#endif
+#if !STRATA_GR_FAST_BUILD
+ if (m.a[0].q8_mixed != nullptr && qfuse_hc_local()) {
+ switch (no_multi_gr ? kFusedGrMaxT : n_tok) {
+ case 1: gr_up_q32_kernel<1, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
+ case 2: gr_up_q32_kernel<2, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
+ case 3: gr_up_q32_kernel<3, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
+ case 4: gr_up_q32_kernel<4, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
+ case 5: gr_up_q32_kernel<5, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
+ case 6: gr_up_q32_kernel<6, true><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
+ default: gr_up_q32_kernel<kFusedGrMaxT, false><<<Q32_BLOCKS, Q32_THREADS, 0, st>>>(m); break;
+ }
+ return;
+ }
#endif
switch (no_multi_gr ? kFusedGrMaxT : n_tok) {
case 1: gr_up_multi_kernel<1, true><<<UPM_BLOCKS, THREADS, 0, st>>>(m); break;
diff --git a/src/kernels/cuda/native_moe.cu b/src/kernels/cuda/native_moe.cu
index 456ab09c3..fc28c4a91 100644
--- a/src/kernels/cuda/native_moe.cu
+++ b/src/kernels/cuda/native_moe.cu
@@ -95,6 +95,49 @@ __global__ void combine_k10_vec4(const float4* __restrict__ parts4, const float*
sum.w += sh.w;
output4[c4] = sum;
}
+__global__ void combine_k10_vec4_hits(const float4* __restrict__ parts4, const float4* __restrict__ hits4,
+ const int32_t* __restrict__ dst, const int32_t* __restrict__ count,
+ const float* __restrict__ weights, const float4* __restrict__ shared4,
+ float4* __restrict__ output4, int64_t n4) {
+ const int64_t tk = blockIdx.y;
+ __shared__ int hit_mask;
+ if (threadIdx.x == 0) hit_mask = 0;
+ __syncthreads();
+ const int nh = *count;
+ for (int h = threadIdx.x; h < nh; h += blockDim.x) {
+ const int r = dst[h];
+ if (r / 10 == (int) tk) atomicOr(&hit_mask, 1 << (r % 10));
+ }
+ __syncthreads();
+ const int mask = hit_mask;
+ parts4 += tk * 10 * n4;
+ hits4 += tk * 10 * n4;
+ weights += tk * 10;
+ shared4 += tk * n4;
+ output4 += tk * n4;
+ const int64_t c4 = int64_t(blockIdx.x) * blockDim.x + threadIdx.x;
+ if (c4 >= n4) return;
+ const float w0 = __ldg(weights + 0);
+ float4 p0 = parts4[c4];
+ if (mask & 1) { const float4 hh = hits4[c4]; p0.x += hh.x; p0.y += hh.y; p0.z += hh.z; p0.w += hh.w; }
+ float4 sum = make_float4(p0.x * w0, p0.y * w0, p0.z * w0, p0.w * w0);
+#pragma unroll
+ for (int expert = 1; expert < 10; ++expert) {
+ const float w = __ldg(weights + expert);
+ float4 p = parts4[int64_t(expert) * n4 + c4];
+ if ((mask >> expert) & 1) { const float4 hh = hits4[int64_t(expert) * n4 + c4]; p.x += hh.x; p.y += hh.y; p.z += hh.z; p.w += hh.w; }
+ sum.x = fmaf(p.x, w, sum.x);
+ sum.y = fmaf(p.y, w, sum.y);
+ sum.z = fmaf(p.z, w, sum.z);
+ sum.w = fmaf(p.w, w, sum.w);
+ }
+ const float4 sh = shared4[c4];
+ sum.x += sh.x;
+ sum.y += sh.y;
+ sum.z += sh.z;
+ sum.w += sh.w;
+ output4[c4] = sum;
+}
bool valid_span(const void* p, size_t bytes) {
const auto address = reinterpret_cast<uintptr_t>(p);
return p && address % alignof(float) == 0 && bytes <= UINTPTR_MAX - address;
@@ -131,6 +174,22 @@ void native_moe_combine(const float* parts, const float* weights, const float* s
const auto error = cudaGetLastError();
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
}
+bool native_moe_combine_hits_supported(const float* parts, const float* shared, const float* output, int64_t n_embd, int64_t k, int n_tok) {
+ return k == 10 && shared != nullptr && n_tok >= 1 && n_tok <= 8 && n_embd > 0 && (n_embd & 3) == 0 &&
+ (((uintptr_t) parts | (uintptr_t) shared | (uintptr_t) output) & 15u) == 0;
+}
+void native_moe_combine_multi_hits_added(const float* parts, const float* hits, const int32_t* dst, const int32_t* count,
+ const float* weights, const float* shared, float* output, int64_t n_embd, int64_t k,
+ int n_tok, void* stream) {
+ if (!stream || !native_moe_combine_hits_supported(parts, shared, output, n_embd, k, n_tok) || ((uintptr_t) hits & 15u) != 0)
+ throw std::invalid_argument("native MoE combine (hits added) requires k = 10, aligned spans, 1..8 tokens");
+ const int64_t n4 = n_embd >> 2;
+ combine_k10_vec4_hits<<<dim3(unsigned((n4 + 127) / 128), (unsigned) n_tok), 128, 0, static_cast<cudaStream_t>(stream)>>>(
+ reinterpret_cast<const float4*>(parts), reinterpret_cast<const float4*>(hits), dst, count, weights,
+ reinterpret_cast<const float4*>(shared), reinterpret_cast<float4*>(output), n4);
+ const auto error = cudaGetLastError();
+ if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
+}
void native_moe_combine_multi(const float* parts, const float* weights, const float* shared, float* output,
int64_t n_embd, int64_t k, int n_tok, void* stream) {
if (!stream || n_embd <= 0 || k < 1 || k > 15 || n_tok < 1)
diff --git a/src/kernels/cuda/verify_kernels.cu b/src/kernels/cuda/verify_kernels.cu
index c7a2fe3e9..0c189ea7c 100644
--- a/src/kernels/cuda/verify_kernels.cu
+++ b/src/kernels/cuda/verify_kernels.cu
@@ -33,12 +33,12 @@ void check(const char* what) {
}
// `commit` (one token): the history then keeps it - [hist1, hist2, x_0], each thread its channel's, after reading them
-__global__ void __launch_bounds__(S) gdn_conv_l2_multi_kernel(float* hist, const float* __restrict__ qkv,
+__device__ __forceinline__ void gdn_conv_l2_body(const int bx, const int by, float* hist, const float* __restrict__ qkv,
const float* __restrict__ w, float* __restrict__ h,
int C, int qk_heads, float eps, int t_begin, bool commit) {
__shared__ float part[S / 32];
- const int t = t_begin + blockIdx.y;
- const int c = blockIdx.x * S + threadIdx.x;
+ const int t = t_begin + by;
+ const int c = bx * S + threadIdx.x;
// the window of token t: [hist0, hist1, hist2, x_0, ..., x_t], its last four entries
float win[3];
#pragma unroll
@@ -54,7 +54,7 @@ __global__ void __launch_bounds__(S) gdn_conv_l2_multi_kernel(float* hist, const
}
float sum = v0 * w[c * 4] + v1 * w[c * 4 + 1] + v2 * w[c * 4 + 2] + x * w[c * 4 + 3];
float y = sum / (1.0f + __expf(-sum));
- if ((int) blockIdx.x < qk_heads) {
+ if ((int) bx < qk_heads) {
float sq = y * y;
for (int o = 16; o > 0; o >>= 1) sq += __shfl_xor_sync(0xffffffffu, sq, o);
if ((threadIdx.x & 31) == 0) part[threadIdx.x >> 5] = sq;
@@ -65,6 +65,12 @@ __global__ void __launch_bounds__(S) gdn_conv_l2_multi_kernel(float* hist, const
h[(size_t) t * C + c] = y;
}
+__global__ void __launch_bounds__(S) gdn_conv_l2_multi_kernel(float* hist, const float* __restrict__ qkv,
+ const float* __restrict__ w, float* __restrict__ h,
+ int C, int qk_heads, float eps, int t_begin, bool commit) {
+ gdn_conv_l2_body(blockIdx.x, blockIdx.y, hist, qkv, w, h, C, qk_heads, eps, t_begin, commit);
+}
+
__global__ void gdn_conv_commit_kernel(float* __restrict__ hist, const float* __restrict__ qkv, int C,
const int32_t* __restrict__ n_keep) {
const int c = blockIdx.x * blockDim.x + threadIdx.x;
@@ -83,12 +89,12 @@ __global__ void gdn_conv_commit_kernel(float* __restrict__ hist, const float* __
}
template <int MAX_T = kVerifyMaxT, bool EXACT_T = false>
-__global__ void __launch_bounds__(64) gdn_ab_multi_kernel(const float* __restrict__ x, const uint16_t* __restrict__ wa,
+__device__ __forceinline__ void gdn_ab_body(const int bx, const float* __restrict__ x, const uint16_t* __restrict__ wa,
const uint16_t* __restrict__ wb,
const float* __restrict__ dt,
const float* __restrict__ ssm_a, float* __restrict__ gate,
float* __restrict__ beta, int n, int h_v, int T) {
- const int row = blockIdx.x * 2 + (threadIdx.x >> 5), lane = threadIdx.x & 31;
+ const int row = bx * 2 + (threadIdx.x >> 5), lane = threadIdx.x & 31;
if (row >= 2 * h_v) return;
const bool is_beta = row >= h_v;
const int r = is_beta ? row - h_v : row;
@@ -133,6 +139,33 @@ __global__ void __launch_bounds__(64) gdn_ab_multi_kernel(const float* __restric
}
}
+template <int MAX_T = kVerifyMaxT, bool EXACT_T = false>
+__global__ void __launch_bounds__(64) gdn_ab_multi_kernel(const float* __restrict__ x, const uint16_t* __restrict__ wa,
+ const uint16_t* __restrict__ wb,
+ const float* __restrict__ dt,
+ const float* __restrict__ ssm_a, float* __restrict__ gate,
+ float* __restrict__ beta, int n, int h_v, int T) {
+ gdn_ab_body<MAX_T, EXACT_T>(blockIdx.x, x, wa, wb, dt, ssm_a, gate, beta, n, h_v, T);
+}
+
+// 143-amdk (STRATA_FUSE_CONVAB): the conv + L2 norm and the a/b projections in ONE launch. They read different inputs (the qkv
+// projection and the layer input) and write different outputs, so they only share a node: blocks [0, conv) run the conv body,
+// the following blocks (token row 0 only) the a/b body, each exactly as its own kernel.
+template <int MAX_T, bool EXACT_T>
+__global__ void __launch_bounds__(S) gdn_conv_ab_kernel(int conv_blocks, float* hist, const float* __restrict__ qkv,
+ const float* __restrict__ w, float* __restrict__ h, int C, int qk_heads,
+ float eps, int t_begin, bool commit, const float* __restrict__ x,
+ const uint16_t* __restrict__ wa, const uint16_t* __restrict__ wb,
+ const float* __restrict__ dt, const float* __restrict__ ssm_a,
+ float* __restrict__ gate, float* __restrict__ beta, int n, int h_v, int T) {
+ const int ab_blocks = (2 * h_v + 1) / 2; // the a/b blocks first: their dot chain is the longer one, so it starts with the first wave
+ if ((int) blockIdx.x >= ab_blocks) {
+ gdn_conv_l2_body((int) blockIdx.x - ab_blocks, (int) blockIdx.y, hist, qkv, w, h, C, qk_heads, eps, t_begin, commit);
+ } else if (blockIdx.y == 0 && threadIdx.x < 64) {
+ gdn_ab_body<MAX_T, EXACT_T>((int) blockIdx.x, x, wa, wb, dt, ssm_a, gate, beta, n, h_v, T);
+ }
+}
+
// State-only commit kernel: each head's 128 independent state columns are split across 4 blocks of 32 columns
// (48 * 4 = 192 blocks of 128 threads across all SMs, vs 48 blocks of 512 threads), with no sq/o/norm/z/y work.
// DBG (STRATA_DBG_GDN=1, #937): a window count the launch never allowed (n_keep is read from device memory; n_max is the
@@ -883,6 +916,31 @@ void gdn_conv_l2_multi(const float* history, const float* qkv, const float* conv
check("gdn_conv_l2_multi");
}
+void gdn_conv_ab_multi(const float* history, const float* qkv, const float* conv_w, float* h, int channels, int qk_heads,
+ float eps, int n_tok, int t_begin, bool commit, const float* x, const uint16_t* w_alpha,
+ const uint16_t* w_beta, const float* dt, const float* ssm_a, float* gate, float* beta, int n_embd,
+ int h_v, void* stream) {
+ if (!history || !qkv || !conv_w || !h || channels % S != 0 || n_tok < 1 || n_tok > kVerifyMaxT || n_embd % 8 != 0 ||
+ (commit && (n_tok != 1 || t_begin != 0))) {
+ std::fprintf(stderr, "gdn_conv_ab_multi: invalid arguments\n");
+ std::exit(1);
+ }
+ const int conv_blocks = channels / S, ab_blocks = (2 * h_v + 1) / 2;
+ const dim3 grid((unsigned) (conv_blocks + ab_blocks), (unsigned) n_tok);
+ cudaStream_t st = (cudaStream_t) stream;
+ float* hist = const_cast<float*>(history);
+ switch (n_tok) {
+ case 1: gdn_conv_ab_kernel<1, true><<<grid, S, 0, st>>>(conv_blocks, hist, qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit, x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
+ case 2: gdn_conv_ab_kernel<2, true><<<grid, S, 0, st>>>(conv_blocks, hist, qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit, x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
+ case 3: gdn_conv_ab_kernel<3, true><<<grid, S, 0, st>>>(conv_blocks, hist, qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit, x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
+ case 4: gdn_conv_ab_kernel<4, true><<<grid, S, 0, st>>>(conv_blocks, hist, qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit, x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
+ case 5: gdn_conv_ab_kernel<5, true><<<grid, S, 0, st>>>(conv_blocks, hist, qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit, x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
+ case 6: gdn_conv_ab_kernel<6, true><<<grid, S, 0, st>>>(conv_blocks, hist, qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit, x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
+ default: gdn_conv_ab_kernel<kVerifyMaxT, false><<<grid, S, 0, st>>>(conv_blocks, hist, qkv, conv_w, h, channels, qk_heads, eps, t_begin, commit, x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
+ }
+ check("gdn_conv_ab_multi");
+}
+
void gdn_conv_commit(float* history, const float* qkv, int channels, const int32_t* n_keep, void* stream) {
gdn_conv_commit_kernel<<<(unsigned) ((channels + 255) / 256), 256, 0, (cudaStream_t) stream>>>(history, qkv,
channels, n_keep);