test_kron_op.py 7.5 KB
Newer Older
F
Feiyu Chan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 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
import numpy as np
from op_test import OpTest

import paddle
import paddle.fluid as fluid
import paddle.fluid.dygraph as dg
Y
YuanRisheng 已提交
24
from paddle.fluid.framework import _test_eager_guard
F
Feiyu Chan 已提交
25 26 27 28 29


class TestKronOp(OpTest):
    def setUp(self):
        self.op_type = "kron"
Y
YuanRisheng 已提交
30
        self.python_api = paddle.kron
F
Feiyu Chan 已提交
31 32 33 34 35 36 37 38 39 40 41
        self.dtype = self._init_dtype()
        x = np.random.uniform(size=(10, 10)).astype(self.dtype)
        y = np.random.uniform(size=(10, 10)).astype(self.dtype)
        out_ref = np.kron(x, y)
        self.inputs = {'X': x, 'Y': y}
        self.outputs = {'Out': out_ref}

    def _init_dtype(self):
        return "float64"

    def test_check_output(self):
Y
YuanRisheng 已提交
42
        self.check_output(check_eager=True)
F
Feiyu Chan 已提交
43 44

    def test_check_grad(self):
Y
YuanRisheng 已提交
45
        self.check_grad(['X', 'Y'], 'Out', check_eager=True)
F
Feiyu Chan 已提交
46

47
    def test_check_grad_ignore_x(self):
Y
YuanRisheng 已提交
48
        self.check_grad(['Y'], 'Out', no_grad_set=set('X'), check_eager=True)
49 50

    def test_check_grad_ignore_y(self):
Y
YuanRisheng 已提交
51
        self.check_grad(['X'], 'Out', no_grad_set=set('Y'), check_eager=True)
52

F
Feiyu Chan 已提交
53 54 55 56

class TestKronOp2(TestKronOp):
    def setUp(self):
        self.op_type = "kron"
Y
YuanRisheng 已提交
57
        self.python_api = paddle.kron
F
Feiyu Chan 已提交
58 59 60 61 62 63 64 65 66 67 68
        self.dtype = self._init_dtype()
        x = np.random.uniform(size=(5, 5, 4)).astype(self.dtype)
        y = np.random.uniform(size=(10, 10)).astype(self.dtype)
        out_ref = np.kron(x, y)
        self.inputs = {'X': x, 'Y': y}
        self.outputs = {'Out': out_ref}


class TestKronOp3(TestKronOp):
    def setUp(self):
        self.op_type = "kron"
Y
YuanRisheng 已提交
69
        self.python_api = paddle.kron
F
Feiyu Chan 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        self.dtype = self._init_dtype()
        x = np.random.uniform(size=(10, 10)).astype(self.dtype)
        y = np.random.uniform(size=(5, 5, 4)).astype(self.dtype)
        out_ref = np.kron(x, y)
        self.inputs = {'X': x, 'Y': y}
        self.outputs = {'Out': out_ref}


class TestKronLayer(unittest.TestCase):
    def test_case(self):
        a = np.random.randn(10, 10).astype(np.float64)
        b = np.random.randn(10, 10).astype(np.float64)

        place = fluid.CPUPlace()
        with dg.guard(place):
            a_var = dg.to_variable(a)
            b_var = dg.to_variable(b)
            c_var = paddle.kron(a_var, b_var)
            np.testing.assert_allclose(c_var.numpy(), np.kron(a, b))

    def test_case_with_output(self):
        a = np.random.randn(10, 10).astype(np.float64)
        b = np.random.randn(10, 10).astype(np.float64)

        main = fluid.Program()
        start = fluid.Program()
        with fluid.unique_name.guard():
            with fluid.program_guard(main, start):
                a_var = fluid.data("a", [-1, -1], dtype="float64")
                b_var = fluid.data("b", [-1, -1], dtype="float64")
W
WuHaobo 已提交
100
                out_var = paddle.kron(a_var, b_var)
F
Feiyu Chan 已提交
101 102 103 104 105 106 107

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(start)
        c, = exe.run(main, feed={'a': a, 'b': b}, fetch_list=[out_var])
        np.testing.assert_allclose(c, np.kron(a, b))

Y
YuanRisheng 已提交
108 109 110 111 112
    def test_api_eager_dygraph(self):
        with _test_eager_guard():
            self.test_case()
            self.test_case_with_output()

