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
HIP: llama.cpp mmvq IQ kernels on Strix Halo, IQ2_XXS sign fix (#200)
Yaniss Amazouz9a466cb2ada2
diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cppindex 4bb2f28..6402a1a 100644--- a/exllamav3/exllamav3_ext/bindings.cpp+++ b/exllamav3/exllamav3_ext/bindings.cpp@@ -192,9 +192,7 @@ static void bind_exl3_dec(py::module_& m)}#endif-#if !defined(USE_ROCM)#include "foreign_quant/fq_mmvq_api.h"-#endifPYBIND11_MODULE(TORCH_EXTENSION_NAME, m){@@ -633,7 +631,6 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)py::arg("qb"), py::arg("packed"), py::arg("out"));m.def("fq_gemv_iq3_s_q8", &fq_gemv_iq3_s_q8, "fq_gemv_iq3_s_q8",py::arg("qb"), py::arg("packed"), py::arg("out"));-#if !defined(USE_ROCM)m.def("fq_mmvq_iq2_xxs", &fq_mmvq_iq2_xxs, "fq_mmvq_iq2_xxs",py::arg("qb"), py::arg("packed"), py::arg("out"));m.def("fq_mmvq_iq2_xs", &fq_mmvq_iq2_xs, "fq_mmvq_iq2_xs",@@ -655,7 +652,6 @@ 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"));-#endif// 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/build_config.py b/exllamav3/exllamav3_ext/build_config.pyindex fa108ce..0a76a92 100644--- a/exllamav3/exllamav3_ext/build_config.py+++ b/exllamav3/exllamav3_ext/build_config.py@@ -12,7 +12,7 @@ ROCM_EXCLUDE_DIRS = {'parallel', 'comp_units', 'foreign_quant'}# Other comp_units remain excluded because they implement unported MoE/GEMM paths.ROCM_ALLOW_PREFIXES = ('foreign_quant/fq_dequant.cu', 'foreign_quant/fq_dequant_kq.cu', 'foreign_quant/fq_gemv.cu',- 'foreign_quant/fq_gemv_kq.cu', 'foreign_quant/fq_gemv_q8.cu', 'foreign_quant/fq_moe_kq.cu', 'foreign_quant/fq_gemm.cu',+ 'foreign_quant/fq_gemv_kq.cu', 'foreign_quant/fq_gemv_q8.cu', 'foreign_quant/fq_mmvq.cu', 'foreign_quant/fq_moe_kq.cu', 'foreign_quant/fq_gemm.cu','quant/comp_units/exl3_gemv_int8_inst_','quant/comp_units/quantize_tiles_inst_',)diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_gemv.cuh b/exllamav3/exllamav3_ext/foreign_quant/fq_gemv.cuhindex eb9895f..50cdcf7 100644--- a/exllamav3/exllamav3_ext/foreign_quant/fq_gemv.cuh+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_gemv.cuh@@ -546,7 +546,7 @@ __device__ __forceinline__ uint32_t fq_vcmpne4(uint32_t a)uint32_t v = 0;#pragma unrollfor (int m = 0; m < 4; m++)- v |= ((0u - ((a >> (8 * m)) & 1u)) & 0xFFu) << (8 * m);+ v |= (((a >> (8 * m)) & 0xFFu) != 0u ? 0xFFu : 0u) << (8 * m); // any set bit of the byte (the sign masks use bits 0..7)return v;#endif}diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cuindex 4c1cc38..c1d9d12 100644--- a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu@@ -1,4 +1,4 @@-// IQ-format decode GEMV, llama.cpp mmvq style (CUDA only): q8_1 activations, integer dp4a dots of the ggml vec_dot_iq*_q8_1+// IQ-format decode GEMV, llama.cpp mmvq style (CUDA and HIP, wave32): q8_1 activations, integer dp4a dots of the ggml vec_dot_iq*_q8_1// functions (ggml_mmvq/ggml_vecdotq_iq.cuh), 1..8 activation columns per launch (weights read once for all columns).//// The kernel structure follows llama.cpp ggml/src/ggml-cuda/mmvq.cu (mul_mat_vec_q; https://github.com/ggml-org/llama.cpp,@@ -23,6 +23,11 @@#include "ggml_mmvq/ggml_vecdotq_iq.cuh"#include "ggml_mmvq/ggml_vecdotq_k.cuh"+#if defined(USE_ROCM)+// HIP: maskless shuffles (RDNA runs these kernels as wave32, MQ_WARP = 32)+#define __shfl_xor_sync(mask, var, ...) __shfl_xor(var, __VA_ARGS__)+#endif+namespace {using namespace ggml_iq;diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.hindex 3aba416..71a57f1 100644--- a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h@@ -1,4 +1,4 @@-// llama.cpp mmvq-style IQ decode GEMV (fq_mmvq.cu, CUDA only): q8_1 activations qb [M, K/32*36] (fq_quant_q8_1_rows),+// llama.cpp mmvq-style IQ decode GEMV (fq_mmvq.cu, CUDA and HIP): q8_1 activations qb [M, K/32*36] (fq_quant_q8_1_rows),// packed = raw GGUF bytes [N, K/QK*block_bytes], out [M, N] half or float. Any M (launches of <= 8 columns).#pragma once#include <torch/extension.h>diff --git a/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_iq.cuh b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_iq.cuhindex 863a3dd..5a616bd 100644--- a/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_iq.cuh+++ b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_iq.cuh@@ -2,16 +2,49 @@// ggml/src/ggml-cuda/vecdotq.cuh (https://github.com/ggml-org/llama.cpp, commit 7182ef7) and the block// layouts 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 -> __dp4a, CUDA-only helpers (no HIP/MUSA branches), everything in namespace ggml_iq,-// MMQ constants dropped. The dot-product bodies are unchanged.+// Changes: ggml_cuda_dp4a -> ggml_dp4a, the HIP helpers of ggml/src/ggml-cuda/vendors/hip.h (__vsub4, __vcmpne4, ggml_hip_perm) and+// the HIP branch of get_int_from_table_16 (llama.cpp commit ded522f) under USE_ROCM, no MUSA branches, everything in namespace+// ggml_iq, MMQ constants dropped. The dot-product bodies are unchanged.#pragma once#include <cuda_fp16.h>#include <stdint.h>#include "ggml_iq_tables.cuh"+#if defined(USE_ROCM)+// HIP has no __vsub4 / __vcmpne4: llama.cpp ggml/src/ggml-cuda/vendors/hip.h (MIT)+static __device__ __forceinline__ int __vsub4(const int a, const int b) {+ const unsigned int a_large = a | 0x80808080;+ const unsigned int b_small = b & 0x7f7f7f7f;+ const unsigned int result_low_7bits = a_large - b_small;+ const unsigned int should_flip_high_1bit = (a ^ ~b) & 0x80808080;+ return result_low_7bits ^ should_flip_high_1bit;+}+static __device__ __forceinline__ unsigned int __vcmpne4(unsigned int a, unsigned int b) {+ const unsigned int x = a ^ b;+ const unsigned int ne_low_7bits = ((x & 0x7f7f7f7f) + 0x7f7f7f7f) & 0x80808080;+ const unsigned int ne_high_1bit = x & 0x80808080;+ const unsigned int ne_any_bit = ne_low_7bits | ne_high_1bit;+ return (ne_any_bit >> 7) * 0xff;+}+#endif+namespace ggml_iq {+// signed int8 x int8 4-way dot (llama.cpp ggml_cuda_dp4a): CUDA __dp4a; RDNA3 / RDNA3.5 / RDNA4 v_dot4_i32_iu8; else scalar+static __device__ __forceinline__ int ggml_dp4a(const int a, const int b, int c) {+#if !defined(USE_ROCM)+ return __dp4a(a, b, c);+#elif defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || defined(__gfx1150__) || \+ defined(__gfx1151__) || defined(__gfx1152__) || defined(__gfx1200__) || defined(__gfx1201__)+ return __builtin_amdgcn_sudot4(true, a, true, b, c, false);+#else+ const int8_t * a8 = reinterpret_cast<const int8_t *>(&a);+ const int8_t * b8 = reinterpret_cast<const int8_t *>(&b);+ return c + a8[0] * b8[0] + a8[1] * b8[1] + a8[2] * b8[2] + a8[3] * b8[3];+#endif+}+#define QK_K 256#define QK4_NL 32#define IQ3S_N_SCALE (QK_K / 64)@@ -43,8 +76,20 @@ static __device__ __forceinline__ int get_int_b4(const void * x, const int & i32return ((const int *) x)[i32]; // assume at least 4 byte alignment}-// q4 contains 8 indices with 4 bit each; returns the table bytes at the even / odd indices (CUDA branch of llama.cpp get_int_from_table_16)+// q4 contains 8 indices with 4 bit each; returns the table bytes at the even / odd indices (llama.cpp get_int_from_table_16)static __device__ __forceinline__ int2 get_int_from_table_16(const int & q4, const int8_t * table) {+#if defined(USE_ROCM)+ const uint32_t * values = (const uint32_t *) table;+ const uint32_t q_even = q4;+ const uint32_t q_odd = (q4 >> 4);+ const uint32_t v_even_low = __builtin_amdgcn_perm(values[1], values[0], q_even & 0x07070707);+ const uint32_t v_odd_low = __builtin_amdgcn_perm(values[1], values[0], q_odd & 0x07070707);+ const uint32_t v_even_high = __builtin_amdgcn_perm(values[3], values[2], q_even & 0x07070707);+ const uint32_t v_odd_high = __builtin_amdgcn_perm(values[3], values[2], q_odd & 0x07070707);+ const uint32_t mask_even = 0x03020100 | ((q_even & 0x08080808) >> 1);+ const uint32_t mask_odd = 0x03020100 | ((q_odd & 0x08080808) >> 1);+ return make_int2(__builtin_amdgcn_perm(v_even_high, v_even_low, mask_even), __builtin_amdgcn_perm(v_odd_high, v_odd_low, mask_odd));+#elseconst uint32_t * table32 = (const uint32_t *) table;uint32_t tmp[2];const uint32_t low_high_selection_indices = (0x32103210 | ((q4 & 0x88888888) >> 1));@@ -56,6 +101,7 @@ static __device__ __forceinline__ int2 get_int_from_table_16(const int & q4, contmp[i] = __byte_perm(low, high, low_high_selection_indices >> shift);}return make_int2(__byte_perm(tmp[0], tmp[1], 0x6420), __byte_perm(tmp[0], tmp[1], 0x7531));+#endif}static __device__ __forceinline__ uint32_t unpack_ksigns(const uint8_t v) {@@ -85,12 +131,12 @@ static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(const int signs0 = __vcmpne4(signs & 0x08040201, 0);const int grid0 = __vsub4(grid_pos.x ^ signs0, signs0);const int u0 = get_int_b4(bq8_1[iqs/2].qs, k0 + 0);- sumi = __dp4a(grid0, u0, sumi);+ sumi = ggml_dp4a(grid0, u0, sumi);const int signs1 = __vcmpne4(signs & 0x80402010, 0);const int grid1 = __vsub4(grid_pos.y ^ signs1, signs1);const int u1 = get_int_b4(bq8_1[iqs/2].qs, k0 + 1);- sumi = __dp4a(grid1, u1, sumi);+ sumi = ggml_dp4a(grid1, u1, sumi);}const int ls = aux32 >> 27 | 1; // (scale * 2 + 1)@@ -127,11 +173,11 @@ static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);if (l0 < 4) {- sumi0 = __dp4a(grid_l, u0, sumi0);- sumi0 = __dp4a(grid_h, u1, sumi0);+ sumi0 = ggml_dp4a(grid_l, u0, sumi0);+ sumi0 = ggml_dp4a(grid_h, u1, sumi0);} else {- sumi1 = __dp4a(grid_l, u0, sumi1);- sumi1 = __dp4a(grid_h, u1, sumi1);+ sumi1 = ggml_dp4a(grid_l, u0, sumi1);+ sumi1 = ggml_dp4a(grid_h, u1, sumi1);}}const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4;@@ -173,11 +219,11 @@ static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);if (l0 < 4) {- sumi0 = __dp4a(grid_l, u0, sumi0);- sumi0 = __dp4a(grid_h, u1, sumi0);+ sumi0 = ggml_dp4a(grid_l, u0, sumi0);+ sumi0 = ggml_dp4a(grid_h, u1, sumi0);} else {- sumi1 = __dp4a(grid_l, u0, sumi1);- sumi1 = __dp4a(grid_h, u1, sumi1);+ sumi1 = ggml_dp4a(grid_l, u0, sumi1);+ sumi1 = ggml_dp4a(grid_h, u1, sumi1);}}const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4;@@ -213,8 +259,8 @@ static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);- sumi = __dp4a(grid_l, u0, sumi);- sumi = __dp4a(grid_h, u1, sumi);+ sumi = ggml_dp4a(grid_l, u0, sumi);+ sumi = ggml_dp4a(grid_h, u1, sumi);}const int ls = aux32 >> 28;@@ -255,8 +301,8 @@ static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0);const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);- sumi = __dp4a(grid_l, u0, sumi);- sumi = __dp4a(grid_h, u1, sumi);+ sumi = ggml_dp4a(grid_l, u0, sumi);+ sumi = ggml_dp4a(grid_h, u1, sumi);}sumi *= 1 + 2*((bq3->scales[iqs/4] >> ((iqs << 1) & 0x04)) & 0x0F);@@ -280,8 +326,8 @@ static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(const int aux_q4 = get_int_b2(bq4->qs, iqs + l);const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl);- sumi = __dp4a(v.x, q8[l + 0], sumi);- sumi = __dp4a(v.y, q8[l + 4], sumi);+ sumi = ggml_dp4a(v.x, q8[l + 0], sumi);+ sumi = ggml_dp4a(v.y, q8[l + 4], sumi);}const float d = __half2float(bq4->d) * __low2float(bq8_1->ds);@@ -304,8 +350,8 @@ static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(const int u0 = get_int_b4(bq8_1[iqs/4].qs, j + 0);const int u1 = get_int_b4(bq8_1[iqs/4].qs, j + 4);- sumi = __dp4a(v.x, u0, sumi);- sumi = __dp4a(v.y, u1, sumi);+ sumi = ggml_dp4a(v.x, u0, sumi);+ sumi = ggml_dp4a(v.y, u1, sumi);}const int ls = ((bq4->scales_l[iqs/8] >> (iqs & 0x04)) & 0x0F) | (((bq4->scales_h >> (iqs/2)) & 0x03) << 4);diff --git a/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k.cuh b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k.cuhindex 41c3579..0fe4c0f 100644--- a/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k.cuh+++ b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k.cuh@@ -1,7 +1,7 @@// Q5_K vec-dot (q8_1 activations, integer dp4a dots) ported from llama.cpp ggml/src/ggml-cuda/vecdotq.cuh// (https://github.com/ggml-org/llama.cpp, commit 7182ef7) and block_q5_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 -> __dp4a, namespace ggml_iq. The dot-product body is unchanged.+// Changes: ggml_cuda_dp4a -> ggml_dp4a, namespace ggml_iq. The dot-product body is unchanged.#pragma once#include "ggml_vecdotq_iq.cuh"@@ -41,8 +41,8 @@ static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(const int v0i = vl0i | vh0i;const int v1i = vl1i | vh1i;- const int dot1 = __dp4a(v0i, u[2*i+0], __dp4a(v1i, u[2*i+1], 0)); // SIMD dot product- const int dot2 = __dp4a(0x01010101, u[2*i+0], __dp4a(0x01010101, u[2*i+1], 0)); // sum of u+ const int dot1 = ggml_dp4a(v0i, u[2*i+0], ggml_dp4a(v1i, u[2*i+1], 0)); // SIMD dot product+ const int dot2 = ggml_dp4a(0x01010101, u[2*i+0], ggml_dp4a(0x01010101, u[2*i+1], 0)); // sum of usumf_d += d8[i] * (dot1 * sc[i]);sumf_m += d8[i] * (dot2 * m[i]);diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.pyindex 7db0fd1..2a3868c 100644--- a/exllamav3/modules/block_sparse_mlp.py+++ b/exllamav3/modules/block_sparse_mlp.py@@ -225,7 +225,9 @@ _FQ_MOE_IQ = os.environ.get("MCPM_FQ_MOE_IQ", "1") != "0"# BOLT_FQ_MMVQ (default 1, CUDA): IQ experts (all 7 IQ formats, gate/up and down) take the llama.cpp mmvq-style slot-parallel decode MoE# (fq_mmvq.cu fq_moe_mmvq_grouped) instead of fq_moe_kq.cu. 0 = the previous kernels (A/B)._MQ_ID = {"iq2_xxs": 0, "iq2_xs": 1, "iq2_s": 2, "iq3_xxs": 3, "iq3_s": 4, "iq4_nl": 5, "iq4_xs": 6}-_MQ_MOE = os.environ.get("BOLT_FQ_MMVQ", "1") != "0" and bool(torch.version.cuda) and not torch.version.hip and hasattr(ext, "fq_moe_mmvq_grouped")+# HIP: opt-in (BOLT_FQ_MMVQ_MOE=1) until measured: the Strix Halo IQ MoE path already beats llama.cpp ROCm (35B IQ3_XXS N4 1.13).+_MQ_MOE = (os.environ.get("BOLT_FQ_MMVQ", "1") != "0" and os.environ.get("BOLT_FQ_MMVQ_MOE", "0" if torch.version.hip else "1") != "0"+ and hasattr(ext, "fq_moe_mmvq_grouped"))# Rows up to which the slot-parallel IQ MoE kernel also serves the prefill-sized forwards (the grouped prefill path dequantizes every expert on every# call: 0.25 s per short prompt on the 35B, llama.cpp needs ~30 ms; the slot kernel re-reads one expert per (row, pick) and wins up to ~500 rows)._MQ_MOE_MAXR = int(os.environ.get("BOLT_FQ_MMVQ_MOE_MAXR", "256"))diff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.pyindex 10df8f9..bfba8c5 100644--- a/exllamav3/modules/foreign_linear.py+++ b/exllamav3/modules/foreign_linear.py@@ -69,9 +69,9 @@ _KQ_FORMATS = ("q4_k", "q5_k", "q6_k", "iq4_xs")# EXL3_IQ3_Q8 (default 1): IQ3_XXS decode rows run the q8_1 dp4a GEMV (fq_gemv_iq3_xxs_q8) instead of the Triton decode_IQ3_Q8 = os.environ.get("EXL3_IQ3_Q8", "1") != "0"_IQ_Q8_FN = {"iq3_xxs": "fq_gemv_iq3_xxs_q8", "iq2_s": "fq_gemv_iq2_s_q8", "iq3_s": "fq_gemv_iq3_s_q8"}-# BOLT_FQ_MMVQ (default 1, CUDA): IQ formats decode (1..16 rows) through the llama.cpp mmvq-style kernel (fq_mmvq.cu: ggml vec_dot_iq*_q8_1 dp4a dots,+# BOLT_FQ_MMVQ (default 1, CUDA and HIP): IQ formats decode (1..16 rows) through the llama.cpp mmvq-style kernel (fq_mmvq.cu: ggml vec_dot_iq*_q8_1 dp4a dots,# warp-per-row blocks, 1..8 activation columns per launch, weights read once). 0 = the previous paths (A/B).-_MMVQ_ON = os.environ.get("BOLT_FQ_MMVQ", "1") != "0" and bool(torch.version.cuda) and not torch.version.hip+_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"}_MMVQ_QK = {"iq4_nl": 32}