test_sum_op.py 4.2 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"
27 28
        self.use_mkldnn = False
        self.init_kernel_type()
29 30 31
        x0 = np.random.random((3, 4)).astype('float32')
        x1 = np.random.random((3, 4)).astype('float32')
        x2 = np.random.random((3, 4)).astype('float32')
32
        self.inputs = {"X": [("x0", x0), ("x1", x1), ("x2", x2)]}
33 34
        y = x0 + x1 + x2
        self.outputs = {'Out': y}
35
        self.attrs = {'use_mkldnn': self.use_mkldnn}
36 37

    def test_check_output(self):
Q
qijun 已提交
38
        self.check_output()
39 40

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

43 44 45
    def init_kernel_type(self):
        pass

46

T
tangwei12 已提交
47 48 49
class TestSelectedRowsSumOp(OpTest):
    def check_with_place(self, place):
        scope = core.Scope()
Q
qiaolongfei 已提交
50 51 52 53 54

        self.height = 10
        self.row_numel = 12
        self.rows = [0, 1, 2, 3, 4, 5, 6]

T
tangwei12 已提交
55 56 57 58 59
        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)

Q
qiaolongfei 已提交
60 61 62 63 64 65
    def _get_array(self, row_num, row_numel):
        array = np.ones((row_num, row_numel)).astype("float32")
        for i in range(row_num):
            array[i] *= i
        return array

T
tangwei12 已提交
66 67 68 69 70 71 72 73 74 75
    def check_input_and_optput(self,
                               scope,
                               place,
                               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 已提交
76 77 78 79 80 81 82 83

        # 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)

T
tangwei12 已提交
84
        has_data_w_num = 0
Q
qiaolongfei 已提交
85 86
        for has_data in [w1_has_data, w2_has_data, w3_has_data]:
            if has_data:
T
tangwei12 已提交
87
                has_data_w_num += 1
T
tangwei12 已提交
88

Q
qiaolongfei 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101
        if has_data_w_num > 0:
            self.assertEqual(len(out.rows()), 7)
            self.assertTrue(
                np.array_equal(
                    np.array(out.get_tensor()),
                    self._get_array(len(self.rows), self.row_numel) *
                    has_data_w_num))
        else:
            self.assertEqual(len(out.rows()), 0)
            self.assertTrue(
                np.array_equal(
                    np.array(out.get_tensor()),
                    self._get_array(0, self.row_numel) * has_data_w_num))
T
tangwei12 已提交
102

Q
qiaolongfei 已提交
103
    def create_selected_rows(self, scope, place, var_name, has_data):
T
tangwei12 已提交
104
        # create and initialize W Variable
Q
qiaolongfei 已提交
105 106
        if has_data:
            rows = self.rows
T
tangwei12 已提交
107 108 109 110 111
        else:
            rows = []

        var = scope.var(var_name)
        w_selected_rows = var.get_selected_rows()
Q
qiaolongfei 已提交
112
        w_selected_rows.set_height(self.height)
T
tangwei12 已提交
113
        w_selected_rows.set_rows(rows)
Q
qiaolongfei 已提交
114
        w_array = self._get_array(len(rows), self.row_numel)
T
tangwei12 已提交
115 116 117 118 119 120 121 122 123 124 125 126
        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 已提交
127
if __name__ == "__main__":
128
    unittest.main()