Skip to content

[BUG] Regression from #5274 (fix of #5263): decode_e8m0_scale / decode_mxfp4 now return the process default dtype - under fp16 default (user or a concurrent fp16 GPTQModel.load) E8M0 scales decode to 0/inf, MXFP4 source weights silently zeroed #5297

Closed

Description

@Qubitium

Found on main @ c03416c, RTX 4090 and CPU, torch 2.15 + CUDA 13.2, transformers 5.23.

What is wrong

#5274 (fix of #5263) replaced torch.ldexp(torch.ones_like(x, dtype=torch.float32), e) with torch.pow(2.0, e) in two decoders:

  • gptqmodel/quantization/dtype.py:122 decode_e8m0_scale: decoded = torch.pow(2.0, exponent) with an int32 exponent.
  • gptqmodel/utils/kimi_k3.py:85 decode_mxfp4: scale = torch.pow(2.0, scales.int() - 127).

torch.pow(python_float, int_tensor) returns the process default dtype, not float32. The old code always produced float32. When the default dtype is float16, every E8M0 scale outside about [2^-24, 2^15] decodes to 0 or inf. That covers almost every real MXFP4/MXFP8 block scale. The decoded weights become zeros or inf, and nothing warns.

The process default dtype is float16 whenever:

  • user code calls torch.set_default_dtype(torch.float16);
  • any other thread is inside a model construction. transformers' local_torch_dtype sets the process-global default dtype for the whole from_pretrained/from_config construction, and GPTQModel.load(..., dtype=torch.float16) does this on every load.

These decoders run while GPT-QModel dequantizes MXFP4/E8M0 sources: BaseQModel._decoder_mx_block_scale (models/base.py:5597), utils/model_dequant.py:1183/1459, and the Kimi-K3 and MiMo MXFP4 readers. That work happens during quantize()/dequant, outside any lock. So a long DeepSeek-V4 / Kimi-K3 / MiMo quantize in one thread silently gets zeroed weights whenever another thread loads an fp16 model. docs/concurrency.md says quantize() on already-loaded models stays concurrent with loads.

Repro 1: default dtype (GPU)

import torch
from gptqmodel.quantization.dtype import decode_e8m0_scale
from gptqmodel.utils.kimi_k3 import decode_mxfp4
enc = torch.tensor([0, 100, 110, 127, 140, 150, 254], dtype=torch.uint8, device="cuda")
for d in (torch.float32, torch.bfloat16, torch.float16):
    torch.set_default_dtype(d)
    s = decode_e8m0_scale(enc)
    w = decode_mxfp4(torch.full((1, 16), 0x22, dtype=torch.uint8, device="cuda"),
                     torch.tensor([[100]], dtype=torch.uint8, device="cuda"), dtype=torch.bfloat16)
    print(d, s.dtype, s.tolist(), "mxfp4 w[0,0] =", w[0, 0].item())

b3ec8c85e (parent of #5274):

torch.float16 -> torch.float32 [5.88e-39, 7.45e-09, 7.63e-06, 1.0, 8192.0, 8388608.0, 1.70e+38]   mxfp4 w[0,0] = 7.45e-09

main:

torch.bfloat16 -> torch.bfloat16 [...same values...]
torch.float16  -> torch.float16 [0.0, 0.0, 7.63e-06, 1.0, 8192.0, inf, inf]                     mxfp4 w[0,0] = 0.0

Repro 2: a concurrent load (CPU, no user dtype change)

import threading, torch
from gptqmodel import GPTQModel
from gptqmodel.quantization.dtype import decode_e8m0_scale
enc = torch.tensor([100, 127, 150], dtype=torch.uint8); ref = decode_e8m0_scale(enc)
stop, bad, n = threading.Event(), [], [0]
def decoder():
    while not stop.is_set():
        s = decode_e8m0_scale(enc); n[0] += 1
        if not torch.equal(s.float(), ref): bad.append((s.dtype, s.tolist()))
def loader():
    for _ in range(3):
        GPTQModel.load("Qwen2.5-0.5B-Instruct-gptq-4bit", device="cpu", dtype=torch.float16)
    stop.set()
a, b = threading.Thread(target=decoder), threading.Thread(target=loader); a.start(); b.start(); b.join(); a.join()
print("decodes", n[0], "wrong", len(bad), bad[:1])
b3ec8c85e: decodes 54634 wrong 0
main:      decodes 46668 wrong 160 [(torch.float16, [0.0, 1.0, inf])]

Suggested fix

Pin the dtype explicitly. That is still exact, and it does not use the cross-device ldexp launch:

  • decode_e8m0_scale: torch.pow(torch.tensor(2.0, dtype=torch.float32, device=encoded.device), exponent), or torch.pow(2.0, exponent.to(torch.float32)). The float32 power of two is exact for -127..127.
  • decode_mxfp4: the same, with float32.
  • Sibling: ExllamaV3Linear._prescale_from_row_amax (nn_modules/exllamav3.py:480) does torch.pow(2.0, exponent).to(row_amax.dtype). Under an fp16 default, 2**exponent overflows to inf for exponents above 15 before the cast. Use a float32 base there as well.

Test that should have caught it

tests/test_ldexp_cross_device_5263.py checks values only under the default float32. Run decode_e8m0_scale / decode_mxfp4 under torch.set_default_dtype(torch.float16) and bfloat16, and assert that the result is float32 and bit-identical.

Severity

Medium. The result is silently wrong weights (zeros/inf scales) in MXFP4/E8M0 source dequantization. It needs either a non-fp32 default dtype or a concurrent fp16 load. Before #5274, the decoders were immune to both.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 19, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 finished this issue.

    Pull request: #5298

    Force float32 in the pow-based E8M0/MXFP4/EXL3 decoders so the process default dtype cannot degrade them, and add a default-dtype regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions