test_linear.py 4.27 KB
Newer Older
yuguo960516's avatar
bloom  
yuguo960516 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# coding=utf-8
# Copyright 2021 The OneFlow Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest

import numpy as np
import oneflow as flow
import oneflow.unittest
from omegaconf import DictConfig
from oneflow import nn

from libai.layers import Linear
from libai.utils import distributed as dist


class TestLinear(flow.unittest.TestCase):
    @unittest.skipIf(not flow.cuda.is_available(), "only test gpu cases")
    @flow.unittest.skip_unless_1n1d()
    def test_nn_linear(self):
        dist.setup_dist_util(
            DictConfig(
                dict(
                    data_parallel_size=1,
                    tensor_parallel_size=1,
                    pipeline_parallel_size=1,
                )
            )
        )

        inputs = flow.rand(8, 8, sbp=flow.sbp.broadcast, placement=dist.get_layer_placement(0))
        weight = flow.rand(4, 8, sbp=flow.sbp.broadcast, placement=dist.get_layer_placement(0))
        bias = flow.rand(4, sbp=flow.sbp.broadcast, placement=dist.get_layer_placement(0))

        nn_linear = nn.Linear(8, 4).to("cuda")
        nn_linear.weight.data.copy_(dist.ttol(weight).to("cuda"))
        nn_linear.bias.data.copy_(dist.ttol(bias).to("cuda"))
        nn_output = nn_linear(dist.ttol(inputs).to("cuda"))

        libai_linear = Linear(8, 4)
        libai_linear.weight.data.copy_(weight)
        libai_linear.bias.data.copy_(bias)
        libai_output = libai_linear(inputs)

        self.assertTrue(np.allclose(nn_output.cpu().numpy(), dist.tton(libai_output), 1e-7, 1e-7))

    @flow.unittest.skip_unless_1n2d()
    def test_col_parallel_linear(self):
        dist.setup_dist_util(
            DictConfig(
                dict(
                    data_parallel_size=1,
                    tensor_parallel_size=2,
                    pipeline_parallel_size=1,
                )
            )
        )

        inputs = flow.rand(8, 8, sbp=flow.sbp.broadcast, placement=dist.get_layer_placement(0))
        weight = flow.rand(4, 8, sbp=flow.sbp.split(0), placement=dist.get_layer_placement(0))
        bias = flow.rand(4, sbp=flow.sbp.split(0), placement=dist.get_layer_placement(0))

        nn_linear = nn.Linear(8, 4).to("cuda")
        nn_linear.weight.data.copy_(dist.ttol(weight).to("cuda"))
        nn_linear.bias.data.copy_(dist.ttol(bias).to("cuda"))
        nn_output = nn_linear(dist.ttol(inputs).to("cuda"))

        libai_linear = Linear(8, 4, parallel="col")
        libai_linear.weight.data.copy_(weight)
        libai_linear.bias.data.copy_(bias)
        libai_output = libai_linear(inputs)

        self.assertTrue(np.allclose(nn_output.cpu().numpy(), dist.tton(libai_output), 1e-7, 1e-7))

    @flow.unittest.skip_unless_1n2d()
    def test_row_parallel_linear(self):
        dist.setup_dist_util(
            DictConfig(
                dict(
                    data_parallel_size=1,
                    tensor_parallel_size=2,
                    pipeline_parallel_size=1,
                )
            )
        )

        inputs = flow.rand(8, 8, sbp=flow.sbp.broadcast, placement=dist.get_layer_placement(0))
        weight = flow.rand(4, 8, sbp=flow.sbp.split(1), placement=dist.get_layer_placement(0))
        bias = flow.rand(4, sbp=flow.sbp.broadcast, placement=dist.get_layer_placement(0))

        # move local tensor to cuda
        nn_linear = nn.Linear(8, 4).to("cuda")
        nn_linear.weight.data.copy_(dist.ttol(weight).to("cuda"))
        nn_linear.bias.data.copy_(dist.ttol(bias).to("cuda"))
        nn_output = nn_linear(dist.ttol(inputs).to("cuda"))

        libai_linear = Linear(8, 4, parallel="row")
        libai_linear.weight.data.copy_(weight)
        libai_linear.bias.data.copy_(bias)
        libai_output = libai_linear(inputs)

        self.assertTrue(np.allclose(nn_output.cpu().numpy(), dist.tton(libai_output), 1e-7, 1e-7))