test_matmul_v2_op_xpu.py 8.1 KB
Newer Older
Q
QingshuChen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

T
taixiurong 已提交
15
import unittest
16

T
taixiurong 已提交
17
import numpy as np
R
RedContritio 已提交
18
from get_test_cover_info import (
19
    XPUOpTestWrapper,
20 21 22
    create_test_class,
    get_xpu_op_support_types,
)
R
RedContritio 已提交
23
from op_test_xpu import XPUOpTest
Q
QingshuChen 已提交
24

25 26
import paddle

Q
QingshuChen 已提交
27 28 29 30 31 32 33

def reference_matmul(X, Y, transpose_X=False, transpose_Y=False):
    """Reference forward implementation using np.matmul."""
    # np.matmul does not support the transpose flags, so we manually
    # transpose X and Y appropriately.
    if transpose_X:
        if X.ndim == 1:
34
            X = X.reshape((X.size,))
Q
QingshuChen 已提交
35 36 37
        elif X.ndim == 2:
            X = X.T
        else:
38
            dim = list(range(len(X.shape)))
Q
QingshuChen 已提交
39 40 41 42
            dim[-1], dim[len(X.shape) - 2] = dim[len(X.shape) - 2], dim[-1]
            X = np.transpose(X, tuple(dim))
    if transpose_Y:
        if Y.ndim == 1:
43
            Y = Y.reshape((Y.size,))
Q
QingshuChen 已提交
44
        else:
45
            dim = list(range(len(Y.shape)))
Q
QingshuChen 已提交
46 47 48 49 50 51
            dim[-1], dim[len(Y.shape) - 2] = dim[len(Y.shape) - 2], dim[-1]
            Y = np.transpose(Y, tuple(dim))
    Out = np.matmul(X, Y)
    return Out


