Skip to content

[BUG] EXL3 native bf16 on a non-current GPU (device="cuda:1") aborts with illegal memory access on the first forward: _prescale_from_row_amax calls torch.ldexp without a device guard, and torch.ldexp faults cross-device (fp16 and set_device(1) work) #5263

Description

@Qubitium

Found on main @ c1807f8, RTX 5090 (cuda:0) + RTX 4090 (cuda:1), torch 2.15 + CUDA 13.2. The torch op bug also reproduces on torch 2.16.0.dev20261102.

What is wrong

Any EXL3 checkpoint loaded in bf16 on the native kernel (BACKEND.EXL3_EXLLAMA_V3 / AUTO) on a GPU that is not the current CUDA device aborts the process on its first forward with CUDA error: an illegal memory access. Example: device="cuda:1", device_map={"": 1} or max_memory={1: ...} with the default current device 0. The same checkpoint in fp16 works on cuda:1. bf16 works on cuda:0, after torch.cuda.set_device(1), or under optimize(), because the #5182 compiled-forward scope enters the model's device.

Root cause: ExllamaV3Linear._prescale_from_row_amax (gptqmodel/nn_modules/exllamav3.py:474, added by #4938) calls torch.ldexp(torch.ones_like(row_amax), exponent) without a device guard. torch.ldexp itself faults when its inputs live on a non-current GPU. Pure torch repro:

import torch                              # 2 visible GPUs, current device 0
x = torch.ones(96, 1, device="cuda:1"); e = torch.ones_like(x, dtype=torch.int32)
torch.ldexp(torch.ones_like(x), e); torch.cuda.synchronize(1)
# torch.AcceleratorError: CUDA error: an illegal memory access was encountered
# (torch.frexp, torch.pow(2, e), torch.exp2 and x * torch.exp2(e.float()) are fine on the same tensors)

The fused kernels from #5184 do carry a CUDAGuard, but they do not help here. _native_bf16_input always runs the eager _prescale_from_row_amax on each module's first (warm-up) forward, and on every forward when the fused op is unavailable. Disabling the fused ops changes nothing: it still crashes.

Repro

import torch
from gptqmodel import GPTQModel, BACKEND
q = GPTQModel.load("Llama-3.2-1B-Instruct-EXL3-W4-ALL-d32557b6", backend=BACKEND.EXL3_EXLLAMA_V3,
                   dtype=torch.bfloat16, device="cuda:1")
# torch.cuda.set_device(1)   # <- with this line it works
with torch.inference_mode():
    q.model(torch.randint(100, 20000, (2, 48), device="cuda:1"))

Output (with CUDA_LAUNCH_BLOCKING=1):

  File "gptqmodel/nn_modules/exllamav3.py", line 518, in forward
  File "gptqmodel/nn_modules/exllamav3.py", line 446, in _native_bf16_input
  File "gptqmodel/nn_modules/exllamav3.py", line 474, in _prescale_from_row_amax
torch.AcceleratorError: CUDA error: an illegal memory access was encountered

Without launch blocking, the process dies with GPU assert: an illegal memory access was encountered .../gptqmodel_ext/exllamav3/quant/hadamard.cu 152. That is the next native launch's cudaPeekAtLastError.

Placement matrix, same checkpoint, logits compared with a single-GPU run on the same physical card:

dtype kernel cuda:1 via device / device_map={"":1} / max_memory={1:..}
fp16 EXL3_EXLLAMA_V3 ok, bitwise equal
fp16 EXL3_TORCH ok, bitwise equal
bf16 EXL3_TORCH ok, bitwise equal
bf16 EXL3_EXLLAMA_V3 illegal memory access, process abort
bf16 EXL3_EXLLAMA_V3 + torch.cuda.set_device(1) ok

bf16 is the default dtype of most EXL3 checkpoints (#4767). On a host whose GPUs share a compute capability, any multi-GPU device_map (auto/balanced/explicit) puts layers on cuda:1 while the current device stays 0. Accelerate hooks do not switch the current device, so a bf16 EXL3 model split across GPUs should hit the same fault. I could only verify single-device-on-cuda:1 placements, because this mixed host refuses spanning maps by policy.

Other torch.ldexp call sites that run on accelerator tensors without a guard have the same exposure: gptqmodel/quantization/dtype.py:117 (decode_e8m0_scale, used in models/base.py:5587 and utils/model_dequant.py), gptqmodel/quantization/activation_floatx.py:244-245, and gptqmodel/utils/kimi_k3.py:82.

Suggested fix

  • Replace torch.ldexp(torch.ones_like(row_amax), exponent) with torch.exp2(exponent.to(torch.float32)). It is exact for these integer exponents and verified safe across devices. Do the same at the other ldexp call sites, or wrap them in torch.cuda.device(t.device).
  • Alternatively, enter torch.cuda.device(x.device) around the whole _native_bf16_input / native forward, as the C++ ops already do.
  • Consider reporting the ldexp issue upstream.

Test that should have caught it

tests/test_load_device_context_4863.py and the round-19 cuda:1 placement checks only run EXL3 in fp16. Add a 2-GPU node that loads an EXL3 checkpoint in bf16 on cuda:1, with current device 0, and runs one forward. A unit test can also call ExllamaV3Linear._prescale_from_row_amax on a cuda:1 tensor.

Severity

High. It is a hard process abort (illegal memory access, sticky CUDA error) on the default bf16 dtype for any EXL3 model that is not on the current device.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 finished this issue.

    Pull request: #5274

    Remove the cross-device torch.ldexp fault: use exp2 / device guard in ExllamaV3Linear._prescale_from_row_amax and the other unguarded ldexp sites, plus regression tests.

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