2022-11-14 08:05:09 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
import pytest
|
2022-04-22 10:03:35 +00:00
|
|
|
import torch
|
2022-11-14 08:05:09 +00:00
|
|
|
import torch.multiprocessing as mp
|
2022-04-22 10:03:35 +00:00
|
|
|
|
2022-11-14 08:05:09 +00:00
|
|
|
import colossalai
|
|
|
|
from colossalai.tensor import (
|
|
|
|
ColoParameter,
|
|
|
|
ColoTensorSpec,
|
|
|
|
ComputePattern,
|
|
|
|
ComputeSpec,
|
|
|
|
ProcessGroup,
|
|
|
|
ReplicaSpec,
|
|
|
|
ShardSpec,
|
|
|
|
)
|
|
|
|
from colossalai.testing import parameterize, rerun_if_address_is_in_use
|
|
|
|
from colossalai.utils import free_port
|
2022-04-25 06:24:26 +00:00
|
|
|
from colossalai.utils.cuda import get_current_device
|
2022-11-09 03:50:33 +00:00
|
|
|
from colossalai.utils.model.colo_init_context import ColoInitContext
|
2022-11-14 08:05:09 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
|
|
|
from tests.test_tensor.common_utils import set_seed
|
|
|
|
|
|
|
|
|
|
|
|
def run_colo_init_context(rank: int, world_size: int, port: int):
|
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
|
|
|
|
# make sure seed of each process is the same, so the params are consistent among processes and the params are exactly replicated.
|
|
|
|
set_seed(42)
|
|
|
|
get_components_func = non_distributed_component_funcs.get_callable('gpt2')
|
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
|
|
|
|
|
|
|
# keep parameters replicated during init
|
|
|
|
with ColoInitContext(device=get_current_device()):
|
|
|
|
model1 = model_builder()
|
|
|
|
|
|
|
|
# shard the parameters during init
|
|
|
|
set_seed(42)
|
|
|
|
shard_spec = ReplicaSpec()
|
2022-11-15 08:24:16 +00:00
|
|
|
|
|
|
|
# If using ShardSpec, the assertations will failed.
|
|
|
|
# But it is not a bug, the initialized values are not consist with the original one.
|
|
|
|
# shard_spec = ShardSpec(dims=[0], num_partitions=[world_size])
|
|
|
|
default_pg = ProcessGroup(tp_degree=world_size)
|
|
|
|
with ColoInitContext(device=get_current_device(), default_pg=default_pg, default_dist_spec=shard_spec):
|
2022-11-14 08:05:09 +00:00
|
|
|
model2 = model_builder()
|
|
|
|
|
|
|
|
# reshard both models
|
|
|
|
new_shard = ShardSpec(dims=[-1], num_partitions=[world_size])
|
|
|
|
for p1, p2 in zip(model1.parameters(), model2.parameters()):
|
|
|
|
p1: ColoParameter = p1
|
|
|
|
p1.set_process_group(ProcessGroup(tp_degree=world_size))
|
|
|
|
p1.set_dist_spec(new_shard)
|
|
|
|
p2.set_dist_spec(new_shard)
|
|
|
|
|
|
|
|
for p1, p2 in zip(model1.parameters(), model2.parameters()):
|
|
|
|
assert (torch.allclose(p1, p2))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize('world_size', [1, 4])
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_colo_init_context(world_size):
|
|
|
|
run_func = partial(run_colo_init_context, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_colo_init_context(2)
|