-
Notifications
You must be signed in to change notification settings - Fork 27.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass correct num_items_in_batch
value into the training_step function
#35438
base: main
Are you sure you want to change the base?
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 :-( |
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