2022-07-04 10:54:37 +00:00
|
|
|
from functools import partial
|
2022-11-09 03:50:33 +00:00
|
|
|
|
|
|
|
import pytest
|
2022-04-27 04:00:18 +00:00
|
|
|
import torch
|
2022-04-24 08:43:44 +00:00
|
|
|
import torch.multiprocessing as mp
|
2022-07-04 10:54:37 +00:00
|
|
|
|
|
|
|
import colossalai
|
2022-11-09 03:50:33 +00:00
|
|
|
from colossalai.nn.optimizer import ColossalaiOptimizer
|
|
|
|
from colossalai.tensor import ColoTensor, ProcessGroup
|
|
|
|
from colossalai.tensor.colo_parameter import ColoParameter
|
2022-06-06 07:34:41 +00:00
|
|
|
from colossalai.testing import rerun_if_address_is_in_use
|
2022-04-24 08:43:44 +00:00
|
|
|
from colossalai.utils import free_port
|
2022-11-09 03:50:33 +00:00
|
|
|
from colossalai.utils.cuda import get_current_device
|
2022-06-06 07:34:41 +00:00
|
|
|
from colossalai.utils.model.colo_init_context import ColoInitContext
|
2022-07-04 10:54:37 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
2022-11-09 03:50:33 +00:00
|
|
|
from tests.test_tensor.common_utils import (
|
|
|
|
check_equal,
|
|
|
|
set_seed,
|
|
|
|
split_param_col_tp1d,
|
|
|
|
split_param_row_tp1d,
|
|
|
|
tensor_shard_equal,
|
|
|
|
)
|
2022-04-27 04:00:18 +00:00
|
|
|
|
2022-05-16 06:58:08 +00:00
|
|
|
|
2022-05-06 10:06:22 +00:00
|
|
|
def run_1d_hybrid_tp(model_name):
|
2022-04-28 02:55:40 +00:00
|
|
|
# A simple net with two stacked nn.Linear
|
2022-05-06 09:07:56 +00:00
|
|
|
get_components_func = non_distributed_component_funcs.get_callable(model_name)
|
2022-04-28 02:55:40 +00:00
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
2022-07-12 15:26:45 +00:00
|
|
|
|
2022-06-29 06:08:40 +00:00
|
|
|
rank = torch.distributed.get_rank()
|
2022-07-12 15:26:45 +00:00
|
|
|
world_size = torch.distributed.get_world_size()
|
2022-04-28 02:55:40 +00:00
|
|
|
|
|
|
|
set_seed(1)
|
|
|
|
with ColoInitContext(device=get_current_device()):
|
|
|
|
model = model_builder(checkpoint=True)
|
2022-05-16 06:58:08 +00:00
|
|
|
|
2022-05-13 03:37:23 +00:00
|
|
|
if rank == 0:
|
|
|
|
model_torch = model_builder(checkpoint=True)
|
|
|
|
model_torch = model_torch.cuda()
|
2022-07-14 08:57:48 +00:00
|
|
|
|
|
|
|
optimizer_torch = ColossalaiOptimizer(torch.optim.SGD(model_torch.parameters(), lr=0.1))
|
2022-05-13 03:37:23 +00:00
|
|
|
|
|
|
|
# Make two models have the same init params
|
|
|
|
for p1, p2 in zip(model.parameters(), model_torch.parameters()):
|
|
|
|
p2.data.copy_(p1.data)
|
2022-07-12 15:26:45 +00:00
|
|
|
else:
|
|
|
|
model_torch = None
|
|
|
|
optimizer_torch = None
|
2022-04-28 02:55:40 +00:00
|
|
|
|
2022-06-29 06:08:40 +00:00
|
|
|
pg = ProcessGroup(tp_degree=world_size)
|
2022-05-06 09:07:56 +00:00
|
|
|
if 'bert' == model_name:
|
2022-05-19 04:44:59 +00:00
|
|
|
for name, p in model.named_parameters():
|
2022-05-06 09:07:56 +00:00
|
|
|
if not isinstance(p, ColoTensor):
|
|
|
|
continue
|
2022-07-12 15:45:29 +00:00
|
|
|
|
2022-05-06 10:06:22 +00:00
|
|
|
# num_class = type_vocab_size = 2 | (8, 2)
|
2022-07-12 15:26:45 +00:00
|
|
|
if 'classifier' in name and 'weight' in name:
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_col_tp1d(p, pg)
|
2022-05-06 10:06:22 +00:00
|
|
|
# num_class = vocab_size = 30524 | (30524, 8)
|
2022-07-12 15:45:29 +00:00
|
|
|
elif 'word_embeddings' in name and 'weight' in name:
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_row_tp1d(p, pg)
|
2022-05-06 10:06:22 +00:00
|
|
|
# num_class = seq_len = 512 | (512, 8)
|
2022-07-12 15:45:29 +00:00
|
|
|
elif 'position_embeddings' in name and 'weight' in name:
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_row_tp1d(p, pg)
|
2022-05-06 10:06:22 +00:00
|
|
|
# num_class = type_vocab_size = 2 | (2, 8)
|
2022-07-12 15:45:29 +00:00
|
|
|
elif 'token_type_embeddings' in name and 'weight' in name:
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_col_tp1d(p, pg)
|
2022-07-12 15:45:29 +00:00
|
|
|
|
2022-05-06 09:07:56 +00:00
|
|
|
elif "simple_net" == model_name:
|
|
|
|
# A naive way to set spec for all weights in Linear
|
2022-05-19 04:44:59 +00:00
|
|
|
for name, p in model.named_parameters():
|
2022-05-06 09:07:56 +00:00
|
|
|
if not isinstance(p, ColoTensor):
|
|
|
|
continue
|
2022-05-07 07:49:14 +00:00
|
|
|
if 'embed' in name and 'weight' in name:
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_col_tp1d(p, pg)
|
2022-05-06 09:07:56 +00:00
|
|
|
if 'proj1' in name and ('weight' in name or 'bias' in name):
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_row_tp1d(p, pg)
|
2022-05-06 09:07:56 +00:00
|
|
|
if 'proj2' in name and 'weight' in name:
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_col_tp1d(p, pg)
|
2022-05-07 07:49:14 +00:00
|
|
|
if 'classifier' in name and ('weight' in name or 'bias' in name):
|
2022-07-13 15:06:12 +00:00
|
|
|
split_param_row_tp1d(p, pg)
|
2022-04-29 06:10:05 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
model = model.cuda()
|
2022-07-15 10:19:52 +00:00
|
|
|
model.eval()
|
2022-07-12 15:26:45 +00:00
|
|
|
if rank == 0:
|
2022-07-15 10:19:52 +00:00
|
|
|
model_torch.eval()
|
2022-07-12 15:26:45 +00:00
|
|
|
|
2022-07-14 08:57:48 +00:00
|
|
|
colo_optimizer = ColossalaiOptimizer(torch.optim.SGD(model.parameters(), lr=0.1))
|
2022-07-12 15:26:45 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
for i, (data, label) in enumerate(train_dataloader):
|
2022-07-12 15:26:45 +00:00
|
|
|
|
|
|
|
# Zero grad
|
2022-05-13 03:37:23 +00:00
|
|
|
colo_optimizer.zero_grad()
|
|
|
|
if rank == 0:
|
2022-07-12 15:26:45 +00:00
|
|
|
optimizer_torch.zero_grad()
|
2022-07-15 10:19:52 +00:00
|
|
|
torch.distributed.barrier()
|
2022-05-16 06:58:08 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
data = data.to(get_current_device())
|
|
|
|
label = label.to(get_current_device())
|
|
|
|
|
2022-06-29 02:03:09 +00:00
|
|
|
torch.distributed.broadcast(data, 0, group=pg.tp_process_group())
|
|
|
|
torch.distributed.broadcast(label, 0, group=pg.tp_process_group())
|
2022-07-07 10:09:18 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
# Bcast rank0 data to all processes
|
|
|
|
if criterion:
|
|
|
|
output = model(data)
|
|
|
|
loss = criterion(output, label)
|
|
|
|
else:
|
|
|
|
output = model(data, label)
|
|
|
|
loss = output
|
|
|
|
|
2022-07-12 15:26:45 +00:00
|
|
|
# Test output
|
2022-04-28 02:55:40 +00:00
|
|
|
if rank == 0:
|
|
|
|
if criterion:
|
|
|
|
output_torch = model_torch(data)
|
|
|
|
loss_torch = criterion(output_torch, label)
|
|
|
|
else:
|
|
|
|
output_torch = model_torch(data, label)
|
|
|
|
loss_torch = output_torch
|
2022-12-06 04:53:58 +00:00
|
|
|
assert torch.allclose(loss, loss_torch, rtol=1e-2), f"model_name {model_name} failed"
|
2022-07-15 10:19:52 +00:00
|
|
|
torch.distributed.barrier()
|
2022-04-28 02:55:40 +00:00
|
|
|
|
|
|
|
loss.backward()
|
2022-05-13 03:37:23 +00:00
|
|
|
colo_optimizer.step()
|
2022-04-28 02:55:40 +00:00
|
|
|
|
|
|
|
if rank == 0:
|
|
|
|
loss_torch.backward()
|
2022-07-12 15:26:45 +00:00
|
|
|
optimizer_torch.step()
|
2022-05-13 03:37:23 +00:00
|
|
|
|
|
|
|
with torch.no_grad():
|
|
|
|
# check param
|
2022-06-09 09:36:15 +00:00
|
|
|
for p, torch_p in zip(model.parameters(), model_torch.parameters()):
|
2022-07-04 10:54:37 +00:00
|
|
|
assert tensor_shard_equal(torch_p, p, pg.tp_local_rank(), pg.tp_world_size())
|
2022-07-15 10:19:52 +00:00
|
|
|
torch.distributed.barrier()
|
2022-04-28 02:55:40 +00:00
|
|
|
if i > 5:
|
|
|
|
break
|
2022-04-27 04:00:18 +00:00
|
|
|
|
2022-04-28 07:23:40 +00:00
|
|
|
|
2022-04-28 02:57:14 +00:00
|
|
|
# Test the overrided parameters() and named_parameters() member functions
|
|
|
|
def test_model_parameters():
|
2022-07-06 08:15:16 +00:00
|
|
|
colossalai.launch(config={}, rank=0, world_size=1, host='localhost', port=free_port(), backend='nccl')
|
|
|
|
|
2022-04-28 02:57:14 +00:00
|
|
|
# build a module with 2 Linear, 4 parameters in total.
|
|
|
|
class Net(torch.nn.Module):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.fcs = torch.nn.Sequential(torch.nn.Linear(2, 3), torch.nn.Linear(3, 2))
|
|
|
|
self.extra_param = torch.nn.Parameter(torch.randn(2))
|
|
|
|
|
|
|
|
with ColoInitContext(device=get_current_device()):
|
|
|
|
model = Net()
|
|
|
|
|
|
|
|
param_cnt = 0
|
|
|
|
for name, p in model.named_parameters():
|
|
|
|
param_cnt += 1
|
|
|
|
assert param_cnt == 5
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
for name, colo_p in model.named_parameters():
|
2022-05-06 04:57:14 +00:00
|
|
|
assert colo_p.is_model_data()
|
|
|
|
|
2022-04-28 02:57:14 +00:00
|
|
|
param_cnt = 0
|
|
|
|
for name, p in model.named_parameters(recurse=False):
|
|
|
|
param_cnt += 1
|
|
|
|
assert param_cnt == 1
|
|
|
|
|
|
|
|
param_cnt = 0
|
|
|
|
for p in model.fcs[0].parameters(recurse=False):
|
|
|
|
param_cnt += 1
|
|
|
|
assert param_cnt == 2
|
|
|
|
|
|
|
|
|
2022-04-28 07:23:40 +00:00
|
|
|
def test_colo_optimizer():
|
|
|
|
get_components_func = non_distributed_component_funcs.get_callable('simple_net')
|
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
|
|
|
set_seed(1)
|
2022-11-09 03:50:33 +00:00
|
|
|
with ColoInitContext(device=get_current_device()):
|
2022-04-28 07:23:40 +00:00
|
|
|
model = model_builder(checkpoint=True)
|
|
|
|
|
2022-07-14 08:57:48 +00:00
|
|
|
colo_optimizer = ColossalaiOptimizer(torch.optim.SGD(model.parameters(), lr=0.1))
|
2022-04-28 07:23:40 +00:00
|
|
|
for i, (data, label) in enumerate(train_dataloader):
|
|
|
|
colo_optimizer.zero_grad()
|
|
|
|
data = data.to(get_current_device())
|
|
|
|
label = label.to(get_current_device())
|
|
|
|
|
|
|
|
# Bcast rank0 data to all processes
|
|
|
|
if criterion:
|
|
|
|
output = model(data)
|
|
|
|
loss = criterion(output, label)
|
|
|
|
else:
|
|
|
|
output = model(data, label)
|
|
|
|
loss = output
|
|
|
|
|
|
|
|
loss.backward()
|
|
|
|
colo_optimizer.step()
|
|
|
|
|
|
|
|
if i > 5:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2022-05-06 09:07:56 +00:00
|
|
|
def run_1d_row_tp(model_name: str):
|
2022-04-24 08:43:44 +00:00
|
|
|
# A simple net with two stacked nn.Linear
|
2022-05-06 09:07:56 +00:00
|
|
|
get_components_func = non_distributed_component_funcs.get_callable(model_name)
|
2022-04-24 08:43:44 +00:00
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
2022-06-29 06:08:40 +00:00
|
|
|
rank = torch.distributed.get_rank()
|
2022-04-25 08:01:52 +00:00
|
|
|
|
2022-04-27 04:00:18 +00:00
|
|
|
set_seed(1)
|
2022-04-25 08:01:52 +00:00
|
|
|
with ColoInitContext(device=get_current_device()):
|
2022-04-24 08:43:44 +00:00
|
|
|
model = model_builder(checkpoint=True)
|
|
|
|
|
2022-06-29 06:08:40 +00:00
|
|
|
world_size = torch.distributed.get_world_size()
|
2022-07-06 08:15:16 +00:00
|
|
|
pg = ProcessGroup(tp_degree=world_size)
|
2022-06-29 02:03:09 +00:00
|
|
|
|
2022-05-06 09:07:56 +00:00
|
|
|
set_seed(1)
|
|
|
|
if rank == 0:
|
|
|
|
model_torch = model_builder(checkpoint=True)
|
|
|
|
model_torch = model_torch.cuda()
|
2022-07-12 15:26:45 +00:00
|
|
|
|
2022-04-26 10:11:47 +00:00
|
|
|
# A naive way to set spec for all weights in Linear
|
2022-07-12 15:26:45 +00:00
|
|
|
for mo_name, module in model.named_modules():
|
|
|
|
# print(mo_name)
|
|
|
|
for pa_name, param in module.named_parameters(recurse=False):
|
|
|
|
# print('\t', pa_name, param.shape)
|
|
|
|
if not isinstance(param, ColoTensor):
|
|
|
|
continue
|
|
|
|
if 'weight' in pa_name:
|
|
|
|
if 'embed' in mo_name and 'token' not in mo_name and 'LayerNorm' not in mo_name:
|
|
|
|
split_param_row_tp1d(param, pg)
|
|
|
|
elif 'LayerNorm' not in mo_name and 'ln' not in mo_name:
|
|
|
|
split_param_col_tp1d(param, pg)
|
2022-04-26 10:11:47 +00:00
|
|
|
|
2022-04-27 04:00:18 +00:00
|
|
|
model = model.cuda()
|
2022-04-24 08:43:44 +00:00
|
|
|
|
|
|
|
for i, (data, label) in enumerate(train_dataloader):
|
2022-04-26 10:11:47 +00:00
|
|
|
data = data.to(get_current_device())
|
|
|
|
label = label.to(get_current_device())
|
2022-04-25 08:01:52 +00:00
|
|
|
|
2022-06-29 06:08:40 +00:00
|
|
|
torch.distributed.broadcast(data, 0, group=pg.tp_process_group())
|
|
|
|
torch.distributed.broadcast(label, 0, group=pg.tp_process_group())
|
2022-04-27 04:00:18 +00:00
|
|
|
|
|
|
|
# Bcast rank0 data to all processes
|
2022-04-24 08:43:44 +00:00
|
|
|
if criterion:
|
2022-04-26 10:11:47 +00:00
|
|
|
output = model(data)
|
2022-04-24 08:43:44 +00:00
|
|
|
loss = criterion(output, label)
|
|
|
|
else:
|
2022-04-26 10:11:47 +00:00
|
|
|
output = model(data, label)
|
2022-04-24 08:43:44 +00:00
|
|
|
loss = output
|
|
|
|
|
2022-04-27 04:00:18 +00:00
|
|
|
# For reference
|
|
|
|
if rank == 0:
|
|
|
|
if criterion:
|
|
|
|
output_torch = model_torch(data)
|
|
|
|
loss_torch = criterion(output_torch, label)
|
|
|
|
else:
|
|
|
|
output_torch = model_torch(data, label)
|
|
|
|
loss_torch = output_torch
|
2022-05-19 04:44:59 +00:00
|
|
|
assert torch.allclose(loss, loss_torch, rtol=1e-2)
|
2022-07-15 10:19:52 +00:00
|
|
|
torch.distributed.barrier()
|
2022-04-27 04:00:18 +00:00
|
|
|
|
2022-04-24 08:43:44 +00:00
|
|
|
loss.backward()
|
|
|
|
|
2022-04-27 04:00:18 +00:00
|
|
|
if rank == 0:
|
|
|
|
loss_torch.backward()
|
2022-07-15 10:19:52 +00:00
|
|
|
torch.distributed.barrier()
|
|
|
|
|
2022-04-24 08:43:44 +00:00
|
|
|
if i > 5:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2022-05-09 09:07:35 +00:00
|
|
|
def _run_pretrain_load():
|
|
|
|
from transformers import BertForMaskedLM
|
|
|
|
set_seed(1)
|
|
|
|
model_pretrained = BertForMaskedLM.from_pretrained('bert-base-uncased')
|
2022-11-09 03:50:33 +00:00
|
|
|
with ColoInitContext(device=get_current_device()):
|
2022-05-09 09:07:35 +00:00
|
|
|
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
|
|
|
|
|
|
|
|
model_pretrained = model_pretrained.cuda()
|
|
|
|
model = model.cuda()
|
|
|
|
|
|
|
|
dict_pretrained = {}
|
|
|
|
dict_col = {}
|
2022-05-11 07:48:12 +00:00
|
|
|
c_ref = 0
|
2022-05-09 09:07:35 +00:00
|
|
|
for name, param in model_pretrained.named_parameters():
|
|
|
|
dict_pretrained[name] = param
|
2022-05-11 07:48:12 +00:00
|
|
|
c_ref += 1
|
2022-05-11 02:54:19 +00:00
|
|
|
c1 = 0
|
|
|
|
c2 = 0
|
2022-05-19 04:44:59 +00:00
|
|
|
for name, param in model.named_parameters():
|
2022-05-11 02:54:19 +00:00
|
|
|
if isinstance(param, ColoParameter):
|
2022-05-11 07:48:12 +00:00
|
|
|
c1 += 1
|
2022-05-11 02:54:19 +00:00
|
|
|
else:
|
2022-05-16 06:58:08 +00:00
|
|
|
c2 += 1
|
2022-05-09 09:07:35 +00:00
|
|
|
dict_col[name] = param
|
2022-05-11 07:48:12 +00:00
|
|
|
assert c_ref == c1
|
|
|
|
assert c2 == 0
|
|
|
|
if model_pretrained.cls.predictions.decoder.bias is model_pretrained.cls.predictions.bias:
|
|
|
|
assert model.cls.predictions.decoder.bias is model.cls.predictions.bias
|
2022-05-09 09:07:35 +00:00
|
|
|
|
|
|
|
for name, param in dict_pretrained.items():
|
|
|
|
check_equal(param, dict_col[name])
|
|
|
|
|
|
|
|
|
|
|
|
def run_model_dist(rank, world_size, port):
|
2022-07-06 08:15:16 +00:00
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
2022-07-15 10:19:52 +00:00
|
|
|
# Comment below test for speed consideration
|
|
|
|
# for name in ['bert', 'simple_net']:
|
|
|
|
# run_1d_row_tp(name)
|
2022-07-13 15:06:12 +00:00
|
|
|
for name in ['bert', 'simple_net']:
|
2022-05-06 10:06:22 +00:00
|
|
|
run_1d_hybrid_tp(name)
|
2022-04-24 08:43:44 +00:00
|
|
|
|
2022-05-16 06:58:08 +00:00
|
|
|
|
2022-05-06 07:03:43 +00:00
|
|
|
@pytest.mark.dist
|
2022-05-06 10:06:22 +00:00
|
|
|
@pytest.mark.parametrize('world_size', [1, 4])
|
2022-05-06 07:03:43 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-05-06 09:07:56 +00:00
|
|
|
def test_model(world_size):
|
2022-05-09 09:07:35 +00:00
|
|
|
run_func = partial(run_model_dist, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
|
|
|
def run_pretrain_load_dist(rank, world_size, port):
|
2022-07-06 08:15:16 +00:00
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
2022-05-09 09:07:35 +00:00
|
|
|
_run_pretrain_load()
|
|
|
|
|
|
|
|
|
|
|
|
# The test case has to download huggingface pretrained models from the internet
|
|
|
|
# So we manually trigger the test.
|
2022-06-29 02:03:09 +00:00
|
|
|
@pytest.mark.skip
|
2022-05-09 09:07:35 +00:00
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize('world_size', [1, 4])
|
|
|
|
@rerun_if_address_is_in_use()
|
2022-06-29 02:03:09 +00:00
|
|
|
def test_pretrain_load(world_size):
|
2022-05-09 09:07:35 +00:00
|
|
|
run_func = partial(run_pretrain_load_dist, world_size=world_size, port=free_port())
|
2022-05-06 07:03:43 +00:00
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
2022-04-24 08:43:44 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-05-06 07:03:43 +00:00
|
|
|
# test_model_parameters()
|
2022-07-08 06:18:30 +00:00
|
|
|
# test_colo_optgimizer()
|
2022-07-06 08:15:16 +00:00
|
|
|
test_model(4)
|
|
|
|
# test_pretrain_load(4)
|