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

[Flax] Fix incomplete batches in example scripts #17863

Merged
merged 14 commits into from
Jul 27, 2022

Conversation

sanchit-gandhi
Copy link
Contributor

@sanchit-gandhi sanchit-gandhi commented Jun 24, 2022

What does this PR do?

Currently in our Flax examples scripts, we drop the last incomplete batch during training and inference:

batch_idx = batch_idx[: steps_per_epoch * batch_size] # Skip incomplete batch.

We do this for two reasons:

  1. Because XLA is not shape polymorphic, forming the last batch with shape different from the preceding batches triggers a recompilation of the pmap'd function .
  2. If the batch size is not divisible by the number devices, then the last step must be executed on a single device (or a subset of devices), potentially leading to OOMs.

During training, dropping the last batch isn't an issue: since we shuffle the data and train for multiple epochs, all of the training data is eventually used and the effects of dropping the last batch amortised.

However, during evaluation and prediction, dropping the last batch leads to incorrect results: since we don't account for the examples in the last batch, we do not evaluate over the whole dataset, and thus have partial results.

This PR corrects for the incomplete batches in the relevant Flax training examples.

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?

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

HuggingFaceDocBuilderDev commented Jun 24, 2022

The documentation is not available anymore as the PR was closed or merged.

Copy link
Contributor

@patil-suraj patil-suraj left a comment

Choose a reason for hiding this comment

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

Thanks, this look good to me! The pad_shard_unpad trick is neat!

@@ -847,7 +846,7 @@ def generate_step(params, batch):
# train
for _ in tqdm(range(steps_per_epoch), desc="Training...", position=1, leave=False):
batch = next(train_loader)
state, train_metric = p_train_step(state, batch)
state, train_metric = pad_shard_unpad(p_train_step)(state, batch)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really nit!


for idx in batch_idx:
batch = dataset[idx]
batch = {k: jnp.array(v) for k, v in batch.items()}

batch = shard(batch)
Copy link
Contributor

Choose a reason for hiding this comment

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

does pad_shard_unpad also takes care of sharding the batch ? (seems like it, but just to confirm)

Copy link
Contributor Author

@sanchit-gandhi sanchit-gandhi Jun 24, 2022

Choose a reason for hiding this comment

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

Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

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

Thanks for fixing @sanchit-gandhi !

@sanchit-gandhi
Copy link
Contributor Author

Just waiting to double check that the slow tests pass from test_flax_examples.py before merging. Working with @patil-suraj to verify this ✅

@github-actions
Copy link

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.

@patrickvonplaten
Copy link
Contributor

@patil-suraj @sanchit-gandhi can we merge this one?

@sanchit-gandhi
Copy link
Contributor Author

sanchit-gandhi commented Jul 27, 2022

Just verifying the slow tests from test_flax_examples.py pass on a v3-8 v100 GPU!

@sanchit-gandhi sanchit-gandhi merged commit 7490a97 into huggingface:main Jul 27, 2022
@sanchit-gandhi sanchit-gandhi deleted the flax-eval branch July 27, 2022 14:50
oneraghavan pushed a commit to oneraghavan/transformers that referenced this pull request Sep 26, 2022
* [Flax] Fix incomplete batches in example scripts

* fix dataloader batching

* convert jnp batch idxs to np array

* add missing `pad_shard_unpad` to final prediction generate step

* only `pad_shard_unpad` at inference time

* merge conflicts

* remove incomplete batch step from eval

* fix run_qa.py

* add `pad_shard_unpad` to run_flax_ner.py

* add `pad_shard_unpad` to run_flax_glue.py

* add `pad_shard_unpad` to run_image_classification.py

* make style

* fix mlm flax eval batches

* remove redundant imports
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.

4 participants