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.

qdec: single-buffer the cross-wave S exchange for 3+ q tiles (EXL3_QDEC_HIP_SMM)

YanissAmz8c9fa28d69f0
diff --git a/exllamav3/modules/attention_fn/qdec.hip b/exllamav3/modules/attention_fn/qdec.hip
index 75b2d31..dad92e4 100644
--- a/exllamav3/modules/attention_fn/qdec.hip
+++ b/exllamav3/modules/attention_fn/qdec.hip
@@ -58,6 +58,12 @@ typedef u32 v8u __attribute__((ext_vector_type(8)));
#ifndef VPF
#define VPF 1 // this slab's V runs are issued before its S stage, so their DRAM latency overlaps
#endif // the S dequant/WMMA and the softmax (bit-identical, scheduling only; 0 = pre-lever order)
+#ifndef SMM1
+#define SMM1 0 // DW > 1: single-buffer the cross-wave S exchange (half the LDS, one extra barrier per slab)
+#endif
+#ifndef SMMB
+#define SMMB 0 // DW > 1: keep the double buffer but re-align the waves on every slab (extra barrier only)
+#endif
__device__ __forceinline__ u32 hsub2(u32 a, u32 b) { return __builtin_bit_cast(u32, __builtin_bit_cast(v2h, a) - __builtin_bit_cast(v2h, b)); }
__device__ __forceinline__ u32 hmul2(u32 a, u32 b) { return __builtin_bit_cast(u32, __builtin_bit_cast(v2h, a) * __builtin_bit_cast(v2h, b)); }
@@ -267,7 +273,7 @@ qdec_split(
const float sl = SCALE * LOG2E;
const float qmin = (float)(total_k - QLEN);
- float* Ssm = (float*) scratch; // DW > 1: [2][NSW*DW][NT][8][32] partial S, double buffered
+ float* Ssm = (float*) scratch; // DW > 1: [NSW*DW][NT][8][32] partial S (double buffered unless SMM1)
u32 kw[KWW]; // this lane's token row of K (kvh slice of this wave's groups)
u16 ksc[GWV]; // its scales
int row0 = 0, lim = 0;
@@ -389,7 +395,7 @@ qdec_split(
}
if (DW > 1) {
// sum the partial S of the DW waves of this stream in a fixed order (every wave gets the same bits)
- float* sb = Ssm + (size_t)(it & 1) * NSW * DW * NT * 8 * 32;
+ float* sb = Ssm + (SMM1 ? 0 : (size_t)(it & 1) * NSW * DW * NT * 8 * 32);
if (active) {
#pragma unroll
for (int j = 0; j < NT; j++)
@@ -409,6 +415,7 @@ qdec_split(
}
}
}
+ if ((SMM1 || SMMB) && DW > 1) __syncthreads(); // SMM1: the single buffer is overwritten next iteration; SMMB: phase-align only
if (!active) continue;
#if TQF
const bool more = it + 1 < iters && (sw + (it + 1) * NSW) < nslabs;
diff --git a/exllamav3/modules/attention_fn/qdec_hip.py b/exllamav3/modules/attention_fn/qdec_hip.py
index c1dfae3..3d881cd 100644
--- a/exllamav3/modules/attention_fn/qdec_hip.py
+++ b/exllamav3/modules/attention_fn/qdec_hip.py
@@ -51,7 +51,7 @@ def pick_dw(hd, nt, nw):
return None
-def geometry(hd, nqh, nkv, qlen, nw, dw):
+def geometry(hd, nqh, nkv, qlen, nw, dw, smm = 0, sxmul = 0):
"""(NT, RP, shared bytes): q tile + max(32x32 fp32 q-rotation matrix, S exchange, cross-stream merge buffer), mirrors qdec.hip"""
rows = qlen * (nqh // nkv)
nt = (rows + 15) // 16
@@ -60,7 +60,7 @@ def geometry(hd, nqh, nkv, qlen, nw, dw):
nsw = nw // dw
no = nt * (hd // 16 // dw) * 8
merge = ((nsw - 1) * dw * no * 32 + 2 * (nsw - 1) * nt * 32) * 4
- sx = 2 * nw * nt * 8 * 32 * 4 if dw > 1 else 0
+ sx = (sxmul or (1 if smm else 2)) * nw * nt * 8 * 32 * 4 if dw > 1 else 0
return nt, rp, qs + max(4096, merge, sx)
@@ -77,19 +77,22 @@ def make_cfg(q_len, n_q_heads, n_kv_heads, head_dim, k_bits, v_bits, page_size,
dw = pick_dw(head_dim, nt, nw)
if dw is None:
return None
- _, _, shared = geometry(head_dim, n_q_heads, n_kv_heads, q_len, nw, dw)
+ smmv = int(knob_int("EXL3_QDEC_HIP_SMM", 0))
+ smm = int(smmv != 0 and dw > 1 and nt >= 3) # measured: nt3 -39 %, nt2 +1 %, nt1 flat
+ sxmul = 2 if smmv == 2 else 0 # SMM=2: same kernel as SMM=1 but the old (double) allocation, a footprint probe
+ _, _, shared = geometry(head_dim, n_q_heads, n_kv_heads, q_len, nw, dw, smm, sxmul)
if shared > 64 * 1024:
return None
return dict(hd=head_dim, kb=k_bits, vb=v_bits, nkv=n_kv_heads, nqh=n_q_heads, qlen=q_len, page=page_size, nw=nw, dw=dw,
causal=bool(causal), scale=float(sm_scale), sinks=bool(has_sinks), key_lo=int(key_lo), tq=bool(tq),
- vpf=bool(knob_int("EXL3_QDEC_HIP_VPF", 1)))
+ vpf=bool(knob_int("EXL3_QDEC_HIP_VPF", 1)), smm=smm, sxmul=sxmul)
def _compile(cfg, arch):
from exllamav3.util import hip_compiler
defs = dict(HD=cfg["hd"], KB=cfg["kb"], VB=cfg["vb"], NKV=cfg["nkv"], NQH=cfg["nqh"], QLEN=cfg["qlen"], PAGE=cfg["page"],
NW=cfg["nw"], DW=cfg["dw"], CAUSAL=int(cfg["causal"]), HAS_SINKS=int(cfg["sinks"]), KEY_LO=cfg["key_lo"], TQ=int(cfg.get("tq", False)),
- VPF=int(cfg.get("vpf", False)), SCALE=repr(float(cfg["scale"])) + "f")
+ VPF=int(cfg.get("vpf", False)), SMM1=int(cfg.get("smm", 0)), SCALE=repr(float(cfg["scale"])) + "f")
extra = [f"-D{k}={v}" for k, v in defs.items()] + knob("EXL3_QDEC_HIP_FLAGS", "").split()
flags = ["--genco", f"--offload-arch={arch}", "-O3", "-ffp-contract=off", "--no-gpu-bundle-output"] + extra
gcc = os.environ.get("EXL3_GCC_INSTALL_DIR", "/usr/lib/gcc/x86_64-linux-gnu/13")
@@ -115,7 +118,8 @@ class _Kernel:
arch = torch.cuda.get_device_properties(device).gcnArchName.split(":")[0]
self.cfg = cfg
self.path = _compile(cfg, arch)
- _, self.rp, self.shared = geometry(cfg["hd"], cfg["nqh"], cfg["nkv"], cfg["qlen"], cfg["nw"], cfg["dw"])
+ _, self.rp, self.shared = geometry(cfg["hd"], cfg["nqh"], cfg["nkv"], cfg["qlen"], cfg["nw"], cfg["dw"],
+ cfg.get("smm", 0), cfg.get("sxmul", 0))
self.mod = ctypes.c_void_p()
if lib.hipModuleLoad(ctypes.byref(self.mod), self.path.encode()) != 0:
raise RuntimeError("qdec_hip: hipModuleLoad failed")
diff --git a/exllamav3/util/knobs.py b/exllamav3/util/knobs.py
index 80f1c70..4ce3837 100644
--- a/exllamav3/util/knobs.py
+++ b/exllamav3/util/knobs.py
@@ -49,6 +49,7 @@ KNOBS = {
"EXL3_QDEC_HIP_SPL": "",
"EXL3_QDEC_HIP_WAVES": "0",
"EXL3_QDEC_HIP_VPF": "1",
+ "EXL3_QDEC_HIP_SMM": "0",
"EXL3_QDEC_HIP_FLAGS": "",
}