test_sum_op.py 7.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

17 18 19
import unittest
import numpy as np
from op_test import OpTest
T
tangwei12 已提交
20 21
import paddle.fluid.core as core
from paddle.fluid.op import Operator
22 23 24 25 26


class TestSumOp(OpTest):
    def setUp(self):
        self.op_type = "sum"
C
chengduo 已提交
27
        self.init_kernel_type()
28 29
        self.use_mkldnn = False
        self.init_kernel_type()
C
chengduo 已提交
30 31 32
        x0 = np.random.random((3, 4)).astype(self.dtype)
        x1 = np.random.random((3, 4)).astype(self.dtype)
        x2 = np.random.random((3, 4)).astype(self.dtype)
33
        self.inputs = {"X": [("x0", x0), ("x1", x1), ("x2", x2)]}
34 35
        y = x0 + x1 + x2
        self.outputs = {'Out': y}
36
        self.attrs = {'use_mkldnn': self.use_mkldnn}
37

C
chengduo 已提交
38 39 40
    def init_kernel_type(self):
        self.dtype = np.float32

41
    def test_check_output(self):
Q
qijun 已提交
42
        self.check_output()
43 44

    def test_check_grad(self):
Q
qijun 已提交
45
        self.check_grad(['x0'], 'Out')
46

47 48 49
    def init_kernel_type(self):
        pass

50

51
class TestSelectedRowsSumOp(unittest.TestCase):
C
chengduo 已提交
52
    def setUp(self):
Q
qiaolongfei 已提交
53 54 55
        self.height = 10
        self.row_numel = 12
        self.rows = [0, 1, 2, 3, 4, 5, 6]
C
chengduo 已提交
56 57
        self.dtype = np.float32
        self.init_kernel_type()
Q
qiaolongfei 已提交
58

C
chengduo 已提交
59
    def check_with_place(self, place, inplace):
Q
Qiao Longfei 已提交
60 61 62 63 64 65 66 67
        self.check_input_and_optput(core.Scope(), place, inplace, True, True,
                                    True)
        self.check_input_and_optput(core.Scope(), place, inplace, False, True,
                                    True)
        self.check_input_and_optput(core.Scope(), place, inplace, False, False,
                                    True)
        self.check_input_and_optput(core.Scope(), place, inplace, False, False,
                                    False)
T
tangwei12 已提交
68

C
chengduo 已提交
69
    def init_kernel_type(self):
C
chengduo 已提交
70
        pass
C
chengduo 已提交
71

C
chengduo 已提交
72 73 74 75
    def _get_array(self, rows, row_numel):
        array = np.ones((len(rows), row_numel)).astype(self.dtype)
        for i in range(len(rows)):
            array[i] *= rows[i]
Q
qiaolongfei 已提交
76 77
        return array

T
tangwei12 已提交
78 79 80
    def check_input_and_optput(self,
                               scope,
                               place,
Q
Qiao Longfei 已提交
81
                               inplace,
T
tangwei12 已提交
82 83 84 85 86 87 88
                               w1_has_data=False,
                               w2_has_data=False,
                               w3_has_data=False):

        self.create_selected_rows(scope, place, "W1", w1_has_data)
        self.create_selected_rows(scope, place, "W2", w2_has_data)
        self.create_selected_rows(scope, place, "W3", w3_has_data)
T
tangwei12 已提交
89 90

        # create Out Variable
Q
Qiao Longfei 已提交
91 92 93 94 95
        if inplace:
            out_var_name = "W1"
        else:
            out_var_name = "Out"
        out = scope.var(out_var_name).get_selected_rows()
T
tangwei12 已提交
96 97

        # create and run sum operator
Q
Qiao Longfei 已提交
98
        sum_op = Operator("sum", X=["W1", "W2", "W3"], Out=out_var_name)
T
tangwei12 已提交
99 100
        sum_op.run(scope, place)

T
tangwei12 已提交
101
        has_data_w_num = 0
Q
qiaolongfei 已提交
102 103
        for has_data in [w1_has_data, w2_has_data, w3_has_data]:
            if has_data:
T
tangwei12 已提交
104
                has_data_w_num += 1
T
tangwei12 已提交
105

Q
qiaolongfei 已提交
106 107 108 109 110
        if has_data_w_num > 0:
            self.assertEqual(len(out.rows()), 7)
            self.assertTrue(
                np.array_equal(
                    np.array(out.get_tensor()),
C
chengduo 已提交
111
                    self._get_array(self.rows, self.row_numel) *
Q
qiaolongfei 已提交
112 113 114
                    has_data_w_num))
        else:
            self.assertEqual(len(out.rows()), 0)
T
tangwei12 已提交
115

Q
qiaolongfei 已提交
116
    def create_selected_rows(self, scope, place, var_name, has_data):
