test_multiply.py 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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

18 19 20 21 22
import numpy as np

import paddle
import paddle.tensor as tensor
from paddle.static import Program, program_guard
W
wanghuancoder 已提交
23
from paddle.fluid.framework import _test_eager_guard, in_dygraph_mode
24 25


26
class TestMultiplyApi(unittest.TestCase):
27

28
    def _run_static_graph_case(self, x_data, y_data):
29
        with program_guard(Program(), Program()):
30
            paddle.enable_static()
31 32 33 34 35 36
            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)
37
            res = tensor.multiply(x, y)
38

39 40
            place = paddle.CUDAPlace(
                0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace()
41 42
            exe = paddle.static.Executor(place)
            outs = exe.run(paddle.static.default_main_program(),
43 44 45 46
                           feed={
                               'x': x_data,
                               'y': y_data
                           },
47 48 49 50
                           fetch_list=[res])
            res = outs[0]
            return res

51
    def _run_dynamic_graph_case(self, x_data, y_data):
52
        paddle.disable_static()
53 54
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
55
        res = paddle.multiply(x, y)
56 57
        return res.numpy()

W
wanghuancoder 已提交
58
    def func_test_multiply(self):
59
        np.random.seed(7)
60

61 62 63
        # test static computation graph: 1-d array
        x_data = np.random.rand(200)
        y_data = np.random.rand(200)
64
        res = self._run_static_graph_case(x_data, y_data)
65 66
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

67 68 69
        # test static computation graph: 2-d array
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(2, 500)
70
        res = self._run_static_graph_case(x_data, y_data)
71 72
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

73 74 75
        # test static computation graph: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
76
        res = self._run_static_graph_case(x_data, y_data)
77 78
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

W
will-jl944 已提交
79 80 81 82 83 84
        # test static computation graph: boolean
        x_data = np.random.choice([True, False], size=[200])
        y_data = np.random.choice([True, False], size=[200])
        res = self._run_static_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

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

91 92 93
        # test dynamic computation graph: 2-d array
        x_data = np.random.rand(20, 50)
        y_data = np.random.rand(20, 50)
94
        res = self._run_dynamic_graph_case(x_data, y_data)
95 96
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

97 98 99
        # test dynamic computation graph: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
100
        res = self._run_dynamic_graph_case(x_data, y_data)
101 102
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

W
will-jl944 已提交
103 104 105 106 107 108
        # test dynamic computation graph: boolean
        x_data = np.random.choice([True, False], size=[200])
        y_data = np.random.choice([True, False], size=[200])
        res = self._run_dynamic_graph_case(x_data, y_data)
        self.assertTrue(np.allclose(res, np.multiply(x_data, y_data)))

W
wanghuancoder 已提交
109 110 111 112 113
    def test_multiply(self):
        with _test_eager_guard():
            self.func_test_multiply()
        self.func_test_multiply()

114 115

class TestMultiplyError(unittest.TestCase):
116

W
wanghuancoder 已提交
117
    def func_test_errors(self):
118
        # test static computation graph: dtype can not be int8
119
        paddle.enable_static()
120
        with program_guard(Program(), Program()):
121 122
            x = paddle.static.data(name='x', shape=[100], dtype=np.int8)
            y = paddle.static.data(name='y', shape=[100], dtype=np.int8)
123 124
            self.assertRaises(TypeError, tensor.multiply, x, y)

125
        # test static computation graph: inputs must be broadcastable
126
        with program_guard(Program(), Program()):
127 128
            x = paddle.static.data(name='x', shape=[20, 50], dtype=np.float64)
            y = paddle.static.data(name='y', shape=[20], dtype=np.float64)
129
            self.assertRaises(ValueError, tensor.multiply, x, y)
130 131 132

        np.random.seed(7)
        # test dynamic computation graph: dtype can not be int8
133
        paddle.disable_static()
134 135
        x_data = np.random.randn(200).astype(np.int8)
        y_data = np.random.randn(200).astype(np.int8)
136 137
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
138
        self.assertRaises(RuntimeError, paddle.multiply, x, y)
139 140 141 142

        # test dynamic computation graph: inputs must be broadcastable
        x_data = np.random.rand(200, 5)
        y_data = np.random.rand(200)
143 144
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
145
        self.assertRaises(ValueError, paddle.multiply, x, y)
146

147 148 149 150 151
        # 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)
152
        self.assertRaises(ValueError, paddle.multiply, x, y)
153

154
        # test dynamic computation graph: dtype must be same
155 156 157 158
        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)
159
        self.assertRaises(ValueError, paddle.multiply, x, y)
160

161 162 163 164
        # test dynamic computation graph: dtype must be Tensor type
        x_data = np.random.randn(200).astype(np.int64)
        y_data = np.random.randn(200).astype(np.float64)
        y = paddle.to_tensor(y_data)
165
        self.assertRaises(ValueError, paddle.multiply, x_data, y)
166 167 168 169 170

        # test dynamic computation graph: dtype must be Tensor type
        x_data = np.random.randn(200).astype(np.int64)
        y_data = np.random.randn(200).astype(np.float64)
        x = paddle.to_tensor(x_data)
171
        self.assertRaises(ValueError, paddle.multiply, x, y_data)
172 173 174 175 176

        # test dynamic computation graph: dtype must be Tensor type
        x_data = np.random.randn(200).astype(np.float32)
        y_data = np.random.randn(200).astype(np.float32)
        x = paddle.to_tensor(x_data)
177
        self.assertRaises(ValueError, paddle.multiply, x, y_data)
178 179 180 181 182

        # test dynamic computation graph: dtype must be Tensor type
        x_data = np.random.randn(200).astype(np.float32)
        y_data = np.random.randn(200).astype(np.float32)
        x = paddle.to_tensor(x_data)
183
        self.assertRaises(ValueError, paddle.multiply, x_data, y)
184 185 186 187

        # test dynamic computation graph: dtype must be Tensor type
        x_data = np.random.randn(200).astype(np.float32)
        y_data = np.random.randn(200).astype(np.float32)
188
        self.assertRaises(ValueError, paddle.multiply, x_data, y_data)
189

W
wanghuancoder 已提交
190 191 192 193 194
    def test_errors(self):
        with _test_eager_guard():
            self.func_test_errors()
        self.func_test_errors()

195 196 197

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