ColossalAI/tests/test_zero_data_parallel/test_zero_param_mgr.py

40 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from functools import partial
import pytest
2022-03-08 04:03:35 +00:00
import torch
import torch.multiprocessing as mp
2022-03-08 04:03:35 +00:00
import colossalai
from colossalai.zero.sharded_model.param_manager import Zero3ParameterManager
from colossalai.core import global_context as gpc
from colossalai.context.parallel_mode import ParallelMode
from colossalai.utils import free_port
from common import CONFIG
2022-03-08 04:03:35 +00:00
def run_shard_shape_check(rank, world_size, port):
2022-03-08 04:03:35 +00:00
colossalai.launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
model = torch.nn.Linear(2, 4 * world_size)
gpc.init_parallel_groups()
2022-03-08 04:03:35 +00:00
Zero3ParameterManager(module=model,
process_group=gpc.get_group(ParallelMode.DATA),
offload_config=CONFIG.get('offload_param_config'))
2022-03-08 04:03:35 +00:00
assert (model.weight.numel() == 4 * 2)
assert (model.bias.numel() == 4)
@pytest.mark.dist
2022-03-08 04:03:35 +00:00
@pytest.mark.parametrize("world_size", [1, 2, 4])
def test_run_shard_shape(world_size):
run_func = partial(run_shard_shape_check, world_size=world_size, port=free_port())
mp.spawn(run_func, nprocs=world_size)
2022-03-08 04:03:35 +00:00
if __name__ == '__main__':
2022-03-08 04:03:35 +00:00
test_run_shard_shape(2)