A2PO ↗
noOriginal Documentation
TRL supports A*-PO (Optimal Advantage Regression) as described in the paper Accelerating RL for LLM Reasoning with Optimal Advantage Regression by Kianté Brantley, Mingyu Chen, Zhaolin Gao, Jason D. Lee, Wen Sun, Wenhao Zhan, and Xuezhou Zhang.
The abstract from the paper is the following:
Reinforcement learning (RL) has emerged as a powerful tool for fine-tuning large language models (LLMs) to improve complex reasoning abilities. However, state-of-the-art policy optimization methods often suffer from high computational overhead and memory consumption, primarily due to the need for multiple generations per prompt and the reliance on critic networks or advantage estimates of the current policy. In this paper, we propose A*-PO, a novel two-stage policy optimization framework that directly approximates the optimal advantage function and enables efficient training of LLMs for reasoning tasks. In the first stage, we leverage offline sampling from a reference policy to estimate the optimal value function V*, eliminating the need for costly online value estimation. In the second stage, we perform on-policy updates using a simple least-squares regression loss with only a single generation per prompt. Theoretically, we establish performance guarantees and prove that the KL-regularized RL objective can be optimized without requiring complex exploration strategies. Empirically, A*-PO achieves competitive performance across a wide range of mathematical reasoning benchmarks, while reducing training time by up to 2× and peak memory usage by over 30% compared to PPO, GRPO, and REBEL.
Usage#
A*-PO assumes a binary, verifiable reward (r ∈ {0, 1}) and runs in two stages:
- Offline value estimation. Before training,
num_value_samplescompletions are sampled from the reference policy for every prompt and scored withreward_funcs. The optimal valueV*(x) = β₁·log(mean_i exp(r(x, yᵢ)/β₁))is estimated and cached per prompt. - On-policy regression. During training, a single completion is generated per prompt from the current policy. The loss is the squared error between the implicit reward
β₂·log(π(y|x)/π_ref(y|x))and the optimal advantager(x, y) − V*(x).
from trl.experimental.a2po import A2POConfig, A2POTrainer
# A*-PO assumes a binary, verifiable reward in {0, 1}.
def reward_correct(completions, ground_truth, **kwargs):
return [float(completion.strip() == truth) for completion, truth in zip(completions, ground_truth)]
training_args = A2POConfig(
output_dir="Qwen2.5-0.5B-A2PO",
num_value_samples=8, # Stage 1: samples per prompt from the reference policy to estimate V*
beta1=0.5, # Stage 1: KL temperature for the V* estimate
beta2=1e-3, # Stage 2: KL temperature for the regression target
)
trainer = A2POTrainer(
model="Qwen/Qwen2.5-0.5B",
reward_funcs=reward_correct,
args=training_args,
train_dataset=...,
)
trainer.train()Because V* is estimated entirely from reference-policy samples, A*-PO cannot exceed the reference policy’s Pass@K. The official implementation can be found at ZhaolinGao/A-PO.
A2POTrainer[[trl.experimental.a2po.A2POTrainer]]#
trl.experimental.a2po.A2POTrainer[[trl.experimental.a2po.A2POTrainer]]#
Trainer for the A*-PO (Optimal Advantage Regression) method, introduced in Accelerating RL for LLM Reasoning with Optimal Advantage Regression.
A*-PO runs in two stages:
- Offline value estimation. Before training,
num_value_samplescompletions are sampled from the reference policy for every training prompt and scored withreward_funcs. The optimal value is estimated asV*(x) = beta1 * log(mean_i exp(r(x, y_i) / beta1))and cached per prompt. - On-policy regression. During training, a single completion is generated per prompt from the current policy.
The loss is the squared error between the implicit reward
beta2 * log(pi(y|x) / pi_ref(y|x))and the optimal advantage estimater(x, y) - V*(x).
traintrl.experimental.a2po.A2POTrainer.trainhttps://github.com/huggingface/trl/blob/v1.6.0/trl/experimental/a2po/a2po_trainer.py#L354[{“name”: “*args”, “val”: “”}, {“name”: “**kwargs”, “val”: “”}]
Parameters:
model (PreTrainedModel or str) : Model to be trained, or a model identifier (string) passed to from_pretrained.
reward_funcs (Callable or list[Callable]) : Reward function(s). Each takes prompts and completions (plus dataset columns as keyword arguments) and returns a list of float rewards. When multiple are provided, their weighted sum (see A2POConfig.reward_weights) is the scalar reward r, which A*-PO assumes to be binary (in {0, 1}).
args (A2POConfig, optional) : Configuration for this trainer. If None, a default configuration is used.
train_dataset (Dataset, optional) : Training dataset. Must contain a "prompt" column.
eval_dataset (Dataset, optional) : Evaluation dataset.
processing_class (PreTrainedTokenizerBase, optional) : Processing class used to process the data. If None, it is loaded from the model’s name with from_pretrained.
callbacks (list[~transformers.TrainerCallback], optional) : List of callbacks to customize the training loop.
optimizers (tuple[~torch.optim.Optimizer, ~torch.optim.lr_scheduler.LambdaLR], optional, defaults to (None, None)) : Tuple containing the optimizer and the learning rate scheduler.
save_model[[trl.experimental.a2po.A2POTrainer.save_model]]#
Will save the model, so you can reload it using from_pretrained().
Will only save from the main process.
push_to_hub[[trl.experimental.a2po.A2POTrainer.push_to_hub]]#
Upload self.model and self.processing_class to the 🤗 model hub on the repo self.args.hub_model_id.
Parameters:
commit_message (str, optional, defaults to "End of training") : Message to commit while pushing.
blocking (bool, optional, defaults to True) : Whether the function should return only when the git push has finished.
token (str, optional, defaults to None) : Token with write permission to overwrite Trainer’s original args.
revision (str, optional) : The git revision to commit from. Defaults to the head of the “main” branch.
kwargs (dict[str, Any], optional) : Additional keyword arguments passed along to ~Trainer.create_model_card.
Returns:
The URL of the repository where the model was pushed if blocking=False, or a Future object tracking the
progress of the commit if blocking=True.
A2POConfig[[trl.experimental.a2po.A2POConfig]]#
trl.experimental.a2po.A2POConfig[[trl.experimental.a2po.A2POConfig]]#
Configuration class for the A2POTrainer.
This class includes only the parameters that are specific to A2PO training. For a full list of training arguments, please refer to the TrainingArguments documentation. Note that default values in this class may differ from those in TrainingArguments.