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
MoE prefill gather: read the aligned layout's dummy rows as zeros
YanissAmz13bf7d1ada1b
diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cppindex e4cb1ee..729f06d 100644--- a/exllamav3/exllamav3_ext/bindings.cpp+++ b/exllamav3/exllamav3_ext/bindings.cpp@@ -439,6 +439,7 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)m.def("kda_inter_off", &kda_inter_off, "kda_inter_off");m.def("exl3_moe_gfx12_k3_prefill", &exl3_moe_gfx12_k3_prefill,"exl3_moe_gfx12_k3_prefill");+ m.attr("EXL3_MPW_SHORT_A") = 1; // (ROCm branch: the attr block near the samplers is CUDA-only) exl3_moe_prefill_wmma accepts A with fewer rows than selected (zero rows)m.def("exl3_moe_prefill_wmma", &exl3_moe_prefill_wmma, "exl3_moe_prefill_wmma",py::arg("A"), py::arg("output"), py::arg("selected"), py::arg("weights"),py::arg("order"), py::arg("expert_count"),diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cuindex 99f6bad..453e50a 100644--- a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu@@ -160,7 +160,8 @@ void mpw_gather_had_kernelint top_k,int width,int projections,- int lda // A row pitch (elements, >= width)+ int lda, // A row pitch (elements, >= width)+ int a_rows // rows of A; token rows past it (aligned-layout padding) read as zero){const int blocks = width / 128;@@ -171,7 +172,8 @@ void mpw_gather_had_kernelconst int blk = job % blocks;const int r = (job / blocks) % rows;const int proj = job / ((int64_t) rows * blocks);- const half4 v0 = ((const half4*) (A + (int64_t) r * lda + blk * 128))[t];+ const half4 v0 = r < a_rows ? ((const half4*) (A + (int64_t) r * lda + blk * 128))[t]+ : half4(__float2half2_rn(0.f), __float2half2_rn(0.f));for (int k = 0; k < top_k; ++k){const int64_t orig = (int64_t) r * top_k + k;@@ -328,7 +330,8 @@ void mpw_gather_had2_kernelint assignments,int top_k,int width,- int lda+ int lda,+ int a_rows){const int blocks = width / 128;@@ -347,7 +350,8 @@ void mpw_gather_had2_kernelp0_l = suh_0[e];p1_l = suh_1[e];}- const half4 v0 = ((const half4*) (A + (int64_t) r * lda + blk * 128))[t];+ const half4 v0 = r < a_rows ? ((const half4*) (A + (int64_t) r * lda + blk * 128))[t]+ : half4(__float2half2_rn(0.f), __float2half2_rn(0.f));#pragma unroll 5for (int k = 0; k < top_k; ++k){@@ -1638,11 +1642,14 @@ void exl3_moe_prefill_wmma// A may be a producer's row-padded view (exl3.row_pad_pitch): unit column stride, row pitch >= H, 8-byte rowsTORCH_CHECK(A.dtype() == at::kHalf && A.dim() == 2 && A.stride(1) == 1 && A.stride(0) >= A.size(1) &&A.stride(0) % 4 == 0, "A must be fp16 [rows, H] with unit column stride");- const int rows = A.size(0);+ // A may also be SHORTER than selected (EXL3_MPW_SHORT_A): rows past A.size(0) are the aligned layout's+ // dummy rows, gathered as zeros (the bytes torch.cat((y, zeros)) appended before)+ const int rows = selected.size(0);+ const int a_rows = A.size(0);const int H = A.size(1);const int lda = (int) A.stride(0);TORCH_CHECK(selected.dtype() == at::kLong && selected.is_contiguous() && selected.dim() == 2 &&- selected.size(0) == rows, "selected must be int64 [rows, top_k]");+ a_rows >= 1 && a_rows <= rows, "selected must be int64 [rows >= A rows, top_k]");const int top_k = selected.size(1);const int assignments = rows * top_k;const int64_t experts = gate_trellis.numel();@@ -1699,14 +1706,14 @@ void exl3_moe_prefill_wmmaconst int64_t g2jobs = (int64_t) (assignments / top_k) * (H / 128);mpw_gather_had2_kernel<<<CEIL_DIVIDE(g2jobs, 8), 256, 0, stream>>>((const half*) A.data_ptr(), (half*) gu_had.data_ptr(), sel, inv, P(gate_suh), P(up_suh),- assignments, top_k, H, lda);+ assignments, top_k, H, lda, a_rows);}else{const int64_t gjobs = (int64_t) 2 * (assignments / top_k) * (H / 128);mpw_gather_had_kernel<<<CEIL_DIVIDE(gjobs, 8), 256, 0, stream>>>((const half*) A.data_ptr(), (half*) gu_had.data_ptr(), sel, inv, P(gate_suh), P(up_suh),- assignments, top_k, H, 2, lda);+ assignments, top_k, H, 2, lda, a_rows);}launch_gemm<false>((float) K_gu, dim3(I / mpw::NT, slots, 2), stream,diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.pyindex f824cde..4515f1f 100644--- a/exllamav3/modules/block_sparse_mlp.py+++ b/exllamav3/modules/block_sparse_mlp.py@@ -1720,7 +1720,10 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):y_k, rw_k, sel_k = y, rw, selif _MOE_ALIGN_CHUNK and num_tokens > _MOE_ALIGN_CHUNK:order, flat, dummy = _aligned_expert_layout(flat, num_tokens, top_k, self.num_experts, _MOE_ALIGN_CHUNK)- y_k = torch.cat((y, torch.zeros((dummy, y.shape[1]), dtype = y.dtype, device = y.device)))+ # EXL3_MPW_SHORT_A (default 1, read per call, needs the ext flag): the gather reads the dummy rows as zeros,+ # so y is passed as is instead of copied whole to append them; same gathered bytes+ if not (hasattr(ext, "EXL3_MPW_SHORT_A") and os.environ.get("EXL3_MPW_SHORT_A", "1") != "0"):+ y_k = torch.cat((y, torch.zeros((dummy, y.shape[1]), dtype = y.dtype, device = y.device)))rw_k = torch.cat((rw, torch.zeros((dummy, top_k), dtype = rw.dtype, device = rw.device)))sel_k = flat.view(num_tokens + dummy, top_k)else: