Skip to content

Port Q4NX codebook to EXL3 and extend to add 3.5bit support - #5655

Merged
Qubitium merged 7 commits into
mainfrom
feat/exl3-q4nx
Nov 20, 2026
Merged

Qubitium merged 7 commits into
mainfrom
feat/exl3-q4nx

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Port the Q4NX codebook to EXL3 at 2.5, 3.0, and 3.5 bits per weight. This follows #5619 and preserves the source format's packed bytes and per-projection .q4nx marker.

The EXL3 port adapts the Q4NX codebook, beam encoder, and CUDA fragment decoder by AlexBrandes (Alex Brandes). The original implementation covers W2.5 and W3.0. Qubitium extended Q4NX to W3.5. ExLlamaV3 encoder · vLLM fragment decoder

What Changed

  • EXL3Config(codebook="q4nx") and per-projection dynamic rules select Q4NX at 2.5, 3.0, or 3.5 bpw. W2.5 and W3.0 use a 16-state beam for scale search and a 128-state beam for final encoding. At W3.5 each step appends 14 bits to an 18-bit state. The exact Viterbi search retains all 16 overlap classes.
  • Safetensors persist the original tile layout and scalar INT32 marker. quantize_config.json records the recipe and per-projection tensor_storage. This allows mixed-codebook models and exact packed replay from projection checkpoints.
  • The NVIDIA fused projection GEMM and explicit exl3_moe_coop(q4nx=True) CUDA op decode the packed bytes into tensor-core fragments. They accumulate in FP32 and apply the codebook normalization after accumulation.
  • The Torch path uses the same zero-prefix stream and decodes in bounded output chunks. The port targets NVIDIA CUDA for quantization, including the existing remote worker boundary.

Tests

  • CPU and NVIDIA CUDA suites passed.
  • Independent bitstream, packing, projection, and routed forward tests cover all three rates.

On the pre-port baseline, EXL3Config rejected Q4NX. The W2.5 fixture failed the existing half-rate guard, while W3 decoded all 32,768 fixture weights incorrectly. The new tests compare 40-, 48-, and 56-word tiles against independent bitstreams, check the exact safetensors buffers, and quantize and reload mixed mcg and q4nx projections in a tiny Llama. The W3.5 config and Torch decoder tests hit the unsupported-rate guard before the extension. The CUDA W3.5 encoder now matches an exhaustive FP64 Viterbi reference for all 64 encoded suffixes on the regression tile.

PYTHON_GIL=0 CUDA_VISIBLE_DEVICES='' python -m pytest -q -rs \
  tests/test_exl3_fractional_bits.py tests/test_exllamav3_torch_padding.py \
  tests/test_exllamav3_dynamic_skip.py tests/test_exl3_generic_geometry.py \
  tests/test_exllamav3_cpu.py tests/test_exllamav3_jit.py \
  tests/test_exl3_remote.py tests/test_exl3_projection_checkpoint.py \
  tests/test_exl3_trellis_k.py tests/test_dynamic_config_invalidation_4475.py \
  tests/qcfg/test_config_serialization.py tests/qcfg/test_config_validation_4668.py

PYTHONPATH="$PWD" PYTHON_GIL=0 CUDA_VISIBLE_DEVICES=0 \
  TORCH_NO_COMPILER_WRAPPER=1 NVCC_THREADS=2 MAX_JOBS=20 python -m pytest -q -rs \
  tests/kernels/test_exllamav3_kernel.py tests/kernels/test_exllamav3_kernel_map_packed.py \
  tests/kernels/test_exllamav3_native_decode.py tests/kernels/test_exllamav3_quantize_tiles.py \
  tests/test_exl3_dispatch_concurrency.py tests/test_exl3_routed_cuda.py \
  tests/test_exl3_wide_input.py tests/test_exllamav3_torch_padding.py \
  tests/test_exl3_remote.py tests/test_exl3_projection_checkpoint.py

bash format/check.sh
git diff --check origin/main..HEAD

The CPU run passed 322 tests. Its 66 skips cover 64 CUDA-only nodes and two oneDNN BF16x3 probes unavailable on this host. The NVIDIA PG506 SM80 run passed 262 tests. Two PG506 GPUs concurrently encoded W2.5 and W3 under PYTHON_GIL=0 with exact packed-byte repeatability over eight runs each. The runtime was PyTorch 2.15.0+cu132 and the JIT compiler was CUDA 13.3.

Model Support / Module Plan

The existing Llama definition supplies the end-to-end checkpoint test. Model definitions, module plans and AWQ onboarding fall outside this change.

Logic Flow Documentation

