Pass correct num_items_in_batch
value into the training_step function
#35438
+8
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR follows #34511 #34915 and partially reverts #35121 . We want to handle the loss calculation correctly for models that don't accept
loss_kwargs
when gradient accumulation was enabled.In #35121 , the author always passes
num_items_in_batch
into the training_step function, which makes the logic of the following lines invalid, and consequently leads to incorrect loss calculation.transformers/src/transformers/trainer.py
Lines 3701 to 3707 in 3b0a94e
Currently, there are still many models that don't accept
loss_kwargs
(such as Qwen2-VL). For these models, we need to set thenum_items_in_batch
parameter in training_step function to None, so as to scale the loss correctly in training.However, we've found that since transformers 4.46.0, the loss of the models that don't accept
loss_kwargs
hasn't been calculated correctly!Specifically, in transformers 4.46.0-4.46.1, the loss was wrongly multiplied by the gradient accumulation steps, resulting in a large loss value (see https://twitter.com/TheZachMueller/status/1851677628656423347 ).
https://github.com/huggingface/transformers/blob/v4.46.0/src/transformers/trainer.py#L3605
transformers/src/transformers/trainer.py
Lines 3605 to 3608 in c2820c9
In transformers 4.46.2-4.47.1, the loss was scaled after the backward pass, which also led to larger gradients (also mentioned in #35207 ).
https://github.com/huggingface/transformers/blob/v4.47.1/src/transformers/trainer.py#L3689
transformers/src/transformers/trainer.py
Lines 3687 to 3691 in 241c04d
In the latest version, due to the issues in this PR, the loss isn't scaled correctly either, and the final loss remains large. We hope the maintainers can attach importance to this problem and look forward to a timely fix.
We also conducted a group of experiments to verify the above conclusion. We trained the Qwen2-VL model (which does not accept
loss_kwargs
) using transformers 4.45.2, the main branch (5c75087), and the main branch after the PR fix. We can find that both version 4.45.2 and the fixed version have reasonable loss values and grad norms, while the main branch does not.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@ArthurZucker @muellerzr