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

Replace prefetch with val iterator check in megatron models #7318

Merged
merged 16 commits into from
Aug 29, 2023

Conversation

athitten
Copy link
Collaborator

@athitten athitten commented Aug 25, 2023

What does this PR do ?

  1. Catch the end of dataloader_iter to exit gracefully from validation_step without having to prefetch all microbatches in a step which was being done previously. This is a temporary piece of code that is needed until we have lightning's fix that takes care of catching the end of dataloader_iter
  2. Set limit_val_batches and num_sanity_val_steps for pretraining to be a multiple of num of microbatches. limit_val_batches is reconfigured so that we run as many global batches or validation_steps as the user entered limit_val_batches.

Collection: [Note which collection this PR will affect]

Changelog

  • Add specific line by line info of high level changes in this PR.

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this 

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation?
  • Does the PR affect components that are optional to install? (Ex: Numba, Pynini, Apex etc)
    • Reviewer: Does the PR have correct import guards for all optional libraries?

PR Type:

  • New Feature
  • Bugfix
  • Documentation

If you haven't finished some of the above items you can still open "Draft" PR.

Who can review?

Anyone in the community is free to review the PR once the checks have passed.
Contributor guidelines contains specific people who can review PRs to various areas.

Additional Information

  • Related to # (issue)

@github-actions github-actions bot added the NLP label Aug 25, 2023
@github-actions github-actions bot added the CI label Aug 25, 2023
@github-actions github-actions bot removed the CI label Aug 25, 2023
self.trainer.limit_val_batches *= get_num_microbatches()
# Override num sanity steps equal to num of microbatches and perform one val_step
self.trainer.num_sanity_val_steps = get_num_microbatches()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This method is called only in pretraining models. Not setting limit_val_batches to be a multiple of microbatches for downstream models as we are passing just 1 batch to val_step each time and limit is not required. Val step is run as many times as the user entered limit_val_batches in case of downstream tasks. Also leaving the num_sanity_val_step for downstream at the default val of 2.

mode = 'test' if self.trainer.testing else 'val'
batch = next(dataloader_iter)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was not needed for prompt learning as we are just passing 1 batch.

try:
element = next(iterator)
elements.append(element)
_ = next(iterator) # exhausting the iterator so that PTL knows to go to validation_epoch_end
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Lightning requires to hit a StopIteration to break out of the evaluation loop, otherwise it keeps running indefinitely. It hits a StopIteration at the (n+1)th step so we check for StopIteration after limit_val_batches is exhausted.

ericharper
ericharper previously approved these changes Aug 25, 2023
Copy link
Collaborator

@ericharper ericharper left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@ericharper ericharper self-requested a review August 25, 2023 21:07
@ericharper ericharper dismissed their stale review August 25, 2023 21:08

Other reviewer brought up some good points.

@athitten athitten marked this pull request as ready for review August 26, 2023 01:10
@github-actions github-actions bot added the CI label Aug 26, 2023
@athitten athitten force-pushed the athitten/limit_val_batches branch 3 times, most recently from ab62ff4 to c6cde73 Compare August 27, 2023 05:37
Jenkinsfile Outdated Show resolved Hide resolved
@athitten athitten force-pushed the athitten/limit_val_batches branch 2 times, most recently from a1530f8 to 7f04651 Compare August 28, 2023 20:50
1) Remove if condition, self._val_micro_batches_consumed in def _val_iterator_done and check with just try(and reinsert), except
2) Use _val_iterator_done in all megatron models that use dataloader_iter to maintain uniformity

Signed-off-by: Abhishree <[email protected]>
aklife97
aklife97 previously approved these changes Aug 28, 2023
Copy link
Collaborator

@aklife97 aklife97 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!!

@athitten before finally merging this in, it would be great to re-ensure once again that we:

  1. call _val_iterator_done in all models
  2. do _reconfigure_val_batches only in the pre-training models

any missing/misconfigured models above can make it super difficult for anyone using the model to figure out what might be wrong

Overall, looks amazing!

Copy link
Collaborator

@ericharper ericharper left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@ericharper ericharper merged commit 0e3b935 into main Aug 29, 2023
15 checks passed
@ericharper ericharper deleted the athitten/limit_val_batches branch August 29, 2023 18:24
rohitrango pushed a commit to rohitrango/NeMo that referenced this pull request Jun 25, 2024
)

* Add counter for num_microbatches

Signed-off-by: Abhishree <[email protected]>

* Reset self.total_val_micro_batches

Signed-off-by: Abhishree <[email protected]>

* Replace _prefetch() with _val_iterator_done()

Signed-off-by: Abhishree <[email protected]>

* Override limit_val_batches for pretraining models

Signed-off-by: Abhishree <[email protected]>

* Return iterator in _val_iterator_done when iterator is not exhuasted

Signed-off-by: Abhishree <[email protected]>

* Temporarily comment BioMegatron Bert CI test

Signed-off-by: Abhishree <[email protected]>

* Move _reconfigure_val_batches() to MegatronGPTModel

Signed-off-by: Abhishree <[email protected]>

* Move self_reconfigure_val_batches to build_train_valid_test_datasets

Signed-off-by: Abhishree <[email protected]>

* Avoid fetching and reinserting back to the iterator

Signed-off-by: Abhishree <[email protected]>

* Increase limit_val_batches in CI tests

Signed-off-by: Abhishree <[email protected]>

* Use _val_iterator_done to check for iterator end in all megatron models

1) Remove if condition, self._val_micro_batches_consumed in def _val_iterator_done and check with just try(and reinsert), except
2) Use _val_iterator_done in all megatron models that use dataloader_iter to maintain uniformity

Signed-off-by: Abhishree <[email protected]>

* Minor edit to return outside of try block

Signed-off-by: Abhishree <[email protected]>

* Add _val_iterator_done for megatron_nmt_model

Signed-off-by: Abhishree <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Abhishree <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eric Harper <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants