Skip to content
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

Enable Gradient Accumulation fix across all models + trainer fully in forward() #34283

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

muellerzr
Copy link
Contributor

@muellerzr muellerzr commented Oct 21, 2024

What does this PR do?

Since most users still want OOTB, this trickles the loss kwargs to the rest of the models so that causal loss can be calculated properly

Also relies on huggingface/accelerate#3185 for DDP bits

Fixes # (issue)

Fully fixes #34263 / finishes #34191 & #34198 & fixes #34242

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@ArthurZucker

@muellerzr muellerzr added Core: Modeling Internals of the library; Models. trainer labels Oct 21, 2024
Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I don't see Llama being modified, that's probably because it now has FlashAttentionKwargs type dict as kwargs. We can create ExtraKwargs, a nested dict with both flash kwargs and loss kwargs and default loss kwargs can be type dict?
🤗

@muellerzr
Copy link
Contributor Author

@ArthurZucker LlamaForCausalLM isn't there bc you did it in your PR 😉

@@ -3610,8 +3612,11 @@ def compute_loss(self, model, inputs, return_outputs=False, num_items_in_batch=N
labels = inputs.pop("labels")
else:
labels = None
# if num_items_in_batch is not None:
# inputs["num_items_in_batch"] = num_items_in_batch
if self.model_accepts_loss_kwargs:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if condition doesn't seem to work for PeftModel class (it only has kwargs not loss_kwargs 🫠 )

I tried just changing that condition to if True and ran some tests, and the loss calculation worked perfectly for a LORA on a Llama 3 1B.

I'm wondering if there's a safe/non-breaking way to support peft models here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the flag, that may be why I couldn't recreate #34263 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@man-shar can you try giving peft another go? Should have fixed it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm self.model_accepts_loss_kwargs is the correct value now for a peft model!

But seems like the compute_loss function inside trainer.py isn't getting the num_items_in_batch argument passed to it from training_step.

I notice that argument was removed in 4f3f86d and the commit still says "Experimental". So I assume it will be reverted once you're done experimenting! It should work after that!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed :)

@man-shar
Copy link

Awesome work by all of you on this. Insane dev speed over the past few days 🙏 🔥

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only reviewed the PEFT-related code in trainer.py and it LGTM. Thanks Zach.

@HuggingFaceDocBuilderDev

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
Copy link
Collaborator

Rebasing should probably fix quality!

Comment on lines -2458 to +2468
tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
tr_loss_step = self.training_step(model, inputs)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why num_items_in_batch is removed here?

@@ -1114,6 +1114,7 @@ def forward(
return_dict: Optional[bool] = None,
cache_position: Optional[torch.LongTensor] = None,
num_logits_to_keep: int = 0,
**loss_kwargs,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think making it an argument loss_args rather than kwargs would be better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core: Modeling Internals of the library; Models. trainer
Projects
None yet
6 participants