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.

GGUF IQ formats on CUDA, fused MoE shared expert on HIP, version 1.8.19 (#198)

Yaniss Amazouz30a49e11dc7d
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md
new file mode 100644
index 0000000..75adf98
--- /dev/null
+++ b/THIRD_PARTY_NOTICES.md
@@ -0,0 +1,34 @@
+# Third-party notices
+
+## llama.cpp / ggml (MIT License)
+
+Parts of exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ and foreign_quant/fq_mmvq.cu are derived from
+llama.cpp (https://github.com/ggml-org/llama.cpp, commit 7182ef7):
+
+- ggml_iq_tables.cuh: the IQ lookup tables (iq2xxs_grid, iq2xs_grid, iq2s_grid, iq3xxs_grid, iq3s_grid, kvalues_iq4nl) and block layouts
+ from ggml/src/ggml-common.h, copied unchanged.
+- 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).
+
+MIT License
+
+Copyright (c) 2023-2026 The ggml authors
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cpp
index 9515bab..4bb2f28 100644
--- a/exllamav3/exllamav3_ext/bindings.cpp
+++ b/exllamav3/exllamav3_ext/bindings.cpp
@@ -192,6 +192,10 @@ static void bind_exl3_dec(py::module_& m)
}
#endif
+#if !defined(USE_ROCM)
+#include "foreign_quant/fq_mmvq_api.h"
+#endif
+
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
{
#if !defined(USE_ROCM)
@@ -629,6 +633,29 @@ 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",
+ py::arg("qb"), py::arg("packed"), py::arg("out"));
+ m.def("fq_mmvq_iq2_s", &fq_mmvq_iq2_s, "fq_mmvq_iq2_s",
+ py::arg("qb"), py::arg("packed"), py::arg("out"));
+ m.def("fq_mmvq_iq3_xxs", &fq_mmvq_iq3_xxs, "fq_mmvq_iq3_xxs",
+ py::arg("qb"), py::arg("packed"), py::arg("out"));
+ m.def("fq_mmvq_iq3_s", &fq_mmvq_iq3_s, "fq_mmvq_iq3_s",
+ py::arg("qb"), py::arg("packed"), py::arg("out"));
+ m.def("fq_mmvq_iq4_nl", &fq_mmvq_iq4_nl, "fq_mmvq_iq4_nl",
+ py::arg("qb"), py::arg("packed"), py::arg("out"));
+ m.def("fq_mmvq_iq4_xs", &fq_mmvq_iq4_xs, "fq_mmvq_iq4_xs",
+ py::arg("qb"), py::arg("packed"), py::arg("out"));
+ m.def("fq_dequant_iq", &fq_dequant_iq, "fq_dequant_iq", py::arg("packed"), py::arg("out"), py::arg("fmt"));
+ m.def("fq_moe_mmvq_grouped", &fq_moe_mmvq_grouped, "fq_moe_mmvq_grouped",
+ py::arg("h"), py::arg("sel"), py::arg("wts"), py::arg("gate_ptrs"), py::arg("up_ptrs"), py::arg("down_ptrs"),
+ py::arg("gate_stride"), py::arg("up_stride"), py::arg("down_stride"), py::arg("type_ug"), py::arg("type_down"),
+ 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 too
m.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"),
@@ -656,5 +683,12 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
py::arg("gate_stride"), py::arg("up_stride"), py::arg("down_stride"),
py::arg("fmt_ug"), py::arg("fmt_down"),
py::arg("partial"), py::arg("out"));
+ m.def("fq_moe_kq_grouped_mixed_sh_q8", &fq_moe_kq_grouped_mixed_sh_q8, "fq_moe_kq_grouped_mixed_sh_q8",
+ py::arg("h"), py::arg("sel"), py::arg("wts"),
+ py::arg("gate_ptrs"), py::arg("up_ptrs"), py::arg("down_ptrs"),
+ py::arg("gate_stride"), py::arg("up_stride"), py::arg("down_stride"),
+ py::arg("fmt_ug"), py::arg("fmt_down"),
+ py::arg("partial"), py::arg("out"),
+ py::arg("sh_gate"), py::arg("sh_up"), py::arg("sh_down"), py::arg("sh_wg"));
#endif
}
diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_api.h b/exllamav3/exllamav3_ext/foreign_quant/fq_api.h
index 0ce6961..2792550 100644
--- a/exllamav3/exllamav3_ext/foreign_quant/fq_api.h
+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_api.h
@@ -81,3 +81,11 @@ void fq_moe_kq_grouped_mixed_q8(torch::Tensor h, torch::Tensor sel, torch::Tenso
int64_t gate_stride, int64_t up_stride, int64_t down_stride,
int64_t fmt_ug, int64_t fmt_down,
torch::Tensor partial, torch::Tensor out);
+void fq_moe_kq_grouped_mixed_sh_q8(torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
+ torch::Tensor gate_ptrs, torch::Tensor up_ptrs,
+ torch::Tensor down_ptrs,
+ int64_t gate_stride, int64_t up_stride, int64_t down_stride,
+ int64_t fmt_ug, int64_t fmt_down,
+ torch::Tensor partial, torch::Tensor out,
+ torch::Tensor sh_gate, torch::Tensor sh_up, torch::Tensor sh_down,
+ torch::Tensor sh_wg);
diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu
new file mode 100644
index 0000000..4c1cc38
--- /dev/null
+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq.cu
@@ -0,0 +1,557 @@
+// IQ-format decode GEMV, llama.cpp mmvq style (CUDA only): 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,
+// commit 7182ef7). MIT License, Copyright (c) 2023-2026 The ggml authors. See THIRD_PARTY_NOTICES.md.
+// Per block: nwarps warps of 32 lanes, RPB output rows; a lane takes (qi / vdr)-lane groups per weight block and walks the
+// row with stride blocks_per_iter; per-warp partials meet in shared memory, then a warp shuffle reduction.
+//
+// Output [M, N] row-major (half or float); qb = fq_quant_q8_1_rows output [M, K/32 * 36] bytes (block_q8_1: half d, half s, 32 x i8);
+// packed = raw GGUF bytes [N, K/QK * block_bytes].
+#include <cuda_fp16.h>
+#include <cstdint>
+#include <cstdlib>
+#include <algorithm>
+#include <type_traits>
+#include <map>
+#include <tuple>
+#include <torch/extension.h>
+#include <c10/cuda/CUDAGuard.h>
+#include <ATen/cuda/CUDAContext.h>
+#include "../util.h"
+#include "fq_api.h"
+#include "ggml_mmvq/ggml_vecdotq_iq.cuh"
+#include "ggml_mmvq/ggml_vecdotq_k.cuh"
+
+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 };
+
+// qk = weight elements per block, qi = ints per block (ggml QI*), vdr = ints per lane per call, bb = block bytes
+template <int T> struct MqTraits;
+#define MQ_TRAITS(TYPE, QK, QI, VDR, BB, FN) \
+ template <> struct MqTraits<TYPE> { \
+ static constexpr int qk = QK, qi = QI, vdr = VDR, bb = BB; \
+ static __device__ __forceinline__ float dot(const void* vbq, const block_q8_1* y, int kbx, int iqs) \
+ { return FN(vbq, y, kbx, iqs); } \
+ }
+MQ_TRAITS(MQ_IQ2_XXS, 256, 16, VDR_IQ2_XXS_Q8_1_MMVQ, 66, vec_dot_iq2_xxs_q8_1);
+MQ_TRAITS(MQ_IQ2_XS, 256, 16, VDR_IQ2_XS_Q8_1_MMVQ, 74, vec_dot_iq2_xs_q8_1);
+MQ_TRAITS(MQ_IQ2_S, 256, 16, VDR_IQ2_S_Q8_1_MMVQ, 82, vec_dot_iq2_s_q8_1);
+MQ_TRAITS(MQ_IQ3_XXS, 256, 16, VDR_IQ3_XXS_Q8_1_MMVQ, 98, vec_dot_iq3_xxs_q8_1);
+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);
+#undef MQ_TRAITS
+
+constexpr int MQ_WARP = 32;
+constexpr int MQ_MAX_COLS = 8;
+
+// llama.cpp calc_nwarps / calc_rows_per_block (generic table, Ampere): 1..4 columns 4 warps, 5..8 columns 2 warps; 2 rows per block when batched
+constexpr int mq_nwarps(int ncols) { return ncols <= 4 ? 4 : 2; }
+constexpr int mq_rows(int ncols) { return ncols == 1 ? 1 : 2; }
+
+__device__ __forceinline__ float mq_warp_sum(float x)
+{
+#pragma unroll
+ for (int o = MQ_WARP / 2; o > 0; o >>= 1)
+ x += __shfl_xor_sync(0xffffffffu, x, o, MQ_WARP);
+ return x;
+}
+
+template <typename OUT> __device__ __forceinline__ void mq_store(OUT* p, float v);
+template <> __device__ __forceinline__ void mq_store<float>(float* p, float v) { *p = v; }
+template <> __device__ __forceinline__ void mq_store<__half>(__half* p, float v) { *p = __float2half_rn(v); }
+
+// One launch: out[j][row] = dot(weights row, activation column j), j < NC. Weight blocks of row r start at vx + r * row_bytes.
+template <int T, int NC, typename OUT>
+__global__ void __launch_bounds__(MQ_WARP * mq_nwarps(NC), 1)
+fq_mmvq_kernel(const uint8_t* __restrict__ vx, const block_q8_1* __restrict__ vy, OUT* __restrict__ dst,
+ int ncols_x, int nrows, int64_t row_blocks, int64_t y_col_blocks, int64_t dst_col_stride)
+{
+ using Tr = MqTraits<T>;
+ constexpr int NW = mq_nwarps(NC);
+ constexpr int RPB = mq_rows(NC);
+ constexpr int blocks_per_iter = Tr::vdr * NW * MQ_WARP / Tr::qi;
+
+ const int tid = MQ_WARP * threadIdx.y + threadIdx.x;
+ const int row0 = RPB * blockIdx.x;
+ const int blocks_per_row_x = ncols_x / Tr::qk;
+
+ float tmp[NC][RPB];
+#pragma unroll
+ for (int j = 0; j < NC; ++j)
+#pragma unroll
+ for (int i = 0; i < RPB; ++i)
+ tmp[j][i] = 0.0f;
+
+ // rows past the end read the last valid row (their result is not stored)
+ int64_t rbase[RPB];
+#pragma unroll
+ for (int i = 0; i < RPB; ++i)
+ rbase[i] = (int64_t)min(row0 + i, nrows - 1) * row_blocks;
+
+ for (int kbx = tid / (Tr::qi / Tr::vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter)
+ {
+ const int kby = kbx * (Tr::qk / 32);
+ const int kqs = Tr::vdr * (tid % (Tr::qi / Tr::vdr));
+#pragma unroll
+ for (int j = 0; j < NC; ++j)
+#pragma unroll
+ for (int i = 0; i < RPB; ++i)
+ tmp[j][i] += Tr::dot(vx + rbase[i] * Tr::bb, &vy[j * y_col_blocks + kby], kbx, kqs);
+ }
+
+ __shared__ float tmp_shared[NW > 1 ? NW - 1 : 1][NC][RPB][MQ_WARP];
+ if constexpr (NW > 1)
+ {
+ if (threadIdx.y > 0)
+ {
+#pragma unroll
+ for (int j = 0; j < NC; ++j)
+#pragma unroll
+ for (int i = 0; i < RPB; ++i)
+ tmp_shared[threadIdx.y - 1][j][i][threadIdx.x] = tmp[j][i];
+ }
+ __syncthreads();
+ if (threadIdx.y > 0)
+ return;
+ }
+#pragma unroll
+ for (int j = 0; j < NC; ++j)
+ {
+#pragma unroll
+ for (int i = 0; i < RPB; ++i)
+ {
+ float v = tmp[j][i];
+ if constexpr (NW > 1)
+ {
+#pragma unroll
+ for (int l = 0; l < NW - 1; ++l)
+ v += tmp_shared[l][j][i][threadIdx.x];
+ }
+ v = mq_warp_sum(v);
+ if (threadIdx.x == 0 && row0 + i < nrows)
+ mq_store<OUT>(&dst[(int64_t)j * dst_col_stride + row0 + i], v);
+ }
+ }
+}
+
+template <int T, int NC, typename OUT>
+void mq_launch(const uint8_t* w, const uint8_t* qb, OUT* out, int K, int N, int64_t y_col_blocks, int64_t dst_col_stride, cudaStream_t stream)
+{
+ using Tr = MqTraits<T>;
+ constexpr int NW = mq_nwarps(NC);
+ constexpr int RPB = mq_rows(NC);
+ dim3 grid((N + RPB - 1) / RPB, 1, 1), block(MQ_WARP, NW, 1);
+ fq_mmvq_kernel<T, NC, OUT><<<grid, block, 0, stream>>>(w, reinterpret_cast<const block_q8_1*>(qb), out, K, N,
+ (int64_t)(K / Tr::qk), y_col_blocks, dst_col_stride);
+}
+
+template <int T, typename OUT>
+void mq_cols(int nc, const uint8_t* w, const uint8_t* qb, OUT* out, int K, int N, int64_t y_col_blocks, cudaStream_t stream)
+{
+ switch (nc)
+ {
+ case 1: mq_launch<T, 1, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ case 2: mq_launch<T, 2, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ case 3: mq_launch<T, 3, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ case 4: mq_launch<T, 4, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ case 5: mq_launch<T, 5, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ case 6: mq_launch<T, 6, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ case 7: mq_launch<T, 7, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ default: mq_launch<T, 8, OUT>(w, qb, out, K, N, y_col_blocks, N, stream); break;
+ }
+}
+
+template <int T>
+void mq_run(torch::Tensor qb, torch::Tensor packed, torch::Tensor out)
+{
+ using Tr = MqTraits<T>;
+ TORCH_CHECK(qb.is_cuda() && packed.is_cuda() && out.is_cuda(), "tensors must be CUDA");
+ TORCH_CHECK(qb.is_contiguous() && packed.is_contiguous() && out.is_contiguous(), "tensors must be contiguous");
+ TORCH_CHECK(out.dtype() == torch::kHalf || out.dtype() == torch::kFloat, "out must be half or float");
+ TORCH_CHECK_DIM(qb, 2);
+ TORCH_CHECK_DIM(packed, 2);
+ TORCH_CHECK_DIM(out, 2);
+ const int M = (int)qb.size(0);
+ const int N = (int)packed.size(0);
+ const int K = (int)(qb.size(1) / 36 * 32);
+ TORCH_CHECK((int)out.size(0) == M && (int)out.size(1) == N, "out must be [M, N]");
+ TORCH_CHECK(K % Tr::qk == 0, "K must be a multiple of the format block size");
+ TORCH_CHECK(packed.size(1) == (int64_t)(K / Tr::qk) * Tr::bb, "packed width must match K");
+ TORCH_CHECK((uintptr_t)packed.data_ptr() % 4 == 0 && ((int64_t)(K / Tr::qk) * Tr::bb) % 2 == 0, "weights must be 2-byte aligned (4 for IQ4_XS)");
+ TORCH_CHECK((uintptr_t)qb.data_ptr() % 4 == 0, "qb must be 4-byte aligned");
+ at::cuda::CUDAGuard guard(qb.device());
+ auto stream = at::cuda::getCurrentCUDAStream().stream();
+ const int64_t y_col_blocks = K / 32;
+ const uint8_t* w = packed.data_ptr<uint8_t>();
+ const uint8_t* q = qb.data_ptr<uint8_t>();
+ for (int m0 = 0; m0 < M; m0 += MQ_MAX_COLS)
+ {
+ const int nc = std::min(MQ_MAX_COLS, M - m0);
+ const uint8_t* qm = q + (int64_t)m0 * y_col_blocks * 36;
+ if (out.dtype() == torch::kHalf)
+ mq_cols<T, __half>(nc, w, qm, reinterpret_cast<__half*>(out.data_ptr<at::Half>()) + (int64_t)m0 * N, K, N, y_col_blocks, stream);
+ else
+ mq_cols<T, float>(nc, w, qm, out.data_ptr<float>() + (int64_t)m0 * N, K, N, y_col_blocks, stream);
+ }
+}
+
+
+// ---------------------------------------------------------------------------------------------------------------------------------
+// Grouped decode MoE (slot-parallel): assignment s = row * topk + j -> expert sel[s]. Up+gate kernel: act[s, n] = silu(gate . h) * (up . h)
+// for the q8_1 activation row s / topk; down kernel: partial[s, n] = down[sel[s]] row n . q8_1(act row s). One warp = 32 / LPR rows
+// side by side (LPR lanes per row; 16 when a row has only 2 weight blocks, e.g. the 512-wide down projection), each warp 2 such row
+// groups in sequence, 4 warps per block. Same dots and the same half-rounded SwiGLU epilogue as fq_moe_kq.cu.
+// ---------------------------------------------------------------------------------------------------------------------------------
+constexpr int MQM_WARPS = 4;
+constexpr int MQM_ITER = 2;
+
+template <int LPR> __device__ __forceinline__ float mq_group_sum(float x)
+{
+#pragma unroll
+ for (int o = LPR / 2; o > 0; o >>= 1)
+ x += __shfl_xor_sync(0xffffffffu, x, o, MQ_WARP);
+ return x;
+}
+
+template <int T, int LPR>
+__global__ void __launch_bounds__(MQ_WARP * MQM_WARPS)
+fq_mmvq_moe_upgate_kernel(const uint8_t* __restrict__ qb_h, const int64_t* __restrict__ sel,
+ const uint8_t* const* __restrict__ gate_bases, const uint8_t* const* __restrict__ up_bases,
+ int64_t gate_stride, int64_t up_stride, int64_t qb_stride, __half* __restrict__ act,
+ int I, int K, int E, int topk)
+{
+ using Tr = MqTraits<T>;
+ constexpr int GPW = MQ_WARP / LPR; // row groups per warp
+ constexpr int ROWS_BLOCK = MQM_WARPS * MQM_ITER * GPW;
+ const int s = blockIdx.y;
+ int64_t e = sel[s];
+ e = (e < 0 || e >= E) ? 0 : e;
+ const int lane = threadIdx.x, warp = threadIdx.y;
+ const int grp = lane / LPR, ll = lane % LPR;
+ const block_q8_1* y = reinterpret_cast<const block_q8_1*>(qb_h + (int64_t)(s / topk) * qb_stride);
+ const int nb = K / Tr::qk;
+ constexpr int per_iter = LPR * Tr::vdr / Tr::qi;
+ const uint8_t* gb = gate_bases[e];
+ const uint8_t* ub = up_bases[e];
+#pragma unroll
+ for (int it = 0; it < MQM_ITER; ++it)
+ {
+ const int n = blockIdx.x * ROWS_BLOCK + (warp * MQM_ITER + it) * GPW + grp;
+ const int nr = min(n, I - 1);
+ const uint8_t* gr = gb + (int64_t)nr * gate_stride;
+ const uint8_t* ur = ub + (int64_t)nr * up_stride;
+ float g = 0.0f, u = 0.0f;
+ for (int kbx = ll / (Tr::qi / Tr::vdr); kbx < nb; kbx += per_iter)
+ {
+ const int kqs = Tr::vdr * (ll % (Tr::qi / Tr::vdr));
+ const block_q8_1* yb = y + kbx * (Tr::qk / 32);
+ g += Tr::dot(gr, yb, kbx, kqs);
+ u += Tr::dot(ur, yb, kbx, kqs);
+ }
+ g = mq_group_sum<LPR>(g);
+ u = mq_group_sum<LPR>(u);
+ if (ll == 0 && n < I)
+ {
+ const float gh = __half2float(__float2half_rn(g));
+ const float uh = __half2float(__float2half_rn(u));
+ const float sig = 1.0f / (1.0f + expf(-gh));
+ act[(int64_t)s * I + n] = __float2half_rn(sig * gh * uh);
+ }
+ }
+}
+
+template <int T, int LPR>
+__global__ void __launch_bounds__(MQ_WARP * MQM_WARPS)
+fq_mmvq_moe_down_kernel(const uint8_t* __restrict__ qb_a, int64_t qb_stride, const int64_t* __restrict__ sel,
+ const uint8_t* const* __restrict__ down_bases, int64_t down_stride, float* __restrict__ partial,
+ int I, int K, int E)
+{
+ using Tr = MqTraits<T>;
+ constexpr int GPW = MQ_WARP / LPR;
+ constexpr int ROWS_BLOCK = MQM_WARPS * MQM_ITER * GPW;
+ const int s = blockIdx.y;
+ int64_t e = sel[s];
+ e = (e < 0 || e >= E) ? 0 : e;
+ const int lane = threadIdx.x, warp = threadIdx.y;
+ const int grp = lane / LPR, ll = lane % LPR;
+ const block_q8_1* y = reinterpret_cast<const block_q8_1*>(qb_a + (int64_t)s * qb_stride);
+ const int nb = I / Tr::qk;
+ constexpr int per_iter = LPR * Tr::vdr / Tr::qi;
+ const uint8_t* db = down_bases[e];
+#pragma unroll
+ for (int it = 0; it < MQM_ITER; ++it)
+ {
+ const int n = blockIdx.x * ROWS_BLOCK + (warp * MQM_ITER + it) * GPW + grp;
+ const uint8_t* dr = db + (int64_t)min(n, K - 1) * down_stride;
+ float p = 0.0f;
+ for (int kbx = ll / (Tr::qi / Tr::vdr); kbx < nb; kbx += per_iter)
+ {
+ const int kqs = Tr::vdr * (ll % (Tr::qi / Tr::vdr));
+ p += Tr::dot(dr, y + kbx * (Tr::qk / 32), kbx, kqs);
+ }
+ p = mq_group_sum<LPR>(p);
+ if (ll == 0 && n < K)
+ partial[(int64_t)s * K + n] = p;
+ }
+}
+
+template <int T>
+void mq_moe_upgate_t(const uint8_t* qb_h, const int64_t* sel, const uint8_t* const* gb, const uint8_t* const* ub, int64_t gs, int64_t us,
+ int64_t qs, __half* act, int S, int I, int K, int E, int topk, cudaStream_t st)
+{
+ using Tr = MqTraits<T>;
+ const int lanes = (K / Tr::qk) * (Tr::qi / Tr::vdr);
+ auto go = [&](auto lpr_c)
+ {
+ constexpr int LPR = decltype(lpr_c)::value;
+ constexpr int RB = MQM_WARPS * MQM_ITER * (MQ_WARP / LPR);
+ dim3 grid((I + RB - 1) / RB, S, 1), block(MQ_WARP, MQM_WARPS, 1);
+ fq_mmvq_moe_upgate_kernel<T, LPR><<<grid, block, 0, st>>>(qb_h, sel, gb, ub, gs, us, qs, act, I, K, E, topk);
+ };
+ if (lanes >= 32) go(std::integral_constant<int, 32>());
+ else if (lanes >= 16) go(std::integral_constant<int, 16>());
+ else go(std::integral_constant<int, 8>());
+}
+
+template <int T>
+void mq_moe_down_t(const uint8_t* qb_a, int64_t qs, const int64_t* sel, const uint8_t* const* db, int64_t ds, float* partial, int S, int I, int K,
+ int E, cudaStream_t st)
+{
+ using Tr = MqTraits<T>;
+ const int lanes = (I / Tr::qk) * (Tr::qi / Tr::vdr);
+ auto go = [&](auto lpr_c)
+ {
+ constexpr int LPR = decltype(lpr_c)::value;
+ constexpr int RB = MQM_WARPS * MQM_ITER * (MQ_WARP / LPR);
+ dim3 grid((K + RB - 1) / RB, S, 1), block(MQ_WARP, MQM_WARPS, 1);
+ fq_mmvq_moe_down_kernel<T, LPR><<<grid, block, 0, st>>>(qb_a, qs, sel, db, ds, partial, I, K, E);
+ };
+ if (lanes >= 32) go(std::integral_constant<int, 32>());
+ else if (lanes >= 16) go(std::integral_constant<int, 16>());
+ else go(std::integral_constant<int, 8>());
+}
+
+
+// Weighted fold: out[r, k] = sum_j w[r, j] * partial[r * topk + j, k] (same order and rounding as fq_moe_kq_combine_kernel)
+__global__ void fq_mmvq_moe_combine_kernel(const float* __restrict__ partial, const __half* __restrict__ wts, float* __restrict__ out, int S, int K)
+{
+ partial += (int64_t)blockIdx.y * S * K;
+ wts += (int64_t)blockIdx.y * S;
+ out += (int64_t)blockIdx.y * K;
+ for (int k = blockIdx.x * blockDim.x + threadIdx.x; k < K; k += gridDim.x * blockDim.x)
+ {
+ float acc = 0.0f;
+ for (int s = 0; s < S; s++)
+ acc = __fadd_rn(acc, __fmul_rn(partial[(int64_t)s * K + k], __half2float(wts[s])));
+ out[k] = acc;
+ }
+}
+
+
+// ---------------------------------------------------------------------------------------------------------------------------------
+// Dequantize to fp16 [N, K] (prefill / large-batch path: dequant + GEMM). Port of ggml/src/ggml-cuda/convert.cu dequantize_block_iq*
+// (MIT, The ggml authors): one 32-thread block per 256-element super block, 8 outputs per thread (4 + 4 for the 4-bit formats).
+// ---------------------------------------------------------------------------------------------------------------------------------
+template <int T> __global__ void fq_deq_iq_kernel(const uint8_t* __restrict__ packed, __half* __restrict__ out, int64_t row_bytes, int64_t K, int nb)
+{
+ const int blk = blockIdx.x; // block id in [0, N * nb)
+ const int64_t row = blk / nb, bi = blk % nb;
+ const uint8_t* base = packed + row * row_bytes;
+ __half* y0 = out + row * K + bi * 256;
+ const int tid = threadIdx.x;
+ const int il = tid / 8, ib = tid % 8;
+ if constexpr (T == MQ_IQ2_XXS)
+ {
+ const block_iq2_xxs* x = (const block_iq2_xxs*)base + bi;
+ __half* y = y0 + 32 * ib + 8 * il;
+ const uint16_t* q2 = x->qs + 4 * ib;
+ const uint8_t* aux8 = (const uint8_t*)q2;
+ const uint8_t* grid = (const uint8_t*)(iq2xxs_grid + aux8[il]);
+ const uint32_t aux32 = q2[2] | (q2[3] << 16);
+ const float d = __half2float(x->d) * (0.5f + (aux32 >> 28)) * 0.25f;
+ const uint8_t signs = ksigns_iq2xs[(aux32 >> 7 * il) & 127];
+ for (int j = 0; j < 8; ++j) y[j] = __float2half_rn(d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f));
+ }
+ else if constexpr (T == MQ_IQ2_XS)
+ {
+ const block_iq2_xs* x = (const block_iq2_xs*)base + bi;
+ __half* y = y0 + 32 * ib + 8 * il;
+ const uint16_t* q2 = x->qs + 4 * ib;
+ const uint8_t* grid = (const uint8_t*)(iq2xs_grid + (q2[il] & 511));
+ const float d = __half2float(x->d) * (0.5f + ((x->scales[ib] >> 4 * (il / 2)) & 0xf)) * 0.25f;
+ const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
+ for (int j = 0; j < 8; ++j) y[j] = __float2half_rn(d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f));
+ }
+ else if constexpr (T == MQ_IQ2_S)
+ {
+ const block_iq2_s* x = (const block_iq2_s*)base + bi;
+ __half* y = y0 + 32 * ib + 8 * il;
+ const uint8_t* grid = (const uint8_t*)(iq2s_grid + (x->qs[4 * ib + il] | ((x->qh[ib] << (8 - 2 * il)) & 0x300)));
+ const float d = __half2float(x->d) * (0.5f + ((x->scales[ib] >> 4 * (il / 2)) & 0xf)) * 0.25f;
+ const uint8_t signs = x->qs[QK_K / 8 + 4 * ib + il];
+ for (int j = 0; j < 8; ++j) y[j] = __float2half_rn(d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f));
+ }
+ else if constexpr (T == MQ_IQ3_XXS)
+ {
+ const block_iq3_xxs* x = (const block_iq3_xxs*)base + bi;
+ __half* y = y0 + 32 * ib + 8 * il;
+ const uint8_t* q3 = x->qs + 8 * ib;
+ const uint16_t* gas = (const uint16_t*)(x->qs + QK_K / 4) + 2 * ib;
+ const uint8_t* grid1 = (const uint8_t*)(iq3xxs_grid + q3[2 * il + 0]);
+ const uint8_t* grid2 = (const uint8_t*)(iq3xxs_grid + q3[2 * il + 1]);
+ const uint32_t aux32 = gas[0] | (gas[1] << 16);
+ const float d = __half2float(x->d) * (0.5f + (aux32 >> 28)) * 0.5f;
+ const uint8_t signs = ksigns_iq2xs[(aux32 >> 7 * il) & 127];
+ for (int j = 0; j < 4; ++j)
+ {
+ y[j + 0] = __float2half_rn(d * grid1[j] * (signs & kmask_iq2xs[j + 0] ? -1.f : 1.f));
+ y[j + 4] = __float2half_rn(d * grid2[j] * (signs & kmask_iq2xs[j + 4] ? -1.f : 1.f));
+ }
+ }
+ else if constexpr (T == MQ_IQ3_S)
+ {
+ const block_iq3_s* x = (const block_iq3_s*)base + bi;
+ __half* y = y0 + 32 * ib + 8 * il;
+ const uint8_t* qs = x->qs + 8 * ib;
+ const uint8_t* grid1 = (const uint8_t*)(iq3s_grid + (qs[2 * il + 0] | ((x->qh[ib] << (8 - 2 * il)) & 256)));
+ const uint8_t* grid2 = (const uint8_t*)(iq3s_grid + (qs[2 * il + 1] | ((x->qh[ib] << (7 - 2 * il)) & 256)));
+ const float d = __half2float(x->d) * (1 + 2 * ((x->scales[ib / 2] >> 4 * (ib % 2)) & 0xf));
+ const uint8_t signs = x->signs[4 * ib + il];
+ for (int j = 0; j < 4; ++j)
+ {
+ y[j + 0] = __float2half_rn(d * grid1[j] * (signs & kmask_iq2xs[j + 0] ? -1.f : 1.f));
+ y[j + 4] = __float2half_rn(d * grid2[j] * (signs & kmask_iq2xs[j + 4] ? -1.f : 1.f));
+ }
+ }
+ else if constexpr (T == MQ_IQ4_NL)
+ {
+ const block_iq4_nl* x = (const block_iq4_nl*)base + bi * (QK_K / QK4_NL);
+ __half* y = y0 + 32 * ib + 4 * il;
+ const uint8_t* q4 = x[ib].qs + 4 * il;
+ const float d = __half2float(x[ib].d);
+ for (int j = 0; j < 4; ++j)
+ {
+ y[j + 0] = __float2half_rn(d * kvalues_iq4nl[q4[j] & 0xf]);
+ y[j + 16] = __float2half_rn(d * kvalues_iq4nl[q4[j] >> 4]);
+ }
+ }
+ else // MQ_IQ4_XS
+ {
+ const block_iq4_xs* x = (const block_iq4_xs*)base + bi;
+ __half* y = y0 + 32 * ib + 4 * il;
+ const uint8_t* q4 = x->qs + 16 * ib + 4 * il;
+ const float d = __half2float(x->d) * ((((x->scales_l[ib / 2] >> 4 * (ib % 2)) & 0xf) | (((x->scales_h >> 2 * ib) & 3) << 4)) - 32);
+ for (int j = 0; j < 4; ++j)
+ {
+ y[j + 0] = __float2half_rn(d * kvalues_iq4nl[q4[j] & 0xf]);
+ y[j + 16] = __float2half_rn(d * kvalues_iq4nl[q4[j] >> 4]);
+ }
+ }
+}
+
+template <int T> void mq_dequant_t(torch::Tensor packed, torch::Tensor out)
+{
+ using Tr = MqTraits<T>;
+ TORCH_CHECK(packed.is_cuda() && out.is_cuda() && packed.is_contiguous() && out.is_contiguous(), "tensors must be contiguous CUDA");
+ TORCH_CHECK(out.dtype() == torch::kHalf, "out must be half");
+ TORCH_CHECK_DIM(packed, 2);
+ TORCH_CHECK_DIM(out, 2);
+ const int64_t N = packed.size(0), K = out.size(1);
+ TORCH_CHECK(out.size(0) == N && K % 256 == 0 && packed.size(1) == (K / Tr::qk) * Tr::bb, "shape mismatch");
+ TORCH_CHECK((uintptr_t)packed.data_ptr() % 4 == 0, "weights must be 4-byte aligned");
+ at::cuda::CUDAGuard guard(packed.device());
+ auto stream = at::cuda::getCurrentCUDAStream().stream();
+ const int nb = (int)(K / 256);
+ fq_deq_iq_kernel<T><<<(unsigned)(N * nb), 32, 0, stream>>>(packed.data_ptr<uint8_t>(), reinterpret_cast<__half*>(out.data_ptr<at::Half>()),
+ packed.size(1), K, nb);
+}
+
+} // namespace
+
+void fq_mmvq_iq2_xxs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_IQ2_XXS>(qb, packed, out); }
+void fq_mmvq_iq2_xs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_IQ2_XS>(qb, packed, out); }
+void fq_mmvq_iq2_s(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_IQ2_S>(qb, packed, out); }
+void fq_mmvq_iq3_xxs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_IQ3_XXS>(qb, packed, out); }
+void fq_mmvq_iq3_s(torch::Tensor qb, torch::Tensor packed, torch::Tensor out) { mq_run<MQ_IQ3_S>(qb, packed, 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); }
+
+// 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:
+// h [R, K] half, sel [R * topk] int64, wts [R * topk] half, per-expert base pointers (E int64), strides = bytes per row, partial [R * topk, K] float,
+// out [R, K] float.
+void fq_moe_mmvq_grouped(torch::Tensor h, torch::Tensor sel, torch::Tensor wts, torch::Tensor gate_ptrs, torch::Tensor up_ptrs,
+ torch::Tensor down_ptrs, int64_t gate_stride, int64_t up_stride, int64_t down_stride, int64_t type_ug, int64_t type_down,
+ torch::Tensor partial, torch::Tensor out)
+{
+ static const int kQk[MQ_NTYPES] = {256, 256, 256, 256, 256, 32, 256, 256};
+ static const int kBb[MQ_NTYPES] = {66, 74, 82, 98, 110, 18, 136, 176};
+ TORCH_CHECK(type_ug >= 0 && type_ug < MQ_NTYPES && type_down >= 0 && type_down < MQ_NTYPES, "fq_moe_mmvq_grouped: bad format id");
+ const int K = (int)h.size(1), R = (int)h.size(0), S = (int)sel.numel();
+ TORCH_CHECK(S % R == 0 && R >= 1, "sel must be R x topk");
+ TORCH_CHECK(h.dtype() == torch::kHalf && h.is_cuda() && h.is_contiguous(), "h must be contiguous half CUDA");
+ const int topk = S / R;
+ const int E = (int)up_ptrs.numel();
+ const int I = (int)(down_stride / kBb[type_down]) * kQk[type_down];
+ TORCH_CHECK(K % kQk[type_ug] == 0 && I % kQk[type_down] == 0 && I % 32 == 0, "K / I must match the format block sizes");
+ at::cuda::CUDAGuard guard(h.device());
+ auto stream = at::cuda::getCurrentCUDAStream().stream();
+ torch::Tensor qb_h = fq_q8_scratch(h.get_device(), R, K);
+ fq_quant_q8_1_rows(h, qb_h);
+ static thread_local std::map<std::tuple<int, int, int>, torch::Tensor> act_cache;
+ auto key = std::make_tuple((int)h.get_device(), S, I);
+ auto it = act_cache.find(key);
+ torch::Tensor act;
+ if (it != act_cache.end())
+ act = it->second;
+ else
+ {
+ act = torch::empty({S, I}, torch::dtype(torch::kHalf).device(h.device()));
+ act_cache[key] = act;
+ }
+ const int64_t qb_stride_h = (int64_t)(K / 32) * 36;
+ const uint8_t* const* gb = reinterpret_cast<const uint8_t* const*>(gate_ptrs.data_ptr<int64_t>());
+ const uint8_t* const* ub = reinterpret_cast<const uint8_t* const*>(up_ptrs.data_ptr<int64_t>());
+ const uint8_t* const* db = reinterpret_cast<const uint8_t* const*>(down_ptrs.data_ptr<int64_t>());
+ __half* actp = reinterpret_cast<__half*>(act.data_ptr<at::Half>());
+ switch (type_ug)
+ {
+#define MQ_UG(T) case T: mq_moe_upgate_t<T>(qb_h.data_ptr<uint8_t>(), sel.data_ptr<int64_t>(), gb, ub, gate_stride, up_stride, qb_stride_h, actp, S, I, K, E, topk, stream); break;
+ MQ_UG(MQ_IQ2_XXS) MQ_UG(MQ_IQ2_XS) MQ_UG(MQ_IQ2_S) MQ_UG(MQ_IQ3_XXS) MQ_UG(MQ_IQ3_S) MQ_UG(MQ_IQ4_NL) MQ_UG(MQ_IQ4_XS) MQ_UG(MQ_Q5_K)
+#undef MQ_UG
+ }
+ torch::Tensor qb_act = fq_q8_scratch(h.get_device(), S, I);
+ fq_quant_q8_1_rows(act, qb_act);
+ const int64_t qb_stride_a = (int64_t)(I / 32) * 36;
+ switch (type_down)
+ {
+#define MQ_DN(T) case T: mq_moe_down_t<T>(qb_act.data_ptr<uint8_t>(), qb_stride_a, sel.data_ptr<int64_t>(), db, down_stride, partial.data_ptr<float>(), S, I, K, E, stream); break;
+ MQ_DN(MQ_IQ2_XXS) MQ_DN(MQ_IQ2_XS) MQ_DN(MQ_IQ2_S) MQ_DN(MQ_IQ3_XXS) MQ_DN(MQ_IQ3_S) MQ_DN(MQ_IQ4_NL) MQ_DN(MQ_IQ4_XS) MQ_DN(MQ_Q5_K)
+#undef MQ_DN
+ }
+ fq_mmvq_moe_combine_kernel<<<dim3((K + 255) / 256, R), 256, 0, stream>>>(partial.data_ptr<float>(), reinterpret_cast<const __half*>(wts.data_ptr<at::Half>()),
+ out.data_ptr<float>(), topk, K);
+}
+
+// out [N, K] half from raw GGUF bytes; fmt = MQ type id (0 iq2_xxs, 1 iq2_xs, 2 iq2_s, 3 iq3_xxs, 4 iq3_s, 5 iq4_nl, 6 iq4_xs), K % 256 == 0
+void fq_dequant_iq(torch::Tensor packed, torch::Tensor out, int64_t fmt)
+{
+ switch (fmt)
+ {
+ case MQ_IQ2_XXS: mq_dequant_t<MQ_IQ2_XXS>(packed, out); break;
+ case MQ_IQ2_XS: mq_dequant_t<MQ_IQ2_XS>(packed, out); break;
+ case MQ_IQ2_S: mq_dequant_t<MQ_IQ2_S>(packed, out); break;
+ case MQ_IQ3_XXS: mq_dequant_t<MQ_IQ3_XXS>(packed, out); break;
+ case MQ_IQ3_S: mq_dequant_t<MQ_IQ3_S>(packed, out); break;
+ case MQ_IQ4_NL: mq_dequant_t<MQ_IQ4_NL>(packed, out); break;
+ case MQ_IQ4_XS: mq_dequant_t<MQ_IQ4_XS>(packed, out); break;
+ default: TORCH_CHECK(false, "fq_dequant_iq: bad format id ", fmt);
+ }
+}
diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h
new file mode 100644
index 0000000..3aba416
--- /dev/null
+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_mmvq_api.h
@@ -0,0 +1,20 @@
+// 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),
+// 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>
+
+void fq_mmvq_iq2_xxs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);
+void fq_mmvq_iq2_xs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);
+void fq_mmvq_iq2_s(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);
+void fq_mmvq_iq3_xxs(torch::Tensor qb, torch::Tensor packed, torch::Tensor out);
+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);
+// 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,
+ torch::Tensor down_ptrs, int64_t gate_stride, int64_t up_stride, int64_t down_stride, int64_t type_ug, int64_t type_down,
+ torch::Tensor partial, torch::Tensor out);
+// Dequantize IQ blocks to fp16 [N, K] (K % 256 == 0); fmt = MQ type id as above (0..6). Port of ggml convert.cu dequantize_block_iq*.
+void fq_dequant_iq(torch::Tensor packed, torch::Tensor out, int64_t fmt);
diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu b/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu
index 450748f..2436d0d 100644
--- a/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu
+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu
@@ -33,26 +33,35 @@ __global__ void fq_moe_kq_upgate_kernel(const uint8_t* __restrict__ aqb,
int64_t up_stride,
int64_t qb_stride,
__half* __restrict__ act,
- int S, int I, int K, int E, int ntiles_u, int topk)
+ int S, int I, int K, int E, int ntiles_u, int topk,
+ const uint8_t* __restrict__ sh_gate = nullptr,
+ const uint8_t* __restrict__ sh_up = nullptr,
+ int64_t sh_stride = 0)
{
int b = blockIdx.x;
int s = b / ntiles_u;
int t = b % ntiles_u;
int grp = threadIdx.x / FQ_GEMV_LANES;
int lane = threadIdx.x % FQ_GEMV_LANES;
- int64_t e = sel[s];
+ const bool shs = s >= S; // shared-expert slot of row s - S (sh_gate set): Q8_0 gate / up
+ int64_t e = shs ? 0 : sel[s];
e = (e < 0 || e >= E) ? 0 : e;
int n = t * FQ_GEMV_OUT_PER_BLOCK + grp;
- const uint8_t* aq = aqb + (int64_t)(s / topk) * qb_stride; // assignment s = row * topk + j reads h row s / topk
+ const uint8_t* aq = aqb + (int64_t)(shs ? s - S : s / topk) * qb_stride; // assignment s = row * topk + j reads h row s / topk
extern __shared__ float red[];
float g = 0.0f, u = 0.0f;
if (n < I)
{
- const uint8_t* gr = gate_bases[e] + (int64_t)n * gate_stride;
- const uint8_t* ur = up_bases[e] + (int64_t)n * up_stride;
- if constexpr (FMT == 0)
+ const uint8_t* gr = shs ? sh_gate + (int64_t)n * sh_stride : gate_bases[e] + (int64_t)n * gate_stride;
+ const uint8_t* ur = shs ? sh_up + (int64_t)n * sh_stride : up_bases[e] + (int64_t)n * up_stride;
+ if (shs)
+ {
+ g = fq_dot_q8_0_q8(gr, aq, K, lane, FQ_GEMV_LANES);
+ u = fq_dot_q8_0_q8(ur, aq, K, lane, FQ_GEMV_LANES);
+ }
+ else if constexpr (FMT == 0)
{
g = fq_dot_q4_k_q8(gr, aq, K, lane, FQ_GEMV_LANES);
u = fq_dot_q4_k_q8(ur, aq, K, lane, FQ_GEMV_LANES);
@@ -119,19 +128,28 @@ __global__ void __launch_bounds__(256) fq_moe_kq_upgate_v2_kernel(const uint8_t*
int64_t up_stride,
int64_t qb_stride,
__half* __restrict__ act,
- int S, int I, int K, int E, int ntiles_u, int topk)
+ int S, int I, int K, int E, int ntiles_u, int topk,
+ const uint8_t* __restrict__ sh_gate = nullptr,
+ const uint8_t* __restrict__ sh_up = nullptr,
+ int64_t sh_stride = 0)
{
__shared__ float pg[FQ_MOE_V2_ROWS * FQ_MOE_V2_PS];
__shared__ float pu[FQ_MOE_V2_ROWS * FQ_MOE_V2_PS];
const int s = blockIdx.x / ntiles_u;
const int t = blockIdx.x % ntiles_u;
const int r = threadIdx.x >> 3, j = threadIdx.x & 7;
- int64_t e = sel[s];
+ const bool shs = s >= S; // shared-expert slot of row s - S: Q8_0 gate / up, 8 lanes per row
+ int64_t e = shs ? 0 : sel[s];
e = (e < 0 || e >= E) ? 0 : e;
const int n = t * FQ_MOE_V2_ROWS + r;
- const uint8_t* aq = aqb + (int64_t)(s / topk) * qb_stride;
+ const uint8_t* aq = aqb + (int64_t)(shs ? s - S : s / topk) * qb_stride;
float ag[4] = {0.0f, 0.0f, 0.0f, 0.0f}, au[4] = {0.0f, 0.0f, 0.0f, 0.0f};
- if (n < I)
+ if (n < I && shs)
+ {
+ ag[0] = fq_dot_q8_0_q8(sh_gate + (int64_t)n * sh_stride, aq, K, j, 8);
+ au[0] = fq_dot_q8_0_q8(sh_up + (int64_t)n * sh_stride, aq, K, j, 8);
+ }
+ else if (n < I)
{
const uint8_t* gr = gate_bases[e] + (int64_t)n * gate_stride;
const uint8_t* ur = up_bases[e] + (int64_t)n * up_stride;
@@ -194,17 +212,22 @@ __global__ void __launch_bounds__(256) fq_moe_kq_down_v2_kernel(const uint8_t* _
const uint8_t* __restrict__ const* down_bases,
int64_t down_stride,
float* __restrict__ partial,
- int S, int I, int K, int E, int ntiles_d)
+ int S, int I, int K, int E, int ntiles_d,
+ const uint8_t* __restrict__ sh_down = nullptr,
+ int64_t sh_stride = 0)
{
__shared__ float pp[FQ_MOE_V2_ROWS * FQ_MOE_V2_PS];
const int s = blockIdx.x / ntiles_d;
const int t = blockIdx.x % ntiles_d;
const int r = threadIdx.x >> 3, j = threadIdx.x & 7;
- int64_t e = sel[s];
+ const bool shs = s >= S; // shared-expert slot (act row s): Q8_0 down, 8 lanes per row
+ int64_t e = shs ? 0 : sel[s];
e = (e < 0 || e >= E) ? 0 : e;
const int n = t * FQ_MOE_V2_ROWS + r;
float acc[4] = {0.0f, 0.0f, 0.0f, 0.0f};
- if (n < K)
+ if (n < K && shs)
+ acc[0] = fq_dot_q8_0_q8(sh_down + (int64_t)n * sh_stride, aqb + (int64_t)s * qb_stride, I, j, 8);
+ else if (n < K)
{
const uint8_t* dr = down_bases[e] + (int64_t)n * down_stride;
const uint8_t* aq = aqb + (int64_t)s * qb_stride;
@@ -251,12 +274,14 @@ __global__ void __launch_bounds__(256) fq_moe_kq_down_v2_kernel(const uint8_t* _
template <int FMT>
static void fq_moe_kq_down_v2_launch(const uint8_t* aq, int64_t qb_stride, const int64_t* sel, const uint8_t* const* bases,
- int64_t down_stride, float* partial, int S, int I, int K, int E, cudaStream_t stream)
+ int64_t down_stride, float* partial, int S, int I, int K, int E, cudaStream_t stream,
+ int Sg = 0, const uint8_t* sh_down = nullptr, int64_t sh_stride = 0)
{
if constexpr (FMT <= 2)
{
const int nt2 = (K + FQ_MOE_V2_ROWS - 1) / FQ_MOE_V2_ROWS;
- fq_moe_kq_down_v2_kernel<FMT><<<S * nt2, 256, 0, stream>>>(aq, qb_stride, sel, bases, down_stride, partial, S, I, K, E, nt2);
+ fq_moe_kq_down_v2_kernel<FMT><<<(Sg ? Sg : S) * nt2, 256, 0, stream>>>(aq, qb_stride, sel, bases, down_stride, partial, S, I, K, E, nt2,
+ sh_down, sh_stride);
}
}
@@ -317,14 +342,17 @@ __global__ void fq_moe_kq_down_kernel(const uint8_t* __restrict__ aqb,
const uint8_t* __restrict__ const* down_bases,
int64_t down_stride,
float* __restrict__ partial,
- int S, int I, int K, int E, int ntiles_d)
+ int S, int I, int K, int E, int ntiles_d,
+ const uint8_t* __restrict__ sh_down = nullptr,
+ int64_t sh_stride = 0)
{
int b = blockIdx.x;
int s = b / ntiles_d;
int t = b % ntiles_d;
int grp = threadIdx.x / FQ_GEMV_LANES;
int lane = threadIdx.x % FQ_GEMV_LANES;
- int64_t e = sel[s];
+ const bool shs = s >= S; // shared-expert slot (act row s): Q8_0 down
+ int64_t e = shs ? 0 : sel[s];
e = (e < 0 || e >= E) ? 0 : e;
int n = t * FQ_GEMV_OUT_PER_BLOCK + grp;
@@ -333,9 +361,11 @@ __global__ void fq_moe_kq_down_kernel(const uint8_t* __restrict__ aqb,
float p = 0.0f;
if (n < K)
{
- const uint8_t* dr = down_bases[e] + (int64_t)n * down_stride;
+ const uint8_t* dr = shs ? sh_down + (int64_t)n * sh_stride : down_bases[e] + (int64_t)n * down_stride;
const uint8_t* aq = aqb + (int64_t)s * qb_stride;
- if constexpr (FMT == 0)
+ if (shs)
+ p = fq_dot_q8_0_q8(dr, aq, I, lane, FQ_GEMV_LANES);
+ else if constexpr (FMT == 0)
p = fq_dot_q4_k_q8(dr, aq, I, lane, FQ_GEMV_LANES);
else if constexpr (FMT == 1)
p = fq_dot_q5_k_q8(dr, aq, I, lane, FQ_GEMV_LANES);
@@ -369,8 +399,30 @@ __global__ void fq_moe_kq_down_kernel(const uint8_t* __restrict__ aqb,
// Weighted fold (same as fq_moe_q8.cu's): out[k] = sum_s w[s]*partial[s,k].
__global__ void fq_moe_kq_combine_kernel(const float* __restrict__ partial,
const __half* __restrict__ wts,
- float* __restrict__ out, int S, int K)
+ float* __restrict__ out, int S, int K,
+ const float* __restrict__ sh_partial = nullptr, // [R, K] shared-expert rows
+ const __half* __restrict__ x = nullptr, // [R, K] MoE input (shared gate)
+ const float* __restrict__ wg = nullptr) // [K] shared gate row or nullptr
{
+ // shared expert: g = sigmoid(x_r . wg) (1 without a gate), the same for every block of row r
+ __shared__ float gred[256];
+ float g = 1.0f;
+ if (sh_partial != nullptr && wg != nullptr)
+ {
+ const __half* xr = x + (int64_t)blockIdx.y * K;
+ float d = 0.0f;
+ for (int i = threadIdx.x; i < K; i += blockDim.x)
+ d = fmaf(__half2float(xr[i]), wg[i], d);
+ gred[threadIdx.x] = d;
+ __syncthreads();
+ for (int w = blockDim.x / 2; w > 0; w >>= 1)
+ {
+ if ((int)threadIdx.x < w) gred[threadIdx.x] += gred[threadIdx.x + w];
+ __syncthreads();
+ }
+ g = 1.0f / (1.0f + expf(-gred[0]));
+ }
+ const float* shr = sh_partial != nullptr ? sh_partial + (int64_t)blockIdx.y * K : nullptr;
// blockIdx.y = row r: its S = topk assignments are partial[r*S .. r*S+S), weights wts[r*S ..)
partial += (int64_t)blockIdx.y * S * K;
wts += (int64_t)blockIdx.y * S;
@@ -385,17 +437,29 @@ __global__ void fq_moe_kq_combine_kernel(const float* __restrict__ partial,
acc = __fadd_rn(acc, __fmul_rn(partial[(int64_t)s * K + k],
__half2float(wts[s])));
}
+ if (shr != nullptr) acc = fmaf(g, shr[k], acc);
out[k] = acc;
}
}
+// Shared expert fused into the grouped launches (all null: none). Q8_0 gate / up / down, intermediate I.
+struct FqMoeSh
+{
+ const uint8_t* gate = nullptr;
+ const uint8_t* up = nullptr;
+ const uint8_t* down = nullptr;
+ int64_t ug_stride = 0;
+ int64_t d_stride = 0;
+ const float* wg = nullptr;
+};
+
template <int FMT_UG, int FMT_D>
static void fq_moe_kq_grouped_launch(torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
torch::Tensor gate_ptrs, torch::Tensor up_ptrs,
torch::Tensor down_ptrs,
int64_t gate_stride, int64_t up_stride,
int64_t down_stride, int64_t block_bytes, // block_bytes = down's format
- torch::Tensor partial, torch::Tensor out)
+ torch::Tensor partial, torch::Tensor out, FqMoeSh sh = FqMoeSh())
{
int K = (int)h.size(1);
int R = (int)h.size(0);
@@ -409,20 +473,22 @@ static void fq_moe_kq_grouped_launch(torch::Tensor h, torch::Tensor sel, torch::
int I = (int)(down_stride / block_bytes) * ((FMT_D >= 6) ? 32 : FQ_Q4_K_GROUP); // ids 6 / 7: 32-element blocks
at::cuda::CUDAGuard guard(h.device());
auto stream = at::cuda::getCurrentCUDAStream().stream();
+ const int Sg = S + (sh.gate != nullptr ? R : 0); // routed slots + one shared-expert slot per row
+ TORCH_CHECK(partial.size(0) >= Sg, "fq_moe_kq: partial must hold the shared-expert rows too");
int ntiles_u = (I + FQ_GEMV_OUT_PER_BLOCK - 1) / FQ_GEMV_OUT_PER_BLOCK;
int ntiles_d = (K + FQ_GEMV_OUT_PER_BLOCK - 1) / FQ_GEMV_OUT_PER_BLOCK;
torch::Tensor qb_h = fq_q8_scratch(h.get_device(), R, K);
fq_quant_q8_1_rows(h, qb_h);
int64_t qb_stride_h = (int64_t)(K / FQ_Q8_1_GROUP) * FQ_Q8_1_BYTES;
static thread_local std::map<std::tuple<int, int, int>, torch::Tensor> act_cache;
- auto key = std::make_tuple((int)h.get_device(), S, I);
+ auto key = std::make_tuple((int)h.get_device(), Sg, I);
auto it = act_cache.find(key);
torch::Tensor act;
if (it != act_cache.end())
act = it->second;
else
{
- act = torch::empty({S, I}, torch::dtype(torch::kHalf).device(h.device()));
+ act = torch::empty({Sg, I}, torch::dtype(torch::kHalf).device(h.device()));
act_cache[key] = act;
}
// Up+gate: 2 floats per thread in shared (g and u side by side).
@@ -430,40 +496,42 @@ static void fq_moe_kq_grouped_launch(torch::Tensor h, torch::Tensor sel, torch::
if (FMT_UG == 0 && K % 256 == 0 && fq_moe_v2_on())
{
const int nt2 = (I + FQ_MOE_V2_ROWS - 1) / FQ_MOE_V2_ROWS;
- fq_moe_kq_upgate_v2_kernel<<<S * nt2, 256, 0, stream>>>(
+ fq_moe_kq_upgate_v2_kernel<<<Sg * nt2, 256, 0, stream>>>(
qb_h.data_ptr<uint8_t>(), sel.data_ptr<int64_t>(),
reinterpret_cast<const uint8_t* const*>(gate_ptrs.data_ptr<int64_t>()),
reinterpret_cast<const uint8_t* const*>(up_ptrs.data_ptr<int64_t>()),
gate_stride, up_stride, qb_stride_h,
reinterpret_cast<__half*>(act.data_ptr<at::Half>()),
- S, I, K, E, nt2, topk);
+ S, I, K, E, nt2, topk, sh.gate, sh.up, sh.ug_stride);
}
else
- fq_moe_kq_upgate_kernel<FMT_UG><<<S * ntiles_u, FQ_GEMV_THREADS, shmem_u, stream>>>(
+ fq_moe_kq_upgate_kernel<FMT_UG><<<Sg * ntiles_u, FQ_GEMV_THREADS, shmem_u, stream>>>(
qb_h.data_ptr<uint8_t>(), sel.data_ptr<int64_t>(),
reinterpret_cast<const uint8_t* const*>(gate_ptrs.data_ptr<int64_t>()),
reinterpret_cast<const uint8_t* const*>(up_ptrs.data_ptr<int64_t>()),
gate_stride, up_stride, qb_stride_h,
reinterpret_cast<__half*>(act.data_ptr<at::Half>()),
- S, I, K, E, ntiles_u, topk);
- torch::Tensor qb_act = fq_q8_scratch(h.get_device(), S, I);
+ S, I, K, E, ntiles_u, topk, sh.gate, sh.up, sh.ug_stride);
+ torch::Tensor qb_act = fq_q8_scratch(h.get_device(), Sg, I);
fq_quant_q8_1_rows(act, qb_act);
int64_t qb_stride_a = (int64_t)(I / FQ_Q8_1_GROUP) * FQ_Q8_1_BYTES;
size_t shmem_d = (size_t)FQ_GEMV_THREADS * sizeof(float);
if (FMT_D <= 2 && I % 256 == 0 && fq_moe_v2_on())
fq_moe_kq_down_v2_launch<FMT_D>(qb_act.data_ptr<uint8_t>(), qb_stride_a, sel.data_ptr<int64_t>(),
reinterpret_cast<const uint8_t* const*>(down_ptrs.data_ptr<int64_t>()),
- down_stride, partial.data_ptr<float>(), S, I, K, E, stream);
+ down_stride, partial.data_ptr<float>(), S, I, K, E, stream, Sg, sh.down, sh.d_stride);
else
- fq_moe_kq_down_kernel<FMT_D><<<S * ntiles_d, FQ_GEMV_THREADS, shmem_d, stream>>>(
+ fq_moe_kq_down_kernel<FMT_D><<<Sg * ntiles_d, FQ_GEMV_THREADS, shmem_d, stream>>>(
qb_act.data_ptr<uint8_t>(), qb_stride_a, sel.data_ptr<int64_t>(),
reinterpret_cast<const uint8_t* const*>(down_ptrs.data_ptr<int64_t>()),
- down_stride, partial.data_ptr<float>(), S, I, K, E, ntiles_d);
+ down_stride, partial.data_ptr<float>(), S, I, K, E, ntiles_d, sh.down, sh.d_stride);
int nblocks = (K + 255) / 256;
fq_moe_kq_combine_kernel<<<dim3(nblocks, R), 256, 0, stream>>>(
partial.data_ptr<float>(),
reinterpret_cast<const __half*>(wts.data_ptr<at::Half>()),
- out.data_ptr<float>(), topk, K);
+ out.data_ptr<float>(), topk, K,
+ sh.gate != nullptr ? partial.data_ptr<float>() + (int64_t)S * K : nullptr,
+ sh.gate != nullptr ? reinterpret_cast<const __half*>(h.data_ptr<at::Half>()) : nullptr, sh.wg);
}
void fq_moe_q4k_grouped_q8(torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
@@ -517,38 +585,78 @@ template <int FU>
static void fq_moe_kq_dispatch_d(int fd, torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
torch::Tensor gp, torch::Tensor up, torch::Tensor dp,
int64_t gs, int64_t us, int64_t ds, torch::Tensor partial,
- torch::Tensor out)
+ torch::Tensor out, FqMoeSh sh = FqMoeSh())
{
switch (fd)
{
- case 0: fq_moe_kq_grouped_launch<FU, 0>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[0], partial, out); break;
- case 1: fq_moe_kq_grouped_launch<FU, 1>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[1], partial, out); break;
- case 2: fq_moe_kq_grouped_launch<FU, 2>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[2], partial, out); break;
- case 3: fq_moe_kq_grouped_launch<FU, 3>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[3], partial, out); break;
- case 4: fq_moe_kq_grouped_launch<FU, 4>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[4], partial, out); break;
- case 5: fq_moe_kq_grouped_launch<FU, 5>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[5], partial, out); break;
- case 6: fq_moe_kq_grouped_launch<FU, 6>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[6], partial, out); break;
- case 7: fq_moe_kq_grouped_launch<FU, 7>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[7], partial, out); break;
+ case 0: fq_moe_kq_grouped_launch<FU, 0>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[0], partial, out, sh); break;
+ case 1: fq_moe_kq_grouped_launch<FU, 1>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[1], partial, out, sh); break;
+ case 2: fq_moe_kq_grouped_launch<FU, 2>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[2], partial, out, sh); break;
+ case 3: fq_moe_kq_grouped_launch<FU, 3>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[3], partial, out, sh); break;
+ case 4: fq_moe_kq_grouped_launch<FU, 4>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[4], partial, out, sh); break;
+ case 5: fq_moe_kq_grouped_launch<FU, 5>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[5], partial, out, sh); break;
+ case 6: fq_moe_kq_grouped_launch<FU, 6>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[6], partial, out, sh); break;
+ case 7: fq_moe_kq_grouped_launch<FU, 7>(h, sel, wts, gp, up, dp, gs, us, ds, kFmtBytes[7], partial, out, sh); break;
default: TORCH_CHECK(false, "fq_moe_kq: bad down format ", fd);
}
}
// Mixed per-projection formats (unsloth UD: gate/up Q4_K + down Q5_K). fmt ids: 0 q4_k, 1 q5_k, 2 q6_k, 3 iq4_xs, 4 iq3_xxs, 5 iq2_s.
-void fq_moe_kq_grouped_mixed_q8(torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
+static void fq_moe_kq_grouped_mixed_impl(torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
torch::Tensor gate_ptrs, torch::Tensor up_ptrs,
torch::Tensor down_ptrs,
int64_t gate_stride, int64_t up_stride, int64_t down_stride,
int64_t fmt_ug, int64_t fmt_down,
- torch::Tensor partial, torch::Tensor out)
+ torch::Tensor partial, torch::Tensor out, FqMoeSh sh)
{
switch (fmt_ug)
{
- case 0: fq_moe_kq_dispatch_d<0>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out); break;
- case 1: fq_moe_kq_dispatch_d<1>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out); break;
- case 2: fq_moe_kq_dispatch_d<2>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out); break;
- case 3: fq_moe_kq_dispatch_d<3>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out); break;
- case 4: fq_moe_kq_dispatch_d<4>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out); break;
- case 5: fq_moe_kq_dispatch_d<5>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out); break;
+ case 0: fq_moe_kq_dispatch_d<0>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out, sh); break;
+ case 1: fq_moe_kq_dispatch_d<1>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out, sh); break;
+ case 2: fq_moe_kq_dispatch_d<2>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out, sh); break;
+ case 3: fq_moe_kq_dispatch_d<3>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out, sh); break;
+ case 4: fq_moe_kq_dispatch_d<4>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out, sh); break;
+ case 5: fq_moe_kq_dispatch_d<5>((int)fmt_down, h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride, partial, out, sh); break;
default: TORCH_CHECK(false, "fq_moe_kq: bad gate/up format ", fmt_ug);
}
}
+
+void fq_moe_kq_grouped_mixed_q8(torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
+ torch::Tensor gate_ptrs, torch::Tensor up_ptrs,
+ torch::Tensor down_ptrs,
+ int64_t gate_stride, int64_t up_stride, int64_t down_stride,
+ int64_t fmt_ug, int64_t fmt_down,
+ torch::Tensor partial, torch::Tensor out)
+{
+ fq_moe_kq_grouped_mixed_impl(h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride,
+ fmt_ug, fmt_down, partial, out, FqMoeSh());
+}
+
+// Same + the shared expert (Q8_0 gate / up [I, K] and down [K, I] packed rows, optional fp32 gate row [K]) in the same launches:
+// partial needs S + R rows; out = routed + sigmoid(x . sh_wg) * shared (no gate: + shared).
+void fq_moe_kq_grouped_mixed_sh_q8(torch::Tensor h, torch::Tensor sel, torch::Tensor wts,
+ torch::Tensor gate_ptrs, torch::Tensor up_ptrs,
+ torch::Tensor down_ptrs,
+ int64_t gate_stride, int64_t up_stride, int64_t down_stride,
+ int64_t fmt_ug, int64_t fmt_down,
+ torch::Tensor partial, torch::Tensor out,
+ torch::Tensor sh_gate, torch::Tensor sh_up, torch::Tensor sh_down,
+ torch::Tensor sh_wg)
+{
+ TORCH_CHECK(sh_gate.dtype() == torch::kUInt8 && sh_up.dtype() == torch::kUInt8 && sh_down.dtype() == torch::kUInt8,
+ "fq_moe_kq: shared expert weights must be packed Q8_0 bytes");
+ TORCH_CHECK(sh_gate.is_contiguous() && sh_up.is_contiguous() && sh_down.is_contiguous(), "fq_moe_kq: shared weights contiguous");
+ FqMoeSh sh;
+ sh.gate = sh_gate.data_ptr<uint8_t>();
+ sh.up = sh_up.data_ptr<uint8_t>();
+ sh.down = sh_down.data_ptr<uint8_t>();
+ sh.ug_stride = (int64_t)(h.size(1) / 32) * FQ_Q8_0_BYTES;
+ sh.d_stride = sh_down.numel() / h.size(1);
+ if (sh_wg.numel() > 0)
+ {
+ TORCH_CHECK(sh_wg.dtype() == torch::kFloat && sh_wg.is_contiguous() && sh_wg.numel() == h.size(1), "fq_moe_kq: sh_wg [K] fp32");
+ sh.wg = sh_wg.data_ptr<float>();
+ }
+ fq_moe_kq_grouped_mixed_impl(h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride,
+ fmt_ug, fmt_down, partial, out, sh);
+}
diff --git a/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_iq_tables.cuh b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_iq_tables.cuh
new file mode 100644
index 0000000..ec3875a
--- /dev/null
+++ b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_iq_tables.cuh
@@ -0,0 +1,589 @@
+// Lookup tables of the IQ formats, verbatim from llama.cpp ggml/src/ggml-common.h
+// (https://github.com/ggml-org/llama.cpp, commit 7182ef7).
+// MIT License, Copyright (c) 2023-2026 The ggml authors. See THIRD_PARTY_NOTICES.md at the repository root.
+// Extracted only: iq2xxs_grid, iq2xs_grid, iq2s_grid, iq3xxs_grid, iq3s_grid, kvalues_iq4nl, kmask_iq2xs, ksigns_iq2xs.
+// Plain __device__ (global, L1-cached), like ggml: every lane reads a different entry (see fq_tables.cuh).
+#pragma once
+#include <stdint.h>
+
+namespace ggml_iq {
+
+static __device__ const uint64_t iq2xxs_grid[256] = {
+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
+ 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
+ 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
+ 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
+ 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
+ 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
+ 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
+ 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
+ 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
+ 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
+ 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
+ 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
+ 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
+ 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
+ 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
+ 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
+ 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
+ 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
+ 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
+ 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
+ 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
+ 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
+ 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
+ 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
+ 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
+ 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
+ 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
+ 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
+ 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
+ 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
+ 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
+ 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
+ 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
+ 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
+ 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
+ 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
+ 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
+ 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
+ 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
+ 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
+ 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
+ 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
+ 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
+ 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
+ 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
+ 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
+ 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
+ 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
+ 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
+ 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
+ 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
+ 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
+ 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
+ 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
+ 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
+ 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
+ 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
+ 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
+ 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
+ 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
+};
+
+static __device__ const uint64_t iq2xs_grid[512] = {
+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
+ 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
+ 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
+ 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
+ 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
+ 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
+ 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
+ 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
+ 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
+ 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
+ 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
+ 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
+ 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
+ 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
+ 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
+ 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
+ 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
+ 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
+ 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
+ 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
+ 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
+ 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
+ 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
+ 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
+ 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
+ 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
+ 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
+ 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
+ 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
+ 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
+ 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
+ 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
+ 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
+ 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
+ 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
+ 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
+ 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
+ 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
+ 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
+ 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
+ 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
+ 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
+ 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
+ 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
+ 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
+ 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
+ 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
+ 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
+ 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
+ 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
+ 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
+ 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
+ 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
+ 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
+ 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
+ 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
+ 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
+ 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
+ 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
+ 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
+ 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
+ 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
+ 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
+ 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
+ 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
+ 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
+ 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
+ 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
+ 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
+ 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
+ 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
+ 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
+ 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
+ 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
+ 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
+ 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
+ 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
+ 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
+ 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
+ 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
+ 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
+ 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
+ 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
+ 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
+ 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
+ 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
+ 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
+ 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
+ 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
+ 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
+ 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
+ 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
+ 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
+ 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
+ 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
+ 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
+ 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
+ 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
+ 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
+ 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
+ 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
+ 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
+ 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
+ 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
+ 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
+ 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
+ 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
+ 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
+ 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
+ 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
+ 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
+ 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
+ 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
+ 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
+ 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
+ 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
+ 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
+ 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
+ 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
+ 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
+ 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
+ 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
+ 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
+ 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
+ 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
+};
+
+static __device__ const uint64_t iq2s_grid[1024] = {
+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
+ 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
+ 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
+ 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
+ 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
+ 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
+ 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
+ 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
+ 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
+ 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
+ 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
+ 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
+ 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
+ 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
+ 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
+ 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
+ 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
+ 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
+ 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
+ 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
+ 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
+ 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
+ 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
+ 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
+ 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
+ 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
+ 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
+ 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
+ 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
+ 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
+ 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
+ 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
+ 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
+ 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
+ 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
+ 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
+ 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
+ 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
+ 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
+ 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
+ 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
+ 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
+ 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
+ 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
+ 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
+ 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
+ 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
+ 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
+ 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
+ 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
+ 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
+ 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
+ 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
+ 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
+ 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
+ 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
+ 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
+ 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
+ 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
+ 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
+ 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
+ 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
+ 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
+ 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
+ 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
+ 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
+ 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
+ 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
+ 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
+ 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
+ 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
+ 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
+ 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
+ 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
+ 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
+ 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
+ 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
+ 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
+ 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
+ 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
+ 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
+ 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
+ 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
+ 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
+ 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
+ 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
+ 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
+ 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
+ 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
+ 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
+ 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
+ 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
+ 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
+ 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
+ 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
+ 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
+ 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
+ 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
+ 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
+ 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
+ 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
+ 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
+ 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
+ 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
+ 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
+ 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
+ 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
+ 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
+ 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
+ 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
+ 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
+ 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
+ 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
+ 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
+ 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
+ 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
+ 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
+ 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
+ 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
+ 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
+ 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
+ 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
+ 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
+ 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
+ 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
+ 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
+ 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
+ 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
+ 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
+ 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
+ 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
+ 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
+ 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
+ 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
+ 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
+ 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
+ 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
+ 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
+ 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
+ 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
+ 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
+ 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
+ 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
+ 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
+ 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
+ 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
+ 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
+ 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
+ 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
+ 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
+ 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
+ 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
+ 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
+ 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
+ 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
+ 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
+ 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
+ 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
+ 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
+ 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
+ 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
+ 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
+ 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
+ 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
+ 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
+ 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
+ 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
+ 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
+ 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
+ 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
+ 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
+ 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
+ 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
+ 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
+ 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
+ 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
+ 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
+ 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
+ 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
+ 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
+ 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
+ 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
+ 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
+ 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
+ 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
+ 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
+ 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
+ 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
+ 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
+ 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
+ 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
+ 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
+ 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
+ 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
+ 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
+ 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
+ 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
+ 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
+ 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
+ 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
+ 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
+ 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
+ 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
+ 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
+ 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
+ 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
+ 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
+ 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
+ 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
+ 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
+ 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
+ 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
+ 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
+ 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
+ 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
+ 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
+ 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
+ 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
+ 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
+ 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
+ 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
+ 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
+ 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
+ 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
+ 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
+ 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
+ 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
+ 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
+ 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
+ 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
+ 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
+ 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
+ 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
+ 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
+ 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
+ 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
+ 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
+ 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
+ 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
+ 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
+ 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
+ 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
+ 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
+ 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
+ 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
+ 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
+ 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
+ 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
+ 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
+ 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
+ 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
+ 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
+ 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
+ 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
+};
+
+static __device__ const uint32_t iq3xxs_grid[256] = {
+ 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
+ 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
+ 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
+ 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
+ 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
+ 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
+ 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
+ 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
+ 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
+ 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
+ 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
+ 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
+ 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
+ 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
+ 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
+ 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
+ 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
+ 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
+ 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
+ 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
+ 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
+ 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
+ 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
+ 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
+ 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
+ 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
+ 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
+ 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
+ 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
+ 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
+ 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
+ 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
+};
+
+static __device__ const uint32_t iq3s_grid[512] = {
+ 0x01010101, 0x01010103, 0x01010105, 0x0101010b, 0x0101010f, 0x01010301, 0x01010303, 0x01010305,
+ 0x01010309, 0x0101030d, 0x01010501, 0x01010503, 0x0101050b, 0x01010707, 0x01010901, 0x01010905,
+ 0x0101090b, 0x0101090f, 0x01010b03, 0x01010b07, 0x01010d01, 0x01010d05, 0x01010f03, 0x01010f09,
+ 0x01010f0f, 0x01030101, 0x01030103, 0x01030105, 0x01030109, 0x01030301, 0x01030303, 0x0103030b,
+ 0x01030501, 0x01030507, 0x0103050f, 0x01030703, 0x0103070b, 0x01030909, 0x01030d03, 0x01030d0b,
+ 0x01030f05, 0x01050101, 0x01050103, 0x0105010b, 0x0105010f, 0x01050301, 0x01050307, 0x0105030d,
+ 0x01050503, 0x0105050b, 0x01050701, 0x01050709, 0x01050905, 0x0105090b, 0x0105090f, 0x01050b03,
+ 0x01050b07, 0x01050f01, 0x01050f07, 0x01070107, 0x01070303, 0x0107030b, 0x01070501, 0x01070505,
+ 0x01070703, 0x01070707, 0x0107070d, 0x01070909, 0x01070b01, 0x01070b05, 0x01070d0f, 0x01070f03,
+ 0x01070f0b, 0x01090101, 0x01090307, 0x0109030f, 0x01090503, 0x01090509, 0x01090705, 0x01090901,
+ 0x01090907, 0x01090b03, 0x01090f01, 0x010b0105, 0x010b0109, 0x010b0501, 0x010b0505, 0x010b050d,
+ 0x010b0707, 0x010b0903, 0x010b090b, 0x010b090f, 0x010b0d0d, 0x010b0f07, 0x010d010d, 0x010d0303,
+ 0x010d0307, 0x010d0703, 0x010d0b05, 0x010d0f03, 0x010f0101, 0x010f0105, 0x010f0109, 0x010f0501,
+ 0x010f0505, 0x010f050d, 0x010f0707, 0x010f0b01, 0x010f0b09, 0x03010101, 0x03010103, 0x03010105,
+ 0x03010109, 0x03010301, 0x03010303, 0x03010307, 0x0301030b, 0x0301030f, 0x03010501, 0x03010505,
+ 0x03010703, 0x03010709, 0x0301070d, 0x03010b09, 0x03010b0d, 0x03010d03, 0x03010f05, 0x03030101,
+ 0x03030103, 0x03030107, 0x0303010d, 0x03030301, 0x03030309, 0x03030503, 0x03030701, 0x03030707,
+ 0x03030903, 0x03030b01, 0x03030b05, 0x03030f01, 0x03030f0d, 0x03050101, 0x03050305, 0x0305030b,
+ 0x0305030f, 0x03050501, 0x03050509, 0x03050705, 0x03050901, 0x03050907, 0x03050b0b, 0x03050d01,
+ 0x03050f05, 0x03070103, 0x03070109, 0x0307010f, 0x03070301, 0x03070307, 0x03070503, 0x0307050f,
+ 0x03070701, 0x03070709, 0x03070903, 0x03070d05, 0x03070f01, 0x03090107, 0x0309010b, 0x03090305,
+ 0x03090309, 0x03090703, 0x03090707, 0x03090905, 0x0309090d, 0x03090b01, 0x03090b09, 0x030b0103,
+ 0x030b0301, 0x030b0307, 0x030b0503, 0x030b0701, 0x030b0705, 0x030b0b03, 0x030d0501, 0x030d0509,
+ 0x030d050f, 0x030d0909, 0x030d090d, 0x030f0103, 0x030f0107, 0x030f0301, 0x030f0305, 0x030f0503,
+ 0x030f070b, 0x030f0903, 0x030f0d05, 0x030f0f01, 0x05010101, 0x05010103, 0x05010107, 0x0501010b,
+ 0x0501010f, 0x05010301, 0x05010305, 0x05010309, 0x0501030d, 0x05010503, 0x05010507, 0x0501050f,
+ 0x05010701, 0x05010705, 0x05010903, 0x05010907, 0x0501090b, 0x05010b01, 0x05010b05, 0x05010d0f,
+ 0x05010f01, 0x05010f07, 0x05010f0b, 0x05030101, 0x05030105, 0x05030301, 0x05030307, 0x0503030f,
+ 0x05030505, 0x0503050b, 0x05030703, 0x05030709, 0x05030905, 0x05030b03, 0x05050103, 0x05050109,
+ 0x0505010f, 0x05050503, 0x05050507, 0x05050701, 0x0505070f, 0x05050903, 0x05050b07, 0x05050b0f,
+ 0x05050f03, 0x05050f09, 0x05070101, 0x05070105, 0x0507010b, 0x05070303, 0x05070505, 0x05070509,
+ 0x05070703, 0x05070707, 0x05070905, 0x05070b01, 0x05070d0d, 0x05090103, 0x0509010f, 0x05090501,
+ 0x05090507, 0x05090705, 0x0509070b, 0x05090903, 0x05090f05, 0x05090f0b, 0x050b0109, 0x050b0303,
+ 0x050b0505, 0x050b070f, 0x050b0901, 0x050b0b07, 0x050b0f01, 0x050d0101, 0x050d0105, 0x050d010f,
+ 0x050d0503, 0x050d0b0b, 0x050d0d03, 0x050f010b, 0x050f0303, 0x050f050d, 0x050f0701, 0x050f0907,
+ 0x050f0b01, 0x07010105, 0x07010303, 0x07010307, 0x0701030b, 0x0701030f, 0x07010505, 0x07010703,
+ 0x07010707, 0x0701070b, 0x07010905, 0x07010909, 0x0701090f, 0x07010b03, 0x07010d07, 0x07010f03,
+ 0x07030103, 0x07030107, 0x0703010b, 0x07030309, 0x07030503, 0x07030507, 0x07030901, 0x07030d01,
+ 0x07030f05, 0x07030f0d, 0x07050101, 0x07050305, 0x07050501, 0x07050705, 0x07050709, 0x07050b01,
+ 0x07070103, 0x07070301, 0x07070309, 0x07070503, 0x07070507, 0x0707050f, 0x07070701, 0x07070903,
+ 0x07070907, 0x0707090f, 0x07070b0b, 0x07070f07, 0x07090107, 0x07090303, 0x0709030d, 0x07090505,
+ 0x07090703, 0x07090b05, 0x07090d01, 0x07090d09, 0x070b0103, 0x070b0301, 0x070b0305, 0x070b050b,
+ 0x070b0705, 0x070b0909, 0x070b0b0d, 0x070b0f07, 0x070d030d, 0x070d0903, 0x070f0103, 0x070f0107,
+ 0x070f0501, 0x070f0505, 0x070f070b, 0x09010101, 0x09010109, 0x09010305, 0x09010501, 0x09010509,
+ 0x0901050f, 0x09010705, 0x09010903, 0x09010b01, 0x09010f01, 0x09030105, 0x0903010f, 0x09030303,
+ 0x09030307, 0x09030505, 0x09030701, 0x0903070b, 0x09030907, 0x09030b03, 0x09030b0b, 0x09050103,
+ 0x09050107, 0x09050301, 0x0905030b, 0x09050503, 0x09050707, 0x09050901, 0x09050b0f, 0x09050d05,
+ 0x09050f01, 0x09070109, 0x09070303, 0x09070307, 0x09070501, 0x09070505, 0x09070703, 0x0907070b,
+ 0x09090101, 0x09090105, 0x09090509, 0x0909070f, 0x09090901, 0x09090f03, 0x090b010b, 0x090b010f,
+ 0x090b0503, 0x090b0d05, 0x090d0307, 0x090d0709, 0x090d0d01, 0x090f0301, 0x090f030b, 0x090f0701,
+ 0x090f0907, 0x090f0b03, 0x0b010105, 0x0b010301, 0x0b010309, 0x0b010505, 0x0b010901, 0x0b010909,
+ 0x0b01090f, 0x0b010b05, 0x0b010d0d, 0x0b010f09, 0x0b030103, 0x0b030107, 0x0b03010b, 0x0b030305,
+ 0x0b030503, 0x0b030705, 0x0b030f05, 0x0b050101, 0x0b050303, 0x0b050507, 0x0b050701, 0x0b05070d,
+ 0x0b050b07, 0x0b070105, 0x0b07010f, 0x0b070301, 0x0b07050f, 0x0b070909, 0x0b070b03, 0x0b070d0b,
+ 0x0b070f07, 0x0b090103, 0x0b090109, 0x0b090501, 0x0b090705, 0x0b09090d, 0x0b0b0305, 0x0b0b050d,
+ 0x0b0b0b03, 0x0b0b0b07, 0x0b0d0905, 0x0b0f0105, 0x0b0f0109, 0x0b0f0505, 0x0d010303, 0x0d010307,
+ 0x0d01030b, 0x0d010703, 0x0d010707, 0x0d010d01, 0x0d030101, 0x0d030501, 0x0d03050f, 0x0d030d09,
+ 0x0d050305, 0x0d050709, 0x0d050905, 0x0d050b0b, 0x0d050d05, 0x0d050f01, 0x0d070101, 0x0d070309,
+ 0x0d070503, 0x0d070901, 0x0d09050b, 0x0d090907, 0x0d090d05, 0x0d0b0101, 0x0d0b0107, 0x0d0b0709,
+ 0x0d0b0d01, 0x0d0d010b, 0x0d0d0901, 0x0d0f0303, 0x0d0f0307, 0x0f010101, 0x0f010109, 0x0f01010f,
+ 0x0f010501, 0x0f010505, 0x0f01070d, 0x0f010901, 0x0f010b09, 0x0f010d05, 0x0f030105, 0x0f030303,
+ 0x0f030509, 0x0f030907, 0x0f03090b, 0x0f050103, 0x0f050109, 0x0f050301, 0x0f05030d, 0x0f050503,
+ 0x0f050701, 0x0f050b03, 0x0f070105, 0x0f070705, 0x0f07070b, 0x0f070b07, 0x0f090103, 0x0f09010b,
+ 0x0f090307, 0x0f090501, 0x0f090b01, 0x0f0b0505, 0x0f0b0905, 0x0f0d0105, 0x0f0d0703, 0x0f0f0101,
+};
+
+static __device__ const int8_t kvalues_iq4nl[16] = {
+ -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113,
+};
+
+static __device__ const uint8_t kmask_iq2xs[8] = {
+ 1, 2, 4, 8, 16, 32, 64, 128
+};
+
+static __device__ const uint8_t ksigns_iq2xs[128] = {
+ 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
+ 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
+ 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
+ 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
+ 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
+ 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
+ 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
+ 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
+};
+
+} // namespace ggml_iq
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.cuh
new file mode 100644
index 0000000..863a3dd
--- /dev/null
+++ b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_iq.cuh
@@ -0,0 +1,318 @@
+// IQ vec-dot functions (activations as q8_1 blocks, integer dp4a dots) ported from llama.cpp
+// 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.
+#pragma once
+
+#include <cuda_fp16.h>
+#include <stdint.h>
+#include "ggml_iq_tables.cuh"
+
+namespace ggml_iq {
+
+#define QK_K 256
+#define QK4_NL 32
+#define IQ3S_N_SCALE (QK_K / 64)
+#define VDR_Q4_0_Q8_1_MMVQ 2
+
+struct block_q8_1 {
+ half2 ds; // ds.x = d (scale), ds.y = d * sum(qs)
+ int8_t qs[32];
+};
+struct block_iq2_xxs { half d; uint16_t qs[QK_K / 8]; };
+struct block_iq2_xs { half d; uint16_t qs[QK_K / 8]; uint8_t scales[QK_K / 32]; };
+struct block_iq2_s { half d; uint8_t qs[QK_K / 4]; uint8_t qh[QK_K / 32]; uint8_t scales[QK_K / 32]; };
+struct block_iq3_xxs { half d; uint8_t qs[3 * QK_K / 8]; };
+struct block_iq3_s { half d; uint8_t qs[QK_K / 4]; uint8_t qh[QK_K / 32]; uint8_t signs[QK_K / 8]; uint8_t scales[IQ3S_N_SCALE]; };
+struct block_iq4_nl { half d; uint8_t qs[QK4_NL / 2]; };
+struct block_iq4_xs { half d; uint16_t scales_h; uint8_t scales_l[QK_K / 64]; uint8_t qs[QK_K / 2]; };
+static_assert(sizeof(block_iq2_xxs) == 66 && sizeof(block_iq2_xs) == 74 && sizeof(block_iq2_s) == 82 && sizeof(block_iq3_xxs) == 98 &&
+ sizeof(block_iq3_s) == 110 && sizeof(block_iq4_nl) == 18 && sizeof(block_iq4_xs) == 136 && sizeof(block_q8_1) == 36,
+ "ggml block layout");
+
+static __device__ __forceinline__ int get_int_b2(const void * x, const int & i32) {
+ const uint16_t * x16 = (const uint16_t *) x; // assume at least 2 byte alignment
+ int x32 = x16[2*i32 + 0] << 0;
+ x32 |= x16[2*i32 + 1] << 16;
+ return x32;
+}
+
+static __device__ __forceinline__ int get_int_b4(const void * x, const int & i32) {
+ return ((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)
+static __device__ __forceinline__ int2 get_int_from_table_16(const int & q4, const int8_t * table) {
+ const uint32_t * table32 = (const uint32_t *) table;
+ uint32_t tmp[2];
+ const uint32_t low_high_selection_indices = (0x32103210 | ((q4 & 0x88888888) >> 1));
+#pragma unroll
+ for (uint32_t i = 0; i < 2; ++i) {
+ const uint32_t shift = 16 * i;
+ const uint32_t low = __byte_perm(table32[0], table32[1], q4 >> shift);
+ const uint32_t high = __byte_perm(table32[2], table32[3], q4 >> shift);
+ tmp[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));
+}
+
+static __device__ __forceinline__ uint32_t unpack_ksigns(const uint8_t v) {
+ // v is a 7 bit int, with the 8th sign being encodable as popcnt
+ const uint32_t p = __popc(v) & 1;
+ const uint32_t s = v ^ p << 7;
+ return s * 0x01010101;
+}
+
+#define VDR_IQ2_XXS_Q8_1_MMVQ 2
+
+static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq + kbx;
+
+ const int q2 = get_int_b2(bq2->qs, iqs);
+ const uint8_t * aux8 = (const uint8_t *) &q2;
+ const uint32_t aux32 = get_int_b2(bq2->qs, iqs + 1);
+
+ int sumi = 0;
+#pragma unroll
+ for (int k0 = 0; k0 < 8; k0 += 2) {
+ const uint2 grid_pos = ((const uint2*)iq2xxs_grid)[aux8[k0/2]];
+ const uint32_t signs = unpack_ksigns(aux32 >> (7 * k0 / 2));
+
+ 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);
+
+ 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);
+ }
+
+ const int ls = aux32 >> 27 | 1; // (scale * 2 + 1)
+ sumi = sumi * ls / 8; // (sumi * scale + sumi / 2) / 4
+ const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds);
+ return d * sumi;
+}
+
+#define VDR_IQ2_XS_Q8_1_MMVQ 2
+
+static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq + kbx;
+
+ const int2 q2_packed = make_int2(get_int_b2(bq2->qs, iqs + 0), get_int_b2(bq2->qs, iqs + 1));
+ const uint16_t * q2 = (const uint16_t *) &q2_packed;
+ const int ls0 = bq2->scales[iqs/2] & 0x0F;
+ const int ls1 = bq2->scales[iqs/2] >> 4;
+
+ int sumi0 = 0;
+ int sumi1 = 0;
+#pragma unroll
+ for (int l0 = 0; l0 < 8; l0 += 2) {
+ const uint2 grid_pos = ((const uint2*)iq2xs_grid)[q2[l0/2] & 0x1FF];
+ const uint32_t signs = unpack_ksigns(q2[l0/2] >> 9);
+
+ const int signs0 = __vcmpne4(signs & 0x08040201, 0);
+ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);
+ const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0);
+
+ const int signs1 = __vcmpne4(signs & 0x80402010, 0);
+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);
+ 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);
+ } else {
+ sumi1 = __dp4a(grid_l, u0, sumi1);
+ sumi1 = __dp4a(grid_h, u1, sumi1);
+ }
+ }
+ const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4;
+ const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds);
+ return d * sumi;
+}
+
+#define VDR_IQ2_S_Q8_1_MMVQ 2
+
+static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_iq2_s * bq2 = (const block_iq2_s *) vbq + kbx;
+
+ const int qs_packed = get_int_b2(bq2->qs, iqs/2);
+ const uint8_t * qs = (const uint8_t *) &qs_packed;
+
+ const int qh = bq2->qh[iqs/2];
+
+ const int signs_packed_32 = get_int_b2(bq2->qs, QK_K/32 + iqs/2);
+ const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32;
+
+ const int ls0 = bq2->scales[iqs/2] & 0x0F;
+ const int ls1 = bq2->scales[iqs/2] >> 4;
+
+ int sumi0 = 0;
+ int sumi1 = 0;
+#pragma unroll
+ for (int l0 = 0; l0 < 8; l0 += 2) {
+ const int * grid_pos = (const int *)(iq2s_grid + (qs[l0/2] | ((qh << (8-l0)) & 0x300)));
+
+ const int signs0 = __vcmpne4(((signs_packed_8[l0/2] & 0x03) << 7) | ((signs_packed_8[l0/2] & 0x0C) << 21), 0x00000000);
+ const int signs1 = __vcmpne4(((signs_packed_8[l0/2] & 0x30) << 3) | ((signs_packed_8[l0/2] & 0xC0) << 17), 0x00000000);
+
+ const int grid_l = __vsub4(grid_pos[0] ^ signs0, signs0);
+ const int grid_h = __vsub4(grid_pos[1] ^ signs1, signs1);
+
+ 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);
+
+ if (l0 < 4) {
+ sumi0 = __dp4a(grid_l, u0, sumi0);
+ sumi0 = __dp4a(grid_h, u1, sumi0);
+ } else {
+ sumi1 = __dp4a(grid_l, u0, sumi1);
+ sumi1 = __dp4a(grid_h, u1, sumi1);
+ }
+ }
+ const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4;
+
+ const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds);
+ return d * sumi;
+}
+
+#define VDR_IQ3_XXS_Q8_1_MMVQ 2
+
+static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_iq3_xxs * bq3 = (const block_iq3_xxs *) vbq + kbx;
+
+ const int2 q3_packed = make_int2(get_int_b2(bq3->qs, iqs), get_int_b2(bq3->qs, iqs+1));
+ const uint8_t * q3 = (const uint8_t *) &q3_packed;
+ const uint32_t aux32 = get_int_b2(bq3->qs, QK_K/16 + iqs/2);
+
+ int sumi = 0;
+#pragma unroll
+ for (int l0 = 0; l0 < 8; l0 += 2) {
+ const int2 grid_pos = make_int2(iq3xxs_grid[q3[l0 + 0]], iq3xxs_grid[q3[l0 + 1]]);
+ const uint32_t signs = unpack_ksigns(aux32 >> (7*l0/2));
+
+ const int signs0 = __vcmpne4(signs & 0x08040201, 0);
+ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);
+
+ const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0);
+
+ const int signs1 = __vcmpne4(signs & 0x80402010, 0);
+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);
+
+ 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);
+ }
+
+ const int ls = aux32 >> 28;
+ sumi = (ls*sumi + sumi/2)/2;
+ const float d = __half2float(bq3->d) * __low2float(bq8_1[iqs/2].ds);
+ return d * sumi;
+}
+
+#define VDR_IQ3_S_Q8_1_MMVQ 2
+
+// TODO: don't use lookup table for signs
+static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_iq3_s * bq3 = (const block_iq3_s *) vbq + kbx;
+
+ const int2 qs_packed = make_int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1));
+ const uint8_t * qs = (const uint8_t *) &qs_packed;
+
+ const int qh = bq3->qh[iqs/2];
+
+ const int signs_packed_32 = get_int_b2(bq3->signs, iqs/2);
+ const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32;
+
+ int sumi = 0;
+#pragma unroll
+ for (int l0 = 0; l0 < 8; l0 += 2) {
+ const int2 grid_pos = make_int2(
+ iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],
+ iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);
+
+ const int signs0 = __vcmpne4(((signs_packed_8[l0/2] & 0x03) << 7) | ((signs_packed_8[l0/2] & 0x0C) << 21), 0x00000000);
+ const int signs1 = __vcmpne4(((signs_packed_8[l0/2] & 0x30) << 3) | ((signs_packed_8[l0/2] & 0xC0) << 17), 0x00000000);
+
+ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);
+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);
+
+ 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 *= 1 + 2*((bq3->scales[iqs/4] >> ((iqs << 1) & 0x04)) & 0x0F);
+
+ const float d = __half2float(bq3->d) * __low2float(bq8_1[iqs/2].ds);
+ return d * sumi;
+}
+
+#define VDR_IQ4_NL_Q8_1_MMVQ 2
+
+static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_iq4_nl * bq4 = (const block_iq4_nl *) vbq + kbx;
+
+ const int * q8 = (const int *) bq8_1->qs + iqs;
+
+ int sumi = 0;
+#pragma unroll
+ for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
+ 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);
+ }
+
+ const float d = __half2float(bq4->d) * __low2float(bq8_1->ds);
+ return d * sumi;
+}
+
+#define VDR_IQ4_XS_Q8_1_MMVQ 4
+
+static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq + kbx;
+
+ int sumi = 0;
+#pragma unroll
+ for (int j = 0; j < 4; ++j) {
+ const int aux_q4 = get_int_b4(bq4->qs, iqs + j);
+ const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl);
+
+ 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);
+ }
+
+ const int ls = ((bq4->scales_l[iqs/8] >> (iqs & 0x04)) & 0x0F) | (((bq4->scales_h >> (iqs/2)) & 0x03) << 4);
+ sumi *= ls - 32;
+
+ const float d = __half2float(bq4->d) * __low2float(bq8_1[iqs/4].ds);
+ return d * sumi;
+}
+
+} // namespace ggml_iq
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.cuh
new file mode 100644
index 0000000..41c3579
--- /dev/null
+++ b/exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ggml_vecdotq_k.cuh
@@ -0,0 +1,109 @@
+// 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.
+#pragma once
+
+#include "ggml_vecdotq_iq.cuh"
+
+namespace ggml_iq {
+
+#define QR5_K 2
+#define QI8_1 8
+#define K_SCALE_SIZE 12
+
+struct block_q5_K {
+ half2 dm; // d, dmin
+ uint8_t scales[K_SCALE_SIZE];
+ uint8_t qh[QK_K / 8];
+ uint8_t qs[QK_K / 2];
+};
+static_assert(sizeof(block_q5_K) == 176, "ggml block layout");
+
+#define VDR_Q5_K_Q8_1_MMVQ 2
+
+// contiguous v/x values
+static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(
+ const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,
+ const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {
+
+ float sumf_d = 0.0f;
+ float sumf_m = 0.0f;
+
+#pragma unroll
+ for (int i = 0; i < QR5_K; ++i) {
+ const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
+ const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
+
+ const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
+ const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
+
+ 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
+
+ sumf_d += d8[i] * (dot1 * sc[i]);
+ sumf_m += d8[i] * (dot2 * m[i]);
+
+ }
+
+ const float2 dm5f = __half22float2(dm5);
+
+ return dm5f.x*sumf_d - dm5f.y*sumf_m;
+}
+
+
+static __device__ __forceinline__ float vec_dot_q5_K_q8_1(
+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {
+
+ const block_q5_K * bq5_K = (const block_q5_K *) vbq + kbx;
+
+ int vl[2];
+ int vh[2];
+ int u[2*QR5_K];
+ float d8[QR5_K];
+
+ const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
+ const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
+ const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
+
+ vl[0] = ql[0];
+ vl[1] = ql[4];
+
+ vh[0] = qh[0] >> bq8_offset;
+ vh[1] = qh[4] >> bq8_offset;
+
+ // same as q4_K
+ const uint16_t * scales = (const uint16_t *)bq5_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 < QR5_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_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
+}
+
+} // namespace ggml_iq
diff --git a/exllamav3/exllamav3_ext/hc_mix_q8r.cuh b/exllamav3/exllamav3_ext/hc_mix_q8r.cuh
index 7080eff..4dc3385 100644
--- a/exllamav3/exllamav3_ext/hc_mix_q8r.cuh
+++ b/exllamav3/exllamav3_ext/hc_mix_q8r.cuh
@@ -39,6 +39,21 @@
#ifndef GRR_FIN_UNROLL
#define GRR_FIN_UNROLL 1
#endif
+// Dots: preload a thread's weight words (H x GRR_PF_C columns x JT rows) before the math when D / 8 <=
+// GRR_PF_C * GR_THREADS_A and the register cost H * GRR_PF_C * JT int2 stays <= 32. Bit-identical.
+#ifndef GRR_DOTS_PF
+#define GRR_DOTS_PF 1
+#endif
+#ifndef GRR_PF_C
+#define GRR_PF_C 3
+#endif
+// Finalize: preload a lane's up words (GRR_FIN_PF_I rank steps x H) when LR <= 32 * GRR_FIN_PF_I. Bit-identical.
+#ifndef GRR_FIN_PF
+#define GRR_FIN_PF 1
+#endif
+#ifndef GRR_FIN_PF_I
+#define GRR_FIN_PF_I 10
+#endif
template <int RT> struct GrrJ { static constexpr int v = RT == 1 ? GRR_J1 : RT == 2 ? GRR_J2 : RT == 3 ? GRR_J3 : GRR_J4; };
template <int H, int RT, int JT>
@@ -64,6 +79,24 @@ void gr_dots_q8r_kernel
const int lane = threadIdx.x % 32;
const int warp = threadIdx.x / 32;
+ constexpr int PF_C = (GRR_DOTS_PF && H * GRR_PF_C * JT <= 32) ? GRR_PF_C : 1;
+ const bool pf = PF_C > 1 && !sq && !gs && D4 / 2 <= PF_C * GR_THREADS_A;
+ int2 pre[H][PF_C][JT];
+ if (pf)
+ {
+ #pragma unroll
+ for (int h = 0; h < H; ++h)
+ #pragma unroll
+ for (int k = 0; k < PF_C; ++k)
+ #pragma unroll
+ for (int u = 0; u < JT; ++u)
+ {
+ const int c = threadIdx.x + k * GR_THREADS_A;
+ const int2* f8 = (const int2*) (fn + ((size_t) min(j0 + u, M - 1) * H + h) * D);
+ pre[h][k][u] = c < D4 / 2 ? f8[c] : make_int2(0, 0);
+ }
+ }
+
#pragma unroll
for (int h = 0; h < H; ++h)
{
@@ -72,7 +105,40 @@ void gr_dots_q8r_kernel
for (int u = 0; u < JT; ++u)
#pragma unroll
for (int q = 0; q < RT; ++q) a[u][q] = 0.0f;
- if (!sq)
+ if (pf)
+ {
+ #pragma unroll
+ for (int k = 0; k < PF_C; ++k)
+ {
+ const int c = threadIdx.x + k * GR_THREADS_A;
+ if (c >= D4 / 2) break;
+ float wv[JT][8];
+ #pragma unroll
+ for (int u = 0; u < JT; ++u)
+ {
+ unpack_s8x4((uint32_t) pre[h][k][u].x, wv[u][0], wv[u][1], wv[u][2], wv[u][3]);
+ unpack_s8x4((uint32_t) pre[h][k][u].y, wv[u][4], wv[u][5], wv[u][6], wv[u][7]);
+ }
+ #pragma unroll
+ for (int q = 0; q < RT; ++q)
+ {
+ const float4* s4 = (const float4*) (streams + (size_t) q * H * D);
+ float4 s0 = s4[(size_t) h * D4 + 2 * c];
+ float4 s1 = s4[(size_t) h * D4 + 2 * c + 1];
+ #pragma unroll
+ for (int u = 0; u < JT; ++u)
+ {
+ float x = a[u][q];
+ x = fmaf(s0.x, wv[u][0], x); x = fmaf(s0.y, wv[u][1], x);
+ x = fmaf(s0.z, wv[u][2], x); x = fmaf(s0.w, wv[u][3], x);
+ x = fmaf(s1.x, wv[u][4], x); x = fmaf(s1.y, wv[u][5], x);
+ x = fmaf(s1.z, wv[u][6], x); x = fmaf(s1.w, wv[u][7], x);
+ a[u][q] = x;
+ }
+ }
+ }
+ }
+ else if (!sq)
{
const int2* f8[JT];
#pragma unroll
@@ -230,6 +296,44 @@ void gr_finalize_q8r_kernel
#pragma unroll
for (int h = 0; h < H; ++h) g[q][h] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 gsc[H];
+ constexpr int FI = GRR_FIN_PF ? GRR_FIN_PF_I : 1;
+ if (GRR_FIN_PF && RT <= 2 && !gs && LR <= 32 * FI) // RT >= 3: the register cost loses (op bench R=3 +4 %, R=4 +15 %)
+ {
+ uint32_t ur[FI][H];
+ #pragma unroll
+ for (int k = 0; k < FI; ++k)
+ #pragma unroll
+ for (int h = 0; h < H; ++h)
+ {
+ const int i = lane + 32 * k;
+ ur[k][h] = i < LR ? *(const uint32_t*) (upt + ((((size_t) h * D4 + c) * LR + i) * 4)) : 0u;
+ }
+ #pragma unroll
+ for (int k = 0; k < FI; ++k)
+ {
+ const int i = lane + 32 * k;
+ if (i >= LR) break;
+ float ti[RT];
+ #pragma unroll
+ for (int q = 0; q < RT; ++q) ti[q] = t_s[q * LR + i];
+ #pragma unroll
+ for (int h = 0; h < H; ++h)
+ {
+ float u0, u1, u2, u3;
+ unpack_s8x4(ur[k][h], u0, u1, u2, u3);
+ #pragma unroll
+ for (int q = 0; q < RT; ++q)
+ {
+ g[q][h].x = fmaf(ti[q], u0, g[q][h].x);
+ g[q][h].y = fmaf(ti[q], u1, g[q][h].y);
+ g[q][h].z = fmaf(ti[q], u2, g[q][h].z);
+ g[q][h].w = fmaf(ti[q], u3, g[q][h].w);
+ }
+ }
+ }
+ }
+ else
+ {
PORT_UNROLL(GRR_FIN_UNROLL)
for (int i = lane; i < LR; i += 32)
{
@@ -258,6 +362,7 @@ void gr_finalize_q8r_kernel
}
}
}
+ }
#pragma unroll
for (int q = 0; q < RT; ++q)
#pragma unroll
diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.py
index b2e8768..7db0fd1 100644
--- a/exllamav3/modules/block_sparse_mlp.py
+++ b/exllamav3/modules/block_sparse_mlp.py
@@ -215,8 +215,20 @@ def _aligned_expert_layout(flat: torch.Tensor, num_tokens: int, top_k: int, num_
_SHGATE_FUSE = os.environ.get("EXL3_SHGATE_FUSE", "1") != "0"
+# EXL3_MOE_SH_FUSE (default 1 on HIP, 0 on CUDA): a Q8_0 shared expert runs inside the grouped q8_1 decode MoE launches (extra slot per row,
+# gate in the combine): 5 launches less per MoE layer; 0 keeps the separate shared-expert path
+# Default on for HIP only: Strix Halo 35B-A3B N4 +6 %, N8 +5 %; on the RTX 3090 it measured N4 -1.4 % (8 sessions), so CUDA keeps the separate path.
+_MOE_SH_FUSE = os.environ.get("EXL3_MOE_SH_FUSE", "1" if torch.version.hip else "0") != "0"
+_MOE_SH_SAID = [False]
# MCPM_FQ_MOE_IQ (default 1): IQ3_XXS / IQ2_S experts take the grouped q8_1 decode MoE (fq_moe_kq.cu FMT 4 / 5)
_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")
+# 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"))
@dataclass
class FusedBuffers:
@@ -1765,6 +1777,8 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
# gate/up share one format, down has its own (unsloth UD: gate/up Q4_K + down Q5_K)
fmt = fmt_d = None
_ug_ok = ("q4_k", "q5_k", "q6_k", "iq4_xs") + (("iq3_xxs", "iq2_s") if _FQ_MOE_IQ else ())
+ if _FQ_MOE_IQ and _MQ_MOE:
+ _ug_ok = _ug_ok + ("iq2_xs", "iq3_s", "iq4_nl", "iq2_xxs")
for m in self.gates + self.ups:
if not isinstance(m, ForeignLinear) or m.format not in _ug_ok:
return None
@@ -1782,6 +1796,8 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
fmt_d = self.downs[0].format
if any(m.format != fmt for m in self.gates + self.ups) or any(m.format != fmt_d for m in self.downs):
return None
+ if (fmt in ("iq2_xs", "iq3_s", "iq4_nl", "iq2_xxs") or fmt_d in ("iq2_xs", "iq3_s", "iq4_nl", "iq2_xxs")) and not (fmt in _MQ_ID and fmt_d in _MQ_ID):
+ return None # only the mmvq MoE kernel has these formats
if self.router_pre_norm is not None or self.routed_pre_norm is not None:
return None
if self.routed_post_norm is not None:
@@ -1941,7 +1957,7 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
return final_hidden_states
def _forward_foreign_grouped_kq(self, x, y, selected_experts, routing_weights,
- params, out_dtype, eshape):
+ params, out_dtype, eshape, big=False):
"""Grouped KQ MoE for SwiGLU decode, R = 1..FQ_GROUPED_MAX_ROWS rows (one per user / session). One fq_moe kernel
runs all R x top-k assignments (gate+up dots, torch-half SwiGLU, down dots, per-row weighted fold), replacing the
per-expert torch loop (host-synced expert counts, ~10x slower from the second row). Returns routed+shared sum or None."""
@@ -1951,7 +1967,7 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
fmt, fmt_d = fmts
S = self.num_experts_per_tok
R = y.shape[0]
- if R > FQ_GROUPED_MAX_ROWS or tuple(selected_experts.shape) != (R, S):
+ if (R > FQ_GROUPED_MAX_ROWS and not (big and fmt in _MQ_ID and fmt_d in _MQ_ID and R <= _MQ_MOE_MAXR)) or tuple(selected_experts.shape) != (R, S):
return None
sel = selected_experts.reshape(-1)
wts = routing_weights.reshape(-1)
@@ -1964,7 +1980,9 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
h = y.reshape(R, -1)
dev = h.device
K = h.shape[1]
- _BB = {"q4_k": 144, "q5_k": 176, "q6_k": 210, "iq4_xs": 136, "iq3_xxs": 98, "iq2_s": 82, "q8_0": 34, "q5_1": 24}
+ _BB = {"q4_k": 144, "q5_k": 176, "q6_k": 210, "iq4_xs": 136, "iq3_xxs": 98, "iq2_s": 82, "q8_0": 34, "q5_1": 24,
+ "iq2_xs": 74, "iq3_s": 110, "iq4_nl": 18, "iq2_xxs": 66}
+ _QK = lambda f: 32 if f in ("q8_0", "q5_1", "iq4_nl") else 256
bb = _BB[fmt]
bb_d = _BB[fmt_d]
tbl = getattr(self, "_fq_kq_ptrs", None)
@@ -1979,23 +1997,37 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
self._fq_kq_ptrs = tbl
_, gate_ptrs, up_ptrs, down_ptrs, _ = tbl
I = self.intermediate_size
- gate_stride = (K // 256) * bb
+ gate_stride = (K // _QK(fmt)) * bb
up_stride = gate_stride
- down_stride = (I // (32 if fmt_d in ("q8_0", "q5_1") else 256)) * bb_d
+ down_stride = (I // _QK(fmt_d)) * bb_d
# buffers sized once for the largest row count (stable pointers for CUDA graphs); the kernel gets row-prefix views
+ _GR = max(FQ_GROUPED_MAX_ROWS, _MQ_MOE_MAXR) if (_MQ_MOE and fmt in _MQ_ID and fmt_d in _MQ_ID) else FQ_GROUPED_MAX_ROWS
if getattr(self, "_fq_kq_partial", None) is None or \
- self._fq_kq_partial.shape != (FQ_GROUPED_MAX_ROWS * S, K) or self._fq_kq_partial.device != dev:
- # partial sums: scratch consumed inside this call -> one buffer per (device, shape) for every MoE layer
- key = (str(dev), FQ_GROUPED_MAX_ROWS * S, K)
+ self._fq_kq_partial.shape != (_GR * (S + 1), K) or self._fq_kq_partial.device != dev:
+ # partial sums: scratch consumed inside this call -> one buffer per (device, shape) for every MoE layer;
+ # + one row per token for the shared expert fused into the same launches (_fq_sh_fused)
+ key = (str(dev), _GR * (S + 1), K)
if key not in _FQ_KQ_PARTIAL:
- _FQ_KQ_PARTIAL[key] = torch.empty((FQ_GROUPED_MAX_ROWS * S, K), dtype=torch.float, device=dev)
+ _FQ_KQ_PARTIAL[key] = torch.empty((_GR * (S + 1), K), dtype=torch.float, device=dev)
self._fq_kq_partial = _FQ_KQ_PARTIAL[key]
- self._fq_kq_routed = torch.empty((FQ_GROUPED_MAX_ROWS, K), dtype=torch.float, device=dev)
+ self._fq_kq_routed = torch.empty((_GR, K), dtype=torch.float, device=dev)
partial_v = self._fq_kq_partial[:R * S]
+ # CUDA IQ: the llama.cpp-style mmvq grouped MoE wins; the shared expert then runs on its own path (sh None)
+ use_mq = _MQ_MOE and fmt in _MQ_ID and fmt_d in _MQ_ID and K % _QK(fmt) == 0 and I % _QK(fmt_d) == 0 and I % 32 == 0
+ sh = None if use_mq else (self._fq_sh_fused(x, h, K, I, fmt, fmt_d) if h.data_ptr() == x.data_ptr() and h.dtype == x.dtype else None)
routed_v = self._fq_kq_routed[:R]
_FN = {"q4_k": "fq_moe_q4k_grouped_q8", "q5_k": "fq_moe_q5k_grouped_q8",
"q6_k": "fq_moe_q6k_grouped_q8", "iq4_xs": "fq_moe_iq4xs_grouped_q8"}
- if fmt == fmt_d and fmt in _FN:
+ if use_mq:
+ ext.fq_moe_mmvq_grouped(h, sel, wts, gate_ptrs, up_ptrs, down_ptrs, gate_stride, up_stride, down_stride,
+ _MQ_ID[fmt], _MQ_ID[fmt_d], partial_v, routed_v)
+ elif sh is not None:
+ # shared expert in the same launches (extra slot per row); its gate row and the add are in the combine
+ _FID = {"q4_k": 0, "q5_k": 1, "q6_k": 2, "iq4_xs": 3, "iq3_xxs": 4, "iq2_s": 5, "q8_0": 6, "q5_1": 7}
+ ext.fq_moe_kq_grouped_mixed_sh_q8(h, sel, wts, gate_ptrs, up_ptrs, down_ptrs,
+ gate_stride, up_stride, down_stride, _FID[fmt], _FID[fmt_d],
+ self._fq_kq_partial[:R * (S + 1)], routed_v, *sh)
+ elif fmt == fmt_d and fmt in _FN:
getattr(ext, _FN[fmt])(h, sel, wts, gate_ptrs, up_ptrs, down_ptrs,
gate_stride, up_stride, down_stride,
partial_v, routed_v)
@@ -2005,7 +2037,7 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
gate_stride, up_stride, down_stride, _FID[fmt], _FID[fmt_d],
partial_v, routed_v)
final_hidden_states = routed_v.view(x.shape) if R > 1 else routed_v # rows -> [jobs, q, K] like the per-expert path
- if self.shared_experts is not None:
+ if self.shared_experts is not None and sh is None:
se_params = {**params, "moe_valu": True} if _MOE_VALU else params
if R == 1 and h.data_ptr() == x.data_ptr() and h.dtype == x.dtype:
# the routed call above left q8_1(x) in fq_q8_scratch(dev, 1, K): the shared gate+up reads it (mlp._fq_gu_cat)
@@ -2035,6 +2067,42 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
final_hidden_states = final_hidden_states.to(out_dtype)
return final_hidden_states
+ def _fq_sh_fused(self, x, h, K, I, fmt, fmt_d):
+ """(sh_gate, sh_up, sh_down, sh_wg) when the shared expert can run inside the grouped routed launches: Q8_0 gate / up /
+ down of the routed intermediate size, half input, optional scalar sigmoid gate (fp32 row), no post-norm; else None.
+ EXL3_MOE_SH_FUSE=0 keeps the separate shared-expert path."""
+ if not _MOE_SH_FUSE or not hasattr(ext, "fq_moe_kq_grouped_mixed_sh_q8"):
+ return None
+ se = self.shared_experts
+ if se is None or self.shared_experts_post_norm is not None or h.dtype != torch.half or not h.is_contiguous():
+ return None
+ c = getattr(self, "_fq_sh_cache", None)
+ if c is None:
+ c = False
+ try:
+ g, u, d = se.gates[0], se.ups[0], se.downs[0]
+ if len(se.gates) == 1 and all(getattr(m, "format", None) == "q8_0" and getattr(m, "packed", None) is not None
+ for m in (g, u, d)) and \
+ g.in_features == K and u.in_features == K and g.out_features == I and u.out_features == I and \
+ d.in_features == I and d.out_features == K:
+ wg = torch.empty(0, dtype = torch.float, device = h.device)
+ ok = True
+ if self.shared_gate is not None:
+ w = self.shared_gate.inner.weight
+ ok = w.dim() == 2 and w.shape[1] == 1 and w.shape[0] == K
+ if ok:
+ wg = w.to(torch.float).reshape(-1).contiguous()
+ if ok:
+ c = (g.packed.reshape(-1).contiguous(), u.packed.reshape(-1).contiguous(),
+ d.packed.reshape(-1).contiguous(), wg)
+ if not _MOE_SH_SAID[0]:
+ _MOE_SH_SAID[0] = True
+ print(" -- MoE shared expert fused into the grouped launches", flush = True)
+ except AttributeError:
+ c = False
+ self._fq_sh_cache = c
+ return c or None
+
def _shgate_hip(self, x):
"""sigmoid(x . w_gate) for the shared expert gate: a (1, K) x (K, 1) fp32 matmul costs ~64 us on ROCm (hipBLAS GEMV), an
elementwise product + row sum over a cached fp32 row ~14 us (Strix, K 2048). EXL3_SHGATE_MULSUM=0 restores the matmul."""
@@ -2378,6 +2446,12 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
if grouped is not None:
return grouped
+ if _MQ_MOE and FQ_GROUPED_MAX_ROWS < bsz <= _MQ_MOE_MAXR and not params.get("activate_all_experts") and not params.get("reconstruct") and not params.get("autosplit_measure") and not params.get("dflash_verify"):
+ grouped = self._forward_foreign_grouped_kq(
+ x, y, selected_experts, routing_weights, params, out_dtype, eshape, big=True)
+ if grouped is not None:
+ return grouped
+
# Lean foreign prefill (Bolt latent MoE, rows > 1): same values as
# the dense per-expert torch path, minus the EXL3 tier scaffolding
# (see _forward_foreign_prefill). Strict eligibility inside; falls
diff --git a/exllamav3/modules/foreign_linear.py b/exllamav3/modules/foreign_linear.py
index 373a505..10df8f9 100644
--- a/exllamav3/modules/foreign_linear.py
+++ b/exllamav3/modules/foreign_linear.py
@@ -69,6 +69,16 @@ _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,
+# 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_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}
+_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
+# on every call (1.7 s for the 8B at ANY prompt length). Past ~64 rows the dequant + GEMM path is the better one again.
+_MMVQ_MAXR = int(os.environ.get("BOLT_FQ_MMVQ_MAXR", "64"))
_KQ_BLOCK_BYTES = {"q4_k": 144, "q5_k": 176, "q6_k": 210, "iq4_xs": 136}
_KQ_GEMM_ROWS = 16
_KQ_GEMM = os.environ.get("MCPM_FQ_KQ_GEMM", "1") != "0"
@@ -184,6 +194,11 @@ class ForeignLinear(Module):
N = self.out_features
assert 0 <= first and first + count <= N
dev = self.packed.device
+ if _MMVQ_ON and self.format in _DEQ_IQ_ID and self.in_features % 256 == 0 and hasattr(ext, "fq_dequant_iq"):
+ # native fp16 dequant of the IQ blocks (ggml convert.cu port); the torch gap_dequant below takes ~1.7 s for the whole 8B
+ out = torch.empty((count, self.in_features), dtype=torch.float16, device=dev)
+ ext.fq_dequant_iq(self.packed[first:first + count].reshape(count, -1).contiguous(), out, _DEQ_IQ_ID[self.format])
+ return out
if self.format in _GAP_FORMATS:
return gap_dequant(self.format, self.packed[first:first + count])
if self.format in _KQ_FORMATS:
@@ -243,7 +258,17 @@ class ForeignLinear(Module):
if not flat.is_contiguous():
flat = flat.contiguous()
out: Optional[torch.Tensor] = None
- if (_ROWS_TRITON and flat.dtype == torch.half and (_ROWS_TRITON_Q8 or self.format != "q8_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 == "q5_k" else _MMVQ_MAXR) and K % _MMVQ_QK.get(self.format, 256) == 0
+ and hasattr(ext, _MMVQ_FN[self.format])):
+ N = self.out_features
+ out = torch.empty((R, N), dtype=dtype if dtype in (torch.half, torch.float) else torch.half, device=dev)
+ dev_idx = dev.index if dev.index is not None else torch.cuda.current_device()
+ qb = ext.fq_q8_scratch(dev_idx, R, K)
+ ext.fq_quant_q8_1_rows(flat, qb)
+ getattr(ext, _MMVQ_FN[self.format])(qb, self.packed.reshape(N, -1), out)
+ if out.dtype != dtype:
+ 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 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)
diff --git a/exllamav3/version.py b/exllamav3/version.py
index 91c1a02..608978d 100644
--- a/exllamav3/version.py
+++ b/exllamav3/version.py
@@ -1 +1 @@
-__version__ = "1.8.18"
+__version__ = "1.8.19"
diff --git a/tests/test_mmvq_iq.py b/tests/test_mmvq_iq.py
new file mode 100644
index 0000000..95e3b9c
--- /dev/null
+++ b/tests/test_mmvq_iq.py
@@ -0,0 +1,158 @@
+"""llama.cpp mmvq-style IQ decode kernels (fq_mmvq.cu: fq_mmvq_<fmt> dense GEMV, fq_moe_mmvq_grouped MoE) on synthetic blocks of all 7 IQ formats.
+Reference = dequantized weights (fp32) times the q8_1-DEQUANTIZED activations (what the integer dots compute), so the only remaining difference
+is the float summation order: a correct port agrees to ~1e-5 relative, a wrong grid / sign / scale / index bit gives errors of order 1.
+Random bytes are valid blocks for every IQ format (only the half scale d is set to a sane value).
+Usage: python tests/test_mmvq_iq.py"""
+import sys
+
+import torch
+
+import exllamav3_ext as ext
+from exllamav3.foreign_quant import gap_formats
+
+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),
+}
+
+
+def make_weights(fmt, n, k, seed, dscale=1.0):
+ bb, qk, _ = FMTS[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)
+ w[:, :, 2:4] = d
+ return w.reshape(n, -1).cuda()
+
+
+def dequant(fmt, w, n, k):
+ bb, qk, _ = FMTS[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"):
+ out = torch.empty((n, k), dtype=torch.half, device="cuda")
+ getattr(ext, f"fq_dequant_{fmt}")(w, out)
+ return out.float()
+ return gap_formats.dequant(fmt, w).float().reshape(n, k)
+
+
+def q8_dequant(qb, m, k):
+ b = qb.reshape(m, k // 32, 36)
+ d = b[:, :, 0:2].contiguous().view(torch.half).float().reshape(m, k // 32, 1)
+ q = b[:, :, 4:].contiguous().view(torch.int8).float().reshape(m, k // 32, 32)
+ return (d * q).reshape(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"}
+worst_old = [0.0]
+
+
+def test_dense():
+ worst = 0.0
+ for fmt, (bb, qk, _) in FMTS.items():
+ for n, k in ((1024, 4096), (777, 2048), (512, 512)):
+ w = make_weights(fmt, n, k, 7)
+ wd = dequant(fmt, w, n, k)
+ for m in (1, 2, 3, 4, 5, 7, 8, 9, 16):
+ x = (torch.randn(m, k, generator=torch.Generator().manual_seed(m)) * 0.5).half().cuda()
+ qb = ext.fq_q8_scratch(torch.cuda.current_device(), m, k)
+ ext.fq_quant_q8_1_rows(x, qb)
+ ref = q8_dequant(qb, m, k) @ wd.t()
+ for dt in (torch.half, torch.float):
+ out = torch.empty((m, n), dtype=dt, device="cuda")
+ getattr(ext, f"fq_mmvq_{fmt}")(qb, w, out)
+ torch.cuda.synchronize()
+ rel = float((out.float() - ref).norm() / ref.norm())
+ tol = 3e-3 if dt == torch.half else 1e-3 # the fp16 dequant of the reference alone is ~3e-4
+ assert rel < tol, f"{fmt} [{n}x{k}] m={m} {dt}: rel err {rel:.6f}"
+ worst = max(worst, rel if dt == torch.float else 0.0)
+ old = OLD.get(fmt)
+ if old is not None and dt == torch.float:
+ # same integer dots as the previous kernels: only the float summation order differs
+ o = torch.empty((m, n), dtype=torch.float, device="cuda")
+ getattr(ext, old)(qb, w.reshape(n, k // 256, 66) if fmt == "iq2_xxs" else w, o)
+ torch.cuda.synchronize()
+ d = float((out - o).norm() / o.norm())
+ assert d < 2e-5, f"{fmt} [{n}x{k}] m={m}: differs from {old} by {d:.2e}"
+ worst_old[0] = max(worst_old[0], d)
+ print(f"dense {fmt}: ok")
+ print(f"dense worst fp32-out rel err vs dequant reference {worst:.2e}, vs previous q8 kernels {worst_old[0]:.2e}")
+
+
+def test_dequant():
+ for fmt, (bb, qk, i) in FMTS.items():
+ if fmt == "q5_k":
+ continue
+ n, k = 96, 1024
+ w = make_weights(fmt, n, k, 11)
+ ref = dequant(fmt, w, n, k).half()
+ out = torch.empty((n, k), dtype=torch.half, device="cuda")
+ ext.fq_dequant_iq(w, out, i)
+ torch.cuda.synchronize()
+ bad = int((out.view(torch.int16) != ref.view(torch.int16)).sum())
+ print(f"dequant {fmt}: {bad} of {n * k} fp16 values differ from the reference")
+ assert bad == 0, f"{fmt}: fq_dequant_iq differs from the reference"
+
+
+def moe_case(fug, fd, K, I, E, R, topk, seed):
+ bbu, qku, idu = FMTS[fug]
+ bbd, qkd, idd = FMTS[fd]
+ wg = [make_weights(fug, I, K, seed + e, 0.1) for e in range(E)]
+ wu = [make_weights(fug, I, K, seed + 100 + e, 0.1) for e in range(E)]
+ wd = [make_weights(fd, K, I, seed + 200 + e, 0.1) for e in range(E)]
+ Gd = [dequant(fug, w, I, K) for w in wg]; Ud = [dequant(fug, w, I, K) for w in wu]; Dd = [dequant(fd, w, K, I) for w in wd]
+ ptrs = lambda ws: torch.tensor([w.data_ptr() for w in ws], dtype=torch.int64, device="cuda")
+ g = torch.Generator().manual_seed(seed)
+ x = (torch.randn(R, K, generator=g) * 0.5).half().cuda()
+ sel = torch.stack([torch.randperm(E, generator=g)[:topk] for _ in range(R)]).cuda()
+ wts = torch.rand(R, topk, generator=g).half().cuda()
+ qb = ext.fq_q8_scratch(torch.cuda.current_device(), R, K).clone()
+ ext.fq_quant_q8_1_rows(x, qb)
+ xd = q8_dequant(qb, R, K)
+ ref = torch.zeros(R, K, device="cuda")
+ for r in range(R):
+ for j in range(topk):
+ e = int(sel[r, j])
+ gg = (xd[r] @ Gd[e].t()).half().float(); uu = (xd[r] @ Ud[e].t()).half().float()
+ act = (torch.sigmoid(gg) * gg * uu).half()
+ # the down dots see the q8_1 quantization of the half activations
+ aq = ext.fq_q8_scratch(torch.cuda.current_device(), 1, I).clone()
+ ext.fq_quant_q8_1_rows(act.reshape(1, I).contiguous(), aq)
+ ref[r] += float(wts[r, j]) * (q8_dequant(aq, 1, I)[0] @ Dd[e].t())
+ partial = torch.empty(R * topk, K, dtype=torch.float, device="cuda")
+ out = torch.empty(R, K, dtype=torch.float, device="cuda")
+ ext.fq_moe_mmvq_grouped(x, sel.reshape(-1).contiguous(), wts.reshape(-1).contiguous(), ptrs(wg), ptrs(wu), ptrs(wd),
+ wg[0].shape[1], wu[0].shape[1], wd[0].shape[1], idu, idd, partial, out)
+ torch.cuda.synchronize()
+ OLDID = {"iq4_xs": 3, "iq3_xxs": 4, "iq2_s": 5}
+ if fug in OLDID and fd in OLDID:
+ part2 = torch.empty_like(partial); out2 = torch.empty_like(out)
+ ext.fq_moe_kq_grouped_mixed_q8(x, sel.reshape(-1).contiguous(), wts.reshape(-1).contiguous(), ptrs(wg), ptrs(wu), ptrs(wd),
+ wg[0].shape[1], wu[0].shape[1], wd[0].shape[1], OLDID[fug], OLDID[fd], part2, out2)
+ torch.cuda.synchronize()
+ d = float((out - out2).norm() / out2.norm())
+ print(f" vs fq_moe_kq_grouped_mixed_q8: {d:.2e}")
+ assert d < 5e-3, f"differs from the previous MoE kernel by {d}"
+ return float((out - ref).norm() / ref.norm())
+
+
+def test_moe():
+ cases = [("iq2_s", "iq3_xxs"), ("iq2_s", "iq4_xs"), ("iq3_xxs", "iq3_xxs"), ("iq2_xs", "iq3_s"), ("iq4_nl", "iq4_nl"), ("iq2_xxs", "iq2_xxs"),
+ ("iq3_s", "iq2_xs"), ("iq4_xs", "iq4_xs")]
+ for fug, fd in cases:
+ for R in (1, 4, 8, 40):
+ rel = moe_case(fug, fd, 1024, 512, 8, R, 4, 3)
+ print(f"moe ug={fug} down={fd} R={R}: rel err {rel:.2e}")
+ assert rel < 5e-3, f"{fug}/{fd} R={R}: rel err {rel}"
+
+
+if __name__ == "__main__":
+ test_dequant()
+ test_dense()
+ test_moe()
+ print("PASS")
diff --git a/tests/test_moe_sh_fused.py b/tests/test_moe_sh_fused.py
new file mode 100644
index 0000000..40d9c2f
--- /dev/null
+++ b/tests/test_moe_sh_fused.py
@@ -0,0 +1,90 @@
+"""Shared expert fused into the grouped MoE decode launches (ext.fq_moe_kq_grouped_mixed_sh_q8) vs an fp32 reference on the
+dequantized weights: 8 routed experts (gate / up Q4_K, down Q8_0 id 6 = v1 down kernel, or Q4_K id 0 = v2 down kernel at I 768),
+plus a Q8_0 shared expert of the same intermediate size, with and without a sigmoid gate row; R 1 / 2. Also checks that the
+routed part equals the plain grouped call (the shared slots must not disturb it). BOLT_FQ_MOE_V2=0 covers the v1 upgate kernel.
+Run in the tree: [BOLT_FQ_MOE_V2=0] python test_moe_sh_fused.py"""
+import numpy as np
+import torch
+import torch.nn.functional as F
+from exllamav3.foreign_quant import gap_formats
+from exllamav3.foreign_quant.k_quants import dequant_q4_K_bulk
+
+E, K, topk = 8, 2560, 2
+
+
+def f16bytes(a):
+ return np.frombuffer(np.asarray(a, np.float16).tobytes(), np.uint8)
+
+
+def rand_q4k(rng, N, Kc):
+ nb = Kc // 256
+ b = rng.integers(0, 256, (N, nb, 144), dtype=np.uint8)
+ b[:, :, 0:2] = f16bytes(np.full(N * nb, 0.0004)).reshape(N, nb, 2)
+ b[:, :, 2:4] = f16bytes(np.full(N * nb, 0.0002)).reshape(N, nb, 2)
+ deq = dequant_q4_K_bulk(b.reshape(N * nb, 144)).reshape(N, Kc).astype(np.float32)
+ return b.reshape(N, nb * 144), deq
+
+
+def rand_q8(rng, N, Kc, d = 0.003):
+ nb = Kc // 32
+ b = rng.integers(0, 256, (N, nb, 34), dtype=np.uint8)
+ b[:, :, 0:2] = f16bytes(np.full(N * nb, d)).reshape(N, nb, 2)
+ t = torch.from_numpy(b.reshape(N, nb * 34)).cuda()
+ return b.reshape(N, nb * 34), gap_formats.dequant("q8_0", t).float().cpu().numpy()
+
+
+def act(hf, wg, wu):
+ return (F.silu(torch.tensor(hf @ wg.T)) * torch.tensor(hf @ wu.T)).half().float().numpy()
+
+
+def main():
+ global ext
+ from exllamav3.ext import exllamav3_ext as ext
+ bad = 0
+ for fmt_d, fid, I in (("q8_0", 6, 640), ("q4_k", 0, 768)):
+ rng = np.random.default_rng(fid + 11)
+ gates, ups, downs, Wg, Wu, Wd = [], [], [], [], [], []
+ for e in range(E):
+ g, gd = rand_q4k(rng, I, K); u, ud = rand_q4k(rng, I, K)
+ d, dd = rand_q4k(rng, K, I) if fmt_d == "q4_k" else rand_q8(rng, K, I)
+ gates.append(torch.from_numpy(g).cuda()); ups.append(torch.from_numpy(u).cuda()); downs.append(torch.from_numpy(d).cuda())
+ Wg.append(gd); Wu.append(ud); Wd.append(dd)
+ sg, sgd = rand_q8(rng, I, K, 0.0015); su, sud = rand_q8(rng, I, K, 0.0015); sd, sdd = rand_q8(rng, K, I)
+ shg, shu, shd = (torch.from_numpy(a).cuda().reshape(-1).contiguous() for a in (sg, su, sd))
+ gp = torch.tensor([t.data_ptr() for t in gates], dtype=torch.int64, device="cuda")
+ up = torch.tensor([t.data_ptr() for t in ups], dtype=torch.int64, device="cuda")
+ dp = torch.tensor([t.data_ptr() for t in downs], dtype=torch.int64, device="cuda")
+ gs = (K // 256) * 144
+ ds = downs[0].shape[1]
+ for R in (1, 2):
+ for gated in (True, False):
+ h = (torch.randn(R, K) * 0.5).half().cuda()
+ sel = torch.from_numpy(np.stack([rng.choice(E, topk, replace=False) for _ in range(R)])).long().cuda().reshape(-1)
+ wts = torch.rand(R * topk).half().cuda()
+ wrow = torch.from_numpy(rng.standard_normal(K).astype(np.float32) * 0.02).cuda() if gated else \
+ torch.empty(0, dtype=torch.float, device="cuda")
+ plain = torch.zeros(R, K, dtype=torch.float, device="cuda")
+ ext.fq_moe_kq_grouped_mixed_q8(h, sel, wts, gp, up, dp, gs, gs, ds, 0, fid,
+ torch.zeros(R * topk, K, device="cuda"), plain)
+ fused = torch.zeros(R, K, dtype=torch.float, device="cuda")
+ ext.fq_moe_kq_grouped_mixed_sh_q8(h, sel, wts, gp, up, dp, gs, gs, ds, 0, fid,
+ torch.zeros(R * (topk + 1), K, device="cuda"), fused, shg, shu, shd, wrow)
+ torch.cuda.synchronize()
+ hf = h.float().cpu().numpy()
+ ref_sh = np.zeros((R, K), np.float32)
+ for r in range(R):
+ gsc = 1.0 / (1.0 + np.exp(-float(hf[r] @ wrow.cpu().numpy()))) if gated else 1.0
+ ref_sh[r] = gsc * (act(hf[r], sgd, sud) @ sdd.T)
+ got_sh = (fused - plain).cpu().numpy() # the shared part the fused call added
+ rel = float(np.abs(got_sh - ref_sh).max() / (np.abs(ref_sh).max() + 1e-9))
+ l2 = float(np.linalg.norm(got_sh - ref_sh) / (np.linalg.norm(ref_sh) + 1e-9))
+ ok = l2 < 3e-2 and rel < 6e-2 and np.isfinite(fused.cpu().numpy()).all() and np.abs(ref_sh).max() > 0
+ bad += not ok
+ print(f"down {fmt_d} I {I} R {R} gate {gated}: shared part max rel {rel:.2e} L2 rel {l2:.2e} "
+ f"(|shared| max {np.abs(ref_sh).max():.3f}, |routed| max {plain.abs().max().item():.3f}) {'ok' if ok else 'BAD'}")
+ print("PASS" if bad == 0 else f"FAIL ({bad})")
+ return bad
+
+
+if __name__ == "__main__":
+ raise SystemExit(1 if main() else 0)