test_linear_tp.py 5.43 KB
Newer Older
Ziyue Jiang's avatar
Ziyue Jiang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import torch
from colossalai.context.parallel_mode import ParallelMode
from colossalai.tensor import ColoTensor

from functools import partial

import colossalai
import pytest
import torch
import torch.multiprocessing as mp
from colossalai.testing import parameterize, rerun_if_address_is_in_use
from colossalai.utils.cuda import get_current_device
from colossalai.utils import free_port
from colossalai.core import global_context as gpc
15
from colossalai.tensor import TensorSpec, ComputePattern, ParallelAction
Ziyue Jiang's avatar
Ziyue Jiang committed
16

17
from _utils import check_equal, replace_parameter_add_grad, broadcast_tensor_chunk
Ziyue Jiang's avatar
Ziyue Jiang committed
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
def run_linear_tp1d_col_test():
    device = get_current_device()
    dtype = torch.float32
    DEPTH = gpc.get_world_size(ParallelMode.PARALLEL_1D)
    in_features = 4
    out_features = 8

    local_rank = gpc.get_local_rank(ParallelMode.PARALLEL_1D)

    layer_master = torch.nn.Linear(in_features, out_features)
    layer = torch.nn.Linear(in_features, out_features)

    A_shape = (2, in_features)
    A_master = torch.randn(A_shape, dtype=dtype, device=device)
    A = broadcast_tensor_chunk(A_master, chunk_size=1)
    A.requires_grad = True

    W_shape = (out_features, in_features)
    W_master = torch.randn(W_shape, dtype=dtype, device=device)
    W = broadcast_tensor_chunk(W_master, chunk_size=1)
    W.requires_grad = True

    B_shape = (out_features)
    B_master = torch.randn(B_shape, dtype=dtype, device=device)
    B = broadcast_tensor_chunk(B_master, chunk_size=1)
    B.requires_grad = True

    # replace the torch nn.Parameters with ColoTensor
    sharded_weight = ColoTensor.init_from_torch_tensor(W)
    sharded_bias = ColoTensor.init_from_torch_tensor(B)
    parallel_action_list = [
        ParallelAction(priority=1, compute_pattern=ComputePattern.TP1DCol, parallel_mode=ParallelMode.PARALLEL_1D)
    ]
    spec = TensorSpec(parallel_action_list)
    sharded_weight.set_spec(spec) # reshard
    sharded_bias.set_spec(spec)

    replace_parameter_add_grad(layer, sharded_weight, sharded_bias)
    out = layer(A)

    replace_parameter_add_grad(layer_master, W_master, B_master)
    A_master.requires_grad = True
    #C_master = torch.matmul(A_master, W_master.transpose(0, 1)) + B_master
    C_master = layer_master(A_master)
    C = C_master.clone()

    check_equal(out, C)

    grad_shape = C_master.shape
    grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
    grad = broadcast_tensor_chunk(grad_master, chunk_size=1)
    out.backward(grad)

    grad_master = grad_master.clone()
    C_master.backward(grad_master)

    W_grad = W_master.grad
    W_grad = torch.chunk(W_grad, DEPTH, dim=0)[local_rank]
    check_equal(W_grad, layer.weight.grad)

    B_grad = B_master.grad
    B_grad = torch.chunk(B_grad, DEPTH, dim=0)[local_rank]
    check_equal(B_grad, layer.bias.grad)
82

Ziyue Jiang's avatar
Ziyue Jiang committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
def run_linear_tp1d_row_test():
    device = get_current_device()
    dtype = torch.float32
    DEPTH = gpc.get_world_size(ParallelMode.PARALLEL_1D)
    in_features = 4
    out_features = 5

    local_rank = gpc.get_local_rank(ParallelMode.PARALLEL_1D)

    layer_master = torch.nn.Linear(in_features, out_features)
    layer = torch.nn.Linear(in_features, out_features)

    A_shape = (2, in_features)
    A_master = torch.randn(A_shape, dtype=dtype, device=device)
    A = broadcast_tensor_chunk(A_master, chunk_size=1)
    A.requires_grad = True

    W_shape = (out_features, in_features)
    W_master = torch.randn(W_shape, dtype=dtype, device=device)
102
    W = broadcast_tensor_chunk(W_master, chunk_size=1)
Ziyue Jiang's avatar
Ziyue Jiang committed
103
104
105
106
107
108
109
110
111
    W.requires_grad = True

    B_shape = (out_features)
    B_master = torch.randn(B_shape, dtype=dtype, device=device)
    B = broadcast_tensor_chunk(B_master, chunk_size=1)
    B.requires_grad = True

    # replace the torch nn.Parameters with ColoTensor
    sharded_weight = ColoTensor.init_from_torch_tensor(W)
112
113
114
115
116
    parallel_action_list = [
        ParallelAction(priority=1, compute_pattern=ComputePattern.TP1DRow, parallel_mode=ParallelMode.PARALLEL_1D)
    ]
    spec = TensorSpec(parallel_action_list)
    sharded_weight.set_spec(spec=spec) # reshard
Ziyue Jiang's avatar
Ziyue Jiang committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    sharded_bias = ColoTensor.init_from_torch_tensor(B)
    replace_parameter_add_grad(layer, sharded_weight, sharded_bias)
    out = layer(A)

    replace_parameter_add_grad(layer_master, W_master, B_master)
    A_master.requires_grad = True
    #C_master = torch.matmul(A_master, W_master.transpose(0, 1)) + B_master
    C_master = layer_master(A_master)
    C = C_master.clone()

    check_equal(out, C)

    grad_shape = C_master.shape
    grad_master = torch.randn(grad_shape, dtype=dtype, device=get_current_device())
    grad = broadcast_tensor_chunk(grad_master, chunk_size=1)
    out.backward(grad)

    grad_master = grad_master.clone()
    C_master.backward(grad_master)

    W_grad = W_master.grad
    W_grad = torch.chunk(W_grad, DEPTH, dim=-1)[local_rank]
    check_equal(W_grad, layer.weight.grad)

    B_grad = B_master.grad
    check_equal(B_grad, layer.bias.grad)

144

Ziyue Jiang's avatar
Ziyue Jiang committed
145
146
147
148
def run_dist(rank, world_size, port):
    config = dict(parallel=dict(tensor=dict(mode="1d", size=world_size),))
    colossalai.launch(config=config, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
    run_linear_tp1d_row_test()
149
    run_linear_tp1d_col_test()
Ziyue Jiang's avatar
Ziyue Jiang committed
150
151
152
153
154
155
156
157
158
159
160

@pytest.mark.dist
@parameterize('world_size', [1, 4])
@rerun_if_address_is_in_use()
def test_linear_1d(world_size):
    run_func = partial(run_dist, world_size=world_size, port=free_port())
    mp.spawn(run_func, nprocs=world_size)


if __name__ == '__main__':
    test_linear_1d()