test_dot_op.py 3.8 KB
Newer Older
L
liuwei1031 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#  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 paddle
import paddle.fluid as fluid
import unittest
import numpy as np
from op_test import OpTest, skip_check_grad_ci
from paddle.fluid.op import Operator
from paddle.fluid import compiler, Program, program_guard


class DotOp(OpTest):
    def setUp(self):
        self.op_type = "dot"
        self.init_dtype()
        self.init_input_output()

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

    def test_check_output(self):
        self.check_output()

    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'))

    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):
        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])
        self.out = np.sum(self.x * self.y, axis=1).reshape([11, 1])


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
            x1 = fluid.layers.data(name='x1', shape=[120], dtype="uint8")
            y1 = fluid.layers.data(name='y1', shape=[120], dtype="uint8")
76
            self.assertRaises(Exception, fluid.layers.dot, x1, y1)
L
liuwei1031 已提交
77 78 79

            x2 = fluid.layers.data(name='x2', shape=[2, 3], dtype="float32")
            y2 = fluid.layers.data(name='y2', shape=[2, 3], dtype="float32")
80
            self.assertRaises(Exception, fluid.layers.dot, x2, y2)
L
liuwei1031 已提交
81 82 83

            x3 = fluid.layers.data(name='x3', shape=[3], dtype="float32")
            y3 = fluid.layers.data(name='y3', shape=[2, 3], dtype="float32")
84
            self.assertRaises(Exception, fluid.layers.dot, x2, y3)
L
liuwei1031 已提交
85 86 87 88 89 90 91 92


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))
            self.assertTrue(
93
                np.allclose(fluid.layers.dot(x1, y1).numpy(), np.array([17])))
L
liuwei1031 已提交
94 95 96 97 98 99 100

            x1 = fluid.dygraph.to_variable(
                np.array([[1, 3], [3, 5]]).astype(np.float32))
            y1 = fluid.dygraph.to_variable(
                np.array([[2, 5], [6, 8]]).astype(np.float32))
            self.assertTrue(
                np.array_equal(
101
                    fluid.layers.dot(x1, y1).numpy(), np.array([[17], [58]])))
L
liuwei1031 已提交
102 103 104 105


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