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
mmvq: Q4_K / Q6_K decode rows through the llama.cpp vec-dots (CUDA default) (#207)
Yaniss Amazouz2e391224b004
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.mdindex 75adf98..3fdc11a 100644--- a/THIRD_PARTY_NOTICES.md+++ b/THIRD_PARTY_NOTICES.md@@ -10,6 +10,8 @@ llama.cpp (https://github.com/ggml-org/llama.cpp, commit 7182ef7):- ggml_vecdotq_iq.cuh: the vec_dot_iq*_q8_1 functions and helpers from ggml/src/ggml-cuda/vecdotq.cuh (only ggml_cuda_dp4a renamed to __dp4a,CUDA-only helpers).- fq_mmvq.cu: kernel structure after ggml/src/ggml-cuda/mmvq.cu (mul_mat_vec_q, mul_mat_vec_q_moe).+- ggml_vecdotq_k46.cuh: vec_dot_q4_K_q8_1 and vec_dot_q6_K_q8_1 (with their _impl helpers) and the block_q4_K / block_q6_K layouts,+ from llama.cpp commit 61dbf49 (only ggml_cuda_dp4a renamed to ggml_dp4a).MIT Licensediff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cppindex 75b0743..67596cd 100644--- a/exllamav3/exllamav3_ext/bindings.cpp+++ b/exllamav3/exllamav3_ext/bindings.cpp@@ -654,6 +654,10 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)py::arg("partial"), py::arg("out"));m.def("fq_mmvq_q5_k", &fq_mmvq_q5_k, "fq_mmvq_q5_k",py::arg("qb"), py::arg("packed"), py::arg("out"));+ m.def("fq_mmvq_q4_k", &fq_mmvq_q4_k, "fq_mmvq_q4_k",+ py::arg("qb"), py::arg("packed"), py::arg("out"));+ m.def("fq_mmvq_q6_k", &fq_mmvq_q6_k, "fq_mmvq_q6_k",+ py::arg("qb"), py::arg("packed"), py::arg("out"));// grouped KQ/IQ4_XS decode MoE (fq_moe_kq.cu): portable dp4a kernels, built on ROCm toom.def("fq_moe_q4k_grouped_q8", &fq_moe_q4k_grouped_q8, "fq_moe_q4k_grouped_q8",py::arg("h"), py::arg("sel"), py::arg("wts"),diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cuindex c1d9d12..76c9a0e 100644--- a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu@@ -21,7 +21,7 @@#include "../util.h"#include "fq_api.h"#include "ggml_mmvq/ggml_vecdotq_iq.cuh"-#include "ggml_mmvq/ggml_vecdotq_k.cuh"+#include "ggml_mmvq/ggml_vecdotq_k46.cuh"#if defined(USE_ROCM)// HIP: maskless shuffles (RDNA runs these kernels as wave32, MQ_WARP = 32)@@ -32,7 +32,7 @@ namespace {using namespace ggml_iq;-enum { MQ_IQ2_XXS = 0, MQ_IQ2_XS = 1, MQ_IQ2_S = 2, MQ_IQ3_XXS = 3, MQ_IQ3_S = 4, MQ_IQ4_NL = 5, MQ_IQ4_XS = 6, MQ_Q5_K = 7, MQ_NTYPES = 8 };+enum { MQ_IQ2_XXS = 0, MQ_IQ2_XS = 1, MQ_IQ2_S = 2, MQ_IQ3_XXS = 3, MQ_IQ3_S = 4, MQ_IQ4_NL = 5, MQ_IQ4_XS = 6, MQ_Q5_K = 7, MQ_Q4_K = 8, MQ_Q6_K = 9, MQ_NTYPES = 10 };// qk = weight elements per block, qi = ints per block (ggml QI*), vdr = ints per lane per call, bb = block bytestemplate <int T> struct MqTraits;@@ -50,6 +50,8 @@ MQ_TRAITS(MQ_IQ3_S, 256, 16, VDR_IQ3_S_Q8_1_MMVQ, 110, vec_dot_iq3_s_q8_1);MQ_TRAITS(MQ_IQ4_NL, 32, 4, VDR_IQ4_NL_Q8_1_MMVQ, 18, vec_dot_iq4_nl_q8_1);MQ_TRAITS(MQ_IQ4_XS, 256, 32, VDR_IQ4_XS_Q8_1_MMVQ, 136, vec_dot_iq4_xs_q8_1);MQ_TRAITS(MQ_Q5_K, 256, 32, VDR_Q5_K_Q8_1_MMVQ, 176, vec_dot_q5_K_q8_1);+MQ_TRAITS(MQ_Q4_K, 256, 32, VDR_Q4_K_Q8_1_MMVQ, 144, vec_dot_q4_K_q8_1);+MQ_TRAITS(MQ_Q6_K, 256, 32, VDR_Q6_K_Q8_1_MMVQ, 210, vec_dot_q6_K_q8_1);#undef MQ_TRAITSconstexpr int MQ_WARP = 32;@@ -487,6 +489,8 @@ void fq_mmvq_iq3_s(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) {void fq_mmvq_iq4_nl(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_IQ4_NL>(qb, packed, out); }void fq_mmvq_iq4_xs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_IQ4_XS>(qb, packed, out); }void fq_mmvq_q5_k(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_Q5_K>(qb, packed, out); }+void fq_mmvq_q4_k(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_Q4_K>(qb, packed, out); }+void fq_mmvq_q6_k(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_Q6_K>(qb, packed, out); }// Standalone grouped decode MoE for IQ experts (any of the 7 IQ formats for gate/up and for down; type ids = MQ_* order:// 0 iq2_xxs, 1 iq2_xs, 2 iq2_s, 3 iq3_xxs, 4 iq3_s, 5 iq4_nl, 6 iq4_xs). Same arguments and results as fq_moe_kq_grouped_mixed_q8:diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.hindex 71a57f1..ca082d0 100644--- a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h@@ -11,6 +11,8 @@ void fq_mmvq_iq3_s(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);void fq_mmvq_iq4_nl(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);void fq_mmvq_iq4_xs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);void fq_mmvq_q5_k(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);+void fq_mmvq_q4_k(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);+void fq_mmvq_q6_k(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);// Grouped decode MoE for IQ experts: format ids 0 iq2_xxs, 1 iq2_xs, 2 iq2_s, 3 iq3_xxs, 4 iq3_s, 5 iq4_nl, 6 iq4_xs, 7 q5_k (gate/up and down independent).// Arguments and results as fq_moe_kq_grouped_mixed_q8 (fq_api.h).void fq_moe_mmvq_grouped(torch::Tensor h, torch::Tensor sel, torch::Tensor wts, torch::Tensor gate_ptrs, torch::Tensor up_ptrs,diff --git a/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k46.cuh b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k46.cuhnew file mode 100644index 0000000..df606a2--- /dev/null+++ b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k46.cuh@@ -0,0 +1,167 @@+// Q4_K and Q6_K vec-dots (q8_1 activations, integer dp4a dots) ported from llama.cpp ggml/src/ggml-cuda/vecdotq.cuh+// (https://github.com/ggml-org/llama.cpp, commit 61dbf49) and block_q4_K / block_q6_K of ggml/src/ggml-common.h.+// MIT License, Copyright (c) 2023-2026 The ggml authors. See THIRD_PARTY_NOTICES.md at the repository root.+// Changes: ggml_cuda_dp4a -> ggml_dp4a, namespace ggml_iq, plain half2 / half block fields. The dot-product bodies are unchanged.+#pragma once++#include "ggml_vecdotq_k.cuh"++namespace ggml_iq {++#define QR4_K 2+#define QI4_K (QK_K / (4*QR4_K))+#define QR6_K 2+#define QI6_K (QK_K / (4*QR6_K))++struct block_q4_K {+ half2 dm; // d, dmin+ uint8_t scales[K_SCALE_SIZE];+ uint8_t qs[QK_K / 2];+};+static_assert(sizeof(block_q4_K) == 144, "ggml block layout");++struct block_q6_K {+ uint8_t ql[QK_K / 2];+ uint8_t qh[QK_K / 4];+ int8_t scales[QK_K / 16];+ half d;+};+static_assert(sizeof(block_q6_K) == 210, "ggml block layout");++#define VDR_Q4_K_Q8_1_MMVQ 2+#define VDR_Q6_K_Q8_1_MMVQ 2++// contiguous v/x values+static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(+ const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,+ const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {++ float sumf_d = 0.0f;+ float sumf_m = 0.0f;++#pragma unroll+ for (int i = 0; i < QR4_K; ++i) {+ const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;+ const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;++ const int dot1 = ggml_dp4a(v1i, u[2*i+1], ggml_dp4a(v0i, u[2*i+0], 0)); // SIMD dot product+ const int dot2 = ggml_dp4a(0x01010101, u[2*i+1], ggml_dp4a(0x01010101, u[2*i+0], 0)); // sum of u++ sumf_d += d8[i] * (dot1 * sc[i]);+ sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values+ }++ const float2 dm4f = __half22float2(dm4);++ return dm4f.x*sumf_d - dm4f.y*sumf_m;+}++// contiguous v/x values+static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(+ const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const int8_t * __restrict__ scales,+ const float & d, const float * __restrict__ d8) {++ float sumf = 0.0f;++#pragma unroll+ for (int i = 0; i < QR6_K; ++i) {+ const int sc = scales[4*i];++ int sumi = 0;+#pragma unroll+ for (int j = 0; j < VDR_Q6_K_Q8_1_MMVQ; ++j) {+ const int vil = (vl[j] >> (4*i)) & 0x0F0F0F0F;+ const int vih = ((vh[j] >> (4*i)) << 4) & 0x30303030;++ // (vil | vih) - 32 for each byte, the - 32 is done by the second dp4a+ sumi = ggml_dp4a(vil | vih, u[VDR_Q6_K_Q8_1_MMVQ*i + j], sumi); // SIMD dot product+ sumi = ggml_dp4a(0xE0E0E0E0, u[VDR_Q6_K_Q8_1_MMVQ*i + j], sumi);+ }++ sumf += d8[i] * (sumi * sc);+ }++ return d*sumf;+}++static __device__ __forceinline__ float vec_dot_q4_K_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q4_K * bq4_K = (const block_q4_K *) vbq + kbx;++ int v[2];+ int u[2*QR4_K];+ float d8[QR4_K];++ // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6+ const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));++ const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));+ v[0] = q4[0];+ v[1] = q4[4];++ // branchless so nvcc can hoist this out of the ncols_dst loop+ const uint16_t * scales = (const uint16_t *)bq4_K->scales;+ const int j = bq8_offset/2;+ const int jm = j & 1;++ const uint32_t s0 = scales[jm + 0];+ const uint32_t s2 = scales[jm + 2];+ const uint32_t s4 = scales[jm + 4];++ const uint32_t hi = (uint32_t) -(int32_t) (j >= 2);++ uint16_t aux[2];+ aux[0] = (uint16_t) (((s0 & 0x3f3f) & ~hi) | ((((s4 >> 0) & 0x0f0f) | ((s0 & 0xc0c0) >> 2)) & hi));+ aux[1] = (uint16_t) (((s2 & 0x3f3f) & ~hi) | ((((s4 >> 4) & 0x0f0f) | ((s2 & 0xc0c0) >> 2)) & hi));+ const uint8_t * sc = (const uint8_t *)aux;+ const uint8_t * m = sc + 2;++#pragma unroll+ for (int i = 0; i < QR4_K; ++i) {+ const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;+ d8[i] = __low2float(bq8i->ds);++ const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);+ u[2*i+0] = q8[0];+ u[2*i+1] = q8[4];+ }++ return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);+}++static __device__ __forceinline__ float vec_dot_q6_K_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q6_K * bq6_K = (const block_q6_K *) vbq + kbx;++ const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);+ const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);+ const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));++ int vl[VDR_Q6_K_Q8_1_MMVQ];+ int vh[VDR_Q6_K_Q8_1_MMVQ];+#pragma unroll+ for (int j = 0; j < VDR_Q6_K_Q8_1_MMVQ; ++j) {+ vl[j] = get_int_b2(bq6_K->ql, iqs + j);+ vh[j] = get_int_b2(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4) + j) >> vh_shift;+ }++ const int8_t * scales = bq6_K->scales + scale_offset;++ int u[VDR_Q6_K_Q8_1_MMVQ*QR6_K];+ float d8[QR6_K];++#pragma unroll+ for (int i = 0; i < QR6_K; ++i) {+#pragma unroll+ for (int j = 0; j < VDR_Q6_K_Q8_1_MMVQ; ++j) {+ u[VDR_Q6_K_Q8_1_MMVQ*i + j] = get_int_b4(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1 + j);+ }+ d8[i] = __low2float(bq8_1[bq8_offset + 2*i].ds);+ }++ return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, __half2float(bq6_K->d), d8);+}++} // namespace ggml_iqdiff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.pyindex 844ee92..671eee8 100644--- a/exllamav3/modules/foreign_linear.py+++ b/exllamav3/modules/foreign_linear.py@@ -78,6 +78,10 @@ _IQ_Q8_FN = {"iq3_xxs": "fq_gemv_iq3_xxs_q8", "iq2_s": "fq_gemv_iq2_s_q8", "iq3__MMVQ_ON = os.environ.get("BOLT_FQ_MMVQ", "1") != "0"_MMVQ_FN = {"iq2_xxs": "fq_mmvq_iq2_xxs", "iq2_xs": "fq_mmvq_iq2_xs", "iq2_s": "fq_mmvq_iq2_s", "iq3_xxs": "fq_mmvq_iq3_xxs","iq3_s": "fq_mmvq_iq3_s", "iq4_nl": "fq_mmvq_iq4_nl", "iq4_xs": "fq_mmvq_iq4_xs", "q5_k": "fq_mmvq_q5_k"}+# BOLT_FQ_MMVQ_KQ: Q4_K / Q6_K decode rows (1..15) also through the mmvq kernel (llama.cpp vec_dot_q4_K / q6_K_q8_1). Default on for+# CUDA (RTX 3090, 8B Q4_K_M served: N8 +28 %, N4 +11 %, N1 +4 %), off on ROCm until measured there; 0 / 1 forces.+if os.environ.get("BOLT_FQ_MMVQ_KQ", "1" if (torch.version.cuda and not torch.version.hip) else "0") == "1":+ _MMVQ_FN.update({"q4_k": "fq_mmvq_q4_k", "q6_k": "fq_mmvq_q6_k"})_MMVQ_QK = {"iq4_nl": 32}_DEQ_IQ_ID = {"iq2_xs": 1, "iq2_s": 2, "iq3_xxs": 3, "iq3_s": 4, "iq4_nl": 5} # fq_dequant_iq format ids (the others have native dequants already)# rows served by the mmvq kernel (8 columns per launch, so R/8 passes over the weights): the old IQ path above 16 rows dequantized the whole weight in torch@@ -262,7 +266,7 @@ class ForeignLinear(Module):if not flat.is_contiguous():flat = flat.contiguous()out: Optional[torch.Tensor] = None- if (_MMVQ_ON and self.format in _MMVQ_FN and flat.dtype == torch.half and R <= (min(_MMVQ_MAXR, FUSED_ROWS_MAX - 1) if self.format == "q5_k" else _MMVQ_MAXR) and K % _MMVQ_QK.get(self.format, 256) == 0+ if (_MMVQ_ON and self.format in _MMVQ_FN and flat.dtype == torch.half and R <= (min(_MMVQ_MAXR, FUSED_ROWS_MAX - 1) if self.format in ("q5_k", "q4_k", "q6_k") else _MMVQ_MAXR) and K % _MMVQ_QK.get(self.format, 256) == 0and hasattr(ext, _MMVQ_FN[self.format])):N = self.out_featuresout = torch.empty((R, N), dtype=dtype if dtype in (torch.half, torch.float) else torch.half, device=dev)diff --git a/tests/test_mmvq_iq.py b/tests/test_mmvq_iq.pyindex 95e3b9c..92850d4 100644--- a/tests/test_mmvq_iq.py+++ b/tests/test_mmvq_iq.py@@ -14,26 +14,31 @@ FMTS = { # name: (block bytes, elements per block, mmvq id)"iq2_xxs": (66, 256, 0), "iq2_xs": (74, 256, 1), "iq2_s": (82, 256, 2), "iq3_xxs": (98, 256, 3),"iq3_s": (110, 256, 4), "iq4_nl": (18, 32, 5), "iq4_xs": (136, 256, 6), "q5_k": (176, 256, 7),}+# dense-only K-quant entries (llama.cpp vec_dot_q4_K / q6_K_q8_1 in fq_mmvq.cu; no grouped MoE id)+KQ_DENSE = {"q4_k": (144, 256, 8), "q6_k": (210, 256, 9)}def make_weights(fmt, n, k, seed, dscale=1.0):- bb, qk, _ = FMTS[fmt]+ bb, qk, _ = {**FMTS, **KQ_DENSE}[fmt]g = torch.Generator().manual_seed(seed)w = torch.randint(0, 256, (n, k // qk, bb), generator=g, dtype=torch.uint8)d = ((torch.rand(n, k // qk, generator=g) * 0.02 + 0.001) * dscale).half().view(torch.uint8).reshape(n, k // qk, 2)- w[:, :, 0:2] = d- if fmt == "q5_k": # half2 (d, dmin)+ if fmt == "q6_k": # d is the last field of block_q6_K+ w[:, :, 208:210] = d+ else:+ w[:, :, 0:2] = d+ if fmt in ("q5_k", "q4_k"): # half2 (d, dmin)w[:, :, 2:4] = dreturn w.reshape(n, -1).cuda()def dequant(fmt, w, n, k):- bb, qk, _ = FMTS[fmt]+ bb, qk, _ = {**FMTS, **KQ_DENSE}[fmt]if fmt == "iq2_xxs":out = torch.empty((n, k), dtype=torch.half, device="cuda")ext.fq_dequant_iq2_xxs(w.reshape(n, k // 256, 66), out)return out.float()- if fmt in ("iq4_xs", "q5_k"):+ if fmt in ("iq4_xs", "q5_k", "q4_k", "q6_k"):out = torch.empty((n, k), dtype=torch.half, device="cuda")getattr(ext, f"fq_dequant_{fmt}")(w, out)return out.float()@@ -48,13 +53,13 @@ def q8_dequant(qb, m, k):OLD = {"iq2_xxs": "fq_gemv_iq2_xxs_q8", "iq3_xxs": "fq_gemv_iq3_xxs_q8", "iq2_s": "fq_gemv_iq2_s_q8", "iq3_s": "fq_gemv_iq3_s_q8",- "iq4_xs": "fq_gemv_iq4_xs_q8", "q5_k": "fq_gemv_q5_k_q8"}+ "iq4_xs": "fq_gemv_iq4_xs_q8", "q5_k": "fq_gemv_q5_k_q8", "q4_k": "fq_gemv_q4_k_q8", "q6_k": "fq_gemv_q6_k_q8"}worst_old = [0.0]def test_dense():worst = 0.0- for fmt, (bb, qk, _) in FMTS.items():+ for fmt, (bb, qk, _) in {**FMTS, **KQ_DENSE}.items():for n, k in ((1024, 4096), (777, 2048), (512, 512)):w = make_weights(fmt, n, k, 7)wd = dequant(fmt, w, n, k)