2023-09-18 08:31:06 +00:00
|
|
|
import argparse
|
2022-12-29 02:51:42 +00:00
|
|
|
import os
|
2023-08-24 01:29:25 +00:00
|
|
|
from contextlib import nullcontext
|
2022-11-08 08:14:07 +00:00
|
|
|
from functools import partial
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
2023-01-06 03:11:26 +00:00
|
|
|
from commons.model_zoo import model_builder
|
2023-01-31 06:21:22 +00:00
|
|
|
from commons.utils import get_data, get_profile_context, get_tflops, get_time_stamp
|
2022-11-08 08:14:07 +00:00
|
|
|
from packaging import version
|
|
|
|
|
|
|
|
import colossalai
|
2023-05-30 10:41:41 +00:00
|
|
|
from colossalai.booster import Booster
|
|
|
|
from colossalai.booster.plugin import GeminiPlugin, LowLevelZeroPlugin, TorchDDPPlugin
|
2023-08-24 01:29:25 +00:00
|
|
|
from colossalai.lazy import LazyInitContext
|
2022-11-08 08:14:07 +00:00
|
|
|
from colossalai.logging import disable_existing_loggers, get_dist_logger
|
2023-01-30 09:58:05 +00:00
|
|
|
from colossalai.nn.optimizer import HybridAdam
|
2022-11-08 08:14:07 +00:00
|
|
|
from colossalai.utils import get_current_device
|
2023-01-03 05:38:14 +00:00
|
|
|
|
|
|
|
CAI_VERSION = colossalai.__version__
|
|
|
|
|
2022-11-08 08:14:07 +00:00
|
|
|
|
2022-11-08 09:17:19 +00:00
|
|
|
def parse_args():
|
2023-09-18 08:31:06 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2022-11-16 03:36:27 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--distplan",
|
|
|
|
type=str,
|
2023-01-30 09:58:05 +00:00
|
|
|
default='CAI_Gemini',
|
2022-12-20 06:30:27 +00:00
|
|
|
help="The distributed plan [colossalai, zero1, zero2, torch_ddp, torch_zero].",
|
2022-11-16 03:36:27 +00:00
|
|
|
)
|
2022-12-28 05:54:08 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--batch_size",
|
|
|
|
type=int,
|
|
|
|
default=8,
|
|
|
|
help="batch size per DP group of training.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--model_type",
|
|
|
|
type=str,
|
2022-12-29 04:00:00 +00:00
|
|
|
default="gpt2_medium",
|
2022-12-28 05:54:08 +00:00
|
|
|
help="model model scale",
|
|
|
|
)
|
2023-01-16 06:44:29 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--train_step",
|
|
|
|
type=int,
|
|
|
|
default=10,
|
|
|
|
help="training iterations for test",
|
|
|
|
)
|
|
|
|
|
2022-11-08 09:17:19 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
2022-11-08 08:14:07 +00:00
|
|
|
class GPTLMLoss(nn.Module):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.loss_fn = nn.CrossEntropyLoss()
|
|
|
|
|
|
|
|
def forward(self, logits, labels):
|
|
|
|
shift_logits = logits[..., :-1, :].contiguous()
|
|
|
|
shift_labels = labels[..., 1:].contiguous()
|
|
|
|
# Flatten the tokens
|
|
|
|
return self.loss_fn(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
|
|
|
|
|
|
|
|
|
|
|
|
def get_cpu_mem():
|
|
|
|
return psutil.Process().memory_info().rss / 1024**2
|
|
|
|
|
|
|
|
|
|
|
|
def get_gpu_mem():
|
|
|
|
return torch.cuda.memory_allocated() / 1024**2
|
|
|
|
|
|
|
|
|
|
|
|
def get_mem_info(prefix=''):
|
|
|
|
return f'{prefix}GPU memory usage: {get_gpu_mem():.2f} MB, CPU memory usage: {get_cpu_mem():.2f} MB'
|
|
|
|
|
|
|
|
|
2022-12-29 02:51:42 +00:00
|
|
|
def get_model_size(model: nn.Module):
|
|
|
|
total_numel = 0
|
|
|
|
for module in model.modules():
|
|
|
|
for p in module.parameters(recurse=False):
|
|
|
|
total_numel += p.numel()
|
|
|
|
return total_numel
|
|
|
|
|
|
|
|
|
2022-12-29 04:01:45 +00:00
|
|
|
def model_size_formatter(numel: int) -> str:
|
|
|
|
GB_SIZE = 10**9
|
|
|
|
MB_SIZE = 10**6
|
|
|
|
KB_SIZE = 10**3
|
|
|
|
if numel >= GB_SIZE:
|
|
|
|
return f'{numel / GB_SIZE:.1f}B'
|
|
|
|
elif numel >= MB_SIZE:
|
|
|
|
return f'{numel / MB_SIZE:.1f}M'
|
|
|
|
elif numel >= KB_SIZE:
|
|
|
|
return f'{numel / KB_SIZE:.1f}K'
|
|
|
|
else:
|
|
|
|
return str(numel)
|
|
|
|
|
|
|
|
|
2022-12-29 02:51:42 +00:00
|
|
|
def set_cpu_maximum_parallelism():
|
|
|
|
conf_str = torch.__config__.parallel_info()
|
|
|
|
inter_str = conf_str.split("hardware_concurrency() : ")[1]
|
|
|
|
max_concurrency = inter_str.split('\n')[0]
|
|
|
|
os.environ["OMP_NUM_THREADS"] = max_concurrency
|
|
|
|
print(f"environmental variable OMP_NUM_THREADS is set to {max_concurrency}.")
|
|
|
|
|
|
|
|
|
2022-11-08 08:14:07 +00:00
|
|
|
def main():
|
2023-01-03 05:38:14 +00:00
|
|
|
# version check
|
2023-01-30 09:58:05 +00:00
|
|
|
# this example is supposed to work for versions greater than 0.2.0
|
|
|
|
assert version.parse(CAI_VERSION) >= version.parse("0.2.0")
|
2023-01-03 05:38:14 +00:00
|
|
|
|
2022-12-29 02:51:42 +00:00
|
|
|
set_cpu_maximum_parallelism()
|
2022-11-08 09:17:19 +00:00
|
|
|
args = parse_args()
|
|
|
|
|
2023-01-30 09:58:05 +00:00
|
|
|
# if args.distplan not in ["colossalai", "torch_ddp", "torch_zero", "zero1", "zero2"]:
|
|
|
|
if args.distplan not in ["CAI_ZeRO1", "CAI_ZeRO2", "CAI_Gemini", "Pytorch_DDP", "Pytorch_ZeRO"]:
|
2022-12-20 06:30:27 +00:00
|
|
|
raise TypeError(f"{args.distplan} is error")
|
|
|
|
|
2022-12-28 05:54:08 +00:00
|
|
|
# batch size per DP degree
|
|
|
|
BATCH_SIZE = args.batch_size
|
2022-11-08 08:14:07 +00:00
|
|
|
SEQ_LEN = 1024
|
|
|
|
VOCAB_SIZE = 50257
|
2022-12-27 09:39:53 +00:00
|
|
|
|
2023-01-16 06:44:29 +00:00
|
|
|
NUM_STEPS = args.train_step
|
|
|
|
|
2022-12-29 02:51:42 +00:00
|
|
|
WARMUP_STEPS = 1
|
|
|
|
assert WARMUP_STEPS < NUM_STEPS, "warmup steps should smaller than the total steps"
|
2023-01-31 06:21:22 +00:00
|
|
|
assert (NUM_STEPS - WARMUP_STEPS) % 2 == 1, "the number of valid steps should be odd to take the median"
|
|
|
|
PROF_FLAG = False # The flag of profiling, False by default
|
2022-11-08 09:17:19 +00:00
|
|
|
|
2022-11-08 08:14:07 +00:00
|
|
|
disable_existing_loggers()
|
|
|
|
colossalai.launch_from_torch(config={})
|
|
|
|
|
2022-11-08 09:17:19 +00:00
|
|
|
logger = get_dist_logger()
|
2022-12-28 05:54:08 +00:00
|
|
|
logger.info(f"{args.model_type}, {args.distplan}, batch size {BATCH_SIZE}", ranks=[0])
|
2022-11-08 08:14:07 +00:00
|
|
|
|
|
|
|
# build criterion
|
|
|
|
criterion = GPTLMLoss()
|
2022-11-16 03:36:27 +00:00
|
|
|
torch.manual_seed(123)
|
2023-01-30 09:58:05 +00:00
|
|
|
if args.distplan.startswith("CAI"):
|
2023-08-24 01:29:25 +00:00
|
|
|
ctx = LazyInitContext(default_device=get_current_device()) if args.distplan == "CAI_Gemini" else nullcontext()
|
2022-11-16 03:36:27 +00:00
|
|
|
# build GPT model
|
2023-08-24 01:29:25 +00:00
|
|
|
with ctx:
|
2023-01-30 09:58:05 +00:00
|
|
|
model = model_builder(args.model_type)(checkpoint=True)
|
2022-11-16 03:36:27 +00:00
|
|
|
|
2023-06-08 08:09:32 +00:00
|
|
|
# assign running configurations
|
2023-01-30 09:58:05 +00:00
|
|
|
if args.distplan == "CAI_ZeRO1":
|
|
|
|
zero_stage = 1
|
|
|
|
elif args.distplan == "CAI_ZeRO2":
|
|
|
|
zero_stage = 2
|
|
|
|
elif args.distplan == "CAI_Gemini":
|
|
|
|
zero_stage = 3
|
|
|
|
else:
|
|
|
|
raise RuntimeError
|
|
|
|
|
2023-05-30 10:41:41 +00:00
|
|
|
plugin = None
|
|
|
|
if args.distplan.startswith("CAI_ZeRO"):
|
|
|
|
plugin = LowLevelZeroPlugin(stage=zero_stage,
|
2023-06-19 03:22:42 +00:00
|
|
|
reduce_bucket_size_in_m=12,
|
2023-05-30 10:41:41 +00:00
|
|
|
overlap_communication=True,
|
|
|
|
verbose=True)
|
|
|
|
elif args.distplan == "CAI_Gemini":
|
2023-08-24 01:29:25 +00:00
|
|
|
plugin = GeminiPlugin(search_range_m=128, hidden_dim=model.config.n_embd)
|
2023-05-30 10:41:41 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
# build a highly optimized gpu/cpu optimizer
|
|
|
|
optimizer = HybridAdam(model.parameters(), lr=1e-3)
|
2022-11-16 03:36:27 +00:00
|
|
|
|
|
|
|
logger.info(get_mem_info(prefix='After init optim, '), ranks=[0])
|
2023-01-30 09:58:05 +00:00
|
|
|
elif args.distplan.startswith("Pytorch"):
|
2023-01-09 07:52:17 +00:00
|
|
|
assert args.tp_degree == 1, "The degree of TP should be 1 for DDP examples."
|
2022-12-28 05:54:08 +00:00
|
|
|
model = model_builder(args.model_type)(checkpoint=True).cuda()
|
2023-05-30 10:41:41 +00:00
|
|
|
plugin = TorchDDPPlugin()
|
2023-01-30 09:58:05 +00:00
|
|
|
if args.distplan.endswith("DDP"):
|
|
|
|
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
|
|
|
|
elif args.distplan.endswith("ZeRO"):
|
2022-12-20 06:30:27 +00:00
|
|
|
from torch.distributed.optim import ZeroRedundancyOptimizer
|
2023-01-30 09:58:05 +00:00
|
|
|
optimizer = ZeroRedundancyOptimizer(model.parameters(), optimizer_class=torch.optim.Adam, lr=1e-3)
|
2023-05-30 10:41:41 +00:00
|
|
|
|
2023-01-30 09:58:05 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError
|
2023-05-30 10:41:41 +00:00
|
|
|
# wrap your model and optimizer
|
|
|
|
booster = Booster(plugin=plugin)
|
|
|
|
model, optimizer, criterion, _, _ = booster.boost(model, optimizer, criterion)
|
2022-11-16 03:36:27 +00:00
|
|
|
|
2022-12-28 05:54:08 +00:00
|
|
|
# model is shared after TP
|
2022-12-29 02:51:42 +00:00
|
|
|
numel = get_model_size(model)
|
2022-12-29 04:01:45 +00:00
|
|
|
logger.info(f"the size of testing model size is {model_size_formatter(numel)}.")
|
2022-11-16 03:36:27 +00:00
|
|
|
logger.info(get_mem_info(prefix='After init model, '), ranks=[0])
|
2022-12-28 05:54:08 +00:00
|
|
|
|
|
|
|
# Tflops_per_GPU = global_batch * global_numel * seq_len * 8 / #gpu
|
|
|
|
# = (batch_per_DP_group * dp_degree) * (numel * tp_degree) * seq_len * 8 / (tp_degree * dp_degree)
|
|
|
|
# = batch_per_DP_group * numel * seq_len * 8
|
2022-11-16 03:36:27 +00:00
|
|
|
get_tflops_func = partial(get_tflops, numel, BATCH_SIZE, SEQ_LEN)
|
2022-11-08 08:14:07 +00:00
|
|
|
|
2022-11-08 09:17:19 +00:00
|
|
|
torch.cuda.synchronize()
|
2022-11-08 08:14:07 +00:00
|
|
|
model.train()
|
2022-12-29 02:51:42 +00:00
|
|
|
tflops_list = []
|
2023-01-31 06:21:22 +00:00
|
|
|
|
|
|
|
def train_step():
|
2022-11-08 08:14:07 +00:00
|
|
|
# we just use randomly generated data here
|
|
|
|
input_ids, attn_mask = get_data(BATCH_SIZE, SEQ_LEN, VOCAB_SIZE)
|
|
|
|
optimizer.zero_grad()
|
2022-12-29 02:51:42 +00:00
|
|
|
|
2022-11-08 08:14:07 +00:00
|
|
|
start = time()
|
|
|
|
outputs = model(input_ids, attn_mask)
|
|
|
|
loss = criterion(outputs, input_ids)
|
2022-12-29 02:51:42 +00:00
|
|
|
torch.cuda.synchronize()
|
|
|
|
fwd_end = time()
|
|
|
|
fwd_time = fwd_end - start
|
|
|
|
logger.info(get_mem_info(prefix=f'[{n + 1}/{NUM_STEPS}] Forward '), ranks=[0])
|
2023-05-30 10:41:41 +00:00
|
|
|
booster.backward(loss, optimizer)
|
2023-01-30 09:58:05 +00:00
|
|
|
|
2022-12-29 02:51:42 +00:00
|
|
|
torch.cuda.synchronize()
|
|
|
|
bwd_end = time()
|
|
|
|
bwd_time = bwd_end - fwd_end
|
|
|
|
logger.info(get_mem_info(prefix=f'[{n + 1}/{NUM_STEPS}] Backward '), ranks=[0])
|
|
|
|
|
2022-11-08 08:14:07 +00:00
|
|
|
optimizer.step()
|
2022-12-22 12:51:35 +00:00
|
|
|
torch.cuda.synchronize()
|
2022-12-29 02:51:42 +00:00
|
|
|
optim_time = time() - bwd_end
|
2022-11-08 08:14:07 +00:00
|
|
|
step_time = time() - start
|
2022-12-29 02:51:42 +00:00
|
|
|
logger.info(get_mem_info(prefix=f'[{n + 1}/{NUM_STEPS}] Optimizer step '), ranks=[0])
|
2022-11-08 08:14:07 +00:00
|
|
|
|
2022-12-29 02:51:42 +00:00
|
|
|
step_tflops = get_tflops_func(step_time)
|
|
|
|
logger.info(
|
|
|
|
f"[{n + 1}/{NUM_STEPS}] Loss:{loss.item():.3f}, Step time: {step_time:.3f}s, TFLOPS: {get_tflops_func(step_time):.3f}, FWD time: {fwd_time:.3f}s, BWD time: {bwd_time:.3f}s, OPTIM time: {optim_time:.3f}s",
|
|
|
|
ranks=[0],
|
|
|
|
)
|
|
|
|
if n >= WARMUP_STEPS:
|
|
|
|
tflops_list.append(step_tflops)
|
|
|
|
|
2023-01-31 06:21:22 +00:00
|
|
|
demo_profiler = get_profile_context(PROF_FLAG,
|
|
|
|
WARMUP_STEPS,
|
|
|
|
NUM_STEPS - WARMUP_STEPS,
|
|
|
|
save_dir=f"profile/{get_time_stamp()}-demo")
|
|
|
|
|
|
|
|
with demo_profiler as prof:
|
|
|
|
for n in range(NUM_STEPS):
|
|
|
|
train_step()
|
|
|
|
prof.step()
|
|
|
|
|
2022-12-29 02:51:42 +00:00
|
|
|
tflops_list.sort()
|
|
|
|
median_index = ((NUM_STEPS - WARMUP_STEPS) >> 1) + WARMUP_STEPS
|
|
|
|
logger.info(f"Median TFLOPS is {tflops_list[median_index]:.3f}")
|
2022-11-08 09:17:19 +00:00
|
|
|
torch.cuda.synchronize()
|
|
|
|
|
2022-11-08 08:14:07 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|