Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Add EXL3_DENSE_MPW2: dense EXL3 GEMM with in-GEMM decode for mid-size prefill forwards
YanissAmz37f25b863e52
diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cppindex 729f06d..62e568e 100644--- a/exllamav3/exllamav3_ext/bindings.cpp+++ b/exllamav3/exllamav3_ext/bindings.cpp@@ -451,6 +451,7 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)py::arg("tiles"), py::arg("tile_count"),py::arg("act_limit") = 0.0, py::arg("mcg") = false, py::arg("addend") = py::none());m.def("exl3_moe_prefill_gemm_test", &exl3_moe_prefill_gemm_test, "exl3_moe_prefill_gemm_test");+ m.def("exl3_dense_mpw2", &exl3_dense_mpw2, "exl3_dense_mpw2");m.def("routing_std_gfx12_bsz1", &routing_std_gfx12_bsz1,"routing_std_gfx12_bsz1");m.def("dsa_topk_gfx12", &dsa_topk_gfx12, "dsa_topk_gfx12",diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cuindex 453e50a..9b202df 100644--- a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu@@ -18,6 +18,7 @@#include <cuda_fp16.h>#endif#include <c10/cuda/CUDAGuard.h>+#include <ATen/ops/empty.h>#include <ATen/cuda/CUDAContext.h>#include "../util.h"#include "../util.cuh"@@ -1788,4 +1789,60 @@ void exl3_moe_prefill_gemm_testcuda_check(hipPeekAtLastError());}+// One-group tile metadata for exl3_dense_mpw2, built on the device from the row count (no host copy):+// expert 0 holds every row, m-tile i -> tile id i, table[0] = the trellis pointer.+__global__ void mpw_dense_metadata_kernel(int64_t* offs, int* tiles, int* num_tiles, int64_t* table,+ int64_t trellis, int rows, int mt)+{+ const int n = (rows + mt - 1) / mt;+ for (int i = threadIdx.x; i < n; i += blockDim.x) tiles[i] = i;+ if (threadIdx.x == 0) { offs[0] = 0; offs[1] = rows; *num_tiles = n; table[0] = trellis; }+}++// Dense EXL3 GEMM through the expert prefill kernel (EXL3_DENSE_MPW2, exl3.py): C = A @ W_inner, had domain, no+// input/output transforms, W decoded from the trellis per tile (no fp16 weight round trip). Same launch as the expert+// GEMM with one group; bounds proven for MT 128 / NW 2, N % 256 == 0.+void exl3_dense_mpw2+(+ const at::Tensor& A,+ at::Tensor& C,+ const at::Tensor& trellis,+ double K+)+{+ const at::cuda::OptionalCUDAGuard device_guard(A.device());+ hipStream_t stream = at::cuda::getCurrentCUDAStream().stream();+ TORCH_CHECK(A.dim() == 2 && A.stride(1) == 1 && A.dtype() == at::kHalf, "exl3_dense_mpw2: A must be fp16 [rows, K] rows");+ const int rows = A.size(0);+ const int size_k = A.size(1);+ const int a_ld = (int) A.stride(0);+ const int size_n = C.size(1);+ const int bits = (int) K;+ TORCH_CHECK(K == 3.0 || K == 4.0, "exl3_dense_mpw2: K must be 3 or 4");+ TORCH_CHECK(trellis.dtype() == at::kShort && trellis.is_contiguous() && trellis.dim() == 3 &&+ trellis.size(0) * 16 == size_k && trellis.size(1) * 16 == size_n && trellis.size(2) == 16 * bits,+ "exl3_dense_mpw2: trellis shape");+ TORCH_CHECK(C.dim() == 2 && C.size(0) == rows && C.is_contiguous() &&+ (C.dtype() == at::kHalf || C.dtype() == at::kFloat), "exl3_dense_mpw2: C shape");+ TORCH_CHECK(rows >= 1 && size_k % (mpw::KS * 16) == 0 && size_n % 256 == 0, "exl3_dense_mpw2: unsupported shape");+ const int kern = 2;+ const int mt = mpw_mt(kern);+ TORCH_CHECK(mt == 128 && mpw_nw(size_n) == 2, "exl3_dense_mpw2: needs MT 128 / NW 2 (MPW_MT, MPW_NW, EXL3_MPW2_VARIANT)");+ const int slots = tile_slots(rows, 1, kern);+ at::Tensor meta = at::empty({ 3 }, A.options().dtype(at::kLong));+ at::Tensor tmeta = at::empty({ slots + 1 }, A.options().dtype(at::kInt));+ int64_t* offs = (int64_t*) meta.data_ptr();+ int64_t* tt = offs + 2;+ int* tl = (int*) tmeta.data_ptr();+ int* tc = tl + slots;+ mpw_dense_metadata_kernel<<<1, 64, 0, stream>>>(offs, tl, tc, tt, (int64_t) trellis.data_ptr(), rows, mt);+ if (C.dtype() == at::kFloat)+ launch_gemm<true>((float) K, dim3(size_n / mpw::NT, slots, 1), stream,+ (const half*) A.data_ptr(), 0, offs, tl, tc, tt, tt, C.data_ptr(), 0, size_k, size_n, false, kern, a_ld);+ else+ launch_gemm<false>((float) K, dim3(size_n / mpw::NT, slots, 1), stream,+ (const half*) A.data_ptr(), 0, offs, tl, tc, tt, tt, C.data_ptr(), 0, size_k, size_n, false, kern, a_ld);+ cuda_check(hipPeekAtLastError());+}+#endif // USE_ROCMdiff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cuh b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cuhindex 90bd4c1..6b519f7 100644--- a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cuh+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cuh@@ -52,3 +52,12 @@ void exl3_moe_prefill_gemm_testat::Tensor& tiles,at::Tensor& tile_count);++// Dense EXL3 GEMM (one group) through the expert prefill kernel: C = A @ W_inner, had domain, K 3 or 4, N % 256 == 0+void exl3_dense_mpw2+(+ const at::Tensor& A, // fp16 [rows, K], unit column stride+ at::Tensor& C, // fp16 or fp32 [rows, N], contiguous+ const at::Tensor& trellis, // int16 [K / 16, N / 16, 16 * bits]+ double K+);diff --git a/exllamav3/modules/quant/exl3.py b/exllamav3/modules/quant/exl3.pyindex f78e870..969ae37 100644--- a/exllamav3/modules/quant/exl3.py+++ b/exllamav3/modules/quant/exl3.py@@ -109,6 +109,22 @@ def row_pad_pitch(lin, k: int, rows: int, params: dict) -> int:# NOTE: the knobs are read per call, not at import: the A/B harness (glm_base --ab-sets)# flips os.environ between variants inside one process, so an import-time read would# silently measure the base arm four times.+def _dense_mpw2_ok(lin, x, rows):+ """EXL3_DENSE_MPW2=<max rows> (default 0 = off): forwards of AUTO_RECONSTRUCT_THRESHOLD + 1 .. max rows run the big+ K 3 / 4 mul1 shapes (in * out >= EXL3_DENSE_MPW2_MINKN, default 16 M) through the expert prefill kernel as one group+ (ext.exl3_dense_mpw2): the weight is decoded per tile, no fp16 reconstruct. 29-32 TF against 16-23 TF for reconstruct ++ rocBLAS at 256-512 rows; small shapes and >= 1024 rows lose. Same inner product up to fp32+ summation order. Read per call: the A/B harnesses flip it in-process."""+ mx = int(os.environ.get("EXL3_DENSE_MPW2", "0"))+ if not mx or rows > mx or rows <= AUTO_RECONSTRUCT_THRESHOLD:+ return False+ return (bool(torch.version.hip) and hasattr(ext, "exl3_dense_mpw2") and x.is_contiguous()+ and lin.mul1 and not lin.mcg and lin.K in (3, 4)+ and lin.in_features % 32 == 0 and lin.out_features % 256 == 0+ and lin.in_features * lin.out_features >= int(os.environ.get("EXL3_DENSE_MPW2_MINKN", "16777216"))+ and lin.suh is not None and lin.svh is not None and lin.trellis.dtype == torch.int16)++def had_pf_fast():return os.environ.get("EXL3_HAD_PF_FAST", "0") != "0"@@ -589,6 +605,18 @@ class LinearEXL3:else:y_ = y.view(rows, self.out_features)+ # EXL3_DENSE_MPW2 (see _dense_mpw2_ok): the expert prefill kernel as a one-group GEMM on the trellis+ if _dense_mpw2_ok(self, x, rows):+ xh = torch.empty_like(x)+ ext.had_r_128(x, xh, self.suh, None, 1.0)+ ext.exl3_dense_mpw2(xh, y_, self.trellis, self.K)+ ext.had_r_128(y_, y_, None, self.svh, 1.0)+ if via_f16:+ y.view(rows, self.out_features).copy_(y_)+ if self.bias is not None:+ y += self.bias+ return y+# Fused path: reconstruct emits ORIGINAL-basis weights (both Hadamards + sign# vectors folded into the memory-bound reconstruct kernel), so the gemm runs on the# raw input and the standalone input/output had_r_128 launches disappear (~14% of@@ -602,7 +630,8 @@ class LinearEXL3:# The fused kernel costs ~4x plain reconstruct (k*n-proportional) while the saved# had launches scale with rows*(k+n); breakeven is rows ~400-900 across shapes- use_fused = self._fused_reconstruct and rows >= 1024+ # EXL3_FUSED_MIN_ROWS: the fused reconstruct was no slower than the unfused one on every GLM dense shape from 512 rows+ use_fused = self._fused_reconstruct and rows >= int(os.environ.get("EXL3_FUSED_MIN_ROWS", "1024"))if use_fused:xh = pad_copy_in(x, self.in_features)