LongStraw: Long-Context RL Beyond 2M Tokens under a Fixed GPU Budget
Summary (Overview)
- LongStraw enables million-token GRPO post-training on a fixed GPU budget (8×H20 for Qwen, 32×H20 for GLM) by separating the long prompt from the short response computation.
- The shared prompt is evaluated once without automatic differentiation; only the architecture-specific state (recurrent/attention/MoE) needed by later tokens is retained. Response branches are replayed serially one at a time under autograd, reducing the live training graph from the full sequence to a single response branch.
- Two substantially different model families are implemented: the hybrid recurrent + full-attention Qwen3.6-27B and the compressed-attention Mixture-of-Experts GLM-5.2.
- On eight H20 GPUs, Qwen completes grouped scoring and response backward at 2.1M context positions with group sizes 2 and 8; a stress test extends the envelope to 4.46M positions. On 32 H20 GPUs, GLM validates a 2.1M-token prompt across all 78 layers.
- The key finding is that state lifetime and physical ownership are the primary determinants of the practical context limit, rather than attention sparsity or parameter count alone.
Introduction and Theoretical Foundation
AI agents operate over long trajectories (ReAct, LongCat-Flash-Thinking), creating a growing gap between inference-time context lengths (approaching millions) and post-training lengths (often ≤256K tokens). While inference can cache prompt state and discard computational graphs, RL post-training must score and backpropagate through multiple responses conditioned on the same shared prompt - a much heavier memory burden.
Group Relative Policy Optimization (GRPO) compares responses within a group through normalized advantages. For a prompt of tokens, group member with scored tokens, and responses, the policy objective is:
where is the importance ratio, and is the normalized group-relative advantage.
Existing techniques (FlashAttention, LoRA, QLoRA, activation checkpointing) reduce individual costs but leave the live autograd graph spanning the full prompt and all responses competing for device memory. Scale-out approaches (Ring Attention, DeepSpeed-Ulysses, ByteScale) distribute work across many GPUs; LongStraw asks the complementary question: how far can GRPO go when the accelerator count stays fixed?
Methodology
LongStraw Design
LongStraw separates the long prompt from short response computation through a four-phase transaction:
- Prompt capture: Evaluate under current parameters with autograd disabled. Save only the model-specific conditional state needed by future tokens; release all transient hidden states, attention scratch, FFN activations, and routing buffers.
- Pre-step scoring: Materialize old-policy and reference-policy response log-probabilities before any policy backward, with parameters fixed through the group.
- Policy replay: For each group member, rebuild only the short response path under autograd, backpropagate, and immediately free that member's graph. Reuse the same read-only prompt state.
- Optimizer transaction: Accumulate local gradients over all members, synchronize, and issue one optimizer call per worker.
The dominant activation scale changes from to , with total resource accounting:
This bounds the live policy graph by the largest member rather than by the number of members. The critical trade-off: serial replay increases elapsed time but avoids keeping the prompt graph and every response graph live simultaneously.
State Inventory and Ownership
The captured prompt state is architecture-specific:
| State component | Placement & ownership | Scaling with |
|---|---|---|
| Qwen GDN recurrent state | GPU per rank (CP8) | Fixed size (independent of ) |
| Qwen full-attention KV pages | Compact GPU pages across CP8 for 16 layers | per rank |
| GLM MLA latent pages | CPU; Megatron-zigzag CP32 shards for 78 layers | per rank |
| GLM DSA indexer-key pages | CPU; CP32 shards for 21 index-computing layers | at those layers |
For Qwen at 2,088,960 prompt tokens with CP8, page size :
BF16 KV storage per rank across 16 full-attention layers:
Parallel Layout and Key Operations
| Path | Layout | Response-time collectives | Audited semantic boundary |
|---|---|---|---|
| Qwen3.6-27B | 8 H20, CP8 | Global CP8 max/normalizer/value-sum merge (BF16 numerator) | Global response-attention forward preserved; dQ all-reduced; dK/dV adapter gradients not synchronized |
| GLM-5.2 | 32 H20, TP1/CP32/EP32/ETP1/PP1 | EP32 dispatch/combine; DSA CP-local (no global candidate merge) | All ranks complete 78-layer backwards; custom path skips Megatron gradient finalization for CP-replicated non-expert adapters |
Global Full-Attention Forward Composition (Qwen)
For response query , rank returns local output and log-normalizer . Global output is reconstructed without moving KV pages:
Then , . This requires one MAX all-reduce and two SUM all-reduces per composed output.
Empirical Validation / Results
Audited Execution Receipts
Table 5: Fixed-budget execution receipts on 8 H20s (Qwen) and 32 H20s (GLM), 2.1M context positions.
| Path | Prompt / response | Hardware/layout | Terminal evidence on every worker | Wall (s) | Peak (GB) | |
|---|---|---|---|---|---|---|
| Qwen global forward | 2,088,960 / 8,192 input ×2 | 2 | 8 H20, CP8 | Old+ref+backward+local AdamW call | 5,198.780 | 97.503 |
| Qwen global forward | 2,088,960 / 8,192 input ×8 | 8 | 8 H20, CP8 | 8 serial members + local AdamW call | 6,785.225 | 97.711 |
| GLM CP-local DSA | 2,097,152 / 3 input (2 scored) | 2 | 32 H20, TP1/CP32/EP32 | Two old scores + 2×78-layer backward + optimizer call | 2,975.138 | n/r |
Qwen Group Scaling
- to : peak memory increases only 0.208 GB (0.213%); wall time grows 1,586.445 s. Post-prefix cost per member remains ∼266–271 s.
- The shared prefix (∼4,656 s) dominates total time; amortizing it over more members reduces mean wall time per response from 2,599.39 to 848.15 s.
- 4.25M stress test: 4,456,448 positions (4,448,256 prompt + 8,192 response) complete G=8 response replay at 82.960 GB peak. Under a prefix-frozen objective, 8 consecutive G=8 optimizer steps (64 member replays) run at 83.894 GB peak.
GLM Terminal Manifest
- 32/32 ranks report complete: 2 old score phases, 2 live 78-layer policy backwards, 1 optimizer call per worker.
- Trace inventory: 128 JSONL files, 35,584 events. Policy traces record 78 forward and 78 backward layer-end events per rank.
- CPU prompt state: 5.8125 GiB per rank (72 MiB per MLA layer + 16 MiB per index-key set). Per-layer GPU staging: 72–88 MiB.
- Gradient composition gap: custom path bypasses
finalize_model_grads, so CP-replicated non-expert adapter gradients remain unreduced before the distributed optimizer call.
Memory Accounting and Evidence Levels
The paper defines four levels of evidence:
- Execution capacity: All requested backwards, collectives, optimizer events complete (✓ both paths).
- Response-operator fidelity: Qwen CP8 global merge ✓; GLM DSA CP-local only (×).
- Distributed-update consistency: Qwen dK/dV reduction missing; GLM gradient finalization skipped (×).
- Full-gradient parity: Prompt state detached in both; no numerical reference exists (×).
Theoretical and Practical Implications
Key systems lessons:
- Fixed-budget capacity comes from lifetime management, not sparsity alone. Both models evaluate every prompt token through every layer; the gain comes from allowing prompt scratch, intermediates, and routing buffers to die before response backward. DSA reduces attention arithmetic but retains long-context indexing; MoE activates only selected experts but creates dispatch/combine overhead.
- Physical ownership must match logical ownership. A context shard is only useful when its physical allocation is also sharded. Copying selected pages into right-sized tensors (rather than retaining view parent allocations) is critical.
- Dense and MoE architectures move the peak to different places. For Qwen, context-growing storage is concentrated in 16 full-attention KV sets (48 GDN layers remain fixed-size). For GLM, full-sequence storage is dominated by expanded routed hidden buffers (6 GiB for one balanced BF16 buffer at 65K-token shard), not by attention scratch.
- Context parallelism and expert parallelism are orthogonal. CP partitions token history; EP partitions model parameters. CP collectives cannot replace EP collectives and vice versa.
- Group scaling is a scheduling result. Serial replay makes group size primarily a time dimension, not a memory dimension. vs changes peak memory by only 0.2%.
- Forward fidelity, update consistency, and gradient parity are distinct claims. The paper achieves level 1 (execution capacity) for both models, level 2 for Qwen only, and neither reaches levels 3 or 4.
Practical implications: By reducing reliance on ever-larger GPU clusters, LongStraw enables more researchers and smaller teams to explore long-context training under limited accelerator budgets. The explicit identification of missing gradient composition paths provides a concrete roadmap for reaching full training correctness.
Conclusion
LongStraw demonstrates that long-context GRPO under a fixed GPU budget is a tensor-lifetime and ownership problem, not a context-length race or a single-kernel optimization. By evaluating the shared prompt once without autograd, retaining only architecture-specific state, and replaying response branches serially, LongStraw composes million-token GRPO-shaped execution on 8 H20 GPUs for Qwen and 32 H20 GPUs for GLM – a four- to eightfold increase over the native context settings of these models.
The current results establish execution capacity, not full training correctness. The prompt state is detached from the backward pass, and both implementations have incomplete distributed gradient composition:
- Qwen: shard-local dK/dV contributions to replicated KV projection adapters are not synchronized across CP8 ranks.
- GLM: the custom replay path bypasses Megatron gradient finalization, leaving CP-replicated non-expert adapter gradients unreduced.
Closing these gaps – restoring gradient finalization, validating global DSA candidate/output composition, and running full-gradient parity comparisons – is the necessary next step before complete online RL training. The paper provides a clear dependency-ordered validation roadmap toward that goal.
Related papers
- Hierarchical Sparse Attention Done Right: Toward Infinite Context Modeling
HiLS-Attention achieves 512x length extrapolation and 15x speedup via hierarchical softmax and learnable chunk summaries optimized end-to-end.
- TurboServe: Serving Streaming Video Generation Efficiently and Economically
TURBOSERVE reduces worst-case per-chunk latency by 37.5% and GPU cost by 37.2% through joint migration-aware placement and load-driven autoscaling.
- BlockPilot: Instance-Adaptive Policy Learning for Diffusion-based Speculative Decoding
BlockPilot adaptively selects block size per sample via a lightweight predictor, achieving up to 4.20x speedup in diffusion-based speculative decoding.