2022-10-18 02:44:23 +00:00
|
|
|
import torch
|
2023-04-06 06:51:35 +00:00
|
|
|
from torch.fx import symbolic_trace
|
|
|
|
|
2022-10-18 02:44:23 +00:00
|
|
|
from colossalai.fx._compatibility import is_compatible_with_meta
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.fx.passes.adding_split_node_pass import split_with_split_nodes_pass, uniform_split_pass
|
2022-07-07 08:22:00 +00:00
|
|
|
from colossalai.fx.passes.meta_info_prop import MetaInfoProp
|
|
|
|
from colossalai.fx.passes.utils import get_comm_size
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import clear_cache_before_run
|
2022-10-18 02:44:23 +00:00
|
|
|
|
|
|
|
is_compatible = is_compatible_with_meta()
|
|
|
|
if is_compatible:
|
|
|
|
from colossalai.fx.profiler import MetaTensor
|
2022-07-07 08:22:00 +00:00
|
|
|
|
|
|
|
MODEL_DIM = 16
|
|
|
|
BATCH_SIZE = 8
|
|
|
|
PIPELINE_SIZE = 2
|
|
|
|
|
|
|
|
|
|
|
|
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-04-06 06:51:35 +00:00
|
|
|
@clear_cache_before_run()
|
2022-07-07 08:22:00 +00:00
|
|
|
def test_comm_size_compute():
|
|
|
|
model = MLP(MODEL_DIM)
|
2023-09-19 06:20:26 +00:00
|
|
|
input_sample = torch.rand(BATCH_SIZE, MODEL_DIM, device="meta")
|
2022-07-07 08:22:00 +00:00
|
|
|
gm = symbolic_trace(model)
|
2022-10-18 02:44:23 +00:00
|
|
|
if is_compatible:
|
|
|
|
input_sample = MetaTensor(input_sample, fake_device=next(gm.parameters()).device)
|
2022-07-07 08:22:00 +00:00
|
|
|
MetaInfoProp(gm).run(input_sample)
|
|
|
|
annotated_model = uniform_split_pass(gm, PIPELINE_SIZE)
|
|
|
|
split_model, split_submodules = split_with_split_nodes_pass(annotated_model)
|
|
|
|
submodule_list = list(split_model.children())
|
|
|
|
comm_size = get_comm_size(submodule_list[0], submodule_list[1])
|
|
|
|
# the shape of tensor send from partition 0 to partition 1 is (8, 16)
|
|
|
|
assert comm_size == 128
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2022-07-07 08:22:00 +00:00
|
|
|
test_comm_size_compute()
|