Skip to content

Fix missing num_items_in_batch in unsloth_prediction_step - #4616

Merged
danielhanchen merged 3 commits into
mainfrom
fix-prediction-step-num-items-in-batch
Mar 27, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-prediction-step-num-items-in-batch

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

  • unsloth_prediction_step in rl.py calls compute_loss without num_items_in_batch during evaluation
  • This causes a spurious warning for all models when gradient_accumulation_steps > 1:
    Unsloth: Not an error, but {model} does not accept num_items_in_batch.
    Using gradient accumulation will be very slightly less accurate.
    
  • The standard transformers prediction_step computes num_items_in_batch via _get_num_items_in_batch before passing it to compute_loss. This patch does the same in unsloth_prediction_step
  • Without this fix, eval loss computation during gradient accumulation is not properly scaled by token count

Test plan

  • Llama-3.2-1B-Instruct with gradient_accumulation_steps=3, eval_steps=3 -- warning gone, eval loss correct
  • Olmo-3-7B-Instruct with gradient_accumulation_steps=3, eval_steps=3 -- warning gone, eval loss correct

unsloth_prediction_step calls compute_loss without num_items_in_batch
during evaluation. This causes _unsloth_pre_compute_loss to see
num_items_in_batch=None, which triggers a spurious warning for every
model when gradient_accumulation_steps > 1:

  "Unsloth: Not an error, but {model} does not accept num_items_in_batch.
   Using gradient accumulation will be very slightly less accurate."

The standard transformers prediction_step computes num_items_in_batch
via _get_num_items_in_batch before passing it to compute_loss. This
patch does the same in unsloth_prediction_step.

Tested on Llama-3.2-1B-Instruct and Olmo-3-7B-Instruct with
gradient_accumulation_steps=3 and eval_steps=3. Warning is gone and
eval loss is computed correctly for both.
@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 issue where the evaluation loss calculation in unsloth_prediction_step was not correctly accounting for batch sizes during gradient accumulation. By explicitly calculating and passing the number of items in a batch to the loss computation, the change eliminates a misleading warning and improves the accuracy of evaluation metrics, particularly when using gradient accumulation.

Highlights

  • Evaluation Loss Calculation Fix: The unsloth_prediction_step function in rl.py was updated to correctly pass num_items_in_batch to compute_loss during evaluation. Previously, this parameter was missing.
  • Warning Resolution: This change resolves a spurious warning related to gradient_accumulation_steps > 1 and num_items_in_batch not being accepted by the model, which indicated slightly less accurate gradient accumulation.
  • Accuracy Improvement: By including num_items_in_batch, the evaluation loss computation during gradient accumulation is now properly scaled by token count, ensuring greater accuracy.
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. ↩

@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 modifies the unsloth_prediction_step function in unsloth/models/rl.py to calculate num_items_in_batch and pass it as a new argument to the self.compute_loss method. This change likely supports an updated signature for compute_loss that requires batch size information. There are no review comments to provide feedback on.

_get_num_items_in_batch was added in transformers 4.46. Wrap the call
in try/except so older versions fall back to num_items_in_batch=None,
which preserves the original behavior of not passing it.
@danielhanchen
danielhanchen merged commit 6b5da2e into main Mar 27, 2026
4 checks passed
@danielhanchen
danielhanchen deleted the fix-prediction-step-num-items-in-batch branch March 27, 2026 10:07
shibizhao pushed a commit to shibizhao/unsloth-npu that referenced this pull request Apr 7, 2026
…4616)

* Fix missing num_items_in_batch in unsloth_prediction_step

unsloth_prediction_step calls compute_loss without num_items_in_batch
during evaluation. This causes _unsloth_pre_compute_loss to see
num_items_in_batch=None, which triggers a spurious warning for every
model when gradient_accumulation_steps > 1:

  "Unsloth: Not an error, but {model} does not accept num_items_in_batch.
   Using gradient accumulation will be very slightly less accurate."

The standard transformers prediction_step computes num_items_in_batch
via _get_num_items_in_batch before passing it to compute_loss. This
patch does the same in unsloth_prediction_step.

Tested on Llama-3.2-1B-Instruct and Olmo-3-7B-Instruct with
gradient_accumulation_steps=3 and eval_steps=3. Warning is gone and
eval loss is computed correctly for both.

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

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

* Guard _get_num_items_in_batch for older transformers versions

_get_num_items_in_batch was added in transformers 4.46. Wrap the call
in try/except so older versions fall back to num_items_in_batch=None,
which preserves the original behavior of not passing it.

---------

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