Skip to content

Conversation

@mnoukhov
Copy link
Contributor

@mnoukhov mnoukhov commented Nov 24, 2025

move stop_rate and other metrics before mask_truncated_responses filtering that skews results

add masked_truncated_responses as a metric to log


Note

Compute metrics before truncation filtering, add masked_truncated_responses, refactor filtering, and centralize sequence-length metrics.

  • Training pipeline (open_instruct/grpo_fast.py):
    • Compute val/stop_rate, good_outputs, and sequence-length metrics before applying truncation filtering.
    • Add val/masked_truncated_responses and include centralized sequence-length metrics in logged outputs.
    • Refactor truncation filtering into maybe_mask_truncated_completions(...) and use its outputs.
    • Simplify eval stop_rate calculation using boolean mean.
  • Utilities (open_instruct/rl_utils.py):
    • Add calculate_sequence_length_metrics(...) to produce aggregate and per-class (solved/unsolved) length stats.
    • Export and use the new helper from grpo_fast.py.}

Written by Cursor Bugbot for commit 735e876. This will update automatically on new commits. Configure here.

move stop_rate and other metrics before mask_truncated_responses
filtering that skews results

add masked_truncated_responses as a metric to log
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @mnoukhov, 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 refactors the metric calculation logic within grpo_fast.py to ensure accuracy. It moves several key metrics, such as stop rate and sequence lengths, to be computed prior to the masking of truncated responses. This change guarantees that these metrics reflect the complete set of generated responses before any data is discarded. Furthermore, it enhances observability by adding a new metric to track the exact count of responses that are masked due to truncation.

Highlights

  • Metric Calculation Order: Metrics such as stop_rate, good_outputs, and sequence lengths are now calculated before any filtering of truncated responses. This ensures these metrics reflect the original, unfiltered data and prevents skewed results.
  • New Metric for Truncation: A new metric, masked_truncated_responses, has been introduced to explicitly log the number of responses that were filtered out because they did not finish with a 'stop' reason.
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

@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 refactors metric calculations in grpo_fast.py to ensure they are computed before response filtering, which prevents skewed results. It also introduces a new metric to log the number of masked truncated responses. The changes are logical and align with the goal of improving metric accuracy. I have a few suggestions to improve the code further by simplifying some calculations, fixing a potential inconsistency in how 'unsolved' responses are defined, and using more idiomatic NumPy operations.

Comment on lines 2039 to 2044
sequence_length_solved = (
np.array([]) if np.all(scores == 0) else np.array(sequence_lengths[scores == args.max_possible_score])
)
sequence_length_unsolved = (
np.array([]) if np.all(scores == args.max_possible_score) else np.array(sequence_lengths[scores == 0])
)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The calculation for sequence_length_unsolved appears to be inconsistent with how unsolved_num_responses is calculated later (line 2124). unsolved_num_responses considers any score less than args.max_possible_score as unsolved, while sequence_length_unsolved only considers scores equal to 0. This can lead to confusing metrics if there are non-zero, non-max scores.

Additionally, the conditional logic for both sequence_length_solved and sequence_length_unsolved is redundant. NumPy's boolean array indexing will correctly return an empty array if no elements match the condition, making the if np.all(...) checks unnecessary.

I suggest simplifying these calculations and making sequence_length_unsolved consistent with unsolved_num_responses.

sequence_length_solved = sequence_lengths[scores == args.max_possible_score]
sequence_length_unsolved = sequence_lengths[scores < args.max_possible_score]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 2028 to 2030
stop_rate = sum(int(finish_reason == "stop") for finish_reason in result.finish_reasons) / len(
result.finish_reasons
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This calculation for stop_rate is correct, but it can be expressed more idiomatically and concisely using numpy.mean. This improves readability and leverages numpy's optimized operations for boolean arrays.

stop_rate = np.mean([fr == "stop" for fr in result.finish_reasons])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Collaborator

@finbarrtimbers finbarrtimbers left a comment

Choose a reason for hiding this comment

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

Can we pull these out into two functions:

  1. calculate_sequence_lengths(result) -> dict[str, float]:
  2. maybe_mask_truncated_completions(result) -> tuple[GenerationResult, Batch, dict[str, float]]:
    Then it becomes trivial to reorder them.

cleaner metric implementations
separate out metric implementation into function
separate out whether to truncate into function
@mnoukhov
Copy link
Contributor Author

mnoukhov commented Nov 26, 2025

/gemini review

@gemini-code-assist
Copy link
Contributor

Hello @mnoukhov, it looks like you might have intended to use a command with /gemini. Please specify a command such as /gemini review, /gemini summary, or /gemini help to get assistance. If you'd like me to perform a code review, please use /gemini review.

- mean, hist of correct lengths
- mean, hist of incorrect results
"""
sequence_lengths = np.array([len(response) for response in result.responses])
Copy link

Choose a reason for hiding this comment

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

Bug: Parameter name mismatch causes undefined variable error

The function calculate_sequence_length_metrics has a parameter named results (plural) but the function body on line 261 uses result.responses (singular). This variable result is undefined in the function scope, causing a NameError at runtime. The call site in grpo_fast.py passes a single GenerationResult object named result, so the parameter should be renamed from results to result and the type hint changed from list[GenerationResult] to GenerationResult.

Fix in Cursor Fix in Web

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.

4 participants