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.

nc2 S3: ablit attention output widened + projected in one exact pass (EXL3_NC2_ABLIT_F16); MLA o_proj returns fp16 when the residual reads it (EXL3_NC2_MLA_OPROJ_F16)

YanissAmz32c127bd8f62
diff --git a/exllamav3/modules/ablit_runtime.py b/exllamav3/modules/ablit_runtime.py
index 309fa00..4532d31 100644
--- a/exllamav3/modules/ablit_runtime.py
+++ b/exllamav3/modules/ablit_runtime.py
@@ -139,6 +139,19 @@ try:
r = tl.load(r_ptr + offs, mask = m, other = 0.0)
d = tl.sum(y.to(tl.float32) * r, axis = 0)
tl.store(y_ptr + row * H + offs, (y.to(tl.float32) - (w * d) * r).to(y.dtype), mask = m)
+
+ @triton.jit
+ def _project_widen_kernel(x_ptr, y_ptr, r_ptr, w, H: tl.constexpr, BLOCK: tl.constexpr):
+ # EXL3_NC2_ABLIT_F16: _project_kernel on an fp32 y that is the exact widen of fp16 x, without materialising it:
+ # reads x (fp16), writes y (fp32); same fp32 values in, same arithmetic
+ row = tl.program_id(0).to(tl.int64)
+ offs = tl.arange(0, BLOCK)
+ m = offs < H
+ # contiguity hint 4: the fp16 load keeps _project_kernel's 4-wide layout (8-wide would change the tl.sum tree)
+ y = tl.load(x_ptr + row * H + tl.max_contiguous(offs, 4), mask = m, other = 0.0).to(tl.float32)
+ r = tl.load(r_ptr + offs, mask = m, other = 0.0)
+ d = tl.sum(y.to(tl.float32) * r, axis = 0)
+ tl.store(y_ptr + row * H + offs, (y.to(tl.float32) - (w * d) * r).to(tl.float32), mask = m)
except ImportError:
triton = None
@@ -188,3 +201,15 @@ def project(y: torch.Tensor, w: float, r32: torch.Tensor, r16: torch.Tensor) ->
d = torch.mv(y2.float(), r32).to(y2.dtype)
y2.addr_(d, r16, alpha = -w)
return y
+
+
+def project_widen(x: torch.Tensor, w: float, r32: torch.Tensor, r16: torch.Tensor) -> torch.Tensor:
+ """EXL3_NC2_ABLIT_F16: fp32 result of project(x.float(), ...) for an fp16 x, in one pass when the Triton kernel
+ applies (bit-equal: the widen is exact and the kernel arithmetic is the fp32 one). Returns a new fp32 tensor."""
+ h = r32.shape[0]
+ if w == 0.0 or ROW_MODE is not None or not _USE_TRITON or not x.is_contiguous() or x.dtype != torch.half:
+ return project(x.float(), w, r32, r16)
+ y = torch.empty(x.shape, dtype = torch.float32, device = x.device)
+ x2 = x.view(-1, h)
+ _project_widen_kernel[(x2.shape[0],)](x2, y.view(-1, h), r32, w, H = h, BLOCK = _block(h))
+ return y
diff --git a/exllamav3/modules/mla_attn.py b/exllamav3/modules/mla_attn.py
index 771f92f..5cffa4e 100644
--- a/exllamav3/modules/mla_attn.py
+++ b/exllamav3/modules/mla_attn.py
@@ -584,9 +584,27 @@ class MLAttention(Module):
x2 = x.reshape(rows, lin.in_features)
y = torch.cat([dec_proj_single(lin, x2[r : r + 1]) for r in range(rows)], dim = 0)
return to2(y.view(x.shape[:-1] + (lin.out_features,)), None, lin.out_dtype)
+ if lin is self.o_proj and params.get("pfe_y_half") and self._nc2_oproj_f16(lin, x, params):
+ return lin.forward(x, params, torch.half)
return lin.forward(x, params)
+ def _nc2_oproj_f16(self, m, x: torch.Tensor, params: dict) -> bool:
+ """EXL3_NC2_MLA_OPROJ_F16: the block's residual apply reads fp16 y (params["pfe_y_half"]), so o_proj may return
+ its fp16 GEMM output instead of the fp32 widen of it (same predicate as Attention.project_o): bit-exact."""
+ if os.environ.get("EXL3_NC2_MLA_OPROJ_F16", "0") != "1" or "capture" in params or "quant_preserve" in params:
+ return False
+ from .quant import exl3 as _exl3
+ rows = x.numel() // x.shape[-1]
+ return bool(
+ _exl3._f32_via_f16 and rows > max(_exl3.AUTO_RECONSTRUCT_THRESHOLD, _exl3._f32_via_f16_min_rows - 1)
+ and type(m.inner).__name__ == "LinearEXL3" and m.inner.bias is None
+ and not self.config.infer_params.no_reconstruct and not params.get("reconstruct")
+ and not m.lora_a_tensors and m.pre_scale == 1.0 and m.post_scale == 1.0
+ and m.softcap == 0.0 and m.out_features == m.out_features_unpadded
+ )
+
+
@staticmethod
def _proj_gemv_r() -> bool:
return EXL3_MLA_PROJ_R == "gemv_r" and dec_gemv_r_mcg()
diff --git a/exllamav3/modules/transformer.py b/exllamav3/modules/transformer.py
index c9b244e..d258c88 100644
--- a/exllamav3/modules/transformer.py
+++ b/exllamav3/modules/transformer.py
@@ -1,5 +1,6 @@
from __future__ import annotations
from typing_extensions import override
+import os
import torch
from ..util.tensor import to2
from ..model.config import Config
@@ -282,8 +283,10 @@ class TransformerBlock(Module):
y = x.half()
# EXL3_PFE_OPROJ: this site's residual apply (hc_apply) reads fp16 or fp32 y alike, so the
# attention output projection may skip its fp32 widen (see GatedDeltaNet / Attention.project_o)
- pfe_half = bool(self.attn_hc) and not self.ablit and self.attn_resid_scalar is None \
- and not export_state and "pfe_y_half" not in params
+ # EXL3_NC2_ABLIT_F16: an ablit block widens + projects the fp16 output in one exact pass (project_widen)
+ pfe_half = bool(self.attn_hc) and self.attn_resid_scalar is None \
+ and not export_state and "pfe_y_half" not in params and \
+ (not self.ablit or os.environ.get("EXL3_NC2_ABLIT_F16", "0") == "1")
if pfe_half:
params["pfe_y_half"] = True
try:
@@ -298,7 +301,10 @@ class TransformerBlock(Module):
if params.get("prefill") and not export_state:
return x
if self.ablit:
- ablit_runtime.project(y, self.ablit[0], *self.ablit[2:])
+ if pfe_half and y.dtype == torch.half and self.ablit[0] != 0.0:
+ y = ablit_runtime.project_widen(y, self.ablit[0], *self.ablit[2:])
+ else:
+ ablit_runtime.project(y, self.ablit[0], *self.ablit[2:])
if self.attn_resid_scalar is not None:
y *= self.attn_resid_scalar
if self.attn_hc: