Repository navigation
feat(rl): implement REINFORCE++ variants with global normalization - #202
Conversation
f6a0d42 to
83dffaf
Compare
|
整体看下来有几个地方建议再调整一下:
if global_count.item() == 0: 这个函数在每个训练 batch 都会走,.item() 会触发 GPU 到 CPU 的同步,和项目规范冲突。建议改成不触发 host sync 的设备端/异步校验,或者把空 global batch 的检查移到训练热路径之外。
|
Co-authored-by: MaybeIcanShow <[email protected]>
83dffaf to
6d03e71
Compare
|
感谢审阅,三个问题均已调整:
验证结果包括:Task 29 定向测试 51 passed、 |
|
辛苦开下ready to review |
|
感谢指出。问题确认为 GitHub Markdown Preview 与 VitePress public-root 的路径解析差异:原来的 已在 commit 验证结果:
|
|
补充说明:此前评论中引用的文件链接固定在旧提交 当前 PR head 已是 |
…edai-studio#202) #### Summary - Closes redai-studio#192。 - 新增两个命名明确、实现相互独立的优势估计器: `reinforce_plus_plus` 和 `reinforce_plus_plus_baseline`。 - 基于 REINFORCE++ v9 主公式和固定版本的 OpenRLHF 归一化参考,明确并冻结 return、baseline、mask、分布式归一化、KL 与 reduction 语义,同时记录二者在 KL 处理上的差异。 - 使用独立的总体统计实现,确保现有 GRPO、GSPO 和 SAPO 行为保持不变。 #### Changes - Registers both estimator names and validates their mutually exclusive k1/k2 configuration contracts. - Implements masked token KL reward shaping and reverse returns for REINFORCE++. - Implements an inclusive same-prompt group-mean baseline without group-std normalization, followed by global valid-token normalization. - Adds a real DP-aware masked population normalizer (`ddof=0`, variance floor `1e-8`) with coordinated empty-population handling. - Uses boolean masking in return, normalization, and reduction paths so masked `NaN`/`Inf` storage cannot contaminate valid tokens; a fully masked local response remains collective-safe. - Integrates response-mean PPO and independent k2 loss semantics plus algorithm-specific monitoring metrics in the Megatron loss path. - Adds independent float64 element-wise references and a real two-process Gloo test; covers variable masks, padding, zero reward, zero variance, one token, one empty local rank, PPO clipping, and reduced policy/KL losses. - Adds one parameterized Qwen3-0.6B recipe for GRPO and both new variants. - Adds a design guide and an equal-budget three-seed training/evaluation report with path-free CSV evidence, an 18-job SHA256 evidence index, and SVG curves. CLI compatibility: - Existing estimator defaults and GRPO/GSPO/SAPO paths are unchanged. - The two new variants intentionally reject fully-async, hybrid, CP > 1, per-token global loss reduction, and inconsistent KL settings because those modes do not satisfy the documented closed-global-batch contract. - The baseline also rejects custom reward/advantage hooks that would bypass its frozen inclusive group-mean semantics. #### Verification Environment and source: - Experiment source: `5f7cd574372288391bb1c41ca0677422cd31e725` - PR base after rebase: `0bc99af8dd39de8fd99c588a98b3f3a463bc818c` - Current report commit: `83dffaf` - Current submission head: `83dffaf` - A `git range-diff` marks all five trained Task 29 commits patch-identical before and after the upstream rebase. - GPU image: Python 3.12.3, PyTorch 2.11.0+cu129, CUDA 12.9, Ray 2.56.0, SGLang 0.5.12.post1; one NVIDIA A40 per formal run. Focused host tests after rebase: ```bash PYTHONNOUSERSITE=1 PYTHONPATH=$PWD python -m pytest -q \ tests/core/test_registry_reinforce_plus_plus.py \ tests/utils/test_arguments_reinforce_plus_plus.py \ tests/utils/training/test_reinforce_plus_plus.py \ tests/utils/test_distributed_masked_normalize.py \ tests/utils/test_reinforce_reward_processing.py \ tests/backends/megatron/test_reinforce_plus_plus_loss.py \ tests/backends/megatron/test_fp16_optimizer_config.py \ tests/utils/training/test_ppo_utils_grpo.py \ tests/utils/test_arguments_opd_teacher_colocate.py \ tests/core/test_registry_sft.py ``` Result: `55 passed, 2 skipped in 20.37s`. The host skips require Megatron, which is not installed in that Conda environment. After repository formatters modified six Task 29 files, the focused Task 29 selection was rerun and passed: `43 passed, 1 skipped in 45.97s`; the skip is again the unavailable Megatron-dependent upstream FP16 test. Final pre-review mask/edge-case audit: ```text 62 passed, 2 skipped in 10.65s ``` This selection adds masked `NaN`/`Inf`, fully masked local response, all-zero reward/KL, safe reduction, and production-dispatch integration coverage. The two skips are upstream modules requiring a full Megatron installation. Broader host regression: ```bash NUMBA_CACHE_DIR=/tmp/task29-numba-cache \ PYTHONNOUSERSITE=1 PYTHONPATH=$PWD python -m pytest -q \ tests/utils tests/core \ --ignore=tests/utils/test_metrics_service.py \ --ignore=tests/utils/test_train_metric_utils.py ``` Historical post-rebase result: `204 passed, 12 skipped, 3 warnings in 18.61s`. After the final audit additions: `209 passed, 12 skipped, 3 warnings in 26.02s`. Post-rebase pinned-container selection: `57 passed, 1 skipped, 30 warnings in 22.65s`. The skip is the upstream FP16 module because the standalone container process cannot import the image's root-owned Megatron checkout; no Task 29 test is skipped in that result. At the experiment commit, the pinned container also passed 43 Task 29 tests and 14 metrics tests. GPU stability and equal-budget comparison: - 3 algorithms x 3 training seeds x 50 steps: all nine runs retained 50 rollout records, 50 Actor updates, finite metrics, and iteration 49. - 9 final-checkpoint evaluations x 1,024 responses on an identical frozen 256-prompt subset: all accepted jobs passed summary/TensorBoard/cleanup gates. - Fixed-subset reward/pass@1, mean ± sample SD across training seeds: - GRPO: `0.6077 ± 0.0133` - REINFORCE++: `0.5866 ± 0.0135` - REINFORCE++-baseline: `0.6042 ± 0.0215` - Mean pass@4: GRPO `0.7773`, REINFORCE++ `0.7734`, baseline `0.7891`. - All paired reward 95% t-intervals cross zero; no superiority claim is made. - `train/ppo_kl` is documented as old/current policy drift rather than reference KL. Across the three REINFORCE++ seeds, the last-10 mean `rollout/returns - rollout/raw_reward` differences are `-0.017225`, `-0.018403`, and `-0.017273`, confirming nonzero k1 return shaping; the baseline separately reports finite k2 `train/kl_loss`. - Full commands, numerical semantics, per-seed results, incidents, curves, and limitations are in `docs/en/guide/reinforce-plus-plus-training-report.md`. - Sanitized raw logs, all 18 expanded commands, GPU samples, metrics and an internal manifest are published in the [reproducibility evidence release](https://github.com/zheself/Relax/releases/tag/task29-reinforcepp-evidence-c72caf1). Archive SHA256: `0b52d8e8e6a85ff534e16569dd48c9dbef336402a2c80b6aa96b8bf2ffd7834f`. Additional checks: - `git diff --check`: pass. - Changed Python modules and the evidence summarizer compile: pass. - Recipe `bash -n`: pass. - Eight SVGs parse with `rsvg-convert`: pass. - 450 published training-step rows match the generated evidence export: pass. - The 18 accepted-job SHA256 rows match the retained local manifest and job IDs: pass. - Path/credential scan of the report assets: pass. - The published evidence archive was downloaded again from GitHub; its outer SHA256, gzip stream, safe tar paths, 72-file internal manifest and repeated sensitive-data scan all passed. - Ruff check/format, Python compilation, recipe syntax, conflict-marker, ASCII-box, CSV/SVG and sensitive-path checks on the final local delta: pass. - Current head `83dffaf` passed GitHub pre-commit, lint and the Python 3.10/3.11/3.12 test matrix (five successful checks). - `npm run docs:build`: pass with Node 20.19.5 and VitePress 1.6.4. Generated OpenAPI runtime differences were discarded and are not part of this diff. #### Risk & Rollback - Global population statistics depend on using the intended data-parallel process group; the real Gloo tests cover unequal and empty local populations, but the first implementation deliberately rejects async and CP modes whose ownership/batch boundary is not closed. - The two algorithms reject inconsistent configuration instead of silently falling back to another KL or reduction convention. - The small-model comparison has only three training seeds, one fixed evaluation subset/decoding seed, no initial-checkpoint control, and high response truncation. It validates stability, not algorithm superiority. - Rollback is selecting an existing estimator or reverting these Task 29 commits. No checkpoint format, Controller/Service path, or existing algorithm default changes. #### Checklist - [x] Diff only contains Task 29 implementation, tests, recipe, design, and validation evidence. - [x] New and relevant tests pass in the documented host/container scopes. - [x] Documentation, parameterized recipe, raw path-free tables, and curves are included. - [x] No credentials, datasets, checkpoints, internal paths, or private machine identifiers are included. - [x] `pre-commit run --all-files`. - [x] VitePress documentation build. - [x] Current-head CI. - [ ] Maintainer review after the PR leaves Draft. --------- Co-authored-by: 郑宝炜 <[email protected]>

