Skip to content

Fix correctness bugs in rl.py, rl_replacements.py, and vision.py#3811

Merged
danielhanchen merged 2 commits into
mainfrom
fix/correctness-bugs
Jan 1, 2026
Merged

Fix correctness bugs in rl.py, rl_replacements.py, and vision.py#3811
danielhanchen merged 2 commits into
mainfrom
fix/correctness-bugs

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

@danielhanchen danielhanchen commented Jan 1, 2026

Summary

  • rl_replacements.py:864,870 - Fixed undefined nanmin/nanmax functions. PyTorch doesn't have torch.nanmin/torch.nanmax, so replaced with .nan_to_num(nan=float('inf')).min() and .nan_to_num(nan=float('-inf')).max() pattern
  • vision.py:150 - Fixed bug where code checked for "input" key but accessed kwargs["input_ids"] instead of kwargs["input"]
  • vision.py:159 - Fixed bug where literal string "key" was used instead of variable key when accessing kwargs
  • rl.py:903,905 - Fixed non-existent MathError exception by replacing with ValueError

Test plan

  • Run GRPO training to verify importance sampling ratio metrics are logged correctly
  • Test vision model generation with various input kwargs patterns
  • Verify temperature validation raises proper ValueError

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @danielhanchen, 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 several correctness bugs across the rl.py, rl_replacements.py, and vision.py modules. The changes ensure robust handling of numerical operations involving NaN values, correct parsing of input arguments in vision models, and proper exception types for validation, leading to more stable and predictable behavior within the system.

Highlights

  • RL Replacements: Fixed undefined nanmin and nanmax functions in rl_replacements.py by implementing a PyTorch-compatible pattern using .nan_to_num(nan=float('inf')).min() and .nan_to_num(nan=float('-inf')).max() for importance sampling ratio metrics.
  • Vision Model Input Handling: Corrected a bug in vision.py where the code checked for an 'input' key but incorrectly accessed kwargs["input_ids"] instead of kwargs["input"].
  • Dynamic Keyword Argument Access: Resolved an issue in vision.py where the literal string "key" was mistakenly used instead of the variable key when dynamically accessing keyword arguments.
  • RL Temperature Validation: Replaced non-existent MathError exceptions with ValueError in rl.py for temperature validation checks, ensuring proper error handling.

🧠 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.

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
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 addresses several correctness bugs across three files. The changes include replacing a non-existent MathError with ValueError, fixing undefined nanmin/nanmax function calls with equivalent PyTorch tensor operations, and correcting two dictionary key access errors in vision.py. The fixes are accurate and improve the robustness of the code. I've added one suggestion to use isinstance() for type checking, which is a more standard and robust practice in Python.

Comment thread unsloth/models/vision.py
else:
key = next(iter(kwargs.keys()))
if type(kwargs["key"]) is not torch.Tensor:
if type(kwargs[key]) is not torch.Tensor:
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

For type checking, it's generally better to use isinstance() instead of type(). isinstance() correctly handles inheritance, making the code more robust and aligned with Python best practices.

Suggested change
if type(kwargs[key]) is not torch.Tensor:
if not isinstance(kwargs[key], torch.Tensor):

1. rl_replacements.py (lines 864, 870): Fixed undefined `nanmin`/`nanmax`
   functions by using `.nan_to_num(nan=inf/-inf).min()/.max()` pattern.
   PyTorch doesn't have torch.nanmin/nanmax, so we replace NaN values
   before computing min/max.

2. vision.py (line 150): Fixed bug where code checked for "input" key
   but then accessed kwargs["input_ids"] instead of kwargs["input"].

3. vision.py (line 159): Fixed bug where literal string "key" was used
   instead of the variable `key` when accessing kwargs.

4. rl.py (lines 903, 905): Fixed non-existent `MathError` exception
   by replacing with `ValueError`.
@danielhanchen danielhanchen merged commit ab95425 into main Jan 1, 2026
4 checks passed
abiswas-realadvice pushed a commit to abiswas-realadvice/unsloth that referenced this pull request May 14, 2026
…lothai#3811)

* Fix correctness bugs in rl.py, rl_replacements.py, and vision.py

1. rl_replacements.py (lines 864, 870): Fixed undefined `nanmin`/`nanmax`
   functions by using `.nan_to_num(nan=inf/-inf).min()/.max()` pattern.
   PyTorch doesn't have torch.nanmin/nanmax, so we replace NaN values
   before computing min/max.

2. vision.py (line 150): Fixed bug where code checked for "input" key
   but then accessed kwargs["input_ids"] instead of kwargs["input"].

3. vision.py (line 159): Fixed bug where literal string "key" was used
   instead of the variable `key` when accessing kwargs.

4. rl.py (lines 903, 905): Fixed non-existent `MathError` exception
   by replacing with `ValueError`.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

1 participant