52 53 54 55 56 57 58 59 60 61 62
class XPUTestMatmulV2Op(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = "matmul_v2"
        self.use_dynamic_create_class = False

    class TestMatMulV2Op(XPUOpTest):
        """
        case 1
        """

        def config(self):
63 64
            self.x_shape = (100,)
            self.y_shape = (100,)
65 66 67 68 69 70 71
            self.trans_x = False
            self.trans_y = False

        def setUp(self):
            self.dtype = self.in_type
            self.config()
            self.op_type = "matmul_v2"
72 73
            if self.dtype == np.float16 or self.dtype == "float16":
                self.__class__.no_need_check_grad = True
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
            x = np.random.random(self.x_shape).astype(self.dtype)
            y = np.random.random(self.y_shape).astype(self.dtype)
            # -0.1 ~ 0.1
            x = -0.1 + 0.2 * x
            y = -0.1 + 0.2 * y
            result = reference_matmul(x, y, self.trans_x, self.trans_y)
            result = result.astype(self.dtype)
            self.inputs = {
                'X': x,
                'Y': y,
            }
            self.attrs = {'trans_x': self.trans_x, 'trans_y': self.trans_y}
            self.outputs = {'Out': result}

        def test_check_output(self):
            place = paddle.XPUPlace(0)
            self.check_output_with_place(place)

        def test_check_grad(self):
93 94
            if (
                hasattr(self.__class__, "no_need_check_grad")
95
                and self.__class__.no_need_check_grad
96
            ):
97
                return
98 99 100 101 102 103 104 105 106
            place = paddle.XPUPlace(0)
            self.check_grad_with_place(place, ['X', 'Y'], 'Out')

    class TestMatMulOp2(TestMatMulV2Op):
        """
        case 2
        """

        def config(self):
107
            self.x_shape = 100
108 109 110 111 112 113 114 115 116 117
            self.y_shape = (100, 3)
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp3(TestMatMulV2Op):
        """
        case 3
        """

        def config(self):
118
            self.x_shape = (100,)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            self.y_shape = (1, 1, 100, 2)
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp4(TestMatMulV2Op):
        """
        case 4
        """

        def config(self):
            self.x_shape = (1, 1, 100, 1)
            self.y_shape = (1, 100)
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp5(TestMatMulV2Op):
        """
        case 5
        """

        def config(self):
            self.x_shape = (1, 1, 100, 1)
141
            self.y_shape = (100,)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            self.trans_x = True
            self.trans_y = False

    class TestMatMulOp6(TestMatMulV2Op):
        """
        case 6
        """

        def config(self):
            self.x_shape = (1, 2, 102, 10)
            self.y_shape = (2, 10, 111)
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp7(TestMatMulV2Op):
        """
        case 7
        """

        def config(self):
            self.x_shape = (1, 2, 100, 1)
            self.y_shape = (2, 100, 12)
            self.trans_x = True
            self.trans_y = False

    class TestMatMulOp8(TestMatMulV2Op):
        """
        case 8
        """

        def config(self):
            self.x_shape = (1, 1, 2, 100)
            self.y_shape = (1, 1, 100, 2)
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp9(TestMatMulV2Op):
        """
        case 9
        """

        def config(self):
184 185
            self.x_shape = (5, 20, 7)
            self.y_shape = (5, 7, 7)
186 187 188 189 190 191 192 193 194
            self.trans_x = False
            self.trans_y = True

    class TestMatMulOp10(TestMatMulV2Op):
        """
        case 10
        """

        def config(self):
195 196
            self.x_shape = (3, 20, 8)
            self.y_shape = (3, 20, 8)
197 198 199 200 201 202 203 204 205
            self.trans_x = True
            self.trans_y = False

    class TestMatMulOp11(TestMatMulV2Op):
        """
        case 11
        """

        def config(self):
206 207
            self.x_shape = (2, 20, 11)
            self.y_shape = (11, 30)
208 209 210 211 212 213 214 215 216 217
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp12(TestMatMulV2Op):
        """
        case 12
        """

        def config(self):
            self.x_shape = (1, 20, 100)
218
            self.y_shape = (100,)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp13(TestMatMulV2Op):
        """
        case 13
        """

        def config(self):
            self.x_shape = (2, 2, 10, 10)
            self.y_shape = (2, 2, 10, 10)
            self.trans_x = True
            self.trans_y = False

    class TestMatMulOp14(TestMatMulV2Op):
        """
        case 14_1
        """

        def config(self):
239 240
            self.x_shape = (7, 2, 100, 10)
            self.y_shape = (7, 2, 10, 90)
241 242 243 244 245 246 247 248 249
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp15(TestMatMulV2Op):
        """
        case 14_2
        """

        def config(self):
250 251
            self.x_shape = (3, 2, 4, 10)
            self.y_shape = (3, 2, 4, 10)
252 253 254 255 256 257 258 259 260 261
            self.trans_x = False
            self.trans_y = True

    class TestMatMulOp17(TestMatMulV2Op):
        """
        case 17 : to check the gradient for special case
        """

        def config(self):
            self.x_shape = (2, 1, 100)
262
            self.y_shape = 100
263 264 265 266 267 268 269 270 271
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp18(TestMatMulV2Op):
        """
        case 18 : for ppyoloe model
        """

        def config(self):
272
            self.x_shape = (8, 11, 4, 17)
273
            self.y_shape = 17
274 275 276
            self.trans_x = False
            self.trans_y = False

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    class TestMatMulOp19(TestMatMulV2Op):
        """
        case 19 : (x.ndim <= 2) && (y.ndim >= 3),
                  x need to broadcast and trans_y is false
        """

        def config(self):
            self.x_shape = (10, 20)
            self.y_shape = (2, 20, 4)
            self.trans_x = False
            self.trans_y = False

    class TestMatMulOp20(TestMatMulV2Op):
        """
        case 20 : (x.ndim <= 2) && (y.ndim >= 3),
                  x need to broadcast and trans_y is false
        """

        def config(self):
            self.x_shape = (20, 10)
            self.y_shape = (2, 20, 4)
            self.trans_x = True
            self.trans_y = False

301 302 303 304

support_types = get_xpu_op_support_types('matmul_v2')
for stype in support_types:
    create_test_class(globals(), XPUTestMatmulV2Op, stype)
Q
QingshuChen 已提交
305 306

if __name__ == "__main__":
T
taixiurong 已提交
307
    paddle.enable_static()
Q
QingshuChen 已提交
308
    unittest.main()