2022-11-29 05:00:30 +00:00
|
|
|
import copy
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
|
|
|
from torch.nn.parallel import DistributedDataParallel as DDP
|
|
|
|
from torch.testing import assert_close
|
|
|
|
|
|
|
|
import colossalai
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import spawn
|
2022-11-29 05:00:30 +00:00
|
|
|
from colossalai.testing.random import seed_all
|
2023-07-04 04:00:33 +00:00
|
|
|
from colossalai.utils import conditional_context
|
2022-11-29 05:00:30 +00:00
|
|
|
from colossalai.zero import LowLevelZeroOptimizer
|
|
|
|
|
|
|
|
|
2023-01-29 07:09:57 +00:00
|
|
|
class MlpModel(nn.Module):
|
2022-11-29 05:00:30 +00:00
|
|
|
def __init__(self):
|
2023-01-29 07:09:57 +00:00
|
|
|
super(MlpModel, self).__init__()
|
2022-11-29 05:00:30 +00:00
|
|
|
self.linear1 = nn.Linear(128, 256)
|
|
|
|
self.linear2 = nn.Linear(256, 512)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
x = self.linear1(x)
|
|
|
|
x = self.linear2(x)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
def exam_zero_1_2_grad_acc():
|
|
|
|
local_rank = torch.distributed.get_rank()
|
|
|
|
seed_all(2009)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
# create optimizer
|
|
|
|
zero1_optimizer = torch.optim.Adam(zero1_model.parameters(), lr=1)
|
|
|
|
zero2_optimizer = torch.optim.Adam(zero2_model.parameters(), lr=1)
|
2023-09-19 06:20:26 +00:00
|
|
|
zero1_optimizer = LowLevelZeroOptimizer(
|
|
|
|
zero1_optimizer, overlap_communication=True, initial_scale=32, clip_grad_norm=1.0, verbose=True
|
|
|
|
)
|
|
|
|
zero2_optimizer = LowLevelZeroOptimizer(
|
|
|
|
zero2_optimizer, overlap_communication=True, partition_grad=True, initial_scale=32, clip_grad_norm=1.0
|
|
|
|
)
|
2022-11-29 05:00:30 +00:00
|
|
|
# create data
|
|
|
|
seed_all(2021 + local_rank)
|
|
|
|
input_data1 = torch.randn(32, 128).cuda()
|
|
|
|
input_data2 = torch.randn(32, 128).cuda()
|
|
|
|
|
2023-06-30 07:30:50 +00:00
|
|
|
def fwd_bwd_func(number, cur_data, check_flag):
|
2022-11-29 05:00:30 +00:00
|
|
|
# zero-dp forward
|
|
|
|
zero1_output = zero1_model(cur_data)
|
|
|
|
zero2_output = zero2_model(cur_data)
|
|
|
|
assert torch.equal(zero1_output, zero2_output)
|
|
|
|
|
|
|
|
# zero-dp backward
|
2023-08-25 05:44:07 +00:00
|
|
|
zero1_optimizer.backward(zero1_output.sum().float())
|
|
|
|
zero2_optimizer.backward(zero2_output.sum().float())
|
2022-11-29 05:00:30 +00:00
|
|
|
|
2023-06-30 07:30:50 +00:00
|
|
|
fwd_bwd_func(0, input_data1, True)
|
|
|
|
fwd_bwd_func(1, input_data2, False)
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
# step
|
|
|
|
zero1_optimizer.step()
|
|
|
|
zero2_optimizer.step()
|
|
|
|
|
|
|
|
# check updated param
|
|
|
|
for z1p, z2p in zip(zero1_model.parameters(), zero2_model.parameters()):
|
|
|
|
assert torch.equal(z1p.data, z2p.data)
|
|
|
|
|
|
|
|
|
2023-08-25 05:44:07 +00:00
|
|
|
def exam_zero_1_grad_acc(sync):
|
2022-11-29 05:00:30 +00:00
|
|
|
local_rank = torch.distributed.get_rank()
|
|
|
|
seed_all(2008)
|
|
|
|
|
|
|
|
# create models
|
2023-01-29 07:09:57 +00:00
|
|
|
zero_model = MlpModel()
|
2022-11-29 05:00:30 +00:00
|
|
|
torch_model = copy.deepcopy(zero_model)
|
|
|
|
|
2023-01-13 02:05:58 +00:00
|
|
|
seed_all(2008)
|
2022-11-29 05:00:30 +00:00
|
|
|
zero_model = zero_model.cuda()
|
|
|
|
torch_model = DDP(torch_model.cuda(), bucket_cap_mb=0)
|
|
|
|
|
|
|
|
# create optimizer
|
|
|
|
zero_optimizer = torch.optim.Adam(zero_model.parameters(), lr=1)
|
|
|
|
|
|
|
|
# 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
|
2023-09-19 06:20:26 +00:00
|
|
|
zero_optimizer = LowLevelZeroOptimizer(
|
|
|
|
zero_optimizer, overlap_communication=False, reduce_bucket_size=262144, clip_grad_norm=1.0
|
|
|
|
)
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
torch_optimizer = torch.optim.Adam(torch_model.parameters(), lr=1)
|
|
|
|
|
|
|
|
# create data
|
|
|
|
seed_all(2022 + local_rank)
|
|
|
|
input_data1 = torch.randn(32, 128).cuda()
|
|
|
|
input_data2 = torch.randn(32, 128).cuda()
|
|
|
|
|
2023-08-25 05:44:07 +00:00
|
|
|
def fwd_bwd_func(no_sync, cur_data, check_flag):
|
2023-07-04 04:00:33 +00:00
|
|
|
# zero1 fwd and bwd
|
|
|
|
with conditional_context(zero_optimizer.no_sync(), no_sync):
|
|
|
|
zero_output = zero_model(cur_data)
|
|
|
|
zero_optimizer.backward(zero_output.sum().float())
|
2022-11-29 05:00:30 +00:00
|
|
|
|
2023-07-04 04:00:33 +00:00
|
|
|
# torch-ddp fwd and bwd
|
|
|
|
with conditional_context(torch_model.no_sync(), no_sync):
|
2023-06-30 07:30:50 +00:00
|
|
|
torch_output = torch_model(cur_data)
|
|
|
|
assert torch.equal(zero_output, torch_output)
|
|
|
|
torch_output.sum().backward()
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
if check_flag:
|
|
|
|
# check grad
|
|
|
|
for (n, p), z1p in zip(torch_model.named_parameters(), zero_model.parameters()):
|
2023-06-05 07:58:31 +00:00
|
|
|
assert torch.equal(p.grad, z1p.grad)
|
2022-11-29 05:00:30 +00:00
|
|
|
|
2023-08-25 05:44:07 +00:00
|
|
|
fwd_bwd_func(sync, input_data1, sync)
|
|
|
|
fwd_bwd_func(False, input_data2, False)
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
zero_optimizer.step()
|
|
|
|
torch.nn.utils.clip_grad_norm_(torch_model.parameters(), 1.0)
|
|
|
|
torch_optimizer.step()
|
|
|
|
|
|
|
|
# check updated param
|
|
|
|
for (n, p), z1p in zip(torch_model.named_parameters(), zero_model.parameters()):
|
|
|
|
# print(n, p.shape, torch.max(p.data), torch.max(z1p.data), torch.max(torch.abs(p.data - z1p.data)))
|
|
|
|
assert_close(p.data, z1p.data)
|
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
2023-09-19 06:20:26 +00:00
|
|
|
colossalai.launch(config=dict(), rank=rank, world_size=world_size, port=port, host="localhost")
|
2022-11-29 05:00:30 +00:00
|
|
|
|
2023-08-25 05:44:07 +00:00
|
|
|
exam_zero_1_grad_acc(sync=True)
|
|
|
|
exam_zero_1_grad_acc(sync=False)
|
|
|
|
exam_zero_1_2_grad_acc()
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
def test_grad_accumulation():
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(run_dist, 2)
|
2022-11-29 05:00:30 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2022-11-29 05:00:30 +00:00
|
|
|
test_grad_accumulation()
|