Skip to content

⛰️ Reduce peak vram consumption with efficient selective log_softmax#2799

Merged
qgallouedec merged 12 commits into
huggingface:mainfrom
tyler-romero:tyler/extract-logprobs-efficient
Feb 7, 2025
Merged

⛰️ Reduce peak vram consumption with efficient selective log_softmax#2799
qgallouedec merged 12 commits into
huggingface:mainfrom
tyler-romero:tyler/extract-logprobs-efficient

Conversation

@tyler-romero
Copy link
Copy Markdown
Contributor

@tyler-romero tyler-romero commented Feb 7, 2025

What does this PR do?

Many TRL Trainers use the same log_softmax -> gather operation to compute a selected set of logprobs. This approach is inefficient b/c it allocates a bs*seqlen*vocab_size tensor to hold the logprobs. For modest bs/seqlen/vocab_size this tensor can require >2GB vram. There are a variety of more memory efficient (and faster) approaches.

This PR creates a utility function to hold a more efficient implementation of this operation and uses that utility function broadly across TRL.

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.

@tyler-romero
Copy link
Copy Markdown
Contributor Author

tyler-romero commented Feb 7, 2025

See benchmarks here: #2773 (comment) (thanks @qgallouedec )

Notably, the most efficient approach in these benchmarks is not stable with bfloat16, and so we fall back to the approach that loops over log_softmax for bfloat16 and float16.

@tyler-romero tyler-romero marked this pull request as ready for review February 7, 2025 19:29
@tyler-romero
Copy link
Copy Markdown
Contributor Author

@qgallouedec


import requests
import torch
import wandb
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.

changed by running precommit

Comment thread tests/test_core.py Outdated
Comment thread tests/test_core.py Outdated
@qgallouedec
Copy link
Copy Markdown
Member

That's a super cool improvement! Thanks!
Just some minor rewarks to adresse and we're good to merge

Comment thread trl/core.py Outdated
Comment thread trl/core.py Outdated
logsumexp_values = torch.stack([torch.logsumexp(lg, dim=-1) for lg in logits])
per_token_logps = selected_logits - logsumexp_values # log_softmax(x_i) = x_i - logsumexp(x)
else:
# logsumexp approach is unstable with bfloat16, fall back to slightly less efficent approach
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.

nice finding!

@tyler-romero
Copy link
Copy Markdown
Contributor Author

Ready for re-review!

@qgallouedec qgallouedec changed the title Reduce peak vram consumption with efficient selective log_softmax ⛰️ Reduce peak vram consumption with efficient selective log_softmax Feb 7, 2025
@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.

@qgallouedec
Copy link
Copy Markdown
Member

Thanks again!

@qgallouedec qgallouedec merged commit 09eefa7 into huggingface:main Feb 7, 2025
yxliu-TAMU pushed a commit to mincheolseong/ECEN743-GRPO-Project-Proposal that referenced this pull request Apr 20, 2025
…uggingface#2799)

* Reduce mem consumption across many trainers with efficient selective log-softmax approach

* rename

* typo fix

* precommit

* Update tests/test_core.py

* relocate

* precommit

* style

* smaller values for test, and run on cpu

* nit doc improvements

* style

* fix test

---------

Co-authored-by: Quentin Gallouédec <quentin.gallouedec@huggingface.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants