Skip to content

[loss refactor] [2] file structure#753

Open
yueming-yuan wants to merge 15 commits intomainfrom
loss_refactor/copy_paste
Open

[loss refactor] [2] file structure#753
yueming-yuan wants to merge 15 commits intomainfrom
loss_refactor/copy_paste

Conversation

@yueming-yuan
Copy link
Copy Markdown
Collaborator

No description provided.

@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 significantly refactors the loss calculation logic by introducing a new, more modular directory structure. The primary goal is to improve code organization, maintainability, and testability of the various loss functions and their associated helper utilities. This change centralizes related functionalities into distinct modules, making it easier to understand, modify, and extend different components of the loss computation pipeline. Additionally, a robust snapshot testing system has been implemented to guard against unintended regressions during future development.

Highlights

  • Loss Function Refactoring: Loss-related functions, including policy, value, and SFT loss calculations, along with their helpers (e.g., get_responses, get_log_probs_and_entropy, get_values, importance sampling functions), have been moved from miles/backends/training_utils/loss.py into a new modular loss_hub directory structure.
  • New Module Structure: The new loss_hub directory now contains dedicated modules for advantage_estimators, importance_sampling, logit_processing, and losses, promoting better organization and separation of concerns within the training utilities.
  • Advantage Estimator Centralization: The logic for computing advantages and returns, previously spread across conditional blocks in compute_advantages_and_returns, has been consolidated into a new compute_advantages function within loss_hub/advantage_estimators.py.
  • Introduction of Snapshot Testing: A comprehensive snapshot testing framework has been added for loss functions, including utilities for deterministic input generation (loss_test_utils.py) and a parameterized test suite (test_loss_snapshot.py) to ensure bitwise identical outputs across refactors.
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.

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
Copy Markdown
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 is a significant and well-executed refactoring of the loss computation logic. By moving functions from the monolithic loss.py into a new, more modular loss_hub subpackage, the code structure, readability, and maintainability are greatly improved. The addition of a comprehensive snapshot testing framework is particularly commendable, as it provides strong guarantees against regressions. Overall, this is a high-quality change. I have only a few minor stylistic suggestions.

"tis_abs": ice_abs.clone().detach(),
}
pg_loss = pg_loss * ice_weight
return pg_loss, loss_masks, metrics
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.

medium

To adhere to PEP 8, please add a newline character at the end of the file.

Suggested change
return pg_loss, loss_masks, metrics
return pg_loss, loss_masks, metrics
References
  1. PEP 8 recommends that all files should end with a single newline character. (link)

max_seq_lens=max_seq_lens,
)

return res
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.

medium

To adhere to PEP 8, please add a newline character at the end of the file.

Suggested change
return res
return res
References
  1. PEP 8 recommends that all files should end with a single newline character. (link)

{
"loss": loss.clone().detach(),
},
) No newline at end of file
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.

medium

To adhere to PEP 8, please add a newline character at the end of the file.

Suggested change
)
)
References
  1. PEP 8 recommends that all files should end with a single newline character. (link)

@yueming-yuan yueming-yuan force-pushed the loss_refactor/copy_paste branch from e8b358d to ea939b8 Compare March 20, 2026 17:31
@yueming-yuan yueming-yuan changed the title [loss refactor] [2] copy paste to new file structure [loss refactor] [2] change to new file structure Mar 20, 2026
@yueming-yuan yueming-yuan changed the title [loss refactor] [2] change to new file structure [loss refactor] [2] file structure Mar 20, 2026
@yushengsu-thu yushengsu-thu self-assigned this Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants