test_hybrid_device.py 3.04 KB
Newer Older
1
2
from colossalai.utils import free_port, get_current_device
from colossalai.utils.model.colo_init_context import ColoInitContext
3
from colossalai.testing import rerun_if_address_is_in_use
4
from colossalai.tensor import ComputePattern, ParallelAction
5

6
7
8
from functools import partial
from colossalai.core import global_context as gpc
from colossalai.context import ParallelMode
9

10
11
from colossalai.nn.parallel.layers import init_colo_module
from colossalai.nn.parallel.data_parallel import ColoDDP
12
from colossalai.nn.optimizer import ColoOptimizer
13
14
15
16
17
18

import colossalai
import torch
import torch.multiprocessing as mp
import pytest

19

20
class Net(torch.nn.Module):
21

22
23
24
25
    def __init__(self):
        super(Net, self).__init__()
        self.embed = torch.nn.Embedding(20, 4)
        self.proj = torch.nn.Linear(4, 8)
26

27
28
29
30
31
32
33
34
35
36
    def forward(self, x):
        # move input to cpu and restore output
        current_dev = x.device
        x = x.to('cpu')
        x = self.embed(x)
        x = x.to(current_dev)

        x = self.proj(x)
        return x

37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def run_hybrid_device(use_ddp):
    with ColoInitContext(device=get_current_device()):
        model = Net()

    real_model = model
    if use_ddp:
        model = ColoDDP(model)
        real_model = model.module

    print(f'embedding weight size: {real_model.embed.weight.size()} | device: {real_model.embed.weight.device}')
    #print(f'linear weight size: {real_model.proj.weight.size()} | device: {real_model.proj.weight.device}')
    parallel_action = ParallelAction(ComputePattern.TP1D)
    init_colo_module(model, parallel_action, recursive=True, mode='col')

    # use cpu gloo to handle embedding
    real_model.embed.to('cpu')
    gloo_group_tp = gpc.get_cpu_group(ParallelMode.PARALLEL_1D)
    real_model.embed.weight.spec.dist_spec.process_group = gloo_group_tp

    print(f'embedding weight size: {real_model.embed.weight.size()} | new device: {real_model.embed.weight.device}')
    #print(f'linear weight size: {real_model.proj.weight.size()} | new device: {real_model.proj.weight.device}')
59

60
    optimizer = ColoOptimizer(dict(model.named_parameters()), torch.optim.SGD, lr=0.1)
61
62
63
    data = torch.randint(low=0, high=20, size=(16,), device=get_current_device())
    out = model(data)
    out.sum().backward()
64
    optimizer.step()
65

66
67
68
69
70
71
72
73
def run_dist(rank, world_size, port, use_ddp):
    if use_ddp and world_size == 1:
        return
    tp_world_size = world_size // 2 if use_ddp else world_size
    config = dict(parallel=dict(tensor=dict(mode="1d", size=tp_world_size),))
    colossalai.launch(config=config, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
    run_hybrid_device(use_ddp)

74

75
76
77
78
79
80
81
82
83
@pytest.mark.dist
@pytest.mark.parametrize('world_size', [1, 4])
@pytest.mark.parametrize('use_ddp', [False, True])
@rerun_if_address_is_in_use()
# Working for simulate the embedding(CPU DP+TP) -> nn(GPU DP+TP)
def _test_hybrid_device(world_size, use_ddp):
    run_func = partial(run_dist, world_size=world_size, port=free_port(), use_ddp=use_ddp)
    mp.spawn(run_func, nprocs=world_size)

84

85
if __name__ == '__main__':
86
    _test_hybrid_device(4, True)