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
Prefill: producers write GEMM-padded rows (EXL3_NC2_KDA_OPAD, EXL3_NC2_HC_YPAD[_MLP])
Yaniss7795693ee48b
diff --git a/exllamav3/exllamav3_ext/hc_mix.cu b/exllamav3/exllamav3_ext/hc_mix.cuindex 323eecf..a2d9f42 100644--- a/exllamav3/exllamav3_ext/hc_mix.cu+++ b/exllamav3/exllamav3_ext/hc_mix.cu@@ -312,7 +312,8 @@ __device__ __forceinline__ void hcn_finalize_bodyconst float constant_bias,const float constant_scale,- const int r+ const int r,+ const int ldy // y row pitch (elements, >= D)){constexpr int M = M_;@@ -422,7 +423,7 @@ __device__ __forceinline__ void hcn_finalize_bodyfor (int w_ = 0; w_ < NUM_THREADS / 32; ++w_) tot += nred[w_];const float nscale = rsqrtf(tot / (float) D + norm_eps) * constant_scale;- half* yr = y + (size_t) r * D;+ half* yr = y + (size_t) r * ldy;#pragma unrollfor (int j = 0; j < HCN_MAX; ++j){@@ -458,10 +459,11 @@ void hc_mix_finalize_norm_kernelconst int sinkhorn_iters,const float norm_eps,const float constant_bias,- const float constant_scale+ const float constant_scale,+ const int ldy){- hcn_finalize_body<H, M_>(streams, partials, base, scale, post, comb, y, w, wtype, D, chunksA, rms_eps, hc_eps, sinkhorn_iters, norm_eps, constant_bias, constant_scale, blockIdx.x);+ hcn_finalize_body<H, M_>(streams, partials, base, scale, post, comb, y, w, wtype, D, chunksA, rms_eps, hc_eps, sinkhorn_iters, norm_eps, constant_bias, constant_scale, blockIdx.x, ldy);}/*@@ -497,7 +499,8 @@ void hc_mix_norm_1pass_kernelconst int sinkhorn_iters,const float norm_eps,const float constant_bias,- const float constant_scale+ const float constant_scale,+ const int ldy){constexpr int M = M_;@@ -575,7 +578,7 @@ void hc_mix_norm_1pass_kernel__threadfence();if (threadIdx.x == 0) tickets[r] = 0;hcn_finalize_body<H, M_>(streams, partials, base, scale, post, comb, y, w, wtype, D, chunksA,- rms_eps, hc_eps, sinkhorn_iters, norm_eps, constant_bias, constant_scale, r);+ rms_eps, hc_eps, sinkhorn_iters, norm_eps, constant_bias, constant_scale, r, ldy);}@@ -624,7 +627,8 @@ void hc_pf_apply_mix_norm_1pass_kernelconst int sinkhorn_iters,const float norm_eps,const float constant_bias,- const float constant_scale+ const float constant_scale,+ const int ldy){constexpr int M = M_;@@ -761,7 +765,7 @@ void hc_pf_apply_mix_norm_1pass_kernel__threadfence();if (threadIdx.x == 0) tickets[r] = 0;hcn_finalize_body<H, M_>(sro, partials, base, scale, post, comb, y, w, wtype, D, 1,- rms_eps, hc_eps, sinkhorn_iters, norm_eps, constant_bias, constant_scale, r);+ rms_eps, hc_eps, sinkhorn_iters, norm_eps, constant_bias, constant_scale, r, ldy);}@@ -1422,6 +1426,14 @@ static void hc_check_commonTORCH_CHECK(tensor.is_contiguous(), op, ": ", name, " must be contiguous");}+// y of the mix_norm ops: (R, D) half with contiguous columns and a row pitch >= D (a producer-padded view, see+// exl3.row_pad_pitch); a contiguous y has pitch D.+static void hc_check_y_rows(const at::Tensor& y, const at::Device& device, const char* op)+{+ TORCH_CHECK(y.is_cuda() && y.device() == device, op, ": y must be on the same CUDA device");+ TORCH_CHECK(y.dim() == 2 && y.stride(1) == 1 && y.stride(0) >= y.size(1), op, ": y must be (R, D) with unit column stride");+}+static void hc_check_aligned(const at::Tensor& tensor, const char* op, const char* name){TORCH_CHECK(@@ -1677,12 +1689,13 @@ void hc_mix_normhc_check_common(partials, device, op, "partials");hc_check_common(post, device, op, "post");hc_check_common(comb, device, op, "comb");- hc_check_common(y, device, op, "y");+ hc_check_y_rows(y, device, op);TORCH_CHECK(streams.scalar_type() == at::kFloat && base.scalar_type() == at::kFloat &&scale.scalar_type() == at::kFloat && partials.scalar_type() == at::kFloat &&post.scalar_type() == at::kFloat && comb.scalar_type() == at::kFloat, op, ": dtype");TORCH_CHECK(fn.scalar_type() == at::kFloat || fn.scalar_type() == at::kHalf, op, ": fn dtype");- TORCH_CHECK(y.scalar_type() == at::kHalf && y.numel() == (int64_t) R * D, op, ": y must be half (R, D)");+ TORCH_CHECK(y.scalar_type() == at::kHalf && y.dim() == 2 && y.size(0) == R && y.size(1) == D, op, ": y must be half (R, D)");+ const int ldy = (int) y.stride(0);TORCH_CHECK(fn.dim() == 2 && fn.size(0) == M && fn.size(1) == H * D, op, ": fn shape");TORCH_CHECK(base.numel() == M && scale.numel() == 3 && post.numel() == R * H && comb.numel() == R * H * H, op, ": shapes");const int n_chunks_a = hc_mix_num_chunks(R, H * D);@@ -1714,7 +1727,7 @@ void hc_mix_normchunk_cols, tickets, (const float*) base.data_ptr(), (const float*) scale.data_ptr(), \(float*) post.data_ptr(), (float*) comb.data_ptr(), (half*) y.data_ptr(), \wtype ? w->data_ptr() : nullptr, wtype, D, (float) rms_eps, (float) hc_eps, (int) sinkhorn_iters, \- (float) norm_eps, (float) constant_bias, (float) constant_scale+ (float) norm_eps, (float) constant_bias, (float) constant_scale, ldyif (fn.dtype() == at::kHalf)hc_mix_norm_1pass_kernel<4, 24, half><<<grid_a, NUM_THREADS + 32, 0, stream>>>(HC1_ARGS(half));else@@ -1736,7 +1749,7 @@ void hc_mix_norm(float*) post.data_ptr(), (float*) comb.data_ptr(), (half*) y.data_ptr(),wtype ? w->data_ptr() : nullptr, wtype, D, n_chunks_a,(float) rms_eps, (float) hc_eps, (int) sinkhorn_iters,- (float) norm_eps, (float) constant_bias, (float) constant_scale);+ (float) norm_eps, (float) constant_bias, (float) constant_scale, ldy);cuda_check(cudaPeekAtLastError());}@@ -1881,14 +1894,15 @@ void hc_pf_apply_mix_normhc_check_common(partials, device, op, "partials");hc_check_common(post, device, op, "post");hc_check_common(comb, device, op, "comb");- hc_check_common(y, device, op, "y");+ hc_check_y_rows(y, device, op);TORCH_CHECK(x.scalar_type() == at::kFloat && post_a.scalar_type() == at::kFloat &&comb_a.scalar_type() == at::kFloat && base.scalar_type() == at::kFloat &&scale.scalar_type() == at::kFloat && partials.scalar_type() == at::kFloat &&post.scalar_type() == at::kFloat && comb.scalar_type() == at::kFloat, op, ": dtype");TORCH_CHECK(ya.scalar_type() == at::kFloat || ya.scalar_type() == at::kHalf, op, ": ya dtype");TORCH_CHECK(fn.scalar_type() == at::kFloat || fn.scalar_type() == at::kHalf, op, ": fn dtype");- TORCH_CHECK(y.scalar_type() == at::kHalf && y.numel() == (int64_t) R * D, op, ": y must be half (R, D)");+ TORCH_CHECK(y.scalar_type() == at::kHalf && y.dim() == 2 && y.size(0) == R && y.size(1) == D, op, ": y must be half (R, D)");+ const int ldy = (int) y.stride(0);TORCH_CHECK(ya.dim() == 2 && ya.size(0) == R && ya.size(1) == D, op, ": ya shape");TORCH_CHECK(post_a.dim() == 2 && post_a.size(0) == R && post_a.size(1) == H, op, ": post_a shape");TORCH_CHECK(comb_a.dim() == 3 && comb_a.size(0) == R && comb_a.size(1) == H &&@@ -1925,7 +1939,7 @@ void hc_pf_apply_mix_norm(const float*) base.data_ptr(), (const float*) scale.data_ptr(), \(float*) post.data_ptr(), (float*) comb.data_ptr(), (half*) y.data_ptr(), \wtype ? w->data_ptr() : nullptr, wtype, D, (float) rms_eps, (float) hc_eps, \- (int) sinkhorn_iters, (float) norm_eps, (float) constant_bias, (float) constant_scale+ (int) sinkhorn_iters, (float) norm_eps, (float) constant_bias, (float) constant_scale, ldyif (ya.dtype() == at::kHalf){if (fn.dtype() == at::kHalf)diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cuindex 6b8b083..99f6bad 100644--- a/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_prefill.cu@@ -159,7 +159,8 @@ void mpw_gather_had_kernelint assignments,int top_k,int width,- int projections+ int projections,+ int lda // A row pitch (elements, >= width)){const int blocks = width / 128;@@ -170,7 +171,7 @@ 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 * width + blk * 128))[t];+ const half4 v0 = ((const half4*) (A + (int64_t) r * lda + blk * 128))[t];for (int k = 0; k < top_k; ++k){const int64_t orig = (int64_t) r * top_k + k;@@ -326,7 +327,8 @@ void mpw_gather_had2_kernelconst int64_t* __restrict__ suh_1,int assignments,int top_k,- int width+ int width,+ int lda){const int blocks = width / 128;@@ -345,7 +347,7 @@ void mpw_gather_had2_kernelp0_l = suh_0[e];p1_l = suh_1[e];}- const half4 v0 = ((const half4*) (A + r * width + blk * 128))[t];+ const half4 v0 = ((const half4*) (A + (int64_t) r * lda + blk * 128))[t];#pragma unroll 5for (int k = 0; k < top_k; ++k){@@ -1633,9 +1635,12 @@ void exl3_moe_prefill_wmmaconst at::cuda::OptionalCUDAGuard device_guard(A.device());hipStream_t stream = at::cuda::getCurrentCUDAStream().stream();- TORCH_CHECK(A.dtype() == at::kHalf && A.is_contiguous() && A.dim() == 2, "A must be fp16 [rows, H]");+ // A may be a producer's row-padded view (exl3.row_pad_pitch): unit column stride, row pitch >= H, 8-byte rows+ TORCH_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);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]");const int top_k = selected.size(1);@@ -1694,14 +1699,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);+ assignments, top_k, H, lda);}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);+ assignments, top_k, H, 2, lda);}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 5f92fc9..74a7858 100644--- a/exllamav3/modules/block_sparse_mlp.py+++ b/exllamav3/modules/block_sparse_mlp.py@@ -1704,7 +1704,8 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):# decoded inside the GEMM; replaces the per-expert reconstruct + hgemm loop at prefillelif (self.support_hip_wmma and bsz >= _HIP_WMMA_MIN_ROWS and- y.dtype == torch.half and y.is_contiguous() and+ y.dtype == torch.half and (y.is_contiguous() or # or a row-padded producer view+ (y.dim() == 2 and y.stride(1) == 1 and y.stride(0) >= y.size(1) and y.stride(0) % 4 == 0)) andos.environ.get("EXL3_MOE_WMMA", "1") != "0" andnot params.get("activate_all_experts") andnot params.get("reconstruct") and not params.get("autosplit_measure")diff --git a/exllamav3/modules/gated_delta_net.py b/exllamav3/modules/gated_delta_net.pyindex 09cb29a..200df98 100644--- a/exllamav3/modules/gated_delta_net.py+++ b/exllamav3/modules/gated_delta_net.py@@ -1221,7 +1221,17 @@ class GatedDeltaNet(Module):n.rms_norm_eps, n.constant_bias, n.groups, n.gate_first, 1)return yz = self.g_b_proj.forward(gaf[0].view(*core_attn_out.shape[:-2], gaf.shape[-1]), params)- return n.forward(core_attn_out, params, gate = z.view(core_attn_out.shape))+ z = z.view(core_attn_out.shape)+ # EXL3_NC2_KDA_OPAD=1 (read per call): write the norm output at the o_proj row pitch, so o_proj skips its+ # pad_copy_in (same kernel, only the store address moves: bit-exact)+ o_pitch = 0+ _pad_ok = getattr(ext.gated_rms_norm, "pad_ok", None)+ if os.environ.get("EXL3_NC2_KDA_OPAD", "0") == "1" and _pad_ok is not None and not n.gate_first and \+ _pad_ok(core_attn_out, n.weight, n.out_dtype or torch.half, z, n.groups,+ 1 if n.gate_activation == "sigmoid" else 0):+ o_pitch = _row_pad_pitch(self.o_proj, self.num_v_heads * self.v_head_dim,+ core_attn_out.numel() // (self.num_v_heads * self.v_head_dim), params)+ return n.forward(core_attn_out, params, gate = z, out_pitch = o_pitch)def _kda_gates_f16(self, x: torch.Tensor, params: dict, small_first: bool):h = torch.halfdiff --git a/exllamav3/modules/hyperconnections.py b/exllamav3/modules/hyperconnections.pyindex 699e796..1dd7325 100644--- a/exllamav3/modules/hyperconnections.py+++ b/exllamav3/modules/hyperconnections.py@@ -14,6 +14,20 @@ def _pad_pitch_for(k: int) -> int:# keep in step with exllamav3_ext/pad_pitch.h pad_pitch_forreturn k + _PAD_ELEMS if (k * 2) % 1024 == 0 else k+# EXL3_NC2_HC_YPAD=1 (attention site) / EXL3_NC2_HC_YPAD_MLP=1 (MLP site), read per call: the fused mix_norm writes+# its normed output at the GEMM row pitch when the site accepts a row-padded input (params["hc_pad_ok"], set by the transformer), so the consumers' pad_copy_in disappears.+# Same kernels, only the store address moves: bit-exact.+def _hc_y_out(b: int, s: int, D: int, dev, params: dict) -> torch.Tensor:+ R = b * s+ site = params.get("hc_pad_ok")+ flag = "EXL3_NC2_HC_YPAD_MLP" if site == "mlp" else "EXL3_NC2_HC_YPAD"+ if site and R >= _PAD_MIN_ROWS and os.environ.get(flag, "0") == "1" \+ and os.environ.get("EXL3_OPROJ_PAD", "1") != "0":+ pitch = _pad_pitch_for(D)+ if pitch != D:+ return torch.empty((R, pitch), dtype = torch.half, device = dev)[:, :D].view(b, s, D)+ return torch.empty((b, s, D), dtype = torch.half, device = dev)+# gfx1151: store the GatedResidual mixer weights (the only fp16 bulk in an EXL3 model, 22% of# decode weight bytes) as per-row int8 and dequantize in the fused kernel. ROCm only; opt out# with EXL3_HIP_GR_MIX_Q8=0.@@ -366,7 +380,7 @@ class HyperConnection(Module):partials = torch.empty((R, 1, M1), dtype = torch.float, device = dev)post = torch.empty((R, H), dtype = torch.float, device = dev)comb = torch.empty((R, H * H), dtype = torch.float, device = dev).view(R, H, H)- y = torch.empty((b, s, D), dtype = torch.half, device = dev)+ y = _hc_y_out(b, s, D, dev, params)ext.hc_pf_apply_mix_norm(streams.view(R, H, D), ya.contiguous().view(R, D),post_a.contiguous().view(R, H), comb_a.contiguous().view(R, H, H),fn, self.base, self.scale, self.rms_eps, self.hc_eps,@@ -408,7 +422,7 @@ class HyperConnection(Module):fn = self.fn_helse:fn = self.fn- y = torch.empty((b, s, D), dtype = torch.half, device = dev)+ y = _hc_y_out(b, s, D, dev, params)ext.hc_mix_norm(streams.view(R, H, D), fn, self.base, self.scale, self.rms_eps, self.hc_eps,self.sinkhorn_iters, partials, post, comb, y.view(R, D), w,norm.rms_norm_eps, norm.constant_bias, norm.constant_scale)diff --git a/exllamav3/modules/transformer.py b/exllamav3/modules/transformer.pyindex 634464a..c9b244e 100644--- a/exllamav3/modules/transformer.py+++ b/exllamav3/modules/transformer.py@@ -155,7 +155,11 @@ class TransformerBlock(Module):if hc_pending is not None and not self.mlp_norm and not gr_pend:HyperConnection.flush_pending(x, hc_pending)hc_pending = None- fused = self.mlp_hc.mix_norm(x, params, self.mlp_norm, hc_pending) if self.mlp_norm else None+ params["hc_pad_ok"] = "mlp" # MLP site: GEMMs and the MoE gather take a row-padded input+ try:+ fused = self.mlp_hc.mix_norm(x, params, self.mlp_norm, hc_pending) if self.mlp_norm else None+ finally:+ params.pop("hc_pad_ok", None)if fused is not None:hc_post, hc_comb, y = fusedelse:@@ -255,7 +259,11 @@ class TransformerBlock(Module):if self.attn:if self.attn_hc:- fused = self.attn_hc.mix_norm(x, params, self.attn_norm, hc_pending) if self.attn_norm else None+ params["hc_pad_ok"] = True # the attention site accepts a row-padded block input+ try:+ fused = self.attn_hc.mix_norm(x, params, self.attn_norm, hc_pending) if self.attn_norm else None+ finally:+ params.pop("hc_pad_ok", None)if fused is not None:hc_post, hc_comb, y = fusedelse: