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

DPO trainer supports num_logits_to_keep to save memory #2129

Merged
merged 55 commits into from
Nov 10, 2024

Conversation

xyangk
Copy link
Contributor

@xyangk xyangk commented Sep 26, 2024

What does this PR do?

Since transformers v4.45.0 supports num_logits_to_keep in the forward pass(check this PR), it can be useful for saving memory and speeding up training by computing logits only for the last completion tokens, especially in scenarios with very long prompts where labels are ignored (-100).

I implemented the logic to calculate num_logits_to_keep based on the labels and tested this on DPO, resulting in substantial memory savings.

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? 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.
  • 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.

@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.

@kashif
Copy link
Collaborator

kashif commented Sep 27, 2024

@xyangk thank you! can you run the pre-commit task via the makefile to fix the formatting?

@xyangk
Copy link
Contributor Author

xyangk commented Sep 27, 2024

Sure!

@kashif kashif added the 🏋 DPO Related to DPO label Oct 6, 2024
@xyangk
Copy link
Contributor Author

xyangk commented Oct 28, 2024

Implemented the calculation of num_logits_to_keep on the new master code, fixed a bug in the previous calculation, and used more rigorous test code.

@xyangk
Copy link
Contributor Author

xyangk commented Oct 31, 2024

Hi @kashif , I wanted to check in on this PR. Please let me know if there's anything further I can clarify or update. Thank you for your time and review!

@kashif
Copy link
Collaborator

kashif commented Nov 6, 2024

@xyangk can you use the transformers helper check_min_version instead of implementing the logic yourself?

@xyangk
Copy link
Contributor Author

xyangk commented Nov 6, 2024

@xyangk can you use the transformers helper check_min_version instead of implementing the logic yourself?

I've implemented the suggested changes. Thank you for the feedback!

@xyangk xyangk closed this Nov 6, 2024
@xyangk xyangk reopened this Nov 6, 2024
@kashif
Copy link
Collaborator

kashif commented Nov 7, 2024

@xyangk in view of the fix #2332 can you kindly confirm that this feature is not affected by this?

@xyangk
Copy link
Contributor Author

xyangk commented Nov 8, 2024

in view of the fix #2332 can you kindly confirm that this feature is not affected by this?

I've reviewed the code in realtion to fix #2332. I can confirm that this feature remains unaffected by those changes. I've also added some comments to improve clarity.

@kashif kashif merged commit 0238d96 into huggingface:main Nov 10, 2024

# num_logits_to_keep is supported since transformers v4.45.0
if self.use_num_logits_to_keep:
check_min_version("4.45.0")
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I missed that the transformers version was already updated. Since the current PR is already merged, I'll create a new PR to remove the version check. Thank you for cathing this!

Copy link
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏋 DPO Related to DPO
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants