2023-01-04 03:59:56 +00:00
|
|
|
import copy
|
|
|
|
|
|
|
|
import pytest
|
2022-11-10 08:40:26 +00:00
|
|
|
import torch
|
2023-01-04 03:59:56 +00:00
|
|
|
|
|
|
|
import colossalai
|
2023-09-18 08:31:06 +00:00
|
|
|
from colossalai.legacy.amp import convert_to_apex_amp, convert_to_torch_amp
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import assert_close_loose, clear_cache_before_run, rerun_if_address_is_in_use, spawn
|
2023-10-20 02:35:08 +00:00
|
|
|
from tests.kit.model_zoo import model_zoo
|
2022-11-10 08:40:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_torch_amp():
|
|
|
|
"""
|
|
|
|
In this test, we compare the torch amp and apex amp implemented in colossalai
|
|
|
|
"""
|
|
|
|
|
|
|
|
torch.backends.cudnn.benchmark = False
|
|
|
|
torch.backends.cudnn.deterministic = True
|
|
|
|
|
|
|
|
# create layer
|
2023-10-20 02:35:08 +00:00
|
|
|
test_models = ["torchvision_resnet18", "custom_simple_net"]
|
2022-11-10 08:40:26 +00:00
|
|
|
for test_name in test_models:
|
2023-10-20 02:35:08 +00:00
|
|
|
model_builder, data_gen_fn, *_ = next(iter(model_zoo.get_sub_registry(test_name).values()))
|
2022-11-10 08:40:26 +00:00
|
|
|
|
|
|
|
# create model
|
2023-10-20 02:35:08 +00:00
|
|
|
torch_amp_model = model_builder().cuda()
|
2022-11-10 08:40:26 +00:00
|
|
|
apex_amp_model = copy.deepcopy(torch_amp_model)
|
|
|
|
|
|
|
|
# create optimizer
|
2023-01-04 03:59:56 +00:00
|
|
|
# we use SGD here, since the correctness of gradient clipping can't be tested with Adam
|
|
|
|
torch_amp_optimizer = torch.optim.SGD(torch_amp_model.parameters(), lr=1e-3)
|
|
|
|
apex_amp_optimizer = torch.optim.SGD(apex_amp_model.parameters(), lr=1e-3)
|
2022-11-10 08:40:26 +00:00
|
|
|
|
|
|
|
# inject torch and apex amp
|
2023-01-04 03:59:56 +00:00
|
|
|
torch_amp_config = dict(init_scale=128, enabled=True)
|
2023-09-19 06:20:26 +00:00
|
|
|
torch_amp_model, torch_amp_optimizer, _ = convert_to_torch_amp(
|
|
|
|
torch_amp_model, torch_amp_optimizer, amp_config=torch_amp_config
|
|
|
|
)
|
|
|
|
apex_amp_config = dict(opt_level="O1", loss_scale=128)
|
2022-11-10 08:40:26 +00:00
|
|
|
apex_amp_model, apex_amp_optimizer = convert_to_apex_amp(apex_amp_model, apex_amp_optimizer, apex_amp_config)
|
|
|
|
|
|
|
|
# create data
|
2023-10-20 02:35:08 +00:00
|
|
|
data = data_gen_fn()
|
|
|
|
data = {k: v.cuda() if isinstance(v, torch.Tensor) else v for k, v in data.items()}
|
2022-11-10 08:40:26 +00:00
|
|
|
|
|
|
|
# forward pass
|
2023-10-20 02:35:08 +00:00
|
|
|
torch_amp_output = torch_amp_model(**data)
|
|
|
|
apex_amp_output = apex_amp_model(**data)
|
2022-11-10 08:40:26 +00:00
|
|
|
assert_close_loose(torch_amp_output, apex_amp_output)
|
|
|
|
|
|
|
|
for torch_amp_param, apex_amp_param in zip(torch_amp_model.parameters(), apex_amp_model.parameters()):
|
|
|
|
assert_close_loose(torch_amp_param, apex_amp_param)
|
|
|
|
|
|
|
|
# backward
|
2023-01-04 03:59:56 +00:00
|
|
|
# use sum() to get big gradient
|
|
|
|
torch_amp_optimizer.backward(torch_amp_output.sum())
|
|
|
|
apex_amp_optimizer.backward(apex_amp_output.sum())
|
2022-11-10 08:40:26 +00:00
|
|
|
|
|
|
|
# check grad
|
|
|
|
# In apex amp, grad is not scaled before backward, but torch amp does
|
|
|
|
for torch_amp_param, apex_amp_param in zip(torch_amp_model.parameters(), apex_amp_model.parameters()):
|
2023-09-19 06:20:26 +00:00
|
|
|
assert_close_loose(torch_amp_param.grad, apex_amp_param.grad * apex_amp_config["loss_scale"])
|
2022-11-10 08:40:26 +00:00
|
|
|
|
2023-01-04 03:59:56 +00:00
|
|
|
# clip gradient
|
|
|
|
apex_amp_optimizer.clip_grad_norm(model=apex_amp_model, max_norm=1.0)
|
|
|
|
torch_amp_optimizer.clip_grad_norm(model=torch_amp_model, max_norm=1.0)
|
|
|
|
|
2022-11-10 08:40:26 +00:00
|
|
|
# step
|
|
|
|
torch_amp_optimizer.step()
|
|
|
|
apex_amp_optimizer.step()
|
|
|
|
|
|
|
|
# check updated param and grad
|
|
|
|
for torch_amp_param, apex_amp_param in zip(torch_amp_model.parameters(), apex_amp_model.parameters()):
|
|
|
|
assert_close_loose(torch_amp_param.grad, apex_amp_param.grad)
|
|
|
|
assert_close_loose(torch_amp_param, apex_amp_param)
|
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
2023-09-19 06:20:26 +00:00
|
|
|
colossalai.legacy.launch(config=dict(), rank=rank, world_size=world_size, port=port, host="localhost")
|
2022-11-10 08:40:26 +00:00
|
|
|
run_torch_amp()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@rerun_if_address_is_in_use()
|
2023-04-06 06:51:35 +00:00
|
|
|
@clear_cache_before_run()
|
2022-11-10 08:40:26 +00:00
|
|
|
def test_torch_amp():
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(run_dist, 1)
|
2022-11-10 08:40:26 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2022-11-10 08:40:26 +00:00
|
|
|
test_torch_amp()
|