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.

Hy4: carry the grouped MoE kernel changes the loader needs (swiglu clamp, expert-major block order) into the extension sources

YanissAmzb3b239905264
diff --git a/doc/hy4.md b/doc/hy4.md
index 0bd70f8..e3adffd 100644
--- a/doc/hy4.md
+++ b/doc/hy4.md
@@ -30,10 +30,10 @@ The script uses the repository venv (`EXL3_VENV`, default `.venv`), builds three
The server stops at start when the MoE extension is missing and names this script (`HY4_ALLOW_SLOW=1` starts on the slow path anyway). A missing attention library prints a warning.
-Optional: a Q5_K copy of the output head for the MTP drafts (smaller than the Q8_0 default):
+Optional: a Q5_K copy of the output head for the MTP drafts (0.255 GB instead of 0.394 GB for Q8_0, which the server uses when no file is given). It needs the `libggml-base.so` of a llama.cpp build:
```bash
-python tools/hy4/make_head_bin.py ./hy4/Hy4-flash-Q3_K-00001-of-00002.gguf ./hy4/head_q5_k.bin
+GGML_LIB=/path/to/libggml-base.so python tools/hy4/make_head_bin.py ./hy4/Hy4-flash-Q3_K-00001-of-00002.gguf ./hy4/head_q5_k.bin
export HY4_MTP_HEAD_BIN=$PWD/hy4/head_q5_k.bin
```
diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cpp
index 60ac6f0..c0df9fc 100644
--- a/exllamav3/exllamav3_ext/bindings.cpp
+++ b/exllamav3/exllamav3_ext/bindings.cpp
@@ -712,6 +712,7 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
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("partial"), py::arg("out"));
+ m.def("fq_moe_kq_set_swiglu_limit", &fq_moe_kq_set_swiglu_limit, "fq_moe_kq_set_swiglu_limit", py::arg("lim"));
m.def("fq_moe_kq_grouped_mixed_q8", &fq_moe_kq_grouped_mixed_q8, "fq_moe_kq_grouped_mixed_q8",
py::arg("h"), py::arg("sel"), py::arg("wts"),
py::arg("gate_ptrs"), py::arg("up_ptrs"), py::arg("down_ptrs"),
diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_api.h b/exllamav3/exllamav3_ext/foreign_quant/fq_api.h
index 492c6d4..435ea97 100644
--- a/exllamav3/exllamav3_ext/foreign_quant/fq_api.h
+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_api.h
@@ -77,6 +77,7 @@ void fq_moe_iq4xs_grouped_q8(torch::Tensor h, torch::Tensor sel, torch::Tensor w
torch::Tensor down_ptrs,
int64_t gate_stride, int64_t up_stride, int64_t down_stride,
torch::Tensor partial, torch::Tensor out);
+void fq_moe_kq_set_swiglu_limit(double lim); // 0 = off; gate <= lim, |up| <= lim on routed slots of the grouped kq kernels
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,
diff --git a/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu b/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu
index 05c18d8..aa18be0 100644
--- a/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu
+++ b/exllamav3/exllamav3_ext/foreign_quant/fq_moe_kq.cu
@@ -19,6 +19,45 @@
namespace {
+// Optional swiglu clamp for the routed slots (gate <= lim, -lim <= up <= lim); 0 = off. Set per layer from python (fq_moe_kq_set_swiglu_limit).
+static float g_fq_moe_swiglu_lim = 0.0f;
+
+
+// expert-major block order: slot p of the sorted assignment list, tile t (tile-major so blocks of the same expert tile are adjacent)
+__device__ __forceinline__ void fq_moe_slot_tile(int b, int ntiles, int S, const int32_t* __restrict__ perm, int nslots, int& s, int& t)
+{
+ if (perm != nullptr)
+ {
+ const int p = b % nslots;
+ t = b / nslots;
+ s = (p < S) ? (int)perm[p] : p;
+ }
+ else
+ {
+ s = b / ntiles;
+ t = b % ntiles;
+ }
+}
+
+// one block: perm[rank of assignment i by (expert id, i)] = i
+__global__ void fq_moe_sort_kernel(const int64_t* __restrict__ sel, int32_t* __restrict__ perm, int S, int E)
+{
+ const int i = threadIdx.x;
+ if (i < S)
+ {
+ int64_t k = sel[i];
+ k = (k < 0 || k >= E) ? 0 : k;
+ int rank = 0;
+ for (int j = 0; j < S; j++)
+ {
+ int64_t kj = sel[j];
+ kj = (kj < 0 || kj >= E) ? 0 : kj;
+ rank += (kj < k) || (kj == k && j < i);
+ }
+ perm[rank] = i;
+ }
+}
+
// Up+gate kernel: block (s, t) computes up to 8 gate rows and 8 up rows of
// expert sel[s] against the pre-quantized h (aqb, one row), SwiGLU epilogue
// (silu(gate) * up, torch half path) into act[s]. Lane mapping: group grp
@@ -36,11 +75,12 @@ __global__ void fq_moe_kq_upgate_kernel(const uint8_t* __restrict__ aqb,
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)
+ int64_t sh_stride = 0, float lim = 0.0f,
+ const int32_t* __restrict__ perm = nullptr, int nslots = 0)
{
int b = blockIdx.x;
- int s = b / ntiles_u;
- int t = b % ntiles_u;
+ int s, t;
+ fq_moe_slot_tile(b, ntiles_u, S, perm, nslots, s, t);
int grp = threadIdx.x / FQ_GEMV_LANES;
int lane = threadIdx.x % FQ_GEMV_LANES;
const bool shs = s >= S; // shared-expert slot of row s - S (sh_gate set): Q8_0 gate / up
@@ -114,6 +154,7 @@ __global__ void fq_moe_kq_upgate_kernel(const uint8_t* __restrict__ aqb,
// silu in float, product back to half.
float gh = __half2float(__float2half_rn(sg));
float uh = __half2float(__float2half_rn(su));
+ if (lim > 0.0f && s < S) { gh = fminf(gh, lim); uh = fminf(fmaxf(uh, -lim), lim); } // swiglu clamp (routed slots only)
float sig = 1.0f / (1.0f + expf(-gh));
act[(int64_t)s * I + n] = __float2half_rn(sig * gh * uh);
}
@@ -136,12 +177,13 @@ __global__ void __launch_bounds__(256) fq_moe_kq_upgate_v2_kernel(const uint8_t*
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)
+ int64_t sh_stride = 0, float lim = 0.0f,
+ const int32_t* __restrict__ perm = nullptr, int nslots = 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;
+ int s, t;
+ fq_moe_slot_tile(blockIdx.x, ntiles_u, S, perm, nslots, s, t);
const int r = threadIdx.x >> 3, j = threadIdx.x & 7;
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];
@@ -202,6 +244,7 @@ __global__ void __launch_bounds__(256) fq_moe_kq_upgate_v2_kernel(const uint8_t*
}
float gh = __half2float(__float2half_rn(sg));
float uh = __half2float(__float2half_rn(su));
+ if (lim > 0.0f && s < S) { gh = fminf(gh, lim); uh = fminf(fmaxf(uh, -lim), lim); } // swiglu clamp (routed slots only)
float sig = 1.0f / (1.0f + expf(-gh));
act[(int64_t)s * I + n] = __float2half_rn(sig * gh * uh);
}
@@ -219,11 +262,12 @@ __global__ void __launch_bounds__(256) fq_moe_kq_down_v2_kernel(const uint8_t* _
float* __restrict__ partial,
int S, int I, int K, int E, int ntiles_d,
const uint8_t* __restrict__ sh_down = nullptr,
- int64_t sh_stride = 0)
+ int64_t sh_stride = 0,
+ const int32_t* __restrict__ perm = nullptr, int nslots = 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;
+ int s, t;
+ fq_moe_slot_tile(blockIdx.x, ntiles_d, S, perm, nslots, s, t);
const int r = threadIdx.x >> 3, j = threadIdx.x & 7;
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];
@@ -280,16 +324,23 @@ __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,
- int Sg = 0, const uint8_t* sh_down = nullptr, int64_t sh_stride = 0)
+ int Sg = 0, const uint8_t* sh_down = nullptr, int64_t sh_stride = 0,
+ const int32_t* perm = nullptr)
{
if constexpr (FMT <= 2)
{
const int nt2 = (K + FQ_MOE_V2_ROWS - 1) / FQ_MOE_V2_ROWS;
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);
+ sh_down, sh_stride, perm, Sg ? Sg : S);
}
}
+static bool fq_moe_sort_on()
+{
+ const char* e = std::getenv("HY4_MOE_SORT"); // read at every launch (graph capture time), so arms can toggle it
+ return e != nullptr && e[0] == '1';
+}
+
static bool fq_moe_v2_on()
{
static const char* e = std::getenv("BOLT_FQ_MOE_V2");
@@ -349,11 +400,12 @@ __global__ void fq_moe_kq_down_kernel(const uint8_t* __restrict__ aqb,
float* __restrict__ partial,
int S, int I, int K, int E, int ntiles_d,
const uint8_t* __restrict__ sh_down = nullptr,
- int64_t sh_stride = 0)
+ int64_t sh_stride = 0,
+ const int32_t* __restrict__ perm = nullptr, int nslots = 0)
{
int b = blockIdx.x;
- int s = b / ntiles_d;
- int t = b % ntiles_d;
+ int s, t;
+ fq_moe_slot_tile(b, ntiles_d, S, perm, nslots, s, t);
int grp = threadIdx.x / FQ_GEMV_LANES;
int lane = threadIdx.x % FQ_GEMV_LANES;
const bool shs = s >= S; // shared-expert slot (act row s): Q8_0 down
@@ -486,6 +538,15 @@ static void fq_moe_kq_grouped_launch(torch::Tensor h, torch::Tensor sel, torch::
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);
+ torch::Tensor perm_t;
+ const int32_t* perm = nullptr;
+ if (R > 1 && S <= 128 && fq_moe_sort_on())
+ {
+ perm_t = torch::empty({S}, torch::dtype(torch::kInt).device(h.device()));
+ int32_t* perm_w = perm_t.data_ptr<int32_t>();
+ perm = perm_w;
+ fq_moe_sort_kernel<<<1, 128, 0, stream>>>(sel.data_ptr<int64_t>(), perm_w, S, E);
+ }
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(), Sg, I);
@@ -509,7 +570,7 @@ static void fq_moe_kq_grouped_launch(torch::Tensor h, torch::Tensor sel, torch::
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, sh.gate, sh.up, sh.ug_stride);
+ S, I, K, E, nt2, topk, sh.gate, sh.up, sh.ug_stride, g_fq_moe_swiglu_lim, perm, Sg);
}
else
fq_moe_kq_upgate_kernel<FMT_UG><<<Sg * ntiles_u, FQ_GEMV_THREADS, shmem_u, stream>>>(
@@ -518,7 +579,7 @@ static void fq_moe_kq_grouped_launch(torch::Tensor h, torch::Tensor sel, torch::
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, sh.gate, sh.up, sh.ug_stride);
+ S, I, K, E, ntiles_u, topk, sh.gate, sh.up, sh.ug_stride, g_fq_moe_swiglu_lim, perm, Sg);
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;
@@ -526,12 +587,12 @@ static void fq_moe_kq_grouped_launch(torch::Tensor h, torch::Tensor sel, torch::
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, Sg, sh.down, sh.d_stride);
+ down_stride, partial.data_ptr<float>(), S, I, K, E, stream, Sg, sh.down, sh.d_stride, perm);
else
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, sh.down, sh.d_stride);
+ down_stride, partial.data_ptr<float>(), S, I, K, E, ntiles_d, sh.down, sh.d_stride, perm, Sg);
int nblocks = (K + 255) / 256;
fq_moe_kq_combine_kernel<<<dim3(nblocks, R), 256, 0, stream>>>(
partial.data_ptr<float>(),
@@ -630,6 +691,8 @@ static void fq_moe_kq_grouped_mixed_impl(torch::Tensor h, torch::Tensor sel, tor
}
}
+void fq_moe_kq_set_swiglu_limit(double lim) { g_fq_moe_swiglu_lim = (float)lim; }
+
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,
diff --git a/exllamav3/loader/dense_req.py b/exllamav3/loader/dense_req.py
index d8ec039..a7cf71b 100644
--- a/exllamav3/loader/dense_req.py
+++ b/exllamav3/loader/dense_req.py
@@ -32,6 +32,8 @@ def quantize_rows(w, kind, nth=12, rows_per=64):
import ctypes
from concurrent.futures import ThreadPoolExecutor
if not _LIB:
+ if not os.path.isfile(GGML_LIB):
+ raise RuntimeError(f"ggml library not found ({GGML_LIB}): set GGML_LIB to the libggml-base.so of a llama.cpp build")
lib = ctypes.CDLL(GGML_LIB)
lib.ggml_quantize_chunk.restype = ctypes.c_size_t
lib.ggml_quantize_chunk.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int64, ctypes.c_int64, ctypes.c_int64, ctypes.c_void_p]