test_multi_dot_op.py 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2021 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
17
from op_test import OpTest
18 19 20
from numpy.linalg import multi_dot
from op_test import OpTest
import paddle
21
from paddle.fluid.framework import _test_eager_guard
22 23 24 25

paddle.enable_static()


26 27
# the unittest of multi_dot
# compare the result of paddle multi_dot and numpy multi_dot
28 29 30
class TestMultiDotOp(OpTest):
    def setUp(self):
        self.op_type = "multi_dot"
31
        self.python_api = paddle.linalg.multi_dot
32 33 34 35 36 37 38 39 40 41 42 43 44
        self.dtype = self.get_dtype()
        self.get_inputs_and_outputs()

    def get_dtype(self):
        return "float64"

    def get_inputs_and_outputs(self):
        self.A = np.random.random((2, 8)).astype(self.dtype)
        self.B = np.random.random((8, 4)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B)]}
        self.outputs = {'Out': multi_dot([self.A, self.B])}

    def test_check_output(self):
45
        self.check_output(check_eager=True)
46 47

    def test_check_grad(self):
48 49
        self.check_grad(['x0'], 'Out', check_eager=True)
        self.check_grad(['x1'], 'Out', check_eager=True)
50 51


52
# (A*B)*C
53 54 55 56 57 58 59 60 61
class TestMultiDotOp3Mat(TestMultiDotOp):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((2, 10)).astype(self.dtype)
        self.B = np.random.random((10, 4)).astype(self.dtype)
        self.C = np.random.random((4, 3)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B), ('x2', self.C)]}
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C])}

    def test_check_grad(self):
62 63 64
        self.check_grad(['x0'], 'Out', check_eager=True)
        self.check_grad(['x1'], 'Out', check_eager=True)
        self.check_grad(['x2'], 'Out', check_eager=True)
65 66


67
# A*(B*C)
68 69 70 71 72 73 74 75 76
class TestMultiDotOp3Mat2(TestMultiDotOp):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((3, 4)).astype(self.dtype)
        self.B = np.random.random((4, 8)).astype(self.dtype)
        self.C = np.random.random((8, 2)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B), ('x2', self.C)]}
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C])}

    def test_check_grad(self):
77 78 79
        self.check_grad(['x0'], 'Out', check_eager=True)
        self.check_grad(['x1'], 'Out', check_eager=True)
        self.check_grad(['x2'], 'Out', check_eager=True)
80 81 82 83 84 85 86 87 88


class TestMultiDotOp4Mat(TestMultiDotOp):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((8, 6)).astype(self.dtype)
        self.B = np.random.random((6, 3)).astype(self.dtype)
        self.C = np.random.random((3, 4)).astype(self.dtype)
        self.D = np.random.random((4, 5)).astype(self.dtype)
        self.inputs = {
89 90 91 92 93 94
            'X': [
                ('x0', self.A),
                ('x1', self.B),
                ('x2', self.C),
                ('x3', self.D),
            ]
95 96 97 98
        }
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C, self.D])}

    def test_check_grad(self):
99 100 101 102
        self.check_grad(['x0'], 'Out', check_eager=True)
        self.check_grad(['x1'], 'Out', check_eager=True)
        self.check_grad(['x2'], 'Out', check_eager=True)
        self.check_grad(['x3'], 'Out', check_eager=True)
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


class TestMultiDotOpFirst1D(TestMultiDotOp):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((4)).astype(self.dtype)
        self.B = np.random.random((4, 3)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B)]}
        self.outputs = {'Out': multi_dot([self.A, self.B])}


class TestMultiDotOp3MatFirst1D(TestMultiDotOp3Mat):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((4)).astype(self.dtype)
        self.B = np.random.random((4, 3)).astype(self.dtype)
        self.C = np.random.random((3, 3)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B), ('x2', self.C)]}
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C])}


class TestMultiDotOp4MatFirst1D(TestMultiDotOp4Mat):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((4)).astype(self.dtype)
        self.B = np.random.random((4, 3)).astype(self.dtype)
        self.C = np.random.random((3, 4)).astype(self.dtype)
        self.D = np.random.random((4, 5)).astype(self.dtype)
        self.inputs = {
129 130 131 132 133 134
            'X': [
                ('x0', self.A),
                ('x1', self.B),
                ('x2', self.C),
                ('x3', self.D),
            ]
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        }
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C, self.D])}


class TestMultiDotOpLast1D(TestMultiDotOp):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((3, 6)).astype(self.dtype)
        self.B = np.random.random((6)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B)]}
        self.outputs = {'Out': multi_dot([self.A, self.B])}


class TestMultiDotOp3MatLast1D(TestMultiDotOp3Mat):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((2, 4)).astype(self.dtype)
        self.B = np.random.random((4, 3)).astype(self.dtype)
        self.C = np.random.random((3)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B), ('x2', self.C)]}
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C])}

    def test_check_grad(self):
156 157 158
        self.check_grad(['x0'], 'Out', check_eager=True)
        self.check_grad(['x1'], 'Out', check_eager=True)
        self.check_grad(['x2'], 'Out', check_eager=True)
159 160 161 162 163 164 165 166 167


class TestMultiDotOp4MatLast1D(TestMultiDotOp4Mat):
    def get_inputs_and_outputs(self):
        self.A = np.random.random((2, 3)).astype(self.dtype)
        self.B = np.random.random((3, 2)).astype(self.dtype)
        self.C = np.random.random((2, 3)).astype(self.dtype)
        self.D = np.random.random((3)).astype(self.dtype)
        self.inputs = {
168 169 170 171 172 173
            'X': [
                ('x0', self.A),
                ('x1', self.B),
                ('x2', self.C),
                ('x3', self.D),
            ]
174 175 176 177 178 179
        }
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C, self.D])}


class TestMultiDotOpFirstAndLast1D(TestMultiDotOp):
    def get_inputs_and_outputs(self):
180
        self.A = np.random.random((4,)).astype(self.dtype)
181 182 183 184 185 186 187
        self.B = np.random.random((4)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B)]}
        self.outputs = {'Out': multi_dot([self.A, self.B])}


class TestMultiDotOp3MatFirstAndLast1D(TestMultiDotOp3Mat):
    def get_inputs_and_outputs(self):
188
        self.A = np.random.random((6,)).astype(self.dtype)
189 190 191 192 193 194 195 196
        self.B = np.random.random((6, 4)).astype(self.dtype)
        self.C = np.random.random((4)).astype(self.dtype)
        self.inputs = {'X': [('x0', self.A), ('x1', self.B), ('x2', self.C)]}
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C])}


class TestMultiDotOp4MatFirstAndLast1D(TestMultiDotOp4Mat):
    def get_inputs_and_outputs(self):
197
        self.A = np.random.random((3,)).astype(self.dtype)
198 199 200 201
        self.B = np.random.random((3, 4)).astype(self.dtype)
        self.C = np.random.random((4, 2)).astype(self.dtype)
        self.D = np.random.random((2)).astype(self.dtype)
        self.inputs = {
202 203 204 205 206 207
            'X': [
                ('x0', self.A),
                ('x1', self.B),
                ('x2', self.C),
                ('x3', self.D),
            ]
208 209 210 211 212 213 214
        }
        self.outputs = {'Out': multi_dot([self.A, self.B, self.C, self.D])}


#####python API test#######
class TestMultiDotOpError(unittest.TestCase):
    def test_errors(self):
215 216 217
        with paddle.static.program_guard(
            paddle.static.Program(), paddle.static.Program()
        ):
218 219
            # The inputs type of multi_dot must be list matrix.
            input1 = 12
220 221 222
            self.assertRaises(
                TypeError, paddle.linalg.multi_dot, [input1, input1]
            )
223 224

            # The inputs dtype of multi_dot must be float64, float64 or float16.
225 226 227 228 229 230
            input2 = paddle.static.data(
                name='input2', shape=[10, 10], dtype="int32"
            )
            self.assertRaises(
                TypeError, paddle.linalg.multi_dot, [input2, input2]
            )
231 232 233

            # the number of tensor must be larger than 1
            x0 = paddle.static.data(name='x0', shape=[3, 2], dtype="float64")
234
            self.assertRaises(ValueError, paddle.linalg.multi_dot, [x0])
235

236
            # the first tensor must be 1D or 2D
237 238
            x1 = paddle.static.data(name='x1', shape=[3, 2, 3], dtype="float64")
            x2 = paddle.static.data(name='x2', shape=[3, 2], dtype="float64")
239
            self.assertRaises(ValueError, paddle.linalg.multi_dot, [x1, x2])
240

241
            # the last tensor must be 1D or 2D
242 243
            x3 = paddle.static.data(name='x3', shape=[3, 2], dtype="float64")
            x4 = paddle.static.data(name='x4', shape=[3, 2, 2], dtype="float64")
244
            self.assertRaises(ValueError, paddle.linalg.multi_dot, [x3, x4])
245

246
            # the tensor must be 2D, except first and last tensor
247 248 249
            x5 = paddle.static.data(name='x5', shape=[3, 2], dtype="float64")
            x6 = paddle.static.data(name='x6', shape=[2], dtype="float64")
            x7 = paddle.static.data(name='x7', shape=[2, 2], dtype="float64")
250
            self.assertRaises(ValueError, paddle.linalg.multi_dot, [x5, x6, x7])
251 252 253 254 255 256 257 258


class APITestMultiDot(unittest.TestCase):
    def test_out(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            x0 = paddle.static.data(name='x0', shape=[3, 2], dtype="float64")
            x1 = paddle.static.data(name='x1', shape=[2, 3], dtype='float64')
259
            result = paddle.linalg.multi_dot([x0, x1])
260 261 262
            exe = paddle.static.Executor(paddle.CPUPlace())
            data1 = np.random.rand(3, 2).astype("float64")
            data2 = np.random.rand(2, 3).astype("float64")
263 264 265
            (np_res,) = exe.run(
                feed={'x0': data1, 'x1': data2}, fetch_list=[result]
            )
266 267
            expected_result = np.linalg.multi_dot([data1, data2])

268 269 270 271 272 273
        np.testing.assert_allclose(
            np_res,
            expected_result,
            rtol=1e-05,
            atol=1e-05,
            err_msg='two value is            {}\n{}, check diff!'.format(
274 275 276
                np_res, expected_result
            ),
        )
277 278 279 280 281 282 283 284

    def test_dygraph_without_out(self):
        paddle.disable_static()
        device = paddle.CPUPlace()
        input_array1 = np.random.rand(3, 4).astype("float64")
        input_array2 = np.random.rand(4, 3).astype("float64")
        data1 = paddle.to_tensor(input_array1)
        data2 = paddle.to_tensor(input_array2)
285
        out = paddle.linalg.multi_dot([data1, data2])
286
        expected_result = np.linalg.multi_dot([input_array1, input_array2])
287
        np.testing.assert_allclose(expected_result, out.numpy(), rtol=1e-05)
288

289
    def test_dygraph_api(self):
290 291 292
        with _test_eager_guard():
            self.test_dygraph_without_out()

293 294 295

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