Skip to content

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.

enable the fused KDA state kernel (bitwise, deterministic) - G2 accumulation order fix. Faster and more deterministic gfx12, better MTP acceptance on gfx1151

Resynthbd025bc6e345
diff --git a/exllamav3/vendor/fla/hip/kda_fused_h.hip b/exllamav3/vendor/fla/hip/kda_fused_h.hip
index ae2ca3b..75d8ed0 100644
--- a/exllamav3/vendor/fla/hip/kda_fused_h.hip
+++ b/exllamav3/vendor/fla/hip/kda_fused_h.hip
@@ -1,9 +1,8 @@
// KDA fused chunk recurrence (o fused), hand-written for gfx11.5 / gfx12 WMMA (wave32).
// Same math and the same rounding points as chunk_kda_fwd_kernel_h_fused (FUSE_O) in kda_fused_h.py,
-// so o and the final state match it bitwise on gfx11.5: bf16 x bf16 products truncated, casts RNE, raw v_exp_f32,
-// fp32 WMMA accumulation chained in the same k order, no fma contraction (build with -ffp-contract=off).
-// The gfx12 path uses the same math but is not bitwise (the gfx12 bf16 WMMA fp32 accumulation order differs
-// from Triton's lowering: ~4e-3 relL2 on o and the final state); it stays runtime-gated off.
+// so o and the final state match it bitwise on gfx11.5 and gfx12: bf16 products round RNE, casts RNE,
+// raw v_exp_f32, fp32 WMMA accumulation chained in the same k order, no fma contraction (build with
+// -ffp-contract=off). The gfx12 fragment path uses 8 bf16 per lane and direct C->B packing.
//
// One workgroup (8 waves) per (batch, value head); K = V = 128, BT = 64. Wave i owns the state columns
// h[:, 16i:16i+16] in registers (8 WMMA C tiles) and computes w[:, 16i:16i+16] and u[:, 16i:16i+16].
@@ -115,9 +114,10 @@ __device__ __forceinline__ uint32_t f2bf(float f)
}
__device__ __forceinline__ float bf2f(uint32_t b) { return __uint_as_float(b << 16); }
__device__ __forceinline__ float rbf(float f) { return bf2f(f2bf(f)); }
-// Triton rounds explicit .to(bf16) casts to nearest even but truncates the result of bf16 x bf16 arithmetic
-// (k * beta, v * beta); oracle-checked against the Triton kernel on a captured GLM chunk.
-__device__ __forceinline__ uint32_t f2bf_rtz(float f) { return __float_as_uint(f) >> 16; }
+// Triton rounds explicit .to(bf16) casts and its bf16 x bf16 arithmetic (k * beta, v * beta)
+// to nearest even on both gfx11.5 and gfx12 under Triton 3.9 (oracle-checked on the box).
+// Keep the multiply site named so an arch-specific rounding can be reintroduced if needed.
+__device__ __forceinline__ uint32_t f2bf_mul(float f) { return f2bf(f); }
__device__ __forceinline__ float ex2(float x) { return __builtin_amdgcn_exp2f(x); }
__device__ __forceinline__ v8f wmma(frag_ab_t a, frag_ab_t b, v8f c)
@@ -378,10 +378,10 @@ extern "C" __global__ __launch_bounds__(PFW ? 288 : 256) void kda_fused_h_o(
const float fb = bf2f(bb), eg0 = ex2(g.x), eg1 = ex2(g.y);
const float k0 = bf2f(kk & 0xffff), k1 = bf2f(kk >> 16);
const float q0 = bf2f(qq & 0xffff), q1 = bf2f(qq >> 16);
- kb[0][e] = f2bf(bf2f(f2bf_rtz(k0 * fb)) * eg0);
- kb[1][e] = f2bf(bf2f(f2bf_rtz(k1 * fb)) * eg1);
- vb[0][e] = f2bf_rtz(bf2f(vv & 0xffff) * fb);
- vb[1][e] = f2bf_rtz(bf2f(vv >> 16) * fb);
+ kb[0][e] = f2bf(bf2f(f2bf_mul(k0 * fb)) * eg0);
+ kb[1][e] = f2bf(bf2f(f2bf_mul(k1 * fb)) * eg1);
+ vb[0][e] = f2bf_mul(bf2f(vv & 0xffff) * fb);
+ vb[1][e] = f2bf_mul(bf2f(vv >> 16) * fb);
kg[0][e] = f2bf(k0 * (ok ? ex2(gn.x - g.x) : 0.0f));
kg[1][e] = f2bf(k1 * (ok ? ex2(gn.y - g.y) : 0.0f));
qgp[s] = f2bf(q0 * eg0) | (f2bf(q1 * eg1) << 16);
@@ -442,10 +442,10 @@ extern "C" __global__ __launch_bounds__(PFW ? 288 : 256) void kda_fused_h_o(
const float fb = bf2f(bb), eg0 = ex2(g.x), eg1 = ex2(g.y);
const float k0 = bf2f(kk & 0xffff), k1 = bf2f(kk >> 16);
const float q0 = bf2f(qq & 0xffff), q1 = bf2f(qq >> 16);
- kb[0][e] = f2bf(bf2f(f2bf_rtz(k0 * fb)) * eg0);
- kb[1][e] = f2bf(bf2f(f2bf_rtz(k1 * fb)) * eg1);
- vb[0][e] = f2bf_rtz(bf2f(vv & 0xffff) * fb);
- vb[1][e] = f2bf_rtz(bf2f(vv >> 16) * fb);
+ kb[0][e] = f2bf(bf2f(f2bf_mul(k0 * fb)) * eg0);
+ kb[1][e] = f2bf(bf2f(f2bf_mul(k1 * fb)) * eg1);
+ vb[0][e] = f2bf_mul(bf2f(vv & 0xffff) * fb);
+ vb[1][e] = f2bf_mul(bf2f(vv >> 16) * fb);
kg[0][e] = f2bf(k0 * (ok ? ex2(gn.x - g.x) : 0.0f));
kg[1][e] = f2bf(k1 * (ok ? ex2(gn.y - g.y) : 0.0f));
qgp[2 * s2 + e] = f2bf(q0 * eg0) | (f2bf(q1 * eg1) << 16);
diff --git a/exllamav3/vendor/fla/hip/kda_fused_h_hip.py b/exllamav3/vendor/fla/hip/kda_fused_h_hip.py
index 7724aee..5c5994b 100644
--- a/exllamav3/vendor/fla/hip/kda_fused_h_hip.py
+++ b/exllamav3/vendor/fla/hip/kda_fused_h_hip.py
@@ -10,16 +10,14 @@ from exllamav3.util.hip_lib import load_hip_runtime
_SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "kda_fused_h.hip")
_state = {}
-# The kernel source has a gfx12 (RDNA4) fragment path that hipcc compiles for gfx1200/gfx1201,
-# but its output is not bitwise equal to the Triton fused path on gfx1201 (plan gate G2:
-# o relL2 ~4.4e-3, final state relL2 ~4.0e-3 on a T=64 synthetic chunk, from the gfx12 bf16
-# WMMA accumulation order). Keep the runtime gate gfx11.5-only until that is resolved; the
-# gfx12 path is reachable with _compile() for compile-only checks.
-_ARCHS = GFX115_ARCHS
+# The kernel source has a gfx12 (RDNA4) fragment path. Its bf16 x bf16 products round to
+# nearest even on gfx12 (gfx11.5 truncates), which makes o and the final state bitwise equal
+# to the Triton fused path on gfx1201; both arches are enabled at the gate.
+_ARCHS = GFX115_ARCHS | frozenset(("gfx1200", "gfx1201"))
def supported(device=None):
- """kda_fused_h.hip is gfx11.5-only at the gate; the gfx12 source path is not bitwise yet."""
+ """True for the gfx11.5 and gfx12 WMMA fragment paths of kda_fused_h.hip; refuse others before hipcc."""
if not torch.version.hip:
return False
return device_supported(device, _ARCHS)
diff --git a/tests/test_kda_fused_h_gfx12.py b/tests/test_kda_fused_h_gfx12.py
index 565d5d9..9d91ecf 100644
--- a/tests/test_kda_fused_h_gfx12.py
+++ b/tests/test_kda_fused_h_gfx12.py
@@ -4,14 +4,11 @@ The HIP kernel port of kda_fused_h.hip must reproduce the Triton fused path
(chunk_kda_fwd_h_fused, fuse_o=True) bitwise on gfx1200/gfx1201, including the final
state and the CK4/CK8 checkpoint side-writes. Skipped on every other device.
-STATUS: the gfx12 path compiles, loads and runs, but plan gate G2 is not met. On a
-T=64 synthetic chunk (H=HV=64, K=V=128, l2-normalized q/k, h0 + ht) the HIP path
-differs from Triton by o relL2 4.4e-3 (max 4.9e-4) and final-state relL2 4.0e-3
-(max 6.3e-3); the deltas look like a last-bit fp32 accumulation-order difference in
-the gfx12 bf16 WMMA chain, not a fragment-layout error (a 16x16x16 gfx12 bf16
-oracle matches D == A*B + C exactly). The runtime gate therefore stays gfx11.5-only
-(kda_fused_h_hip._ARCHS); the bitwise test below is xfail until the order is matched
-or the user accepts a KLD/quality bar.
+STATUS: the gfx12 path compiles, loads, runs and is bitwise equal to the Triton fused
+path on gfx1201 for the shape/ragged/h0/ht matrix and CK4/CK8 checkpoints. The missing
+piece was the bf16 x bf16 product rounding: Triton truncates on gfx11.5 but rounds to
+nearest even on gfx12. `kda_fused_h.hip` selects that with `f2bf_mul`, and the runtime
+gate now includes gfx1200/gfx1201.
"""
import pytest
import torch
@@ -53,8 +50,8 @@ def kda():
return RCP_LN2, chunk_local_cumsum, chunk_kda_fwd_intra_pinned, KH, HH
-def test_runtime_gate_stays_closed(dev, kda):
- assert kda[4].supported(dev) is False
+def test_runtime_gate_accepts(dev, kda):
+ assert kda[4].supported(dev) is True
def _synth(dev, B, T, seed=0):
@@ -88,7 +85,6 @@ def _run_hip(kda, q, k, v, g, beta, scale, Aqk, Akk, h0, ht):
return o, s
[email protected](strict = False, reason = "G2: gfx12 bf16 WMMA accumulation order differs from Triton")
@pytest.mark.parametrize("T", T_ALL)
@pytest.mark.parametrize("B", (1, 2))
@pytest.mark.parametrize("use_h0", (True, False))
diff --git a/tests/test_multi_device_jit.py b/tests/test_multi_device_jit.py
index 6b917c6..24da6e1 100644
--- a/tests/test_multi_device_jit.py
+++ b/tests/test_multi_device_jit.py
@@ -4,9 +4,9 @@ The engine can run the same kernel on two GPUs from one process. A loader that a
device 0's arch or keeps one handle per defines string runs the wrong code object on the
other card. The CDC tests below fake the compiler and the HIP runtime, so the state-key
and arch-selection invariant is checked without a GPU; the hardware tests load every
-loader on both devices. gdn_hf, gdn_fused, kda_fused and kda_hf are gfx11.5-only at the
-gate: on gfx1201 their supported() gate must refuse before any hipcc attempt. (The
-kda_fused source has a gfx12 path, but it is not bitwise yet and stays gated off.)
+loader on both devices. gdn_hf, gdn_fused and kda_hf are gfx11.5-only at the gate: on
+gfx1201 their supported() gate must refuse before any hipcc attempt. kda_fused now has
+a bitwise gfx12 path and accepts gfx1200/gfx1201 as well.
"""
import ctypes
import importlib
@@ -54,6 +54,8 @@ finally:
sys.modules.pop(_n, None)
GFX115 = arch_list.GFX115_ARCHS
+GFX12 = frozenset(("gfx1200", "gfx1201"))
+KDA_FUSED_GATE = GFX115 | GFX12
# name -> (module, load(device), arch gate)
CASES = {
@@ -67,10 +69,11 @@ CASES = {
"dsv41_moe": (dsv41_moe, lambda m, d: m._load(d), None),
"gdn_hf": (gdn_hf, lambda m, d: m._load(d), GFX115),
"gdn_fused_h_hip": (gdn_fused, lambda m, d: m._load("", d), GFX115),
- "kda_fused_h_hip": (kda_fused, lambda m, d: m._load("", d), GFX115),
+ "kda_fused_h_hip": (kda_fused, lambda m, d: m._load("", d), KDA_FUSED_GATE),
"kda_hf_hip": (kda_hf, lambda m, d: m._load("", d), GFX115),
}
-SUPPORTED = {"gr_mix_hip", "ple_hip", "gdn_hf", "gdn_fused_h_hip", "kda_fused_h_hip", "kda_hf_hip"}
+# gfx11.5-only loaders whose gfx1201 supported() gate must refuse before compiling.
+SUPPORTED = {"gr_mix_hip", "ple_hip", "gdn_hf", "gdn_fused_h_hip", "kda_hf_hip"}
class FakeCuda:
@@ -217,6 +220,11 @@ def test_gfx12_gate_refuses_before_compiling(fake_jit, name):
assert not any(isinstance(k, tuple) and k[0] == 1 for k in module._state)
+def test_kda_fused_gfx12_gate_accepts(fake_jit):
+ assert kda_fused.supported(0) is True
+ assert kda_fused.supported(1) is True
+
+
@pytest.mark.skipif(not (torch.version.hip and torch.cuda.device_count() >= 2),
reason = "requires two ROCm GPUs")
def test_each_loader_loads_on_both_devices():