|
| 1 | +# Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | + |
| 3 | +"""Tests for ``wrap_model_chunks_with_ddp`` pg_collection threading. |
| 4 | +
|
| 5 | +These cover the training-loop helper directly (not the MIMO models), verifying |
| 6 | +that an explicit ``pg_collection`` built from a ``HyperCommGrid`` (no |
| 7 | +``parallel_state`` globals) is forwarded to standard ``DistributedDataParallel``, |
| 8 | +and that the default ``pg_collection=None`` path still falls back to MPU groups. |
| 9 | +""" |
| 10 | + |
| 11 | +import pytest |
| 12 | +import torch |
| 13 | +import torch.nn as nn |
| 14 | + |
| 15 | +from megatron.core import parallel_state |
| 16 | +from megatron.core.distributed import DistributedDataParallel, DistributedDataParallelConfig |
| 17 | +from megatron.core.hyper_comm_grid import HyperCommGrid |
| 18 | +from megatron.core.process_groups_config import ProcessGroupCollection |
| 19 | +from megatron.core.transformer import TransformerConfig |
| 20 | +from tests.unit_tests.test_utilities import Utils |
| 21 | + |
| 22 | +pytestmark = pytest.mark.skipif(torch.cuda.device_count() < 8, reason="Requires 8 GPUs") |
| 23 | + |
| 24 | + |
| 25 | +class _TinyModel(nn.Module): |
| 26 | + def __init__(self): |
| 27 | + super().__init__() |
| 28 | + self.fc1 = nn.Linear(16, 8) |
| 29 | + |
| 30 | + def forward(self, x): |
| 31 | + return self.fc1(x) |
| 32 | + |
| 33 | + |
| 34 | +def _make_chunk(): |
| 35 | + return _TinyModel().bfloat16().cuda() |
| 36 | + |
| 37 | + |
| 38 | +def _config(): |
| 39 | + return TransformerConfig(num_attention_heads=1, num_layers=1) |
| 40 | + |
| 41 | + |
| 42 | +class TestExplicitPgCollection: |
| 43 | + """Drive the helper with a HyperCommGrid-derived pgc, no parallel_state.""" |
| 44 | + |
| 45 | + def test_explicit_pg_collection_forwarded_to_ddp(self): |
| 46 | + from megatron.training.training import wrap_model_chunks_with_ddp |
| 47 | + |
| 48 | + Utils.initialize_distributed() # torch.distributed only, no model-parallel state |
| 49 | + # One pure-DP grid over the 8 ranks; tp/pp/ep are size-1 (no model parallelism). |
| 50 | + grid = HyperCommGrid([1, 1, 1, 8], ["tp", "pp", "ep", "dp"], backend="nccl") |
| 51 | + dp = grid.create_pg("dp") |
| 52 | + pg_collection = ProcessGroupCollection( |
| 53 | + tp=grid.create_pg("tp"), |
| 54 | + pp=grid.create_pg("pp"), |
| 55 | + ep=grid.create_pg("ep"), |
| 56 | + dp=dp, |
| 57 | + dp_cp=dp, |
| 58 | + expt_dp=dp, |
| 59 | + ) |
| 60 | + |
| 61 | + ddp_config = DistributedDataParallelConfig(use_distributed_optimizer=True) |
| 62 | + wrapped = wrap_model_chunks_with_ddp( |
| 63 | + [_make_chunk()], _config(), ddp_config, pg_collection=pg_collection |
| 64 | + ) |
| 65 | + |
| 66 | + chunk = wrapped[0] |
| 67 | + assert isinstance(chunk, DistributedDataParallel) |
| 68 | + # DDP was constructed against the grid-derived dp_cp group, not parallel_state. |
| 69 | + assert chunk.dp_cp_group is dp |
| 70 | + grid.destroy() |
| 71 | + |
| 72 | + |
| 73 | +class TestDefaultPath: |
| 74 | + """Isolated back-compat check: pg_collection=None falls back to MPU globals.""" |
| 75 | + |
| 76 | + def teardown_method(self, method): |
| 77 | + Utils.destroy_model_parallel() |
| 78 | + |
| 79 | + def test_default_path_uses_mpu(self): |
| 80 | + from megatron.training.training import wrap_model_chunks_with_ddp |
| 81 | + |
| 82 | + Utils.initialize_model_parallel( |
| 83 | + tensor_model_parallel_size=1, pipeline_model_parallel_size=1 |
| 84 | + ) |
| 85 | + ddp_config = DistributedDataParallelConfig(use_distributed_optimizer=True) |
| 86 | + wrapped = wrap_model_chunks_with_ddp([_make_chunk()], _config(), ddp_config) |
| 87 | + |
| 88 | + chunk = wrapped[0] |
| 89 | + assert isinstance(chunk, DistributedDataParallel) |
| 90 | + assert chunk.dp_cp_group is parallel_state.get_data_parallel_group( |
| 91 | + with_context_parallel=True |
| 92 | + ) |
0 commit comments