2022-03-08 02:19:18 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
import colossalai
|
|
|
|
import pytest
|
|
|
|
import torch.multiprocessing as mp
|
|
|
|
from colossalai.amp import AMP_TYPE
|
2022-03-09 06:18:23 +00:00
|
|
|
from colossalai.context import Config
|
2022-03-08 02:19:18 +00:00
|
|
|
from colossalai.core import global_context as gpc
|
|
|
|
from colossalai.utils import free_port
|
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
2022-03-17 07:44:17 +00:00
|
|
|
from colossalai.testing import parameterize
|
2022-03-08 02:19:18 +00:00
|
|
|
|
|
|
|
CONFIG = dict(parallel=dict(pipeline=dict(size=1), tensor=dict(size=1, mode=None)),
|
|
|
|
fp16=dict(mode=None),
|
|
|
|
clip_grad_norm=1.0)
|
|
|
|
|
|
|
|
|
2022-03-17 07:44:17 +00:00
|
|
|
@parameterize('model_name', ['repeated_computed_layers', 'resnet18', 'repeated_computed_layers'])
|
|
|
|
@parameterize('amp_mode', [AMP_TYPE.APEX, AMP_TYPE.TORCH, AMP_TYPE.NAIVE, None])
|
|
|
|
def run_train(model_name, amp_mode):
|
2022-03-09 06:18:23 +00:00
|
|
|
# FIXME: test bert
|
2022-03-17 07:44:17 +00:00
|
|
|
get_components_func = non_distributed_component_funcs.get_callable(model_name)
|
|
|
|
gpc.config.fp16['mode'] = amp_mode
|
|
|
|
model_builder, train_dataloader, _, optimizer_class, criterion = get_components_func()
|
|
|
|
|
|
|
|
model = model_builder(checkpoint=False)
|
|
|
|
engine, train_dataloader, *args = colossalai.initialize(model=model,
|
|
|
|
optimizer=optimizer_class(model.parameters(), lr=1e-3),
|
|
|
|
criterion=criterion,
|
|
|
|
train_dataloader=train_dataloader)
|
|
|
|
|
|
|
|
try:
|
|
|
|
engine.train()
|
|
|
|
for data, label in train_dataloader:
|
|
|
|
engine.zero_grad()
|
|
|
|
data = data.cuda()
|
|
|
|
label = label.cuda()
|
|
|
|
if criterion:
|
|
|
|
output = engine(data)
|
|
|
|
loss = engine.criterion(output, label)
|
|
|
|
else:
|
|
|
|
loss = engine(data, label)
|
|
|
|
engine.backward(loss)
|
|
|
|
engine.step()
|
|
|
|
break
|
|
|
|
except IndexError:
|
|
|
|
# if using apex amp, NetWithRepeatedlyComputedLayers will raise an index out of range issue
|
|
|
|
# the following check fails in apex
|
|
|
|
# if cached_x.grad_fn.next_functions[1][0].variable is not x:
|
|
|
|
pass
|
2022-03-08 02:19:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_engine(rank, world_size, port):
|
|
|
|
# init dist env
|
2022-03-17 07:44:17 +00:00
|
|
|
colossalai.launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
run_train()
|
2022-03-08 02:19:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
def test_engine():
|
2022-03-14 12:48:41 +00:00
|
|
|
world_size = 2
|
2022-03-08 02:19:18 +00:00
|
|
|
run_func = partial(run_engine, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_engine()
|