test_2p5d_init.py 3.45 KB
Newer Older
zbian's avatar
zbian committed
1
2
3
4
5
6
7
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from functools import partial
from pathlib import Path

import pytest
Frank Lee's avatar
Frank Lee committed
8
import torch
zbian's avatar
zbian committed
9
10
11
import torch.multiprocessing as mp
from colossalai.context.parallel_mode import ParallelMode
from colossalai.core import global_context as gpc
Frank Lee's avatar
Frank Lee committed
12
from colossalai.initialize import launch
13
from colossalai.utils import free_port
zbian's avatar
zbian committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

CONFIG_PATH = Path(__file__).parent.joinpath('configs/parallel_2p5d_init.py').absolute()


def check_data_parallel_rank(rank):
    dp_rank = gpc.get_local_rank(ParallelMode.DATA)

    if rank in list(range(16)):
        assert dp_rank == 0
    elif rank in list(range(16, 32)):
        assert dp_rank == 1


def check_pipeline_parallel_rank(rank):
    ppr = gpc.get_local_rank(ParallelMode.PIPELINE)

    if rank in list(range(8)):
        assert ppr == 0
    elif rank in list(range(8, 16)):
        assert ppr == 1
    elif rank in list(range(16, 24)):
        assert ppr == 0
    elif rank in list(range(24, 32)):
        assert ppr == 1


ver217's avatar
ver217 committed
40
41
42
43
44
45
def check_model_parallel_rank(rank):
    for i in range(16):
        if rank in [i, i+16]:
            assert gpc.get_local_rank(ParallelMode.MODEL) == i


zbian's avatar
zbian committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def check_tensor_parallel_rank(rank):
    tp_rank = gpc.get_local_rank(ParallelMode.TENSOR)

    for i in range(8):
        ranks = list(range(i, 32, 8))
        if rank in ranks:
            assert tp_rank == i, f'{rank}:{tp_rank}'


def check_2p5d_parallel_rank(rank):
    rp_rank = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW)
    cp_rank = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_COL)
    dp_rank = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_DEP)
    xp_rank = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_XZ)

    # check for row parallel group
    for i in range(2):
        ranks = list(range(i, 32, 2))
        if rank in ranks:
            assert rp_rank == i

    # check for col parallel group
    for i in range(2):
        ranks = list(range(i * 2, 32, 4))
        ranks_plus_ones = [val + 1 for val in ranks]
        ranks.extend(ranks_plus_ones)
        if rank in ranks:
            assert cp_rank == i

    # check for depth parallel group
    for i in range(2):
        ranks = []
        for j in range(i * 4, 32, 8):
            ranks.extend([j + k for k in range(4)])
        if rank in ranks:
            assert dp_rank == i

    # check for xz parallel group
    for i in range(2):
        ranks = list(range(i * 2, 32, 8))
        ranks_plus_one = [val + 1 for val in ranks]
        ranks.extend(ranks_plus_one)
        if rank in ranks:
            assert xp_rank == i


Frank Lee's avatar
Frank Lee committed
92
def init_2halfd(rank, world_size, backend, port, host):
zbian's avatar
zbian committed
93
94
    dist_args = dict(
        config=CONFIG_PATH,
Frank Lee's avatar
Frank Lee committed
95
        rank=rank,
zbian's avatar
zbian committed
96
97
98
        world_size=world_size,
        backend=backend,
        port=port,
Frank Lee's avatar
Frank Lee committed
99
100
        host=host,
        verbose=True
zbian's avatar
zbian committed
101
    )
Frank Lee's avatar
Frank Lee committed
102
103
104
105
106
    launch(**dist_args)
    check_data_parallel_rank(rank)
    check_pipeline_parallel_rank(rank)
    check_tensor_parallel_rank(rank)
    check_2p5d_parallel_rank(rank)
ver217's avatar
ver217 committed
107
    check_model_parallel_rank(rank)
zbian's avatar
zbian committed
108
    gpc.destroy()
Frank Lee's avatar
Frank Lee committed
109
    torch.cuda.empty_cache()
zbian's avatar
zbian committed
110
111
112
113
114
115
116
117
118
119
120


@pytest.mark.cpu
def test_2halfd_init():
    """
    As no computation or communication is done, we can run this test on CPU.
    """
    world_size = 32
    test_fn = partial(init_2halfd,
                      world_size=world_size,
                      backend='gloo',
121
                      port=free_port(),
zbian's avatar
zbian committed
122
123
124
125
126
127
128
                      host='localhost'
                      )
    mp.spawn(test_fn, nprocs=world_size)


if __name__ == '__main__':
    test_2halfd_init()