Skip to content

LeapQuant

Qubitium edited this page Nov 27, 2026 · 6 revisions

LeapQuant recurrent-state quantization

🏠 Home · 🧭 Choose a method · 🚀 Quickstart · 🧰 Help

On this page

LeapQuant compresses the fixed-size recurrent state of delta-rule linear attention during native GPT-QModel inference. It works with dense or weight-quantized checkpoints on supported ports without changing the weights, checkpoint format, or quant_method.

LeapQuant was introduced by Yi Pan et al. in LeapQuant: Efficient Linear Attention with Accurate Recurrent State Quantization. See the official implementation and BibTeX citation.

Source revision 1c891c2f9.

Enable LeapQuant

Pass state_quant=LeapQuantConfig(...) to GPTQModel.load() with a local path or Hub ID for a supported checkpoint. LeapQuant is off by default.

from gptqmodel import GPTQModel, LeapQuantConfig

model = GPTQModel.load(
    "/path/to/supported-checkpoint",
    state_quant=LeapQuantConfig(bits=8),
)
output_ids = model.generate("Explain linear attention.", use_cache=True, max_new_tokens=64)

For an already loaded model, call model.enable_state_quant(LeapQuantConfig()) or model.enable_state_quant(None) before the first forward to select packed or dense state. Start generation with a fresh cache to apply the policy. A caller-provided past_key_values keeps the policy it was created with. With no supplied cache, use_cache=False skips cache creation. Passing a config to a model with no supported port raises ValueError. Loader source · Toggle source.

Configuration

LeapQuantConfig field Default Effect
bits 8 Symmetric code width. The 4 and 6 modes bit-pack their codes for lower storage. Start with 8 for accuracy.
window 16 Positive number of decode-token updates between requantizations. 1 requantizes after every decode token.
rank 4 Number of low-rank Compensator Tokens. 0 disables them. The rank must fit both head dimensions.
smooth True Rescales key rows of the residual before quantization.
use_triton True Enables fused decode and Compensator sketch kernels where supported. Set False to use the portable Torch path.
buffer_dtype torch.bfloat16 Dtype for buffered keys and updates. Accepts torch.float16, torch.bfloat16, or torch.float32.
lowrank_iters 2 Nonnegative number of power iterations for the Compensator fit.
lowrank_oversample 4 Nonnegative number of extra sketch columns for the fit.
seed 0 Integer seed for the randomized Compensator fit.

See the configuration and state implementation.

Supported models and generation limits

Native model port Recurrent layers
Kimi K3 Kimi Delta Attention (KDA)
Dense Qwen3.5 (multimodal wrapper or text wrapper) Gated DeltaNet (GDN)
Qwen3.5-MoE (multimodal wrapper or text wrapper) GDN
Qwen3-Next GDN
GLM-5.3 KDA

LeapQuant applies to the listed ports' recurrent layers. Other attention layers keep their usual cache format. Use native GPT-QModel generation and leave cache_implementation at the port's default.

  • Use unpadded prompts with either no attention mask or a 2D all-ones mask. Packed sequence boundaries such as cu_seqlens are rejected.
  • Ordinary generation and cached continuation are supported. Live sequences can be reordered at the same batch size. A forward with a changed batch size or recurrent-state geometry raises ValueError. Start with a fresh cache for a different shape. Live batch expansion and cache cropping are unsupported.
  • Use the packed state for inference. Training and autograd are unsupported.

State storage and performance

Prefill runs the port's existing recurrence and quantizes its final state. During decode, LeapQuant keeps a quantized state between window boundaries and buffers the per-token decay, key, and update records. At a boundary it fits a low-rank Compensator, smooths the remaining residual when enabled, and quantizes it again.

Persistent storage includes the codes, FP32 scales, FP16 Compensator factors, and the window buffer. Each code row represents V values along the recurrent state's value dimension.

Code width Stored code row Bytes per row
4 uint8, two signed codes per byte ceil(V / 2)
6 uint8, four signed codes per three bytes 3 * ceil(V / 4)
8 int8, one signed code per byte V

Packing reduces the code tensor. LeapQuantState.checkpoint_bytes() counts the codes, scales, and factors, and storage_bytes() adds the window buffers. These counters exclude working memory such as Triton scratch and the cached randomized sketch. During decode, the portable Torch path unpacks 4- and 6-bit codes and materializes a temporary FP32 recurrent state for each step. Prefill uses a transient dense state.

LeapQuantState.packed_state() returns a version 2 snapshot with code_container set to "packed4", "packed6", or "int8". load_packed_state() checks the format version, container, and state configuration. Version 1 snapshots are rejected. Use a fresh cache when upgrading.

On CUDA and ROCm, with Triton available, fused decode reads packed 4-, 6-, or 8-bit codes directly and replays the buffered window in FP32. It requires a key dimension of at most 256 and a Compensator rank from 1 through 16. It writes the full FP32 next state at the window boundary for requantization. Decode checks for non-finite values before committing the token to the cache.

The randomized Compensator fit uses Triton modified Gram–Schmidt for supported sketches with up to 256 rows and 16 columns. The Torch path uses Cholesky-based orthonormalization. Both use a small Gram-matrix eigendecomposition. Each kernel falls back to Torch on CPU, when disabled, or when Triton is unavailable or the kernel's inputs are unsupported. See the state implementation and Triton dispatch checks.

With 8 heads, key and value dimensions of 128, and bits=8, window=16, and rank=4, the repository's benchmark measured 588.3 µs per decode token with Torch and 130.8 µs with fused kernels on one accelerator. The Compensator fit took 979.0 µs with Torch and 489.6 µs with fused kernels per window. The benchmark results also include a second accelerator family.

Run the benchmark from the source checkout with GPT-QModel installed. The default --backend auto uses Triton where supported. Use --backend torch to force the portable path. Set --bits to 4, 6, or 8 (the default) to compare checkpoint storage and decode timings. Benchmark script.

python scripts/benchmark_leapquant_state.py --device cuda --bits 4
python scripts/benchmark_leapquant_state.py --device cuda --bits 4 --backend torch

Clone this wiki locally