-
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
Scale loss before backward #35207
Scale loss before backward #35207
Conversation
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one feels more like the right solution. Do the slow tests pass under this? (There is one in there for grad accum)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes because in reality this is exactly the right solution: since we are no longer relying on accelerate to div the loss, we need to do so before backward()
if we don't know our num items in the batch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM !
They pass locally. I get errors but I think they aren't related:
|
@ArthurZucker @muellerzr @SunMarc I'll let you merge when you think it's good. |
Merging! 🤗 |
@qgallouedec @SunMarc @ArthurZucker @muellerzr transformers/src/transformers/trainer.py Line 3703 in 24c91f0
The current implementation only divides the loss scalar when num_items_in_batch is None . However, I believe we should divide the loss scalar regardless of the num_items_in_batch value , as was the case in transformers<=4.45.2 .This change has led to an unexpected doubling of the logged loss when gradient_accumulated_steps > 1 .You can observe the differences by comparing versions: v4.45.2...v4.46.0. |
@qgallouedec Hello, not sure If I understand correctly, does this commit mean |
@qgallouedec Just wondering have you observed consistant hehaviors in when switching from transformers v4.45.2 to v4.46 |
I think you're wrong @yzhangcs: if I apply your modification: - if num_items_in_batch is None:
- loss = loss / self.args.gradient_accumulation_steps
+ loss = loss / self.args.gradient_accumulation_steps and run the following script with
import torch
from transformers import AutoModelForCausalLM, Trainer, TrainingArguments
from datasets import Dataset
num_batch = 32
gradient_accumulation_steps = 2 # or 1
per_device_train_batch_size = 3 # or 6
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_{batch_size}_grad_{grad_accum_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() You get the following results: Without the modification (current main)With the modifAfter the suggested modification, altering the gradient accumulation steps alter the results. While before the modification, everything overlap nicely. |
@qgallouedec Hi, thank you for sharing these easy-to-run scripts! FYI For v4.45.2, everything seems fine across different settings:
When I switched to v4.47.0, the first setting remained the same as in v4.45.2:
However, the results changed significantly for larger gradient accumulation steps:
After upgrading to the latest v4.47.1 and higher (including your new commit), the loss is the same across different grad accum steps
But is quite different compared to v4.45.2 |
I'm not entirely sure where the questions are arising, so I'm diving into the details of the Trainer. I'll keep you updated as soon as I have more information. |
Encountered a similar issue when running the above script.
|
Yes, probably related to the grad accum issue see #34198. As mentionned above, I think that everything works as expected with the dev version |
What does this PR do?
Fixes huggingface/trl#2456
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.