Repository navigation
Description
Problem
The GRPO baseline decides which responses belong together by comparing their prompt tokens, not by the group they were generated in. calculate_baseline_and_std_per_prompt groups rows with torch.unique(prompts, dim=0) (utils.py#L146), and on the single-controller path those prompts are the prompt_ids_for_adv token rows (payload.py#L174).
This causes three problems:
- Two groups with the same prompt text share one baseline, but only if they land in the same call. Whether they share depends on streaming chunk boundaries on main. With feat(sc): compute advantages in dedicated and sharded Ray actors #4326 it also depends on
num_advantage_workers, because the new split can put them on different actors. Datasets that repeat prompts hit this. For example, DAPO-Math-17k stores each of its 17,917 prompts 100 times, which gives about 7 same-prompt pairs per 512-prompt chunk. Worked example with real numbers: https://terrykong.github.io/gh-pages-poc/terryk/pr-4326-duplicate-prompts.html - Rows of one group must have identical prompt tokens, or the group splits. The code already works around this. The token-capture finalizer overwrites every row's prompt with one valid sibling's prefix (rollout_reassembler.py#L511-L524). If an environment rewrites or compacts the context, the token key is fragile.
- The feat(sc): compute advantages in dedicated and sharded Ray actors #4326 splitter cannot find group boundaries on its own. It assumes every group has
num_generations_per_promptrows (advantage_stage.py#L194-L211). Any feature with variable-size groups, such as Never Give Up, would be cut silently: https://terrykong.github.io/gh-pages-poc/terryk/pr-4326-dapo-ngu-design.html
The right key already exists. The controller mints one group_id per group, and every row's sample id is f"{group_id}_g{i}" on both the native path (payload.py#L227) and the token-capture path (rollout_recovery.py#L247). The estimators' own docstrings already describe prompt_ids as an identifier: "identifying which prompt each sample belongs to" (advantage_estimator.py#L95).
Proposed fix (single-controller path)
Key the baseline on the group id instead of on prompt tokens.
- Tag every row with its group id. Add
"group_id": group_idto the row tags inpack_payload(payload.py#L230-L238). Both writers already passgroup_idin: the native commit (replay_buffer.py#L1251) and the finalizer (rollout_reassembler.py#L584). A tag is a short string, so it passesassert_metadata_only. It is better than parsing the_g{i}suffix out ofsample_ids, which would tie correctness to a naming convention. - Pass an integer group index as
prompt_ids. InAdvantageComputer.run(advantage_stage.py#L256), build the key frommeta.tagsinstead of theprompt_ids_for_advcolumn:# One integer per distinct group; the baseline only compares rows for equality. index = {gid: i for i, gid in enumerate(dict.fromkeys(t["group_id"] for t in meta.tags))} prompt_ids = torch.tensor([[index[t["group_id"]]] for t in meta.tags], dtype=torch.long)
calculate_baseline_and_std_per_promptonly callstorch.unique(..., dim=0)and compares rows for equality, so an[N, 1]integer tensor works unchanged. Raise if any row is missing the tag, rather than falling back to tokens. - Split on real group boundaries. In
split_meta_by_prompt_group, find boundaries where thegroup_idtag changes instead of assumingnum_generations_per_promptrows per group. That makes variable-size groups safe, and the "decline" path for a row count that is not a multiple of the group size is no longer needed. - Clean up after the switch:
prompt_ids_for_advis no longer read on this path, so it can stop being fetched (AdvantageStageConfig.input_fields).- The finalizer's "copy a sibling's prompt" workaround is no longer needed for the baseline.
- Update the
SHARD_INVARIANT_ESTIMATORScomment: with a group key, a whole-group shard really does give the same numbers as the whole batch.
Tests
- In
tests/unit/single_controller/test_advantage_stage.py: two groups with identical prompt tokens but different group ids get separate baselines, and the sharded result equals the unsharded one (today they differ). - Splitter: groups of different sizes (e.g. 4, 12, 4 rows) are never cut.
pack_payloadsets thegroup_idtag on both paths.
Scope and numerics
- This changes numbers only on data where two groups in one call share prompt text. Unique-prompt data is unaffected. Please include a short same-seed A/B on a repeated-prompt dataset in the PR.
- Sync GRPO (
grpo.py,grpo_sync.py) has no per-row group id and keeps the token key. Aligning it is a separate change.
Can land in #4326 or right after it.
Activity