Skip to content

[PERF] EXL3 in bf16 (the default load dtype for Llama-3/Qwen checkpoints) never uses the native CUDA kernel: ExllamaV3Linear routes every non-fp16 input to the pure-Torch trellis decode, 200x slower at M=1 (8.0 vs 0.04 ms), 7.7 vs 266 tok/s #4767

Closed

Description

@Qubitium

Found on main @ b225a43, RTX 4090 (CUDA 13.2, torch 2.15).

What is wrong

ExllamaV3Linear.forward (gptqmodel/nn_modules/exllamav3.py:311-314) sends every non-FP16 activation to ExllamaV3TorchLinear, which runs a pure-Torch trellis decode: about 700 small index/bitwise_and/mul/sum launches per call. #3731 introduced this routing so that BF16 would stop crashing in the FP16-only native kernels (docs/exl3-wide-activation-dispatch.md).

BF16 is the default dtype in practice. An EXL3 checkpoint made from a bf16 model (Llama-3.x, Qwen2.5/3, ...) stores torch_dtype: bfloat16, and GPTQModel.load(path) with no dtype loads it in bf16. So the default way to run an EXL3 model on CUDA never touches the native EXL3 kernels. Neither the docs nor the load log mention this; the only signal is a 20-200x slowdown.

Measured

Same EXL3 4-bit checkpoint (Llama-3.1-8B-Instruct truncated to 4 layers), default AUTO kernel ExllamaV3Linear:

fp16 (native) bf16 (default load) GPTQ 4-bit Marlin, bf16
mlp.gate_proj 4096x14336, M=1 0.040 ms 8.04 ms (200x) 0.017 ms
same, M=512 0.456 ms 10.05 ms (22x) 0.21 ms
4-layer model greedy decode 266 tok/s 7.7 tok/s 314 tok/s

Full Llama-3.1-8B-Instruct EXL3 4-bit, bf16 reload: wikitext-2 40x512 evaluation takes 76.8 s (the GPU was shared with one other quantize job during that run), against 1.7 s for the GPTQ 4-bit Marlin checkpoint of the same model on the same card. The PPL itself is good (9.336 vs bf16 9.141).

torch.profiler of one bf16 M=1 call: CUDA time is spread over aten::sum (28%), aten::mul (22%), aten::index (18%), bitwise_and (10%), cat and copy_, with 3530 cudaLaunchKernel calls for 5 forwards.

Repro

import time, torch
from gptqmodel import GPTQModel
for dt in (torch.float16, torch.bfloat16):
    q = GPTQModel.load("Llama-3.1-8B-Instruct-EXL3-4bit", dtype=dt)       # made by EXL3Config(bits=4) from the bf16 model
    lin = q.model.model.layers[0].mlp.gate_proj                           # ExllamaV3Linear
    x = torch.randn(1, 4096, device="cuda", dtype=dt)
    with torch.inference_mode():
        for _ in range(3): lin(x)
        torch.cuda.synchronize(); t = time.time()
        for _ in range(20): lin(x)
        torch.cuda.synchronize()
    print(dt, (time.time() - t) / 20 * 1e3, "ms")
# torch.float16 0.040 ms
# torch.bfloat16 8.042 ms

Suggested fix

  • Run bf16 activations through the native kernel by casting to fp16 the way upstream exllamav3 runs everything. The concern fix(exl3): route wide activations through Torch #3731 addressed is fp16 range, and it can be handled without giving up the native kernel for every call:
    • keep the fast path when x.abs().amax() < 65504, and pre-scale x (and the output) by a power of two otherwise. This is exact for the scale and costs one tiny reduction.
    • Or add a bf16 instantiation of the native GEMM/HGEMM: the activations are only loaded and converted in the kernel prologue.
  • At minimum, warn once at load time when an EXL3 model is loaded in bf16/fp32 on CUDA/ROCm ("native EXL3 needs float16; using the Torch reconstruction path, ~20-200x slower; load with dtype=torch.float16"), and say so in docs/exl3-wide-activation-dispatch.md.

Test that should have caught it

A kernel benchmark gate (tests/benchmark/...) that compares the default-dtype EXL3 forward with the native fp16 forward, or with Marlin at the same shape, and fails above a fixed ratio. tests/test_exl3_wide_input.py checks only numerics.

Severity

Medium (performance): every bf16 EXL3 model, which is the default for current model families, runs 20-200x slower per layer on CUDA than the native kernel, and nothing tells the user.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Taking this.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4817

    EXL3 bf16 routes non-fp16 inputs to Torch trellis decode; worktree /root/work-4767

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Additional data (memory side of the same path), main @ dba142d, RTX 4090.

    The bf16 route through ExllamaV3TorchLinear._forward_chunked is also the largest per-call scratch of any CUDA kernel I measured this round. Peak allocated during one module forward, minus its output (Qwen2.5-0.5B EXL3 4-bit, AUTO = ExllamaV3Linear):

    module (K x N), packed MiB fp16 (native) M=1 / 16 / 256 bf16 (torch path) M=1 / 16 / 256
    q_proj 896x896, 0.39 0.00 / 0.03 / 5.97 MiB 25.0 / 25.1 / 27.2 MiB
    up_proj 896x4864, 2.09 0.00 / 0.03 / 12.75 26.8 / 27.0 / 31.4
    down_proj 4864x896, 2.09 0.01 / 0.15 / 14.69 135.7 / 136.2 / 145.6

    That is about 54 bytes of transient per weight in each 512-column chunk (_EXL3_OUTPUT_TILES_PER_CHUNK = 32), regardless of M. The cost comes from _unpack_indices (nn_modules/exllamav3_torch.py:413-458), which builds the symbol stream in int64:

    • value ... .to(torch.long);
    • padded.unfold(...) multiplied by an int64 weights vector, which for 4-bit materializes [K, 512, 256/16..., warmup+1=4] int64, i.e. 32 B/weight before .sum;
    • then the fp32 LUT gather and permutes on top.

    The chunk bounds only N, not K. Projected from the measured 54 B/weight, Llama-3.1-8B down_proj (K=14336) is about 380 MiB of scratch per call, and the full-model eager forward peak goes 8.4 -> 136 MiB at M=1 on this 0.5B model.

    Fix that also helps the slowness: build the 16-bit sliding window with int32 shifts and ORs (((prev << bits) | sym) & 0xFFFF over the warmup lags) instead of the int64 unfold-multiply-sum. Also bound the chunk by K*N elements rather than by output tiles only. The real fix is still the native-kernel route proposed above.

    🤖 Generated with Claude Code

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Superseded: #4817 landed this fix while my worktree was in progress, so I stood down without opening a duplicate PR (my "Taking this." above is void). Independently verified on current main (0d9396e) on an RTX 4090 / sm_80, packed 4-bit EXL3 4096x14336 layer:

    • BF16 no longer equals the Torch reconstruction path — the native kernel is used.
    • Accuracy vs the FP32 reference: 3.2e-3 relative error; a 1e6-wide activation stays finite at 2.6e-3; a bias-dominant case exercises the bias-rescale path at 2.7e-3.
    • M=1: fp16 0.120 ms, bf16 0.212 ms, Torch path 14.83 ms (70x). M=512: 0.382 / 0.559 / 14.92 ms (27x).

    I also A/B'd the per-call float(vector_norm(x)) host read against an equivalent device-side scale: identical outputs, and the merged host-sync version is slightly faster here (0.216 vs 0.261 ms at M=1), so no follow-up is warranted on that account.

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