2022-10-18 08:31:22 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
2023-08-24 01:29:25 +00:00
|
|
|
import torch.distributed as dist
|
2022-10-18 08:31:22 +00:00
|
|
|
from torch.nn.parallel import DistributedDataParallel as DDP
|
2022-11-29 05:42:06 +00:00
|
|
|
from torch.testing import assert_close
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
import colossalai
|
2023-09-18 08:31:06 +00:00
|
|
|
from colossalai.legacy.amp import convert_to_apex_amp
|
2022-11-30 02:40:31 +00:00
|
|
|
from colossalai.nn.optimizer import HybridAdam
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import parameterize, rerun_if_address_is_in_use, spawn
|
2023-09-18 08:31:06 +00:00
|
|
|
from colossalai.utils import set_seed
|
2022-10-18 08:31:22 +00:00
|
|
|
from colossalai.utils.cuda import get_current_device
|
2023-08-24 01:29:25 +00:00
|
|
|
from colossalai.zero import GeminiDDP, GeminiOptimizer
|
|
|
|
from colossalai.zero.gemini.chunk import search_chunk_configuration
|
|
|
|
from tests.components_to_test import run_fwd_bwd
|
2022-10-18 08:31:22 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
|
|
|
|
2023-08-24 01:29:25 +00:00
|
|
|
PLACEMENT_CONFIGS = [
|
2023-09-19 06:20:26 +00:00
|
|
|
{"placement_policy": "static", "shard_param_frac": 0.0}, # zero2
|
|
|
|
{"placement_policy": "static", "shard_param_frac": 1.0}, # zero3
|
|
|
|
{"placement_policy": "static", "shard_param_frac": 0.5}, # zero3-half
|
|
|
|
{"placement_policy": "auto"},
|
2023-08-24 01:29:25 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def check_grad(model: GeminiDDP, torch_model: torch.nn.Module):
|
2022-10-18 08:31:22 +00:00
|
|
|
chunk_manager = model.chunk_manager
|
|
|
|
param_list = [p for p in model.parameters()]
|
|
|
|
chunk_list = chunk_manager.get_chunks(param_list)
|
2023-10-12 02:39:08 +00:00
|
|
|
if not model.reuse_fp16_chunk:
|
|
|
|
chunk_list = [chunk.grad_chunk for chunk in chunk_list]
|
2022-10-18 08:31:22 +00:00
|
|
|
for chunk in chunk_list:
|
|
|
|
chunk_manager.access_chunk(chunk)
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
for p0, p1 in zip(model.parameters(), torch_model.parameters()):
|
2022-11-29 05:42:06 +00:00
|
|
|
assert_close(p0, p1.grad, rtol=1e-3, atol=5e-5)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
@parameterize("placement_config", PLACEMENT_CONFIGS)
|
|
|
|
@parameterize("keep_gather", [False, True])
|
2023-10-12 02:39:08 +00:00
|
|
|
@parameterize("model_name", ["gpt2", "bert"])
|
2023-09-19 06:20:26 +00:00
|
|
|
@parameterize("use_grad_checkpoint", [False, True])
|
2023-10-12 02:39:08 +00:00
|
|
|
@parameterize("master_weights", [False, True])
|
2023-03-20 03:40:25 +00:00
|
|
|
def exam_gpt_fwd_bwd(
|
2023-08-24 01:29:25 +00:00
|
|
|
placement_config,
|
2023-03-20 03:40:25 +00:00
|
|
|
keep_gather,
|
|
|
|
model_name: str,
|
|
|
|
use_grad_checkpoint: bool = False,
|
2023-10-12 02:39:08 +00:00
|
|
|
master_weights: bool = True,
|
2023-03-20 03:40:25 +00:00
|
|
|
):
|
|
|
|
init_device = get_current_device()
|
2022-11-29 01:26:06 +00:00
|
|
|
get_components_func = non_distributed_component_funcs.get_callable(model_name)
|
2022-10-18 08:31:22 +00:00
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
|
|
|
|
2022-12-07 03:58:37 +00:00
|
|
|
set_seed(42)
|
2023-08-24 01:29:25 +00:00
|
|
|
model = model_builder(use_grad_checkpoint)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2022-12-07 03:58:37 +00:00
|
|
|
set_seed(42)
|
2022-11-29 01:26:06 +00:00
|
|
|
torch_model = model_builder(use_grad_checkpoint).cuda()
|
2022-10-18 08:31:22 +00:00
|
|
|
for torch_p, p in zip(torch_model.parameters(), model.parameters()):
|
|
|
|
torch_p.data.copy_(p.data)
|
|
|
|
|
|
|
|
world_size = torch.distributed.get_world_size()
|
2023-06-25 05:34:15 +00:00
|
|
|
config_dict, *_ = search_chunk_configuration(model, search_range_m=1, search_interval=100)
|
2023-09-19 06:20:26 +00:00
|
|
|
config_dict[world_size]["chunk_size"] = 5000
|
|
|
|
config_dict[world_size]["keep_gathered"] = keep_gather
|
2023-10-12 02:39:08 +00:00
|
|
|
model = GeminiDDP(
|
|
|
|
model, config_dict, init_device, pin_memory=True, **placement_config, master_weights=master_weights
|
|
|
|
)
|
2022-11-30 02:40:31 +00:00
|
|
|
optimizer = HybridAdam(model.parameters(), lr=1e-3)
|
2023-08-24 01:29:25 +00:00
|
|
|
zero_optim = GeminiOptimizer(optimizer, model, initial_scale=1)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2023-08-24 01:29:25 +00:00
|
|
|
rank = dist.get_rank()
|
2023-10-12 02:39:08 +00:00
|
|
|
amp_config = dict(opt_level="O2", keep_batchnorm_fp32=False, loss_scale=1, master_weights=master_weights)
|
2022-10-18 08:31:22 +00:00
|
|
|
torch_optim = torch.optim.Adam(torch_model.parameters(), lr=1e-3)
|
|
|
|
torch_model, torch_optim = convert_to_apex_amp(torch_model, torch_optim, amp_config)
|
2023-08-24 01:29:25 +00:00
|
|
|
torch_model = DDP(torch_model, device_ids=[rank])
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2023-08-24 01:29:25 +00:00
|
|
|
set_seed(rank)
|
2022-11-24 08:51:45 +00:00
|
|
|
for i, (input_ids, label) in enumerate(train_dataloader):
|
2022-11-29 01:26:06 +00:00
|
|
|
# you can only test a single fwd + bwd.
|
|
|
|
# after bwd param is grad for Gemini, due to the chunk reuse optimization.
|
2022-10-18 08:31:22 +00:00
|
|
|
if i > 0:
|
|
|
|
break
|
2022-11-30 02:40:31 +00:00
|
|
|
input_ids, label = input_ids.cuda(), label.cuda()
|
2022-12-07 03:58:37 +00:00
|
|
|
|
|
|
|
torch_optim.zero_grad()
|
|
|
|
zero_optim.zero_grad()
|
|
|
|
|
|
|
|
# set random seed is same as torch_model.eval()
|
|
|
|
set_seed(42)
|
2022-11-30 02:40:31 +00:00
|
|
|
torch_loss = run_fwd_bwd(torch_model, input_ids, label, criterion, torch_optim)
|
2022-12-07 03:58:37 +00:00
|
|
|
set_seed(42)
|
2022-11-30 02:40:31 +00:00
|
|
|
loss = run_fwd_bwd(model, input_ids, label, criterion, zero_optim)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2022-11-29 05:42:06 +00:00
|
|
|
assert torch.equal(torch_loss, loss)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2022-11-29 03:19:52 +00:00
|
|
|
check_grad(model, torch_model)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
config = {}
|
2023-09-19 06:20:26 +00:00
|
|
|
colossalai.launch(config=config, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2022-10-18 08:31:22 +00:00
|
|
|
exam_gpt_fwd_bwd()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
2023-09-19 06:20:26 +00:00
|
|
|
@pytest.mark.parametrize("world_size", [1, 4])
|
2022-10-18 08:31:22 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_gpt(world_size):
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(run_dist, world_size)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2023-10-12 02:39:08 +00:00
|
|
|
test_gpt(1)
|