mirror of https://github.com/hpcaitech/ColossalAI
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.9 KiB
92 lines
2.9 KiB
3 years ago
|
import torch
|
||
2 years ago
|
import pytest
|
||
|
import colossalai
|
||
|
import torch.nn.functional as F
|
||
|
import torch.multiprocessing as mp
|
||
|
from functools import partial
|
||
2 years ago
|
from colossalai.tensor import ColoTensor, ProcessGroup, ColoTensorSpec, ShardSpec
|
||
3 years ago
|
from colossalai.utils import get_current_device
|
||
3 years ago
|
from torch.nn import Parameter
|
||
2 years ago
|
from colossalai.testing import rerun_if_address_is_in_use
|
||
|
from colossalai.utils import free_port
|
||
3 years ago
|
|
||
|
|
||
2 years ago
|
def _run_layer_norm():
|
||
3 years ago
|
ln_op = torch.nn.LayerNorm(2, 3, device=get_current_device())
|
||
|
|
||
|
input_t = torch.randn(3, 2, device=get_current_device())
|
||
2 years ago
|
|
||
|
pg = ProcessGroup(tp_degree=torch.distributed.get_world_size())
|
||
|
input_t_colo = ColoTensor.from_torch_tensor(input_t.clone().detach(), ColoTensorSpec(pg))
|
||
3 years ago
|
|
||
|
# prepare colossalai LN
|
||
2 years ago
|
weight = ColoTensor(Parameter(ln_op.weight.detach()), ColoTensorSpec(pg))
|
||
|
bias = ColoTensor(Parameter(ln_op.bias.detach()), ColoTensorSpec(pg))
|
||
3 years ago
|
|
||
|
output = ln_op(input_t)
|
||
3 years ago
|
output_colo = F.layer_norm(input_t_colo, ln_op.normalized_shape, weight, bias, ln_op.eps)
|
||
3 years ago
|
|
||
3 years ago
|
assert torch.allclose(output_colo, output)
|
||
3 years ago
|
|
||
|
torch.mean(output).backward()
|
||
|
torch.mean(output_colo).backward()
|
||
|
|
||
3 years ago
|
assert torch.allclose(ln_op.weight.grad, weight.grad)
|
||
3 years ago
|
|
||
3 years ago
|
|
||
2 years ago
|
def check_spec_eq(tensor, other):
|
||
|
assert isinstance(tensor, ColoTensor) and isinstance(other, ColoTensor)
|
||
2 years ago
|
for k in dir(tensor.dist_spec):
|
||
2 years ago
|
if not k.startswith('__'):
|
||
2 years ago
|
assert hasattr(other.dist_spec, k), f"{k}"
|
||
2 years ago
|
assert getattr(tensor.dist_spec, k) == getattr(other.dist_spec, k)
|
||
2 years ago
|
|
||
|
|
||
|
def check_element_wise_ops():
|
||
2 years ago
|
world_size = torch.distributed.get_world_size()
|
||
|
pg = ProcessGroup(tp_degree=world_size)
|
||
2 years ago
|
t = torch.rand(2, 2)
|
||
2 years ago
|
x = ColoTensor(t, spec=ColoTensorSpec(pg, ShardSpec([0], [pg.tp_world_size()])))
|
||
2 years ago
|
|
||
2 years ago
|
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()
|
||
2 years ago
|
_run_layer_norm()
|
||
2 years ago
|
|
||
|
|
||
|
@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)
|
||
|
|
||
|
|
||
2 years ago
|
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)
|
||
|
|
||
|
|
||
3 years ago
|
def check_all():
|
||
2 years ago
|
test_element_wise_ops(2)
|
||
3 years ago
|
|
||
3 years ago
|
|
||
3 years ago
|
if __name__ == '__main__':
|
||
3 years ago
|
check_all()
|