Skip to content

add support for custom function for reducing the batch size - #3071

Merged
SunMarc merged 3 commits into
huggingface:mainfrom
winglian:auto_batch_size_callback
Apr 8, 2025
Merged

add support for custom function for reducing the batch size#3071
SunMarc merged 3 commits into
huggingface:mainfrom
winglian:auto_batch_size_callback

Conversation

@winglian

@winglian winglian commented Sep 3, 2024

Copy link
Copy Markdown
Collaborator

What does this PR do?

when using auto_find_batch_size in transformers, there is no way to compensate with gradient_accumulation_steps or learning rate when a new batch size is tried. This adds the ability to use a callable function that transformers can set to manage updating the TrainingArgs.

Fixes # (issue)

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?

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

@winglian

winglian commented Sep 3, 2024

Copy link
Copy Markdown
Collaborator Author

blah, not quite correct, let me fix and get back to you

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

@muellerzr muellerzr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

Comment thread src/accelerate/utils/memory.py Outdated
Comment on lines 106 to 109
def find_executable_batch_size(function: callable = None, starting_batch_size: int = 128, reduce_batch_size_fn: callable = None):
"""
A basic decorator that will try to execute `function`. If it fails from exceptions related to out-of-memory or
CUDNN, the batch size is cut in half and passed to `function`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's make sure to add reduce_batch_size_fn to the docstring, describing what it should take in and return (a batch size and return a modified batch size?)

@muellerzr
muellerzr requested a review from SunMarc September 3, 2024 19:43

@SunMarc SunMarc left a comment

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 ! Thanks for adding !

@muellerzr

Copy link
Copy Markdown
Contributor

@winglian can you do a make style; make quality?

@github-actions

github-actions Bot commented Oct 7, 2024

Copy link
Copy Markdown
Contributor

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@muellerzr muellerzr added the wip Work in progress label Oct 10, 2024
@SunMarc

SunMarc commented Dec 24, 2024

Copy link
Copy Markdown
Member

gentle ping @winglian

@SunMarc

SunMarc commented Apr 8, 2025

Copy link
Copy Markdown
Member

@bot /style

@github-actions

github-actions Bot commented Apr 8, 2025

Copy link
Copy Markdown
Contributor

Style fixes have been applied. View the workflow run here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wip Work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants