Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Qwen GDN prefill: 16-row WY chunk kernel adapted from hipfire (opt-in EXL3_GDN_HF)
YanissAmzcfb9aba5ccf8
diff --git a/exllamav3/vendor/fla/__init__.py b/exllamav3/vendor/fla/__init__.pyindex d71590f..03eaf3d 100644--- a/exllamav3/vendor/fla/__init__.py+++ b/exllamav3/vendor/fla/__init__.py@@ -104,6 +104,15 @@ def chunk_gated_delta_rule_pf(q, k, v, g, beta, scale=None, initial_state=None,with torch.cuda.device(q.device.index):qn = _gdn_pf.l2norm_strided(q)kn = _gdn_pf.l2norm_strided(k)+ # EXL3_GDN_HF=1 (default off, read per call): 16-row-chunk WY kernel adapted from hipfire (hip/gdn_hf.hip) replaces+ # kkt_solve + the chunk kernel, incl. the prefix-cache checkpoint side-write; takes the raw log decay (its own chunk+ # cumsum). Not bitwise (f16 WMMA operands)+ from .hip.gdn_hf_hip import gdn_hf_ok, chunk_gdn_hf+ if gdn_hf_ok(q, v, qn, kn, g, beta, initial_state):+ try:+ return chunk_gdn_hf(q, v, qn, kn, g, beta, scale, initial_state, output_final_state, ckpt=ckpt)+ except Exception as e:+ print(f" -- EXL3_GDN_HF failed, Triton/HIP chain for the rest of the process: {e!r}", flush=True)g = chunk_local_cumsum(g, chunk_size = chunk_size, scale = RCP_LN2)if os.environ.get("EXL3_GDN_FUSE", "0") == "1":from .gdn_fused import chunk_gdn_fuseddiff --git a/exllamav3/vendor/fla/hip/gdn_hf.hip b/exllamav3/vendor/fla/hip/gdn_hf.hipnew file mode 100644index 0000000..08ce95a--- /dev/null+++ b/exllamav3/vendor/fla/hip/gdn_hf.hip@@ -0,0 +1,305 @@+// Adapted from hipfire (https://github.com/warpfront/hipfire, v0.4.2.1-13-gbfb90b9, kernels/src/gated_delta_chunk_wmma.gfx1151.hip),+// Copyright the hipfire authors, licensed under the Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0).+// Changes for exllamav3 (EXL3_GDN_HF): KYO_RAW build = no gated RMSNorm epilogue, the chunk's BF16 o rows are stored as they are+// (our fused_gated_rms_norm runs after, unchanged); z / norm arguments are then unused (may be null). Everything else is the original.+// SPDX-License-Identifier: Apache-2.0+// hipfire — see LICENSE and NOTICE in the project root.++#include <hip/hip_runtime.h>+#include <hip/hip_bfloat16.h>++// Gated DeltaNet prefill recurrence + gated RMSNorm on gfx11 WMMA, in the+// chunked (WY) form of torch_chunk_gated_delta_rule with 16-row chunks. Per+// value head (block) and chunk, with G the chunk-local cumulative log decay:+// L = strictly-lower(-beta_i (k_i . k_j) e^{G_i - G_j}), T = (I - L)^-1+// Vnew = T (v beta - (k beta e^G) S) (S the state entering the chunk)+// O = e^G (q S) + (q k^T e^{G_i - G_j}, j <= i) Vnew+// S = e^{G_last} S + (k e^{G_last - G})^T Vnew+// T is the product (I + L)(I + L^2)(I + L^4)(I + L^8) (L is nilpotent). All+// products are F16 WMMA with F32 accumulation (q/k are BF16 values; S, T,+// Vnew and the decayed operands round to F16), so the route is KLD-gated;+// the gate is gated_delta_gate_bf16_f32_batched's expression.+// Wave w owns value columns 16w .. 16w + 15 of S as eight C-layout tiles in+// registers; a C tile becomes a B operand by swapping half-waves. The next+// chunk's inputs are prefetched into registers during the current one; the+// block barriers wait on LDS only, so the prefetch stays in flight.+// `projection` is the convolution output as packed BF16 bits. The output+// values are BF16-rounded; `output_bf16` stores them as BF16 bits, else F32.+// Grid [value_heads], block 256. key_dim == value_dim == 128.++typedef _Float16 __attribute__((ext_vector_type(16))) gc_h16;+typedef float __attribute__((ext_vector_type(8))) gc_f8;+typedef unsigned int __attribute__((ext_vector_type(8))) gc_u8;+typedef _Float16 __attribute__((ext_vector_type(2))) gc_h2;+// __syncthreads also waits for every outstanding global access (WGP-mode+// workgroup fence), which would drain the prefetch.+#define GC_BARRIER() asm volatile("s_waitcnt lgkmcnt(0)\n\ts_barrier" ::: "memory")+// Wave-private LDS handoff: order this wave's stores before its loads.+#define GC_WAVE_SYNC() do { __builtin_amdgcn_fence(__ATOMIC_RELEASE, "wavefront"); __builtin_amdgcn_wave_barrier(); __builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "wavefront"); } while (0)+#define GC_WMMA(a, b, c) __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c)+static __device__ __forceinline__ float gc_bf16r(float v) { return (float)(hip_bfloat16)v; }+static __device__ __forceinline__ unsigned int gc_pack(float a, float b) {+ return __builtin_bit_cast(unsigned int, gc_h2{(_Float16)a, (_Float16)b});+}+// C-layout tile (lane: column l16, rows 2r + hi) -> the B operand of that+// matrix (lane: column l16, all 16 rows), by swapping half-waves.+static __device__ __forceinline__ gc_h16 gc_c_to_b(const gc_f8 c, int hi) {+ gc_u8 out;+#pragma unroll+ for (int p = 0; p < 4; ++p) {+ const unsigned int own = gc_pack(c[2 * p], c[2 * p + 1]);+ const unsigned int oth = __builtin_amdgcn_permlanex16(own, own, 0x76543210u, 0xfedcba98u, false, false);+ const unsigned int a = hi ? oth : own, b = hi ? own : oth;+ out[2 * p] = __builtin_amdgcn_perm(b, a, 0x05040100u);+ out[2 * p + 1] = __builtin_amdgcn_perm(b, a, 0x07060302u);+ }+ return __builtin_bit_cast(gc_h16, out);+}+__launch_bounds__(256)+extern "C" __global__ void gated_delta_chunk_gate_wmma(+ const unsigned short* __restrict__ projection, const unsigned short* __restrict__ qn, const unsigned short* __restrict__ kn,+ const float* __restrict__ gate, const float* __restrict__ beta, float* __restrict__ state,+ const float* __restrict__ z, const hip_bfloat16* __restrict__ norm, void* __restrict__ output,+ int rows, int qkv_width, int key_heads, int value_heads, float query_scale, int output_bf16+#ifdef KYO_RAW+ , float* __restrict__ ck0, int ck_row0, float* __restrict__ ck1, int ck_row1 // state entering row ck_row* (multiple of 16), or row -1+#endif+#ifdef NPU_TRUNK_ARENA+ , int z_pitch, int z_col0+#endif+ ) {+ const int vh = blockIdx.x;+ const int tid = threadIdx.x, wave = tid >> 5, lane = tid & 31, l16 = lane & 15, hi = lane >> 4;+ const int kh = vh / (value_heads / key_heads);+ const int v_offset = 2 * key_heads * 128 + vh * 128;+ const int value_width = value_heads * 128;+ const size_t state_base = (size_t)vh * 128 * 128;+ __shared__ __align__(16) _Float16 k_s[16][136]; // K rows+ __shared__ __align__(16) _Float16 q_s[16][136]; // scaled Q rows+ __shared__ __align__(16) _Float16 kb_s[16][136]; // k beta e^G rows+ __shared__ __align__(16) _Float16 kdT[128][16]; // (k e^{GL-G})^T+ __shared__ float vb_s[16][132]; // v beta rows (F32)+ __shared__ __align__(16) _Float16 t_s[16][16];+ __shared__ __align__(16) _Float16 p_s[16][16];+ __shared__ float g_s[16], bt_s[16], inv_s[16];+ __shared__ __align__(16) unsigned short out_s[16][136];+ const int dv0 = wave * 16;+ gc_f8 S[8];+#pragma unroll+ for (int t = 0; t < 8; ++t)+#pragma unroll+ for (int r = 0; r < 8; ++r)+ S[t][r] = state[state_base + (size_t)(16 * t + 2 * r + hi) * 128 + dv0 + l16];+ const int si = tid & 15, sd = (tid >> 4) * 8;+ // Raw prefetch registers, widened where used so the loads stay in flight.+ uint4 pk, pq, pv; float pg = 0.f, pb = 0.f;+#ifdef KYO_RAW+ const float nw = 0.0f; (void) nw;+#else+ const float nw = (float)norm[tid & 127];+#endif+ auto fetch = [&](int c0) {+ const int nn = min(16, rows - c0);+ pk = make_uint4(0, 0, 0, 0); pq = pk; pv = pk;+ if (si < nn) {+ const size_t b = ((size_t)(c0 + si) * key_heads + kh) * 128 + sd;+ pk = *(const uint4*)(kn + b);+ pq = *(const uint4*)(qn + b);+ pv = *(const uint4*)(projection + (size_t)(c0 + si) * qkv_width + v_offset + sd);+ }+ if (tid < 16) {+ pg = tid < nn ? gate[(c0 + tid) * value_heads + vh] : 0.f;+ pb = tid < nn ? beta[(c0 + tid) * value_heads + vh] : 0.f;+ }+ };+ // Gated RMSNorm of a finished chunk's rows (out_s, all 128 columns).+ auto epilogue = [&](int c0, const float* zc) {+ const int nn = min(16, rows - c0);+ {+ const int r = 2 * wave + hi;+ const uint4 w = *(const uint4*)&out_s[r][l16 * 8];+ const unsigned int wv[4] = {w.x, w.y, w.z, w.w};+ float sum = 0.f;+#pragma unroll+ for (int h = 0; h < 4; ++h) {+ const float lo = __uint_as_float(wv[h] << 16), hv = __uint_as_float(wv[h] & 0xFFFF0000u);+ sum += lo * lo; sum += hv * hv;+ }+#pragma unroll+ for (int o = 8; o > 0; o >>= 1) sum += __shfl_xor(sum, o, 16);+ if (l16 == 0) inv_s[r] = rsqrtf(sum / 128.0f + 1.0e-6f);+ }+ GC_BARRIER();+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const int e = tid + 256 * i, r = e >> 7, c = e & 127;+ if (r < nn) {+ const size_t at = (size_t)(c0 + r) * value_width + vh * 128 + c;+ const float v = __uint_as_float((unsigned int)out_s[r][c] << 16);+ const float normalized = gc_bf16r(v * inv_s[r] * nw);+ const float g = gc_bf16r(zc[i]);+ const float y = gc_bf16r(normalized / (1.0f + expf(-g)));+ if (output_bf16)+ ((unsigned short*)output)[at] = (unsigned short)(__float_as_uint(y) >> 16);+ else+ ((float*)output)[at] = y;+ }+ }+ };+ fetch(0);+ for (int t0 = 0; t0 < rows; t0 += 16) {+ float zc[8];+ {+ const int nn = min(16, rows - t0);+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const int e = tid + 256 * i, r = e >> 7;+#if defined(KYO_RAW)+ zc[i] = 0.0f; (void) r; (void) e;+#elif defined(NPU_TRUNK_ARENA)+ zc[i] = r < nn ? __uint_as_float((unsigned int)((const unsigned short*)z)[(size_t)(t0 + r) * z_pitch + z_col0 + vh * 128 + (e & 127)] << 16) : 0.0f;+#else+ zc[i] = r < nn ? z[(size_t)(t0 + r) * value_width + vh * 128 + (e & 127)] : 0.0f;+#endif+ }+ }+ const unsigned int kw[4] = {pk.x, pk.y, pk.z, pk.w}, qw[4] = {pq.x, pq.y, pq.z, pq.w};+ float kf[8];+ const float vf[8] = {__uint_as_float(pv.x << 16), __uint_as_float(pv.x & 0xFFFF0000u),+ __uint_as_float(pv.y << 16), __uint_as_float(pv.y & 0xFFFF0000u),+ __uint_as_float(pv.z << 16), __uint_as_float(pv.z & 0xFFFF0000u),+ __uint_as_float(pv.w << 16), __uint_as_float(pv.w & 0xFFFF0000u)};+#pragma unroll+ for (int h = 0; h < 4; ++h) {+ kf[2 * h] = __uint_as_float(kw[h] << 16); kf[2 * h + 1] = __uint_as_float(kw[h] & 0xFFFF0000u);+ *(unsigned int*)&k_s[si][sd + 2 * h] = gc_pack(kf[2 * h], kf[2 * h + 1]);+ *(unsigned int*)&q_s[si][sd + 2 * h] = gc_pack(__uint_as_float(qw[h] << 16) * query_scale,+ __uint_as_float(qw[h] & 0xFFFF0000u) * query_scale);+ }+ if (tid < 16) {+ float G = pg;+#pragma unroll+ for (int o = 1; o < 16; o <<= 1) {+ const float up = __shfl_up(G, o, 16);+ if (tid >= o) G += up;+ }+ g_s[tid] = G;+ bt_s[tid] = pb;+ }+ GC_BARRIER();+ {+ const float b = bt_s[si], eg = expf(g_s[si]), ed = expf(g_s[15] - g_s[si]);+#pragma unroll+ for (int h = 0; h < 8; ++h) {+ vb_s[si][sd + h] = vf[h] * b;+ kdT[sd + h][si] = (_Float16)(kf[h] * ed);+ }+#pragma unroll+ for (int h = 0; h < 4; ++h)+ *(unsigned int*)&kb_s[si][sd + 2 * h] = gc_pack(kf[2 * h] * b * eg, kf[2 * h + 1] * b * eg);+ }+ if (t0 + 16 < rows) fetch(t0 + 16);+ GC_BARRIER();+ if (wave == 0) {+ gc_f8 c = {0, 0, 0, 0, 0, 0, 0, 0};+#pragma unroll+ for (int s = 0; s < 8; ++s)+ c = GC_WMMA(*(const gc_h16*)&k_s[l16][16 * s], *(const gc_h16*)&k_s[l16][16 * s], c);+ // L strictly lower; T = (I - L)^-1 = (I + L)(I + L^2)(I + L^4)(I + L^8).+ gc_f8 T, P;+#pragma unroll+ for (int r = 0; r < 8; ++r) {+ const int i = 2 * r + hi, j = l16;+ P[r] = j < i ? -bt_s[i] * c[r] * expf(g_s[i] - g_s[j]) : 0.f;+ T[r] = P[r] + (i == j ? 1.f : 0.f);+ }+#pragma unroll+ for (int s = 0; s < 3; ++s) {+ // P as A: rows via LDS (t_s scratch), as B: half-wave swap.+#pragma unroll+ for (int r = 0; r < 8; ++r) t_s[2 * r + hi][l16] = (_Float16)P[r];+ GC_WAVE_SYNC();+ const gc_h16 pa = *(const gc_h16*)&t_s[l16][0];+ const gc_h16 pbv = gc_c_to_b(P, hi);+ GC_WAVE_SYNC();+#pragma unroll+ for (int r = 0; r < 8; ++r) t_s[2 * r + hi][l16] = (_Float16)T[r];+ GC_WAVE_SYNC();+ const gc_h16 ta = *(const gc_h16*)&t_s[l16][0];+ gc_f8 P2 = {0, 0, 0, 0, 0, 0, 0, 0};+ P2 = GC_WMMA(pa, pbv, P2);+ T = GC_WMMA(ta, gc_c_to_b(P2, hi), T);+ P = P2;+ GC_WAVE_SYNC();+ }+#pragma unroll+ for (int r = 0; r < 8; ++r) t_s[2 * r + hi][l16] = (_Float16)T[r];+ } else if (wave == 1) {+ gc_f8 c = {0, 0, 0, 0, 0, 0, 0, 0};+#pragma unroll+ for (int s = 0; s < 8; ++s)+ c = GC_WMMA(*(const gc_h16*)&q_s[l16][16 * s], *(const gc_h16*)&k_s[l16][16 * s], c);+#pragma unroll+ for (int r = 0; r < 8; ++r) {+ const int i = 2 * r + hi, j = l16;+ p_s[i][j] = (_Float16)(j <= i ? c[r] * expf(g_s[i] - g_s[j]) : 0.f);+ }+ }+ // K' = (k beta e^G) S and O_inter = Q S on this wave's slice (S as B from registers).+ gc_f8 Ks = {0, 0, 0, 0, 0, 0, 0, 0}, Oi = {0, 0, 0, 0, 0, 0, 0, 0};+#pragma unroll+ for (int t = 0; t < 8; ++t) {+ const gc_h16 sb = gc_c_to_b(S[t], hi);+ Ks = GC_WMMA(*(const gc_h16*)&kb_s[l16][16 * t], sb, Ks);+ Oi = GC_WMMA(*(const gc_h16*)&q_s[l16][16 * t], sb, Oi);+ }+ gc_f8 Dm;+#pragma unroll+ for (int r = 0; r < 8; ++r) Dm[r] = vb_s[2 * r + hi][dv0 + l16] - Ks[r];+ GC_BARRIER(); // T, P ready+ gc_f8 Vn = {0, 0, 0, 0, 0, 0, 0, 0};+ Vn = GC_WMMA(*(const gc_h16*)&t_s[l16][0], gc_c_to_b(Dm, hi), Vn);+ const gc_h16 vb = gc_c_to_b(Vn, hi);+ gc_f8 O = {0, 0, 0, 0, 0, 0, 0, 0};+ O = GC_WMMA(*(const gc_h16*)&p_s[l16][0], vb, O);+#pragma unroll+ for (int r = 0; r < 8; ++r) {+ const int i = 2 * r + hi;+ out_s[i][dv0 + l16] = __builtin_bit_cast(unsigned short, (hip_bfloat16)(O[r] + expf(g_s[i]) * Oi[r]));+ }+ const float eGL = expf(g_s[15]);+#pragma unroll+ for (int t = 0; t < 8; ++t) {+#pragma unroll+ for (int r = 0; r < 8; ++r) S[t][r] *= eGL;+ S[t] = GC_WMMA(*(const gc_h16*)&kdT[16 * t + l16][0], vb, S[t]);+ }+ GC_BARRIER(); // chunk inputs consumed; out_s complete+#ifdef KYO_RAW+ if (t0 + 16 == ck_row0 || t0 + 16 == ck_row1) { // prefix-cache checkpoint: S after this chunk = state entering row t0 + 16+ float* ck = t0 + 16 == ck_row0 ? ck0 : ck1;+#pragma unroll+ for (int t = 0; t < 8; ++t)+#pragma unroll+ for (int r = 0; r < 8; ++r)+ ck[state_base + (size_t)(16 * t + 2 * r + hi) * 128 + dv0 + l16] = S[t][r];+ }+ { // raw o rows (BF16 bits) -> output [rows, value_width]+ const int nn = min(16, rows - t0);+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const int e = tid + 256 * i, r = e >> 7, c = e & 127;+ if (r < nn) ((unsigned short*)output)[(size_t)(t0 + r) * value_width + vh * 128 + c] = out_s[r][c];+ }+ GC_BARRIER(); // out_s is rewritten by the next chunk+ }+#else+ epilogue(t0, zc);+#endif+ }+#pragma unroll+ for (int t = 0; t < 8; ++t)+#pragma unroll+ for (int r = 0; r < 8; ++r)+ state[state_base + (size_t)(16 * t + 2 * r + hi) * 128 + dv0 + l16] = S[t][r];+}diff --git a/exllamav3/vendor/fla/hip/gdn_hf_hip.py b/exllamav3/vendor/fla/hip/gdn_hf_hip.pynew file mode 100644index 0000000..81441a0--- /dev/null+++ b/exllamav3/vendor/fla/hip/gdn_hf_hip.py@@ -0,0 +1,99 @@+# EXL3_GDN_HF (default off): Qwen GDN prefill recurrence with the 16-row-chunk WY kernel adapted from hipfire (gdn_hf.hip, Apache-2.0).+# One kernel per layer replaces kkt_solve + gdn_fused_h_o (the gated norm still runs after). F16 WMMA operands with fp32 accumulation+# (S, T, Vnew rounded to f16 per 16-row chunk): NOT bitwise equal to the Triton / gdn_fused_h chain; quality-gated (KLD).+# Compiled with the ROCm SDK hipcc on first use (cached by source hash), launched on torch's stream like gdn_fused_h_hip.+import ctypes, hashlib, os, subprocess+import torch+from exllamav3.util import hip_compiler+from exllamav3.util.hip_lib import load_hip_runtime++_SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "gdn_hf.hip")+_state = {}+++def _load():+ if "fn" in _state:+ return _state["fn"]+ arch = torch.cuda.get_device_properties(0).gcnArchName.split(":")[0]+ src = open(_SRC, "rb").read()+ flags = ["--genco", f"--offload-arch={arch}", "-O3", "-DKYO_RAW=1"]+ gcc = os.environ.get("EXL3_GCC_INSTALL_DIR", "/usr/lib/gcc/x86_64-linux-gnu/13")+ if os.path.isdir(gcc):+ flags.append(f"--gcc-install-dir={gcc}")+ tag = hashlib.sha1(src + " ".join(flags).encode() + b"\0" + hip_compiler.key().encode()).hexdigest()[:16]+ cache = os.path.join(os.path.expanduser("~/.cache/exllamav3"), f"gdn_hf_{arch}_{tag}.hsaco")+ if not os.path.isfile(cache):+ os.makedirs(os.path.dirname(cache), exist_ok=True)+ tmp = cache + f".{os.getpid()}.tmp"+ subprocess.run([hip_compiler.hipcc(), *flags, "-o", tmp, _SRC], check=True, capture_output=True)+ os.replace(tmp, cache)+ lib = load_hip_runtime()+ torch.cuda.init()+ mod, fn = ctypes.c_void_p(), ctypes.c_void_p()+ if lib.hipModuleLoad(ctypes.byref(mod), cache.encode()) != 0:+ raise RuntimeError("gdn_hf_hip: hipModuleLoad failed")+ if lib.hipModuleGetFunction(ctypes.byref(fn), mod, b"gated_delta_chunk_gate_wmma") != 0:+ raise RuntimeError("gdn_hf_hip: hipModuleGetFunction failed")+ lib.hipModuleLaunchKernel.argtypes = [ctypes.c_void_p] + [ctypes.c_uint] * 7 + [ctypes.c_void_p] * 3+ _state.update(lib=lib, mod=mod, fn=fn)+ return fn+++def gdn_hf_ok(q_raw, v, qn, kn, g, beta, initial_state):+ """q_raw, v: views of the conv output row (q at column 0, v at column 2 H K, same token pitch); qn, kn [1, T, H, 128] bf16+ contiguous (l2-normalized); g [1, T, HV] fp32 raw log decay (natural log, before the chunk cumsum); beta [1, T, HV]."""+ if os.environ.get("EXL3_GDN_HF", "0") != "1" or _state.get("fail"):+ return False+ B, T, H, K = qn.shape+ HV, V = v.shape[2], v.shape[3]+ bf = torch.bfloat16+ ok = (B == 1 and K == 128 and V == 128 and HV % H == 0 and T >= 1+ and qn.dtype == kn.dtype == v.dtype == q_raw.dtype == bf and qn.is_contiguous() and kn.is_contiguous() and kn.shape == qn.shape+ and v.stride(3) == 1 and v.stride(2) == V and q_raw.stride(1) == v.stride(1) and v.stride(1) >= 2 * H * K + HV * V+ and v.data_ptr() == q_raw.data_ptr() + 2 * H * K * 2 and q_raw.stride(3) == 1+ and g.shape == (1, T, HV) and g.dtype == torch.float32 and beta.shape == (1, T, HV)+ and (initial_state is None or (initial_state.dtype == torch.float32 and initial_state.shape == (1, HV, K, V))))+ if not ok and not _state.get("warned"):+ _state["warned"] = True+ print(f" -- EXL3_GDN_HF: shape/layout gate refused (q {tuple(q_raw.shape)} {q_raw.dtype} stride {q_raw.stride()}, v stride {v.stride()}, "+ f"v - q {v.data_ptr() - q_raw.data_ptr()} B, g {tuple(g.shape)} {g.dtype}, beta {tuple(beta.shape)}); Triton/HIP chain used", flush=True)+ return ok+++def chunk_gdn_hf(q_raw, v, qn, kn, g, beta, scale, initial_state, output_final_state, ckpt=None):+ """Returns (o [1, T, HV, 128] bf16, final_state or None). ckpt: None, a dict or a list of <= 2 dicts {"s": fp32 [1, HV, K, V]+ contiguous, "chunk": n} (gdn_fused_h_hip contract): the state after row 64 n is written to ck["s"] and ck["ok"] set."""+ try:+ fn = _load()+ except Exception:+ _state["fail"] = True # no hipcc / no module: the Triton chain for the rest of the process+ raise+ _, T, H, K = qn.shape+ HV = v.shape[2]+ g = g.contiguous()+ beta = beta.float().contiguous()+ state = (torch.zeros(1, HV, K, K, device=v.device, dtype=torch.float32) if initial_state is None+ else initial_state.contiguous().clone()) # the kernel reads the entry state and writes the final one in place+ o = torch.empty(1, T, HV, 128, device=v.device, dtype=torch.bfloat16)+ P = ctypes.c_void_p+ args = [P(q_raw.data_ptr()), P(qn.data_ptr()), P(kn.data_ptr()), P(g.data_ptr()), P(beta.data_ptr()), P(state.data_ptr()),+ P(0), P(0), P(o.data_ptr()), ctypes.c_int(T), ctypes.c_int(v.stride(1)), ctypes.c_int(H), ctypes.c_int(HV),+ ctypes.c_float(scale), ctypes.c_int(1)]+ cks = [] if ckpt is None else (list(ckpt) if isinstance(ckpt, (list, tuple)) else [ckpt])+ cks = [c for c in cks if 0 < 64 * c["chunk"] <= T]+ assert len(cks) <= 2+ for c in cks:+ assert c["s"].dtype == torch.float32 and c["s"].is_contiguous() and c["s"].shape == (1, HV, K, K)+ for i in range(2):+ c = cks[i] if i < len(cks) else None+ args += [P(0 if c is None else c["s"].data_ptr()), ctypes.c_int(-1 if c is None else 64 * c["chunk"])]+ params = (ctypes.c_void_p * len(args))(*[ctypes.cast(ctypes.byref(a), ctypes.c_void_p) for a in args])+ stream = torch.cuda.current_stream().cuda_stream+ r = _state["lib"].hipModuleLaunchKernel(fn, HV, 1, 1, 256, 1, 1, 0, stream, params, None)+ if r != 0:+ _state["fail"] = True+ raise RuntimeError(f"gdn_hf_hip: launch failed ({r})")+ for c in cks:+ c["ok"] = True+ _state["calls"] = _state.get("calls", 0) + 1+ return o, (state if output_final_state else None)