Key Implementation Points of Single-Trajectory Asynchronous Optimization in Agent Reinforcement Learning
This article explores the implementation of Single-Trajectory Asynchronous Optimization (SAO) in Agent reinforcement learning. It compares SAO with group sampling, explains its basic workflow and prerequisites, details the implementation of Direct Bilateral Importance Sampling (DIS), and provides configuration recommendations for value models.
1. Why Focus on Single-Trajectory Sampling
In long-horizon Agent tasks, the model interacts with the environment over multiple rounds, causing the context length to increase rapidly. Group sampling requires generating multiple trajectories per task before estimating gradients, leading to memory and computational overhead that grows with both group size and trajectory length. Longer trajectories increase the discrepancy between the policy used during data generation and the policy during updates, making off-policy issues more pronounced.
Single-Trajectory Asynchronous Optimization (SAO) takes a different approach: it generates only one trajectory per task and uses it immediately for updates. The table below compares the two methods.
| Dimension | Group Sampling | Single-Trajectory Asynchronous Optimization |
|---|---|---|
| Trajectories per task | Multiple | Single |
| Memory peak | Scales with group size | Lower, mainly affected by single context length |
| Off-policy step gap | In-group waiting widens the gap | Data consumed immediately, smaller step gap |
| Gradient variance | In-group baseline reduces variance | Lacks in-group comparison, variance usually higher |
| Suitable tasks | Dense rewards, shorter trajectories | Long-horizon tasks, requires fine-grained scheduling in the training pipeline |
2. Basic Concept of SAO
The SAO workflow can be broken down into three steps: sample a trajectory, immediately compute the training signal for that trajectory, and then update the model asynchronously. This brings two direct benefits. First, the gap between the parameters during data generation and during updates becomes smaller, keeping the data fresher. Second, the pipeline does not need to wait for all trajectories in a group to complete, allowing for finer scheduling granularity.
These benefits come with prerequisites. In tasks with sparse rewards, a single trajectory often fails to obtain effective rewards, resulting in weak training signals. In high-variance tasks, the lack of in-group comparison increases gradient noise. Therefore, before choosing SAO, it is essential to examine the task’s reward density and trajectory length distribution.
3. How to Implement Direct Bilateral Importance Sampling (DIS)
In asynchronous training, the behavior policy μ that generates the trajectory and the current policy π_θ to be updated are not identical. Importance sampling corrects this bias using probability ratios. The original text only mentions “comprehensively considering the bilateral probability distribution of the generation and update stages” without providing specific formulas. Below is a common implementation; actual details should be based on the code of the framework used.
First, calculate the ratio token by token:
rt = πθ(a_t | s_t) / μ(a_t | s_t)
Then apply bilateral clipping, restricting the ratio to the interval [c_low, c_high]:
w_t = clip(r_t, c_low, c_high)
w_t is multiplied by the policy gradient term of the token. Clipping prevents individual ratios from becoming too large and causing gradient explosions while retaining a certain degree of bias correction.
The pseudocode is as follows:
def dis_weight(logp_cur, logp_behav, c_low=0.5, c_high=2.0):ratio = torch.exp(logp_cur - logp_behav) # Token-wise ratiow = torch.clamp(ratio, c_low, c_high) # Bilateral clippingreturn w.detach() # Weights do not backpropagate gradientsdef policy_loss(logp_cur, logp_behav, adv, mask):w = dis_weight(logp_cur, logp_behav)loss = -(w * adv * logp_cur) * maskreturn loss.sum() / mask.sum()
c_low and c_high are hyperparameters that need tuning; the 0.5 and 2.0 in the example are just starting points.
Compared with other methods:
- Standard importance sampling does not use clipping, resulting in high variance and unstable training when ratios deviate.
- PPO’s ratio clipping mainly limits the magnitude of policy changes during multiple updates on the same batch of data and does not directly correct the distribution shift of a single data point.
- V-trace clips the importance ratio and is used for asynchronous actor-critic with value estimation. Its form is similar to DIS, but the clipping targets and value estimation methods differ.
For small latency and short trajectories, standard clipping is usually sufficient. For large latency, clipping combined with continuous monitoring is required.
4. How to Configure the Value Model
The value model estimates the value of a state, and the accuracy of the advantage function depends on it. The original text provides several settings: update the policy and value models at a 1:2 ratio, freeze the attention modules, only train the projection layers of the MoE, and expand the pretraining data for the value model. These are the empirical configurations reported in the original text, which did not provide ablation experiments. Therefore, they should be treated as starting points and validated on your own tasks.
Suggested verification order is as follows:
