Repository navigation
Port Q4NX codebook to EXL3 and extend to add 3.5bit support - #5655
Merged
Merged
Conversation
Qubitium
marked this pull request as ready for review
November 20, 2026 04:34
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.
This was referenced Nov 20, 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.
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
.q4nxmarker.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-projectiondynamicrules 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.quantize_config.jsonrecords the recipe and per-projectiontensor_storage. This allows mixed-codebook models and exact packed replay from projection checkpoints.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.Tests
On the pre-port baseline,
EXL3Configrejected 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 mixedmcgandq4nxprojections 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.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=0with 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
EXL3Configaccepts3inst,mcgandmul1_normalize_exl3_bitsaccepts Q4NX at 2.5, 3.0, and 3.5 bpwpack_trellis_q4nxwrites one suffix per vector of four values.mcgand.mul1select their original packed kernels.q4nxkey selects the zero-prefix decoderflowchart 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"]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
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.
mul1mul1Fused top-6 routed gate, up, down, and combine, in µs per call. The graph rotates the expert IDs across the resident bank.
mul1mul1Q4NX'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.