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
RDNA GEMV: enable gfx1151 dense path and add EXL3_DEC_DENSE (#172)
resynth1b3c7e58276b
diff --git a/doc/env_vars.md b/doc/env_vars.mdindex 7f09511..47f8397 100644--- a/doc/env_vars.md+++ b/doc/env_vars.md@@ -1401,6 +1401,15 @@ persistent expert-group kernel.Slice height of the int8 decode GEMV on 4/5-bit weights: taller k-slices, about two work units per SM. `0` restores theformer rule, `EXL3_GEMV_ROWS=<n>` forces a slice height.+#### `EXL3_DEC_DENSE` (default `1`; ROCm)++Dense-only switch for the batch-1 `exl3_dec` GEMV, which fuses both Hadamard stages into one launch on gfx11.5. `0`+keeps the routed-MoE `dec` kernels but moves the K<=6 dense projections (and the fused dense MLA dec projections) to+the shared `exl3_gemv` route, which is the RDNA fdot2 kernel on gfx12 and gfx11.5. K7 and opt-in K8 dense projections+stay on `dec` even with the switch off, because the HIP GEMV gate caps K at 6 and there is no shared-route alternative.+Pack-dependent and not a universal gfx1151 default: a K6 Qwen3.8-Flash-Next pack measured +3.9% decode, a K3 Qwen 27B+pack measured -3.7% plain (MTP unchanged). `EXL3_DEC=0` still disables both the dense and MoE dec kernels.+#### `EXL3_QC_PF_MAXNREG` (default `255` for 8-bit K+V and above, else `0`)Register cap of the quantized-KV prefill attention kernel (`0` = compiler default). Uncapped registers free 1.3 GiB ofdiff --git a/exllamav3/exllamav3_ext/quant/exl3_gemv.cu b/exllamav3/exllamav3_ext/quant/exl3_gemv.cuindex 3516d75..3d96c57 100644--- a/exllamav3/exllamav3_ext/quant/exl3_gemv.cu+++ b/exllamav3/exllamav3_ext/quant/exl3_gemv.cu@@ -1642,8 +1642,9 @@ void exl3_gemvbool ok = false;[[maybe_unused]] bool out_rotated = false;#if defined(USE_ROCM)- // gfx1200/gfx1201: the fdot2 dot core takes the dense multi-row GEMV- // as well as m == 1. The input Hadamard above has already produced A_had.+ // gfx12 (RDNA4) and gfx11.5 (RDNA3.5): the shared fdot2 dot core takes the+ // dense multi-row GEMV as well as m == 1. The input Hadamard above has+ // already produced A_had.// For m == 1 the output Hadamard is fused into the dot kernel when the// width fits the per-device segment counters; rows > 1 store the raw// products and the had_r_128 below rotates the whole C as before.diff --git a/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cu b/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cuindex 7213beb..46add29 100644--- a/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cu+++ b/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cu@@ -177,12 +177,16 @@ bool exl3_gemv_rdna_arch(int device)cache[device] = -1;return false;}- // This path is tuned for gfx1200/gfx1201. Keep the arch test explicit so- // another RDNA family is opted in only after its own measurement and- // tuning.+ // Shared RDNA fdot2 core: the instructions are gfx10.3+/gfx11/gfx12+ // compatible, and the decode path was measured on both families. Keep the+ // arch test explicit so another part is opted in only after its own+ // measurement and tuning.const bool ok =strncmp(prop.gcnArchName, "gfx1200", 7) == 0 ||- strncmp(prop.gcnArchName, "gfx1201", 7) == 0;+ strncmp(prop.gcnArchName, "gfx1201", 7) == 0 ||+ strncmp(prop.gcnArchName, "gfx1150", 7) == 0 ||+ strncmp(prop.gcnArchName, "gfx1151", 7) == 0 ||+ strncmp(prop.gcnArchName, "gfx1152", 7) == 0;cache[device] = ok ? 1 : -1;return ok;}diff --git a/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cuh b/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cuhindex 7accc88..e338ac6 100644--- a/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cuh+++ b/exllamav3/exllamav3_ext/rocm/quant/exl3_gemv_rdna.cuh@@ -1,6 +1,7 @@#pragma once-// RDNA (gfx1200/gfx1201) fdot2 GEMV for the dense single-matrix forms:+// RDNA fdot2 GEMV for the dense single-matrix forms, shared by RDNA4+// (gfx1200/gfx1201) and RDNA3.5 (gfx1150/1151/1152):// M == 1, and M == 2..8 split-K.//// The generic GEMV kernel (quant/exl3_gemv_kernel.cuh) is built on m16n8k16@@ -18,7 +19,8 @@#include <cuda_fp16.h>#include <hip/hip_runtime.h>-// True on gfx1200/gfx1201 (the parts whose decode was measured with this core)+// True on gfx1200/gfx1201 (RDNA4) and gfx1150/1151/1152 (RDNA3.5), the parts+// whose decode was measured with this shared core.bool exl3_gemv_rdna_arch(int device);// Launch the dot kernel for M == 1..8. A is the already-rotated inputdiff --git a/exllamav3/modules/attention_fn/qdec_hip.py b/exllamav3/modules/attention_fn/qdec_hip.pyindex 58cfbd5..a2eea03 100644--- a/exllamav3/modules/attention_fn/qdec_hip.py+++ b/exllamav3/modules/attention_fn/qdec_hip.py@@ -19,10 +19,14 @@ _SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "qdec.hip")_CACHE = os.path.expanduser(os.environ.get("EXL3_QDEC_CACHE", "~/.cache/exllamav3"))+_family_cache: dict[int, bool] = {}++def _gfx115_family(device=None) -> bool:"""qdec.hip uses gfx11.5-only (RDNA3.5) wave32 WMMA; enable it only on that family.- The check is per call, not per process: on a mixed gfx1201+gfx1151 box the model- device may be index 1 while device 0 is the other part. An extension without the+ The check is per device, not per process: on a mixed gfx1201+gfx1151 box the model+ device may be index 1 while device 0 is the other part. The result is cached per+ device index because make_cfg runs per layer per chunk. An extension without thefamily probe keeps the historical behavior."""if not torch.version.hip:return False@@ -39,7 +43,10 @@ def _gfx115_family(device=None) -> bool:idx = device.index if device.index is not None else torch.cuda.current_device()else:idx = int(device)- return ext.exl3_gemv_wmma_family(idx) == 2+ cached = _family_cache.get(idx)+ if cached is None:+ cached = _family_cache[idx] = (ext.exl3_gemv_wmma_family(idx) == 2)+ return cachedexcept Exception: # noqa: BLE001return Falsediff --git a/exllamav3/modules/hyperconnections.py b/exllamav3/modules/hyperconnections.pyindex ae55109..0ed41f6 100644--- a/exllamav3/modules/hyperconnections.py+++ b/exllamav3/modules/hyperconnections.py@@ -500,32 +500,40 @@ def _gr_hip():return _gr_hip_mod[0]+_wmma_family_cache: dict[int, int] = {}++def _wmma_family(device=None) -> int:"""WMMA family of the module device: 0 none, 1 gfx12 (RDNA4), 2 gfx11.5 (RDNA3.5).gr_mix.hip is a gfx11.5-only hand kernel (different builtin and fragment layout fromgfx12) and its JIT compiles for the module's device, so it must not be selected on agfx1201 part. Mirrors exl3_gemv_wmma_family, with an arch-name fallback for builds that- predate the probe."""+ predate the probe. Cached per device index: the probe runs through pybind + a mutex and+ this predicate is evaluated per forward."""if device is None:idx = torch.cuda.current_device()elif isinstance(device, torch.device):idx = device.index if device.index is not None else torch.cuda.current_device()else:idx = int(device)+ cached = _wmma_family_cache.get(idx)+ if cached is not None:+ return cached+ family = 0try:- return ext.exl3_gemv_wmma_family(idx)+ family = int(ext.exl3_gemv_wmma_family(idx))except Exception: # noqa: BLE001- pass- try:- arch = torch.cuda.get_device_properties(idx).gcnArchName.split(":")[0]- if arch in ("gfx1200", "gfx1201"):- return 1- if arch in ("gfx1150", "gfx1151", "gfx1152"):- return 2- except Exception: # noqa: BLE001- pass- return 0+ try:+ arch = torch.cuda.get_device_properties(idx).gcnArchName.split(":")[0]+ if arch in ("gfx1200", "gfx1201"):+ family = 1+ elif arch in ("gfx1150", "gfx1151", "gfx1152"):+ family = 2+ except Exception: # noqa: BLE001+ pass+ _wmma_family_cache[idx] = family+ return familyclass GatedResidual(Module):diff --git a/exllamav3/modules/mla_attn.py b/exllamav3/modules/mla_attn.pyindex 38e3ae5..891c89f 100644--- a/exllamav3/modules/mla_attn.py+++ b/exllamav3/modules/mla_attn.py@@ -72,7 +72,11 @@ def _pf_notile_ok(rows, t):# when its dec_ok holds, but that gate is mul1-only, so on a GLM-5.3-Flash pack (all mcg) every# projection here falls back to the generic path; this wiring decodes either codebook and folds# q_a_proj + kv_a_proj_with_mqa into one launch. EXL3_MLA_DEC=0 restores the plain forwards.-EXL3_MLA_DEC = os.environ.get("EXL3_MLA_DEC", "1") != "0"+# Cross-gated with the dense dec switch (quant/exl3.py: EXL3_DEC_DENSE) so EXL3_DEC_DENSE=0+# also moves the fused dense MLA dec projections to the shared exl3_gemv route.+EXL3_MLA_DEC = (os.environ.get("EXL3_MLA_DEC", "1") != "0"+ and os.environ.get("EXL3_DEC", "1") != "0"+ and os.environ.get("EXL3_DEC_DENSE", "1") != "0")# Verify batch-invariance (glm-mtp step 7): at 2..EXL3_MLA_DEC_ROWLOOP_MAX rows of an MTP verify# forward, run the MLA projections as one exl3_dec GEMV launch PER ROW (the exact batch-1 kernel)@@ -113,9 +117,13 @@ def dec_proj_ok(lin) -> bool:inner = getattr(lin, "inner", None)if inner is None or not hasattr(inner, "K"): # LinearFP16 (test models) has no EXL3 trellisreturn False+ from .quant.exl3 import _hip_wmma_familyk2 = inner.K * 2return (EXL3_MLA_DEC and bool(torch.version.hip) and hasattr(ext, "exl3_dec_gemv") and+ # exl3_dec is a gfx11.5 (RDNA3.5) WMMA kernel; keep this gate consistent with+ # LinearEXL3.dec_supported so gfx12 uses the RDNA fdot2 GEMV instead.+ _hip_wmma_family(inner.trellis.device) == 2 andgetattr(lin, "quant_type", None) == "exl3" and (inner.mul1 or inner.mcg) andint(round(k2)) in _DEC_KB2 and k2 == int(round(k2)) andinner.in_features % 512 == 0 and inner.out_features % 128 == 0 anddiff --git a/exllamav3/modules/quant/exl3.py b/exllamav3/modules/quant/exl3.pyindex 061a892..facbd7d 100644--- a/exllamav3/modules/quant/exl3.py+++ b/exllamav3/modules/quant/exl3.py@@ -235,6 +235,13 @@ _hip_gemv_family_cache: dict[int, int] = {}# Batch-1 decode kernels (exl3_dec.cu, gfx11.5): one launch per GEMV with both Hadamard stages# fused. EXL3_DEC=0 disables them (falls back to exl3_gemv / reconstruct).EXL3_DEC = os.environ.get("EXL3_DEC", "1") != "0"+# Dense-only switch (see doc/rdna-gemv-shared-2026-11-05.md). EXL3_DEC_DENSE=0 keeps the routed+# MoE dec kernels but moves the dense projections to the shared exl3_gemv route: the RDNA fdot2+# kernel, also on gfx1150/1151/1152. Pack-dependent (helps the K6 Flash-Next projections, costs+# ~3.7% plain on the K3 27B), so it stays opt-in and is not a gfx1151 default. K7 and K8 dense+# projections stay on dec even with the switch off: the HIP GEMV gate caps K at 6, so there is+# no shared-route alternative for them (dec_supported applies EXL3_DEC separately).+EXL3_DEC_DENSE = os.environ.get("EXL3_DEC_DENSE", "1") != "0"_DEC_KB2 = (4, 5, 6, 8, 10, 12)# dense projections also decode K7 (attention, linear-attention and shared-expert projections of 5 bpw packs); the routed-MoE kernels stop at K6_DEC_KB2_DENSE = _DEC_KB2 + (14,)@@ -300,7 +307,8 @@ def dec_supported(inner) -> bool:The exl3_dec kernels are gfx11.5 (RDNA3.5) WMMA only, so the gate also pins the device tofamily 2 -- on gfx12 (RDNA4) the direct WMMA GEMV path must be used instead."""return (- EXL3_DEC and bool(torch.version.hip) and hasattr(ext, "exl3_dec_gemv") and+ EXL3_DEC and (EXL3_DEC_DENSE or int(round(inner.K * 2)) > 12) and+ bool(torch.version.hip) and hasattr(ext, "exl3_dec_gemv") and_hip_wmma_family(inner.trellis.device) == 2 andinner.mul1 and not inner.mcg and (int(round(inner.K * 2)) in _DEC_KB2_DENSE ordiff --git a/tests/test_dense_k8_cpu.py b/tests/test_dense_k8_cpu.pyindex 73cdf51..4835991 100644--- a/tests/test_dense_k8_cpu.py+++ b/tests/test_dense_k8_cpu.py@@ -60,7 +60,7 @@ int main() {return !(run<10>() && run<12>() && run<14>() && run<16>());}subprocess.run([str(exe)],check=True,env={**os.environ,'ASAN_OPTIONS':'detect_leaks=0'})-def helpers(family=2):+def helpers(family=2, dense=True):tree = ast.parse((ROOT / 'exllamav3/modules/quant/exl3.py').read_text())nodes = [n for n in tree.body if(isinstance(n, ast.FunctionDef) and n.name in ('dec_supported','dec_moe_supported','_hip_wmma_family')) or@@ -70,6 +70,7 @@ def helpers(family=2):ns = {'os':os,'torch':NS(version=NS(hip=True), device=object, cuda=NS(current_device=lambda:0)),'EXL3_DEC':True,+ 'EXL3_DEC_DENSE':dense,'ext':NS(exl3_dec_gemv=True, exl3_dec_moe=True,exl3_gemv_wmma_family=lambda index: family)}exec(compile(ast.Module(body=nodes,type_ignores=[]),'eligibility','exec'),ns)@@ -118,5 +119,15 @@ class DenseK8Checks(unittest.TestCase):self.assertEqual(ns['dec_supported'](inner()), enabled, family)self.assertEqual(ns['dec_moe_supported'](inner(k=2)), enabled, family)+ def test_dec_dense_switch_is_dense_only(self):+ """EXL3_DEC_DENSE=0 moves K<=6 dense GEMV off exl3_dec but keeps dec_moe and K7/K8."""+ with patch.dict(os.environ, {'EXL3_DENSE_K8':'1'}):+ ns = helpers(2, dense=False)+ self.assertFalse(ns['dec_supported'](inner(k=3)))+ # K7 and the opt-in K8 shapes have no HIP GEMV alternative, so they stay on dec.+ self.assertTrue(ns['dec_supported'](inner(k=7)))+ self.assertTrue(ns['dec_supported'](inner()))+ self.assertTrue(ns['dec_moe_supported'](inner(k=2)))+if __name__ == '__main__':unittest.main()