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

prefill: STRATA_GDN_CHUNKED=1 on gfx11, the prompt's DeltaNet recurrence in 16-token chunks on the matrix cores (opt-in)

Niko12211b1bd4b4a84b
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f17db2202..b7d4fd1d2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -701,6 +701,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
target_link_libraries(hip_gdn_rec_head PRIVATE strata_prefill strata_hip_runtime)
add_test(NAME hip_gdn_rec_head COMMAND hip_gdn_rec_head)
list(APPEND _strata_hip_tests hip_gdn_rec_head)
+ add_executable(hip_gdn_chunked tests/hip/gdn_chunked.cpp)
+ target_link_libraries(hip_gdn_chunked PRIVATE strata_prefill strata_hip_runtime)
+ add_test(NAME hip_gdn_chunked COMMAND hip_gdn_chunked)
+ add_test(NAME hip_gdn_chunked_off COMMAND hip_gdn_chunked --off)
+ add_test(NAME hip_gdn_chunked_default COMMAND hip_gdn_chunked --default)
+ list(APPEND _strata_hip_tests hip_gdn_chunked hip_gdn_chunked_off hip_gdn_chunked_default)
# #313: the RDNA3 / RDNA3.5 WMMA dense GEMMs (opt-in STRATA_WMMA_GEMM=1) against a reference
add_executable(hip_prefill_wmma_gemm_parity tests/hip/prefill_wmma_gemm_parity.cpp)
target_include_directories(hip_prefill_wmma_gemm_parity PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/prefill")
diff --git a/docs/AMD_HIP.md b/docs/AMD_HIP.md
index a8fe88349..132b2efed 100644
--- a/docs/AMD_HIP.md
+++ b/docs/AMD_HIP.md
@@ -153,6 +153,64 @@ pinned by this repository. For an offline build, point `STRATA_GGML_DIR` at a
checkout of that exact revision; using an arbitrary newer checkout changes the
dependency being tested.
+## Opt-in chunked prompt GDN on gfx11
+
+`STRATA_GDN_CHUNKED=1` runs the prompt's gated DeltaNet recurrence in 16-token chunks on the gfx11 matrix cores,
+with FP16 operands and FP32 accumulation, and fuses the output norm. It is off by default: rounding differs from
+the token-by-token FP32 recurrence. The CUDA switch of the same name uses a separate FP32 implementation.
+
+The HIP path accepts gfx1100, gfx1101, gfx1102, gfx1150 and gfx1151, and prompt chunks of at least 64 tokens.
+Other cards, shorter chunks, an output row stride not divisible by eight, and `STRATA_GDN_REC_HEADS` use the
+existing recurrence. Runtime measurements and numerical checks were run on gfx1151 only; building for another
+target does not validate its speed or answers. The synthetic test compares the FP16 output and carried FP32 state
+against the existing recurrence, and short cases against a double-precision CPU calculation. It covers partial
+chunks, padded rows, weak decay and a split request; the off/default tests require identical output and state bits.
+It does not establish model answer equivalence.
+
+`STRATA_GDN_CHUNKED=2`, which on CUDA skips the SM-count test, is the same as 1 here.
+
+Measured on a Ryzen AI Max+ 395 (gfx1151, ROCm 7.14.1), UD-IQ4_XS. One layer's recurrence and norm take 1.60x less time than
+gfx1151's default kernels at 16K and 32K tokens (`hip_gdn_chunked --bench` against `--bench-default`, three runs each: 20.92 ->
+13.11 ms at 16K, 42.14 -> 26.30 ms at 32K). A whole prompt: the same binary with only this switch changed, a fresh engine per
+side, 11 pairs in alternating order after a discarded one, the engine's `prompt_ms`, every prompt read in full (no reused
+tokens). Each engine reads a discarded 4K prompt first, because the first prompt after a start varies more (on those first
+prompts the same pairs gave 95% intervals of [1.004, 1.032] and [0.981, 1.015]); the prompts follow each other once
+`/proc/vmstat`'s page-migration and direct-reclaim counters have stood still for 3 s, and a pair in which either prompt still
+met page migration or direct reclaim was dropped and measured again (3 of the 69 pairs, all 32K with the switch on; kept in, 32K
+gives 1.020 and 1.015). "Section 5" is the nine variables of `STRIX_HALO.md` section 5 (with `STRATA_PF_SWITCH_MIN_T=2048`);
+"defaults" is the engine without them (section 4's gfx1151 defaults stay). Times are medians; the ratio is the geometric mean of
+the 11 off / on ratios with a 95% bootstrap interval; "faster" counts the pairs in which the switch was faster:
+
+| tokens | arm | off -> on | off / on | faster |
+|---:|---|---:|---:|---:|
+| 4,096 | section 5 | 3.703 -> 3.641 s | 1.018 [1.016, 1.020] | 11 of 11 |
+| 4,096 | defaults | 5.914 -> 5.829 s | 1.014 [1.010, 1.017] | 10 of 11 |
+| 32,768 | section 5 | 26.157 -> 25.589 s | 1.021 [1.020, 1.023] | 11 of 11 |
+| 32,768 | defaults | 44.082 -> 43.479 s | 1.015 [1.012, 1.017] | 11 of 11 |
+| 128,000 | section 5 | 107.149 -> 104.866 s | 1.021 [1.020, 1.023] | 11 of 11 |
+| 128,000 | defaults | 193.439 -> 191.055 s | 1.014 [1.012, 1.016] | 11 of 11 |
+
+So about 2% on top of section 5 and about 1.5% on the defaults. At 36 gated-DeltaNet layers the layer figures predict about
+0.56 s for a 32K prompt; 0.57 s (section 5) and 0.60 s (defaults) were measured.
+
+First-token KL against the same run without the switch, 12 prompts of 1.5K-24K tokens: UD-IQ4_XS 0.0067 mean (0.044 max) on the
+defaults and 0.0039 (0.027) on top of section 5; UD-Q4_K_XL 0.0063 (0.043) and 0.0040 (0.014); the same top token in all of
+them (across the 12 prompts the first token takes only two distinct values). Section 5's switches alone are 0.0068 (IQ4_XS) and
+0.0031 (Q4_K_XL) against the defaults, a different change that is listed for scale and not as a limit for this one. Only the
+first generated token was compared: a long generation and perplexity were not measured.
+
+Build and run these checks in the HIP build directory:
+
+```sh
+cmake --build build-hip --target hip_gdn_chunked hip_gdn_rec_head
+ctest --test-dir build-hip -R 'hip_gdn_(chunked|rec_head)' --output-on-failure
+for i in 1 2 3; do ./build-hip/hip_gdn_chunked --bench-default; ./build-hip/hip_gdn_chunked --bench; done
+```
+
+Each benchmark run reports the GPU-event median of seven runs after two warmups, the same nonzero initial state every run:
+`--bench` this path, `--bench-default` gfx1151's default kernels (`STRATA_GDN_PP=2`, `STRATA_GDN_NOY=1`). The switch is read
+once per process, so the two are separate runs. It times one layer's recurrence and norm, not a whole prompt.
+
## Model and serving configuration
Prepare a supported model pack and its MTP runtime using the existing tools.
diff --git a/docs/DETAILS.md b/docs/DETAILS.md
index 5bd5fbbbd..50e787252 100644
--- a/docs/DETAILS.md
+++ b/docs/DETAILS.md
@@ -1727,12 +1727,17 @@ None of these changes the default answers; each was measured against the default
engine runs (a fresh engine per side, warmed, 200-token greedy answers on three fixed prompts, medians; "pairs faster" counts
the pairs in which the switched arm won). They are here so you can try them on your own card.
-- **`STRATA_GDN_CHUNKED=1`: the DeltaNet recurrence of the prompt in chunks of 32 tokens** (sergiywith, #1372; WY form, FP32,
+- **`STRATA_GDN_CHUNKED=1`: the DeltaNet recurrence of the prompt, opt-in**. On CUDA it runs in chunks of 32 tokens
+ (sergiywith, #1372; WY form, FP32,
other bits than the default: first-token KL against the default was 0.002-0.009 nats on average, the same top token in
every prompt tried). The kernel needs 128 SMs in one wave, so by itself it runs only on a card with 128 or more
(RTX 5090: 1.8x on the recurrence). On smaller cards `STRATA_GDN_CHUNKED=2` forces it for a measurement, and it is
slower there: 0.73x on an RTX 3060 (28 SMs) and 0.87x on an RTX 5070 (48 SMs) at 2K-32K tokens (`gdn_rec_parity --bench`),
and the prompt time did not improve: RTX 5070 2K 1.01x, 8K 1.00x of the default's time; RTX 3060 2K, 8K, 16K 1.01x, no pair faster.
+ On HIP, gfx1100-gfx1102, gfx1150 and gfx1151 use 16-token WMMA chunks with FP16 operands and a fused output norm; other cards and short
+ chunks keep the existing path. It changes rounding too (first-token KL against the default 0.006-0.007 on average, the same top
+ token in 12 of 12 prompts); on a Radeon 8060S (gfx1151) the layer's recurrence is 1.6x faster and a 4K-128K prompt after a warm-up prompt 1.4-2.1%; see
+ [the HIP conditions and numerical checks](AMD_HIP.md#opt-in-chunked-prompt-gdn-on-gfx11).
- **`STRATA_FS_SLOTS=N`: the Foresight swap space** (q8atnight, #1348): N VRAM slots per layer, refilled on a copy stream
with the experts that just missed and with the ones the next layer's router predicts (`STRATA_FS_AHEAD=0` for the
misses only), so that an expert the cache lacks can be computed on the GPU instead of by the CPU. The model's own router
diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp
index f12f34b94..9cc17d221 100644
--- a/include/strata/prefill/kernels.hpp
+++ b/include/strata/prefill/kernels.hpp
@@ -63,7 +63,8 @@ void gdn_recurrence(float* state, const float* h, const float* gate, const float
int64_t ld16 = 0); // ld16: y16's row stride (0 = 6144; S23 STRATA_PF_PAD pads it)
/// The kernels behind gdn_recurrence, for the parity test: 0 = the column-split kernels + the norm kernel (or the
/// one-block-per-head kernel under STRATA_GDN_REC_HEADS), 1 = four lanes per column (no barrier per token) + the grid-stride norm, 2 = the
-/// one-block-per-head kernel (its fused norm rounds differently). 0 and 1 give the same bits.
+/// one-block-per-head kernel (its fused norm rounds differently). 0 and 1 give the same bits. On HIP, 0 and 1 run the opt-in
+/// chunked WMMA recurrence instead (gfx11, STRATA_GDN_CHUNKED=1, 64 tokens and more: other bits, see docs/AMD_HIP.md).
/// SYCL only: 6 = the Xe sub-group recurrence (16-lane sub-groups, one barrier per token) + the norm kernel, the same
/// bits as 0 on an Arc A770; gdn_recurrence takes it by default on Alchemist (STRATA_GDN_XE=0 off, =1 any Intel GPU).
void gdn_recurrence_variant(int variant, float* state, const float* h, const float* gate, const float* beta,
diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu
index f58a189c6..322cd1265 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -1469,6 +1469,340 @@ __global__ void __launch_bounds__(S) gdn_out_norm_loop_kernel(const float* __res
}
}
}
+// STRATA_GDN_CHUNKED=1 on the gfx11 matrix cores: the recurrence in chunks of 16 tokens (the WY form of the CUDA
+// chunked path above), FP16 operands and FP32 accumulation, the output norm fused. Per value head, gamma_t the
+// gate's sum from the chunk's start to t (inclusive; the code's sg) and S0 the state before the chunk:
+// A[t][i] = beta_t exp(gamma_t - gamma_i) k_t.k_i (i < t), T = (I + A)^-1, P[t][i] = exp(gamma_t - gamma_i) q_t.k_i (i <= t),
+// Y = beta (V - exp(gamma) K S0), D = T Y, o = exp(gamma) Q S0 + P D,
+// S = exp(gamma_last) S0 + K^T (exp(gamma_last - gamma) D).
+// gdn_wmma_prep_kernel computes T and P of every chunk at once (they do not depend on the state); gdn_wmma_scan_kernel
+// walks the chunks, a block per value head and a wave per 16 value columns, the state in the waves' WMMA accumulators
+// (FP32), each product a 16 x 16 x 16 WMMA: k, q, T, P, D and the state as an operand are rounded to FP16 (relative
+// 2^-11), so the output differs from the FP32 kernels by more than their rounding (tests/hip/gdn_chunked.cpp holds it
+// to the bounds of its Error::ok). The norm and the z gate run in the scan.
+#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1150__) || defined(__gfx1151__)
+#define STRATA_GDN_WMMA 1 // device pass with the gfx11 WMMA intrinsic (the list of gfx_arch_is_gfx11_wmma)
+#endif
+constexpr int GW_CK = 16; // tokens per chunk (a WMMA tile)
+constexpr int GW_NW = S / 16; // the scan's waves per block: one per 16 value columns
+constexpr int GW_VPK = HV / HK; // value heads per key head: the prep's waves per block
+constexpr int GW_KP = S + 8; // the scan's padded FP16 k / q rows
+constexpr int GW_AHEAD = 3; // the scan warms the cache this many chunks ahead
+constexpr int64_t kGdnWmmaMin = 64; // STRATA_GDN_CHUNKED=1 takes prompt chunks of this many tokens and more
+typedef _Float16 gw_h16 __attribute__((ext_vector_type(16)));
+typedef _Float16 gw_h8 __attribute__((ext_vector_type(8)));
+typedef _Float16 gw_h2 __attribute__((ext_vector_type(2)));
+typedef float gw_f8 __attribute__((ext_vector_type(8)));
+typedef float gw_f4 __attribute__((ext_vector_type(4))); // (HIP's float4 copies through memcpy, which kept a staged
+ // copy in scratch: a store that waits for the load)
+__device__ __forceinline__ gw_f8 gw_mma(gw_h16 a, gw_h16 b, gw_f8 c) {
+#if defined(STRATA_GDN_WMMA)
+ return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c);
+#else
+ (void) a; (void) b; (void) c;
+ __builtin_trap(); // not a gfx11 device pass: the runtime gate never launches it
+#endif
+}
+// An accumulator tile (lane l: rows 2v + (l >> 4) for v = 0..7, column l & 15) as a B operand (lane l: column l & 15,
+// rows 0..15, both halves of the wave alike): pairs of rows rounded to FP16 and packed, the other half's pairs fetched
+// across (permlanex16), then interleaved by a byte permute whose selector depends on the lane's half (gw_sel).
+struct GwSel { unsigned even, odd; };
+__device__ __forceinline__ GwSel gw_sel(int hi) { return hi ? GwSel{0x01000504u, 0x03020706u} : GwSel{0x05040100u, 0x07060302u}; }
+__device__ __forceinline__ gw_h16 gw_acc_to_b(gw_f8 x, GwSel ps) {
+ unsigned wd[8];
+#pragma unroll
+ for (int m = 0; m < 4; ++m) {
+ const gw_h2 p = {(_Float16) x[2 * m], (_Float16) x[2 * m + 1]};
+ const unsigned own = __builtin_bit_cast(unsigned, p);
+#if defined(STRATA_GDN_WMMA)
+ const unsigned other = __builtin_amdgcn_permlanex16(own, own, 0x76543210u, 0xfedcba98u, false, false);
+#else
+ const unsigned other = own;
+#endif
+ wd[2 * m] = __builtin_amdgcn_perm(other, own, ps.even);
+ wd[2 * m + 1] = __builtin_amdgcn_perm(other, own, ps.odd);
+ }
+ return __builtin_bit_cast(gw_h16, wd);
+}
+__device__ __forceinline__ gw_h16 gw_ld(const _Float16* p) { // 32 bytes of LDS, 16-byte aligned
+ const uint4 a = *reinterpret_cast<const uint4*>(p), b = *reinterpret_cast<const uint4*>(p + 8);
+ const uint4 ab[2] = {a, b};
+ return __builtin_bit_cast(gw_h16, ab);
+}
+// The wave's own LDS writes complete (read back across its lanes). A gfx11 instruction: gfx12 device passes never run these kernels.
+__device__ __forceinline__ void gw_lds_wait() {
+#if defined(STRATA_GDN_WMMA)
+ __asm__ volatile("s_waitcnt lgkmcnt(0)" ::: "memory");
+#endif
+}
+// A barrier over LDS only. __syncthreads() is a workgroup fence over all memory, which on gfx11 also waits for every
+// global load in flight: the scan's loads of the next chunk would have to land before each barrier.
+__device__ __forceinline__ void gw_lds_barrier() {
+#if defined(STRATA_GDN_WMMA)
+ __asm__ volatile("s_waitcnt lgkmcnt(0)\n\ts_barrier" ::: "memory");
+#else
+ __syncthreads(); // the gfx11-only instruction must not reach a gfx12 device pass
+#endif
+}
+
+// A block per (key head, chunk) - the grid's x the key head, so the blocks in flight read the same token rows (blocks
+// that walked the key heads' slices of far-apart rows took 14 ms against 2.2 ms for 16K tokens on gfx1151) - and a wave per value head of the key head.
+// T then P, FP16 row-major (1 KB), at tp[(chunk * HV + value head) * 512].
+__global__ void __launch_bounds__(32 * GW_VPK) gdn_wmma_prep_kernel(const float* __restrict__ h, const float* __restrict__ gate,
+ const float* __restrict__ beta, _Float16* __restrict__ tp,
+ int64_t T) {
+ __shared__ float sa[GW_VPK][GW_CK * GW_CK], sg[GW_VPK][GW_CK], sb[GW_VPK][GW_CK];
+ const int qh = blockIdx.x, ch = blockIdx.y, w = threadIdx.x >> 5, l = threadIdx.x & 31, c = l & 15, hi = l >> 4;
+ const int vh = qh + w * HK;
+ const int64_t t0 = (int64_t) ch * GW_CK;
+ gw_f8 kk = {}, qk = {}; // k_t.k_i and q_t.k_i (lane: column i = c, rows t = 2v + hi)
+ {
+ const bool ok = t0 + c < T; // past the end: zero rows
+ const float* ht = h + (ok ? t0 + c : 0) * C;
+#pragma unroll
+ for (int j = 0; j < S / 16; ++j) {
+ gw_h16 ka, qa;
+#pragma unroll
+ for (int i = 0; i < 4; ++i) {
+ const gw_f4 k4 = *reinterpret_cast<const gw_f4*>(ht + HK * S + qh * S + 16 * j + 4 * i);
+ const gw_f4 q4 = *reinterpret_cast<const gw_f4*>(ht + qh * S + 16 * j + 4 * i);
+#pragma unroll
+ for (int e = 0; e < 4; ++e) {
+ ka[4 * i + e] = (_Float16) (ok ? k4[e] : 0.0f);
+ qa[4 * i + e] = (_Float16) (ok ? q4[e] : 0.0f);
+ }
+ }
+ kk = gw_mma(ka, ka, kk); // B = K^T: lane i holds column i of K^T, row i of K - the A operand itself
+ qk = gw_mma(qa, ka, qk);
+ }
+ }
+ if (l < GW_CK) { // past the end: gate 0, beta 0
+ const bool ok = t0 + l < T;
+ float g = ok ? gate[(t0 + l) * HV + vh] : 0.0f;
+#pragma unroll
+ for (int o = 1; o < GW_CK; o <<= 1) {
+ const float u = __shfl_up(g, o, GW_CK);
+ if (l >= o) g += u;
+ }
+ sg[w][l] = g;
+ sb[w][l] = ok ? beta[(t0 + l) * HV + vh] : 0.0f;
+ }
+ gw_lds_wait(); // the wave's own LDS writes, read across its lanes
+ _Float16* out = tp + ((int64_t) ch * HV + vh) * (2 * GW_CK * GW_CK);
+#pragma unroll
+ for (int v = 0; v < 8; ++v) {
+ const int t = 2 * v + hi;
+ sa[w][t * GW_CK + c] = c < t ? sb[w][t] * __expf(sg[w][t] - sg[w][c]) * kk[v] : 0.0f;
+ out[GW_CK * GW_CK + t * GW_CK + c] = (_Float16) (c <= t ? __expf(sg[w][t] - sg[w][c]) * qk[v] : 0.0f);
+ }
+ gw_lds_wait();
+ if (l < GW_CK) { // lane c: column c of T = (I + A)^-1 by forward substitution
+ float tc[GW_CK];
+#pragma unroll
+ for (int t = 0; t < GW_CK; ++t) {
+ float v = t == c ? 1.0f : 0.0f;
+#pragma unroll
+ for (int k = 0; k < t; ++k) v = fmaf(-sa[w][t * GW_CK + k], tc[k], v);
+ tc[t] = v;
+ out[t * GW_CK + c] = (_Float16) v;
+ }
+ }
+}
+
+// A thread's slice of a chunk in the scan (token threadIdx / 16, columns (threadIdx % 16) * 8 .. + 7): k, q and v,
+// loaded a chunk ahead, and one word of the chunk's T and P; lanes 0..15 of wave 0 also gate and beta of token l.
+// Rows past the end read the last row (finite: their gate and beta become 0 at staging, which keeps them out of the
+// state, of the real rows' products and of the output) - no select on the loaded values, which would wait for them.
+struct GwChunk {
+ gw_f4 k0, k1, q0, q1, v0, v1;
+ float g, b;
+ unsigned tp;
+};
+__device__ __forceinline__ void gw_load(GwChunk& in, const float* __restrict__ h, const float* __restrict__ gate,
+ const float* __restrict__ beta, const unsigned* __restrict__ tp, int64_t t0,
+ int64_t T, int vh, int qh) {
+ const int t = threadIdx.x >> 4, r = (threadIdx.x & 15) * 8;
+ const float* ht = h + std::min<int64_t>(t0 + t, T - 1) * C;
+ in.tp = tp[((t0 / GW_CK) * HV + vh) * (GW_CK * GW_CK) + threadIdx.x];
+ in.k0 = *reinterpret_cast<const gw_f4*>(ht + HK * S + qh * S + r);
+ in.k1 = *reinterpret_cast<const gw_f4*>(ht + HK * S + qh * S + r + 4);
+ in.q0 = *reinterpret_cast<const gw_f4*>(ht + qh * S + r);
+ in.q1 = *reinterpret_cast<const gw_f4*>(ht + qh * S + r + 4);
+ in.v0 = *reinterpret_cast<const gw_f4*>(ht + 2 * HK * S + vh * S + r);
+ in.v1 = *reinterpret_cast<const gw_f4*>(ht + 2 * HK * S + vh * S + r + 4);
+ if (threadIdx.x < GW_CK) {
+ const int64_t tl = std::min<int64_t>(t0 + threadIdx.x, T - 1);
+ in.g = gate[tl * HV + vh];
+ in.b = beta[tl * HV + vh];
+ }
+}
+// Brings a later chunk's lines (this block's k, q, v and z rows: 256 lines of 128 bytes, one a thread) into the cache
+// with a load whose result is not used, so the real loads of that chunk hit the cache. An ordinary load: the compiler
+// tracks it, and the result is consumed (by an empty asm) one chunk later, after the store of the previous chunk's output
+// rows, where the wait for the loads issued before that point also covers it.
+__device__ __forceinline__ unsigned gw_touch(const float* __restrict__ h, const float* __restrict__ z, int64_t t0,
+ int64_t T, int vh, int qh) {
+ const int t = threadIdx.x >> 4, part = threadIdx.x & 15, kind = part >> 2;
+ if (t0 + t >= T) return 0u;
+ const float* p = kind == 0 ? h + (t0 + t) * C + HK * S + qh * S
+ : kind == 1 ? h + (t0 + t) * C + qh * S
+ : kind == 2 ? h + (t0 + t) * C + 2 * HK * S + vh * S
+ : z + (t0 + t) * HV * S + vh * S;
+ return __float_as_uint(p[32 * (part & 3)]);
+}
+// Per chunk: stage it (k / q FP16, k also transposed, v, the gate's running sum, beta, T and P); barrier (1); store
+// the previous chunk's output rows (the z gate on its normed output, 16 bytes a thread); X = K S0, QS = Q S0; Y, D,
+// o and the wave's sums of o^2; the state update; barrier (2); the norm. LDS is single-buffered: an array written in
+// the staging of chunk c + 1 or after its barrier (2) was last read before that wave reached barrier (1) of c + 1.
+__global__ void __launch_bounds__(GW_NW * 32) gdn_wmma_scan_kernel(float* __restrict__ state, const float* __restrict__ h,
+ const float* __restrict__ gate, const float* __restrict__ beta,
+ const float* __restrict__ z, const float* __restrict__ gamma,
+ const unsigned* __restrict__ tp, float eps,
+ uint16_t* __restrict__ y16, int64_t T, int64_t ld16) {
+ __shared__ __attribute__((aligned(16))) _Float16 sk[GW_CK * GW_KP], sq[GW_CK * GW_KP], skt[S * GW_CK],
+ stp[2 * GW_CK * GW_CK];
+ __shared__ __attribute__((aligned(16))) float sv[GW_CK * S], so[GW_CK * S], sn[GW_CK * GW_NW];
+ __shared__ float sg[GW_CK], sb[GW_CK], sgam[S];
+ const _Float16 *st_ = stp, *sp_ = stp + GW_CK * GW_CK; // T, P
+ const int vh = blockIdx.x, qh = vh % HK, w = threadIdx.x >> 5, l = threadIdx.x & 31, c = l & 15, hi = l >> 4;
+ const int col = 16 * w + c; // this lane's column in the products
+ const int st = threadIdx.x >> 4, sr = (threadIdx.x & 15) * 8; // this thread's staging slice
+ const size_t rs = (size_t) HV * S;
+ const GwSel ps = gw_sel(hi);
+ gw_f8 s[S / 16]; // the state: rows 16j + 2v + hi, column col
+#pragma unroll
+ for (int j = 0; j < S / 16; ++j)
+#pragma unroll
+ for (int v = 0; v < 8; ++v) s[j][v] = state[(size_t) (16 * j + 2 * v + hi) * rs + (size_t) vh * S + col];
+ if (threadIdx.x < S) sgam[threadIdx.x] = gamma[threadIdx.x];
+ const float out_scale = rsqrtf((float) S);
+ GwChunk cur;
+ gw_load(cur, h, gate, beta, tp, 0, T, vh, qh);
+ unsigned touched = 0;
+ gw_f4 z0 = {}, z1 = {}; // the z slice of the chunk whose output rows are stored next
+ auto store_rows = [&](int64_t c0) { // the output rows of the chunk at c0 (its norm in so): z gate, FP16
+ if (c0 + st >= T) return;
+ const float* o = &so[st * S + sr];
+ gw_h8 r;
+#pragma unroll
+ for (int i = 0; i < 8; ++i) r[i] = (_Float16) (o[i] * sigm(i < 4 ? z0[i] : z1[i - 4]));
+ *reinterpret_cast<uint4*>(y16 + (c0 + st) * ld16 + vh * S + sr) = __builtin_bit_cast(uint4, r);
+ };
+ for (int64_t t0 = 0; t0 < T; t0 += GW_CK) {
+ { // stage this chunk
+ gw_h8 kh, qq;
+#pragma unroll
+ for (int i = 0; i < 8; ++i) {
+ kh[i] = (_Float16) (i < 4 ? cur.k0[i] : cur.k1[i - 4]);
+ qq[i] = (_Float16) (i < 4 ? cur.q0[i] : cur.q1[i - 4]);
+ skt[(sr + i) * GW_CK + st] = kh[i];
+ }
+ *reinterpret_cast<uint4*>(&sk[st * GW_KP + sr]) = __builtin_bit_cast(uint4, kh);
+ *reinterpret_cast<uint4*>(&sq[st * GW_KP + sr]) = __builtin_bit_cast(uint4, qq);
+ *reinterpret_cast<gw_f4*>(&sv[st * S + sr]) = cur.v0;
+ *reinterpret_cast<gw_f4*>(&sv[st * S + sr + 4]) = cur.v1;
+ reinterpret_cast<unsigned*>(stp)[threadIdx.x] = cur.tp;
+ if (threadIdx.x < GW_CK) { // past the end: gate 0, beta 0
+ const bool ok = t0 + l < T;
+ float g = ok ? cur.g : 0.0f;
+#pragma unroll
+ for (int o = 1; o < GW_CK; o <<= 1) {
+ const float u = __shfl_up(g, o, GW_CK);
+ if (l >= o) g += u;
+ }
+ sg[l] = g;
+ sb[l] = ok ? cur.b : 0.0f;
+ }
+ }
+ if (t0 + GW_CK < T) gw_load(cur, h, gate, beta, tp, t0 + GW_CK, T, vh, qh); // lands during this chunk
+ const unsigned touch_now = gw_touch(h, z, t0 + GW_AHEAD * GW_CK, T, vh, qh);
+ gw_lds_barrier(); // (1) this chunk staged; the previous chunk's norm in so
+ if (t0 > 0) store_rows(t0 - GW_CK);
+ __asm__ volatile("" ::"v"(touched)); // the previous touch, consumed here
+ touched = touch_now;
+ { // this chunk's z slice, for its output rows after barrier (1) of the next chunk
+ const int64_t tz = std::min<int64_t>(t0 + st, T - 1);
+ z0 = *reinterpret_cast<const gw_f4*>(z + tz * HV * S + vh * S + sr);
+ z1 = *reinterpret_cast<const gw_f4*>(z + tz * HV * S + vh * S + sr + 4);
+ }
+ gw_f8 x = {}, qs = {}; // K S0 and Q S0, this wave's columns
+#pragma unroll
+ for (int j = 0; j < S / 16; ++j) {
+ const gw_h16 b = gw_acc_to_b(s[j], ps);
+ x = gw_mma(gw_ld(sk + c * GW_KP + 16 * j), b, x);
+ qs = gw_mma(gw_ld(sq + c * GW_KP + 16 * j), b, qs);
+ }
+ gw_f8 y;
+#pragma unroll
+ for (int v = 0; v < 8; ++v) {
+ const int t = 2 * v + hi;
+ y[v] = sb[t] * (sv[t * S + col] - __expf(sg[t]) * x[v]);
+ }
+ const gw_f8 d = gw_mma(gw_ld(st_ + c * GW_CK), gw_acc_to_b(y, ps), gw_f8{});
+ gw_f8 o;
+#pragma unroll
+ for (int v = 0; v < 8; ++v) o[v] = __expf(sg[2 * v + hi]) * qs[v];
+ o = gw_mma(gw_ld(sp_ + c * GW_CK), gw_acc_to_b(d, ps), o);
+#pragma unroll
+ for (int v = 0; v < 8; ++v) { // the wave's 16 columns of o^2 per token, then the 8 waves' through LDS
+ o[v] *= out_scale;
+ float p = o[v] * o[v];
+#pragma unroll
+ for (int m = 1; m < 16; m <<= 1) p += __shfl_xor(p, m);
+ if (c == 0) sn[(2 * v + hi) * GW_NW + w] = p;
+ }
+ const float gl = sg[GW_CK - 1];
+ gw_f8 dd;
+#pragma unroll
+ for (int v = 0; v < 8; ++v) dd[v] = __expf(gl - sg[2 * v + hi]) * d[v];
+ const gw_h16 db = gw_acc_to_b(dd, ps);
+ const float eg = __expf(gl);
+#pragma unroll
+ for (int j = 0; j < S / 16; ++j) s[j] = gw_mma(gw_ld(skt + (16 * j + c) * GW_CK), db, s[j] * eg);
+ gw_lds_barrier(); // (2) the sums of o^2
+ const float g_col = sgam[col];
+#pragma unroll
+ for (int v = 0; v < 8; ++v) {
+ const int t = 2 * v + hi;
+ const gw_f4 n0 = *reinterpret_cast<const gw_f4*>(&sn[t * GW_NW]), n1 = *reinterpret_cast<const gw_f4*>(&sn[t * GW_NW + 4]);
+ const float ss = n0[0] + n0[1] + n0[2] + n0[3] + n1[0] + n1[1] + n1[2] + n1[3];
+ so[t * S + col] = o[v] * rsqrtf(ss * (1.0f / S) + eps) * g_col;
+ }
+ }
+ __asm__ volatile("" ::"v"(touched));
+ gw_lds_barrier();
+ store_rows((T - 1) / GW_CK * GW_CK);
+ __asm__ volatile("" : "+s"(state)); // the state's addresses recomputed here, not kept (spilled) since the start
+#pragma unroll
+ for (int j = 0; j < S / 16; ++j)
+#pragma unroll
+ for (int v = 0; v < 8; ++v) state[(size_t) (16 * j + 2 * v + hi) * rs + (size_t) vh * S + col] = s[j][v];
+}
+// The opt-in chunked recurrence on gfx11 (STRATA_GDN_CHUNKED=1): false (nothing launched) on another part. y, the
+// FP32 scratch of the other kernels (T x 6144 floats), holds T and P (1 KB per chunk and value head: T x 3 KB).
+bool gdn_rec_wmma(float* state, const float* h, const float* gate, const float* beta, const float* z, const float* gamma,
+ float eps, float* y, uint16_t* y16, int64_t T, cudaStream_t s, int64_t ld16) {
+ if (T < kGdnWmmaMin || ld16 % 8 != 0) return false; // the rows are stored 16 bytes at a time
+ // A layer-split worker can change devices: a process-wide answer for the first card can launch a trapping
+ // gfx11 kernel on a later gfx12 card. Cache only the current thread's last device, as properties are not cheap.
+ static thread_local int last_device = -1;
+ static thread_local bool gfx11 = false;
+ int dev = 0;
+ if (cudaGetDevice(&dev) != cudaSuccess) return false;
+ if (dev != last_device) {
+ cudaDeviceProp p{};
+ if (cudaGetDeviceProperties(&p, dev) != cudaSuccess) return false;
+ gfx11 = strata::kernels::gfx_arch_is_gfx11_wmma(p.gcnArchName);
+ last_device = dev;
+ }
+ if (!gfx11) return false;
+ const int64_t nch = (T + GW_CK - 1) / GW_CK;
+ _Float16* tp = reinterpret_cast<_Float16*>(y);
+ gdn_wmma_prep_kernel<<<dim3(HK, (unsigned) nch), 32 * GW_VPK, 0, s>>>(h, gate, beta, tp, T);
+ gdn_wmma_scan_kernel<<<HV, GW_NW * 32, 0, s>>>(state, h, gate, beta, z, gamma, reinterpret_cast<const unsigned*>(tp),
+ eps, y16, T, ld16);
+ return true;
+}
#endif // __HIPCC__
__global__ void __launch_bounds__(S) gdn_out_norm_kernel(const float* __restrict__ z, const float* __restrict__ gamma,
float eps, float* __restrict__ y, uint16_t* __restrict__ y16,
@@ -1939,7 +2273,17 @@ void gdn_recurrence_variant(int variant, float* state, const float* h, const flo
const float* z, const float* gamma, float eps, float* y, uint16_t* y16, int64_t T,
void* stream, int64_t ld16) {
if (ld16 <= 0) ld16 = (int64_t) HV * S;
+ static const bool serial = std::getenv("STRATA_GDN_REC_HEADS") != nullptr; // the one-block-per-head kernel (A/B)
#if defined(__HIPCC__)
+ if (variant == 0 || variant == 1) {
+ // STRATA_GDN_CHUNKED=1 (opt-in): the chunked recurrence on the gfx11 matrix cores, FP16 operands (other bits;
+ // a shorter chunk or another part takes the kernels below)
+ static const bool chunked = [] { const char* v = std::getenv("STRATA_GDN_CHUNKED"); return v != nullptr && std::atoi(v) != 0; }();
+ if (chunked && !serial && gdn_rec_wmma(state, h, gate, beta, z, gamma, eps, y, y16, T, (cudaStream_t) stream, ld16)) {
+ check("gdn_recurrence (chunked, WMMA)");
+ return;
+ }
+ }
if (variant == 3) { // diagnostics: the quad recurrence + the old norm kernel
if (T > 0) {
gdn_rec_quad_kernel<<<HV * (S / QCB), QTH, 0, (cudaStream_t) stream>>>(state, h, gate, beta, y, T);
@@ -1982,7 +2326,6 @@ void gdn_recurrence_variant(int variant, float* state, const float* h, const flo
#else
(void) variant; // the quad recurrence kernels are AMD-only
#endif
- static const bool serial = std::getenv("STRATA_GDN_REC_HEADS") != nullptr; // the one-block-per-head kernel (A/B)
if (serial || T <= 0 || variant == 2) {
gdn_rec_kernel<<<HV, dim3(S, RG), 0, (cudaStream_t) stream>>>(state, h, gate, beta, z, gamma, eps, y, y16, T, ld16);
} else {
diff --git a/tests/hip/gdn_chunked.cpp b/tests/hip/gdn_chunked.cpp
new file mode 100644
index 000000000..a0f29cf26
--- /dev/null
+++ b/tests/hip/gdn_chunked.cpp
@@ -0,0 +1,235 @@
+// Exercise the public opt-in dispatch, including tails, padded rows and a nonzero carried state. The FP32
+// recurrence is the all-head reference; short cases also use an independent double-precision CPU recurrence.
+#include "strata/prefill/kernels.hpp"
+#include "strata/kernels/gfx_arch.hpp"
+#include <cuda_runtime.h>
+#include <cuda_fp16.h>
+#include <algorithm>
+#include <cmath>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <random>
+#include <vector>
+
+namespace {
+constexpr int S = 128, HK = 16, HV = 48, C = 10240, ROW = HV * S;
+constexpr uint16_t SENTINEL = 0x7bff;
+void ck(cudaError_t e, const char* where) {
+ if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", where, cudaGetErrorString(e)); std::exit(2); }
+}
+void setting(const char* key, const char* value) {
+#ifdef _WIN32
+ if (_putenv_s(key, value)) std::exit(2);
+#else
+ if (setenv(key, value, 1)) std::exit(2);
+#endif
+}
+template<class X> X* upload(const std::vector<X>& v) {
+ X* p = nullptr;
+ ck(cudaMalloc(&p, v.size() * sizeof(X)), "malloc");
+ ck(cudaMemcpy(p, v.data(), v.size() * sizeof(X), cudaMemcpyHostToDevice), "upload");
+ return p;
+}
+template<class X> std::vector<X> download(const X* p, size_t n) {
+ std::vector<X> v(n);
+ ck(cudaMemcpy(v.data(), p, n * sizeof(X), cudaMemcpyDeviceToHost), "download");
+ return v;
+}
+float half(uint16_t bits) { __half_raw x; x.x = bits; return __half2float(__half(x)); }
+struct Error {
+ double square = 0, reference = 0, max_error = 0, max_reference = 0;
+ bool finite = true;
+ void add(double a, double b) {
+ finite &= std::isfinite(a) && std::isfinite(b);
+ square += (a - b) * (a - b); reference += a * a;
+ max_error = std::max(max_error, std::fabs(a - b)); max_reference = std::max(max_reference, std::fabs(a));
+ }
+ double rms() const { return std::sqrt(square / std::max(reference, 1e-30)); }
+ bool ok() const { return finite && rms() <= 0.003 && max_error <= 0.02 * max_reference + 1e-5; }
+};
+struct Input {
+ int64_t n;
+ std::vector<float> h, gate, beta, z, gamma, state;
+ Input(int64_t t, float decay, float scale, bool no_update) : n(t), h(std::max<int64_t>(t, 1) * C),
+ gate(std::max<int64_t>(t, 1) * HV), beta(gate.size()), z(std::max<int64_t>(t, 1) * ROW),
+ gamma(S), state(S * ROW) {
+ std::mt19937 rng(11);
+ std::normal_distribution<float> normal(0, 1);
+ std::uniform_real_distribution<float> uniform(0, 1);
+ for (auto& x : h) x = normal(rng);
+ for (int64_t i = 0; i < t; ++i) {
+ float* row = h.data() + i * C;
+ for (int head = 0; head < 2 * HK; ++head) {
+ double norm = 0;
+ for (int d = 0; d < S; ++d) norm += double(row[head * S + d]) * row[head * S + d];
+ for (int d = 0; d < S; ++d) row[head * S + d] /= std::sqrt(norm + 1e-6);
+ }
+ for (int d = 2 * HK * S; d < C; ++d) row[d] *= scale;
+ }
+ for (auto& x : gate) x = -decay * uniform(rng);
+ for (auto& x : beta) x = no_update ? 0 : uniform(rng);
+ for (auto& x : z) x = normal(rng);
+ for (auto& x : gamma) x = 0.5f + uniform(rng);
+ for (auto& x : state) x = 0.1f * scale * normal(rng);
+ }
+};
+struct Output { std::vector<uint16_t> y; std::vector<float> state; };
+Output run(const Input& in, int ld, bool reference, int64_t split = 0) {
+ auto h = upload(in.h), g = upload(in.gate), b = upload(in.beta), z = upload(in.z), gm = upload(in.gamma);
+ auto state = upload(in.state);
+ auto y = upload(std::vector<uint16_t>(std::max<int64_t>(in.n, 1) * ld + 8, SENTINEL));
+ // The chunked path owns y as temporary storage; only y16 and state are observable outputs.
+ auto scratch = upload(std::vector<float>(std::max<int64_t>(in.n, 1) * ROW));
+ auto call = [&](int64_t start, int64_t n) {
+ if (reference)
+ strata::prefill::gdn_recurrence_variant(5, state, h + start * C, g + start * HV, b + start * HV,
+ z + start * ROW, gm, 1e-6f, scratch, y + start * ld, n, nullptr, ld);
+ else
+ strata::prefill::gdn_recurrence(state, h + start * C, g + start * HV, b + start * HV,
+ z + start * ROW, gm, 1e-6f, scratch, y + start * ld, n, nullptr, ld);
+ ck(cudaDeviceSynchronize(), "recurrence");
+ };
+ if (split) { call(0, split); call(split, in.n - split); }
+ else call(0, in.n);
+ Output out{download(y, std::max<int64_t>(in.n, 1) * ld + 8), download(state, in.state.size())};
+ ck(cudaFree(h), "free"); ck(cudaFree(g), "free"); ck(cudaFree(b), "free"); ck(cudaFree(z), "free");
+ ck(cudaFree(gm), "free"); ck(cudaFree(state), "free"); ck(cudaFree(y), "free"); ck(cudaFree(scratch), "free");
+ return out;
+}
+bool compare(const Input& in, int ld, const Output& ref, const Output& got, bool exact, const char* label) {
+ Error y, state;
+ bool guards = true, bits = true;
+ for (int64_t t = 0; t < in.n; ++t) {
+ for (int d = 0; d < ROW; ++d) {
+ y.add(half(ref.y[t * ld + d]), half(got.y[t * ld + d]));
+ bits &= ref.y[t * ld + d] == got.y[t * ld + d];
+ }
+ for (int d = ROW; d < ld; ++d) guards &= got.y[t * ld + d] == SENTINEL;
+ }
+ for (size_t i = in.n * ld; i < got.y.size(); ++i) guards &= got.y[i] == SENTINEL;
+ for (size_t i = 0; i < got.state.size(); ++i) {
+ state.add(ref.state[i], got.state[i]);
+ bits &= std::memcmp(&ref.state[i], &got.state[i], sizeof(float)) == 0;
+ }
+ const bool ok = guards && (exact ? bits : y.ok() && state.ok());
+ std::printf("%s %s T=%lld ld=%d exact=%d y_rms=%.3e y_max=%.3e state_rms=%.3e guards=%d\n",
+ ok ? "PASS" : "FAIL", label, (long long) in.n, ld, exact, y.rms(), y.max_error, state.rms(), guards);
+ return ok;
+}
+bool cpu_check(const Input& in, int ld, const Output& got) {
+ Error y, state;
+ for (int vh : {0, 23, 47}) {
+ const int qh = vh % HK;
+ std::vector<double> s(S * S), output(S), delta(S);
+ for (int r = 0; r < S; ++r)
+ for (int c = 0; c < S; ++c) s[r * S + c] = in.state[r * ROW + vh * S + c];
+ for (int64_t t = 0; t < in.n; ++t) {
+ const float* row = in.h.data() + t * C;
+ const float* q = row + qh * S;
+ const float* k = row + HK * S + qh * S;
+ const float* v = row + 2 * HK * S + vh * S;
+ const double decay = std::exp(in.gate[t * HV + vh]), beta = in.beta[t * HV + vh];
+ for (auto& x : s) x *= decay;
+ for (int c = 0; c < S; ++c) {
+ double prediction = 0;
+ for (int r = 0; r < S; ++r) prediction += k[r] * s[r * S + c];
+ delta[c] = beta * (v[c] - prediction);
+ }
+ double norm = 0;
+ for (int c = 0; c < S; ++c) {
+ double o = 0;
+ for (int r = 0; r < S; ++r) { s[r * S + c] += k[r] * delta[c]; o += q[r] * s[r * S + c]; }
+ output[c] = o / std::sqrt(double(S)); norm += output[c] * output[c];
+ }
+ for (int c = 0; c < S; ++c) {
+ const double value = output[c] / std::sqrt(norm / S + 1e-6) * in.gamma[c]
+ / (1 + std::exp(-in.z[t * ROW + vh * S + c]));
+ y.add(value, half(got.y[t * ld + vh * S + c]));
+ }
+ }
+ for (int r = 0; r < S; ++r)
+ for (int c = 0; c < S; ++c) state.add(s[r * S + c], got.state[r * ROW + vh * S + c]);
+ }
+ const bool ok = y.ok() && state.ok();
+ std::printf("%s CPU T=%lld y_rms=%.3e state_rms=%.3e\n", ok ? "PASS" : "FAIL", (long long) in.n, y.rms(), state.rms());
+ return ok;
+}
+// One path's time for one layer's recurrence + norm (GPU-event median of 7 runs after 2 warmups, the same start state each
+// run). STRATA_GDN_CHUNKED is read once per process, so --bench (this path) and --bench-default (gfx1151's default: the
+// existing kernels with STRATA_GDN_PP=2 and STRATA_GDN_NOY=1) are separate runs of this binary.
+void bench(const char* path) {
+ for (int64_t t : {64, 256, 2048, 16384, 32768}) {
+ const Input in(t, 2, 1, false);
+ auto h = upload(in.h), g = upload(in.gate), b = upload(in.beta), z = upload(in.z), gm = upload(in.gamma);
+ auto state = upload(in.state);
+ auto y = upload(std::vector<uint16_t>(t * (ROW + 8)));
+ auto scratch = upload(std::vector<float>(t * ROW));
+ cudaEvent_t start, end;
+ ck(cudaEventCreate(&start), "event"); ck(cudaEventCreate(&end), "event");
+ std::vector<float> times;
+ for (int round = -2; round < 7; ++round) {
+ ck(cudaMemcpy(state, in.state.data(), in.state.size() * sizeof(float), cudaMemcpyHostToDevice), "reset");
+ ck(cudaEventRecord(start), "event");
+ strata::prefill::gdn_recurrence(state, h, g, b, z, gm, 1e-6f, scratch, y, t, nullptr, ROW + 8);
+ ck(cudaEventRecord(end), "event"); ck(cudaEventSynchronize(end), "time");
+ float ms = 0;
+ ck(cudaEventElapsedTime(&ms, start, end), "time");
+ if (round >= 0) times.push_back(ms);
+ }
+ std::sort(times.begin(), times.end());
+ std::printf("BENCH path=%s T=%lld ms=%.6f repeats=7\n", path, (long long) t, times[3]);
+ ck(cudaEventDestroy(start), "event"); ck(cudaEventDestroy(end), "event");
+ ck(cudaFree(h), "free"); ck(cudaFree(g), "free"); ck(cudaFree(b), "free"); ck(cudaFree(z), "free");
+ ck(cudaFree(gm), "free"); ck(cudaFree(state), "free"); ck(cudaFree(y), "free"); ck(cudaFree(scratch), "free");
+ }
+}
+}
+int main(int argc, char** argv) {
+ const bool bench_default = argc > 1 && std::strcmp(argv[1], "--bench-default") == 0;
+ const bool off = argc > 1 && (std::strcmp(argv[1], "--off") == 0 || std::strcmp(argv[1], "--default") == 0 || bench_default);
+ setting("STRATA_GDN_CHUNKED", off ? "0" : "1");
+ setting("STRATA_GDN_HEAD", "1"); setting("STRATA_GDN_PP", "2");
+ if (argc > 1 && std::strncmp(argv[1], "--bench", 7) == 0) setting("STRATA_GDN_NOY", "1"); // gfx1151's default
+#ifdef _WIN32
+ _putenv_s("STRATA_GDN_REC_HEADS", "");
+ if (argc > 1 && std::strcmp(argv[1], "--default") == 0) _putenv_s("STRATA_GDN_CHUNKED", "");
+#else
+ unsetenv("STRATA_GDN_REC_HEADS");
+ if (argc > 1 && std::strcmp(argv[1], "--default") == 0) unsetenv("STRATA_GDN_CHUNKED");
+#endif
+ int device = 0;
+ cudaDeviceProp props{};
+ ck(cudaGetDevice(&device), "device"); ck(cudaGetDeviceProperties(&props, device), "properties");
+ const bool wmma = strata::kernels::gfx_arch_is_gfx11_wmma(props.gcnArchName);
+ std::printf("device=%s chunked=%d wmma=%d\n", props.gcnArchName, !off, wmma);
+ if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { bench("chunked"); return 0; }
+ if (bench_default) { bench("default"); return 0; }
+ int failures = 0;
+ for (int64_t t : {0, 1, 15, 16, 17, 63, 64, 65, 79, 80, 81, 257, 2051, 16385}) {
+ if (off && t > 2051) continue;
+ const Input in(t, 2, 1, false);
+ for (int padding : {0, 8, 1}) {
+ if (t > 257 && padding != 8) continue;
+ const int ld = ROW + padding;
+ const auto ref = run(in, ld, true), got = run(in, ld, false);
+ const bool exact = off || !wmma || t < 64 || ld % 8;
+ failures += !compare(in, ld, ref, got, exact, "dispatch");
+ if (t == 65 && padding == 8) failures += !cpu_check(in, ld, got);
+ if (t == 257 && padding == 8) failures += !compare(in, ld, got, run(in, ld, false, 129), false, "continuation");
+ }
+ }
+ if (!off) {
+ for (const float decay : {0.f, 0.01f}) {
+ const Input in(2051, decay, 16, false);
+ failures += !compare(in, ROW + 8, run(in, ROW + 8, true), run(in, ROW + 8, false), !wmma, "weak-decay");
+ }
+ const Input in(65, 0.01f, 1, true);
+ const auto got = run(in, ROW, false);
+ failures += !compare(in, ROW, run(in, ROW, true), got, !wmma, "beta-zero");
+ failures += !cpu_check(in, ROW, got);
+ }
+ std::printf("FAILURES: %d\n", failures);
+ return failures ? 1 : 0;
+}
diff --git a/tests/hip/gdn_rec_head.cpp b/tests/hip/gdn_rec_head.cpp
index 06af26bba..6249440e8 100644
--- a/tests/hip/gdn_rec_head.cpp
+++ b/tests/hip/gdn_rec_head.cpp
@@ -9,6 +9,7 @@
#include <cmath>
#include <cstdint>
#include <cstdio>
+#include <cstdlib>
#include <cstring>
#include <random>
#include <vector>
@@ -32,6 +33,13 @@ template <typename T> std::vector<T> host(const T* d, size_t n) {
} // namespace
int main() {
+ // variants 0 and 1 are the references here; STRATA_GDN_CHUNKED=1 would turn them into the opt-in chunked recurrence
+ // (tests/hip/gdn_chunked.cpp), whose bits differ
+#ifdef _WIN32
+ _putenv_s("STRATA_GDN_CHUNKED", "");
+#else
+ unsetenv("STRATA_GDN_CHUNKED");
+#endif
std::mt19937 rng(11);
std::normal_distribution<float> nd(0.f, 1.f);
std::uniform_real_distribution<float> ud(0.f, 1.f);