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

Connect the model to the training type plugin at the start of run #8536

Merged
merged 21 commits into from
Aug 4, 2021

Conversation

carmocca
Copy link
Contributor

@carmocca carmocca commented Jul 23, 2021

What does this PR do?

With this PR, the trainer.lightning_module reference will be set at the very beginning of _run. Concretely, in the self.accelerator.connect(model) call.

We want this because we need it available during all hooks, even the earliest ones like setup.

Also, If the trainer had been reused and the model has changed, the trainer.lightning_module reference would be stale.

Part of #8498

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

(BETA TrainingTypePlugin API):
The setup hook no longer takes a model argument.

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?
  • [n/a] Did you make sure to update the documentation with your changes? (if necessary)
  • [n/a] 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 update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)
  • Did you list all the breaking changes introduced by this pull request?

PR review

  • 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

@carmocca carmocca added bug Something isn't working refactor labels Jul 23, 2021
@carmocca carmocca added this to the v1.4.x milestone Jul 23, 2021
@carmocca carmocca self-assigned this Jul 23, 2021
@carmocca carmocca requested a review from edenlightning as a code owner July 23, 2021 16:57
@codecov
Copy link

codecov bot commented Jul 23, 2021

Codecov Report

Merging #8536 (65ede1b) into master (a1be621) will increase coverage by 0%.
The diff coverage is 98%.

@@          Coverage Diff           @@
##           master   #8536   +/-   ##
======================================
  Coverage      93%     93%           
======================================
  Files         167     169    +2     
  Lines       14037   14072   +35     
======================================
+ Hits        13008   13043   +35     
  Misses       1029    1029           

@carmocca carmocca mentioned this pull request Jul 23, 2021
11 tasks
Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

LGTM !

"""
self.setup_training_type_plugin(model)
self.setup_training_type_plugin()
Copy link
Contributor

Choose a reason for hiding this comment

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

small note: This could be an issue if we ever decide to expose the Accelerator API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why an issue? There's the connect hook to connect the model already.

self.model should be available for the plugin when setup is called

Copy link
Contributor

Choose a reason for hiding this comment

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

@carmocca is this an invariant? should the accelerator assert that the model is available before calling setup training type plugin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ananthsub We could

pytorch_lightning/accelerators/accelerator.py Show resolved Hide resolved
@mergify mergify bot removed the has conflicts label Jul 26, 2021
@mergify mergify bot removed the has conflicts label Aug 2, 2021
@carmocca carmocca modified the milestones: v1.4.x, v1.5 Aug 2, 2021
@carmocca carmocca enabled auto-merge (squash) August 2, 2021 14:04
@mergify mergify bot added the has conflicts label Aug 2, 2021
@mergify mergify bot removed the has conflicts label Aug 2, 2021
@mergify mergify bot added the has conflicts label Aug 2, 2021
@mergify mergify bot removed the has conflicts label Aug 2, 2021
@carmocca carmocca marked this pull request as draft August 4, 2021 11:44
auto-merge was automatically disabled August 4, 2021 11:44

Pull request was converted to draft

Comment on lines +140 to +144
def setup_environment(self) -> None:
super().setup_environment()
model_call_configure_sharded_model_hook = getattr(
self.lightning_module, "call_configure_sharded_model_hook", False
)
Copy link
Contributor Author

@carmocca carmocca Aug 4, 2021

Choose a reason for hiding this comment

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

Documenting the explanation for this change:

Without this, the test tests/plugins/test_ddp_fully_sharded_with_full_state_dict.py::test_fully_sharded_plugin_checkpoint fails.

That test calls model.setup() inside the on_load_checkpoint hook. Its setup implementation manually modifies the value of self.call_configure_sharded_model_hook:

    def setup(self, stage: str) -> None:
        self.call_configure_sharded_model_hook = False
        ...

    def on_load_checkpoint(self, checkpoint: Dict[str, Any]) -> None:
        self.setup("fit")

This means, that before this change, the call_configure_sharded_model_hook value would diverge between the model and the training type plugin. Because its check in the training type plugin was done in the connect hook. (See the diff)

Some pseudocode to illustrate:

Order before this PR

load_checkpoint_weights
    model.on_load_checkpoint (in the test)
        model.setup -> this sets model.call_configure_sharded
acc.connect -> this checks model.call_configure_sharded
acc.setup_env
model.setup
configure_sharded_model -> this sets model.call_configure_sharded
acc.setup

Order after this PR without this change:

acc.connect -> this checks model.call_configure_sharded
load_checkpoint_weights
    model.on_load_checkpoint (in the test)
        model.setup -> this sets model.call_configure_sharded **BUG** as we already checked it
acc.setup_env
model.setup
configure_sharded_model -> this sets model.call_configure_sharded
acc.setup

Order after this PR with this change:

acc.connect
load_checkpoint_weights
    model.on_load_checkpoint (in the test)
        model.setup -> this sets model.call_configure_sharded
acc.setup_env -> this checks model.call_configure_sharded
model.setup
configure_sharded_model -> this sets model.call_configure_sharded
acc.setup

cc @SeanNaren @ananthsub

@carmocca carmocca marked this pull request as ready for review August 4, 2021 14:33
@carmocca carmocca merged commit ed13040 into master Aug 4, 2021
@carmocca carmocca deleted the refactor/ttp-model-connect branch August 4, 2021 15:43
"""
self.setup_training_type_plugin(model)
self.setup_training_type_plugin()
Copy link
Contributor

Choose a reason for hiding this comment

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

@carmocca is this an invariant? should the accelerator assert that the model is available before calling setup training type plugin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready PRs ready to be merged refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants