2022-11-29 03:36:28 +00:00
|
|
|
import torch
|
2022-12-08 05:32:27 +00:00
|
|
|
import pytest
|
|
|
|
import os
|
|
|
|
import torch.multiprocessing as mp
|
|
|
|
import torch.distributed.rpc as rpc
|
2022-11-29 03:36:28 +00:00
|
|
|
|
2022-12-08 05:32:27 +00:00
|
|
|
from torch import nn
|
|
|
|
from torch._C._distributed_rpc import _is_current_rpc_agent_set
|
|
|
|
from colossalai import launch
|
|
|
|
from colossalai.logging import disable_existing_loggers
|
|
|
|
from colossalai.pipeline.pipeline_process_group import ppg
|
2022-11-29 03:36:28 +00:00
|
|
|
from colossalai.pipeline.rpc._pipeline_schedule import OneFOneBPipelineEngine
|
|
|
|
from colossalai.fx.passes.adding_split_node_pass import split_with_split_nodes_pass, balanced_split_pass
|
|
|
|
from colossalai.fx import ColoTracer
|
2022-12-05 12:23:41 +00:00
|
|
|
from colossalai.pipeline.middleware.adaptor import get_fx_topology
|
2022-12-08 05:32:27 +00:00
|
|
|
from rpc_test_utils import MLP, DAG_MLP
|
2022-11-29 03:36:28 +00:00
|
|
|
from functools import partial
|
2022-12-08 05:32:27 +00:00
|
|
|
from colossalai.testing import parameterize, rerun_if_address_is_in_use
|
2022-11-29 03:36:28 +00:00
|
|
|
|
|
|
|
# global variable for model created
|
|
|
|
batch_size = 16
|
|
|
|
dim = 10
|
2022-12-08 05:32:27 +00:00
|
|
|
rpc_is_initialized = _is_current_rpc_agent_set
|
2022-11-29 03:36:28 +00:00
|
|
|
|
|
|
|
def create_partition_module(pp_rank: int, stage_num: int, model, data_kwargs):
|
|
|
|
model.eval()
|
|
|
|
tracer = ColoTracer()
|
|
|
|
meta_args = {k: v.to('meta') for k, v in data_kwargs.items()}
|
|
|
|
graph = tracer.trace(root=model, meta_args=meta_args)
|
|
|
|
gm = torch.fx.GraphModule(model, graph, model.__class__.__name__)
|
|
|
|
annotated_model = balanced_split_pass(gm, stage_num)
|
2022-12-05 12:23:41 +00:00
|
|
|
top_module, split_submodules = split_with_split_nodes_pass(annotated_model, merge_output=True)
|
|
|
|
topo = get_fx_topology(top_module)
|
|
|
|
for submodule in split_submodules:
|
|
|
|
if isinstance(submodule, torch.fx.GraphModule):
|
|
|
|
setattr(submodule, '_topo', topo)
|
|
|
|
return split_submodules[pp_rank+1]
|
2022-11-29 03:36:28 +00:00
|
|
|
|
2022-12-08 05:32:27 +00:00
|
|
|
def partition(model, data_kwargs: dict, pp_rank: int, chunk: int, stage_num: int):
|
2022-11-29 03:36:28 +00:00
|
|
|
torch.manual_seed(1024)
|
|
|
|
partition = create_partition_module(pp_rank, stage_num, model, data_kwargs)
|
|
|
|
return partition
|
|
|
|
|
2022-12-12 04:40:03 +00:00
|
|
|
def run_master(model_cls, world_size, forward_only):
|
2022-11-29 03:36:28 +00:00
|
|
|
torch.manual_seed(100)
|
|
|
|
|
2022-12-12 04:40:03 +00:00
|
|
|
epoch = 3
|
2022-12-08 05:32:27 +00:00
|
|
|
device = 'cuda'
|
|
|
|
stage_num = world_size
|
|
|
|
chunk = 1
|
|
|
|
num_microbatches = 8
|
|
|
|
use_checkpoint = 'store_true'
|
2022-11-29 03:36:28 +00:00
|
|
|
|
2022-12-08 05:32:27 +00:00
|
|
|
if model_cls == MLP:
|
|
|
|
def data_gen():
|
|
|
|
x = torch.zeros((batch_size, dim))
|
|
|
|
kwargs = dict(x=x)
|
|
|
|
return kwargs
|
|
|
|
model = model_cls(dim, stage_num * 3)
|
2022-12-12 04:40:03 +00:00
|
|
|
if forward_only:
|
|
|
|
labels = None
|
|
|
|
else:
|
|
|
|
labels = 1
|
2022-12-08 05:32:27 +00:00
|
|
|
elif model_cls == DAG_MLP:
|
|
|
|
def data_gen():
|
|
|
|
x = torch.zeros((batch_size, dim))
|
|
|
|
y = torch.zeros((batch_size, dim))
|
|
|
|
kwargs = dict(x=x, y=y)
|
|
|
|
return kwargs
|
|
|
|
model = model_cls(dim, stage_num * 3)
|
2022-12-12 04:40:03 +00:00
|
|
|
if forward_only:
|
|
|
|
labels = None
|
|
|
|
else:
|
|
|
|
labels = 1
|
2022-12-08 05:32:27 +00:00
|
|
|
else:
|
|
|
|
pass
|
2022-11-29 03:36:28 +00:00
|
|
|
|
|
|
|
data_kwargs = data_gen()
|
2022-12-12 04:40:03 +00:00
|
|
|
|
2022-12-08 05:32:27 +00:00
|
|
|
engine = OneFOneBPipelineEngine(partition_fn=partial(partition, model, data_kwargs),
|
2022-11-29 03:36:28 +00:00
|
|
|
stage_num=stage_num,
|
|
|
|
num_microbatches=num_microbatches,
|
|
|
|
device=device,
|
|
|
|
chunk=chunk,
|
2022-12-08 05:32:27 +00:00
|
|
|
checkpoint=use_checkpoint,)
|
2022-12-12 04:40:03 +00:00
|
|
|
if not forward_only:
|
|
|
|
engine.initialize_optimizer(getattr(torch.optim, 'SGD'), lr=1e-3)
|
2022-11-29 03:36:28 +00:00
|
|
|
|
|
|
|
for _ in range(epoch):
|
2022-12-08 05:32:27 +00:00
|
|
|
input_x = torch.randn((batch_size, dim), device=device)
|
|
|
|
input_y = torch.randn((batch_size, dim), device=device)
|
2022-12-12 04:40:03 +00:00
|
|
|
logits = engine.forward_backward({'x': input_x, 'y': input_y}, labels=labels, forward_only=forward_only)
|
2022-12-08 05:32:27 +00:00
|
|
|
|
2022-12-12 04:40:03 +00:00
|
|
|
def run_worker(rank, model_cls, world_size, forward_only, master_func):
|
2022-12-08 05:32:27 +00:00
|
|
|
master_addr = 'localhost'
|
|
|
|
master_port = 29020
|
|
|
|
os.environ['MASTER_ADDR'] = master_addr
|
|
|
|
os.environ['MASTER_PORT'] = str(master_port)
|
|
|
|
|
|
|
|
disable_existing_loggers()
|
|
|
|
|
|
|
|
launch(dict(), rank, world_size, master_addr, master_port, 'nccl', verbose=False)
|
|
|
|
ppg.set_global_info(rank=rank,
|
|
|
|
world_size=world_size,
|
|
|
|
dp_degree=1,
|
|
|
|
tp_degree=1,
|
|
|
|
num_worker_threads=128,
|
|
|
|
device='cuda')
|
|
|
|
|
|
|
|
# in rpc mode, only rank 0 is needed to be coded
|
|
|
|
if rank == 0:
|
2022-12-12 04:40:03 +00:00
|
|
|
master_func(model_cls, world_size, forward_only)
|
2022-12-08 05:32:27 +00:00
|
|
|
# barrier here
|
|
|
|
if rpc_is_initialized():
|
|
|
|
rpc.shutdown()
|
|
|
|
|
|
|
|
@pytest.mark.skip("skip due to CI torch version 1.11")
|
|
|
|
@parameterize('model_cls', [MLP, DAG_MLP])
|
2022-12-12 04:40:03 +00:00
|
|
|
@parameterize('forward_only', [True, False])
|
2022-12-08 05:32:27 +00:00
|
|
|
@pytest.mark.dist
|
|
|
|
@rerun_if_address_is_in_use()
|
2022-12-12 04:40:03 +00:00
|
|
|
def test_pp_middleware_fwd(model_cls, forward_only):
|
2022-12-08 05:32:27 +00:00
|
|
|
world_size = 4
|
|
|
|
master_func = run_master
|
2022-12-12 04:40:03 +00:00
|
|
|
mp.spawn(run_worker, args=(model_cls, world_size, forward_only, master_func), nprocs=world_size)
|
2022-11-29 03:36:28 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-12-12 04:40:03 +00:00
|
|
|
test_pp_middleware_fwd()
|