Prefix Grouper: Efficient GRPO Training through Shared-Prefix Forward

Zikang Liu, Tongtian Yue, Yepeng Tang, Longteng Guo, Junxian Cai, Qingbin Liu, Xi Chen, Jing LiuarXiv 2025 · DOI

Abstract

Group Relative Policy Optimization (GRPO) has emerged as a powerful technique for optimizing large language models (LLMs) without the overhead of a separate critic model. However, standard GRPO implementations are computationally inefficient, redundantly re-processing shared prompt prefixes for every response in a group. We introduce Prefix Grouper, a novel training algorithm that utilizes a Shared-Prefix Forward strategy to encode shared prefixes exactly once. Prefix Grouper significantly reduces FLOPs and memory usage—approaching a $1/G$ reduction for a group size $G$—while remaining mathematically equivalent to standard GRPO, ensuring identical training outcomes with vastly improved efficiency.

Introduction

Reinforcement learning from human feedback (RLHF) often relies on actor-critic frameworks. GRPO simplifies this by using group-relative rewards, but at a high computational cost: for a group of $G$ responses to a single prompt, the prompt is encoded $G$ times. This redundancy becomes a critical bottleneck as prompt lengths increase, particularly in long-context tasks or multi-modal applications. Prefix Grouper addresses this by restructuring the attention mechanism to share computations across the group.

Methods: Shared-Prefix Forward

The core innovation of Prefix Grouper is the Shared-Prefix Forward strategy. Instead of treating each prompt-response pair as an independent sequence, the method decomposes the self-attention mechanism into two specialized components:

  1. Prefix Token Attention: The shared prefix is encoded once. Tokens within the prefix attend only to each other, eliminating the $G-1$ redundant encodings present in standard GRPO.
  2. Suffix Token Attention: Each response (suffix) computes its own query embeddings and attends to the combined sequence of the shared prefix and its specific suffix.
Architecture of Prefix Grouper showing the Shared-Prefix Forward strategy with Prefix and Suffix Token Attention.

This approach is implemented via a "concat-attention" mechanism that maintains full differentiability, making it suitable for end-to-end training. Unlike inference-time KV-caching, Prefix Grouper is designed for backpropagation and acts as a plug-and-play replacement for standard attention layers.

Key Insights

  • Mathematical Equivalence: Prefix Grouper is not an approximation. It is proven to produce the exact same forward outputs and backward gradients as standard GRPO. This ensures that model performance remains identical while training speed increases.
  • Theoretical Efficiency Limit: As the ratio of prefix length to suffix length grows, the computational cost of training asymptotically approaches $1/G$ of the baseline cost. This is particularly beneficial for tasks involving long documents or extensive context.
  • Generalizability: The shared-prefix logic is applicable beyond GRPO to any scenario requiring multiple outputs from a single long input, such as multi-question answering or diverse reasoning path generation.

Key Results

The authors evaluated Prefix Grouper across varying prefix lengths (up to 16,384 tokens) and group sizes (up to 16).

FLOPs reduction achieved by Prefix Grouper compared to baseline across various group sizes and prefix-to-suffix ratios.
  • Computational Savings: Prefix Grouper consistently reduced FLOPs across all tested configurations. The efficiency gains were most dramatic at higher prefix-to-suffix ratios and larger group sizes.
  • Memory Optimization: By reducing redundant activations, the method significantly lowers GPU memory requirements. This allows for larger group sizes or longer contexts on the same hardware, which can improve RL stability by reducing gradient variance.

Limitations and Future Work

While Prefix Grouper is architecturally "plug-and-play," it requires careful implementation of attention masks and positional encodings (e.g., RoPE) to ensure the concatenated sequences are treated correctly. Future work could involve developing specialized CUDA kernels to further optimize the "Grouped Attention" operation's memory layout. Additionally, while the method provides universal benefits, the most significant gains are realized in "long-prefix, short-suffix" scenarios; tasks with very long generated responses will see more moderate improvements.

Summary updated

BibTeX
@misc{liu2025prefix,
  author = {Zikang Liu and Tongtian Yue and Yepeng Tang and Longteng Guo and Junxian Cai and Qingbin Liu and Xi Chen and Jing Liu},
  title = {{Prefix Grouper: Efficient GRPO Training through Shared-Prefix Forward}},
  year = {2025},
  eprint = {2506.05433},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.05433},
}
Figure