Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

MXFP4 torch decode: fp16 in fp32 on torch < 2.12, where ldexp overflows (#15121)

Daniel Hanedf19f5f77f5
diff --git a/tests/test_mxfp4_compressed_linear.py b/tests/test_mxfp4_compressed_linear.py
index 9d426beb26c..5f8bf94c50a 100644
--- a/tests/test_mxfp4_compressed_linear.py
+++ b/tests/test_mxfp4_compressed_linear.py
@@ -162,6 +162,20 @@ def test_dequant_matches_compressed_tensors_decompress(device):
assert torch.equal(got.view(torch.int16), want.view(torch.int16)), (o, i)
[email protected]("device", DEVICES)
+def test_torch_fp16_decode_rounds_once_for_every_finite_scale(device):
+ # E8M0 1..254 (0xFF is NaN): past fp16's range a value is inf, a zero nibble stays +-0, small ones go subnormal.
+ # torch < 2.12's ldexp built 2^e in fp16, so 0 * 2^16 was NaN, 2^-10 * 2^20 inf and 6 * 2^-25 zero.
+ packed, scale = _random_packed(8, 64, low = 1, high = 255)
+ packed[0, :16] = torch.arange(0, 256, 16, dtype = torch.uint8)
+ packed[1, :16] = torch.arange(0, 16, dtype = torch.uint8)
+ scale[0, 0], scale[0, 1], scale[1, 0], scale[1, 1] = 143, 147, 102, 254
+ want = _reference_decode(packed, scale).to(torch.float16)
+ got = _dequantize_torch(packed.to(device).view(8, 2, 16), scale.to(device), torch.float16).cpu()
+ assert got.dtype == torch.float16 and got.shape == (8, 64)
+ assert torch.equal(got.view(torch.int16), want.view(torch.int16))
+
+
@pytest.mark.skipif(not has_real_cuda(), reason = "needs a CUDA device")
def test_zoo_kernel_and_torch_path_agree():
packed, scale = _random_packed(256, 512, low = 1, high = 254)
diff --git a/unsloth/models/mxfp4_compressed_linear.py b/unsloth/models/mxfp4_compressed_linear.py
index 5cfc1290109..759be0c2905 100644
--- a/unsloth/models/mxfp4_compressed_linear.py
+++ b/unsloth/models/mxfp4_compressed_linear.py
@@ -64,6 +64,12 @@ _FP4_VALUES = (
-0.0, -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0,
) # fmt: skip
_LUT_CACHE = {}
+# torch < 2.12's ldexp forms 2^e in the input dtype: in fp16 2^16 is inf (0 * 2^16 -> NaN, 2^-10 * 2^20 -> inf)
+# and 2^-25 is 0. Decode fp16 in fp32 there, then round once.
+_LDEXP_IN_INPUT_DTYPE = tuple(int(x) for x in torch.__version__.split("+")[0].split(".")[:2]) < (
+ 2,
+ 12,
+)
def mxfp4_keep_packed_enabled() -> bool:
@@ -71,12 +77,13 @@ def mxfp4_keep_packed_enabled() -> bool:
def _dequantize_torch(blocks, scales, dtype):
- key = (blocks.device, dtype)
+ work = torch.float32 if dtype == torch.float16 and _LDEXP_IN_INPUT_DTYPE else dtype
+ key = (blocks.device, work)
lut = _LUT_CACHE.get(key)
if lut is None:
- lut = _LUT_CACHE[key] = torch.tensor(_FP4_VALUES, dtype = dtype, device = blocks.device)
+ lut = _LUT_CACHE[key] = torch.tensor(_FP4_VALUES, dtype = work, device = blocks.device)
*prefix, G, B = blocks.shape
- out = torch.empty(*prefix, G, B * 2, dtype = dtype, device = blocks.device)
+ out = torch.empty(*prefix, G, B * 2, dtype = work, device = blocks.device)
out[..., 0::2] = lut[(blocks & 0x0F).long()]
out[..., 1::2] = lut[(blocks >> 4).long()]
out = torch.ldexp(out, (scales.to(torch.int32) - 127).unsqueeze(-1)).to(dtype)