-
Notifications
You must be signed in to change notification settings - Fork 5k
Sort checkpoint shard files by numeric rank #8452
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
1
commit into
deepspeedai:master
Choose a base branch
from
ebarkhordar:fix/1381-ckpt-shard-natural-sort
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.
+114
−17
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
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,93 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import os | ||
| from types import SimpleNamespace | ||
|
|
||
| import torch | ||
|
|
||
| from deepspeed.runtime.engine import DeepSpeedEngine | ||
| from deepspeed.runtime.pipe.module import PipelineModule | ||
| from deepspeed.runtime.pipe.topology import PipeModelDataParallelTopology | ||
| from deepspeed.runtime.state_dict_factory import SDLoaderFactory | ||
|
|
||
| # The rank field in a checkpoint file name is padded to two digits, so this is the | ||
| # smallest interesting degree: the field overflows and lexicographic order diverges | ||
| # from rank order. | ||
| MP_DEGREE_OVER_PAD = 128 | ||
| MP_DEGREE_UNDER_PAD = 8 | ||
|
|
||
|
|
||
| class PipelineModuleStub: | ||
| """Supplies only the attributes the two checkpoint path methods read off ``self``.""" | ||
|
|
||
| ckpt_layer_path = PipelineModule.ckpt_layer_path | ||
| ckpt_layer_path_list = PipelineModule.ckpt_layer_path_list | ||
|
|
||
| def __init__(self, topo, global_rank=0): | ||
| self._local_start = 0 | ||
| self.global_rank = global_rank | ||
| self._grid = SimpleNamespace(_topo=topo) | ||
|
|
||
|
|
||
| class EngineStub: | ||
| """Supplies only the attributes the two checkpoint name methods read off ``self``.""" | ||
|
|
||
| _get_ckpt_name = DeepSpeedEngine._get_ckpt_name | ||
| _get_all_ckpt_names = DeepSpeedEngine._get_all_ckpt_names | ||
|
|
||
| def __init__(self, checkpoint_mp_rank=0): | ||
| self.checkpoint_mp_rank = checkpoint_mp_rank | ||
|
|
||
| def zero_optimization_partition_weights(self): | ||
| return False | ||
|
|
||
| def load_universal_checkpoint(self): | ||
| return False | ||
|
|
||
|
|
||
| def write_pipeline_layer_shards(ckpt_dir, mp_degree): | ||
| topo = PipeModelDataParallelTopology(num_pp=1, num_mp=mp_degree, num_dp=1) | ||
| for rank in range(mp_degree): | ||
| torch.save({'rank': rank}, PipelineModuleStub(topo, rank).ckpt_layer_path(ckpt_dir, 0)) | ||
| return topo | ||
|
|
||
|
|
||
| def test_pipeline_layer_shards_load_by_numeric_rank(tmpdir): | ||
| ckpt_dir = str(tmpdir) | ||
| topo = write_pipeline_layer_shards(ckpt_dir, MP_DEGREE_OVER_PAD) | ||
|
|
||
| ckpt_list = PipelineModuleStub(topo).ckpt_layer_path_list(ckpt_dir, 0) | ||
| assert len(ckpt_list) == MP_DEGREE_OVER_PAD | ||
|
|
||
| sd_loader = SDLoaderFactory.get_sd_loader(ckpt_list, version=2.0, checkpoint_engine=None) | ||
| for mp_rank in range(MP_DEGREE_OVER_PAD): | ||
| _, sd, _ = sd_loader.load(MP_DEGREE_OVER_PAD, mp_rank, module_key=None, is_pipe_parallel=True) | ||
| assert sd['rank'] == mp_rank | ||
|
|
||
|
|
||
| def test_engine_checkpoint_names_ordered_by_numeric_rank(tmpdir): | ||
| ckpt_dir, tag = str(tmpdir), 'global_step100' | ||
| os.makedirs(os.path.join(ckpt_dir, tag)) | ||
| for rank in range(MP_DEGREE_OVER_PAD): | ||
| torch.save({'rank': rank}, EngineStub(rank)._get_ckpt_name(ckpt_dir, tag)) | ||
|
|
||
| ckpt_files = EngineStub()._get_all_ckpt_names(ckpt_dir, tag) | ||
| assert len(ckpt_files) == MP_DEGREE_OVER_PAD | ||
|
|
||
| loaded = [torch.load(f, weights_only=True)['rank'] for f in ckpt_files] | ||
| assert loaded == list(range(MP_DEGREE_OVER_PAD)) | ||
|
|
||
|
|
||
| def test_shard_order_below_pad_width_matches_lexicographic(tmpdir): | ||
| # Every checkpoint written before this change has a rank field of at most two | ||
| # digits, where the two orderings agree, so none of them is reordered. | ||
| ckpt_dir = str(tmpdir) | ||
| topo = write_pipeline_layer_shards(ckpt_dir, MP_DEGREE_UNDER_PAD) | ||
|
|
||
| ckpt_list = PipelineModuleStub(topo).ckpt_layer_path_list(ckpt_dir, 0) | ||
| loaded = [torch.load(p, weights_only=True)['rank'] for p in ckpt_list] | ||
| assert loaded == list(range(MP_DEGREE_UNDER_PAD)) | ||
| assert ckpt_list == sorted(ckpt_list) | ||
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.
This is a non-merge commit, but its commit message has no
Signed-off-bytrailer, so it violates the repository's mandatory commit requirement and may fail the corresponding CI/DCO check. Recreate the commit using--signoffwith the configured Git identity.AGENTS.md reference: AGENTS.md:L8-L8
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.
The trailer is there. The commit message ends
Signed-off-by: Ehsan Barkhordar <realbarkhordar@gmail.com>, and the DCO check on this same commit is green.