Skip to content

DPO trainer supports num_logits_to_keep to save memory#2129

Merged
kashif merged 55 commits intohuggingface:mainfrom
xyangk:keep_necessary_logits
Nov 10, 2024
Merged

DPO trainer supports num_logits_to_keep to save memory#2129
kashif merged 55 commits intohuggingface:mainfrom
xyangk:keep_necessary_logits

Conversation

@xyangk
Copy link
Copy Markdown
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
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Contributor Author

xyangk commented Sep 27, 2024

Sure!

@kashif kashif added the 🏋 DPO Related to DPO label Oct 6, 2024
@xyangk
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

yxliu-TAMU pushed a commit to mincheolseong/ECEN743-GRPO-Project-Proposal that referenced this pull request Apr 20, 2025
)

* Support num_logits_to_keep, which computes necessary logits in the forward pass.

* update doc

* bug fix

* update

* check is model supports num_logits_to_keep

* ruff format

* update test file

* peft model support

* test passed

* update

* apply use_num_logits_to_keep

* fix num_logits_to_keep compute bug

* compare all outputs

* pytest

* pass test

* use check_min_version

* format

* test_dpo_trainer_use_num_logits_to_keep passed

* add some comments

---------

Co-authored-by: Kashif Rasul <kashif.rasul@gmail.com>
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