2022-05-19 04:44:59 +00:00
|
|
|
import pytest
|
|
|
|
import colossalai
|
|
|
|
from colossalai.context.parallel_mode import ParallelMode
|
|
|
|
import torch.multiprocessing as mp
|
|
|
|
from colossalai.testing import rerun_if_address_is_in_use
|
|
|
|
from colossalai.utils.cuda import get_current_device
|
|
|
|
from colossalai.utils import free_port
|
2022-06-06 07:34:41 +00:00
|
|
|
from colossalai.utils.model.colo_init_context import ColoInitContext
|
2022-05-19 10:57:56 +00:00
|
|
|
from colossalai.tensor import TensorSpec, ComputePattern, ParallelAction, DistSpecManager, distspec
|
2022-05-19 04:44:59 +00:00
|
|
|
from colossalai.core import global_context as gpc
|
|
|
|
from functools import partial
|
2022-05-21 05:52:04 +00:00
|
|
|
from _utils import tensor_equal, tensor_shard_equal, set_seed
|
2022-05-19 10:57:56 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
2022-05-21 05:52:04 +00:00
|
|
|
from torch.nn.parallel import DistributedDataParallel as DDP
|
2022-06-06 07:34:41 +00:00
|
|
|
from colossalai.nn.parallel.data_parallel import ColoDDP
|
2022-05-19 04:44:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_1d_row_spec(model):
|
|
|
|
spec = TensorSpec(
|
|
|
|
distspec.shard(gpc.get_group(ParallelMode.PARALLEL_1D), [0], [gpc.get_world_size(ParallelMode.PARALLEL_1D)]),
|
2022-05-20 12:19:58 +00:00
|
|
|
ParallelAction(ComputePattern.TP1D))
|
2022-05-19 04:44:59 +00:00
|
|
|
with DistSpecManager.no_grad():
|
|
|
|
for n, p in model.named_parameters():
|
|
|
|
if 'weight' in n and 'ln' not in n:
|
|
|
|
p.set_spec(spec)
|
|
|
|
|
|
|
|
|
|
|
|
def init_1d_col_spec(model):
|
|
|
|
spec = TensorSpec(
|
|
|
|
distspec.shard(gpc.get_group(ParallelMode.PARALLEL_1D), [-1], [gpc.get_world_size(ParallelMode.PARALLEL_1D)]),
|
2022-05-20 12:19:58 +00:00
|
|
|
ParallelAction(ComputePattern.TP1D))
|
2022-05-19 04:44:59 +00:00
|
|
|
with DistSpecManager.no_grad():
|
|
|
|
for n, p in model.named_parameters():
|
|
|
|
if 'ln' not in n and ('weight' in n or 'bias' in n):
|
|
|
|
p.set_spec(spec)
|
|
|
|
|
|
|
|
|
|
|
|
def check_param_equal(model, torch_model):
|
|
|
|
for p, torch_p in zip(model.parameters(), torch_model.parameters()):
|
|
|
|
assert tensor_shard_equal(torch_p, p)
|
|
|
|
|
|
|
|
|
|
|
|
def check_grad_equal(model, torch_model):
|
|
|
|
for p, torch_p in zip(model.parameters(), torch_model.parameters()):
|
|
|
|
assert tensor_shard_equal(torch_p.grad, p.grad)
|
|
|
|
|
|
|
|
|
2022-05-21 05:52:04 +00:00
|
|
|
def run_gpt(init_spec_func, use_ddp):
|
2022-05-19 10:57:56 +00:00
|
|
|
get_components_func = non_distributed_component_funcs.get_callable('gpt2')
|
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
with ColoInitContext(device=get_current_device()):
|
2022-05-19 10:57:56 +00:00
|
|
|
model = model_builder()
|
2022-05-19 04:44:59 +00:00
|
|
|
model = model.cuda()
|
2022-05-19 10:57:56 +00:00
|
|
|
torch_model = model_builder().cuda()
|
2022-05-21 05:52:04 +00:00
|
|
|
if use_ddp:
|
|
|
|
model = ColoDDP(model)
|
|
|
|
torch_model = DDP(torch_model,
|
|
|
|
device_ids=[gpc.get_global_rank()],
|
|
|
|
process_group=gpc.get_group(ParallelMode.DATA))
|
2022-05-19 04:44:59 +00:00
|
|
|
for torch_p, p in zip(torch_model.parameters(), model.parameters()):
|
|
|
|
torch_p.data.copy_(p)
|
|
|
|
init_spec_func(model)
|
|
|
|
check_param_equal(model, torch_model)
|
|
|
|
model.train()
|
|
|
|
torch_model.train()
|
2022-05-21 05:52:04 +00:00
|
|
|
set_seed(gpc.get_local_rank(ParallelMode.DATA))
|
2022-05-19 10:57:56 +00:00
|
|
|
for i, (input_ids, attn_mask) in enumerate(train_dataloader):
|
2022-05-19 04:44:59 +00:00
|
|
|
logits = model(input_ids, attn_mask)
|
|
|
|
torch_logits = torch_model(input_ids, attn_mask)
|
|
|
|
assert tensor_equal(torch_logits, logits)
|
|
|
|
loss = criterion(logits, input_ids)
|
|
|
|
torch_loss = criterion(torch_logits, input_ids)
|
2022-05-21 05:52:04 +00:00
|
|
|
if use_ddp:
|
|
|
|
model.backward(loss)
|
|
|
|
else:
|
|
|
|
loss.backward()
|
2022-05-19 04:44:59 +00:00
|
|
|
torch_loss.backward()
|
|
|
|
check_grad_equal(model, torch_model)
|
2022-05-19 10:57:56 +00:00
|
|
|
if i > 0:
|
|
|
|
break
|
2022-05-19 04:44:59 +00:00
|
|
|
|
|
|
|
|
2022-05-21 05:52:04 +00:00
|
|
|
def run_dist(rank, world_size, port, use_ddp):
|
|
|
|
if use_ddp and world_size == 1:
|
|
|
|
return
|
|
|
|
tp_world_size = world_size // 2 if use_ddp else world_size
|
|
|
|
config = dict(parallel=dict(tensor=dict(mode="1d", size=tp_world_size),))
|
2022-05-19 04:44:59 +00:00
|
|
|
colossalai.launch(config=config, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
2022-05-21 05:52:04 +00:00
|
|
|
run_gpt(init_1d_row_spec, use_ddp)
|
|
|
|
run_gpt(init_1d_col_spec, use_ddp)
|
2022-05-19 04:44:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize('world_size', [1, 4])
|
2022-05-21 05:52:04 +00:00
|
|
|
@pytest.mark.parametrize('use_ddp', [False, True])
|
2022-05-19 04:44:59 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-05-21 05:52:04 +00:00
|
|
|
def test_gpt(world_size, use_ddp):
|
|
|
|
run_func = partial(run_dist, world_size=world_size, port=free_port(), use_ddp=use_ddp)
|
2022-05-19 04:44:59 +00:00
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-06-03 04:09:49 +00:00
|
|
|
test_gpt(4, False)
|