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
NVFP4 multi-row decode GEMV on ROCm: weights read once for 2-16 rows (#204)
Yaniss Amazouz396f6decd512
diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_gemv_q8.cu b/exllamav3/exllamav3_ext/foreign_quant/fq_gemv_q8.cuindex 834a841..591892b 100644--- a/exllamav3/exllamav3_ext/foreign_quant/fq_gemv_q8.cu+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_gemv_q8.cu@@ -205,6 +205,108 @@ __global__ void fq_gemv_q8_nvfp4_kernel(const uint8_t* __restrict__ aqb,}}+// Four doubled-e2m1 int8 x4 words of one 16-element NVFP4 group (8 packed bytes at prc): the same expansion as fq_dot_nvfp4_q8+// (__byte_perm on CUDA, the bank-spread table elsewhere), so a dot built on it is bit-identical to that one.+__device__ __forceinline__ void fq_nvfp4_group_w(const uint8_t* __restrict__ prc, const int8_t* __restrict__ e2m1db,+ int32_t& w0, int32_t& w1, int32_t& w2, int32_t& w3)+{+#if defined(__CUDA_ARCH__)+ uint32_t q4a = reinterpret_cast<const uint32_t*>(prc)[0];+ uint32_t q4b = reinterpret_cast<const uint32_t*>(prc)[1];+ int2 e01 = fq_e2m1_expand_8(q4a);+ int2 e23 = fq_e2m1_expand_8(q4b);+ w0 = e01.x; w1 = e01.y; w2 = e23.x; w3 = e23.y;+#else+ uint32_t b[16];+ #pragma unroll+ for (int j = 0; j < 16; j++)+ {+ uint8_t pbyte = prc[j >> 1];+ uint8_t nib = (j & 1) ? (pbyte >> 4) & 0xF : pbyte & 0xF;+ b[j] = (uint32_t)(uint8_t)e2m1db[((uint32_t)nib << 3) | (uint32_t)(j & 7)];+ }+ w0 = (int32_t)(b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24));+ w1 = (int32_t)(b[4] | (b[5] << 8) | (b[6] << 16) | (b[7] << 24));+ w2 = (int32_t)(b[8] | (b[9] << 8) | (b[10] << 16) | (b[11] << 24));+ w3 = (int32_t)(b[12] | (b[13] << 8) | (b[14] << 16) | (b[15] << 24));+#endif+}++// Multi-row NVFP4 q8 dot (BOLT_FQ_NVFP4_MROWS, default on for 2..16 x-rows): one block per column n, rows m0 .. m0 + mc - 1 (mc <= 8,+// grid.y = row groups). Each 16-element weight group is expanded once and dotted against every row (the one-row kernel re-read the whole+// matrix per x-row: 4 rows cost 3-4x one row cold). Per row: same chunk loop per thread, same scale product, same sequential reduction as+// fq_gemv_q8_nvfp4_kernel, so each output is bit-identical to the one-row kernel.+template <typename OUT>+__global__ void fq_gemv_q8_nvfp4_mrows_kernel(const uint8_t* __restrict__ aqb,+ const uint8_t* __restrict__ packed,+ const uint8_t* __restrict__ scales,+ float global,+ OUT* __restrict__ out,+ int M, int N, int K, int64_t packed_stride,+ int64_t scales_stride, int64_t qb_stride)+{+ constexpr int MR = 8;+ int n = blockIdx.x;+ int m0 = blockIdx.y * MR;+ int mc = min(MR, M - m0);+ int tid = threadIdx.x;+ int nt = blockDim.x;++ extern __shared__ uint8_t sh[];+ int8_t* e2m1db = reinterpret_cast<int8_t*>(sh);+ float* red = reinterpret_cast<float*>(e2m1db + 128); // [MR][nt]++ fq_e2m1db_build(e2m1db);+ __syncthreads();+ float acc[MR];+ #pragma unroll+ for (int r = 0; r < MR; r++) acc[r] = 0.0f;++ const uint8_t* pr = packed + (int64_t)n * packed_stride;+ const uint8_t* sr = scales + (int64_t)n * scales_stride;+ int nchunks = K / 32;+ for (int c = tid; c < nchunks; c += nt)+ {+ for (int g = 0; g < 2; g++)+ {+ float sw = __fmul_rn(__uint_as_float(FQ_E4M3_BITS[sr[c * 2 + g]]), 0.5f);+ int32_t w0, w1, w2, w3;+ fq_nvfp4_group_w(pr + (size_t)c * 16 + g * 8, e2m1db, w0, w1, w2, w3);+ #pragma unroll+ for (int r = 0; r < MR; r++)+ {+ if (r < mc)+ {+ const uint8_t* ab = aqb + (int64_t)(m0 + r) * qb_stride + (size_t)c * FQ_Q8_1_BYTES;+ __half dah;+ memcpy(&dah, ab, 2);+ float d = __fmul_rn(sw, __half2float(dah));+ const int32_t* aq32 = reinterpret_cast<const int32_t*>(ab + 4 + g * 16);+ int sumi = 0;+ sumi = fq_dp4a(w0, aq32[0], sumi);+ sumi = fq_dp4a(w1, aq32[1], sumi);+ sumi = fq_dp4a(w2, aq32[2], sumi);+ sumi = fq_dp4a(w3, aq32[3], sumi);+ acc[r] = __fadd_rn(acc[r], __fmul_rn(d, (float)sumi));+ }+ }+ }+ }+ #pragma unroll+ for (int r = 0; r < MR; r++) red[r * nt + tid] = __fmul_rn(acc[r], global);+ __syncthreads();+ if (tid < mc)+ {+ float s = 0.0f;+ for (int l = 0; l < nt; l++)+ s = __fadd_rn(s, red[tid * nt + l]);+ if constexpr (std::is_same<OUT, __half>::value)+ out[(int64_t)(m0 + tid) * N + n] = __float2half_rn(s);+ else+ out[(int64_t)(m0 + tid) * N + n] = s;+ }+}+// Row-per-block Q8_0 q8 dot: same grid; the dot is fq_dot_q8_0_q8// (8 dp4a per 32-block, weights are int8, no tables).template <typename OUT>@@ -404,8 +506,17 @@ void fq_gemv_nvfp4_q8(torch::Tensor qb, torch::Tensor packed, torch::Tensor scalsize_t shmem = 128 * sizeof(int8_t) + (size_t)nt * sizeof(float);at::cuda::CUDAGuard guard(qb.device());auto stream = at::cuda::getCurrentCUDAStream().stream();+ static const int mrows = [] { const char* e = getenv("BOLT_FQ_NVFP4_MROWS"); return e ? atoi(e) : 1; }();auto launch = [&](auto* op){+ if (M >= 2 && M <= 16 && mrows)+ {+ size_t shm = 128 * sizeof(int8_t) + (size_t)8 * nt * sizeof(float);+ fq_gemv_q8_nvfp4_mrows_kernel<std::decay_t<decltype(*op)>> <<<dim3(N, (M + 7) / 8), nt, shm, stream>>>(+ qb.data_ptr<uint8_t>(), packed.data_ptr<uint8_t>(), scales.data_ptr<uint8_t>(),+ (float)global_scale, op, M, N, K, pstride, sstride, qb_stride);+ return;+ }fq_gemv_q8_nvfp4_kernel<std::decay_t<decltype(*op)>> <<<M * N, nt, shmem, stream>>>(qb.data_ptr<uint8_t>(), packed.data_ptr<uint8_t>(), scales.data_ptr<uint8_t>(),(float)global_scale, op, M, N, K, pstride, sstride, qb_stride);diff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.pyindex bfba8c5..844ee92 100644--- a/exllamav3/modules/foreign_linear.py+++ b/exllamav3/modules/foreign_linear.py@@ -32,6 +32,10 @@ _ROWS_TRITON = os.environ.get("BOLT_FQ_ROWS_TRITON", "1") != "0"# 87 ms N8 step on the 35B-A3B UD-Q4_K_XL, 75 % of device time); the C++ q8_1 GEMV (fq_gemv_q8_0) takes 70-80 us. HIP keeps the Triton kernel.# BOLT_FQ_ROWS_TRITON_Q8=1 restores it on CUDA (A/B)._ROWS_TRITON_Q8 = os.environ.get("BOLT_FQ_ROWS_TRITON_Q8", "0" if (torch.version.cuda and not torch.version.hip) else "1") != "0"+# NVFP4 2..16 rows on the C++ multi-row q8 kernel (fq_gemv_q8_nvfp4_mrows_kernel: weights read once, bit-identical to one row) instead+# of the Triton rows kernel, which streams cold weights at ~22 GB/s on gfx1151 (Ornith-1.5-9B N4: 1.14 ms per MLP GEMV). ROCm default;+# CUDA keeps the Triton rows kernel. Same switch as the C++ side.+_NVFP4_MROWS = os.environ.get("BOLT_FQ_NVFP4_MROWS", "1" if torch.version.hip else "0") != "0"# Q4_K / Q6_K rows 2..16: the C++ multi-row q8_1 GEMV (fq_gemv_kq.cu fq_gemv_kq_mrows_kernel: weights read once for all rows, per-row result bit-identical to# the one-row kernel) instead of the Triton rows kernel (fp16 activations, Triton speed). Default on CUDA; HIP keeps Triton until measured on gfx1151.@@ -270,6 +274,7 @@ class ForeignLinear(Module):out = out.to(dtype)elif (_ROWS_TRITON and flat.dtype == torch.half and (_ROWS_TRITON_Q8 or self.format != "q8_0")and not (_KQ_MROWS and self.format in _KQ_MROWS_FORMATS)+ and not (_NVFP4_MROWS and self.format == "nvfp4")and triton_rows.supported(self.format, R, K)):# 2..16 rows (verify / multi-user): weights read once for all rows (the fq_gemv kernels re-read them per row)out = triton_rows.gemv(self.format, flat, self.packed.reshape(self.out_features, -1), self.scales.reshape(self.out_features, -1)diff --git a/tests/test_nvfp4_gemv_mrows.py b/tests/test_nvfp4_gemv_mrows.pynew file mode 100644index 0000000..7fbc56e--- /dev/null+++ b/tests/test_nvfp4_gemv_mrows.py@@ -0,0 +1,54 @@+#!/usr/bin/env python3+"""NVFP4 multi-row q8 decode GEMV (x-rows 2..16, BOLT_FQ_NVFP4_MROWS): output row m must equal the one-row kernel's output for x-row m+BIT FOR BIT, for R = 1..16 (row groups of 8: R = 9..16 runs two groups), half and float output, several (N, K) shapes. Also checks that+BOLT_FQ_NVFP4_MROWS=0 (the old per-(row, column) launch) gives the same bytes, so batch composition never changes an output.+usage: python tests/test_nvfp4_gemv_mrows.py (needs a CUDA / HIP GPU and the built extension)"""+import os, subprocess, sys++CASES = [(1, 32), (37, 1024), (512, 2048), (1000, 2048), (2048, 4096), (4096, 512), (5, 12288)] # (N, K)+++def child(tag):+ import torch+ from exllamav3.ext import exllamav3_ext as ext+ torch.manual_seed(0)+ bad = n = 0+ outs = []+ for N, K in CASES:+ packed = torch.randint(0, 256, (N, K // 2), dtype=torch.uint8, device="cuda")+ scales = torch.randint(0x28, 0x48, (N, K // 16), dtype=torch.uint8, device="cuda") # positive E4M3 scales+ g = 0.0123+ for R in range(1, 17):+ x = torch.randn(R, K, device="cuda", dtype=torch.half)+ qb = torch.empty(R, K // 32 * 36, device="cuda", dtype=torch.uint8)+ ext.fq_quant_q8_1_rows(x, qb)+ for dt in (torch.half, torch.float):+ out = torch.empty(R, N, device="cuda", dtype=dt)+ ext.fq_gemv_nvfp4_q8(qb, packed, scales, g, out)+ for m in range(R):+ o1 = torch.empty(1, N, device="cuda", dtype=dt)+ ext.fq_gemv_nvfp4_q8(qb[m:m + 1].contiguous(), packed, scales, g, o1)+ n += 1+ if not torch.equal(out[m], o1[0]):+ bad += 1+ print(f"{tag}: MISMATCH N {N} K {K} R {R} row {m} {dt}", flush=True)+ outs.append(out.view(torch.uint8).flatten().cpu())+ torch.save(torch.cat(outs), sys.argv[3])+ print(f"{tag}: {n} row checks, {bad} mismatches", flush=True)+ sys.exit(1 if bad else 0)+++if __name__ == "__main__":+ if len(sys.argv) > 1 and sys.argv[1] == "--child":+ child(sys.argv[2])+ import tempfile+ import torch+ with tempfile.TemporaryDirectory() as td:+ rc = 0+ for tag, val in (("mrows", "1"), ("per_row", "0")):+ env = dict(os.environ, BOLT_FQ_NVFP4_MROWS=val)+ rc |= subprocess.run([sys.executable, __file__, "--child", tag, f"{td}/{tag}.pt"], env=env).returncode+ same = torch.equal(torch.load(f"{td}/mrows.pt"), torch.load(f"{td}/per_row.pt"))+ print("BOLT_FQ_NVFP4_MROWS=1 vs 0: " + ("identical bytes" if same else "DIFFERENT"))+ print("PASS" if rc == 0 and same else "FAIL")+ sys.exit(0 if rc == 0 and same else 1)