2022-03-15 02:05:38 +00:00
|
|
|
import torch
|
2022-03-23 10:03:39 +00:00
|
|
|
import colossalai
|
2022-03-25 09:25:12 +00:00
|
|
|
import torch.multiprocessing as mp
|
|
|
|
from colossalai.amp import convert_to_naive_amp, convert_to_apex_amp
|
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
2022-04-14 16:33:04 +00:00
|
|
|
from colossalai.testing import assert_close_loose, rerun_if_address_is_in_use
|
2022-03-15 02:05:38 +00:00
|
|
|
from colossalai.utils import free_port
|
2022-03-23 10:03:39 +00:00
|
|
|
from colossalai.amp import convert_to_naive_amp, convert_to_apex_amp
|
|
|
|
|
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
|
|
|
|
|
|
|
import copy
|
|
|
|
import pytest
|
2022-03-15 02:05:38 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
|
|
|
|
def check_equal(a, b):
|
|
|
|
"""
|
|
|
|
This function checks if two tensors are equal within tolerance
|
|
|
|
"""
|
|
|
|
assert torch.allclose(a.float(), b.float(), rtol=1e-4, atol=1e-3), f'a = {a}, b = {b}'
|
|
|
|
|
|
|
|
|
|
|
|
def run_naive_amp():
|
|
|
|
"""
|
|
|
|
In this test, we compare the naive fp16 optimizer implemented in colossalai
|
|
|
|
and fp32 torch optimizer
|
|
|
|
"""
|
|
|
|
|
2022-03-18 08:28:16 +00:00
|
|
|
torch.backends.cudnn.benchmark = False
|
|
|
|
torch.backends.cudnn.deterministic = True
|
|
|
|
|
2022-03-15 02:05:38 +00:00
|
|
|
# create layer
|
2022-03-18 08:28:16 +00:00
|
|
|
test_models = ['repeated_computed_layers', 'nested_model', 'resnet18']
|
2022-03-15 02:05:38 +00:00
|
|
|
for test_name in test_models:
|
|
|
|
get_component_func = non_distributed_component_funcs.get_callable(test_name)
|
2022-03-15 03:29:46 +00:00
|
|
|
model_builder, train_dataloader, _, optim_class, _ = get_component_func()
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
# create model
|
2022-03-18 08:28:16 +00:00
|
|
|
naive_amp_model = model_builder(checkpoint=True).cuda()
|
|
|
|
apex_amp_model = copy.deepcopy(naive_amp_model)
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
# create optimizer
|
2022-03-18 08:28:16 +00:00
|
|
|
naive_amp_optimizer = optim_class(naive_amp_model.parameters(), lr=1e-3)
|
|
|
|
apex_amp_optimizer = optim_class(apex_amp_model.parameters(), lr=1e-3)
|
2022-03-15 02:05:38 +00:00
|
|
|
|
2022-03-18 08:28:16 +00:00
|
|
|
# inject naive and apex amp
|
|
|
|
naive_amp_config = dict(initial_scale=128)
|
|
|
|
naive_amp_model, naive_amp_optimizer = convert_to_naive_amp(naive_amp_model, naive_amp_optimizer,
|
|
|
|
naive_amp_config)
|
|
|
|
apex_amp_config = dict(opt_level='O2', loss_scale=128, keep_batchnorm_fp32=False)
|
|
|
|
apex_amp_model, apex_amp_optimizer = convert_to_apex_amp(apex_amp_model, apex_amp_optimizer, apex_amp_config)
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
# create data
|
|
|
|
data_iter = iter(train_dataloader)
|
|
|
|
data, label = next(data_iter)
|
|
|
|
data = data.cuda()
|
|
|
|
|
|
|
|
# forward pass
|
2022-03-18 08:28:16 +00:00
|
|
|
naive_amp_output = naive_amp_model(data)
|
|
|
|
apex_amp_output = apex_amp_model(data)
|
|
|
|
assert_close_loose(naive_amp_output, apex_amp_output)
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
# backward
|
2022-03-18 08:28:16 +00:00
|
|
|
naive_amp_optimizer.backward(naive_amp_output.mean())
|
|
|
|
apex_amp_optimizer.backward(apex_amp_output.mean())
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
# check grad
|
2022-03-18 08:28:16 +00:00
|
|
|
for naive_amp_param, apex_amp_param in zip(naive_amp_model.parameters(), apex_amp_model.parameters()):
|
|
|
|
assert_close_loose(naive_amp_param.grad, apex_amp_param.grad)
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
# step
|
2022-03-18 08:28:16 +00:00
|
|
|
naive_amp_optimizer.step()
|
|
|
|
apex_amp_optimizer.step()
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
# check updated param
|
2022-03-18 08:28:16 +00:00
|
|
|
for naive_amp_param, apex_amp_param in zip(naive_amp_model.parameters(), apex_amp_model.parameters()):
|
|
|
|
assert_close_loose(naive_amp_param, apex_amp_param)
|
2022-03-15 02:05:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
colossalai.launch(config=dict(), rank=rank, world_size=world_size, port=port, host='localhost')
|
|
|
|
run_naive_amp()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
2022-04-14 16:33:04 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-03-15 02:05:38 +00:00
|
|
|
def test_naive_amp():
|
|
|
|
world_size = 1
|
|
|
|
run_func = partial(run_dist, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_naive_amp()
|