Pass correct num_items_in_batch value into the training_step function - #35438
Conversation
num_items_in_batch value into the training_step methodnum_items_in_batch value into the training_step function
|
The test cases have almost passed, except for a few that encountered a timeout error. pipelines_tf - Failed |
|
yes, observe similar phenomenon #35207 (comment) |
|
@hiyouga Hi, could you try this out import torch
from datasets import Dataset
from transformers import AutoModelForCausalLM, Trainer, TrainingArguments
num_batch = 32
gradient_accumulation_steps = 2
per_device_train_batch_size = 3
seq_len = 5
eff_batch_size = per_device_train_batch_size * gradient_accumulation_steps
dataset_len = num_batch * eff_batch_size
data = torch.arange(0, dataset_len * seq_len)
data = data.reshape(dataset_len, seq_len)
data = data.tolist()
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-1.5B").to("cuda")
dataset = Dataset.from_dict({"input_ids": data, "labels": data})
args = TrainingArguments(
output_dir=f"out_bs_{per_device_train_batch_size}_grad_{gradient_accumulation_steps}_before",
per_device_train_batch_size=per_device_train_batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
logging_steps=2,
)
trainer = Trainer(model=model, args=args, train_dataset=dataset)
trainer.train()Looks like the loss is still incorrect for |
|
cc @ArthurZucker and @muellerzr |
|
@yzhangcs Could you provide detailed experimental results and patch fix (if possible) thanks |
|
@hiyouga Hello, the results is similar to v4.47.0, where the loss for
However, the results changed significantly for larger gradient accumulation steps:
still not find a solution though :-( |
|
Hi @muellerzr, what do you think? |
|
@hiyouga sorry I am a bit lost, do you still see the issues on main? |
|
@ArthurZucker Hi Arthur, thank you for looking into this issue, I think it still exists, as #35102 did not fix it. The current state is that the loss is computed correctly for models accept |
muellerzr
left a comment
There was a problem hiding this comment.
Thanks, it makes sense why we need to keep it there in the end, I appreciate the thorough investigation into it you've done
SunMarc
left a comment
There was a problem hiding this comment.
Nice investigation ! Would be nice to include in the release @ArthurZucker
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
ArthurZucker
left a comment
There was a problem hiding this comment.
Cool thanks for digging 🤗
…loom-560m
Issue is:
```
[rank0]: raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
[rank0]: ValueError: Got unexpected arguments: {'num_items_in_batch': 1024}
```
Fixed in upstream in huggingface#35438
…loom-560m
Issue is:
```
[rank0]: raise ValueError(f"Got unexpected arguments: {deprecated_arguments}")
[rank0]: ValueError: Got unexpected arguments: {'num_items_in_batch': 1024}
```
Fixed in upstream in huggingface#35438

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_kwargswhen gradient accumulation was enabled.In #35121 , the author always passes
num_items_in_batchinto 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_batchparameter 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_kwargshasn'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