#   Copyright (c) 2018 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 sys
import unittest

import numpy as np

import paddle

sys.path.append("..")
from op_test_xpu import XPUOpTest

paddle.enable_static()

from xpu.get_test_cover_info import (
    XPUOpTestWrapper,
    create_test_class,
    get_xpu_op_support_types,
)


class XPUTestMulOp(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'mul'
        self.use_dynamic_create_class = False

    class TestXPUMulOp1(XPUOpTest):
        def setUp(self):
            self.op_type = "mul"
            self.dtype = self.in_type
            self.inputs = {
                'X': np.random.random((3, 4, 2, 9)).astype(self.in_type_str),
                'Y': np.random.random((3, 6, 1, 2, 3)).astype(self.in_type_str),
            }
            self.attrs = {
                'x_num_col_dims': 2,
                'y_num_col_dims': 2,
            }
            result = np.dot(
                self.inputs['X'].reshape(3 * 4, 2 * 9),
                self.inputs['Y'].reshape(3 * 6, 1 * 2 * 3),
            )
            result = result.reshape(3, 4, 1, 2, 3)
            self.outputs = {'Out': result}

        def test_check_output(self):
            paddle.enable_static()
            place = paddle.XPUPlace(0)
            self.check_output_with_place(place, atol=0.01)

        def test_check_grad_normal(self):
            place = paddle.XPUPlace(0)
            paddle.enable_static()
            self.check_grad_with_place(
                place, ['X', 'Y'], 'Out', max_relative_error=0.1
            )

        def test_check_grad_ingore_x(self):
            place = paddle.XPUPlace(0)
            paddle.enable_static()
            self.check_grad_with_place(
                place,
                ['Y'],
                'Out',
                max_relative_error=0.1,
                no_grad_set=set("X"),
            )

        def test_check_grad_ignore_y(self):
            place = paddle.XPUPlace(0)
            paddle.enable_static()
            self.check_grad_with_place(
                place,
                ['X'],
                'Out',
                max_relative_error=0.1,
                no_grad_set=set('Y'),
            )

    class TestXPUMulOp2(XPUOpTest):
        def setUp(self):
            self.op_type = "mul"
            self.use_xpu = True
            self.dtype = self.in_type
            self.inputs = {
                'X': np.random.random((20, 5)).astype(self.in_type_str),
                'Y': np.random.random((5, 21)).astype(self.in_type_str),
            }
            self.outputs = {'Out': np.dot(self.inputs['X'], self.inputs['Y'])}

        def test_check_output(self):
            place = paddle.XPUPlace(0)
            paddle.enable_static()
            self.check_output_with_place(place, atol=0.01)

        def test_check_grad_normal(self):
            place = paddle.XPUPlace(0)
            paddle.enable_static()
            self.check_grad_with_place(
                place, ['X', 'Y'], 'Out', max_relative_error=0.1
            )

        def test_check_grad_ingore_x(self):
            place = paddle.XPUPlace(0)
            paddle.enable_static()
            self.check_grad_with_place(
                place,
                ['Y'],
                'Out',
                max_relative_error=0.1,
                no_grad_set=set("X"),
            )

        def test_check_grad_ingore_y(self):
            place = paddle.XPUPlace(0)
            paddle.enable_static()
            self.check_grad_with_place(
                place,
                ['X'],
                'Out',
                max_relative_error=0.1,
                no_grad_set=set('Y'),
            )


support_types = get_xpu_op_support_types('mul')
for stype in support_types:
    create_test_class(globals(), XPUTestMulOp, stype)

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