Architecture
~9 min readUpdated: December 2026

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

ZenMargin Two-Limb NVFP4 Attention and FlashDecoding Kernel Architecture

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.

Technical Review: Smoke Monkey Core Architecture Team
Tested on Node.js 18+ & BunTypeScript 5.x
Quick Answer & Executive Definition

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.

Key Architectural Takeaways
Quick Implementation Examplezenmargin-128k-agent.ts
zenmargin-128k-agent.tstypescript
import { createAgent } from '@smoke-monkey/harness';
// Drive 64K-128K repository RAG on a single 32GB RTX 5090 running ZenMargin-Cache
const 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);
Video Guides

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 FA2RULER-48K (n=180)LongBench-10
PyTorch SDPA FlashAttn-2 (BF16)162.92 ms216 TF/s1.00x99.86%49.14
ThriftAttention (5% FP16, raw)66.44 ms530 TF/s2.45x98.36%49.50
SageAttention2++ (native GQA)60.67 ms580 TF/s2.69x100.00%49.22
FlashInfer 0.7.6 NVFP4 (`pbm=1/0`)52.32 / 42.05 ms673 / 837 TF/s3.02 / 3.76x——
SageAttention3 FP4 (causal mode)58.19 / 49.90 ms605 / 705 TF/s2.80 / 3.27x0–3.7% (bug)—
ZenMargin-FWHT (`Δ=1`, E2E)`37.16 ms``947 TF/s``4.38x``100.00%``49.80`

Shared-Scale Two-Limb Storage & Zero-Repack FlashDecoding

Selective mixed-precision methods like ThriftAttention retain an uncompressed FP16 KV cache alongside 4-bit blocks, sacrificing memory savings. ZenMargin quantizes X^(1) = Q_FP4(X; g_X) and its residual X^(2) = Q_FP4(X - X^(1); g_X) under one shared per-head scale g_X = max|X| / (6 * 448), allowing cross-limb matrix multiplies (Q1K1 + Q2K1 + Q1K2) to accumulate natively on NVFP4 tensor cores with zero rescaling.

Each 64-key stage occupies 288 B/key total (144 B for rec1 + 144 B for rec2), saving 44% KV-cache VRAM vs BF16. During decoding, ZenMargin-Cache reads the exact prefill layout with zero repacking, consuming ~146 B/key per step and boosting 32-layer serving throughput by 2.1–2.9x over FlashInfer BF16.

Prefer Calibration-Free ZenMargin-FWHT Over Banded Calibration

When serving out-of-distribution RoPE lengths (such as Qwen2.5-7B at 38.7K tokens), 6K-calibrated `ZenMargin-Band` drops to 64.12% on RULER, whereas calibration-free `ZenMargin-FWHT` holds 81.71% (matching BF16's 82.21%).

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.

bash
# 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 1

Scaling 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.

Google Search Questions & Answers

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 guides

Build with Smoke Monkey Harness

Zero dependencies. 24 built-in tools. Human-in-the-loop safety. 100% open source under the MIT License.

npm install smoke-monkey-harness