Repository navigation
Fix missing num_items_in_batch in unsloth_prediction_step - #4616
Conversation
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.
Summary of ChangesHello, 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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
…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>
Summary
unsloth_prediction_stepinrl.pycallscompute_losswithoutnum_items_in_batchduring evaluationgradient_accumulation_steps > 1:prediction_stepcomputesnum_items_in_batchvia_get_num_items_in_batchbefore passing it tocompute_loss. This patch does the same inunsloth_prediction_stepTest plan
gradient_accumulation_steps=3,eval_steps=3-- warning gone, eval loss correctgradient_accumulation_steps=3,eval_steps=3-- warning gone, eval loss correct