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

[FSDP] Fix load_sharded_state_dict FQN mismatches for shared parameters #86524

Closed
wants to merge 4 commits into from

Conversation

fegin
Copy link
Contributor

@fegin fegin commented Oct 8, 2022

Stack from ghstack (oldest at bottom):

_sharded_pre_load_state_dict_hook() should calls _param_fqns() to ensure shared parameters names are also included.

Differential Revision: D40201304

`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Oct 8, 2022

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/86524

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures, 1 Pending

As of commit 5764aae:
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the release notes: distributed (sharded) release notes category label Oct 8, 2022
fegin added a commit that referenced this pull request Oct 8, 2022
`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)

ghstack-source-id: 169790107
Pull Request resolved: #86524
Copy link
Contributor

@awgu awgu left a comment

Choose a reason for hiding this comment

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

This change makes sense to me, but if we want to land this formally, should we include a unit test?

Her are some code examples for how to use TransformerWithSharedParams.init(), which has shared parameters (see common_fsdp.py for the precise details).

No FSDP:

model = TransformerWithSharedParams.init(
self.process_group,
FSDPInitMode.NO_FSDP,
CUDAInitMode.CUDA_BEFORE,
deterministic=True,
)

FSDP:

fsdp_kwargs = {
"auto_wrap_policy": functools.partial(
transformer_auto_wrap_policy,
transformer_layer_cls={
TransformerEncoderLayer,
TransformerDecoderLayer,
},
),
"use_orig_params": True,
"sharding_strategy": sharding_strategy,
"backward_prefetch": backward_prefetch,
"cpu_offload": cpu_offload,
}
model = TransformerWithSharedParams.init(
self.process_group,
FSDPInitMode.NO_FSDP,
cuda_init_mode,
deterministic=True,
)

@fegin
Copy link
Contributor Author

fegin commented Oct 10, 2022

@awgu Yup, that makes sense. What is puzzling me is that I thought test_state_dict already has shared parameters test cases but somehow this issue is not caught. I'm looking into the UT to understand what is missing.

@fegin
Copy link
Contributor Author

fegin commented Oct 10, 2022

@awgu Please ignore my previous comment. I didn't find shared parameters testing in test_fsdp_state_dict, probably was confused with Pyper's tests.

…ed parameters"

`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)

[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 10, 2022
Pull Request resolved: #86524

`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.
ghstack-source-id: 169844364

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)
)

fsdp_model = model_creator()
for tensor in itertools.chain(fsdp_model.parameters(), fsdp_model.buffers()):
Copy link
Member

Choose a reason for hiding this comment

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

what does this portion add to the unittest?

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 10, 2022
@property
def _shared_param_fqns(self) -> Iterator[Tuple[str, str, str]]:
for param_name, module_name in (
self._fsdp_wrapped_module.handle.shared_parameter_module_names()
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: i remembered _fsdp_wrapped_module could have multiple handles, so this should be "self._fsdp_wrapped_module.handles[0]"? @awgu

Copy link
Contributor

Choose a reason for hiding this comment

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

I think either @fegin or I will need to rebase, but it is not a big deal either way. The preferred approach will be self._handles[0] since I want to get rid of self._fsdp_wrapped_module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed to self._handles[0] as suggested.

…ed parameters"

`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)

[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 12, 2022
Pull Request resolved: #86524

`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.
ghstack-source-id: 170184507

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)
…ed parameters"

`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)

[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 13, 2022
Pull Request resolved: #86524

`_sharded_pre_load_state_dict_hook()` should calls `_param_fqns()` to ensure shared parameters names are also included.
ghstack-source-id: 170321602

Differential Revision: [D40201304](https://our.internmc.facebook.com/intern/diff/D40201304/)
@facebook-github-bot
Copy link
Contributor

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions
Copy link

Hey @fegin.
You've committed this PR, but it does not have both a 'release notes: ...' and 'topics: ...' label. Please add one of each to the PR. The 'release notes: ...' label should represent the part of PyTorch that this PR changes (fx, autograd, distributed, etc) and the 'topics: ...' label should represent the kind of PR it is (not user facing, new feature, bug fix, perf improvement, etc). The list of valid labels can be found here for the 'release notes: ...' and here for the 'topics: ...'.
For changes that are 'topic: not user facing' there is no need for a release notes label.

@facebook-github-bot facebook-github-bot deleted the gh/fegin/32/head branch June 8, 2023 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (sharded) release notes category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants