Skip to content

perf(exl3): keep bf16 activations on the native fp16 kernels (#4767) - #4817

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4767
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4767

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4767.

ExllamaV3Linear.forward routed every non-FP16 activation to
ExllamaV3TorchLinear. The default checkpoint dtype is BF16, so the default
path paid the ~39 ms torch decode (M=1) / ~44 ms (M=512) instead of the
native kernel's ~0.04 ms / ~0.46 ms.

Change

BF16 now stays on the native EXL3 kernels:

  • BF16 with amax <= 64: exact BF16->FP16 cast. BF16's 8-bit significand is a
    subset of FP16's 11-bit one and the value is inside FP16's finite range, so
    the cast is bit-exact against the plain FP16 path.
  • BF16 with amax > 64: divide by a power of two (math.frexp, so the scaled
    magnitude lands in (0.5, 1]), run native, multiply back. The Hadamard rotation
    is bounded by sqrt(128)*amax and GEMM accumulation stays far below FP16's
    65504 limit, preserving the wide-input guarantee from fix(exl3): route wide activations through Torch #3731.
  • Bias is removed, rescaled, and restored in FP32 (native bc.run bakes bias in;
    hgemm adds it explicitly) so it is never scaled.
  • Non-finite BF16 and FP32 keep the FP32 torch path; zero-row batches handled.

Pre-scale decision costs one vector_norm(inf) reduction plus one host read per
BF16 forward.

Measurements (GB10, sm_121, torch 2.15.0+cu132, synthetic EXL3 4-bit 4096->14336)

The native extension cannot be built on this box (CUDA toolkit/header
mismatch), so fp16/native timings are unavailable. Measured with a stub native
kernel to isolate the bridge:

case before (bf16 -> torch) after (bridge only)
M=1, bf16 38.855 ms 0.0535 ms (0.0799 ms @1e5)
M=512, bf16 43.973 ms 0.1859 ms (0.7247 ms @1e5)

The old torch decode accounted for ~99.8% of BF16 latency. No total-speedup
ratio is claimed for the native kernel itself since it cannot run here.

Tests

tests/test_exl3_wide_input.py -> 27 passed; the four-file EXL3 set ->
205 passed, 8 skipped, 9 failed (all test_exllamav3_torch_padding.py
native-JIT cases, identical failure set on the parent commit).
New tests: dispatch at 1/32/33/64 rows + zero rows, exact cast, power-of-two
pre-scale from 1e4-1e30, mocked-kernel numerics vs FP64 at 1e4/1e5
(scale-relative error <= 1e-2), non-finite fallback.

ruff: clean.

@Qubitium
Qubitium merged commit 55b267b into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4767 branch November 17, 2026 15:45
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