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.

tq cache: fix rotate kernel Triton compile on gfx1151, qdec TQ template

YanissAmz342a4b337742
diff --git a/exllamav3/modules/attention_fn/qdec.hip b/exllamav3/modules/attention_fn/qdec.hip
index 4493192..2305411 100644
--- a/exllamav3/modules/attention_fn/qdec.hip
+++ b/exllamav3/modules/attention_fn/qdec.hip
@@ -102,10 +102,17 @@ template<int B> __device__ __forceinline__ u32 dequant2(u32 raw_m, u32 s2, u32 s
}
// TurboQuant: two raw values (fp16 pair 1024 + raw) -> half2 (codebook[raw] * scale); the codebook sits in LDS as fp16 bits
-__device__ __forceinline__ u32 dequant2_tq(u32 raw_m, u32 s2, const u16* cb)
+#ifndef TQ_NOLUT
+#define TQ_NOLUT 0 // experiment: uniform grid instead of the codebook (timing only, wrong values)
+#endif
+template<int B> __device__ __forceinline__ u32 dequant2_tq(u32 raw_m, u32 s2, const u16* cb)
{
+#if TQ_NOLUT
+ return hmul2(hsub2(raw_m, c2<B>()), s2);
+#else
u32 lo = cb[raw_m & 0x3FFu], hi = cb[(raw_m >> 16) & 0x3FFu];
return hmul2(lo | (hi << 16), s2);
+#endif
}
// K group plane offsets (words)
@@ -281,7 +288,7 @@ qdec_split(
v8u a;
#pragma unroll
for (int i = 0; i < 8; i++)
- a[i] = TQ ? dequant2_tq(raw_pair_m<KB>(kw + gl * KB, 16 * h + 2 * i, 0, KO4, KO2, KO1), sc, cbk_s)
+ a[i] = TQ ? dequant2_tq<KB>(raw_pair_m<KB>(kw + gl * KB, 16 * h + 2 * i, 0, KO4, KO2, KO1), sc, cbk_s)
: dequant2<KB>(raw_pair_m<KB>(kw + gl * KB, 16 * h + 2 * i, 0, KO4, KO2, KO1), sc, sch);
v16h af = __builtin_bit_cast(v16h, a);
#pragma unroll
@@ -389,7 +396,7 @@ qdec_split(
#pragma unroll
for (int e = 0; e < 16; e++) {
u32 sh = hmul2(vsc[e], 0x38003800u);
- dq[e] = TQ ? dequant2_tq(raw_pair_m<VB>(vr[e], 2 * u, 0, RUN_W8, RUN_W8 + RUN_W4, RUN_W8 + RUN_W4 + RUN_W2), vsc[e], cbv_s)
+ dq[e] = TQ ? dequant2_tq<VB>(raw_pair_m<VB>(vr[e], 2 * u, 0, RUN_W8, RUN_W8 + RUN_W4, RUN_W8 + RUN_W4 + RUN_W2), vsc[e], cbv_s)
: dequant2<VB>(raw_pair_m<VB>(vr[e], 2 * u, 0, RUN_W8, RUN_W8 + RUN_W4, RUN_W8 + RUN_W4 + RUN_W2), vsc[e], sh);
}
v8u b0, b1;
diff --git a/exllamav3/modules/attention_fn/tq_triton.py b/exllamav3/modules/attention_fn/tq_triton.py
index 8d75af1..0bfd5e7 100644
--- a/exllamav3/modules/attention_fn/tq_triton.py
+++ b/exllamav3/modules/attention_fn/tq_triton.py
@@ -151,20 +151,25 @@ def _tq_rotate_kernel(
cb = tl.program_id(1)
nb0 = tl.cdiv(n0, BM)
nb1 = tl.cdiv(n1, BM)
- if pid < nb0:
- xp, yp, nr, blk = x0, y0, n0, pid
- elif pid < nb0 + nb1:
- xp, yp, nr, blk = x1, y1, n1, pid - nb0
- else:
- xp, yp, nr, blk = x2, y2, n2, pid - nb0 - nb1
+ is0 = pid < nb0
+ is1 = (pid >= nb0) & (pid < nb0 + nb1)
+ is2 = pid >= nb0 + nb1
+ blk = tl.where(is0, pid, tl.where(is1, pid - nb0, pid - nb0 - nb1))
rows = blk * BM + tl.arange(0, BM)
- rmask = rows < nr
+ m0 = (rows < n0) & is0
+ m1 = (rows < n1) & is1
+ m2 = (rows < n2) & is2
ks = tl.arange(0, D)
cols = cb * BN + tl.arange(0, BN)
- xt = tl.load(xp + rows[:, None].to(tl.int64) * D + ks[None, :], mask = rmask[:, None], other = 0.0)
+ ro = rows[:, None].to(tl.int64) * D
+ xt = tl.load(x0 + ro + ks[None, :], mask = m0[:, None], other = 0.0)
+ xt += tl.load(x1 + ro + ks[None, :], mask = m1[:, None], other = 0.0)
+ xt += tl.load(x2 + ro + ks[None, :], mask = m2[:, None], other = 0.0)
rt = tl.load(rot_t + ks[:, None] * D + cols[None, :])
- acc = tl.dot(xt, rt)
- tl.store(yp + rows[:, None].to(tl.int64) * D + cols[None, :], acc.to(tl.float16), mask = rmask[:, None])
+ acc = tl.dot(xt, rt).to(tl.float16)
+ tl.store(y0 + ro + cols[None, :], acc, mask = m0[:, None])
+ tl.store(y1 + ro + cols[None, :], acc, mask = m1[:, None])
+ tl.store(y2 + ro + cols[None, :], acc, mask = m2[:, None])
def tq_rotate(rot_t, *xs):