2022-04-21 03:42:37 +00:00
|
|
|
import torch
|
2022-06-22 07:16:47 +00:00
|
|
|
import pytest
|
|
|
|
import colossalai
|
|
|
|
import torch.nn.functional as F
|
|
|
|
import torch.multiprocessing as mp
|
|
|
|
from functools import partial
|
2022-07-11 07:51:48 +00:00
|
|
|
from colossalai.tensor import ColoTensor, ProcessGroup, ColoTensorSpec, ShardSpec
|
2022-04-25 03:49:20 +00:00
|
|
|
from colossalai.utils import get_current_device
|
2022-05-19 04:44:59 +00:00
|
|
|
from torch.nn import Parameter
|
2022-06-22 07:16:47 +00:00
|
|
|
from colossalai.testing import rerun_if_address_is_in_use
|
|
|
|
from colossalai.utils import free_port
|
2022-04-25 03:49:20 +00:00
|
|
|
|
|
|
|
|
2022-07-06 08:15:16 +00:00
|
|
|
def _run_layer_norm():
|
2022-04-25 03:49:20 +00:00
|
|
|
ln_op = torch.nn.LayerNorm(2, 3, device=get_current_device())
|
|
|
|
|
|
|
|
input_t = torch.randn(3, 2, device=get_current_device())
|
2022-07-06 08:15:16 +00:00
|
|
|
|
|
|
|
pg = ProcessGroup(tp_degree=torch.distributed.get_world_size())
|
|
|
|
input_t_colo = ColoTensor.from_torch_tensor(input_t.clone().detach(), ColoTensorSpec(pg))
|
2022-04-25 03:49:20 +00:00
|
|
|
|
|
|
|
# prepare colossalai LN
|
2022-07-06 08:15:16 +00:00
|
|
|
weight = ColoTensor(Parameter(ln_op.weight.detach()), ColoTensorSpec(pg))
|
|
|
|
bias = ColoTensor(Parameter(ln_op.bias.detach()), ColoTensorSpec(pg))
|
2022-04-25 03:49:20 +00:00
|
|
|
|
|
|
|
output = ln_op(input_t)
|
2022-05-19 04:44:59 +00:00
|
|
|
output_colo = F.layer_norm(input_t_colo, ln_op.normalized_shape, weight, bias, ln_op.eps)
|
2022-04-25 03:49:20 +00:00
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
assert torch.allclose(output_colo, output)
|
2022-04-25 03:49:20 +00:00
|
|
|
|
|
|
|
torch.mean(output).backward()
|
|
|
|
torch.mean(output_colo).backward()
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
assert torch.allclose(ln_op.weight.grad, weight.grad)
|
2022-04-21 06:21:10 +00:00
|
|
|
|
2022-04-24 04:32:10 +00:00
|
|
|
|
2022-06-22 07:16:47 +00:00
|
|
|
def check_spec_eq(tensor, other):
|
|
|
|
assert isinstance(tensor, ColoTensor) and isinstance(other, ColoTensor)
|
2022-07-06 08:15:16 +00:00
|
|
|
for k in dir(tensor.dist_spec):
|
2022-06-22 07:16:47 +00:00
|
|
|
if not k.startswith('__'):
|
2022-07-07 10:09:18 +00:00
|
|
|
assert hasattr(other.dist_spec, k), f"{k}"
|
2022-07-06 08:15:16 +00:00
|
|
|
assert getattr(tensor.dist_spec, k) == getattr(other.dist_spec, k)
|
2022-06-22 07:16:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_element_wise_ops():
|
2022-07-04 10:54:37 +00:00
|
|
|
world_size = torch.distributed.get_world_size()
|
|
|
|
pg = ProcessGroup(tp_degree=world_size)
|
2022-06-22 07:16:47 +00:00
|
|
|
t = torch.rand(2, 2)
|
2022-07-11 07:51:48 +00:00
|
|
|
x = ColoTensor(t, spec=ColoTensorSpec(pg, ShardSpec([0], [pg.tp_world_size()])))
|
2022-07-07 10:09:18 +00:00
|
|
|
|
2022-06-22 07:16:47 +00:00
|
|
|
check_spec_eq(x, x.cuda())
|
|
|
|
assert torch.equal(x.cuda(), t.cuda())
|
|
|
|
check_spec_eq(x, torch.abs(x))
|
|
|
|
assert torch.equal(torch.abs(x), torch.abs(t))
|
|
|
|
check_spec_eq(x, F.sigmoid(x))
|
|
|
|
assert torch.equal(F.sigmoid(x), F.sigmoid(t))
|
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
check_element_wise_ops()
|
2022-07-06 08:15:16 +00:00
|
|
|
_run_layer_norm()
|
2022-06-22 07:16:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize('world_size', [2])
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_element_wise_ops(world_size):
|
|
|
|
run_func = partial(run_dist, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
2022-07-06 08:15:16 +00:00
|
|
|
def run_dist2(rank, world_size, port):
|
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
_run_layer_norm()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize('world_size', [1])
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_ln(world_size):
|
|
|
|
run_func = partial(run_dist2, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
2022-04-21 09:18:56 +00:00
|
|
|
def check_all():
|
2022-06-22 07:16:47 +00:00
|
|
|
test_element_wise_ops(2)
|
2022-04-27 02:57:49 +00:00
|
|
|
|
2022-04-24 04:32:10 +00:00
|
|
|
|
2022-04-21 09:18:56 +00:00
|
|
|
if __name__ == '__main__':
|
2022-04-26 07:10:47 +00:00
|
|
|
check_all()
|