2022-11-11 01:26:40 +00:00
|
|
|
import copy
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
2023-06-30 07:30:50 +00:00
|
|
|
import torch.distributed as dist
|
2022-11-11 01:26:40 +00:00
|
|
|
import torch.nn as nn
|
|
|
|
from torch.nn.parallel import DistributedDataParallel as DDP
|
2022-11-29 05:00:30 +00:00
|
|
|
from torch.testing import assert_close
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
import colossalai
|
2023-06-05 07:58:31 +00:00
|
|
|
from colossalai.testing import parameterize, rerun_if_address_is_in_use, spawn
|
2022-11-29 05:00:30 +00:00
|
|
|
from colossalai.testing.random import seed_all
|
2022-11-11 01:26:40 +00:00
|
|
|
from colossalai.zero import LowLevelZeroOptimizer
|
|
|
|
|
|
|
|
|
2023-01-29 07:09:57 +00:00
|
|
|
class MlpModel(nn.Module):
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2022-11-29 05:00:30 +00:00
|
|
|
def __init__(self):
|
2023-01-29 07:09:57 +00:00
|
|
|
super(MlpModel, self).__init__()
|
2023-06-30 07:30:50 +00:00
|
|
|
self.linear1 = nn.Linear(123, 253)
|
|
|
|
self.linear_drop = nn.Linear(253, 253)
|
|
|
|
self.linear2 = nn.Linear(253, 512)
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
x = self.linear1(x)
|
|
|
|
x = self.linear2(x)
|
|
|
|
return x
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
|
2023-06-05 07:58:31 +00:00
|
|
|
def loose_close(a, b, dtype: torch.dtype = torch.float32):
|
2022-11-29 05:00:30 +00:00
|
|
|
rtol = None
|
|
|
|
atol = None
|
2023-06-05 07:58:31 +00:00
|
|
|
if dtype is torch.float16:
|
2022-11-29 05:00:30 +00:00
|
|
|
rtol = 5e-2
|
|
|
|
atol = 5e-4
|
2023-06-05 07:58:31 +00:00
|
|
|
elif dtype is torch.bfloat16:
|
|
|
|
rtol = 4e-3
|
|
|
|
atol = 4e-3
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2023-06-05 07:58:31 +00:00
|
|
|
a = a.detach().to(dtype)
|
|
|
|
b = b.detach().to(dtype)
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
assert_close(a, b, rtol=rtol, atol=atol)
|
|
|
|
|
|
|
|
|
2023-06-30 07:30:50 +00:00
|
|
|
def split_ddp_grad(grad, world_size):
|
|
|
|
with torch.no_grad():
|
|
|
|
grad = grad.clone().detach().flatten()
|
|
|
|
padding_size = (world_size - grad.numel() % world_size) % world_size
|
|
|
|
if padding_size > 0:
|
|
|
|
grad = torch.nn.functional.pad(grad, [0, padding_size])
|
|
|
|
splited_grad = grad.split(grad.numel() // world_size)
|
|
|
|
return splited_grad
|
|
|
|
|
|
|
|
|
2022-11-29 05:00:30 +00:00
|
|
|
def exam_zero_1_2():
|
2022-11-11 01:26:40 +00:00
|
|
|
"""
|
|
|
|
In this test, we want to test whether zero stage 1 and 2
|
|
|
|
deliver the same numerical results despite different communication
|
|
|
|
pattern
|
|
|
|
|
|
|
|
we use these prefixes to differentiate the zero stage
|
|
|
|
oss: partition optimizer states
|
|
|
|
pg: partition gradients and optimizer states
|
|
|
|
|
|
|
|
"""
|
2022-11-29 05:00:30 +00:00
|
|
|
local_rank = torch.distributed.get_rank()
|
|
|
|
seed_all(2001)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# create model
|
2023-01-29 07:09:57 +00:00
|
|
|
zero1_model = MlpModel().cuda()
|
2022-11-29 05:00:30 +00:00
|
|
|
zero2_model = copy.deepcopy(zero1_model)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# create optimizer
|
2022-11-29 05:00:30 +00:00
|
|
|
zero1_optimizer = torch.optim.Adam(zero1_model.parameters(), lr=1)
|
|
|
|
zero2_optimizer = torch.optim.Adam(zero2_model.parameters(), lr=1)
|
|
|
|
zero1_optimizer = LowLevelZeroOptimizer(zero1_optimizer,
|
|
|
|
overlap_communication=True,
|
|
|
|
initial_scale=128,
|
|
|
|
verbose=True)
|
|
|
|
zero2_optimizer = LowLevelZeroOptimizer(zero2_optimizer,
|
|
|
|
overlap_communication=True,
|
|
|
|
partition_grad=True,
|
|
|
|
initial_scale=128)
|
|
|
|
# create data
|
|
|
|
seed_all(2001 + local_rank)
|
2023-06-30 07:30:50 +00:00
|
|
|
input_data = torch.randn(32, 123).cuda()
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
zero1_output = zero1_model(input_data)
|
|
|
|
zero2_output = zero2_model(input_data)
|
|
|
|
assert torch.equal(zero1_output, zero2_output)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2022-11-29 05:00:30 +00:00
|
|
|
# zero-dp backward
|
2023-06-30 07:30:50 +00:00
|
|
|
zero1_optimizer.backward(zero1_output.mean().float())
|
|
|
|
zero2_optimizer.backward(zero2_output.mean().float())
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2023-06-30 07:30:50 +00:00
|
|
|
# check grad
|
|
|
|
z1g_list = zero1_optimizer._grad_store.get_working_grads_by_group_id(0)
|
|
|
|
z2g_list = zero2_optimizer._grad_store.get_working_grads_by_group_id(0)
|
|
|
|
for z1g, z2g in zip(z1g_list, z2g_list):
|
|
|
|
assert torch.equal(z1g, z2g)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# step
|
2022-11-29 05:00:30 +00:00
|
|
|
zero1_optimizer.step()
|
|
|
|
zero2_optimizer.step()
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# check updated param
|
2022-11-29 05:00:30 +00:00
|
|
|
for z1p, z2p in zip(zero1_model.parameters(), zero2_model.parameters()):
|
|
|
|
assert torch.equal(z1p.data, z2p.data)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
|
2023-06-05 07:58:31 +00:00
|
|
|
@parameterize('dtype', [torch.float16, torch.bfloat16])
|
2023-06-30 07:30:50 +00:00
|
|
|
def exam_zero_1_torch_ddp(world_size, dtype: torch.dtype):
|
2022-11-11 01:26:40 +00:00
|
|
|
"""
|
|
|
|
In this test, two pairs of model and optimizers are created.
|
|
|
|
1. zero: use sharded optimizer and fp16 parameters
|
|
|
|
2. torch: use torch DDP and fp32 parameters
|
|
|
|
|
|
|
|
We feed these two sets of models with the same input and check if the
|
|
|
|
differences in model output and updated parameters are within tolerance.
|
|
|
|
"""
|
2022-11-29 05:00:30 +00:00
|
|
|
local_rank = torch.distributed.get_rank()
|
|
|
|
seed_all(1453)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2022-11-29 05:00:30 +00:00
|
|
|
# create models
|
2023-06-05 07:58:31 +00:00
|
|
|
torch_model = MlpModel().cuda()
|
|
|
|
zero_model = copy.deepcopy(torch_model).to(dtype)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2023-06-30 07:30:50 +00:00
|
|
|
torch_model = DDP(torch_model.cuda(), static_graph=True).cuda()
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# create optimizer
|
2022-11-29 05:00:30 +00:00
|
|
|
zero_optimizer = torch.optim.SGD(zero_model.parameters(), lr=1)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# we only test stage 1 here
|
|
|
|
# in `check_sharded_param_consistency.py`, we will test whether
|
|
|
|
# level 1 and 2 will produce exactly the same results
|
|
|
|
zero_optimizer = LowLevelZeroOptimizer(zero_optimizer,
|
|
|
|
overlap_communication=True,
|
|
|
|
initial_scale=1,
|
2023-08-11 07:09:24 +00:00
|
|
|
reduce_bucket_size=1024 * 1024)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2022-11-29 05:00:30 +00:00
|
|
|
torch_optimizer = torch.optim.SGD(torch_model.parameters(), lr=1)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
2022-11-29 05:00:30 +00:00
|
|
|
seed_all(1453 + local_rank)
|
2022-11-11 01:26:40 +00:00
|
|
|
# create
|
2023-06-30 07:30:50 +00:00
|
|
|
input_data = torch.rand(32, 123).cuda()
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# zero-dp forward
|
2023-06-05 07:58:31 +00:00
|
|
|
zero_output = zero_model(input_data.to(dtype))
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# torch-ddp forward
|
|
|
|
torch_output = torch_model(input_data)
|
2023-06-05 07:58:31 +00:00
|
|
|
loose_close(zero_output, torch_output, dtype=dtype)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# zero-dp backward
|
2023-06-30 07:30:50 +00:00
|
|
|
zero_optimizer.backward(zero_output.mean().float())
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# torch-ddp backward
|
|
|
|
torch_output.mean().backward()
|
|
|
|
|
|
|
|
# check grad
|
2022-11-29 05:00:30 +00:00
|
|
|
for (n, p), z1p in zip(torch_model.named_parameters(), zero_model.parameters()):
|
2023-06-30 07:30:50 +00:00
|
|
|
if p.grad is not None:
|
|
|
|
zero_grad_list = zero_optimizer._grad_store.get_partitioned_gradients_by_param_id(0, id(z1p))
|
|
|
|
torch_grad_list = split_ddp_grad(p.grad, world_size)
|
|
|
|
for zero_grad, torch_grad in zip(zero_grad_list, torch_grad_list):
|
|
|
|
loose_close(zero_grad, torch_grad, dtype=dtype)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
# zero-dp step
|
|
|
|
zero_optimizer.step()
|
|
|
|
|
|
|
|
# torch ddp step
|
|
|
|
torch_optimizer.step()
|
|
|
|
|
|
|
|
# check updated param
|
2022-11-29 05:00:30 +00:00
|
|
|
for (n, p), z1p in zip(torch_model.named_parameters(), zero_model.parameters()):
|
2023-06-05 07:58:31 +00:00
|
|
|
loose_close(p.data, z1p.data, dtype=dtype)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
colossalai.launch(config=dict(), rank=rank, world_size=world_size, port=port, host='localhost')
|
|
|
|
|
2023-06-30 07:30:50 +00:00
|
|
|
exam_zero_1_torch_ddp(world_size=world_size)
|
2022-11-29 05:00:30 +00:00
|
|
|
exam_zero_1_2()
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
2023-04-03 09:12:22 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-11-29 05:00:30 +00:00
|
|
|
def test_zero_1_2():
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(run_dist, 2)
|
2022-11-11 01:26:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-11-29 05:00:30 +00:00
|
|
|
test_zero_1_2()
|