Skip to content

Fix a bug of preparing Ulysses inputs for loss function in PPO#434

Merged
garrett4wade merged 1 commit intomainfrom
rchardx/fix_ppo
Oct 11, 2025
Merged

Fix a bug of preparing Ulysses inputs for loss function in PPO#434
garrett4wade merged 1 commit intomainfrom
rchardx/fix_ppo

Conversation

@rchardx
Copy link
Collaborator

@rchardx rchardx commented Oct 11, 2025

This pull request refactors how loss masks and input slicing are handled in the Ulysses pipeline, with a focus on improving clarity and correctness for distributed (SP) training. The main changes ensure that the full (unsliced) loss mask is preserved for the loss function, while the sliced version is used for model inputs. Additionally, the code for preparing and slicing inputs has been clarified and made more robust.

Loss mask handling improvements:

  • The loss function (grpo_loss_fn in actor.py) now uses full_loss_mask instead of the sliced loss_mask, ensuring the loss is computed on the correct mask. The slicing of loss_mask is now delegated to ulysses_prepare_inputs.

  • In ulysses_prepare_inputs (ulysses.py), when processing the loss_mask key, the code now saves the full (unsliced) version as full_loss_mask for use in the loss function, and also stores the sliced version for model inputs.

Input preparation and slicing:

  • The ulysses_prepare_inputs function has been refactored for clarity, with improved logic for which tensors to slice and how to handle keys like values, returns, and loss_mask. This includes a more robust check for tensor types and shapes.

  • The function signatures and calls to ulysses_prepare_inputs have been updated throughout the codebase (in both fsdp_engine.py and ulysses.py) to use multi-line arguments for clarity and maintain consistent parameter ordering. [1] [2] [3]

Minor cleanups:

  • Unused variables related to padding size have been removed in fsdp_engine.py, simplifying the code and reducing potential confusion. [1] [2] [3]

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @rchardx, 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 resolves a critical bug in the Ulysses pipeline's PPO loss calculation by ensuring the correct, unsliced loss mask is utilized. It also enhances the overall input preparation logic for distributed training, making it more explicit and robust, particularly concerning how various tensors like values, returns, and loss_mask are handled and sliced for model inputs versus loss computation.

Highlights

  • Loss Mask Correction: The PPO loss function (grpo_loss_fn) now correctly uses the full, unsliced loss mask (full_loss_mask) for accurate loss computation, addressing a bug where a potentially sliced mask might have been used incorrectly.
  • Ulysses Input Preparation Refinement: The ulysses_prepare_inputs function has been significantly refactored to improve clarity and robustness. It now explicitly handles loss_mask by preserving the full mask for the loss function while providing a sliced version for model inputs. It also includes more robust tensor type and shape checks.
  • Code Clarity and Cleanup: Function calls to ulysses_prepare_inputs across fsdp_engine.py and ulysses.py have been updated to use multi-line arguments for better readability. Unused variables related to padding size (ulysses_pad_size) have been removed or ignored, simplifying the codebase.
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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a critical bug in the Ulysses sequence parallelism implementation where the loss function was using sliced loss masks instead of the full mask, which could lead to incorrect loss calculations in distributed training scenarios.

  • Corrected loss mask handling to preserve full masks for loss computation while using sliced versions for model inputs
  • Refactored input preparation logic in ulysses_prepare_inputs with improved tensor type checking and key-specific handling
  • Cleaned up unused variables and improved code formatting for better maintainability

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
areal/utils/ulysses.py Enhanced ulysses_prepare_inputs function with proper loss mask handling and improved tensor slicing logic
areal/engine/ppo/actor.py Fixed loss function to use full_loss_mask instead of sliced loss_mask
areal/engine/fsdp_engine.py Removed unused variables and reformatted function calls for better readability

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

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

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 aims to fix a bug in how inputs are prepared for the PPO loss function when using Ulysses sequence parallelism. The changes correctly delegate input slicing to ulysses_prepare_inputs and introduce a full_loss_mask to be used by the loss function. The code is also refactored for clarity by cleaning up unused variables and improving function call formatting.

While the refactoring is good, I've identified two critical issues with the implementation that could lead to incorrect behavior or runtime errors during training with sequence parallelism:

  1. In ulysses.py, the set of tensors being sliced is incomplete. Important tensors for the actor loss like advantages, logprobs, and prox_logp are not being sliced, which will cause a shape mismatch.
  2. In actor.py, the actor's loss function is changed to use the full_loss_mask. However, since the other tensors it operates on are sliced, this will also result in a shape mismatch. The sliced loss_mask should be used instead.

I've left detailed comments with suggestions to fix these issues. Additionally, a minor comment points out a potentially misleading code comment.

Copy link
Collaborator

@garrett4wade garrett4wade left a comment

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 27c582e into main Oct 11, 2025
1 of 4 checks passed
@garrett4wade garrett4wade deleted the rchardx/fix_ppo branch October 11, 2025 09:33
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.

3 participants