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.
151 lines
5.0 KiB
151 lines
5.0 KiB
2 years ago
|
import argparse
|
||
2 years ago
|
import os
|
||
2 years ago
|
import warnings
|
||
2 years ago
|
|
||
|
import torch
|
||
2 years ago
|
import torch.distributed as dist
|
||
2 years ago
|
import torch.distributed.rpc as rpc
|
||
|
import torch.multiprocessing as mp
|
||
|
from torch import nn
|
||
|
from torch._C._distributed_rpc import _is_current_rpc_agent_set
|
||
|
from torch.optim import SGD, Adam, Optimizer, RMSprop
|
||
2 years ago
|
|
||
1 year ago
|
from colossalai.legacy import launch
|
||
|
from colossalai.legacy.pipeline.pipeline_process_group import ppg
|
||
2 years ago
|
from colossalai.logging import disable_existing_loggers
|
||
|
|
||
2 years ago
|
rpc_is_initialized = _is_current_rpc_agent_set
|
||
|
|
||
2 years ago
|
|
||
|
def color_debug(text, prefix=' ', color='blue'):
|
||
|
color = color.upper()
|
||
|
print(getattr(Back, color), prefix, Style.RESET_ALL, text)
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
class MLP(nn.Module):
|
||
2 years ago
|
|
||
2 years ago
|
def __init__(self, dim: int, layers: int):
|
||
|
super().__init__()
|
||
|
self.layers = torch.nn.ModuleList()
|
||
|
|
||
|
for _ in range(layers):
|
||
|
self.layers.append(nn.Linear(dim, dim, bias=False))
|
||
|
|
||
|
def forward(self, x):
|
||
|
for layer in self.layers:
|
||
|
x = layer(x)
|
||
2 years ago
|
return x.sum()
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
class DAG_MLP(nn.Module):
|
||
2 years ago
|
|
||
2 years ago
|
def __init__(self, dim: int, layers: int):
|
||
|
super().__init__()
|
||
|
self.layers = torch.nn.ModuleList()
|
||
|
self.dag_layer = nn.Linear(dim, dim, bias=False)
|
||
|
|
||
|
for _ in range(layers):
|
||
|
self.layers.append(nn.Linear(dim, dim, bias=False))
|
||
|
|
||
|
def forward(self, x, y):
|
||
|
for layer in self.layers:
|
||
|
x = layer(x)
|
||
|
y = self.dag_layer(y)
|
||
2 years ago
|
return x.sum(), y.sum()
|
||
2 years ago
|
|
||
2 years ago
|
|
||
2 years ago
|
class RpcTestModel(nn.Module):
|
||
|
|
||
|
def __init__(self, stage_id, actual_stage_num, feat_num, h) -> None:
|
||
|
super().__init__()
|
||
|
self.rank = stage_id
|
||
|
self.is_last_rank = stage_id == actual_stage_num - 1
|
||
|
self.linear_name = f'linear_{stage_id}'
|
||
2 years ago
|
|
||
2 years ago
|
if stage_id == 0:
|
||
2 years ago
|
linear = nn.Linear(feat_num, h)
|
||
2 years ago
|
elif stage_id == actual_stage_num - 1:
|
||
2 years ago
|
linear = nn.Linear(h, 1)
|
||
2 years ago
|
else:
|
||
2 years ago
|
linear = nn.Linear(h, h)
|
||
|
|
||
|
setattr(self, self.linear_name, linear)
|
||
2 years ago
|
|
||
|
def forward(self, x) -> torch.Tensor:
|
||
|
linear: nn.Module = getattr(self, self.linear_name)
|
||
|
out: torch.Tensor = linear(x)
|
||
|
|
||
|
if self.is_last_rank:
|
||
|
out = out.sum()
|
||
|
return out
|
||
|
|
||
|
|
||
|
def parse_args():
|
||
|
parser = argparse.ArgumentParser()
|
||
2 years ago
|
parser.add_argument('--epoch', type=int, default=1)
|
||
2 years ago
|
parser.add_argument('--world_size', type=int, default=2)
|
||
2 years ago
|
parser.add_argument('--batch_size', type=int, default=16)
|
||
2 years ago
|
parser.add_argument('--dp_degree', type=int, default=1)
|
||
|
parser.add_argument('--tp_degree', type=int, default=1)
|
||
2 years ago
|
parser.add_argument('--num_microbatches', type=int, default=2)
|
||
|
parser.add_argument('--chunk', type=int, default=1)
|
||
|
parser.add_argument('--use_checkpoint', action='store_true')
|
||
|
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'RMSprop'], default='SGD')
|
||
2 years ago
|
parser.add_argument('--device', type=str, choices=['cpu', 'cuda'], default='cuda')
|
||
2 years ago
|
parser.add_argument('--master_addr', type=str, default='localhost')
|
||
|
parser.add_argument('--master_port', type=str, default='29020')
|
||
|
parser.add_argument('--num_worker_threads', type=str, default=128)
|
||
|
return parser.parse_args()
|
||
|
|
||
|
|
||
2 years ago
|
def pg_parse_args():
|
||
|
parser = argparse.ArgumentParser()
|
||
|
parser.add_argument('--world_size', type=int, default=4)
|
||
|
parser.add_argument('--dp_degree', type=int, default=2)
|
||
|
parser.add_argument('--tp_degree', type=int, default=1)
|
||
|
parser.add_argument('--chunk', type=int, default=1)
|
||
|
parser.add_argument('--num_worker_threads', type=str, default=128)
|
||
|
parser.add_argument('--device', type=str, choices=['cpu', 'cuda'], default='cuda')
|
||
|
parser.add_argument('--master_addr', type=str, default='localhost')
|
||
|
parser.add_argument('--master_port', type=str, default='29020')
|
||
|
return parser.parse_args()
|
||
|
|
||
|
|
||
2 years ago
|
def run_worker(rank, args, master_func):
|
||
|
os.environ['MASTER_ADDR'] = args.master_addr
|
||
|
os.environ['MASTER_PORT'] = args.master_port
|
||
|
|
||
2 years ago
|
device = args.device
|
||
2 years ago
|
world_size = args.world_size
|
||
2 years ago
|
dp_degree = args.dp_degree
|
||
|
tp_degree = args.tp_degree
|
||
|
num_worker_threads = args.num_worker_threads
|
||
|
host = args.master_addr
|
||
|
port = args.master_port
|
||
|
backend = 'nccl' if device == 'cuda' else 'gloo'
|
||
|
|
||
|
disable_existing_loggers()
|
||
|
|
||
|
launch(dict(), rank, world_size, host, int(port), backend, verbose=False)
|
||
|
ppg.set_global_info(rank=rank,
|
||
|
world_size=world_size,
|
||
|
dp_degree=dp_degree,
|
||
|
tp_degree=tp_degree,
|
||
|
num_worker_threads=num_worker_threads,
|
||
|
device=device)
|
||
2 years ago
|
|
||
|
# in rpc mode, only rank 0 is needed to be coded
|
||
|
if rank == 0:
|
||
|
master_func(args)
|
||
|
# barrier here
|
||
2 years ago
|
if rpc_is_initialized():
|
||
|
rpc.shutdown()
|
||
|
else:
|
||
|
warnings.warn("RPC has not been initialized")
|
||
2 years ago
|
|
||
|
|
||
|
def rpc_run(args, master_func):
|
||
|
world_size = args.world_size
|
||
|
assert args.num_microbatches >= args.world_size, "num_microbatches cannot be fewer than world_size!"
|
||
|
mp.spawn(run_worker, args=(args, master_func), nprocs=world_size)
|