F
Feiyu Chan 已提交
113

114 115 116
class TestComplexKronOp(OpTest):
    def setUp(self):
        self.op_type = "kron"
Y
YuanRisheng 已提交
117
        self.python_api = paddle.kron
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        self.x_shape = np.array([10, 10])
        self.y_shape = np.array([3, 35])
        self.out_shape = self.x_shape * self.y_shape
        self.init_base_dtype()
        self.init_input_output()
        self.init_grad_input_output()

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y)
        }
        self.attrs = {'axis': -1, 'use_mkldnn': False}
        self.outputs = {'Out': self.out}

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

    def init_input_output(self):
        self.x = np.random.random(self.x_shape).astype(
            self.dtype) + 1J * np.random.random(self.x_shape).astype(self.dtype)
        self.y = np.random.random(self.y_shape).astype(
            self.dtype) + 1J * np.random.random(self.y_shape).astype(self.dtype)
        self.out = np.kron(self.x, self.y)

    def init_grad_input_output(self):
        self.grad_out = np.ones(self.out_shape, self.dtype) + 1J * np.ones(
            self.out_shape, self.dtype)
        self.grad_x = self.get_grad_x_by_numpy()
        self.grad_y = self.get_grad_y_by_numpy()

    def get_grad_x_by_numpy(self):
149
        grad_x = np.zeros(self.x_shape, np.complex128)
150 151 152 153 154 155 156 157 158 159 160
        for x_i in range(self.x_shape[0]):
            for x_j in range(self.x_shape[1]):
                for i in range(self.y_shape[0]):
                    for j in range(self.y_shape[1]):
                        idx_i = x_i * self.y_shape[0] + i
                        idx_j = x_j * self.y_shape[1] + j
                        grad_x[x_i][x_j] += self.grad_out[idx_i][
                            idx_j] * np.conj(self.y[i][j])
        return grad_x

    def get_grad_y_by_numpy(self):
161
        grad_y = np.zeros(self.y_shape, np.complex128)
162 163 164 165 166 167 168 169 170 171 172
        for y_i in range(self.y_shape[0]):
            for y_j in range(self.y_shape[1]):
                for x_i in range(self.x_shape[0]):
                    for x_j in range(self.x_shape[1]):
                        idx_i = x_i * self.y_shape[0] + y_i
                        idx_j = x_j * self.y_shape[1] + y_j
                        grad_y[y_i][y_j] += self.grad_out[idx_i][
                            idx_j] * np.conj(self.x[x_i][x_j])
        return grad_y

    def test_check_output(self):
Y
YuanRisheng 已提交
173
        self.check_output(check_eager=True)
174 175 176 177 178 179

    def test_check_grad_normal(self):
        self.check_grad(
            ['X', 'Y'],
            'Out',
            user_defined_grads=[self.grad_x, self.grad_y],
Y
YuanRisheng 已提交
180 181
            user_defined_grad_outputs=[self.grad_out],
            check_eager=True)
182 183 184 185 186 187 188

    def test_check_grad_ingore_x(self):
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
            user_defined_grads=[self.grad_y],
Y
YuanRisheng 已提交
189 190
            user_defined_grad_outputs=[self.grad_out],
            check_eager=True)
191 192 193 194 195 196 197

    def test_check_grad_ingore_y(self):
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
            user_defined_grads=[self.grad_x],
Y
YuanRisheng 已提交
198 199
            user_defined_grad_outputs=[self.grad_out],
            check_eager=True)
200 201


C
chentianyu03 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215
class TestKronOpTypePromotion(TestComplexKronOp):
    def init_input_output(self):
        self.x = np.random.random(self.x_shape).astype(self.dtype)
        self.y = np.random.random(self.y_shape).astype(
            self.dtype) + 1J * np.random.random(self.y_shape).astype(self.dtype)
        self.out = np.kron(self.x, self.y)

    def init_grad_input_output(self):
        self.grad_out = np.ones(self.out_shape, self.dtype) + 1J * np.ones(
            self.out_shape, self.dtype)
        self.grad_x = self.get_grad_x_by_numpy().real
        self.grad_y = self.get_grad_y_by_numpy()


F
Feiyu Chan 已提交
216
if __name__ == '__main__':
217
    paddle.enable_static()
F
Feiyu Chan 已提交
218
    unittest.main()