-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Fix ZeRO-3 all_reduce param fetch stride for padded parameters #8158
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
Open
ebarkhordar
wants to merge
2
commits into
deepspeedai:master
Choose a base branch
from
ebarkhordar:fix/zero3-allreduce-fetch-stride-aligned
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/unit/runtime/zero/test_zero_allreduce_fetch_params.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright (c) DeepSpeed Team. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| import deepspeed | ||
| import deepspeed.runtime.zero.partition_parameters as partition_parameters | ||
| from unit.common import DistributedTest | ||
|
|
||
| # Odd numels need a slot of partition padding under world_size=2, so ds_numel_aligned | ||
| # differs from ds_numel. Even numels make the two equal, which is the case the flat-buffer | ||
| # stride already handled; both are checked so the padded fix does not regress it. | ||
| PADDED_NUMELS = [5, 7] | ||
| ALIGNED_NUMELS = [4, 6] | ||
| POISON = 7777.0 | ||
|
|
||
|
|
||
| class ParamHolder(torch.nn.Module): | ||
|
|
||
| def __init__(self, numels): | ||
| super().__init__() | ||
| for i, numel in enumerate(numels): | ||
| self.register_parameter(f"p{i}", torch.nn.Parameter(torch.arange(1, numel + 1, dtype=torch.float32))) | ||
|
|
||
|
|
||
| class TestAllReduceFetchParamsPadded(DistributedTest): | ||
| world_size = 2 | ||
|
|
||
| @pytest.mark.parametrize("numels", [PADDED_NUMELS, ALIGNED_NUMELS], ids=["padded", "aligned"]) | ||
| def test_params_reconstruct_exactly(self, numels): | ||
| config = { | ||
| "train_micro_batch_size_per_gpu": 1, | ||
| "zero_optimization": { | ||
| "stage": 3, | ||
| "stage3_use_all_reduce_for_fetch_params": True, | ||
| }, | ||
| } | ||
|
|
||
| # The tail of the last rank's partition is never written (torch.empty, and the | ||
| # partial-copy branch only copies the elements that exist), so its contents are | ||
| # whatever the allocator returns. Fill new allocations with a sentinel so the test | ||
| # is deterministic instead of depending on whether the allocator hands back a | ||
| # freshly zeroed page. | ||
| real_empty = partition_parameters._orig_torch_empty | ||
|
|
||
| def poisoned_empty(*args, **kwargs): | ||
| tensor = real_empty(*args, **kwargs) | ||
| if tensor.dtype.is_floating_point: | ||
| tensor.fill_(POISON) | ||
| return tensor | ||
|
|
||
| expected = [torch.arange(1, numel + 1, dtype=torch.float32) for numel in numels] | ||
|
|
||
| # Init.__exit__ restores torch.empty by reading _orig_torch_empty, so on the way out | ||
| # it rebinds the public torch.empty to poisoned_empty. Restoring the module global | ||
| # alone would leave every later allocation in this worker process sentinel-filled, | ||
| # so put the public binding back too. | ||
| saved_torch_empty = torch.empty | ||
|
|
||
| partition_parameters._orig_torch_empty = poisoned_empty | ||
| try: | ||
| with deepspeed.zero.Init(config_dict_or_path=config, mem_efficient_linear=False, enabled=True): | ||
| module = ParamHolder(numels) | ||
| finally: | ||
| partition_parameters._orig_torch_empty = real_empty | ||
| torch.empty = saved_torch_empty | ||
|
|
||
| params = [getattr(module, f"p{i}") for i in range(len(numels))] | ||
| params[0].all_gather_coalesced(params).wait() | ||
|
|
||
| for i, param in enumerate(params): | ||
| gathered = param.data.detach().reshape(-1).cpu() | ||
| assert torch.equal( | ||
| gathered, expected[i]), (f"param p{i} was not reconstructed exactly: expected {expected[i].tolist()}, " | ||
| f"got {gathered.tolist()}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this test exits the
deepspeed.zero.Initcontext,Init.__exit__restorestorch.emptyfrompartition_parameters._orig_torch_empty; because this assignment is still pointing atpoisoned_emptyuntil thefinallyblock runs,torch.emptyis left globally patched to return sentinel-filled tensors for the remainder of the worker process. In contexts that reuse the distributed worker or add any later allocation in this test, unrelated code will see poisoned allocations, so the cleanup needs to restore the publictorch.emptybinding as well or avoid mutating_orig_torch_emptythrough the context teardown.Useful? React with 👍 / 👎.