test_outer.py 6.8 KB
Newer Older
Z
zhiboniu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 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

import numpy as np

import paddle
from paddle.static import Program, program_guard
21
from paddle.fluid.framework import _test_eager_guard
Z
zhiboniu 已提交
22 23 24


class TestMultiplyApi(unittest.TestCase):
25

Z
zhiboniu 已提交
26 27 28
    def _run_static_graph_case(self, x_data, y_data):
        with program_guard(Program(), Program()):
            paddle.enable_static()
29 30 31 32 33 34
            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)
Z
zhiboniu 已提交
35 36
            res = paddle.outer(x, y)

37 38
            place = paddle.CUDAPlace(
                0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace()
Z
zhiboniu 已提交
39 40
            exe = paddle.static.Executor(place)
            outs = exe.run(paddle.static.default_main_program(),
41 42 43 44
                           feed={
                               'x': x_data,
                               'y': y_data
                           },
Z
zhiboniu 已提交
45 46 47 48 49 50 51 52 53 54 55
                           fetch_list=[res])
            res = outs[0]
            return res

    def _run_dynamic_graph_case(self, x_data, y_data):
        paddle.disable_static()
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
        res = paddle.outer(x, y)
        return res.numpy()

W
wanghuancoder 已提交
56
    def func_test_multiply(self):
Z
zhiboniu 已提交
57 58 59 60 61 62
        np.random.seed(7)

        # test static computation graph: 3-d array
        x_data = np.random.rand(2, 10, 10).astype(np.float64)
        y_data = np.random.rand(2, 5, 10).astype(np.float64)
        res = self._run_static_graph_case(x_data, y_data)
63
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05)
Z
zhiboniu 已提交
64 65 66 67 68

        # test static computation graph: 2-d array
        x_data = np.random.rand(200, 5).astype(np.float64)
        y_data = np.random.rand(50, 5).astype(np.float64)
        res = self._run_static_graph_case(x_data, y_data)
69
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05)
Z
zhiboniu 已提交
70 71 72 73 74

        # test static computation graph: 1-d array
        x_data = np.random.rand(50).astype(np.float64)
        y_data = np.random.rand(50).astype(np.float64)
        res = self._run_static_graph_case(x_data, y_data)
75
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05)
Z
zhiboniu 已提交
76 77 78 79 80

        # test dynamic computation graph: 3-d array
        x_data = np.random.rand(5, 10, 10).astype(np.float64)
        y_data = np.random.rand(2, 10).astype(np.float64)
        res = self._run_dynamic_graph_case(x_data, y_data)
81
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05)
Z
zhiboniu 已提交
82 83 84 85 86

        # test dynamic computation graph: 2-d array
        x_data = np.random.rand(20, 50).astype(np.float64)
        y_data = np.random.rand(50).astype(np.float64)
        res = self._run_dynamic_graph_case(x_data, y_data)
87
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05)
Z
zhiboniu 已提交
88 89 90 91 92

        # test dynamic computation graph: Scalar
        x_data = np.random.rand(20, 10).astype(np.float32)
        y_data = np.random.rand(1).astype(np.float32).item()
        res = self._run_dynamic_graph_case(x_data, y_data)
93
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=10000.0)
Z
zhiboniu 已提交
94 95

        # test dynamic computation graph: 2-d array Complex
96 97 98 99
        x_data = np.random.rand(20, 50).astype(
            np.float64) + 1J * np.random.rand(20, 50).astype(np.float64)
        y_data = np.random.rand(50).astype(
            np.float64) + 1J * np.random.rand(50).astype(np.float64)
Z
zhiboniu 已提交
100
        res = self._run_dynamic_graph_case(x_data, y_data)
101
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05)
Z
zhiboniu 已提交
102 103

        # test dynamic computation graph: 3-d array Complex
104 105 106 107
        x_data = np.random.rand(5, 10, 10).astype(
            np.float64) + 1J * np.random.rand(5, 10, 10).astype(np.float64)
        y_data = np.random.rand(2, 10).astype(
            np.float64) + 1J * np.random.rand(2, 10).astype(np.float64)
Z
zhiboniu 已提交
108
        res = self._run_dynamic_graph_case(x_data, y_data)
109
        np.testing.assert_allclose(res, np.outer(x_data, y_data), rtol=1e-05)
Z
zhiboniu 已提交
110

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

Z
zhiboniu 已提交
116 117

class TestMultiplyError(unittest.TestCase):
118

W
wanghuancoder 已提交
119
    def func_test_errors(self):
Z
zhiboniu 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        # test static computation graph: dtype can not be int8
        paddle.enable_static()
        with program_guard(Program(), Program()):
            x = paddle.static.data(name='x', shape=[100], dtype=np.int8)
            y = paddle.static.data(name='y', shape=[100], dtype=np.int8)
            self.assertRaises(TypeError, paddle.outer, x, y)

        np.random.seed(7)
        # test dynamic computation graph: dtype can not be int8
        paddle.disable_static()
        x_data = np.random.randn(200).astype(np.int8)
        y_data = np.random.randn(200).astype(np.int8)
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
        self.assertRaises(RuntimeError, paddle.outer, x, y)

136
        # test dynamic computation graph: dtype must be same
Z
zhiboniu 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        x_data = np.random.randn(200).astype(np.float32)
        y_data = np.random.randn(200).astype(np.float64)
        x = paddle.to_tensor(x_data)
        y = paddle.to_tensor(y_data)
        self.assertRaises(ValueError, paddle.outer, x, y)

        # test dynamic computation graph: dtype must be Tensor type
        x_data = np.random.randn(200).astype(np.float64)
        y_data = np.random.randn(200).astype(np.float64)
        y = paddle.to_tensor(y_data)
        self.assertRaises(ValueError, paddle.outer, x_data, y)

        # 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)
        self.assertRaises(ValueError, paddle.outer, x, y_data)

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

W
wanghuancoder 已提交
160 161 162 163 164
    def test_errors(self):
        with _test_eager_guard():
            self.func_test_errors()
        self.func_test_errors()

Z
zhiboniu 已提交
165 166 167

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