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

Multiple fixes in SageMakerTrainer #10687

Merged
merged 9 commits into from Mar 15, 2021
Merged

Multiple fixes in SageMakerTrainer #10687

merged 9 commits into from Mar 15, 2021

Conversation

sgugger
Copy link
Collaborator

@sgugger sgugger commented Mar 12, 2021

What does this PR do?

This PR adds quite a few fixes to the SageMakerTrainer to make sure example scripts run fully. In particular it fixes:

  • save made the training hanging forever
  • predict didn't work
  • evaluation required using drop_last=True which is not something anyone wants.

The goal is now to test a little bit more that functionality before merging the SageMakerTrainer into the main Trainer (otherwise one can't use model parallelism in seq2seq examples or QA example). The plan is to have them merged in the v4.5.0.

@sgugger sgugger changed the title Save model smtrainer Multiple fixes in SageMakerTrainer Mar 12, 2021
Comment on lines +149 to +150
if self.is_model_parallel_enabled:
self._save_smp(output_dir)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need to use a special save because model parallelism requires to:

  • gather the state dict on all processes of d_rank 0 (it triggers a sync across those processes)
  • save it only on processes 0

Comment on lines +199 to +200
# Consolidate the state dict on all processed of dp_rank 0
opt_state_dict = self.optimizer.state_dict()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The method is overloaded for this particular line/behavior. As for the model, the state dict of the optimizer needs to be gathered from all dp rank 0 processes.

os.path.join(checkpoint, "scheduler.pt")
):
self.optimizer.load_state_dict(
torch.load(os.path.join(checkpoint, "optimizer.pt"), map_location="cpu")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The method is overloaded for this particular line (needs to be loaded on the CPU and not the device).

@@ -927,6 +924,9 @@ def train(
if delay_optimizer_creation:
self.create_optimizer_and_scheduler(num_training_steps=max_steps)

# Check if saved optimizer or scheduler states exist
self._load_optimizer_and_scheduler(resume_from_checkpoint)
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 move should be harmless.

elif self.args.local_rank != -1:
world_size = dist.get_world_size()
world_size = max(1, world_size)
world_size = max(1, self.args.world_size)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Refactor + allows world_size to be overloaded in SageMakerTrainingArguments

Copy link
Member

@philschmid philschmid left a comment

Choose a reason for hiding this comment

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

LGTM. Tested on run_glue.py on mnli and mrpc.

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

LGTM

@sgugger sgugger merged commit 6bef764 into master Mar 15, 2021
@sgugger sgugger deleted the save_model_smtrainer branch March 15, 2021 13:28
Iwontbecreative pushed a commit to Iwontbecreative/transformers that referenced this pull request Jul 15, 2021
* Handle save differently

* Missing imports

* Fix typo

* Adapt to recent changes in save_pretrained

* Forgotten brackets

* Optimizer load

* Fix world size

* Deal wth None

* Remove needless self
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.

None yet

4 participants