T
tangwei12 已提交
117
        # create and initialize W Variable
Q
qiaolongfei 已提交
118 119
        if has_data:
            rows = self.rows
T
tangwei12 已提交
120 121 122 123 124
        else:
            rows = []

        var = scope.var(var_name)
        w_selected_rows = var.get_selected_rows()
Q
qiaolongfei 已提交
125
        w_selected_rows.set_height(self.height)
T
tangwei12 已提交
126
        w_selected_rows.set_rows(rows)
C
chengduo 已提交
127
        w_array = self._get_array(self.rows, self.row_numel)
T
tangwei12 已提交
128 129 130 131 132 133 134
        w_tensor = w_selected_rows.get_tensor()
        w_tensor.set(w_array, place)

        return var

    def test_w_is_selected_rows(self):
        places = [core.CPUPlace()]
Q
Qiao Longfei 已提交
135 136
        if core.is_compiled_with_cuda():
            places.append(core.CUDAPlace(0))
T
tangwei12 已提交
137
        for place in places:
Q
Qiao Longfei 已提交
138 139
            for inplace in [True, False]:
                self.check_with_place(place, inplace)
T
tangwei12 已提交
140 141


C
chengduo 已提交
142 143 144 145 146
class TestLoDTensorAndSelectedRowsOp(TestSelectedRowsSumOp):
    def setUp(self):
        self.height = 10
        self.row_numel = 12
        self.rows = [0, 1, 2, 2, 4, 5, 6]
147
        self.dtype = np.float32
C
chengduo 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    def check_with_place(self, place, inplace):
        scope = core.Scope()
        if inplace:
            self.create_lod_tensor(scope, place, "x1")
            self.create_selected_rows(scope, place, "x2", True)
            out = scope.var("x1").get_tensor()
            out_name = "x1"
        else:
            self.create_selected_rows(scope, place, "x1", True)
            self.create_lod_tensor(scope, place, "x2")
            out = scope.var("out").get_tensor()
            out_name = "out"

        # create and run sum operator
        sum_op = Operator("sum", X=["x1", "x2"], Out=out_name)
        sum_op.run(scope, place)

        result = np.ones((1, self.height)).astype(np.int32).tolist()[0]
        for ele in self.rows:
            result[ele] += 1

        out_t = np.array(out)
        self.assertEqual(out_t.shape[0], self.height)
        self.assertTrue(
            np.array_equal(out_t,
                           self._get_array([i for i in range(
                               self.height)], self.row_numel) * np.tile(
                                   np.array(result).reshape(self.height, 1),
                                   self.row_numel)))

    def create_lod_tensor(self, scope, place, var_name):
        var = scope.var(var_name)
        w_tensor = var.get_tensor()
        w_array = self._get_array([i for i in range(self.height)],
                                  self.row_numel)
        w_tensor.set(w_array, place)
        return var


#----------- test fp16 -----------
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
C
chengduo 已提交
191 192 193 194 195
class TestFP16SumOp(TestSumOp):
    def init_kernel_type(self):
        self.dtype = np.float16

    def test_check_output(self):
C
chengduo 已提交
196 197 198
        place = core.CUDAPlace(0)
        if core.is_float16_supported(place):
            self.check_output_with_place(place, atol=2e-2)
C
chengduo 已提交
199 200 201 202

    # FIXME: Because of the precision fp16, max_relative_error
    # should be 0.15 here.
    def test_check_grad(self):
C
chengduo 已提交
203 204 205
        place = core.CUDAPlace(0)
        if core.is_float16_supported(place):
            self.check_grad(['x0'], 'Out', max_relative_error=0.15)
C
chengduo 已提交
206 207


C
chengduo 已提交
208 209 210 211 212 213
def create_test_sum_fp16_class(parent):
    @unittest.skipIf(not core.is_compiled_with_cuda(),
                     "core is not compiled with CUDA")
    class TestSumFp16Case(parent):
        def init_kernel_type(self):
            self.dtype = np.float16
C
chengduo 已提交
214

C
chengduo 已提交
215
        def test_w_is_selected_rows(self):
C
chengduo 已提交
216 217 218 219 220
            place = core.CUDAPlace(0)
            if core.is_float16_supported(place):
                for inplace in [True, False]:
                    self.check_with_place(place, inplace)

C
chengduo 已提交
221 222 223 224 225 226 227
    cls_name = "{0}_{1}".format(parent.__name__, "SumFp16Test")
    TestSumFp16Case.__name__ = cls_name
    globals()[cls_name] = TestSumFp16Case


create_test_sum_fp16_class(TestSelectedRowsSumOp)
create_test_sum_fp16_class(TestLoDTensorAndSelectedRowsOp)
C
chengduo 已提交
228

Q
qijun 已提交
229
if __name__ == "__main__":
230
    unittest.main()