DualKV: Shared-Prompt Flash Attention for Efficient RL Training with Large Rollouts and Long Contexts

Jiading Gai, Shuai Zhang, Xiang Song, Bernie Wang, George Karypis

TL;DR

DualKV is a novel FlashAttention variant and data pipeline redesign that eliminates redundant prompt replication during reinforcement learning (RL) post-training (e.g., GRPO, DAPO). By processing a shared prompt only once across both forward and backward passes, it delivers up to 3.8× policy-update speedups and massive memory savings without any mathematical approximations.

What Problem Does This Paper Address?

Modern RL post-training methods like Group Relative Policy Optimization (GRPO) and Direct Alignment Policy Optimization (DAPO) sample $N$ different response sequences from a single shared prompt to achieve more stable and accurate reward signals. However, standard attention mechanisms like FlashAttention-2 treat these as $N$ independent sequences, replicating the prompt $N$ times.

For long prompts ($P \ge 8K$) and large rollout factors ($N \ge 16$), this duplication dominates compute and memory costs. Every token operation (norms, projections, MLPs) and attention calculation recomputes identical prompt hidden states $N-1$ times. This redundancy frequently leads to out-of-memory (OOM) errors, forcing engineers to rely on communication-heavy sequence parallelism even when a single GPU has enough capacity for the unique tokens.

Methods

The authors introduce DualKV, which eliminates shared-prompt replication at both the kernel and system levels through several key innovations:

  1. Two-Region Fused CUDA Kernels: DualKV splits attention into context self-attention (computed once for the prompt) and a new decoded attention kernel. This custom kernel handles two physically disjoint KV regions—the shared prompt context and the per-sequence response—within a single kernel launch.
  2. Data-Pipeline Redesign: Implemented within the veRL framework, DualKV repacks the micro-batch from $N(P+R)$ tokens down to $P+NR$ tokens. This extends the token reduction beyond attention to the entire model, saving compute on LayerNorms, projections, and MLP layers.
  3. fp32 Atomic Accumulation: In the backward pass, $N$ sequences concurrently write gradients into the single shared prompt KV buffer. DualKV resolves this concurrent-write race condition and prevents precision loss by using fp32 atomic writes in a two-stage design before casting the final aggregated gradient back to bf16.
  4. Architectural Extensions: DualKV supports a head dimension of 512 and hybrid sliding-window/global attention (required for modern architectures like Gemma-4) and seamlessly integrates with Ulysses sequence parallelism for ultra-long contexts.

Datasets

  • LongReason: A synthetic long-context mathematical reasoning benchmark used for primary end-to-end evaluation, featuring prompts up to 8K tokens and responses up to 2K tokens.
  • GSM8K: A standard grade-school math dataset, augmented with synthetic prefixes to test DualKV across varying prompt lengths in a lower-reduction regime.

Key Insights and Ideas

  • Prompt Invariance via Causal Masking: In decoder-only models, causal masking ensures that prompt tokens only attend to preceding prompt tokens, never to the generated responses. Consequently, prompt hidden states are completely identical across all $N$ rollouts at every layer, meaning $N-1$ copies are entirely redundant.
  • Kernel-Level vs. Framework Optimization: Prior framework-only attempts (like Prefix Grouper) still pass replicated tensors to the attention kernel, failing to solve the $O(N \cdot P \cdot d)$ memory bottleneck. True efficiency requires a custom kernel capable of reading from disjoint memory regions.
  • Decoupling Memory from Batch Size: With DualKV, the memory cost of the prompt is paid exactly once. This allows practitioners to freely scale up micro-batch sizes in long-prompt regimes without experiencing a massive memory penalty.

Key Results

  • Policy-Update Speedups: On Qwen3-8B GRPO training (8×H100, $N=32$, 8K context), DualKV achieved a 1.63–2.09× policy-update speedup and allowed 2× larger micro-batches, boosting Model FLOPs Utilization (MFU) from 36% to 76%.
Per-phase step breakdown
  • Scale and Multi-Node Performance: At a 30B Mixture-of-Experts (MoE) scale on 16×H100 GPUs, DualKV delivered a 3.82× policy-update speedup and a 3.38× end-to-end step speedup over FlashAttention-2, which required 4-way Ulysses sequence parallelism just to avoid OOMs.
  • Memory Scaling: As shown in Figure 4, at a 96K prompt length, standard FlashAttention would theoretically require up to 775 GB of memory (far exceeding an H100's 80GB capacity), whereas DualKV keeps the memory footprint flat and well within physical limits.
Memory scaling comparison

Limitations and Future Work

  • Single Shared Prefix Constraint: The current design assumes a single shared prompt per micro-batch group, meaning benefits scale strictly with the prompt-to-response ratio ($P/R$).
  • Future Directions: The authors plan to generalize the kernel to handle arbitrary tree structures, which would support multi-turn conversations with partially overlapping prefixes or tree-of-thought search graphs. They also aim to port DualKV to newer hardware backends, including FlashAttention-3 (Hopper) and FlashAttention-4 (Blackwell).

Summary updated

BibTeX
@inproceedings{gai2026dualkv,
  author = {Jiading Gai and Shuai Zhang and Xiang Song and Bernie Wang and George Karypis},
  title = {{DualKV: Shared-Prompt Flash Attention for Efficient RL Training with Large Rollouts and Long Contexts}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=viyAwqIA4H},
}
Figure