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

import unittest

17 18 19 20 21
import numpy as np

import paddle
import paddle.tensor as tensor
from paddle.static import Program, program_guard
22
from paddle.fluid.framework import _test_eager_guard
23 24


25
class TestMultiplyApi(unittest.TestCase):
26

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

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

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

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

60 61 62
        # test static computation graph: 1-d array
        x_data = np.random.rand(200)
        y_data = np.random.rand(200)
63
        res = self._run_static_graph_case(x_data, y_data)
64
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
65

66 67 68
        # test static computation graph: 2-d array
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(2, 500)
69
        res = self._run_static_graph_case(x_data, y_data)
70
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
71

72 73 74
        # test static computation graph: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
75
        res = self._run_static_graph_case(x_data, y_data)
76
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
77

W
will-jl944 已提交
78 79 80 81
        # 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)
82
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
W
will-jl944 已提交
83

84 85 86
        # test dynamic computation graph: 1-d array
        x_data = np.random.rand(200)
        y_data = np.random.rand(200)
87
        res = self._run_dynamic_graph_case(x_data, y_data)
88
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
89

90 91 92
        # test dynamic computation graph: 2-d array
        x_data = np.random.rand(20, 50)
        y_data = np.random.rand(20, 50)
93
        res = self._run_dynamic_graph_case(x_data, y_data)
94
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
95

96 97 98
        # test dynamic computation graph: broadcast
        x_data = np.random.rand(2, 500)
        y_data = np.random.rand(500)
99
        res = self._run_dynamic_graph_case(x_data, y_data)
100
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
101

W
will-jl944 已提交
102 103 104 105
        # 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)
106
        np.testing.assert_allclose(res, np.multiply(x_data, y_data), rtol=1e-05)
W
will-jl944 已提交
107

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

113 114

class TestMultiplyError(unittest.TestCase):
115

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

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

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

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

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

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

160 161 162 163
        # 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)
164
        self.assertRaises(ValueError, paddle.multiply, x_data, y)
165 166 167 168 169

        # 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)
170
        self.assertRaises(ValueError, paddle.multiply, x, y_data)
171 172 173 174 175

        # 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)
176
        self.assertRaises(ValueError, paddle.multiply, x, y_data)
177 178 179 180 181

        # 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)
182
        self.assertRaises(ValueError, paddle.multiply, x_data, y)
183 184 185 186

        # 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)
187
        self.assertRaises(ValueError, paddle.multiply, x_data, y_data)
188

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

194 195 196

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