Skip to content

GRPO baseline groups rows by prompt text instead of by group id (single controller) #4371

Closed

Description

@terrykong

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:

  1. 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
  2. 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.
  3. 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_prompt rows (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.

  1. Tag every row with its group id. Add "group_id": group_id to the row tags in pack_payload (payload.py#L230-L238). Both writers already pass group_id in: the native commit (replay_buffer.py#L1251) and the finalizer (rollout_reassembler.py#L584). A tag is a short string, so it passes assert_metadata_only. It is better than parsing the _g{i} suffix out of sample_ids, which would tie correctness to a naming convention.
  2. Pass an integer group index as prompt_ids. In AdvantageComputer.run (advantage_stage.py#L256), build the key from meta.tags instead of the prompt_ids_for_adv column:
    # 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_prompt only calls torch.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.
  3. Split on real group boundaries. In split_meta_by_prompt_group, find boundaries where the group_id tag changes instead of assuming num_generations_per_prompt rows 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.
  4. Clean up after the switch:
    • prompt_ids_for_adv is 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_ESTIMATORS comment: 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_payload sets the group_id tag 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

accuracybug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions