test_init_context.py 2.73 KB
Newer Older
Jiarui Fang's avatar
Jiarui Fang committed
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from functools import partial

import colossalai
import pytest
import torch
import torch.multiprocessing as mp
from colossalai.utils import free_port
ver217's avatar
ver217 committed
11
12
from colossalai.utils.cuda import get_current_device
from colossalai.utils.memory_tracer.allocator import GLOBAL_MODEL_DATA_TRACER
13
from colossalai.zero.init_ctx import ZeroInitContext
ver217's avatar
ver217 committed
14
from colossalai.zero.shard_utils import (BucketTensorShardStrategy, TensorShardStrategy)
15
from tests.components_to_test.registry import non_distributed_component_funcs
Jiarui Fang's avatar
Jiarui Fang committed
16

17
from common import CONFIG
18

Jiarui Fang's avatar
Jiarui Fang committed
19

ver217's avatar
ver217 committed
20
def run_dist(rank, world_size, port, init_device, shard_strategy):
Jiarui Fang's avatar
Jiarui Fang committed
21
22
    colossalai.launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')

23
24
    for get_components_func in non_distributed_component_funcs:
        model_builder, _, _, _, _ = get_components_func()
25
        model_numel_tensor = torch.zeros(1, dtype=torch.int)
26
        with ZeroInitContext(convert_fp16=True,
27
                             target_device=init_device,
ver217's avatar
ver217 committed
28
                             shard_strategy=shard_strategy(),
29
30
                             shard_param=True,
                             model_numel_tensor=model_numel_tensor):
31
32
            model = model_builder(checkpoint=True)

33
34
35
36
        for param in model.parameters():
            assert hasattr(param, 'col_attr')
            assert param.col_attr.data.dtype == torch.half
            assert param.col_attr.data.is_sharded
37
38
            assert param.col_attr.data.payload.device.type == init_device.type, \
                f'{param.col_attr.data.payload.device.type} vs. {init_device.type}'
Jiarui Fang's avatar
Jiarui Fang committed
39

40
    print(f'cpu usgae {GLOBAL_MODEL_DATA_TRACER.cpu_usage}')
41
    print(f'cuda usgae {GLOBAL_MODEL_DATA_TRACER.cuda_usage}')
42
    print(f'numel {model_numel_tensor}')
43
44
45
46
    if init_device.type == 'cuda':
        assert (GLOBAL_MODEL_DATA_TRACER.cuda_usage > 0)
    elif init_device.type == 'cpu':
        assert (GLOBAL_MODEL_DATA_TRACER.cpu_usage > 0)
47

Jiarui Fang's avatar
Jiarui Fang committed
48
49

@pytest.mark.dist
50
@pytest.mark.parametrize("world_size", [1, 4])
51
@pytest.mark.parametrize("init_device", [torch.device('cpu'), torch.device(f'cuda:{get_current_device()}')])
ver217's avatar
ver217 committed
52
53
54
55
56
57
58
@pytest.mark.parametrize("shard_strategy", [TensorShardStrategy, BucketTensorShardStrategy])
def test_zero_init_context(world_size, init_device, shard_strategy):
    run_func = partial(run_dist,
                       world_size=world_size,
                       port=free_port(),
                       init_device=init_device,
                       shard_strategy=shard_strategy)
Jiarui Fang's avatar
Jiarui Fang committed
59
60
61
62
    mp.spawn(run_func, nprocs=world_size)


if __name__ == '__main__':
ver217's avatar
ver217 committed
63
64
    test_zero_init_context(2, torch.device('cpu'), TensorShardStrategy)
    test_zero_init_context(2, torch.device(f'cuda:{get_current_device()}'), TensorShardStrategy)