Summary
reinforce_plus_plus和reinforce_plus_plus_baseline。return、baseline、mask、分布式归一化、KL 与 reduction 语义,同时记录二者在
KL 处理上的差异。
Changes
configuration contracts.
REINFORCE++.
normalization, followed by global valid-token normalization.
ddof=0, variance floor1e-8) with coordinated empty-population handling.NaN/Infstorage cannot contaminate valid tokens; a fully masked localresponse remains collective-safe.
algorithm-specific monitoring metrics in the Megatron loss path.
test; covers variable masks, padding, zero reward, zero variance, one token,
one empty local rank, PPO clipping, and reduced policy/KL losses.
with path-free CSV evidence, an 18-job SHA256 evidence index, and SVG curves.
CLI compatibility:
per-token global loss reduction, and inconsistent KL settings because those
modes do not satisfy the documented closed-global-batch contract.
frozen inclusive group-mean semantics.
Verification
Environment and source:
5f7cd574372288391bb1c41ca0677422cd31e7250bc99af8dd39de8fd99c588a98b3f3a463bc818c83dffaf83dffafgit range-diffmarks all five trained Task 29 commits patch-identicalbefore and after the upstream rebase.
SGLang 0.5.12.post1; one NVIDIA A40 per formal run.
Focused host tests after rebase:
PYTHONNOUSERSITE=1 PYTHONPATH=$PWD python -m pytest -q \ tests/core/test_registry_reinforce_plus_plus.py \ tests/utils/test_arguments_reinforce_plus_plus.py \ tests/utils/training/test_reinforce_plus_plus.py \ tests/utils/test_distributed_masked_normalize.py \ tests/utils/test_reinforce_reward_processing.py \ tests/backends/megatron/test_reinforce_plus_plus_loss.py \ tests/backends/megatron/test_fp16_optimizer_config.py \ tests/utils/training/test_ppo_utils_grpo.py \ tests/utils/test_arguments_opd_teacher_colocate.py \ tests/core/test_registry_sft.pyResult:
55 passed, 2 skipped in 20.37s. The host skips require Megatron,which is not installed in that Conda environment.
After repository formatters modified six Task 29 files, the focused Task 29
selection was rerun and passed:
43 passed, 1 skipped in 45.97s; the skip isagain the unavailable Megatron-dependent upstream FP16 test.
Final pre-review mask/edge-case audit:
This selection adds masked
NaN/Inf, fully masked local response, all-zeroreward/KL, safe reduction, and production-dispatch integration coverage. The
two skips are upstream modules requiring a full Megatron installation.
Broader host regression:
NUMBA_CACHE_DIR=/tmp/task29-numba-cache \ PYTHONNOUSERSITE=1 PYTHONPATH=$PWD python -m pytest -q \ tests/utils tests/core \ --ignore=tests/utils/test_metrics_service.py \ --ignore=tests/utils/test_train_metric_utils.pyHistorical post-rebase result:
204 passed, 12 skipped, 3 warnings in 18.61s.After the final audit additions:
209 passed, 12 skipped, 3 warnings in 26.02s.Post-rebase pinned-container selection:
57 passed, 1 skipped, 30 warnings in 22.65s. The skip is the upstream FP16 module because the standalone containerprocess cannot import the image's root-owned Megatron checkout; no Task 29
test is skipped in that result. At the experiment commit, the pinned container
also passed 43 Task 29 tests and 14 metrics tests.
GPU stability and equal-budget comparison:
rollout records, 50 Actor updates, finite metrics, and iteration 49.
256-prompt subset: all accepted jobs passed summary/TensorBoard/cleanup
gates.
0.6077 ± 0.01330.5866 ± 0.01350.6042 ± 0.02150.7773, REINFORCE++0.7734, baseline0.7891.train/ppo_klis documented as old/current policy drift rather thanreference KL. Across the three REINFORCE++ seeds, the last-10 mean
rollout/returns - rollout/raw_rewarddifferences are-0.017225,-0.018403, and-0.017273, confirming nonzero k1 return shaping; thebaseline separately reports finite k2
train/kl_loss.limitations are in
docs/en/guide/reinforce-plus-plus-training-report.md.internal manifest are published in the
reproducibility evidence release.
Archive SHA256:
0b52d8e8e6a85ff534e16569dd48c9dbef336402a2c80b6aa96b8bf2ffd7834f.Additional checks:
git diff --check: pass.bash -n: pass.rsvg-convert: pass.IDs: pass.
SHA256, gzip stream, safe tar paths, 72-file internal manifest and repeated
sensitive-data scan all passed.
ASCII-box, CSV/SVG and sensitive-path checks on the final local delta: pass.
83dffafpassed GitHub pre-commit, lint and the Python3.10/3.11/3.12 test matrix (five successful checks).
npm run docs:build: pass with Node 20.19.5 and VitePress 1.6.4. GeneratedOpenAPI runtime differences were discarded and are not part of this diff.
Risk & Rollback
process group; the real Gloo tests cover unequal and empty local
populations, but the first implementation deliberately rejects async and CP
modes whose ownership/batch boundary is not closed.
falling back to another KL or reduction convention.
evaluation subset/decoding seed, no initial-checkpoint control, and high
response truncation. It validates stability, not algorithm superiority.
commits. No checkpoint format, Controller/Service path, or existing
algorithm default changes.
Checklist
validation evidence.
included.
machine identifiers are included.
pre-commit run --all-files.