test_sum_op.py 3.3 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 17
import unittest
import numpy as np
from op_test import OpTest
T
tangwei12 已提交
18 19
import paddle.fluid.core as core
from paddle.fluid.op import Operator
20 21 22 23 24


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

    def test_check_output(self):
Q
qijun 已提交
36
        self.check_output()
37 38

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

41 42 43
    def init_kernel_type(self):
        pass

44

T
tangwei12 已提交
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
class TestSelectedRowsSumOp(OpTest):
    def check_with_place(self, place):
        scope = core.Scope()
        self.check_input_and_optput(scope, place, True, True, True)
        self.check_input_and_optput(scope, place, False, True, True)
        self.check_input_and_optput(scope, place, False, False, True)
        self.check_input_and_optput(scope, place, False, False, False)

    def check_input_and_optput(self, scope, place, w1=False, w2=False,
                               w3=False):
        W1 = self.create_selected_rows(scope, place, "W1", w1)
        W2 = self.create_selected_rows(scope, place, "W2", w2)
        W3 = self.create_selected_rows(scope, place, "W3", w3)

        # create Out Variable
        out = scope.var('Out').get_selected_rows()

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

        trues = 0
        for w in [w1, w2, w3]:
            if not w:
                trues += 1

        self.assertEqual(7 * trues, len(out.rows()))

    def create_selected_rows(self, scope, place, var_name, isEmpty):
        # create and initialize W Variable
        if not isEmpty:
            rows = [0, 1, 2, 3, 4, 5, 6]
            row_numel = 12
        else:
            rows = []
            row_numel = 12

        var = scope.var(var_name)
        w_selected_rows = var.get_selected_rows()
        w_selected_rows.set_height(len(rows))
        w_selected_rows.set_rows(rows)
        w_array = np.ones((len(rows), row_numel)).astype("float32")
        for i in range(len(rows)):
            w_array[i] *= i
        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()]
        # currently only support CPU
        for place in places:
            self.check_with_place(place)


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