ZenMargin: Two-Limb NVFP4 Attention Kernels for 128K LLMs

ZenMargin (arXiv:2611.39144) eliminates FP16 fallback caches in 4-bit attention by splitting Q, K, and V into two nested NVFP4 limbs sharing one per-head scale. On a single RTX 5090 (SM120), ZenMargin reaches 867–967 TFLOPS (4.38x FlashAttention-2), preserves 100.00% RULER-48K accuracy, and cuts KV-cache memory by 44% below BF16.
ZenMargin: Two-Limb NVFP4 Attention Kernels for 128K LLMs: ZenMargin (arXiv:2611.39144) eliminates FP16 fallback caches in 4-bit attention by splitting Q, K, and V into two nested NVFP4 limbs sharing one per-head scale. On a single RTX 5090 (SM120), ZenMargin reaches 867–967 TFLOPS (4.38x FlashAttention-2), preserves 100.00% RULER-48K accuracy, and cuts KV-cache memory by 44% below BF16. Designed as a zero-dependency, open-source TypeScript architecture under the MIT License with native Model Context Protocol (MCP) support and deterministic phase state machines.
- Zero FP16 Backup Memory: Packs two shared-scale NVFP4 limbs (`rec1` + `rec2` = 288 B/key) to cut KV-cache VRAM 44% below BF16.
- 95%–99% Single-Limb Fast Path: Online margin gating reads only `rec1` (`~146 B/key` during FlashDecoding—3.5x below BF16, 1.75x below FP8) for non-peak blocks.
- 867–967 TFLOPS on RTX 5090: Outperforms FlashAttention-2 by 4.0–4.4x, SageAttention2++ by 1.5–1.6x, and FlashInfer 0.7.6 SM120 NVFP4.
- 100.00% RULER-48K Parity: Causal centering plus FWHT conditioning recovers BF16 accuracy (`49.80` LongBench-10) where causal SageAttention3 collapses to 0–3.7%.
import { createAgent } from '@smoke-monkey/harness';// Drive 64K-128K repository RAG on a single 32GB RTX 5090 running ZenMargin-Cacheconst longContextAgent = createAgent({agentId: 'zenmargin-128k-reviewer',provider: 'openai-compatible',model: 'meta-llama/Llama-3.1-8B-Instruct',baseUrl: 'http://127.0.0.1:8000/v1',workspacePath: process.cwd(),permissions: {read_file: 'allow',write_file: 'ask',run_command: 'ask',},});async function reviewFullMonorepoContext() {const result = await longContextAgent.run('Load all 48K tokens of architecture RFCs and kernel tests, locate memory-leak needles across modules, and produce a fix plan.');console.log('Long-context NVFP4 review:', result.summary);}reviewFullMonorepoContext().catch(console.error);
Watch: Related Video Guides
LLM Quantization Explained: INT4, INT8, FP8 and NVFP4
LLM Interview Questions: KV Cache, Quantization & Speculative Decoding Explained
What Is ZenMargin? Fixing 4-Bit Attention Retrieval Collapse
ZenMargin is a Blackwell SM120 NVFP4 attention and FlashDecoding kernel suite (github.com/wrencanfly/ZenMargin) that replaces full-precision FP16 backup caches with two nested 4-bit limbs under one shared scale. Evaluated across six open LLMs up to 128K context on an RTX 5090, it achieves 4.38x FlashAttention-2 speed with lossless BF16 retrieval.
Uniform 4-bit attention fails on needle-in-a-haystack workloads for two structural reasons: quantization noise concentrates along dominant key spectral directions (creating correlated score distortion), and retrieval heads operate on razor-thin logit margins where tiny 4-bit noise flips the winning key. ZenMargin suppresses spectral outliers via causal centering and a Fast Walsh-Hadamard Transform (FWHT), then dynamically refines only the 1%–5% of blocks near the online softmax peak.
| Attention Kernel (Llama-3.1-8B 64K, RTX 5090) | Latency (ms) | Throughput (TFLOPS) | Speed vs FA2 | RULER-48K (n=180) | LongBench-10 |
|---|---|---|---|---|---|
| PyTorch SDPA FlashAttn-2 (BF16) | 162.92 ms | 216 TF/s | 1.00x | 99.86% | 49.14 |
| ThriftAttention (5% FP16, raw) | 66.44 ms | 530 TF/s | 2.45x | 98.36% | 49.50 |
| SageAttention2++ (native GQA) | 60.67 ms | 580 TF/s | 2.69x | 100.00% | 49.22 |
| FlashInfer 0.7.6 NVFP4 (`pbm=1/0`) | 52.32 / 42.05 ms | 673 / 837 TF/s | 3.02 / 3.76x | — | — |
| SageAttention3 FP4 (causal mode) | 58.19 / 49.90 ms | 605 / 705 TF/s | 2.80 / 3.27x | 0–3.7% (bug) | — |
| ZenMargin-FWHT (`Δ=1`, E2E) | `37.16 ms` | `947 TF/s` | `4.38x` | `100.00%` | `49.80` |
Reproducible SM120 Benchmark & Kernel Setup
Clone the open-source ZenMargin repository on a Blackwell SM120 workstation (RTX 5090 / RTX Pro 6000) to compile the 12-warp CUDA kernels and run RULER/LongBench parity checks against FlashInfer and SageAttention.
# 1. Clone ZenMargin and build Blackwell SM120 NVFP4 kernels
git clone https://github.com/wrencanfly/ZenMargin.git
cd ZenMargin
pip install -e .
# 2. Run real-activation 64K attention benchmark & RULER parity verification
python benchmarks/bench_attention_sm120.py \
--model meta-llama/Llama-3.1-8B-Instruct \
--seq-len 65536 \
--method zenmargin_fwht \
--delta 1Scaling 128K Agent Context Windows on Single-GPU Workstations
Long-horizon coding and repository audit agents in Smoke Monkey Harness routinely accumulate 32K–128K tokens of source files, compiler traces, and MCP tool outputs. Serving those sessions over a ZenMargin-Cache backend slashes 64K time-to-first-token by up to 1.9x and trims KV-cache footprint by 44% without losing single-needle retrieval accuracy across large monorepos.
Frequently Asked Questions
Q:How does ZenMargin avoid keeping an FP16 backup KV cache?
It splits Q, K, and V into a primary 4-bit NVFP4 limb and a residual 4-bit NVFP4 limb under one shared per-head scale (`rec1` and `rec2`, 288 B/key total), allowing refinement directly on 4-bit tensor cores.
Q:Why does ZenMargin apply causal centering and a Hadamard transform to keys?
Standard 4-bit quantization concentrates error along a few dominant key directions, creating correlated attention-score errors. Causal centering plus FWHT spreads energy uniformly across channels before NVFP4 packing.
Q:What percentage of blocks actually trigger the second 4-bit limb?
Because ZenMargin scans recent keys first and then BOS keys, its online margin gate reads only the primary limb (`rec1`) for 95%–99% of blocks and refines only the 1%–5% of near-peak blocks.
Q:How much faster is ZenMargin than FlashAttention-2 and FlashInfer on RTX 5090?
On Llama-3.1-8B 64K real activations on an RTX 5090, ZenMargin-FWHT hits 947 TFLOPS E2E (4.38x FlashAttention-2, 1.63x SageAttention2++, and faster than FlashInfer 0.7.6 NVFP4) while lifting 32-layer decode serving 2.1–2.9x over FlashInfer BF16.
Related Alternatives & Comparisons
Related Architecture Guides
View all guidesBest Open Source Coding Agents in 2026: Free, Local & Fully Hackable Harnesses
MCP Server Security Best Practices: Hardening Model Context Protocol Agents in 2026
Open Source Coding Agent Harness: Build a Forkable, Local AI Engineering Runtime
Build with Smoke Monkey Harness
Zero dependencies. 24 built-in tools. Human-in-the-loop safety. 100% open source under the MIT License.