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.
49 lines
1.6 KiB
49 lines
1.6 KiB
3 years ago
|
from torch.nn import functional as F
|
||
3 years ago
|
from functools import partial
|
||
|
|
||
|
import colossalai
|
||
|
import pytest
|
||
|
import torch
|
||
|
import torch.multiprocessing as mp
|
||
3 years ago
|
from colossalai.testing import rerun_if_address_is_in_use
|
||
3 years ago
|
from colossalai.utils import free_port
|
||
2 years ago
|
from colossalai.tensor import ColoTensorSpec, ProcessGroup, ColoTensor
|
||
2 years ago
|
from tests.test_tensor.common_utils import tensor_equal, tensor_shard_equal, split_param_col_tp1d, split_param_row_tp1d
|
||
3 years ago
|
|
||
|
|
||
2 years ago
|
def run_with_spec(spec_init_func, pg: ProcessGroup):
|
||
3 years ago
|
model = torch.nn.Embedding(12, 32).cuda()
|
||
2 years ago
|
weight = ColoTensor(torch.nn.Parameter(model.weight.detach()), ColoTensorSpec(pg))
|
||
2 years ago
|
|
||
2 years ago
|
spec_init_func(weight, pg)
|
||
2 years ago
|
|
||
3 years ago
|
x = torch.tensor((0, 3, 6, 9)).cuda()
|
||
|
out = model(x)
|
||
|
colo_out = F.embedding(x, weight)
|
||
3 years ago
|
assert tensor_equal(out, colo_out)
|
||
3 years ago
|
grad = torch.rand_like(out)
|
||
|
out.backward(grad)
|
||
|
colo_out.backward(grad)
|
||
2 years ago
|
# compare grad inside a TP group
|
||
|
assert tensor_shard_equal(model.weight.grad, weight.grad, pg.tp_local_rank(), pg.tp_world_size())
|
||
3 years ago
|
|
||
|
|
||
3 years ago
|
def run_dist(rank, world_size, port):
|
||
2 years ago
|
# config = dict(parallel=dict(tensor=dict(mode="1d", size=world_size),))
|
||
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
||
|
pg = ProcessGroup(tp_degree=world_size)
|
||
2 years ago
|
run_with_spec(split_param_row_tp1d, pg)
|
||
|
run_with_spec(split_param_col_tp1d, pg)
|
||
3 years ago
|
|
||
3 years ago
|
|
||
|
@pytest.mark.dist
|
||
3 years ago
|
@pytest.mark.parametrize('world_size', [1, 4])
|
||
3 years ago
|
@rerun_if_address_is_in_use()
|
||
|
def test_embedding_1d(world_size):
|
||
|
run_func = partial(run_dist, world_size=world_size, port=free_port())
|
||
|
mp.spawn(run_func, nprocs=world_size)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
3 years ago
|
test_embedding_1d(4)
|