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

Add BatchSizeFinder callback #11089

Merged
merged 54 commits into from
Sep 27, 2022
Merged

Add BatchSizeFinder callback #11089

merged 54 commits into from
Sep 27, 2022

Conversation

rohitgr7
Copy link
Contributor

@rohitgr7 rohitgr7 commented Dec 15, 2021

What does this PR do?

Part of #11012

TODO:

  • Add docs in followups
  • Address added TODOs here in the followup

Does your PR introduce any breaking changes? If yes, please list them.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

cc @Borda @awaelchli @ananthsub @daniellepintz @rohitgr7 @akihironitta

@rohitgr7 rohitgr7 force-pushed the ref/bs_finder_tuner branch from 5ec2c83 to 3de3cec Compare December 15, 2021 18:31
@Borda Borda self-requested a review December 15, 2021 19:03
@Borda Borda added callback feature Is an improvement or enhancement labels Dec 15, 2021
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
@rohitgr7 rohitgr7 force-pushed the ref/bs_finder_tuner branch 2 times, most recently from 88a00d0 to 9c6bcc1 Compare January 4, 2022 14:40
@rohitgr7 rohitgr7 added the tuner label Jan 4, 2022
@rohitgr7 rohitgr7 added this to the 1.6 milestone Jan 4, 2022
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/loops/base.py Outdated Show resolved Hide resolved
pytorch_lightning/loops/base.py Outdated Show resolved Hide resolved
pytorch_lightning/trainer/connectors/callback_connector.py Outdated Show resolved Hide resolved
pytorch_lightning/tuner/batch_size_scaling.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
@tchaton
Copy link
Contributor

tchaton commented Jan 5, 2022

After chatting with @rohitgr7,

Here is the API design specification for the BatchSizeFinder.

With the tune method.

User story: As a user, I want to fit the best batch size for each stage and get the associated maximal batch size.

trainer = Trainer()

trainer.tune(auto_batch_size="fit | True (BC)", ...) # Compute the train_batch_size and val_batch_size
   # In the LightningModule, DataModule
    self.batch_size = train_batch_size # BC -> depreceated in favor of `{stage}_batch_size`.
    self.train_batch_size = train_max_batch_size
    self.val_batch_size = val_max_batch_size

trainer.tune(auto_batch_size='validate', ...) -> val dataloader batch_size
    # In the LightningModule, DataModule
    self.val_batch_size = max_batch_size

trainer.tune(auto_batch_size='test', ...) -> test dataloader batch_size
    # In the LightningModule, DataModule
    self.test_batch_size = max_batch_size

trainer.tune(auto_batch_size='predict', ...) -> predict dataloader batch_size
    self.predict_batch_size = max_batch_size

With the Trainer entry point method.

User story: As a user, I want to fit the best batch size and run the trainer entry point into a single call.

trainer = Trainer(callbacks=BatchSizeFinder())
trainer.fit(...) -> `on_fit_start` perform tune
trainer.validate(...) -> `on_validation_start` perform tune
trainer.test(...) -> `on_test_start` perform tune
trainer.predict(...) -> `on_predict_start` perform tune

Open questions:

  1. Some use case requires dynamic maximum batch size such as fine-tuning where the memory footprint of the model increases during trainer.

Solution:

  1. Enable a scheduling strategy. Would this be enough to cover use cases?
  2. Re-compute the max_batch_size if the params group of the optimizer changed?

...

@carmocca @ananthsub @awaelchli @justusschock thoughts ?

@rohitgr7 rohitgr7 force-pushed the ref/bs_finder_tuner branch from f5e9a5a to 173ffd3 Compare January 5, 2022 16:27
@rohitgr7
Copy link
Contributor Author

rohitgr7 commented Jan 6, 2022

update on PR:

all that's been discussed here is now added.

Only the following proposal is missing and I need more time to think about how to go about this. Maybe in a sep PR to avoid a lot of changes in a single one.

trainer.tune(auto_batch_size="fit | True (BC)", ...) # Compute the train_batch_size and val_batch_size
   # In the LightningModule, DataModule
    self.batch_size = train_batch_size # BC -> depreceated in favor of `{stage}_batch_size`.
    self.train_batch_size = train_max_batch_size
    self.val_batch_size = val_max_batch_size

Also the API is:

