Skip to content

Commit e5c2fd9

Browse files
committed
[sync][training] fix: honor per-rank checkpoint object loading
Signed-off-by: Yu Yao <yaoyu.094@gmail.com>
1 parent 7983b66 commit e5c2fd9

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎src/megatron/bridge/training/checkpointing.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3836,7 +3836,11 @@ def _load_global_dist_base_checkpoint(
38363836
)
38373837
load_strategy = TorchDistLoadShardedStrategy()
38383838
if ckpt_cfg.fully_parallel_load:
3839-
load_strategy = FullyParallelLoadStrategyWrapper(load_strategy, _checkpoint_dp_cp_group(pg_collection))
3839+
load_strategy = FullyParallelLoadStrategyWrapper(
3840+
load_strategy,
3841+
_checkpoint_dp_cp_group(pg_collection),
3842+
per_rank_object_load=ckpt_cfg.ckpt_fully_parallel_load_per_rank_objects,
3843+
)
38403844
if checkpointing_context is not None:
38413845
checkpointing_context["load_strategy"] = load_strategy
38423846
validate_sharding_integrity = True

‎tests/unit_tests/training/test_checkpointing.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,45 @@ class _DummyClass:
102102
save_sharded_modelopt_state = None
103103

104104

105+
@pytest.mark.unit
106+
@pytest.mark.parametrize("per_rank_objects", [False, True])
107+
def test_global_checkpoint_load_forwards_per_rank_objects(per_rank_objects):
108+
"""The configured object-loading policy must reach the real Core strategy."""
109+
from megatron.bridge.training import checkpointing
110+
111+
config = CheckpointConfig(
112+
async_save=False,
113+
fully_parallel_load=True,
114+
ckpt_fully_parallel_load_per_rank_objects=per_rank_objects,
115+
)
116+
group = Mock()
117+
pg = SimpleNamespace(dp_cp=Mock(), dp_cp_gtp_remat=group)
118+
context = {}
119+
with (
120+
patch.object(checkpointing, "TorchDistLoadShardedStrategy") as base_strategy,
121+
patch.object(checkpointing.dist_checkpointing, "load", return_value={"model": {}}) as load,
122+
):
123+
result = checkpointing._load_global_dist_base_checkpoint(
124+
"checkpoint",
125+
config,
126+
False,
127+
{"model": {}},
128+
1,
129+
False,
130+
checkpoint_path_override="checkpoint/iter_0000001",
131+
checkpointing_context=context,
132+
pg_collection=pg,
133+
)
134+
135+
strategy = load.call_args.args[2]
136+
assert isinstance(strategy, checkpointing.FullyParallelLoadStrategyWrapper)
137+
assert strategy.per_rank_object_load is per_rank_objects
138+
assert strategy.parallelization_group is group
139+
assert strategy.base_strategy is base_strategy.return_value
140+
assert context["load_strategy"] is strategy
141+
assert result[0] == {"model": {}}
142+
143+
105144
def test_checkpoint_group_includes_gtp_and_supports_older_collections():
106145
replica_group = Mock()
107146
distribution_group = Mock()

0 commit comments

Comments
 (0)