test_shard_model_v2.py 2.71 KB
Newer Older
Jiarui Fang's avatar
Jiarui Fang committed
1
2
3
4
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from functools import partial
5

ver217's avatar
ver217 committed
6
7
import colossalai
import pytest
8
import torch
Jiarui Fang's avatar
Jiarui Fang committed
9
import torch.multiprocessing as mp
ver217's avatar
ver217 committed
10
from colossalai.testing import parameterize, rerun_on_exception
Jiarui Fang's avatar
Jiarui Fang committed
11
from colossalai.utils import free_port
ver217's avatar
ver217 committed
12
from colossalai.zero.init_ctx import ZeroInitContext
ver217's avatar
ver217 committed
13
from colossalai.zero.shard_utils import (BucketTensorShardStrategy, TensorShardStrategy)
Jiarui Fang's avatar
Jiarui Fang committed
14
from colossalai.zero.sharded_model import ShardedModelV2
15
from colossalai.zero.sharded_model._utils import cast_tensor_to_fp16
ver217's avatar
ver217 committed
16
from colossalai.zero.sharded_model.utils import col_model_deepcopy
17
from tests.components_to_test.registry import non_distributed_component_funcs
ver217's avatar
ver217 committed
18
19
from torch.nn.parallel import DistributedDataParallel as DDP

20
from common import CONFIG, check_grads_padding, run_fwd_bwd
21
22


23
@parameterize("enable_autocast", [True])
24
@parameterize("shard_strategy_class", [BucketTensorShardStrategy])
25
def run_model_test(enable_autocast, shard_strategy_class):
26
    test_models = ['repeated_computed_layers', 'resnet18', 'bert', 'no_leaf_module']
27
    shard_strategy = shard_strategy_class()
28
29
    for model_name in test_models:
        get_components_func = non_distributed_component_funcs.get_callable(model_name)
30
31
        model_builder, train_dataloader, _, _, criterion = get_components_func()

32
        with ZeroInitContext(target_device=torch.device('cuda', torch.cuda.current_device()),
33
                             shard_strategy=shard_strategy,
34
                             shard_param=True):
35
            zero_model = model_builder(checkpoint=True)
36
        zero_model = ShardedModelV2(zero_model, shard_strategy)
37
38
39
40

        model = model_builder(checkpoint=True).half()
        col_model_deepcopy(zero_model, model)
        model = model.cuda()
41

42
        model = DDP(model, device_ids=[torch.cuda.current_device()])
43
44

        for i, (data, label) in enumerate(train_dataloader):
45
            if i > 5:
46
                break
47

48
49
50
            data, label = cast_tensor_to_fp16(data).cuda(), label.cuda()
            run_fwd_bwd(model, data, label, criterion, enable_autocast)
            run_fwd_bwd(zero_model, data, label, criterion, enable_autocast)
51

ver217's avatar
ver217 committed
52
            check_grads_padding(model, zero_model, loose=True)
Jiarui Fang's avatar
Jiarui Fang committed
53
54


55
56
def run_dist(rank, world_size, port):
    colossalai.launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
57
    run_model_test()
58
59


Jiarui Fang's avatar
Jiarui Fang committed
60
@pytest.mark.dist
61
@pytest.mark.parametrize("world_size", [1, 2])
62
@rerun_on_exception(exception_type=mp.ProcessRaisedException, pattern=".*Address already in use.*")
63
64
def test_shard_model_v2(world_size):
    run_func = partial(run_dist, world_size=world_size, port=free_port())
Jiarui Fang's avatar
Jiarui Fang committed
65
66
67
68
    mp.spawn(run_func, nprocs=world_size)


if __name__ == '__main__':
69
    test_shard_model_v2(world_size=2)