[KongchangAI]
· 2 min read· 1,159 words

ZenMargin: Dual-Limb FP4 Attention Breaks Through Long-Context Reasoning Bottlenecks

ZenMargin: Dual-Limb FP4 Attention Breaks Through Long-Context Reasoning Bottlenecks

ZenMargin uses dual-limb 4-bit representation and margin gating to achieve both speed and accuracy in long-context attention at pure 4-bit precision.

4-bit attention for long-context LLMs faces an accuracy-efficiency dilemma: uniform 4-bit quantization harms long-range retrieval, while FP16 fallback requires a full-precision KV cache and blocks decode acceleration. ZenMargin identifies the failure as error concentration on dominant key directions plus narrow retrieval-head margins, and resolves it within pure 4-bit via causal centering and Hadamard transform, splitting KV pairs into main and residual 4-bit limbs sharing a scale, and an online margin gate that natively refines only 1-5% of peak blocks during prefill while decode reuses the same layout. On RTX 5090 it hits 867-967 TFLOPS (4.0-4.4x FlashAttention-2), 1.9x TTFT at 64K, 44% less KV cache memory than BF16, and matches BF16 accuracy on RULER and LongBench without retraining.

Long-context large language model (LLM) inference serving has long faced a dilemma: either use uniform 4-bit quantization for speed at the cost of long-range retrieval accuracy, or fall back to FP16 precision for critical parts, which requires retaining a full-precision KV cache and prevents decode-stage acceleration. A recently published technical report proposes the ZenMargin approach, which claims to achieve both fast and accurate 4-bit attention without any FP16 fallback.

Why does 4-bit attention fail in long contexts?

The researchers first analyzed the root causes of retrieval failures caused by 4-bit quantization, identifying two key factors. First, quantization error concentrates on a few dominant key directions, producing highly correlated score errors that systematically distort attention weights. Second, the retrieval heads themselves operate on a narrow score margin, so tiny 4-bit noise is enough to cause an otherwise correct key to be wrongly selected. This means the problem is not uniformly distributed random error but structured bias, requiring targeted correction strategies.

ZenMargin paper title screenshot

KV cache is a key component of autoregressive LLM inference: during the prefill stage, the model computes and caches key (Key) and value (Value) vectors for each token in the input sequence; during the decode stage, each newly generated token only needs its Query computed and attention performed against the historical KV cache. As context grows to 128K, the KV cache size can easily exceed the model weights themselves, becoming a memory bottleneck. Quantizing the KV cache to 4 bits (e.g., the NVFP4 format) can directly compress storage by 4x and leverage low-precision tensor cores to accelerate matrix multiplication—this is the core motivation behind 4-bit attention research. But quantization inevitably introduces numerical error, and in long contexts these errors accumulate along attention scores, proving especially fatal for tasks requiring precise retrieval of distant information—precisely the root problem ZenMargin attempts to solve within pure 4-bit precision.

How does ZenMargin solve this with pure 4-bit precision?

ZenMargin's core idea is to directly resolve the above two problems within 4-bit precision. It first applies causal centering and a lightweight Hadamard transform to the keys to smooth out error concentration on dominant directions. It then splits each KV pair into a main 4-bit tensor and a residual 4-bit tensor, both sharing the same scaling factor, forming what is called a "two-limb NVFP4" representation.

During the prefill stage, an online margin gate reads only the main 4-bit limb to process 95-99% of blocks, performing native refinement on FP4 tensor cores only for the remaining 1-5% of blocks near the peak. During the decode stage, ZenMargin-Cache reuses the exact same split layout without any repacking. This design avoids the storage overhead of a full-precision KV cache while maintaining FP4 computational efficiency.

The Hadamard transform is an orthogonal linear transform whose transformation matrix consists only of ±1, computable in O(d log d) time via the fast Hadamard transform (FHT). In quantization, it is often used to "spread" the energy in a vector across all dimensions: if quantization error is concentrated in a few dominant directions, multiplying by the Hadamard matrix rotates and disperses those directions, making the error distribution more uniform and approximately Gaussian, thereby significantly reducing worst-case quantization distortion. This technique has been validated in LLM quantization work such as QuaRot and SpinQuant. ZenMargin applies it lightly to key vectors, working in concert with causal centering to specifically weaken structured error concentration on dominant key directions—a key mechanism for understanding why it can maintain retrieval accuracy at pure 4-bit.

