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.

EXL3 GEMM: mul1 pair pack via v_sad_hi_u8 + mad16 inner loop (v_mul_u32_u24 + v_mad_u16, bit-exact; GLM decode x1.059, bit-identical texts)

YanissAmz9d0815707ef0
diff --git a/exllamav3/exllamav3_ext/quant/codebook.cuh b/exllamav3/exllamav3_ext/quant/codebook.cuh
index 4e1b902..4898617 100644
--- a/exllamav3/exllamav3_ext/quant/codebook.cuh
+++ b/exllamav3/exllamav3_ext/quant/codebook.cuh
@@ -63,11 +63,17 @@ __device__ inline half2 decode_mul1_product_2_sad(uint32_t x0, uint32_t x1)
#if (defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__)) && defined(EXL3_MUL1_DECODE_DOT4)
const uint32_t sum1 = __builtin_amdgcn_udot4(x1, 0x01010101u, 0x6400u, false);
const uint32_t sum0 = __builtin_amdgcn_udot4(x0, 0x01010101u, 0x6400u, false);
+ const uint32_t packed = (sum1 << 16) + sum0; // u + 1024 <= 2029: no 16-bit carry
+#elif defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__)
+ // v_sad_hi_u8 D = (SAD(S0, S1) << 16) + S2 does the pair pack: low half = 0x6400 + u0 (no carry, u0 <= 1020),
+ // high half = 0x6400 + u1. Same bits as the three-op form, one VALU op fewer per two weights.
+ uint32_t packed;
+ asm("v_sad_hi_u8 %0, %1, 0, %2" : "=v"(packed) : "v"(x1), "v"(exl3_sad_u8_(x0, 0x64006400u)));
#else
const uint32_t sum1 = exl3_sad_u8_(x1, 0x6400u);
const uint32_t sum0 = exl3_sad_u8_(x0, 0x6400u);
+ const uint32_t packed = (sum1 << 16) + sum0;
#endif
- const uint32_t packed = (sum1 << 16) + sum0; // u + 1024 <= 2029: no 16-bit carry
half2 k_inv_h2 = __half2half2(__ushort_as_half(0x1eee));
half2 k_bias_h2 = __half2half2(__ushort_as_half(0xc931));
half_uint16 h0((uint16_t) packed);
@@ -168,13 +174,19 @@ __device__ inline half2 decode_3inst_2(uint32_t x0, uint32_t x1)
}
}
-// x * C mod 2^32 for x < 2^16. gfx115x: v_mul_lo_u32 is quarter rate; two full-rate 24-bit multiplies
-// and a shift-add give the same bits: x * C = x * C_lo16 + ((x * C_hi16) << 16) mod 2^32.
+// x * C mod 2^32 for x < 2^16. gfx115x: v_mul_lo_u32 is quarter rate; the identity
+// x * C = x * C_lo16 + ((x * C_hi16) << 16) mod 2^32 (C_lo16 = C & 0xffff, C_hi16 = C >> 16) gives the same bits from
+// full-rate ops only. Written in asm so LLVM cannot fold the split back into one quarter-rate v_mul_lo_u32
+// (exl3_moe_prefill.cu): v_mul_u32_u24 takes the low 24 bits of x and C_lo16 (both < 2^24), then v_mad_u16 with
+// op_sel [0,0,1,1] adds x * C_hi16 into the high half and keeps the low half.
template <uint32_t C>
__device__ __forceinline__ uint32_t mul_const_w16(uint32_t x)
{
#if defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__)
- return __umul24(x, C & 0xffffu) + (__umul24(x, C >> 16) << 16);
+ uint32_t d;
+ asm("v_mul_u32_u24 %0, %1, %2" : "=v"(d) : "v"(x), "s"(C & 0xffffu));
+ asm("v_mad_u16 %0, %1, %2, %0 op_sel:[0,0,1,1]" : "+v"(d) : "v"(x), "s"(C >> 16));
+ return d;
#else
return x * C;
#endif
diff --git a/exllamav3/exllamav3_ext/quant/exl3_gemv_kernel.cuh b/exllamav3/exllamav3_ext/quant/exl3_gemv_kernel.cuh
index c1fc18c..d6fded3 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_gemv_kernel.cuh
+++ b/exllamav3/exllamav3_ext/quant/exl3_gemv_kernel.cuh
@@ -107,11 +107,16 @@ __device__ __forceinline__ half2 decode_pair_cb2_sad_(uint32_t x0, uint32_t x1)
#if (defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__)) && defined(EXL3_MUL1_DECODE_DOT4)
const uint32_t sum1 = __builtin_amdgcn_udot4(x1, 0x01010101u, 0x6400u, false);
const uint32_t sum0 = __builtin_amdgcn_udot4(x0, 0x01010101u, 0x6400u, false);
+ const uint32_t packed = (sum1 << 16) + sum0;
+#elif defined(__gfx1150__) || defined(__gfx1151__) || defined(__gfx1152__)
+ // v_sad_hi_u8 D = (SAD(S0, S1) << 16) + S2 packs the pair in one op: same bits as the form below.
+ uint32_t packed;
+ asm("v_sad_hi_u8 %0, %1, 0, %2" : "=v"(packed) : "v"(x1), "v"(exl3_sad_u8_(x0, 0x64006400u)));
#else
const uint32_t sum1 = exl3_sad_u8_(x1, 0x6400u);
const uint32_t sum0 = exl3_sad_u8_(x0, 0x6400u);
-#endif
const uint32_t packed = (sum1 << 16) + sum0;
+#endif
half2 k_inv_h2 = __half2half2(__ushort_as_half(0x1eee));
half2 k_bias_h2 = __half2half2(__ushort_as_half(0xc931));
half_uint16 h0((uint16_t) packed);