2022-08-02 07:49:13 +00:00
|
|
|
import copy
|
2023-04-04 05:48:16 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
from functools import partial
|
2022-08-02 07:49:13 +00:00
|
|
|
|
2022-06-20 02:51:47 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
import torch.multiprocessing as mp
|
2023-04-04 05:48:16 +00:00
|
|
|
|
|
|
|
import colossalai
|
|
|
|
from colossalai.nn.parallel import ColoDDP
|
|
|
|
from colossalai.tensor import ColoParameter, ProcessGroup
|
2022-06-20 02:51:47 +00:00
|
|
|
from colossalai.testing import rerun_if_address_is_in_use
|
|
|
|
from colossalai.utils import free_port
|
2023-04-04 05:48:16 +00:00
|
|
|
from colossalai.utils.cuda import get_current_device
|
|
|
|
from colossalai.zero import ColoInitContext
|
2022-06-20 02:51:47 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
|
|
|
|
|
|
|
|
|
|
|
def check_state_dict_equal(state_dict: OrderedDict, other_state_dict: OrderedDict):
|
|
|
|
for (k1, t1), (k2, t2) in zip(state_dict.items(), other_state_dict.items()):
|
|
|
|
assert k1 == k2
|
2022-07-26 06:13:38 +00:00
|
|
|
|
|
|
|
if t1.device != t2.device:
|
|
|
|
temp_t2 = t2.to(t1.device)
|
|
|
|
else:
|
|
|
|
temp_t2 = t2
|
|
|
|
|
2022-08-02 07:49:13 +00:00
|
|
|
assert torch.equal(t1, temp_t2), "\t{}\n\t{}".format(t1, temp_t2)
|
|
|
|
|
|
|
|
|
2022-06-20 02:51:47 +00:00
|
|
|
def init_ddp(module: torch.nn.Module) -> ColoDDP:
|
2022-07-04 10:54:37 +00:00
|
|
|
pg = ProcessGroup()
|
|
|
|
return ColoDDP(module, process_group=pg)
|
2022-06-20 02:51:47 +00:00
|
|
|
|
|
|
|
|
2022-08-02 07:49:13 +00:00
|
|
|
def run_ddp_state_dict():
|
|
|
|
get_components_func = non_distributed_component_funcs.get_callable('gpt2')
|
2022-06-20 02:51:47 +00:00
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
|
|
|
torch_model = model_builder().cuda()
|
|
|
|
with ColoInitContext(device=get_current_device()):
|
|
|
|
model = model_builder()
|
2022-08-02 07:49:13 +00:00
|
|
|
model = init_ddp(model)
|
2022-06-20 02:51:47 +00:00
|
|
|
torch_state_dict = torch_model.state_dict()
|
2022-08-02 07:49:13 +00:00
|
|
|
|
2022-07-08 06:18:30 +00:00
|
|
|
for param in model.parameters():
|
|
|
|
if isinstance(param, ColoParameter):
|
|
|
|
assert param.get_process_group() is not None
|
2022-06-20 02:51:47 +00:00
|
|
|
model.load_state_dict(torch_state_dict)
|
2022-07-08 06:18:30 +00:00
|
|
|
|
|
|
|
for param in model.parameters():
|
|
|
|
if isinstance(param, ColoParameter):
|
|
|
|
assert param.get_process_group() is not None
|
|
|
|
|
2022-06-20 02:51:47 +00:00
|
|
|
state_dict = model.state_dict()
|
|
|
|
check_state_dict_equal(torch_state_dict, state_dict)
|
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
2022-08-02 07:49:13 +00:00
|
|
|
run_ddp_state_dict()
|
2022-06-20 02:51:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize('world_size', [1, 2])
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_state_dict(world_size):
|
|
|
|
run_func = partial(run_dist, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_state_dict(2)
|