2023-06-15 08:50:08 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
|
|
|
import colossalai
|
|
|
|
from colossalai.logging import disable_existing_loggers
|
2023-08-08 09:46:44 +00:00
|
|
|
from colossalai.shardformer.layer.utils import Randomizer
|
|
|
|
from colossalai.tensor.d_tensor.api import clear_layout_converter
|
|
|
|
from colossalai.testing import clear_cache_before_run, parameterize, rerun_if_address_is_in_use, spawn
|
2023-06-21 01:32:46 +00:00
|
|
|
from tests.kit.model_zoo import model_zoo
|
2023-08-08 09:46:44 +00:00
|
|
|
from tests.test_shardformer.test_model._utils import (
|
|
|
|
build_model_from_hybrid_plugin,
|
2023-08-30 06:50:34 +00:00
|
|
|
check_all_grad_tensors,
|
2023-08-08 09:46:44 +00:00
|
|
|
check_loss,
|
|
|
|
check_output_hidden_state,
|
|
|
|
check_weight,
|
2023-08-30 06:50:34 +00:00
|
|
|
get_grad_tensors_for_check,
|
2023-08-08 09:46:44 +00:00
|
|
|
run_forward_backward_with_hybrid_plugin,
|
2023-08-16 08:11:57 +00:00
|
|
|
unwrap_model,
|
2023-08-08 09:46:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def check_forward_backward(model_fn, data_gen_fn, output_transform_fn, loss_fn, test_config):
|
2023-09-19 06:20:26 +00:00
|
|
|
org_model, org_optimizer, sharded_model, sharded_optimizer, criterion, booster = build_model_from_hybrid_plugin(
|
|
|
|
model_fn, loss_fn, test_config
|
|
|
|
)
|
2023-08-08 09:46:44 +00:00
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
org_loss, org_output, sharded_loss, sharded_output = run_forward_backward_with_hybrid_plugin(
|
|
|
|
org_model, sharded_model, sharded_optimizer, data_gen_fn, output_transform_fn, criterion, booster
|
|
|
|
)
|
2023-08-08 09:46:44 +00:00
|
|
|
|
|
|
|
stage_manager = booster.plugin.stage_manager
|
|
|
|
tp_group = booster.plugin.tp_group
|
|
|
|
|
|
|
|
# unwrap model
|
2023-08-16 08:11:57 +00:00
|
|
|
t5 = unwrap_model(org_model)
|
|
|
|
sharded_t5 = unwrap_model(sharded_model)
|
2023-08-08 09:46:44 +00:00
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
row_layer_for_check = ["shared", "encoder.block[0].layer[0].SelfAttention.q"]
|
2023-08-08 09:46:44 +00:00
|
|
|
|
2023-08-30 06:50:34 +00:00
|
|
|
# Save gradient tensors for comparison between the original model and the sharded model before optimizer step.
|
|
|
|
grads_to_check = {}
|
2023-09-19 06:20:26 +00:00
|
|
|
if test_config["precision"] == "fp32":
|
2023-08-14 07:49:13 +00:00
|
|
|
atol, rtol = 1e-5, 1e-3
|
|
|
|
else:
|
|
|
|
atol, rtol = 5e-3, 5e-3
|
2023-08-21 04:04:52 +00:00
|
|
|
if (stage_manager is None or stage_manager.is_first_stage()) and booster.plugin.zero_stage == 0:
|
2023-09-19 06:20:26 +00:00
|
|
|
row_layer_grads = get_grad_tensors_for_check(
|
|
|
|
t5, sharded_t5, row_layer_for_check, tp_group, atol=atol, rtol=rtol, dim=0
|
|
|
|
)
|
2023-08-30 06:50:34 +00:00
|
|
|
grads_to_check.update(row_layer_grads)
|
|
|
|
|
|
|
|
# optimizer executes step
|
2023-08-08 09:46:44 +00:00
|
|
|
org_optimizer.step()
|
|
|
|
sharded_optimizer.step()
|
2023-08-30 06:50:34 +00:00
|
|
|
|
|
|
|
# check last hidden state & loss
|
|
|
|
if stage_manager is None or stage_manager.is_last_stage():
|
2023-09-19 06:20:26 +00:00
|
|
|
if test_config["precision"] == "fp32":
|
2023-08-30 06:50:34 +00:00
|
|
|
atol, rtol = 1e-5, 1e-3
|
|
|
|
else:
|
|
|
|
atol, rtol = 5e-3, 5e-3
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if org_model.__class__.__name__ != "T5ForConditionalGeneration":
|
2023-08-30 06:50:34 +00:00
|
|
|
check_output_hidden_state(org_output, sharded_output, stage_manager, atol=atol, rtol=rtol)
|
|
|
|
|
|
|
|
check_loss(org_loss, sharded_loss, atol=atol, rtol=rtol)
|
|
|
|
|
|
|
|
# check weights
|
2023-09-19 06:20:26 +00:00
|
|
|
if test_config["precision"] == "fp32":
|
2023-08-30 06:50:34 +00:00
|
|
|
atol, rtol = 5e-4, 1e-3
|
2023-08-14 07:49:13 +00:00
|
|
|
else:
|
|
|
|
atol, rtol = 5e-3, 5e-3
|
2023-08-08 09:46:44 +00:00
|
|
|
if stage_manager is None or stage_manager.is_first_stage():
|
2023-08-14 07:49:13 +00:00
|
|
|
check_weight(t5, sharded_t5, row_layer_for_check, tp_group, atol=atol, rtol=rtol, dim=0, verbose=False)
|
2023-08-08 09:46:44 +00:00
|
|
|
|
2023-08-30 06:50:34 +00:00
|
|
|
# check grads
|
|
|
|
check_all_grad_tensors(grads_to_check)
|
|
|
|
|
2023-08-08 09:46:44 +00:00
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
@parameterize(
|
|
|
|
"test_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"tp_size": 2,
|
|
|
|
"pp_size": 2,
|
|
|
|
"num_microbatches": 2,
|
|
|
|
"enable_all_optimization": True,
|
|
|
|
"use_lazy_init": True,
|
|
|
|
"precision": "fp16",
|
|
|
|
"initial_scale": 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tp_size": 1,
|
|
|
|
"pp_size": 2,
|
|
|
|
"num_microbatches": 4,
|
|
|
|
"use_lazy_init": False,
|
|
|
|
"precision": "fp16",
|
|
|
|
"initial_scale": 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tp_size": 4,
|
|
|
|
"pp_size": 1,
|
|
|
|
"enable_all_optimization": True,
|
|
|
|
"use_lazy_init": False,
|
|
|
|
"precision": "fp32",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tp_size": 1,
|
|
|
|
"pp_size": 4,
|
|
|
|
"num_microbatches": 4,
|
|
|
|
"enable_all_optimization": False,
|
|
|
|
"use_lazy_init": False,
|
|
|
|
"precision": "fp32",
|
|
|
|
},
|
|
|
|
{"tp_size": 2, "pp_size": 1, "enable_all_optimization": True, "use_lazy_init": False, "precision": "fp32"},
|
|
|
|
{
|
|
|
|
"tp_size": 2,
|
|
|
|
"pp_size": 1,
|
|
|
|
"enable_all_optimization": True,
|
|
|
|
"use_lazy_init": True,
|
|
|
|
"zero_stage": 2,
|
|
|
|
"precision": "fp16",
|
|
|
|
"initial_scale": 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tp_size": 1,
|
|
|
|
"pp_size": 2,
|
|
|
|
"num_microbatches": 2,
|
|
|
|
"enable_all_optimization": True,
|
|
|
|
"use_lazy_init": True,
|
|
|
|
"zero_stage": 1,
|
|
|
|
"precision": "fp16",
|
|
|
|
"initial_scale": 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2023-08-08 09:46:44 +00:00
|
|
|
@clear_cache_before_run()
|
|
|
|
def run_t5_test(test_config):
|
2023-09-19 06:20:26 +00:00
|
|
|
sub_model_zoo = model_zoo.get_sub_registry("transformers_t5")
|
2023-08-08 09:46:44 +00:00
|
|
|
|
2023-06-21 01:32:46 +00:00
|
|
|
for name, (model_fn, data_gen_fn, output_transform_fn, loss_fn, _) in sub_model_zoo.items():
|
2023-08-08 09:46:44 +00:00
|
|
|
# skip 4-stage pp test for t5_encoder
|
2023-09-19 06:20:26 +00:00
|
|
|
if test_config["pp_size"] > 2 and name == "transformers_t5_encoder_model":
|
2023-08-08 09:46:44 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
check_forward_backward(model_fn, data_gen_fn, output_transform_fn, loss_fn, test_config)
|
|
|
|
|
|
|
|
clear_layout_converter()
|
|
|
|
Randomizer.reset_index()
|
2023-06-21 01:32:46 +00:00
|
|
|
torch.cuda.empty_cache()
|
2023-06-15 08:50:08 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
@parameterize(
|
|
|
|
"test_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"tp_size": 2,
|
|
|
|
"pp_size": 2,
|
|
|
|
"num_microbatches": 4,
|
|
|
|
"enable_all_optimization": False,
|
|
|
|
"use_lazy_init": False,
|
|
|
|
"precision": "fp32",
|
|
|
|
"initial_scale": 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tp_size": 2,
|
|
|
|
"pp_size": 2,
|
|
|
|
"num_microbatches": 4,
|
|
|
|
"enable_all_optimization": False,
|
|
|
|
"use_lazy_init": False,
|
|
|
|
"precision": "fp16",
|
|
|
|
"zero_stage": 1,
|
|
|
|
"initial_scale": 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2023-08-23 07:05:24 +00:00
|
|
|
def run_t5_3d_test(test_config):
|
2023-09-19 06:20:26 +00:00
|
|
|
sub_model_zoo = model_zoo.get_sub_registry("transformers_t5")
|
2023-08-23 07:05:24 +00:00
|
|
|
|
|
|
|
for name, (model_fn, data_gen_fn, output_transform_fn, loss_fn, _) in sub_model_zoo.items():
|
|
|
|
check_forward_backward(model_fn, data_gen_fn, output_transform_fn, loss_fn, test_config)
|
|
|
|
|
|
|
|
clear_layout_converter()
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
2023-07-04 01:57:03 +00:00
|
|
|
def check_t5(rank, world_size, port):
|
|
|
|
disable_existing_loggers()
|
2023-09-19 06:20:26 +00:00
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2023-07-04 01:57:03 +00:00
|
|
|
run_t5_test()
|
|
|
|
|
|
|
|
|
2023-08-23 07:05:24 +00:00
|
|
|
def check_t5_3d(rank, world_size, port):
|
|
|
|
disable_existing_loggers()
|
2023-09-19 06:20:26 +00:00
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2023-08-23 07:05:24 +00:00
|
|
|
run_t5_3d_test()
|
|
|
|
|
|
|
|
|
2023-06-15 08:50:08 +00:00
|
|
|
@pytest.mark.dist
|
|
|
|
@rerun_if_address_is_in_use()
|
2023-06-19 09:57:37 +00:00
|
|
|
@clear_cache_before_run()
|
2023-06-15 08:50:08 +00:00
|
|
|
def test_t5():
|
2023-08-08 09:46:44 +00:00
|
|
|
spawn(check_t5, 4)
|
2023-06-15 08:50:08 +00:00
|
|
|
|
|
|
|
|
2023-08-23 07:05:24 +00:00
|
|
|
@pytest.mark.largedist
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
@clear_cache_before_run()
|
|
|
|
def test_t5_3d():
|
|
|
|
spawn(check_t5_3d, 8)
|
|
|
|
|
|
|
|
|
2023-06-15 08:50:08 +00:00
|
|
|
if __name__ == "__main__":
|
2023-06-30 08:16:44 +00:00
|
|
|
test_t5()
|
2023-08-23 07:05:24 +00:00
|
|
|
test_t5_3d()
|