test_dot_op.py 9.0 KB
Newer Older
L
liuwei1031 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#  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
16

L
liuwei1031 已提交
17
import numpy as np
18
from eager_op_test import OpTest
19 20 21 22

import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
23
from paddle.fluid import Program, program_guard
L
liuwei1031 已提交
24 25 26 27 28


class DotOp(OpTest):
    def setUp(self):
        self.op_type = "dot"
29
        self.python_api = paddle.dot
L
liuwei1031 已提交
30 31 32 33 34
        self.init_dtype()
        self.init_input_output()

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
35
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
L
liuwei1031 已提交
36 37 38 39 40
        }
        self.outputs = {'Out': self.out}
        self.attrs = {}

    def test_check_output(self):
41
        self.check_output()
L
liuwei1031 已提交
42 43

    def test_check_grad_normal(self):
R
ronnywang 已提交
44 45 46 47
        if core.is_compiled_with_rocm():
            self.check_grad(
                ['X', 'Y'],
                'Out',
48
                user_defined_grads=[self.inputs['Y'], self.inputs['X']],
49
            )
R
ronnywang 已提交
50
        else:
51 52 53 54
            self.check_grad(
                ['X', 'Y'],
                'Out',
            )
L
liuwei1031 已提交
55 56

    def test_check_grad_ingore_x(self):
R
ronnywang 已提交
57
        if core.is_compiled_with_rocm():
58 59 60 61 62 63
            self.check_grad(
                ['Y'],
                'Out',
                no_grad_set=set("X"),
                user_defined_grads=[self.inputs['X']],
            )
R
ronnywang 已提交
64
        else:
65
            self.check_grad(
66 67 68
                ['Y'],
                'Out',
                no_grad_set=set("X"),
69
            )
L
liuwei1031 已提交
70 71

    def test_check_grad_ingore_y(self):
R
ronnywang 已提交
72
        if core.is_compiled_with_rocm():
73 74 75 76 77 78
            self.check_grad(
                ['X'],
                'Out',
                no_grad_set=set('Y'),
                user_defined_grads=[self.inputs['Y']],
            )
R
ronnywang 已提交
79
        else:
80
            self.check_grad(
81 82 83
                ['X'],
                'Out',
                no_grad_set=set('Y'),
84
            )
L
liuwei1031 已提交
85 86 87 88 89 90 91 92 93 94 95 96

    def init_input_output(self):
        self.x = np.random.uniform(0.1, 1, [121]).astype(self.dtype)
        self.y = np.random.uniform(1, 3, [121]).astype(self.dtype)
        self.out = np.dot(self.x, self.y)

    def init_dtype(self):
        self.dtype = np.float64


class DotOpBatch(DotOp):
    def init_input_output(self):
97 98 99 100 101 102 103 104
        self.x = (
            np.random.uniform(0.1, 1, [132])
            .astype(self.dtype)
            .reshape([11, 12])
        )
        self.y = (
            np.random.uniform(1, 3, [132]).astype(self.dtype).reshape([11, 12])
        )
L
liuwei1031 已提交
105 106
        self.out = np.sum(self.x * self.y, axis=1).reshape([11, 1])

R
ronnywang 已提交
107 108 109 110 111 112 113 114 115
    def test_check_grad_normal(self):
        self.check_grad(['X', 'Y'], 'Out')

    def test_check_grad_ingore_x(self):
        self.check_grad(['Y'], 'Out', no_grad_set=set("X"))

    def test_check_grad_ingore_y(self):
        self.check_grad(['X'], 'Out', no_grad_set=set('Y'))

L
liuwei1031 已提交
116 117 118 119 120 121 122

class TestDotOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):

            # the input dtype of elementwise_mul must be float16 or float32 or float64 or int32 or int64
            # float16 only can be set on GPU place
G
GGBond8488 已提交
123 124
            x1 = paddle.static.data(name='x1', shape=[-1, 120], dtype="uint8")
            y1 = paddle.static.data(name='y1', shape=[-1, 120], dtype="uint8")
L
liuwei1031 已提交
125 126
            self.assertRaises(Exception, paddle.dot, x1, y1)

G
GGBond8488 已提交
127 128 129 130 131 132
            x2 = paddle.static.data(
                name='x2', shape=[-1, 2, 3], dtype="float32"
            )
            y2 = paddle.static.data(
                name='y2', shape=[-1, 2, 3], dtype="float32"
            )
L
liuwei1031 已提交
133 134
            self.assertRaises(Exception, paddle.dot, x2, y2)

G
GGBond8488 已提交
135 136 137 138
            x3 = paddle.static.data(name='x3', shape=[-1, 3], dtype="float32")
            y3 = paddle.static.data(
                name='y3', shape=[-1, 2, 3], dtype="float32"
            )
L
liuwei1031 已提交
139 140 141 142 143 144 145 146
            self.assertRaises(Exception, paddle.dot, x2, y3)


class TestDygraph(unittest.TestCase):
    def test_dygraph(self):
        with fluid.dygraph.guard():
            x1 = fluid.dygraph.to_variable(np.array([1, 3]).astype(np.float32))
            y1 = fluid.dygraph.to_variable(np.array([2, 5]).astype(np.float32))
