test_kron_op.py 6.7 KB
Newer Older
F
Feiyu Chan 已提交
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

F
Feiyu Chan 已提交
17
import numpy as np
18
from eager_op_test import OpTest, convert_float_to_uint16
F
Feiyu Chan 已提交
19 20 21

import paddle
import paddle.fluid.dygraph as dg
22
from paddle import fluid
23
from paddle.fluid import core
F
Feiyu Chan 已提交
24 25 26 27 28


class TestKronOp(OpTest):
    def setUp(self):
        self.op_type = "kron"
29
        self.python_api = paddle.kron
F
Feiyu Chan 已提交
30 31 32 33 34 35 36 37 38 39 40
        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):
W
wanghuancoder 已提交
41
        self.check_output()
F
Feiyu Chan 已提交
42 43

    def test_check_grad(self):
W
wanghuancoder 已提交
44
        self.check_grad(['X', 'Y'], 'Out')
F
Feiyu Chan 已提交
45

46
    def test_check_grad_ignore_x(self):
W
wanghuancoder 已提交
47
        self.check_grad(['Y'], 'Out', no_grad_set=set('X'))
48 49

    def test_check_grad_ignore_y(self):
W
wanghuancoder 已提交
50
        self.check_grad(['X'], 'Out', no_grad_set=set('Y'))
51

F
Feiyu Chan 已提交
52 53 54 55

class TestKronOp2(TestKronOp):
    def setUp(self):
        self.op_type = "kron"
56
        self.python_api = paddle.kron
F
Feiyu Chan 已提交
57 58 59 60 61 62 63 64 65 66 67
        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"
68
        self.python_api = paddle.kron
F
Feiyu Chan 已提交
69 70 71 72 73 74 75 76
        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}


77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
class TestKronFP16Op(TestKronOp):
    def _init_dtype(self):
        return "float16"


@unittest.skipIf(
    not core.is_compiled_with_cuda()
    or not core.is_bfloat16_supported(core.CUDAPlace(0)),
    "core is not complied with CUDA and not support the bfloat16",
)
class TestKronBF16Op(TestKronOp):
    def setUp(self):
        self.op_type = "kron"
        self.python_api = paddle.kron
        self.dtype = np.uint16
        self.np_dtype = "float32"
        x = np.random.uniform(size=(10, 10)).astype(self.np_dtype)
        y = np.random.uniform(size=(10, 10)).astype(self.np_dtype)
        out_ref = np.kron(x, y)
        self.inputs = {
            'X': convert_float_to_uint16(x),
            'Y': convert_float_to_uint16(y),
        }
        self.outputs = {'Out': convert_float_to_uint16(out_ref)}
        # bfloat16 requires using place
        self.place = core.CUDAPlace(0)

    def test_check_output(self):
        self.check_output_with_place(self.place)

    def test_check_grad(self):
        self.check_grad_with_place(self.place, ['X', 'Y'], 'Out')

    def test_check_grad_ignore_x(self):
        self.check_grad_with_place(
            self.place, ['Y'], 'Out', no_grad_set=set('X')
        )

    def test_check_grad_ignore_y(self):
        self.check_grad_with_place(
            self.place, ['X'], 'Out', no_grad_set=set('Y')
        )


F
Feiyu Chan 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
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):
141 142
                a_var = paddle.static.data("a", [-1, -1], dtype="float64")
                b_var = paddle.static.data("b", [-1, -1], dtype="float64")
W
WuHaobo 已提交
143
                out_var = paddle.kron(a_var, b_var)
F
Feiyu Chan 已提交
144 145 146 147

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(start)
148
        (c,) = exe.run(main, feed={'a': a, 'b': b}, fetch_list=[out_var])
F
Feiyu Chan 已提交
149 150 151
        np.testing.assert_allclose(c, np.kron(a, b))


152 153 154
class TestComplexKronOp(OpTest):
    def setUp(self):
        self.op_type = "kron"
155
        self.python_api = paddle.kron
156 157 158 159 160 161 162 163
        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.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(self.x),
164
            'Y': OpTest.np_dtype_to_fluid_dtype(self.y),
165 166 167 168 169
        }
        self.attrs = {'axis': -1, 'use_mkldnn': False}
        self.outputs = {'Out': self.out}

    def init_base_dtype(self):
170
        self.dtype = np.complex128
171 172 173

    def init_input_output(self):
        self.x = np.random.random(self.x_shape).astype(
174 175
            self.dtype
        ) + 1j * np.random.random(self.x_shape).astype(self.dtype)
176
        self.y = np.random.random(self.y_shape).astype(
177 178
            self.dtype
        ) + 1j * np.random.random(self.y_shape).astype(self.dtype)
179 180 181
        self.out = np.kron(self.x, self.y)

    def test_check_output(self):
W
wanghuancoder 已提交
182
        self.check_output()
183 184

    def test_check_grad_normal(self):
185 186 187 188
        self.check_grad(
            ['X', 'Y'],
            'Out',
        )
189 190

    def test_check_grad_ingore_x(self):
191 192 193 194 195
        self.check_grad(
            ['Y'],
            'Out',
            no_grad_set=set("X"),
        )
196 197

    def test_check_grad_ingore_y(self):
198 199 200 201 202
        self.check_grad(
            ['X'],
            'Out',
            no_grad_set=set('Y'),
        )
203 204


C
chentianyu03 已提交
205 206 207 208
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(
209 210
            self.dtype
        ) + 1j * np.random.random(self.y_shape).astype(self.dtype)
C
chentianyu03 已提交
211 212 213
        self.out = np.kron(self.x, self.y)


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