2022-03-16 11:29:37 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
import colossalai
|
2022-03-18 07:44:47 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
|
|
|
import torch.multiprocessing as mp
|
|
|
|
from colossalai.core import global_context as gpc
|
2022-03-16 11:29:37 +00:00
|
|
|
from colossalai.utils import free_port
|
2022-03-18 07:44:47 +00:00
|
|
|
from colossalai.zero.init_ctx import ZeroInitContext
|
|
|
|
from colossalai.zero.sharded_model.utils import col_model_deepcopy
|
2022-03-17 09:24:25 +00:00
|
|
|
from colossalai.zero.sharded_optim._utils import has_inf_or_nan
|
2022-03-25 09:25:12 +00:00
|
|
|
from colossalai.testing import rerun_on_exception
|
2022-03-18 07:44:47 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
2022-03-17 05:16:22 +00:00
|
|
|
from torch.nn.parallel import DistributedDataParallel as DDP
|
2022-03-16 11:29:37 +00:00
|
|
|
|
2022-03-23 06:59:59 +00:00
|
|
|
from common import (MP_PARALLEL_CONFIG, ZERO_PARALLEL_CONFIG, check_params, check_sharded_model_params)
|
2022-03-16 11:29:37 +00:00
|
|
|
|
|
|
|
|
2022-03-17 05:16:22 +00:00
|
|
|
def run_dist(rank, world_size, port, parallel_config):
|
|
|
|
colossalai.launch(config=parallel_config,
|
2022-03-17 02:20:53 +00:00
|
|
|
rank=rank,
|
|
|
|
world_size=world_size,
|
|
|
|
host='localhost',
|
|
|
|
port=port,
|
|
|
|
backend='nccl')
|
|
|
|
|
|
|
|
test_models = ['repeated_computed_layers', 'resnet18', 'bert']
|
2022-03-16 11:29:37 +00:00
|
|
|
for model_name in test_models:
|
|
|
|
get_components_func = non_distributed_component_funcs.get_callable(model_name)
|
|
|
|
model_builder, train_dataloader, _, optimizer_class, criterion = get_components_func()
|
2022-03-18 07:44:47 +00:00
|
|
|
with ZeroInitContext(convert_fp16=hasattr(gpc.config, 'fp16'),
|
|
|
|
target_device=torch.cuda.current_device(),
|
2022-03-18 08:18:31 +00:00
|
|
|
shard_strategy=gpc.config.zero.model_config.shard_strategy,
|
2022-03-18 07:44:47 +00:00
|
|
|
shard_param=True):
|
|
|
|
colo_model = model_builder(checkpoint=True)
|
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
colo_optimizer = optimizer_class(colo_model.parameters(), lr=1e-3)
|
2022-03-17 05:16:22 +00:00
|
|
|
engine, train_dataloader, _, _ = colossalai.initialize(colo_model,
|
2022-03-18 08:18:31 +00:00
|
|
|
optimizer=colo_optimizer,
|
2022-03-16 11:29:37 +00:00
|
|
|
criterion=criterion,
|
|
|
|
train_dataloader=train_dataloader)
|
2022-03-18 08:18:31 +00:00
|
|
|
torch_model = model_builder(checkpoint=True).half()
|
|
|
|
col_model_deepcopy(engine.model, torch_model)
|
|
|
|
torch_model = torch_model.cuda().float()
|
|
|
|
|
2022-03-16 11:29:37 +00:00
|
|
|
engine.train()
|
2022-03-17 09:24:25 +00:00
|
|
|
torch_optimizer = optimizer_class(torch_model.parameters(), lr=1e-3)
|
2022-03-16 11:29:37 +00:00
|
|
|
|
2022-03-17 05:16:22 +00:00
|
|
|
if dist.get_world_size() > 1:
|
|
|
|
torch_model = DDP(torch_model)
|
|
|
|
|
2022-03-16 11:29:37 +00:00
|
|
|
i = 0
|
|
|
|
for data, label in train_dataloader:
|
2022-03-17 05:16:22 +00:00
|
|
|
if i > 4:
|
2022-03-16 11:29:37 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
data, label = data.cuda(), label.cuda()
|
|
|
|
|
|
|
|
engine.zero_grad()
|
|
|
|
torch_optimizer.zero_grad()
|
|
|
|
|
|
|
|
if criterion:
|
|
|
|
output = engine(data)
|
|
|
|
loss = engine.criterion(output, label)
|
|
|
|
|
2022-03-17 02:20:53 +00:00
|
|
|
torch_output = torch_model(data)
|
|
|
|
torch_loss = engine.criterion(torch_output, label)
|
2022-03-16 11:29:37 +00:00
|
|
|
else:
|
|
|
|
loss = engine(data, label)
|
|
|
|
torch_loss = torch_model(data, label)
|
|
|
|
|
|
|
|
engine.backward(loss)
|
|
|
|
engine.step()
|
|
|
|
|
|
|
|
torch_loss.backward()
|
2022-03-17 09:24:25 +00:00
|
|
|
|
|
|
|
for param in torch_model.parameters():
|
|
|
|
if param.grad is not None:
|
|
|
|
assert not has_inf_or_nan(param.grad)
|
|
|
|
|
2022-03-16 11:29:37 +00:00
|
|
|
torch_optimizer.step()
|
|
|
|
i += 1
|
|
|
|
|
2022-03-17 05:16:22 +00:00
|
|
|
if parallel_config == MP_PARALLEL_CONFIG:
|
|
|
|
check_params(torch_model, colo_model, loose=True)
|
2022-03-17 09:24:25 +00:00
|
|
|
elif parallel_config == ZERO_PARALLEL_CONFIG:
|
2022-03-23 06:59:59 +00:00
|
|
|
check_sharded_model_params(torch_model, colo_model, loose=True)
|
2022-03-17 05:16:22 +00:00
|
|
|
|
|
|
|
|
2022-03-18 07:44:47 +00:00
|
|
|
# FIXME: enable this test in next PR
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip
|
2022-03-17 05:16:22 +00:00
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize("world_size", [2, 4])
|
2022-03-25 09:25:12 +00:00
|
|
|
@rerun_on_exception(exception_type=mp.ProcessRaisedException, pattern=".*Address already in use.*")
|
2022-03-17 05:16:22 +00:00
|
|
|
def test_mp_engine(world_size):
|
|
|
|
run_func = partial(run_dist, world_size=world_size, port=free_port(), parallel_config=MP_PARALLEL_CONFIG)
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
2022-03-16 11:29:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize("world_size", [1, 2])
|
2022-03-25 09:25:12 +00:00
|
|
|
@rerun_on_exception(exception_type=mp.ProcessRaisedException, pattern=".*Address already in use.*")
|
2022-03-17 05:16:22 +00:00
|
|
|
def test_zero_engine(world_size):
|
|
|
|
run_func = partial(run_dist, world_size=world_size, port=free_port(), parallel_config=ZERO_PARALLEL_CONFIG)
|
2022-03-16 11:29:37 +00:00
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-03-17 05:16:22 +00:00
|
|
|
test_zero_engine(world_size=4)
|