NVFP4 is a 4-bit floating-point format introduced by NVIDIA on the Blackwell architecture, employing an E2M1 (1-bit sign, 2-bit exponent, 1-bit mantissa) micro-scaling block quantization scheme: every 16 elements share one FP8 scaling factor, balancing dynamic range at extremely low bit-width with native support for hardware tensor cores. Compared to traditional INT4 uniform quantization, NVFP4 is more robust to outliers. ZenMargin's "two-limb" design unfolds precisely within the NVFP4 framework—the main limb and residual limb share a scaling factor, so residual compensation requires no additional metadata overhead and refinement computation can be completed directly on FP4 tensor cores. Understanding NVFP4's block-scaling structure helps grasp why this approach can achieve "pure 4-bit" without falling back to FP16.

Measured performance: triple gains in speed, accuracy, and memory

On a single RTX 5090, tested against six open-source LLMs (context lengths up to 128K), ZenMargin achieves 867-967 TFLOPS throughput on real activations—4.0-4.4x that of FlashAttention-2, 1.5-1.6x that of SageAttention2++, and faster than FlashInfer NVFP4. On diffusion and code workloads, throughput remains stable with no performance cliffs. For time-to-first-token (TTFT) at 64K context, acceleration reaches up to 1.9x.

On memory, ZenMargin-Cache reduces KV cache memory by 44% compared to BF16, while—because it primarily reads the main 4-bit limb—boosting throughput to 2.1-2.9x that of FlashInfer BF16 in 32-layer single-GPU serving, and outperforming FlashInfer FP8, both on long inputs and in incremental-refresh Long-CoT workloads. On accuracy, without retraining, it matches BF16 accuracy on the full 13-task RULER (up to 67.6K) and LongBench benchmarks.

Commentary: ZenMargin's cleverness lies in using the residual limb to preserve the "corner information" discarded by quantization, but activating it only when necessary, thereby finding a new balance between accuracy and efficiency. However, its online gating strategy is sensitive to block-level granularity, and whether it can adapt to more dynamic attention patterns in the future is worth watching.

RULER (Retrieval Under Long-context Evaluation) is a synthetic benchmark for evaluating long-context LLM capabilities, systematically testing model performance on 13 task types—including "needle-in-a-haystack" multi-needle retrieval, multi-hop tracing, and aggregation counting—by controllably generating inputs up to 128K, filling the gap that traditional perplexity metrics cannot reflect long-range retrieval ability. LongBench is a collection of real-world long-text understanding benchmarks covering question answering, summarization, code completion, and other tasks. Together they provide a relatively comprehensive test of whether quantization harms long-context accuracy. ZenMargin matching BF16 accuracy on these two benchmarks under "no retraining" conditions is the most convincing evidence for its accuracy claims.

One-line Summary

ZenMargin uses a two-limb 4-bit representation and margin gating to achieve both speed and accuracy in long-context attention at pure 4-bit precision.

Paragraph Summary

4-bit attention for long-context LLMs has long faced an accuracy-efficiency dilemma: uniform 4-bit quantization harms long-range retrieval, while falling back to FP16 requires retaining a full-precision KV cache and prevents decode acceleration. The ZenMargin technical report identifies the failure root causes as quantization error concentrating on dominant key directions and narrow score margins in retrieval heads, and proposes resolving them within pure 4-bit precision: smoothing errors via causal centering and Hadamard transform, splitting KV pairs into a main 4-bit limb and a residual 4-bit limb sharing a scaling factor, and using an online margin gate during prefill to natively refine only the 1-5% of blocks near the peak, with the decode stage reusing the same layout. Tested on an RTX 5090, it achieves 867-967 TFLOPS throughput—4.0-4.4x FlashAttention-2—1.9x TTFT acceleration at 64K, 44% less KV cache memory than BF16, and matches BF16 accuracy on RULER and LongBench without retraining.

#ZenMargin#FP4 attention#long-context LLM#KV cache quantization#NVFP4#Hadamard transform#4-bit quantization#LLM inference
Share:

Related articles