test_sum_op.py 15.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
20
import paddle
21
from paddle import enable_static
22
import paddle.fluid as fluid
T
tangwei12 已提交
23 24
import paddle.fluid.core as core
from paddle.fluid.op import Operator
25 26 27
from paddle.fluid.tests.unittests.op_test import (OpTest,
                                                  convert_float_to_uint16,
                                                  convert_uint16_to_float)
28
from paddle import _C_ops
29
from paddle.fluid.framework import _test_eager_guard
30 31 32


class TestSumOp(OpTest):
33

34 35
    def setUp(self):
        self.op_type = "sum"
C
chengduo 已提交
36
        self.init_kernel_type()
37 38
        self.use_mkldnn = False
        self.init_kernel_type()
Z
zhupengyang 已提交
39 40 41
        x0 = np.random.random((3, 40)).astype(self.dtype)
        x1 = np.random.random((3, 40)).astype(self.dtype)
        x2 = np.random.random((3, 40)).astype(self.dtype)
42
        self.inputs = {"X": [("x0", x0), ("x1", x1), ("x2", x2)]}
43 44
        y = x0 + x1 + x2
        self.outputs = {'Out': y}
45
        self.attrs = {'use_mkldnn': self.use_mkldnn}
46

C
chengduo 已提交
47
    def init_kernel_type(self):
48
        self.dtype = np.float64
C
chengduo 已提交
49

50
    def test_check_output(self):
Q
qijun 已提交
51
        self.check_output()
52 53

    def test_check_grad(self):
Q
qijun 已提交
54
        self.check_grad(['x0'], 'Out')
55 56


57
class TestSelectedRowsSumOp(unittest.TestCase):
58

C
chengduo 已提交
59
    def setUp(self):
Q
qiaolongfei 已提交
60 61 62
        self.height = 10
        self.row_numel = 12
        self.rows = [0, 1, 2, 3, 4, 5, 6]
63
        self.dtype = np.float64
C
chengduo 已提交
64
        self.init_kernel_type()
Q
qiaolongfei 已提交
65

C
chengduo 已提交
66
    def check_with_place(self, place, inplace):
Q
Qiao Longfei 已提交
67 68 69 70 71 72 73 74
        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 已提交
75

C
chengduo 已提交
76
    def init_kernel_type(self):
C
chengduo 已提交
77
        pass
C
chengduo 已提交
78

C
chengduo 已提交
79 80 81 82
    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 已提交
83 84
        return array

T
tangwei12 已提交
85 86 87
    def check_input_and_optput(self,
                               scope,
                               place,
Q
Qiao Longfei 已提交
88
                               inplace,
T
tangwei12 已提交
89 90 91 92 93 94 95
                               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 已提交
96 97

        # create Out Variable
Q
Qiao Longfei 已提交
98 99 100 101 102
        if inplace:
            out_var_name = "W1"
        else:
            out_var_name = "Out"
        out = scope.var(out_var_name).get_selected_rows()
T
tangwei12 已提交
103 104

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

T
tangwei12 已提交
108
        has_data_w_num = 0
Q
qiaolongfei 已提交
109 110
        for has_data in [w1_has_data, w2_has_data, w3_has_data]:
            if has_data:
T
tangwei12 已提交
111
                has_data_w_num += 1
T
tangwei12 已提交
112

Q
qiaolongfei 已提交
113 114 115 116 117
        if has_data_w_num > 0:
            self.assertEqual(len(out.rows()), 7)
            self.assertTrue(
                np.array_equal(
                    np.array(out.get_tensor()),
C
chengduo 已提交
118
                    self._get_array(self.rows, self.row_numel) *
Q
qiaolongfei 已提交
119 120 121
                    has_data_w_num))
        else:
            self.assertEqual(len(out.rows()), 0)
T
tangwei12 已提交
122

Q
qiaolongfei 已提交
123
    def create_selected_rows(self, scope, place, var_name, has_data):
T
tangwei12 已提交
124
        # create and initialize W Variable
Q
qiaolongfei 已提交
125 126
        if has_data:
            rows = self.rows
T
tangwei12 已提交
127 128 129 130 131
        else:
            rows = []

        var = scope.var(var_name)
        w_selected_rows = var.get_selected_rows()
Q
qiaolongfei 已提交
132
        w_selected_rows.set_height(self.height)
T
tangwei12 已提交
133
        w_selected_rows.set_rows(rows)
C
chengduo 已提交
134
        w_array = self._get_array(self.rows, self.row_numel)
T
tangwei12 已提交
135 136 137 138 139 140 141
        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 已提交
142 143
        if core.is_compiled_with_cuda():
            places.append(core.CUDAPlace(0))
T
tangwei12 已提交
144
        for place in places:
Q
Qiao Longfei 已提交
145 146
            for inplace in [True, False]:
                self.check_with_place(place, inplace)
T
tangwei12 已提交
147 148


149
class TestSelectedRowsSumOpInt(TestSelectedRowsSumOp):
150

151 152 153 154 155 156 157
    def init_kernel_type(self):
        self.dtype = np.int32


@unittest.skipIf(not core.supports_bfloat16(),
                 'place does not support BF16 evaluation')
class TestSelectedRowsSumBF16Op(TestSelectedRowsSumOp):
158

159 160 161 162 163 164 165
    def setUp(self):
        self.height = 10
        self.row_numel = 12
        self.rows = [0, 1, 2, 3, 4, 5, 6]
        self.dtype = np.uint16
        self.init_kernel_type()
        np.random.seed(12345)
166 167
        self.data = np.random.random(
            (len(self.rows), self.row_numel)).astype(np.float32)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    def _get_array(self, rows, row_numel):
        if len(rows) > 0:
            return convert_float_to_uint16(self.data)
        else:
            return np.ndarray((0, row_numel), dtype=self.dtype)

    def check_input_and_optput(self,
                               scope,
                               place,
                               inplace,
                               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)

        # create Out Variable
        if inplace:
            out_var_name = "W1"
        else:
            out_var_name = "Out"
        out = scope.var(out_var_name).get_selected_rows()

        # create and run sum operator
        sum_op = Operator("sum", X=["W1", "W2", "W3"], Out=out_var_name)
        sum_op.run(scope, place)

        has_data_w_num = 0
        for has_data in [w1_has_data, w2_has_data, w3_has_data]:
            if has_data:
                has_data_w_num += 1

        if has_data_w_num > 0:
            self.assertEqual(len(out.rows()), 7)
            out_bf16 = np.array(out.get_tensor())
            out_fp32 = convert_uint16_to_float(out_bf16)
            ref_fp32 = convert_uint16_to_float(
                self._get_array(self.rows, self.row_numel)) * has_data_w_num
            np.testing.assert_allclose(out_fp32, ref_fp32, atol=0, rtol=0.95e-2)
        else:
            self.assertEqual(len(out.rows()), 0)

    def test_w_is_selected_rows(self):
        for inplace in [True, False]:
            self.check_with_place(core.CPUPlace(), inplace)


L
lidanqing 已提交
218
class TestSelectedRowsSumBF16OpBigRow(TestSelectedRowsSumBF16Op):
219

L
lidanqing 已提交
220 221 222 223
    def init_kernel_type(self):
        self.row_numel = 102


C
chengduo 已提交
224
class TestLoDTensorAndSelectedRowsOp(TestSelectedRowsSumOp):
225

C
chengduo 已提交
226 227 228 229
    def setUp(self):
        self.height = 10
        self.row_numel = 12
        self.rows = [0, 1, 2, 2, 4, 5, 6]
230
        self.dtype = np.float64
C
chengduo 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    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(
256 257 258 259 260 261
            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)))
C
chengduo 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274

    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 已提交
275
class TestFP16SumOp(TestSumOp):
276

C
chengduo 已提交
277 278 279 280
    def init_kernel_type(self):
        self.dtype = np.float16

    def test_check_output(self):
C
chengduo 已提交
281 282 283
        place = core.CUDAPlace(0)
        if core.is_float16_supported(place):
            self.check_output_with_place(place, atol=2e-2)
C
chengduo 已提交
284 285 286 287

    # FIXME: Because of the precision fp16, max_relative_error
    # should be 0.15 here.
    def test_check_grad(self):
C
chengduo 已提交
288 289 290
        place = core.CUDAPlace(0)
        if core.is_float16_supported(place):
            self.check_grad(['x0'], 'Out', max_relative_error=0.15)
C
chengduo 已提交
291 292


C
chengduo 已提交
293
def create_test_sum_fp16_class(parent):
294

C
chengduo 已提交
295 296 297
    @unittest.skipIf(not core.is_compiled_with_cuda(),
                     "core is not compiled with CUDA")
    class TestSumFp16Case(parent):
298

C
chengduo 已提交
299 300
        def init_kernel_type(self):
            self.dtype = np.float16
C
chengduo 已提交
301

C
chengduo 已提交
302
        def test_w_is_selected_rows(self):
C
chengduo 已提交
303 304 305 306 307
            place = core.CUDAPlace(0)
            if core.is_float16_supported(place):
                for inplace in [True, False]:
                    self.check_with_place(place, inplace)

C
chengduo 已提交
308 309 310 311 312
    cls_name = "{0}_{1}".format(parent.__name__, "SumFp16Test")
    TestSumFp16Case.__name__ = cls_name
    globals()[cls_name] = TestSumFp16Case


313 314
#----------- test bf16 -----------
class TestSumBF16Op(OpTest):
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    def setUp(self):
        self.op_type = "sum"
        self.init_kernel_type()
        x0 = np.random.random((3, 40)).astype(np.float32)
        x1 = np.random.random((3, 40)).astype(np.float32)
        x2 = np.random.random((3, 40)).astype(np.float32)
        y = x0 + x1 + x2
        self.inputs = {
            "X": [("x0", convert_float_to_uint16(x0)),
                  ("x1", convert_float_to_uint16(x1)),
                  ("x2", convert_float_to_uint16(x2))]
        }
        self.outputs = {'Out': convert_float_to_uint16(y)}

    def init_kernel_type(self):
        self.dtype = np.uint16

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['x0'], 'Out', numeric_grad_delta=0.5)


