2023-06-28 05:28:18 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
|
|
|
import colossalai
|
|
|
|
from colossalai.logging import disable_existing_loggers
|
2023-08-11 07:43:23 +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-28 05:28:18 +00:00
|
|
|
from tests.kit.model_zoo import model_zoo
|
2023-08-11 07:43:23 +00:00
|
|
|
from tests.test_shardformer.test_model._utils import (
|
|
|
|
build_model_from_hybrid_plugin,
|
|
|
|
check_grad,
|
|
|
|
check_loss,
|
|
|
|
check_output_hidden_state,
|
|
|
|
check_weight,
|
|
|
|
run_forward_backward_with_hybrid_plugin,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def check_forward_backward(model_fn, data_gen_fn, output_transform_fn, loss_fn, test_config):
|
|
|
|
|
|
|
|
org_model, org_optimizer, sharded_model, sharded_optimizer, criterion, booster = \
|
|
|
|
build_model_from_hybrid_plugin(model_fn, loss_fn, test_config)
|
2023-06-28 05:28:18 +00:00
|
|
|
|
2023-08-11 07:43:23 +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-06-28 05:28:18 +00:00
|
|
|
|
2023-08-11 07:43:23 +00:00
|
|
|
stage_manager = booster.plugin.stage_manager
|
|
|
|
tp_group = booster.plugin.tp_group
|
2023-08-07 08:41:07 +00:00
|
|
|
|
2023-08-11 07:43:23 +00:00
|
|
|
# check last hidden state & loss
|
|
|
|
if stage_manager is None or stage_manager.is_last_stage():
|
2023-08-07 08:41:07 +00:00
|
|
|
|
2023-08-11 07:43:23 +00:00
|
|
|
if org_model.__class__.__name__ == 'ViTModel':
|
|
|
|
check_output_hidden_state(org_output, sharded_output, stage_manager, atol=1e-5, rtol=1e-3)
|
2023-06-28 05:28:18 +00:00
|
|
|
|
2023-08-11 07:43:23 +00:00
|
|
|
check_loss(org_loss, sharded_loss, atol=1e-6, rtol=1e-3)
|
2023-06-28 05:28:18 +00:00
|
|
|
|
2023-07-25 07:02:29 +00:00
|
|
|
# unwrap model
|
|
|
|
if org_model.__class__.__name__ == 'ViTModel':
|
|
|
|
vit_model = org_model
|
2023-08-11 07:43:23 +00:00
|
|
|
shard_vit_model = sharded_model.unwrap()
|
2023-07-25 07:02:29 +00:00
|
|
|
else:
|
|
|
|
vit_model = org_model.vit
|
2023-08-11 07:43:23 +00:00
|
|
|
shard_vit_model = sharded_model.unwrap().vit
|
2023-06-28 05:28:18 +00:00
|
|
|
|
2023-08-03 06:51:36 +00:00
|
|
|
# check grad
|
2023-08-11 07:43:23 +00:00
|
|
|
row_layer_for_check = ['encoder.layer[0].attention.attention.query', 'embeddings.patch_embeddings.projection']
|
|
|
|
col_layer_for_check = ['encoder.layer[0].attention.output.dense']
|
|
|
|
if stage_manager is None or stage_manager.is_first_stage():
|
|
|
|
check_grad(vit_model,
|
|
|
|
shard_vit_model,
|
|
|
|
row_layer_for_check,
|
|
|
|
tp_group,
|
|
|
|
atol=1e-5,
|
|
|
|
rtol=1e-3,
|
|
|
|
dim=0,
|
|
|
|
verbose=False)
|
|
|
|
check_grad(vit_model,
|
|
|
|
shard_vit_model,
|
|
|
|
col_layer_for_check,
|
|
|
|
tp_group,
|
|
|
|
atol=1e-5,
|
|
|
|
rtol=1e-3,
|
|
|
|
dim=1,
|
|
|
|
verbose=False)
|
|
|
|
|
|
|
|
# check weights after optimizer.step()
|
|
|
|
org_optimizer.step()
|
|
|
|
sharded_optimizer.step()
|
|
|
|
if stage_manager is None or stage_manager.is_first_stage():
|
|
|
|
check_weight(vit_model,
|
|
|
|
shard_vit_model,
|
|
|
|
col_layer_for_check,
|
|
|
|
tp_group,
|
|
|
|
atol=5e-3,
|
|
|
|
rtol=1e-3,
|
|
|
|
dim=1,
|
|
|
|
verbose=False)
|
2023-07-25 07:02:29 +00:00
|
|
|
|
2023-08-11 07:43:23 +00:00
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
@parameterize('test_config', [{
|
|
|
|
'tp_size': 2,
|
|
|
|
'pp_size': 2,
|
|
|
|
'num_microbatches': 4,
|
|
|
|
'enable_fused_normalization': True,
|
|
|
|
'use_lazy_init': False
|
|
|
|
}, {
|
|
|
|
'tp_size': 1,
|
|
|
|
'pp_size': 2,
|
|
|
|
'num_microbatches': 4,
|
|
|
|
'enable_fused_normalization': False,
|
|
|
|
'use_lazy_init': False
|
|
|
|
}, {
|
|
|
|
'tp_size': 4,
|
|
|
|
'pp_size': 1,
|
|
|
|
'enable_fused_normalization': True,
|
|
|
|
'use_lazy_init': False
|
|
|
|
}])
|
|
|
|
def run_vit_test(test_config):
|
|
|
|
|
|
|
|
# TODO: add test_config for TP+DP after supporting & debugging it
|
|
|
|
# {'tp_size': 2, 'pp_size': 1, 'enable_fused_normalization': True}
|
|
|
|
|
|
|
|
# TODO: add test_config for flash attention & jit operator after supporting
|
|
|
|
# TODO: fix bug when settign lazy_init for Conv2D Layers in ViT models
|
2023-07-25 07:02:29 +00:00
|
|
|
|
2023-06-28 05:28:18 +00:00
|
|
|
sub_model_zoo = model_zoo.get_sub_registry('transformers_vit')
|
2023-08-11 07:43:23 +00:00
|
|
|
test_config['precision'] = 'float' # Do not use fp16/bf16 in testing
|
|
|
|
|
2023-06-28 05:28:18 +00:00
|
|
|
for name, (model_fn, data_gen_fn, output_transform_fn, loss_fn, _) in sub_model_zoo.items():
|
2023-08-11 07:43:23 +00:00
|
|
|
check_forward_backward(model_fn, data_gen_fn, output_transform_fn, loss_fn, test_config)
|
|
|
|
|
|
|
|
clear_layout_converter()
|
|
|
|
Randomizer.reset_index()
|
2023-06-28 05:28:18 +00:00
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
2023-07-25 07:02:29 +00:00
|
|
|
def check_vit(rank, world_size, port):
|
|
|
|
disable_existing_loggers()
|
|
|
|
colossalai.launch(config={}, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
run_vit_test()
|
|
|
|
|
|
|
|
|
2023-06-28 05:28:18 +00:00
|
|
|
@pytest.mark.dist
|
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
@clear_cache_before_run()
|
|
|
|
def test_vit():
|
2023-08-11 07:43:23 +00:00
|
|
|
spawn(check_vit, 4)
|
2023-06-28 05:28:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_vit()
|