English

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

Machine Learning 2026-05-29 v2

Abstract

Modern RL post-training methods such as GRPO and DAPO train on NN response sequences of RR tokens sampled from a shared prompt of PP tokens, but standard FlashAttention replicates all PP prompt tokens NN times across both forward and backward passes -- duplicating compute and memory on identical hidden states. In large-rollout, long-context RL training (N16N{\geq}16, P8KP{\geq}8\text{K}), this redundancy dominates the policy update cost. We observe that in decoder-only models, causal masking makes prompt representations invariant across sequences at every layer, so all per-token operations (norms, projections, MLP) and attention can process the prompt once -- a property not yet exploited at the kernel level for training. We propose \textbf{DualKV}, the first FlashAttention kernel variant that eliminates shared-prompt replication during RL training, via (1)~fused CUDA forward and backward kernels that iterate over two disjoint KV regions -- shared context and per-sequence response -- in a single kernel launch, and (2)~a data-pipeline redesign in veRL that repacks N(P+R)N(P{+}R) tokens into P+NRP{+}NR tokens per micro-batch, extending the token reduction from attention to the entire model by a factor ρ=N(P+R)/(P+NR)\rho = N(P{+}R)/(P{+}NR). DualKV is mathematically equivalent to standard attention and introduces no approximation. On Qwen3-8B GRPO training with 8×\timesH100 GPUs (N=32N{=}32, 8K-context), DualKV achieves 1.631.63--2.09×2.09\times policy-update speedup, enables 2×2\times larger micro-batches, and raises MFU from 36%36\% to 76%76\%. Similar gains hold for DAPO (2.47×2.47\times speedup, 77%77\% MFU). At 30B MoE scale on 16×\timesH100, DualKV achieves 3.82×3.82\times policy-update and 3.38×3.38\times end-to-end step speedup over FlashAttention (which requires 4-way Ulysses sequence parallelism to avoid OOM).

Cite

@article{arxiv.2605.15422,
  title  = {DualKV: Shared-Prompt Flash Attention for Efficient RL Training with Large Rollouts and Long Contexts},
  author = {Jiading Gai and Shuai Zhang and Xiang Song and Bernie Wang and George Karypis},
  journal= {arXiv preprint arXiv:2605.15422},
  year   = {2026}
}
R2 v1 2026-07-22T07:13:23.427Z