2023-06-23 10:00:22 +00:00
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
|
|
|
from torch.testing import assert_close
|
|
|
|
|
|
|
|
import colossalai
|
2023-06-26 10:05:00 +00:00
|
|
|
from colossalai.shardformer.layer import FusedLayerNorm
|
2023-06-23 10:00:22 +00:00
|
|
|
from colossalai.testing import rerun_if_address_is_in_use, spawn
|
|
|
|
|
|
|
|
|
2023-06-26 10:05:00 +00:00
|
|
|
def check_layernorm():
|
2023-06-23 10:00:22 +00:00
|
|
|
norm = nn.LayerNorm(128, 0.00001).cuda()
|
2023-06-26 10:05:00 +00:00
|
|
|
norm1d = FusedLayerNorm.from_native_module(norm, process_group=None)
|
2023-06-23 10:00:22 +00:00
|
|
|
|
|
|
|
assert norm1d.weight.shape == torch.Size([128])
|
|
|
|
|
|
|
|
# ensure state dict is reversibly loadable
|
|
|
|
norm.load_state_dict(norm1d.state_dict())
|
|
|
|
norm1d.load_state_dict(norm.state_dict())
|
|
|
|
|
|
|
|
# check computation correctness
|
|
|
|
x = torch.rand(4, 128).cuda()
|
|
|
|
out = norm(x)
|
|
|
|
gather_out = norm1d(x)
|
|
|
|
assert_close(out, gather_out)
|
|
|
|
|
|
|
|
# check backward correctness
|
|
|
|
out.sum().backward()
|
|
|
|
gather_out.sum().backward()
|
|
|
|
|
|
|
|
assert_close(norm.weight.grad, norm1d.weight.grad)
|
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
2023-06-26 10:05:00 +00:00
|
|
|
check_layernorm()
|
2023-06-23 10:00:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@rerun_if_address_is_in_use()
|
2023-06-26 10:05:00 +00:00
|
|
|
def test_layernorm():
|
2023-06-23 10:00:22 +00:00
|
|
|
spawn(run_dist, nprocs=2)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-06-28 05:28:18 +00:00
|
|
|
test_layernorm_1d()
|