2022-06-15 08:36:46 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
from torch.fx import symbolic_trace
|
2023-04-06 06:51:35 +00:00
|
|
|
|
2022-06-15 08:36:46 +00:00
|
|
|
from colossalai.fx.passes import column_shard_linear_pass
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.initialize import launch
|
2023-09-18 08:31:06 +00:00
|
|
|
from colossalai.legacy.core import global_context as gpc
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.logging import disable_existing_loggers
|
|
|
|
from colossalai.testing import clear_cache_before_run, rerun_if_address_is_in_use, spawn
|
2022-06-15 08:36:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MLP(torch.nn.Module):
|
|
|
|
def __init__(self, dim: int):
|
|
|
|
super().__init__()
|
|
|
|
self.linear1 = torch.nn.Linear(dim, dim)
|
|
|
|
self.linear2 = torch.nn.Linear(dim, dim)
|
|
|
|
self.linear3 = torch.nn.Linear(dim, dim)
|
|
|
|
self.linear4 = torch.nn.Linear(dim, dim)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
x = self.linear1(x)
|
|
|
|
x = self.linear2(x)
|
|
|
|
x = self.linear3(x)
|
|
|
|
x = self.linear4(x)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
CONFIG = dict(parallel=dict(tensor=dict(mode="1d", size=2)))
|
2022-06-15 08:36:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_layer(rank, world_size, port):
|
|
|
|
disable_existing_loggers()
|
2023-09-19 06:20:26 +00:00
|
|
|
launch(config=CONFIG, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2022-06-15 08:36:46 +00:00
|
|
|
input_tensor = torch.rand(2, 16).cuda()
|
|
|
|
model = MLP(16).cuda()
|
|
|
|
symbolic_traced = symbolic_trace(model)
|
|
|
|
output = model(input_tensor)
|
|
|
|
splitted_gm = column_shard_linear_pass(symbolic_traced)
|
|
|
|
new_output = splitted_gm(input_tensor)
|
|
|
|
|
|
|
|
assert output.equal(new_output)
|
|
|
|
|
|
|
|
gpc.destroy()
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
2023-04-06 06:51:35 +00:00
|
|
|
@clear_cache_before_run()
|
2022-06-15 08:36:46 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_1d():
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(check_layer, 2)
|
2022-06-15 08:36:46 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2022-06-15 08:36:46 +00:00
|
|
|
test_1d()
|