test_multiply.py 9.5 KB
Newer Older
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
# 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 paddle
import paddle.tensor as tensor
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
import numpy as np
import unittest


class TestMultiplyAPI(unittest.TestCase):
    """TestMultiplyAPI."""

    def __run_static_graph_case(self, x_data, y_data, axis=-1):
        with program_guard(Program(), Program()):
29
            paddle.enable_static()
30 31 32 33
            x = paddle.static.data(
                name='x', shape=x_data.shape, dtype=x_data.dtype)
            y = paddle.static.data(
                name='y', shape=y_data.shape, dtype=y_data.dtype)
34 35 36 37 38 39 40 41 42 43 44 45
            res = tensor.multiply(x, y, axis=axis)

            place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
            ) else fluid.CPUPlace()
            exe = fluid.Executor(place)
            outs = exe.run(fluid.default_main_program(),
                           feed={'x': x_data,
                                 'y': y_data},
                           fetch_list=[res])
            res = outs[0]
            return res

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    def __run_static_graph_case_with_numpy_input(self, x_data, y_data, axis=-1):
        with program_guard(Program(), Program()):
            paddle.enable_static()

            res = tensor.multiply(x_data, y_data, axis=axis)
            place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
            ) else fluid.CPUPlace()
            exe = fluid.Executor(place)
            outs = exe.run(fluid.default_main_program(),
                           feed={'x': x_data,
                                 'y': y_data},
                           fetch_list=[res])
            res = outs[0]
            return res

61
    def __run_dynamic_graph_case(self, x_data, y_data, axis=-1):
62
        paddle.disable_static()
63 64
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
65 66 67
        res = paddle.multiply(x, y, axis=axis)
        return res.numpy()

68 69 70 71 72 73
    def __run_dynamic_graph_case_with_numpy_input(self, x_data, y_data,
                                                  axis=-1):
        paddle.disable_static()
        res = paddle.multiply(x_data, y_data, axis=axis)
        return res.numpy()

74 75 76
    def test_multiply(self):
        """test_multiply."""
        np.random.seed(7)
77

78 79 80 81 82 83
        # test static computation graph: 1-d array
        x_data = np.random.rand(200)
        y_data = np.random.rand(200)
        res = self.__run_static_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

84 85 86 87 88 89
        # test static computation graph: 1-d array
        x_data = np.random.rand(200)
        y_data = np.random.rand(200)
        res = self.__run_static_graph_case_with_numpy_input(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

90 91 92 93 94 95
        # test static computation graph: 2-d array
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(2, 500)
        res = self.__run_static_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

96 97 98 99 100 101
        # test static computation graph with_primitives: 2-d array
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(2, 500)
        res = self.__run_static_graph_case_with_numpy_input(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

102 103 104 105 106 107
        # test static computation graph: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
        res = self.__run_static_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

108 109 110 111 112 113
        # test static computation graph with_primitives: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
        res = self.__run_static_graph_case_with_numpy_input(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

114 115 116 117 118 119 120
        # test static computation graph: broadcast with axis
        x_data = np.random.rand(2, 300, 40)
        y_data = np.random.rand(300)
        res = self.__run_static_graph_case(x_data, y_data, axis=1)
        expected = np.multiply(x_data, y_data[..., np.newaxis])
        self.assertTrue(np.allclose(res, expected))

121 122 123 124 125 126 127 128
        # test static computation graph with_primitives: broadcast with axis
        x_data = np.random.rand(2, 300, 40)
        y_data = np.random.rand(300)
        res = self.__run_static_graph_case_with_numpy_input(
            x_data, y_data, axis=1)
        expected = np.multiply(x_data, y_data[..., np.newaxis])
        self.assertTrue(np.allclose(res, expected))

129 130 131 132 133 134
        # test dynamic computation graph: 1-d array
        x_data = np.random.rand(200)
        y_data = np.random.rand(200)
        res = self.__run_dynamic_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

135 136 137 138 139 140
        # test dynamic numpy input computation graph: 1-d array
        x_data = np.random.rand(200)
        y_data = np.random.rand(200)
        res = self.__run_dynamic_graph_case_with_numpy_input(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

141 142 143 144 145 146
        # test dynamic computation graph: 2-d array
        x_data = np.random.rand(20, 50)
        y_data = np.random.rand(20, 50)
        res = self.__run_dynamic_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

147 148 149 150 151 152
        # test dynamic numpy input computation graph: 1-d array
        x_data = np.random.rand(20, 50)
        y_data = np.random.rand(20, 50)
        res = self.__run_dynamic_graph_case_with_numpy_input(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

153 154 155 156 157 158
        # test dynamic computation graph: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
        res = self.__run_dynamic_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

159 160 161 162 163 164
        # test dynamic computation graph with numpy tensor: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
        res = self.__run_dynamic_graph_case_with_numpy_input(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

165 166 167 168 169 170 171
        # test dynamic computation graph: broadcast with axis
        x_data = np.random.rand(2, 300, 40)
        y_data = np.random.rand(300)
        res = self.__run_dynamic_graph_case(x_data, y_data, axis=1)
        expected = np.multiply(x_data, y_data[..., np.newaxis])
        self.assertTrue(np.allclose(res, expected))

172 173 174 175 176 177 178 179
        # test dynamic computation graph with numpy tensor: broadcast with axis
        x_data = np.random.rand(2, 300, 40)
        y_data = np.random.rand(300)
        res = self.__run_dynamic_graph_case_with_numpy_input(
            x_data, y_data, axis=1)
        expected = np.multiply(x_data, y_data[..., np.newaxis])
        self.assertTrue(np.allclose(res, expected))

180 181 182 183 184 185 186

class TestMultiplyError(unittest.TestCase):
    """TestMultiplyError."""

    def test_errors(self):
        """test_errors."""
        # test static computation graph: dtype can not be int8
187
        paddle.enable_static()
188
        with program_guard(Program(), Program()):
189 190
            x = paddle.static.data(name='x', shape=[100], dtype=np.int8)
            y = paddle.static.data(name='y', shape=[100], dtype=np.int8)
191 192 193 194
            self.assertRaises(TypeError, tensor.multiply, x, y)

        # test static computation graph: inputs must be broadcastable 
        with program_guard(Program(), Program()):
195 196
            x = paddle.static.data(name='x', shape=[20, 50], dtype=np.float64)
            y = paddle.static.data(name='y', shape=[20], dtype=np.float64)
197
            self.assertRaises(ValueError, tensor.multiply, x, y)
198 199 200

        np.random.seed(7)
        # test dynamic computation graph: dtype can not be int8
201
        paddle.disable_static()
202 203
        x_data = np.random.randn(200).astype(np.int8)
        y_data = np.random.randn(200).astype(np.int8)
204 205
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
206
        self.assertRaises(RuntimeError, paddle.multiply, x, y)
207 208 209 210

        # test dynamic computation graph: inputs must be broadcastable
        x_data = np.random.rand(200, 5)
        y_data = np.random.rand(200)
211 212
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
213
        self.assertRaises(ValueError, paddle.multiply, x, y)
214

215 216 217 218 219
        # test dynamic computation graph: inputs must be broadcastable(python)
        x_data = np.random.rand(200, 5)
        y_data = np.random.rand(200)
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
220
        self.assertRaises(ValueError, paddle.multiply, x, y)
221 222 223 224 225 226 227 228

        # test dynamic computation graph: dtype must be same
        x_data = np.random.randn(200).astype(np.int64)
        y_data = np.random.randn(200).astype(np.float64)
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
        self.assertRaises(TypeError, paddle.multiply, x, y)

229 230 231

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