Repository navigation
fix(exl3): keep the bf16 pre-scale off the cross-device torch.ldexp fault (#5263) - #5274
Merged
Merged
Conversation
This was referenced Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5263.
Problem
torch.ldexplaunches its kernel on the current CUDA device, so a tensor thatlives on another GPU aborts with
CUDA error: an illegal memory access. That isthe normal state for
device="cuda:1", and for anydevice_mapsplit while thecurrent device stays 0.
ExllamaV3Linear._prescale_from_row_amaxused it to build the per-row2**exponentpre-scale, and_native_bf16_inputalways runs that eager path oneach 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_scaleutils/kimi_k3.py::decode_mxfp4quantization/activation_floatx.py::_nvfp4_mse_quantize_blocksChange
torch.pow(2.0, exponent). It isbit-exact over the full exponent range on CPU and CUDA (checked against
ldexpfor -149..128).exp2is not a drop-in: CUDA rounds the denormal2**-127one ulp low, andtest_mxfp4_extreme_e8m0_matches_float64_oraclecompares that value exactly.
ldexp, because it scales arbitrary valuesexactly (a
2**-emultiplier can overflow whereldexpdoes not), and nowenters
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]-- withtorch.ldexpmonkeypatched to raise, the pre-scale still reproduces the old
ldexpscalebit for bit.
test_bf16_prescale_on_a_non_current_device-- the issue's repro (cuda:1while 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 devicecontexts the search enters.
test_no_unguarded_ldexp_outside_the_nvfp4_search-- repo-wide policy guard soa new
torch.ldexp(cannot be reintroduced.The 7 non-GPU-count tests fail on
mainand pass with this change; the two-GPUnode skips. This host has a single GPU, so the two-GPU repro could not be
executed directly here -- the monkeypatched-
ldexptests pin the same defectwithout needing a second device.
Existing suites are unchanged:
test_exl3_*,test_kimi_k3_support,test_nvfp4_candidate_overflowandtest_model_dequant_fp8give the same2 failed / 102 passed / 12 skipped / 21 errors before and after, all
environmental (no
CUDA_HOMEfor the JIT extensions).