-
Notifications
You must be signed in to change notification settings - Fork 4.9k
ZeRO-3: stream partitioning of oversized parameters in zero.Init #8103
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
Achyuthan-S
wants to merge
1
commit into
deepspeedai:master
Choose a base branch
from
Achyuthan-S:fix/zero-init-stream-large-params
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.
+238
−2
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
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
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
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,133 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| import deepspeed | ||
| from deepspeed.runtime.zero import partition_parameters as pp | ||
| from deepspeed.runtime.zero.partition_parameters import _partition_chunk_overlap | ||
|
|
||
| from unit.common import DistributedTest | ||
|
|
||
|
|
||
| def _aligned_numel(numel, num_partitions): | ||
| remainder = numel % num_partitions | ||
| return numel + ((num_partitions - remainder) if remainder else 0) | ||
|
|
||
|
|
||
| def _stream_one_partition(full_flat, rank, num_partitions, chunk_numel): | ||
| """Rebuild a single rank's partition by streaming the flattened parameter in | ||
| fixed-size chunks, mirroring ``_partition_param_streaming``. Padding elements | ||
| (beyond the real numel) are left as the ``-1`` sentinel so the test can assert | ||
| they are never written.""" | ||
| ds_numel = full_flat.numel() | ||
| partition_numel = _aligned_numel(ds_numel, num_partitions) // num_partitions | ||
| partition_start = partition_numel * rank | ||
| out = torch.full((partition_numel, ), -1.0) | ||
| offset = 0 | ||
| while offset < ds_numel: | ||
| cur = min(chunk_numel, ds_numel - offset) | ||
| overlap = _partition_chunk_overlap(offset, cur, partition_start, partition_numel) | ||
| if overlap is not None: | ||
| dst_offset, src_offset, numel = overlap | ||
| chunk = full_flat.narrow(0, offset, cur) | ||
| out.narrow(0, dst_offset, numel).copy_(chunk.narrow(0, src_offset, numel)) | ||
| offset += cur | ||
| return out | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("numel,num_partitions,chunk_numel", [ | ||
| (64, 4, 8), | ||
| (64, 4, 7), | ||
| (60, 8, 5), | ||
| (10, 4, 3), | ||
| (1, 2, 4), | ||
| (100, 3, 1), | ||
| (128, 1, 16), | ||
| ]) | ||
| def test_streamed_partitions_match_direct_slicing(numel, num_partitions, chunk_numel): | ||
| full = torch.arange(numel, dtype=torch.float32) | ||
| partition_numel = _aligned_numel(numel, num_partitions) // num_partitions | ||
| aligned = partition_numel * num_partitions | ||
|
|
||
| rebuilt = torch.full((aligned, ), -1.0) | ||
| for rank in range(num_partitions): | ||
| partition = _stream_one_partition(full, rank, num_partitions, chunk_numel) | ||
| rebuilt.narrow(0, rank * partition_numel, partition_numel).copy_(partition) | ||
|
|
||
| # The real (non-padded) region must match the original parameter exactly. | ||
| assert torch.equal(rebuilt.narrow(0, 0, numel), full) | ||
| # Padding elements must never be written by the streaming copy. | ||
| if aligned > numel: | ||
| padding = rebuilt.narrow(0, numel, aligned - numel) | ||
| assert torch.all(padding == -1.0) | ||
|
|
||
|
|
||
| class TestStreamingPartitionMatchesStandard(DistributedTest): | ||
| world_size = 2 | ||
|
|
||
| def test_streaming_matches_standard(self): | ||
|
|
||
| def build(chunk_size): | ||
| config = { | ||
| "train_batch_size": self.world_size, | ||
| "zero_optimization": { | ||
| "stage": 3, | ||
| "stage3_partition_stream_chunk_size": chunk_size, | ||
| }, | ||
| } | ||
| torch.manual_seed(1234) | ||
| with deepspeed.zero.Init(config_dict_or_path=config): | ||
| linear = torch.nn.Linear(64, 64, bias=False) | ||
| return linear | ||
|
|
||
| # Reference: the standard broadcast-then-partition path (streaming disabled). | ||
| reference = build(0) | ||
| reference_partition = reference.weight.ds_tensor.detach().clone() | ||
|
|
||
| # Streaming the same parameter (4096 elements) in 512-element chunks must | ||
| # produce a byte-identical partition while actually exercising the new path. | ||
| streaming_calls = {"count": 0} | ||
| original = pp.Init._partition_param_streaming | ||
|
|
||
| def counting_stream(self, param, *args, **kwargs): | ||
| streaming_calls["count"] += 1 | ||
| return original(self, param, *args, **kwargs) | ||
|
|
||
| pp.Init._partition_param_streaming = counting_stream | ||
| try: | ||
| streamed = build(512) | ||
| finally: | ||
| pp.Init._partition_param_streaming = original | ||
|
|
||
| assert streaming_calls["count"] >= 1, "streaming partition path was not exercised" | ||
| assert torch.equal(streamed.weight.ds_tensor, reference_partition) | ||
|
|
||
| def test_streaming_via_module_path(self): | ||
| # zero.Init(module=...) decides whether to stream on plain torch parameters, | ||
| # before they are converted to ZeRO params. The decision must therefore not | ||
| # require ZeRO metadata (e.g. a per-parameter process group) that is only | ||
| # attached during conversion. | ||
|
|
||
| def build(chunk_size): | ||
| config = { | ||
| "train_batch_size": self.world_size, | ||
| "zero_optimization": { | ||
| "stage": 3, | ||
| "stage3_partition_stream_chunk_size": chunk_size, | ||
| }, | ||
| } | ||
| torch.manual_seed(99) | ||
| linear = torch.nn.Linear(64, 64, bias=True) # built on the host, then converted | ||
| deepspeed.zero.Init(module=linear, config_dict_or_path=config) | ||
| return linear | ||
|
|
||
| reference = build(0) | ||
| # weight (4096 elements) streams; bias (64) stays on the standard path but is | ||
| # still passed through the pre-conversion stream check. | ||
| streamed = build(512) | ||
| assert torch.equal(streamed.weight.ds_tensor, reference.weight.ds_tensor) | ||
| assert torch.equal(streamed.bias.ds_tensor, reference.bias.ds_tensor) |
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
stage3_partition_stream_chunk_sizeis set andzero.Init(module=prebuilt_model, ...)is used, this new pre-check runs on ordinarytorch.nn.Parameters before_zero_init_param()calls_convert_to_deepspeed_param()._should_stream_partition()immediately asks for_partition_world_size(param), which dereferencesparam.ds_process_group; that attribute is only installed later in_convert_to_deepspeed_param(), so the module-conversion path raisesAttributeErroreven for parameters smaller than the chunk size. Move the stream decision until after conversion, or make the pre-check use the default process group without requiring ZeRO metadata.Useful? React with 👍 / 👎.
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.
Fixed. _should_stream_partition now gates on the global num_partitions instead of _partition_world_size(param), so the zero.Init(module=...) path no longer dereferences param.ds_process_group before _convert_to_deepspeed_param attaches it. The per-parameter group is still used in the actual partitioning (_partition_param_streaming), which runs after conversion. Added a DistributedTest that exercises the module= path with streaming enabled to guard this.