Boundary Before With Q4NX
Config and processor EXL3Config accepts 3inst, mcg and mul1 _normalize_exl3_bits accepts Q4NX at 2.5, 3.0, and 3.5 bpw
Checkpoint EXL3 holds one scalar state per tile value pack_trellis_q4nx writes one suffix per vector of four values
Runtime .mcg and .mul1 select their original packed kernels A projection's .q4nx key selects the zero-prefix decoder
flowchart LR
    C["EXL3Config and dynamic rules"] --> R{"Supported rate?"}
    R -->|yes| V["quantize_tiles_q4nx"]
    R -->|no| E["ValueError"]
    V --> P["pack_trellis_q4nx"]
    P --> S["Safetensors trellis, scales and q4nx marker"]
    S --> G["Packed CUDA GEMM or explicit routed decode"]
    S --> T["Chunked EXL3_TORCH reconstruction"]
Loading

Unsupported Q4NX rates and conflicting codebook markers raise during validation. The linked layout reference includes the same flow and the per-projection recipe. Native EXL3 Q4NX documentation

Review Requirements

  • Reviewed the source attribution, layout, dispatch, and config round trip.
  • Checked the CUDA and Torch fallback paths and the packed tensor lifecycle.

Kernel Accuracy Requirements

All three rates reproduced the complete packed bytes on legal zero-error tiles. FP16 and BF16 projection regressions check the FP32 kernel arithmetic and the final output rounding. Routed decode also passed changed-input CUDA graph replay. Native validation covers NVIDIA SM80.

DeepSeek V4.1 Flash expert shapes

The benchmark uses synthetic packed weights at the published dimensions, with FP16 activations and FP32 output, on one NVIDIA PG506-232. The layer has 384 resident routed experts and selects six per token. Each median uses 31 CUDA-event samples over warmed graphs of 20 calls. Each sample rotates the order of the four cases. DeepSeek V4.1 Flash configuration

Packed projection GEMM, in µs per call.

Projection Rows W2.5 mul1 W2.5 Q4NX W3.5 mul1 W3.5 Q4NX
gate/up (5120 → 2304) 1 53.30 38.96 53.15 39.27
gate/up (5120 → 2304) 24 55.81 44.13 55.96 44.03
gate/up (5120 → 2304) 64 84.07 71.27 83.81 70.66
gate+up (5120 → 4608) 1 69.43 39.48 69.38 39.63
gate+up (5120 → 4608) 24 82.18 58.62 82.69 58.83
gate+up (5120 → 4608) 64 107.16 81.10 107.21 80.44
down (2304 → 5120) 1 43.06 27.03 43.06 27.34
down (2304 → 5120) 24 49.51 37.48 49.87 37.73
down (2304 → 5120) 64 64.87 51.00 64.92 50.74

Fused top-6 routed gate, up, down, and combine, in µs per call. The graph rotates the expert IDs across the resident bank.

Token rows W2.5 mul1 W2.5 Q4NX W3.5 mul1 W3.5 Q4NX
1 309.40 154.52 310.22 155.55
24 6,193.41 2,984.24 6,196.99 3,007.23
42 10,808.58 5,176.68 10,815.28 5,217.02

Q4NX's worst relative RMS error against the independent FP64 reference was 0.030% for the projections and 0.019% for routed output. The routed correctness checks include the shared-expert contribution and verify that changed expert IDs reach CUDA graph replay.

Notes

Q4NX preserves EXL3's tile sizes and Hadamard transforms. The marker selects a different state recurrence and codebook at the same packed width.

@Qubitium
Qubitium marked this pull request as ready for review November 20, 2026 04:34
@Qubitium Qubitium changed the title Port AlexBrandes Q4NX codebook to EXL3 Port Q4NX codebook to EXL3 Nov 20, 2026
@Qubitium Qubitium changed the title Port Q4NX codebook to EXL3 Port Q4NX codebook to EXL3 and extend to add 3.5bit support Nov 20, 2026
@Qubitium
Qubitium merged commit 2090c2d into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the feat/exl3-q4nx branch November 20, 2026 05:22
Qubitium added a commit that referenced this pull request Nov 20, 2026
`main` added `quant/q4nx_quant.cu` (#5655) with the unstable
`ATen/cuda/CUDAContext.h`, `ATen/ops/empty.h`, `c10/cuda/CUDAException.h`,
`c10/cuda/CUDAGuard.h` and `torch/library.h` surface, so the stable-ABI
source-graph check failed after the rebase.

Include `torch_stable_compat.h` instead, and use the compat spellings the rest
of exllamav3 already uses: `at::options(input)` instead of
`Tensor::options()`, `mutable_data_ptr` for the written buffers, and
`sizes().equals()` for the shape guard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant