Skip to content

fix(exl3): keep the bf16 pre-scale off the cross-device torch.ldexp fault (#5263) - #5274

Merged
Qubitium merged 1 commit into
mainfrom
fix/exl3-ldexp-device-guard-5263
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/exl3-ldexp-device-guard-5263

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5263.

Problem

torch.ldexp launches its kernel on the current CUDA device, so a tensor that
lives on another GPU aborts with CUDA error: an illegal memory access. That is
the normal state for device="cuda:1", and for any device_map split while the
current device stays 0.

ExllamaV3Linear._prescale_from_row_amax used it to build the per-row
2**exponent pre-scale, and _native_bf16_input always runs that eager path on
each module's first forward. A bf16 EXL3 checkpoint -- the default dtype for most
EXL3 checkpoints -- therefore aborted on its first forward unless it sat on the
current device or was loaded under optimize().

The same unguarded pattern existed at three more sites:

  • quantization/dtype.py::decode_e8m0_scale
  • utils/kimi_k3.py::decode_mxfp4
  • quantization/activation_floatx.py::_nvfp4_mse_quantize_blocks

Change

  • The three power-of-two sites now use torch.pow(2.0, exponent). It is
    bit-exact over the full exponent range on CPU and CUDA (checked against
    ldexp for -149..128). exp2 is not a drop-in: CUDA rounds the denormal
    2**-127 one ulp low, and test_mxfp4_extreme_e8m0_matches_float64_oracle
    compares that value exactly.
  • The NVFP4 block search keeps ldexp, because it scales arbitrary values
    exactly (a 2**-e multiplier can overflow where ldexp does not), and now
    enters torch.cuda.device(blocks.device) around the two calls.

Tests

New tests/test_ldexp_cross_device_5263.py:

  • test_bf16_prescale_never_calls_ldexp[cpu|cuda] -- with torch.ldexp
    monkeypatched to raise, the pre-scale still reproduces the old ldexp scale
    bit for bit.
  • test_bf16_prescale_on_a_non_current_device -- the issue's repro (cuda:1
    while device 0 is current); skips without two GPUs.
  • test_e8m0_decode_never_calls_ldexp -- all 255 E8M0 bytes, bit-exact.
  • test_mxfp4_decode_never_calls_ldexp[cpu|cuda] -- exact decode.
  • test_nvfp4_block_search_enters_the_tensor_device -- records the device
    contexts the search enters.
  • test_no_unguarded_ldexp_outside_the_nvfp4_search -- repo-wide policy guard so
    a new torch.ldexp( cannot be reintroduced.

The 7 non-GPU-count tests fail on main and pass with this change; the two-GPU
node skips. This host has a single GPU, so the two-GPU repro could not be
executed directly here -- the monkeypatched-ldexp tests pin the same defect
without needing a second device.

Existing suites are unchanged: test_exl3_*, test_kimi_k3_support,
test_nvfp4_candidate_overflow and test_model_dequant_fp8 give the same
2 failed / 102 passed / 12 skipped / 21 errors before and after, all
environmental (no CUDA_HOME for the JIT extensions).

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

Labels

None yet

Projects

None yet

1 participant