modelscope / modelscope/ms-swift
[RFC] Support M2PO for stale-rollout GRPO
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 15.7k
- Forks
- 1.7k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 136
Description
Checklist
- I have searched existing issues and pull requests, and this is a new feature request.
Feature Request Description
I would like to contribute support for M2PO (Second-Moment Trust Policy Optimization) to ms-swift's GRPO training stack.
M2PO was published at ICLR 2026. It targets off-policy RL with stale rollout data, which is directly relevant to the fully async rollout item in the current ms-swift roadmap. Instead of PPO's fixed token-wise clipping interval, the final paper's Algorithm 1 constrains the batch-level second moment of the policy/behavior log-ratio and masks only the largest outliers in the active trust-region quadrants.
Paper and reference implementation:
- Paper: https://arxiv.org/abs/2510.01161
- Reference repository: https://github.com/Infini-AI-Lab/M2PO
Proposed scope
- Add
m2poas a GRPOloss_type, with the paper defaultm2_threshold=0.04. - Compute
log(pi_current / pi_behavior)fromrollout_per_token_logpswhen rollout log-probabilities are available. - Apply M2 masking only to tokens in the two PPO clipping quadrants:
(A > 0, ratio > 1)and(A < 0, ratio < 1). - Remove the largest squared log-ratio outliers until the second moment of the remaining trust-region tokens is at most the configured threshold.
- Preserve the paper's denominator: average the masked objective over all valid completion tokens, not only the unmasked tokens.
- Keep Transformers/HF and Megatron-SWIFT behavior aligned.
- Add diagnostics such as
m2_before,m2_after,masked_fraction, andtrust_region_fraction. - Add deterministic unit tests for masking quadrants, threshold boundaries, padding/empty masks, numerical stability, and loss gradients, plus a small stale-rollout comparison against GRPO.
I already have an independent implementation of the final Algorithm 1 under review in AgentScope/Trinity-RFT#616. I would adapt the implementation and its edge-case tests to ms-swift's current GRPOBatch and distributed training abstractions rather than copying framework-specific code.
Before implementation, I would appreciate maintainer guidance on three details:
- Should the initial PR include both the Transformers/HF and Megatron-SWIFT trainers, or land one backend first?
- Should
loss_type=m2porequirerollout_per_token_logps, or fall back toold_per_token_logpswhen rollout log-probabilities are unavailable? - Should the M2 threshold be computed globally across data-parallel ranks, or per local training batch/micro-batch?
Pull Request
Yes. If this proposal fits the roadmap, I plan to submit the implementation, CPU unit tests, configuration/docs, and a reproducible small-model stale-rollout validation. PRs will be split if maintainers prefer separate backend or validation changes.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the GRPO loss_type path around GRPOBatch and rollout_per_token_logps in both the Transformers/HF and Megatron-SWIFT trainers. Run or add the proposed deterministic CPU unit tests for masking, boundaries, padding, numerical stability, gradients, and stale-rollout comparison. Done means m2po is configured, both backends align, diagnostics are exposed, and the tests and validation pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- ai, distributed-systems, machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100