S
Steffy-zxf 已提交
340
class API_Test_Add_n(unittest.TestCase):
341

342 343
    def test_api(self):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
344 345 346 347 348 349
            input0 = fluid.layers.fill_constant(shape=[2, 3],
                                                dtype='int64',
                                                value=5)
            input1 = fluid.layers.fill_constant(shape=[2, 3],
                                                dtype='int64',
                                                value=3)
350 351
            expected_result = np.empty((2, 3))
            expected_result.fill(8)
S
Steffy-zxf 已提交
352
            sum_value = paddle.add_n([input0, input1])
353 354 355
            exe = fluid.Executor(fluid.CPUPlace())
            result = exe.run(fetch_list=[sum_value])

S
Steffy-zxf 已提交
356 357 358 359 360 361 362 363 364
            self.assertEqual((result == expected_result).all(), True)

        with fluid.dygraph.guard():
            input0 = paddle.ones(shape=[2, 3], dtype='float32')
            expected_result = np.empty((2, 3))
            expected_result.fill(2)
            sum_value = paddle.add_n([input0, input0])

            self.assertEqual((sum_value.numpy() == expected_result).all(), True)
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    def test_dygraph_final_state_api(self):
        with fluid.dygraph.guard():
            with _test_eager_guard():
                input0 = paddle.ones(shape=[2, 3], dtype='float32')
                input1 = paddle.ones(shape=[2, 3], dtype='float32')
                input0.stop_gradient = False
                input1.stop_gradient = False
                expected_result = np.empty((2, 3))
                expected_result.fill(2)
                sum_value = paddle.add_n([input0, input1])
                self.assertEqual((sum_value.numpy() == expected_result).all(),
                                 True)

                expected_grad_result = np.empty((2, 3))
                expected_grad_result.fill(1)
                sum_value.backward()
                self.assertEqual(
                    (input0.grad.numpy() == expected_grad_result).all(), True)
                self.assertEqual(
                    (input1.grad.numpy() == expected_grad_result).all(), True)

387

388
class TestRaiseSumError(unittest.TestCase):
389

390
    def test_errors(self):
391

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
        def test_type():
            fluid.layers.sum([11, 22])

        self.assertRaises(TypeError, test_type)

        def test_dtype():
            data1 = fluid.data(name="input1", shape=[10], dtype="int8")
            data2 = fluid.data(name="input2", shape=[10], dtype="int8")
            fluid.layers.sum([data1, data2])

        self.assertRaises(TypeError, test_dtype)

        def test_dtype1():
            data1 = fluid.data(name="input1", shape=[10], dtype="int8")
            fluid.layers.sum(data1)

        self.assertRaises(TypeError, test_dtype1)


class TestRaiseSumsError(unittest.TestCase):
412

413
    def test_errors(self):
414

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
        def test_type():
            fluid.layers.sums([11, 22])

        self.assertRaises(TypeError, test_type)

        def test_dtype():
            data1 = fluid.data(name="input1", shape=[10], dtype="int8")
            data2 = fluid.data(name="input2", shape=[10], dtype="int8")
            fluid.layers.sums([data1, data2])

        self.assertRaises(TypeError, test_dtype)

        def test_dtype1():
            data1 = fluid.data(name="input1", shape=[10], dtype="int8")
            fluid.layers.sums(data1)

        self.assertRaises(TypeError, test_dtype1)

        def test_out_type():
            data1 = fluid.data(name="input1", shape=[10], dtype="flaot32")
            data2 = fluid.data(name="input2", shape=[10], dtype="float32")
            fluid.layers.sums([data1, data2], out=[10])

        self.assertRaises(TypeError, test_out_type)

        def test_out_dtype():
            data1 = fluid.data(name="input1", shape=[10], dtype="flaot32")
            data2 = fluid.data(name="input2", shape=[10], dtype="float32")
            out = fluid.data(name="out", shape=[10], dtype="int8")
            fluid.layers.sums([data1, data2], out=out)

        self.assertRaises(TypeError, test_out_dtype)


L
Leo Chen 已提交
449
class TestSumOpError(unittest.TestCase):
450

L
Leo Chen 已提交
451
    def test_errors(self):
452

L
Leo Chen 已提交
453 454
        def test_empty_list_input():
            with fluid.dygraph.guard():
455
                fluid._C_ops.sum([])
L
Leo Chen 已提交
456 457 458

        def test_list_of_none_input():
            with fluid.dygraph.guard():
459
                fluid._C_ops.sum([None])
L
Leo Chen 已提交
460 461 462 463 464

        self.assertRaises(Exception, test_empty_list_input)
        self.assertRaises(Exception, test_list_of_none_input)


C
chengduo 已提交
465 466
create_test_sum_fp16_class(TestSelectedRowsSumOp)
create_test_sum_fp16_class(TestLoDTensorAndSelectedRowsOp)
C
chengduo 已提交
467

Q
qijun 已提交
468
if __name__ == "__main__":
469
    enable_static()
470
    unittest.main()