Skip to content

fix: keep PPO token stats consistent under context parallelism - #990

Merged
garrett4wade merged 3 commits into
areal-project:mainfrom
yash27-lab:codex/fix-cp-token-stats
Mar 6, 2026
Merged

fix: keep PPO token stats consistent under context parallelism#990
garrett4wade merged 3 commits into
areal-project:mainfrom
yash27-lab:codex/fix-cp-token-stats

Conversation

@yash27-lab

Copy link
Copy Markdown
Contributor

Summary

  • derive n_tokens from full-batch metadata instead of local sliced tensors
  • apply the fix in PPO actor and critic logging paths
  • add regression tests for the cu_seqlens / context-parallel case

Verification

  • ran python -m pytest -q tests/test_ppo_stats.py tests/test_prox_approx.py in a local Python 3.12 CPU environment
  • result: 39 passed

Note

I was able to verify the logging path and adjacent PPO tests locally, but I do not have a GPU-capable environment to run FSDP/Ulysses integration tests here. If someone can validate this on a GPU setup, that would be helpful.

Closes #627

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an inconsistency in how n_tokens statistics were calculated within the PPO trainer, particularly when context parallelism is active. By introducing a new utility function, infer_token_denominator, the system now prioritizes full-batch metadata to accurately determine the total number of tokens, preventing skewed statistics that could arise from using sliced tensors. This ensures that token counts remain consistent and reliable across different execution environments.

Highlights

  • Consistent Token Stats: Derived n_tokens from full-batch metadata instead of local sliced tensors to ensure consistent token statistics, especially under context parallelism.
  • PPO Logging Path Fixes: Applied the fix in both the PPO actor and critic logging paths to correctly account for tokens.
  • Regression Tests: Added regression tests specifically for the cu_seqlens / context-parallel case to validate the n_tokens calculation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • areal/trainer/ppo/actor.py
    • Imported infer_token_denominator function.
    • Updated ppo_update to use infer_token_denominator for n_tokens calculation.
    • Updated grpo_loss_fn to use infer_token_denominator for n_tokens calculation, removing previous comments about n_tokens shape.
  • areal/trainer/ppo/critic.py
    • Imported infer_token_denominator function.
    • Updated ppo_loss_fn to use infer_token_denominator for n_tokens calculation.
  • areal/trainer/ppo/stats.py
    • Added new file stats.py containing the infer_token_denominator function.
    • Implemented infer_token_denominator to derive the full token mask from attention_mask, cu_seqlens, or input_ids from input_data, falling back to a provided tensor if none are available.
  • tests/test_ppo_stats.py
    • Added new test file test_ppo_stats.py.
    • Added test_grpo_loss_fn_uses_full_cu_seqlens_for_n_tokens to verify correct n_tokens shape in grpo_loss_fn when cu_seqlens is present.
    • Added test_critic_loss_fn_uses_full_cu_seqlens_for_n_tokens to verify correct n_tokens shape in ppo_loss_fn when cu_seqlens is present.
Activity
  • Ran pytest -q tests/test_ppo_stats.py tests/test_prox_approx.py in a local Python 3.12 CPU environment, resulting in 39 passed tests.
  • Verified the logging path and adjacent PPO tests locally.
  • Requested validation on a GPU setup for FSDP/Ulysses integration tests due to lack of local GPU environment.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses an issue with inconsistent PPO token statistics when using context parallelism by introducing a new utility function, infer_token_denominator. This function correctly derives the total token count from the full-batch metadata rather than sliced tensors. The fix is applied consistently across both the PPO actor and critic logging paths, and new regression tests are included to verify the behavior. The changes are logical and well-implemented. My review includes a couple of suggestions to enhance the new utility's maintainability and test coverage.

Comment thread areal/trainer/ppo/stats.py Outdated
Comment thread tests/test_ppo_stats.py

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5ba8c32d53

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread areal/trainer/ppo/stats.py Outdated
@yash27-lab

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in follow-up commits:

  • 6ea0a13: refactored infer_token_denominator and added direct coverage for the attention_mask, input_ids, and metadata-fallback branches.
  • 4c4d3b8: fixed the tree-attention edge case by only reusing input_ids when it matches the stat tensor shape, and added a regression test that exercises vocab-stat logging against packed tree inputs.

Local verification:

/tmp/AReaL/.venv/bin/python -m pytest -q tests/test_ppo_stats.py tests/test_prox_approx.py

44 passed in 8.13s

This is still CPU-side validation only, so GPU-capable tree/FSDP confirmation would still be helpful.

@garrett4wade garrett4wade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@garrett4wade
garrett4wade merged commit 412d224 into areal-project:main Mar 6, 2026
5 checks passed
dingzhiqiang pushed a commit that referenced this pull request Mar 16, 2026
* fix: keep PPO token stats consistent under context parallelism

* test: cover infer_token_denominator branches

* fix: avoid padded tree input_ids for n_tokens
leandermaben pushed a commit to leandermaben/AReaL that referenced this pull request Mar 24, 2026
…-project#990)

* fix: keep PPO token stats consistent under context parallelism

* test: cover infer_token_denominator branches

* fix: avoid padded tree input_ids for n_tokens
SathyaGnanakumar pushed a commit to danielkiely/AReaL that referenced this pull request Apr 29, 2026
…-project#990)

* fix: keep PPO token stats consistent under context parallelism

* test: cover infer_token_denominator branches

* fix: avoid padded tree input_ids for n_tokens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] The number of tokens recorded when enabling context parallel with FSDP is not correct

2 participants