Enable NVFP4 KV Cache on RTX 5090 — FlashAttention-2 Patch for vLLM

·5 min readlocal-ai

TL;DR: NVFP4 KV cache on vLLM is officially SM100-only (Blackwell server GPUs). On RTX 5090 (SM120, consumer Blackwell), a community patch forces the KV cache path to use FlashAttention-2 instead of the TRTLLM-gen FlashInfer path, enabling NVFP4 KV cache on consumer hardware.

NVFP4 KV cache compresses the key-value cache from 2 bytes to 0.5 bytes per element — a 4x reduction that makes long-context inference feasible on a single 32 GB GPU. On RTX 5090, this requires a community patch because upstream vLLM gates the NVFP4 path to SM100 (datacenter Blackwell), using TRTLLM-gen kernels that don’t exist for SM120. This post covers the architecture gap, the FlashAttention-2 fallback patch, every launch flag explained, and practical workarounds for the three SM120 limitations (GDN prefill, CUDA graph corruption, accuracy).

Why NVFP4 KV cache is SM100-only

vLLM’s NVFP4 KV cache (--kv-cache-dtype nvfp4) compresses the KV cache from BF16 (2 bytes per element) to FP4 (0.5 bytes per element) — a 4x reduction. For a 27B model at 262K context, that’s the difference between ~29 GB of BF16 KV cache and ~7 GB of NVFP4 KV cache.

Upstream vLLM restricts NVFP4 KV cache to SM100 (Blackwell server-class GPUs like GB200). The check is explicit in vllm/v1/attention/backends/flashinfer.py:

if kv_cache_dtype == "nvfp4":
return (
current_platform.is_device_capability_family(100)
and supports_trtllm_attention(is_prefill=True)
and supports_trtllm_attention(is_prefill=False)
)

The NVFP4 KV cache path uses TRTLLM-generated cuBIN kernels (trtllm_batch_decode_with_kv_cache / trtllm_batch_context_with_kv_cache) that are only compiled for SM100. When you try it on any other architecture, vLLM raises:

--kv-cache-dtype nvfp4 requires the SM100 trtllm-gen FlashInfer path.

SM120 is the compute architecture in the RTX 5090 — the consumer-class Blackwell GPU. It shares the Blackwell family but targets a different segment. SM120 has no TRTLLM-gen NVFP4 KV cache cubins, so vLLM rejects it outright.

The patch: fall back to FlashAttention-2

hikarioyama/vllm-nvfp4-kv-sm120 patches vLLM to route NVFP4 KV cache through FlashAttention-2 on SM120. The main patch (04-vllm-flashinfer-sm120-nvfp4-kv.patch) adds a _use_fa2_for_nvfp4_kv_on_sm120() gate that checks for compute capability 12.0/12.1 and sets backend = "fa2" instead of "trtllm-gen".

The supporting patches handle NVFP4-specific details:

  • 01-flashinfer-page-split-kv-offsets.patch — Adds independent V-stride fields to FlashInfer’s paged_kv_t struct, enabling separate K/V tensors with different last-dim sizes (fp8 K + nvfp4 V).
  • 02-flashinfer-prefill-inkernel-vsf-deswizzle.patch — In-kernel de-swizzling of V scale-factors (eliminates the +5.5% scale-factor scratch cache).
  • 03-flashinfer-codegen-sf-stride.patch — Explicit sf_stride_page/h/n parameters in FlashInfer’s JIT codegen for NVFP4 scale-factor tensors.

The FA2 path is SM120-only — gated by cc major == 12. On all other GPUs, the patch is inert.

Why FA2 works where FA4 doesn’t

FlashAttention-4’s Blackwell kernels (flash_fwd_sm100.py) depend on three SM100-exclusive features: tcgen05 ISA (asynchronous MMA + TMEM ops), TMEM (256 KB on-chip tensor memory), and thread-block clusters (>1 CTA). SM120 lacks all three — no tcgen05 instructions, zero TMEM, cluster size limited to 1. Attempting FA4 on SM120 produces ptxas errors or dispatch failures. This is a fundamental architectural gap, not a software issue.

FA2 uses the older CUTLASS/sm80-style mma.sync GEMM path with no tcgen05 or TMEM dependency, fitting within SM120’s 99 KiB SMEM ceiling. On a single GPU with --max-num-seqs 6, the throughput difference between FA2 and FA4 is negligible — FA4’s advantages materialize at large batch sizes and multi-GPU scale.

Setup

Terminal window
uv venv .venv-nvfp4 --python 3.13 --managed-python
source .venv-nvfp4/bin/activate
uv pip install \
"vllm>=0.25.0" \
"flashinfer-python>=0.6.13" \
"nvidia-cutlass-dsl>=4.5.2" \
--torch-backend=auto
vllm --version

Install and apply the SM120 patches:

Terminal window
git clone https://github.com/hikarioyama/vllm-nvfp4-kv-sm120.git
cd vllm-nvfp4-kv-sm120
FORCE=1 ./apply_patches.sh

FORCE=1 bypasses the version check — the patch script pins an older vLLM version, but the patches apply cleanly against 0.25.x.

Launch command

Terminal window
CUDA_VISIBLE_DEVICES=0 \
NCCL_P2P_DISABLE=1 \
VLLM_SKIP_P2P_CHECK=1 \
VLLM_USE_FLASHINFER_SAMPLER=1 \
FLASHINFER_MAX_NUM_SEQS=6 \
vllm serve ./morosystems/ThinkingCap-Qwen3.6-27B-NVFP4 \
--host 0.0.0.0 \
--port 8082 \
--tensor-parallel-size 1 \
--attention-backend flashinfer \
--quantization modelopt \
--kv-cache-dtype nvfp4 \
--max-model-len auto \
--max-num-seqs 6 \
--max-num-batched-tokens 8192 \
--gpu-memory-utilization 0.90 \
--safetensors-load-strategy prefetch \
--enable-chunked-prefill \
--enable-prefix-caching \
--reasoning-parser qwen3 \
--tool-call-parser qwen3_xml \
--enable-auto-tool-choice \
--served-model-name Qwopus3.6-27B-v2-MTP-NVFP4-GGUF.gguf \
--chat-template ./chat_template-fixes/chat_template.jinja \
--gdn-prefill-backend triton \
--kv-offloading-backend native \
--kv-offloading-size 16 \
--compilation-config '{"cudagraph_mode":"PIECEWISE"}' \
--override-generation-config '{"temperature": 0.7}' \
--generation-config auto

Flag breakdown

Core quantization & attention

FlagPurpose
--kv-cache-dtype nvfp4Enables FP4 KV cache. Only works on SM120 after the patch.
--quantization modeloptLoads ModelOpt-format NVFP4 checkpoints. Detects quant_algo=NVFP4 automatically.
--attention-backend flashinferRequired on Blackwell. Auto-detection does not select FlashInfer and falls back to slower native kernels.

Scheduling & batching

FlagPurpose
--max-num-seqs 6Concurrent sequence limit. Must match FLASHINFER_MAX_NUM_SEQS=6.
--max-num-batched-tokens 8192Max tokens per scheduling step. Works with chunked prefill to handle long prompts incrementally.
--enable-chunked-prefillBreaks long prompts into chunks of max_num_batched_tokens. Prevents OOM on long-context requests.
--enable-prefix-cachingReuses KV cache prefixes across requests. Uses Mamba cache ‘align’ mode for GDN layers.

SM120 workarounds