trainer.tune(method="fit|validate|test|predict") (default fit)

instead of

trainer.tune(auto_batch_size="fit|validate|test|predict")

because, there is another option to run batch_size_finder using trainer.tuner.scale_batch_size() and auto_batch_size argument won't be a good one here. Also auto_batch_size is a Trainer argument already.

Copy link
Contributor

@ananthsub ananthsub left a comment

Choose a reason for hiding this comment

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

IMO a callback is not the right fit here. this will override the behavior of fit/validate/test if used this way. why doesn't this use the loops API instead?

@rohitgr7
Copy link
Contributor Author

rohitgr7 commented Jan 6, 2022

IMO a callback is not the right fit here. this will override the behavior of fit/validate/test if used this way. why doesn't this use the loops API instead?

I tried writing pseudocode to cover up all the possible use-cases we have and convert it into a component that users can easily integrate and plugin, but loops couldn't fit all possible cases. For eg, we have flash finetuning and let's say the user might want to compute new LR or new batch_size after certain epochs of pre-training, then it's not easily configurable within a single call. One can achieve it with multiple calls but since we support strategies within Flash. With callbacks this is now pretty simple since we make sure after each tuning call, the respective loop state_dict is restored completely, and we still get an updated hparam using the tuner. Plus it now runs for val/test/predict.

If you have any ideas to configure it using loops, would love to discuss them.

Also, we discussed the possible solutions for this internally and everyone I talked to recommended callbacks.
#9103
#11012 (comment)

pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/callbacks/batch_size_finder.py Outdated Show resolved Hide resolved
pytorch_lightning/loops/base.py Outdated Show resolved Hide resolved
tests/tuner/test_scale_batch_size.py Outdated Show resolved Hide resolved
@rohitgr7 rohitgr7 force-pushed the ref/bs_finder_tuner branch from 60a7a5b to 8c3e0de Compare January 16, 2022 19:30
@rohitgr7 rohitgr7 requested a review from akihironitta January 16, 2022 19:30
@mergify mergify bot removed the has conflicts label Jan 16, 2022
@mergify mergify bot added ready PRs ready to be merged and removed has conflicts ready PRs ready to be merged labels Sep 16, 2022
@rohitgr7 rohitgr7 requested a review from carmocca September 16, 2022 19:33
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

Synced outside of GitHub! LGTM! Great and challenging work

We can try to remove the exception in the future if we finish #8604

src/pytorch_lightning/tuner/batch_size_scaling.py Outdated Show resolved Hide resolved
@mergify mergify bot added has conflicts ready PRs ready to be merged and removed ready PRs ready to be merged has conflicts labels Sep 23, 2022
@mergify mergify bot added ready PRs ready to be merged and removed has conflicts ready PRs ready to be merged labels Sep 24, 2022
@rohitgr7 rohitgr7 enabled auto-merge (squash) September 24, 2022 14:21
@lexierule lexierule disabled auto-merge September 27, 2022 12:54
@lexierule lexierule merged commit d1a3a3e into master Sep 27, 2022
@lexierule lexierule deleted the ref/bs_finder_tuner branch September 27, 2022 12:54
rohitgr7 added a commit that referenced this pull request Sep 28, 2022
rohitgr7 added a commit that referenced this pull request Sep 28, 2022
lexierule pushed a commit that referenced this pull request Sep 28, 2022
* Revert "Add BatchSizeFinder callback (#11089)"

This reverts commit d1a3a3e.

* Revert "Revert "Add BatchSizeFinder callback (#11089)""

This reverts commit 9cc4695.

* remove pl

* add torch

* add numpy

* rm packages

* add packages

* add packages

* import from PL

* import from PL

* always install PL for doctests

* remove unnecessary requirements

* always install PL in editable mode

* once more

* another attempt

* maybe fix app test?

* Redundant checkgroup path

* Revert "maybe fix app test?"

This reverts commit 8210a43.

* speed up install deps

* damn this

* damn trio

Co-authored-by: otaj <[email protected]>
Co-authored-by: otaj <[email protected]>
Co-authored-by: Carlos Mocholí <[email protected]>
@carmocca carmocca mentioned this pull request Sep 28, 2022
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
callback feature Is an improvement or enhancement pl Generic label for PyTorch Lightning package ready PRs ready to be merged tuner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Tuner Revamp