test_addcmul.py 7.3 KB
Newer Older
B
Bai Yifan 已提交
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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
#  Copyright (c) 2020 PaddlePaddle 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.

from __future__ import print_function
import unittest
import numpy as np
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.op import Operator
from paddle.fluid import compiler, Program, program_guard
from op_test import OpTest, skip_check_grad_ci


class TestAddcmulLayer(unittest.TestCase):
    def setUp(self):
        self._dtype = "float64"
        self.input = np.random.uniform(0.1, 1, [3, 100]).astype(self._dtype)
        self.tensor1 = np.random.uniform(0.1, 1, [100]).astype(self._dtype)
        self.tensor2 = np.random.uniform(0.1, 1, [3, 100]).astype(self._dtype)

    def static(self, value=1.0):
        prog = fluid.Program()
        with fluid.program_guard(prog):
            input = fluid.data(name="input", dtype=self._dtype, shape=[3, 100])
            tensor1 = fluid.data(name="tensor1", dtype=self._dtype, shape=[100])
            tensor2 = fluid.data(
                name="tensor2", dtype=self._dtype, shape=[3, 100])
            out = paddle.addcmul(input, tensor1, tensor2, value)

        exe = fluid.Executor(self._place)
        return exe.run(feed={
            "input": self.input,
            "tensor1": self.tensor1,
            "tensor2": self.tensor2
        },
                       program=prog,
                       fetch_list=[out])[0]

    def dynamic(self, value=1.0):
        with fluid.dygraph.guard(self._place):
            input = fluid.dygraph.to_variable(self.input)
            tensor1 = fluid.dygraph.to_variable(self.tensor1)
            tensor2 = fluid.dygraph.to_variable(self.tensor2)
            out = paddle.addcmul(input, tensor1, tensor2, value)
            return out.numpy()

    def numpy(self, value=1.0):
        self.out = np.add(self.input,
                          np.multiply(self.tensor1, self.tensor2) * value)
        return self.out

    def test_equal(self):
        places = []
        if fluid.core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for place in places:
            self._place = place
            self.assertTrue(np.allclose(self.numpy(), self.static()))
            self.assertTrue(
                np.allclose(
                    self.numpy(value=0.9), self.dynamic(value=0.9)))
            self.assertTrue(
                np.allclose(
                    self.numpy(value=0), self.dynamic(value=0)))


class TestAddcmul(unittest.TestCase):
    def test_addcmul(self):
        program = Program()
        with program_guard(program):
            data_shape = [3, 64, 64]
            input = fluid.data(name='in', shape=data_shape, dtype='float32')
            tensor1 = fluid.data(name='t1', shape=data_shape, dtype='float32')
            tensor2 = fluid.data(name='t2', shape=data_shape, dtype='float32')

            out = paddle.addcmul(input, tensor1, tensor2)
            self.assertEqual(out.shape, input.shape)

    def test_addcmul_with_broadcast0(self):
        program = Program()
        with program_guard(program):
            input = fluid.data(name='in', shape=[3, 100], dtype='float32')
            tensor1 = fluid.data(name='t1', shape=[3, 100], dtype='float32')
            tensor2 = fluid.data(name='t2', shape=[100], dtype='float32')

            out = paddle.addcmul(input, tensor1, tensor2)
            self.assertEqual(out.shape, input.shape)

    def test_addcmul_with_broadcast1(self):
        program = Program()
        with program_guard(program):
            input = fluid.data(name='in', shape=[4, 100], dtype='float32')
            tensor1 = fluid.data(name='t1', shape=[100], dtype='float32')
            tensor2 = fluid.data(name='t2', shape=[4, 100], dtype='float32')

            out = paddle.addcmul(input, tensor1, tensor2)
            self.assertEqual(out.shape, input.shape)

    def test_addcmul_with_broadcast2(self):
        program = Program()
        with program_guard(program):
            input = fluid.data(name='in', shape=[4, 100], dtype='float32')
            tensor1 = fluid.data(name='t1', shape=[100], dtype='float32')
            tensor2 = fluid.data(name='t2', shape=[100], dtype='float32')

            out = paddle.addcmul(input, tensor1, tensor2)
            self.assertEqual(out.shape, input.shape)


class InvalidInputTest(unittest.TestCase):
    def test_error(self):
        def test_invalid_input():
            program = Program()
            with program_guard(program):
                input = [20, 20]
                tensor1 = fluid.data(
                    name='tensor1', shape=[20, 20], dtype='float32')
                tensor2 = fluid.data(
                    name='tensor2', shape=[20, 20], dtype='float32')
                out = paddle.addcmul(input, tensor1, tensor2)

        self.assertRaises(TypeError, test_invalid_input)

        def test_invalid_tensor1():
            program = Program()
            with program_guard(program):
                input = fluid.data(
                    name='input', shape=[20, 20], dtype='float32')
                tensor1 = [20, 20]
                tensor2 = fluid.data(
                    name='tensor2', shape=[20, 20], dtype='float32')
                out = paddle.addcmul(input, tensor1, tensor2)

        self.assertRaises(TypeError, test_invalid_tensor1)

        def test_invalid_tensor2():
            program = Program()
            with program_guard(program):
                input = fluid.data(
                    name='input', shape=[20, 20], dtype='float32')
                tensor1 = fluid.data(
                    name='tensor1', shape=[20, 20], dtype='float32')
                tensor2 = [20, 20]
                out = paddle.addcmul(input, tensor1, tensor2)

        self.assertRaises(TypeError, test_invalid_tensor2)

        def test_invalid_value_int():
            program = Program()
            with program_guard(program):
                input = fluid.data(
                    name='input', shape=[20, 20], dtype='float32')
                tensor1 = fluid.data(
                    name='tensor1', shape=[20, 20], dtype='float32')
                tensor2 = fluid.data(
                    name='tensor2', shape=[20, 20], dtype='float32')
                out = paddle.addcmul(input, tensor1, tensor2, value=1)

        self.assertRaises(TypeError, test_invalid_value_int)

        def test_invalid_value_float():
            program = Program()
            with program_guard(program):
                input = fluid.data(name='input', shape=[20, 20], dtype='int32')
                tensor1 = fluid.data(
                    name='tensor1', shape=[20, 20], dtype='int32')
                tensor2 = fluid.data(
                    name='tensor2', shape=[20, 20], dtype='int32')
                out = paddle.addcmul(input, tensor1, tensor2, value=1.0)

        self.assertRaises(TypeError, test_invalid_value_float)


if __name__ == '__main__':
    unittest.main()