147 148 149
            np.testing.assert_allclose(
                paddle.dot(x1, y1).numpy(), np.array([17]), rtol=1e-05
            )
L
liuwei1031 已提交
150 151

            x1 = fluid.dygraph.to_variable(
152 153
                np.array([[1, 3], [3, 5]]).astype(np.float32)
            )
L
liuwei1031 已提交
154
            y1 = fluid.dygraph.to_variable(
155 156
                np.array([[2, 5], [6, 8]]).astype(np.float32)
            )
157
            np.testing.assert_array_equal(
158 159
                paddle.dot(x1, y1).numpy(), np.array([[17], [58]])
            )
L
liuwei1031 已提交
160 161


C
chentianyu03 已提交
162 163 164
class TestComplexDotOp(OpTest):
    def setUp(self):
        self.op_type = "dot"
165
        self.python_api = paddle.dot
C
chentianyu03 已提交
166 167 168 169 170 171
        self.init_base_dtype()
        self.init_input_output()
        self.init_grad_input_output()

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
172
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
C
chentianyu03 已提交
173 174 175 176 177 178 179 180
        }
        self.outputs = {'Out': self.out}

    def init_base_dtype(self):
        self.dtype = np.float64

    def init_input_output(self):
        self.x = np.random.random(100).astype(
181 182
            self.dtype
        ) + 1j * np.random.random(100).astype(self.dtype)
C
chentianyu03 已提交
183
        self.y = np.random.random(100).astype(
184 185
            self.dtype
        ) + 1j * np.random.random(100).astype(self.dtype)
C
chentianyu03 已提交
186 187 188
        self.out = np.dot(self.x, self.y)

    def init_grad_input_output(self):
189
        self.grad_out = np.ones(1, self.dtype) + 1j * np.ones(1, self.dtype)
C
chentianyu03 已提交
190 191 192 193
        self.grad_x = self.grad_out * np.conj(self.y)
        self.grad_y = self.grad_out * np.conj(self.x)

    def test_check_output(self):
194
        self.check_output()
C
chentianyu03 已提交
195 196

    def test_check_grad_normal(self):
197 198 199 200 201 202
        self.check_grad(
            ['X', 'Y'],
            'Out',
            user_defined_grads=[self.grad_x, self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
C
chentianyu03 已提交
203 204

    def test_check_grad_ingore_x(self):
205 206 207 208 209 210 211
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            user_defined_grads=[self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
C
chentianyu03 已提交
212 213

    def test_check_grad_ingore_y(self):
214 215 216 217 218 219 220
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            user_defined_grads=[self.grad_x],
            user_defined_grad_outputs=[self.grad_out],
        )
C
chentianyu03 已提交
221 222 223 224 225


class TestComplexDotOp2D(OpTest):
    def setUp(self):
        self.op_type = "dot"
226
        self.python_api = paddle.dot
C
chentianyu03 已提交
227 228 229 230 231 232
        self.init_base_dtype()
        self.init_input_output()
        self.init_grad_input_output()

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
233
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
C
chentianyu03 已提交
234 235 236 237 238 239 240
        }
        self.outputs = {'Out': self.out}

    def init_base_dtype(self):
        self.dtype = np.float64

    def init_input_output(self):
241 242 243 244 245 246
        self.x = np.random.random((2, 100)).astype(
            self.dtype
        ) + 1j * np.random.random((2, 100)).astype(self.dtype)
        self.y = np.random.random((2, 100)).astype(
            self.dtype
        ) + 1j * np.random.random((2, 100)).astype(self.dtype)
C
chentianyu03 已提交
247 248 249
        self.out = np.diag(np.dot(self.x, self.y.T)).reshape(-1, 1)

    def init_grad_input_output(self):
250 251 252
        self.grad_out = np.ones((2, 1), self.dtype) + 1j * np.ones(
            (2, 1), self.dtype
        )
C
chentianyu03 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265
        self.grad_x = self._get_grad(self.grad_out, self.y)
        self.grad_y = self._get_grad(self.grad_out, self.x)

    def _get_grad(self, grad_out, input):
        grad = np.empty((0, input.shape[1]))
        for i in range(grad_out.shape[0]):
            grad = np.append(grad, [grad_out[i] * np.conj(input[i])], axis=0)
        return grad

    def test_check_output(self):
        self.check_output()

    def test_check_grad_normal(self):
266 267 268 269 270 271
        self.check_grad(
            ['X', 'Y'],
            'Out',
            user_defined_grads=[self.grad_x, self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
C
chentianyu03 已提交
272 273

    def test_check_grad_ingore_x(self):
274 275 276 277 278 279 280
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            user_defined_grads=[self.grad_y],
            user_defined_grad_outputs=[self.grad_out],
        )
C
chentianyu03 已提交
281 282

    def test_check_grad_ingore_y(self):
283 284 285 286 287 288 289
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            user_defined_grads=[self.grad_x],
            user_defined_grad_outputs=[self.grad_out],
        )
C
chentianyu03 已提交
290 291


L
liuwei1031 已提交
292
if __name__ == '__main__':
C
chentianyu03 已提交
293
    paddle.enable_static()
L
liuwei1031 已提交
294
    unittest.main()