FlagPurpose
--gdn-prefill-backend tritonGDN prefill must use Triton. FlashInfer’s GDN kernels were historically gated to Hopper (device_major == 9); SM120 support was added later in source but the pre-built cuBIN package ships zero SM120 binaries (flashinfer-ai/flashinfer#3294). Prefill is a significant part of the workload — this is a real limitation until nVIDIA ships SM120 cuBIN binaries.
--compilation-config '{"cudagraph_mode":"PIECEWISE"}'Required workaround — FULL CUDA Graph mode produces garbled output with NVFP4 KV cache + MTP speculative decoding (vLLM #49011). The drafter’s captured forward pass corrupts state shared with the target model, likely in the GDN recurrent state. PIECEWISE mode avoids the bug by capturing smaller, per-size graphs.

Generation config override

FlagPurpose
--override-generation-config '{"temperature": 0.7}'Mitigates NVFP4 accuracy loss. Quantization noise in the KV cache acts as an effective temperature boost of ~0.1–0.3 (Tethered Reasoning §3.2). At the model’s default T=1.0, the effective temperature reaches ~1.1–1.3, amplifying quantization artifacts into wrong words and hallucinations — especially at low-entropy tokens (digits, operators, precise facts). Dropping to T=0.7 counteracts the noise, restoring effective temperature to ~0.8–1.0. The ReQAT paper (§3.2–3.3) confirms FP4 quantization increases tail-mass at sampled tokens, and lower temperature narrows the softmax to reduce non-top-1 sampling.
--generation-config autoUse the model’s own generation_config.json as the base (for top_k, top_p, etc.), then apply the temperature override on top. Without this, vLLM would use its own defaults instead of the checkpoint’s config.

Without the override, the model shipped with temperature: 1.0. In practice, NVFP4 KV cache at that temperature produced wrong words in generated blog content — the quantization noise flipped logit rankings at critical tokens. The T=0.7 override is a practical mitigation, not a fix; it trades diversity for reliability.

KV offloading

FlagPurpose
--kv-offloading-backend nativeOffload KV cache pages to CPU when GPU cache is full. Lets you serve requests longer than the GPU KV cache can hold.
--kv-offloading-size 16Preallocate 16 CPU tensors for offloaded KV pages.

Environment variables

  • VLLM_USE_FLASHINFER_SAMPLER=1 — Use FlashInfer’s top-p/top-k sampler instead of PyTorch’s. Faster on GPU.
  • FLASHINFER_MAX_NUM_SEQS=6 — Hard limit compiled into FlashInfer kernels. Must match --max-num-seqs.
  • NCCL_P2P_DISABLE=1 / VLLM_SKIP_P2P_CHECK=1 — Skip P2P bandwidth checks for single-GPU setups.

Startup timeline

Cold start (no compilation cache):

PhaseDuration
Weight loading (prefetch + load)2.22s
torch.compile (Dynamo + AOT)30.23s
FlashInfer autotune (5× fp4_gemm runs)31.97s
Initial profiling/warmup33.86s
CUDA graph capture (PIECEWISE)1s
Total engine init106.69s

Subsequent starts benefit from two caches:

  • torch.compile cache — compiled graphs saved to ~/.cache/vllm/torch_compile_cache/ and reused, dropping compilation from ~30s to ~5s.
  • FlashInfer autotune cache — kernel configs saved to ~/.cache/vllm/flashinfer_autotune_cache/ and reused, dropping autotune from ~32s to near-zero.

What the logs confirm

Key confirmation lines on startup:

Using nvfp4 data type to store kv cache
Using FlashInferCutlassNvFp4LinearKernel for NVFP4 GEMM
Using AttentionBackendEnum.FLASHINFER backend
Using FlashInfer for top-p & top-k sampling
Using Triton/FLA GDN prefill kernel (requested=triton, head_k_dim=128)

KV cache allocation:

Available KV cache memory: 7.32 GiB
GPU KV cache size: 397,109 tokens
Maximum concurrency for 262,144 tokens per request: 1.51x

Memory breakdown

On an RTX 5090 (32 GB VRAM) with Qwen3.6 27B NVFP4:

ComponentSize
Model weights (NVFP4)18.77 GB
KV cache (NVFP4, 397K tokens)7.32 GB
CUDA graphs (PIECEWISE)0.06 GB
Total~26.15 GB
Headroom~5.85 GB

Without NVFP4 KV cache, BF16 KV cache at the same token budget would consume ~29 GB — exceeding the 32 GB VRAM once weights and overhead are added.

Trade-offs

  1. FlashAttention-2 instead of TRTLLM-gen FA4 — FA2 uses mma.sync GEMM with no tcgen05 or TMEM dependency. Throughput on single-GPU small-batch inference is adequate. FA4’s advantages (tcgen05 async copy, TMEM accumulators, thread-block clusters) require SM100 hardware that SM120 simply doesn’t have.
  2. GDN prefill on Triton — GDN (Gated Delta Net) is a linear attention mechanism used in Qwen3.5’s hybrid architecture (24/32 GDN layers + 8 standard attention layers). FlashInfer’s SM120 GDN kernels exist in source but the cuBIN package ships none. Prefill is a significant workload cost; Triton handles it but is slower than native kernels.
  3. Accuracy — NVFP4 KV cache introduces quantization noise that acts as an effective temperature boost of ~0.1–0.3. At T=1.0, this pushes the model into a regime where quantization artifacts more frequently flip logit rankings, producing wrong words (especially at low-entropy tokens like digits and operators). Lowering temperature to T=0.7 counteracts the noise. For accuracy-critical workloads, T ≤ 0.8 is recommended with NVFP4 KV cache. If you need exact numerical parity, stick with BF16 KV cache.

References

  1. vllm-nvfp4-kv-sm120 — hikarioyama, GitHub — https://github.com/hikarioyama/vllm-nvfp4-kv-sm120
  2. NVFP4 KV Cache FA2 Fallback (vLLM RFC #47684) — vLLM Project, GitHub — https://github.com/vllm-project/vllm/issues/47684
  3. FULL CUDA Graph + NVFP4 + MTP Garbled Output (vLLM #49011) — vLLM Project, GitHub — https://github.com/vllm-project/vllm/issues/49011#issuecomment-5028173020
  4. No SM120 cuBIN Binaries (FlashInfer #3294) — FlashInfer, GitHub — https://github.com/flashinfer-ai/flashinfer/issues/3294
  5. SM120 Attention Kernel Wiring (FlashInfer #2555) — FlashInfer, GitHub — https://github.com/flashinfer-ai/flashinfer/issues/2555
  6. FlashInfer Attention Library — FlashInfer AI, GitHub — https://github.com/flashinfer-ai/flashinfer
  7. Tethered Reasoning: Quantization Noise as Effective Temperature Boost — arXiv (2025) — https://arxiv.org/abs/2602.17691
  8. ReQAT: FP4 Quantization Tail-Mass and Sampling Accuracy — arXiv (2025) — https://arxiv.org/abs/2606.15682
  9. NVIDIA ModelOpt Quantization Framework — NVIDIA, GitHub — https://github.com/NVIDIA/ModelOpt

This article was written by Pi (Gemini 2.5 Pro | Google).