test_activation_op_xpu.py 40.0 KB
Newer Older
1
#   Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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.

15

16
import os
T
tianshuo78520a 已提交
17
import unittest
18

R
RedContritio 已提交
19
import numpy as np
20
from eager_op_test import OpTest
R
RedContritio 已提交
21
from get_test_cover_info import (
22
    XPUOpTestWrapper,
23 24 25
    create_test_class,
    get_xpu_op_support_types,
)
R
RedContritio 已提交
26
from op_test_xpu import XPUOpTest
27

28
import paddle
29
import paddle.nn.functional as F
30

31
paddle.enable_static()
32

33 34

class TestActivationOPBase(XPUOpTest):
35
    def setUp(self):
36
        self.place = paddle.XPUPlace(0)
37
        self.init_dtype()
38
        self.set_shape()
39
        self.set_case()
40

41 42 43
    def set_shape(self):
        self.shape = [11, 17]

44 45
    def set_case(self):
        self.op_type = 'exp'
46
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
47
        out = np.exp(x)
48 49 50 51 52 53 54 55
        self.attrs = {'use_xpu': True}
        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}

    def init_dtype(self):
        self.dtype = np.float32

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

58 59
    def test_check_grad(self):
        self.check_grad_with_place(self.place, ['X'], 'Out')
60 61


62 63 64 65
class XPUTestExpOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'exp'
        self.use_dynamic_create_class = False
66

67 68 69 70
    class XPUTestExp(TestActivationOPBase):
        def set_case(self):
            self.op_type = 'exp'
            self.dtype = self.in_type
71

72 73 74 75 76
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            out = np.exp(x)
            self.attrs = {'use_xpu': True}
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}
77

78 79 80 81
    class XPUTestExp_ZeroDIm(TestActivationOPBase):
        def set_shape(self):
            self.shape = []

82

83 84 85
support_types = get_xpu_op_support_types('exp')
for stype in support_types:
    create_test_class(globals(), XPUTestExpOP, stype)
86 87


88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
class XPUTestSiluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'silu'
        self.use_dynamic_create_class = False

    class XPUTestSilu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "silu"
            self.dtype = self.in_type
            self.init_shape()

            np.random.seed(1024)
            x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
            out = x / (np.exp(-x) + 1)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}

107 108 109 110 111
        def test_check_output(self):
            self.set_env()
            self.check_output_with_place(self.place)
            self.delete_env()

112
        def test_check_grad(self):
113
            self.set_env()
114
            self.check_grad_with_place(self.place, ['X'], 'Out')
115
            self.delete_env()
116 117 118 119

        def init_shape(self):
            self.shape = [11, 17]

120 121 122 123 124 125
        def set_env(self):
            pass

        def delete_env(self):
            pass

126 127 128 129
    class TestSilu_ZeroDim(XPUTestSilu):
        def init_shape(self):
            self.shape = []

130 131 132 133 134 135 136 137 138
    class TestSilu_LUT(XPUTestSilu):
        def set_env(self):
            # set "XPU_PADDLE_ACT_LUT" env to enable lut
            os.environ['XPU_PADDLE_ACT_LUT'] = "1"

        def delete_env(self):
            if os.getenv('XPU_PADDLE_ACT_LUT'):
                del os.environ['XPU_PADDLE_ACT_LUT']

139 140 141 142 143 144 145 146 147 148

class TestSiluAPI(unittest.TestCase):
    # test paddle.nn.Silu, paddle.nn.functional.silu
    def setUp(self):
        self.x_np = np.random.uniform(-1, 1, [11, 17]).astype('float32')
        self.place = paddle.XPUPlace(0)

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
149
            x = paddle.static.data('X', [11, 17])
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
            out1 = F.silu(x)
            m = paddle.nn.Silu()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = self.x_np / (1 + np.exp(-self.x_np))
        for r in res:
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.silu(x)
        m = paddle.nn.Silu()
        out2 = m(x)
        out_ref = self.x_np / (1 + np.exp(-self.x_np))
        for r in [out1, out2]:
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
        paddle.enable_static()

    def test_errors(self):
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, F.silu, 1)
            # The input dtype must be float16, float32, float64.
175
            x_int32 = paddle.static.data(
176 177 178 179
                name='x_int32', shape=[11, 17], dtype='int32'
            )
            self.assertRaises(TypeError, F.silu, x_int32)
            # support the input dtype is float16
180
            x_fp16 = paddle.static.data(
181 182 183 184 185 186 187 188 189 190
                name='x_fp16', shape=[11, 17], dtype='float16'
            )
            F.silu(x_fp16)


support_types = get_xpu_op_support_types('silu')
for stype in support_types:
    create_test_class(globals(), XPUTestSiluOP, stype)


191 192 193 194
class XPUTestSigmoidOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'sigmoid'
        self.use_dynamic_create_class = False
195

196 197 198 199
    class XPUTestSigmoid(TestActivationOPBase):
        def set_case(self):
            self.op_type = "sigmoid"
            self.dtype = self.in_type
200 201
            self.init_config()
            out = 1 / (1 + np.exp(-self.x))
T
TTerror 已提交
202

203
            self.attrs = {'use_xpu': True}
204
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)}
205
            self.outputs = {'Out': out}
206

207 208 209
        def init_config(self):
            self.x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)

210 211 212 213
    class XPUTestSigmoid_ZeroDIm(XPUTestSigmoid):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, []).astype(self.dtype)

214 215 216 217 218 219 220 221 222 223 224 225 226 227
    class XPUTestSigmoid2(XPUTestSigmoid):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [100]).astype(self.dtype)

    class XPUTestSigmoid3(XPUTestSigmoid):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [10, 12, 15]).astype(self.dtype)

    class XPUTestSigmoid4(XPUTestSigmoid):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [19, 19]).astype(self.dtype)

    class XPUTestSigmoid5(XPUTestSigmoid):
        def init_config(self):
228 229 230
            self.x = np.random.uniform(-2, 2, [10, 20, 30, 40]).astype(
                self.dtype
            )
231

232

233 234 235
support_types = get_xpu_op_support_types('sigmoid')
for stype in support_types:
    create_test_class(globals(), XPUTestSigmoidOP, stype)
236 237


238 239 240 241
class XPUTestTanhOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'tanh'
        self.use_dynamic_create_class = False
242

243 244 245 246
    class XPUTestTanh(TestActivationOPBase):
        def set_case(self):
            self.op_type = "tanh"
            self.dtype = self.in_type
247

248 249 250 251 252
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            out = np.tanh(x)
            self.attrs = {'use_xpu': True}
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}
253 254


255 256 257
support_types = get_xpu_op_support_types('tanh')
for stype in support_types:
    create_test_class(globals(), XPUTestTanhOP, stype)
258

T
TTerror 已提交
259

260 261 262 263
class XPUTestSqrtOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'sqrt'
        self.use_dynamic_create_class = False
264

265 266 267 268
    class XPUTestSqrt(TestActivationOPBase):
        def set_case(self):
            self.op_type = "sqrt"
            self.dtype = self.in_type
269

270 271
            x = np.random.uniform(0.1, 1, [11, 17]).astype(self.dtype)
            out = np.sqrt(x)
272

273 274 275
            self.attrs = {'use_xpu': True}
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}
276

277

278 279 280
support_types = get_xpu_op_support_types('sqrt')
for stype in support_types:
    create_test_class(globals(), XPUTestSqrtOP, stype)
281 282


283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
class XPUTestFloorOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'floor'
        self.use_dynamic_create_class = False

    class XPUTestSqrt(TestActivationOPBase):
        def set_case(self):
            self.op_type = "floor"
            self.dtype = self.in_type

            x = np.random.uniform(0.1, 1, [11, 17]).astype(self.dtype)
            out = np.floor(x)

            self.attrs = {'use_xpu': True}
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}

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


support_types = get_xpu_op_support_types('floor')
for stype in support_types:
    create_test_class(globals(), XPUTestFloorOP, stype)


309 310 311 312
class XPUTestAbsOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'abs'
        self.use_dynamic_create_class = False
313

314 315 316 317
    class XPUTestAbs(TestActivationOPBase):
        def set_case(self):
            self.op_type = "abs"
            self.dtype = self.in_type
318

319 320 321 322 323 324 325
            x = np.random.uniform(-1, 1, [4, 25]).astype(self.dtype)
            # Because we set delta = 0.005 in calculating numeric gradient,
            # if x is too small, such as 0.002, x_neg will be -0.003
            # x_pos will be 0.007, so the numeric gradient is inaccurate.
            # we should avoid this
            x[np.abs(x) < 0.005] = 0.02
            out = np.abs(x)
T
TTerror 已提交
326

327 328 329
            self.attrs = {'use_xpu': True}
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}
330 331


332 333 334
support_types = get_xpu_op_support_types('abs')
for stype in support_types:
    create_test_class(globals(), XPUTestAbsOP, stype)
335

T
TTerror 已提交
336

337 338 339 340
class XPUTestReluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'relu'
        self.use_dynamic_create_class = False
341

342 343 344 345
    class XPUTestRelu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "relu"
            self.dtype = self.in_type
346

347 348 349 350
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            # The same reason with TestAbs
            x[np.abs(x) < 0.005] = 0.02
            out = np.maximum(x, 0)
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
            self.attrs = {'use_xpu': True}
            self.inputs = {'X': x}
            self.outputs = {'Out': out}


support_types = get_xpu_op_support_types('relu')
for stype in support_types:
    create_test_class(globals(), XPUTestReluOP, stype)


class XPUTestGeluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'gelu'
        self.use_dynamic_create_class = False

    class XPUTestGelu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "gelu"
            self.dtype = self.in_type

            approximate = False
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            out = gelu(x, approximate)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {"approximate": approximate, 'use_xpu': True}
379

380 381 382 383 384 385 386 387 388 389 390 391 392
    class XPUTestGeluApproximate(TestActivationOPBase):
        def set_case(self):
            self.op_type = "gelu"
            self.dtype = self.in_type

            approximate = True
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            out = gelu(x, approximate)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {"approximate": approximate, 'use_xpu': True}

393 394 395 396

support_types = get_xpu_op_support_types('gelu')
for stype in support_types:
    create_test_class(globals(), XPUTestGeluOP, stype)
397 398


399
def gelu(x, approximate):
400
    from scipy.special import erf
401

402
    if approximate:
403 404 405 406 407 408 409 410
        y_ref = (
            0.5
            * x
            * (
                1.0
                + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3)))
            )
        )
411 412 413 414 415
    else:
        y_ref = 0.5 * x * (1 + erf(x / np.sqrt(2)))
    return y_ref.astype(x.dtype)


416
class XPUTestHardSwishOP(XPUOpTestWrapper):
417 418 419
    def __init__(self):
        self.op_name = 'hard_swish'
        self.use_dynamic_create_class = False
P
procr 已提交
420

421 422 423 424
    class XPUTestHardSwish(TestActivationOPBase):
        def set_case(self):
            self.op_type = "hard_swish"
            self.dtype = self.in_type
P
procr 已提交
425

426 427 428 429 430 431 432 433 434 435 436 437 438 439
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            offset = 3.0
            threshold = 6.0
            scale = 6.0
            out = hard_swish(x, offset, threshold, scale)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('hard_swish')
for stype in support_types:
    create_test_class(globals(), XPUTestHardSwishOP, stype)
P
procr 已提交
440 441 442 443 444 445 446


def hard_swish(x, offset, threshold, scale):
    y_ref = np.minimum(threshold, np.maximum(0, x + offset)) * x / scale
    return y_ref.astype(x.dtype)


447 448 449 450
class XPUTestLogOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'log'
        self.use_dynamic_create_class = False
451

452 453 454 455
    class XPUTestLog(TestActivationOPBase):
        def set_case(self):
            self.op_type = "log"
            self.dtype = self.in_type
456
            x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
457
            out = np.log(x)
458

459 460 461
            self.attrs = {'use_xpu': True}
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}
462

463 464 465 466
    class TestLogCase_ZeroDim(XPUTestLog):
        def set_shape(self):
            self.shape = []

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    class TestLogCase1(XPUTestLog):
        def set_shape(self):
            self.shape = [1, 11, 17]

    class TestLogCase2(XPUTestLog):
        def set_shape(self):
            self.shape = [2, 2, 2]

    class TestLogCase3(XPUTestLog):
        def set_shape(self):
            self.shape = [2]

    class TestLogCase4(XPUTestLog):
        def set_shape(self):
            self.shape = [1, 2, 3, 4]

483

484 485 486
support_types = get_xpu_op_support_types('log')
for stype in support_types:
    create_test_class(globals(), XPUTestLogOP, stype)
487 488


489 490 491 492
class XPUTestSquareOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'square'
        self.use_dynamic_create_class = False
T
TTerror 已提交
493

494 495 496 497
    class XPUTestSquare(TestActivationOPBase):
        def set_case(self):
            self.op_type = "square"
            self.dtype = self.in_type
498 499
            self.init_config()
            out = np.square(self.x)
500

501
            self.attrs = {'use_xpu': True}
502
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)}
503
            self.outputs = {'Out': out}
504

505 506 507
        def init_config(self):
            self.x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)

508 509 510 511
    class XPUTestSquare_ZeroDim(XPUTestSquare):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, []).astype(self.dtype)

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    class XPUTestSquare2(XPUTestSquare):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [100]).astype(self.dtype)

    class XPUTestSquare3(XPUTestSquare):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [1, 15, 19]).astype(self.dtype)

    class XPUTestSquare4(XPUTestSquare):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [100, 10]).astype(self.dtype)

    class XPUTestSquare5(XPUTestSquare):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [1, 2, 5, 17]).astype(self.dtype)

528

529 530 531
support_types = get_xpu_op_support_types('square')
for stype in support_types:
    create_test_class(globals(), XPUTestSquareOP, stype)
532

T
TTerror 已提交
533

534 535 536 537
class XPUTestPowOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'pow'
        self.use_dynamic_create_class = False
T
TTerror 已提交
538

539
    class XPUTestPowBase(TestActivationOPBase):
540 541 542 543
        def set_case(self):
            self.op_type = "pow"
            self.dtype = self.in_type

544 545
            self.init_config()
            out = np.power(self.x, self.factor)
546

547 548
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)}
            self.attrs = {'factor': self.factor, 'use_xpu': True}
549 550
            self.outputs = {'Out': out}

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
        def init_config(self):
            self.x = np.random.uniform(-1, 2, [12]).astype(self.dtype)
            self.factor = 3.0

    class XPUTestPow1(XPUTestPowBase):
        def init_config(self):
            self.x = np.random.uniform(-1, 1, [1024, 8]).astype(self.dtype)
            self.factor = 1

    class XPUTestPow2(XPUTestPowBase):
        def init_config(self):
            self.x = np.random.uniform(-1, 1, [1024, 8]).astype(self.dtype)
            self.factor = 2

    class XPUTestPow3(XPUTestPowBase):
        def init_config(self):
567 568 569
            self.x = np.random.uniform(-2, 2, [4, 512, 15, 15]).astype(
                self.dtype
            )
570 571 572 573
            self.factor = 3

    class XPUTestPow4(XPUTestPowBase):
        def init_config(self):
574 575 576
            self.x = np.random.uniform(-2, 2, [4, 256, 22, 22]).astype(
                self.dtype
            )
577 578 579 580
            self.factor = 4

    class XPUTestPow5(XPUTestPowBase):
        def init_config(self):
581 582 583
            self.x = np.random.uniform(0, 1, [4, 256, 22, 22]).astype(
                self.dtype
            )
584 585 586 587 588 589 590
            self.factor = 1.2

    class XPUTestPow6(XPUTestPowBase):
        def init_config(self):
            self.x = np.random.uniform(0, 1, [1024, 8]).astype(self.dtype)
            self.factor = 3.2

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

support_types = get_xpu_op_support_types('pow')
for stype in support_types:
    create_test_class(globals(), XPUTestPowOP, stype)


class XPUTestLeakyReluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'leaky_relu'
        self.use_dynamic_create_class = False

    class XPUTestLeakyRelu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "leaky_relu"
            self.dtype = self.in_type

            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            alpha = np.random.uniform(
                0,
610 611
                1,
            )
612 613 614 615 616 617 618 619 620 621
            out = leaky_relu(x, alpha)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True, 'alpha': alpha}


support_types = get_xpu_op_support_types('leaky_relu')
for stype in support_types:
    create_test_class(globals(), XPUTestLeakyReluOP, stype)
T
TTerror 已提交
622 623 624


def leaky_relu(x, alpha):
625
    if alpha < 1:
T
TTerror 已提交
626 627 628 629 630 631
        y_ref = np.maximum(x, alpha * x)
    else:
        y_ref = np.minimum(x, alpha * x)
    return y_ref.astype(x.dtype)


632 633 634 635
class XPUTestReciprocalOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'reciprocal'
        self.use_dynamic_create_class = False
636

637 638 639 640
    class XPUTestRecipocal(TestActivationOPBase):
        def set_case(self):
            self.op_type = "reciprocal"
            self.dtype = self.in_type
641

642 643 644
            np.random.seed(1024)
            x = np.random.uniform(1, 2, [1111, 1117]).astype(self.dtype)
            out = np.reciprocal(x)
645

646 647 648
            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}
649 650


651 652 653
support_types = get_xpu_op_support_types('reciprocal')
for stype in support_types:
    create_test_class(globals(), XPUTestReciprocalOP, stype)
654 655


656 657 658 659
class XPUTestSoftPlusOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'softplus'
        self.use_dynamic_create_class = False
660

661 662 663 664
    class XPUTestSoftPlusBase(TestActivationOPBase):
        def set_case(self):
            self.op_type = "softplus"
            self.dtype = self.in_type
665

666 667 668 669 670 671 672 673
            self.init_config()
            beta = np.random.uniform(0, 1)
            threshold = np.random.uniform(0, 1)
            out = ref_softplus(self.x, beta, threshold)

            self.inputs = {'X': self.x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True, 'beta': beta, 'threshold': threshold}
674

675 676
        def init_config(self):
            self.x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
677

678 679 680 681
    class XPUTestSoftPlus_ZeroDim(XPUTestSoftPlusBase):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, []).astype(self.dtype)

682 683 684
    class XPUTestSoftPlus2(XPUTestSoftPlusBase):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [1024, 8]).astype(self.dtype)
685

686 687
    class XPUTestSoftPlus3(XPUTestSoftPlusBase):
        def init_config(self):
688 689 690
            self.x = np.random.uniform(-2, 2, [4, 512, 15, 15]).astype(
                self.dtype
            )
691

692 693
    class XPUTestSoftPlus4(XPUTestSoftPlusBase):
        def init_config(self):
694 695 696
            self.x = np.random.uniform(-2, 2, [4, 256, 22, 22]).astype(
                self.dtype
            )
697 698


699 700 701
support_types = get_xpu_op_support_types('softplus')
for stype in support_types:
    create_test_class(globals(), XPUTestSoftPlusOP, stype)
702 703 704 705


def ref_softplus(x, beta=1, threshold=20):
    x_beta = beta * x
706 707 708 709
    out = np.select(
        [x_beta <= threshold, x_beta > threshold],
        [np.log(1 + np.exp(x_beta)) / beta, x],
    )
710 711 712
    return out


L
Lijunhui 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
# XPU_KP unittests, these ops can be found from xpu_op_kpfirst_list.h
class XPUTestBReluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'brelu'
        self.use_dynamic_create_class = False

    class XPUTestBRelu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "brelu"
            self.dtype = self.in_type

            np.random.seed(1024)
            x = np.random.uniform(-5, 10, [10, 12]).astype(self.dtype)
            t_min = 1.0
            t_max = 4.0
            # The same with TestAbs
            x[np.abs(x - t_min) < 0.005] = t_min + 0.02
            x[np.abs(x - t_max) < 0.005] = t_max + 0.02
            t = np.copy(x)
            t[t < t_min] = t_min
            t[t > t_max] = t_max

            self.inputs = {'X': x}
            self.outputs = {'Out': t}
            self.attrs = {'use_xpu': True, 't_min': t_min, 't_max': t_max}


support_types = get_xpu_op_support_types('brelu')
for stype in support_types:
    create_test_class(globals(), XPUTestBReluOP, stype)


class XPUTestCeilOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'ceil'
        self.use_dynamic_create_class = False

    class XPUTestCeil(TestActivationOPBase):
        def set_case(self):
            self.op_type = "ceil"
            self.dtype = self.in_type

            np.random.seed(1024)
            x = np.random.uniform(-1, 1, [10, 12]).astype(self.dtype)
            out = np.ceil(x)

            self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('ceil')
for stype in support_types:
    create_test_class(globals(), XPUTestCeilOP, stype)


class XPUTestCeluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'celu'
        self.use_dynamic_create_class = False

    class XPUTestCelu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "celu"
            self.dtype = self.in_type

            alpha = 1.5
            x = np.random.uniform(-3, 3, [10, 12]).astype(self.dtype)
            out = ref_celu(x, alpha)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True, 'alpha': alpha}


support_types = get_xpu_op_support_types('celu')
for stype in support_types:
    create_test_class(globals(), XPUTestCeluOP, stype)


def ref_celu(x, alpha):
    out_ref = np.maximum(0, x) + np.minimum(0, alpha * (np.exp(x / alpha) - 1))
    return out_ref.astype(x.dtype)


class XPUTestEluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'elu'
        self.use_dynamic_create_class = False

    class XPUTestElu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "elu"
            self.dtype = self.in_type

808
            alpha = 1.0
L
Lijunhui 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
            x = np.random.uniform(-3, 3, [10, 12]).astype(self.dtype)
            out = ref_elu(x, alpha)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True, 'alpha': alpha}


support_types = get_xpu_op_support_types('elu')
for stype in support_types:
    create_test_class(globals(), XPUTestEluOP, stype)


def ref_elu(x, alpha):
    out_ref = np.where(x > 0, x, alpha * (np.exp(x) - 1))
    return out_ref.astype(x.dtype)


class XPUTestFloorOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'floor'
        self.use_dynamic_create_class = False

    class XPUTestFloor(TestActivationOPBase):
        def set_case(self):
            self.op_type = "floor"
            self.dtype = self.in_type

            np.random.seed(1024)
            x = np.random.uniform(-1, 1, [10, 12]).astype(self.dtype)
            out = np.floor(x)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('floor')
for stype in support_types:
    create_test_class(globals(), XPUTestFloorOP, stype)


class XPUTestHardShrinkOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'hard_shrink'
        self.use_dynamic_create_class = False

    class XPUTestHardShrink(TestActivationOPBase):
        def set_case(self):
            self.op_type = "hard_shrink"
            self.dtype = self.in_type

            threshold = 0.5
            # self.set_attrs()
            np.random.seed(1024)
            x = np.random.uniform(-1, 1, [10, 12]).astype(self.dtype) * 10
            out = ref_hardshrink(x, threshold)

            self.attrs = {'use_xpu': True}
            self.inputs = {'X': x}
            self.outputs = {'Out': out}


support_types = get_xpu_op_support_types('hard_shrink')
for stype in support_types:
    create_test_class(globals(), XPUTestHardShrinkOP, stype)


def ref_hardshrink(x, threshold):
    out = np.copy(x)
    out[(out >= -threshold) & (out <= threshold)] = 0
    return out


class XPUTestHardSigmoidOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'hard_sigmoid'
        self.use_dynamic_create_class = False

    class XPUTestHardSigmoid(TestActivationOPBase):
        def set_case(self):
            self.op_type = "hard_sigmoid"
            self.dtype = self.in_type
            self.slope = 0.166666666666667
            self.offset = 0.5

            x = np.random.uniform(-5, 5, [10, 12]).astype(self.dtype)
            lower_threshold = -self.offset / self.slope
897
            upper_threshold = (1.0 - self.offset) / self.slope
L
Lijunhui 已提交
898 899 900 901 902 903 904 905 906 907 908

            # Same reason as TestAbs
            delta = 0.005
            x[np.abs(x - lower_threshold) < delta] = lower_threshold - 0.02
            x[np.abs(x - upper_threshold) < delta] = upper_threshold - 0.02

            out = ref_hardsigmoid(x, self.slope, self.offset)

            self.attrs = {
                'use_xpu': True,
                'slope': self.slope,
909
                'offset': self.offset,
L
Lijunhui 已提交
910 911 912 913 914 915 916 917 918 919 920
            }
            self.inputs = {'X': x}
            self.outputs = {'Out': out}


support_types = get_xpu_op_support_types('hard_sigmoid')
for stype in support_types:
    create_test_class(globals(), XPUTestHardSigmoidOP, stype)


def ref_hardsigmoid(x, slope=0.166666666666667, offset=0.5):
921
    return np.maximum(np.minimum(x * slope + offset, 1.0), 0.0).astype(x.dtype)
L
Lijunhui 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046


class XPUTestLog1pOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'log1p'
        self.use_dynamic_create_class = False

    class XPUTestLog1p(TestActivationOPBase):
        def set_case(self):
            self.op_type = "log1p"
            self.dtype = self.in_type

            np.random.seed(1024)
            x = np.random.uniform(0.1, 1, [11, 17]).astype(self.dtype)
            out = np.log1p(x)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('log1p')
for stype in support_types:
    create_test_class(globals(), XPUTestLog1pOP, stype)


class XPUTestLogsigmoidOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'logsigmoid'
        self.use_dynamic_create_class = False

    class XPUTestLogsigmoid(TestActivationOPBase):
        def set_case(self):
            self.op_type = "logsigmoid"
            self.dtype = self.in_type

            np.random.seed(2048)
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            out = np.log(1 / (1 + np.exp(-x)))

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('logsigmoid')
for stype in support_types:
    create_test_class(globals(), XPUTestLogsigmoidOP, stype)


class XPUTestRelu6OP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'relu6'
        self.use_dynamic_create_class = False

    class XPUTestRelu6(TestActivationOPBase):
        def set_case(self):
            self.op_type = "relu6"
            self.dtype = self.in_type

            np.random.seed(1024)
            x = np.random.uniform(-1, 10, [10, 12]).astype(self.dtype)
            x[np.abs(x) < 0.005] = 0.02
            out = ref_relu6(x)

            self.attrs = {'use_xpu': True}
            self.inputs = {'X': x}
            self.outputs = {'Out': out}


support_types = get_xpu_op_support_types('relu6')
for stype in support_types:
    create_test_class(globals(), XPUTestRelu6OP, stype)


def ref_relu6(x, threshold=6.0):
    out = np.copy(x)
    out[np.abs(x - threshold) < 0.005] = threshold + 0.02
    out = np.minimum(np.maximum(x, 0), threshold)
    return out


class XPUTestSiluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'silu'
        self.use_dynamic_create_class = False

    class XPUTestSilu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "silu"
            self.dtype = self.in_type

            np.random.seed(1024)
            x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
            out = x / (np.exp(-x) + 1)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('silu')
for stype in support_types:
    create_test_class(globals(), XPUTestSiluOP, stype)


class XPUTestSoftReluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'soft_relu'
        self.use_dynamic_create_class = False

    class XPUTestSoftRelu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "soft_relu"
            self.dtype = self.in_type

            np.random.seed(4096)
            x = np.random.uniform(-3, 3, [4, 4]).astype(self.dtype)
            threshold = 2.0
            # The same reason with TestAbs
            x[np.abs(x - threshold) < 0.005] = threshold + 0.02
            x[np.abs(x + threshold) < 0.005] = -threshold - 0.02
            t = np.copy(x)
            t[t < -threshold] = -threshold
            t[t > threshold] = threshold
1047
            out = np.log(np.exp(t) + 1)
L
Lijunhui 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True, 'threshold': threshold}


support_types = get_xpu_op_support_types('soft_relu')
for stype in support_types:
    create_test_class(globals(), XPUTestSoftReluOP, stype)


class XPUTestSoftSignOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'softsign'
        self.use_dynamic_create_class = False

    class XPUTestSoftSign(TestActivationOPBase):
        def set_case(self):
            self.op_type = "softsign"
            self.dtype = self.in_type

            np.random.seed(1024)
            x = np.random.uniform(-1, 1, [10, 12]).astype(self.dtype)
            out = ref_softsign(x)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('softsign')
for stype in support_types:
    create_test_class(globals(), XPUTestSoftSignOP, stype)


def ref_softsign(x):
    out = np.divide(x, 1 + np.abs(x))
    return out


1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
class XPUTestSoftshrinkOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'softshrink'
        self.use_dynamic_create_class = False

    class XPUTestSoftshrink(TestActivationOPBase):
        def set_case(self):
            self.op_type = "softshrink"
            self.dtype = self.in_type

            threshold = 0.5
            np.random.seed(1023)
            x = np.random.uniform(0.25, 10, [10, 12]).astype(self.dtype)
            out = ref_softshrink(x, threshold)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('softshrink')
for stype in support_types:
    create_test_class(globals(), XPUTestSoftshrinkOP, stype)


def ref_softshrink(x, threshold=0.5):
    out = np.copy(x)
    out = (out < -threshold) * (out + threshold) + (out > threshold) * (
1116 1117
        out - threshold
    )
1118 1119 1120
    return out


L
Lijunhui 已提交
1121 1122 1123 1124 1125
class XPUTestSwishOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'swish'
        self.use_dynamic_create_class = False

1126
    class XPUTestSwishBase(TestActivationOPBase):
L
Lijunhui 已提交
1127 1128 1129 1130
        def set_case(self):
            self.op_type = "swish"
            self.dtype = self.in_type

1131 1132
            self.init_config()
            out = ref_swish(self.x)
L
Lijunhui 已提交
1133

1134
            self.inputs = {'X': self.x}
L
Lijunhui 已提交
1135 1136 1137
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}

1138 1139 1140
        def init_config(self):
            self.x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)

1141 1142 1143 1144
    class XPUTestSwish_ZeroDim(XPUTestSwishBase):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, []).astype(self.dtype)

1145 1146 1147 1148 1149 1150
    class XPUTestSwish2(XPUTestSwishBase):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [1024, 8]).astype(self.dtype)

    class XPUTestSwish3(XPUTestSwishBase):
        def init_config(self):
1151 1152 1153
            self.x = np.random.uniform(-2, 2, [4, 512, 15, 15]).astype(
                self.dtype
            )
1154 1155 1156

    class XPUTestSwish4(XPUTestSwishBase):
        def init_config(self):
1157 1158 1159
            self.x = np.random.uniform(-2, 2, [4, 256, 22, 22]).astype(
                self.dtype
            )
1160

L
Lijunhui 已提交
1161 1162 1163 1164 1165 1166 1167 1168

support_types = get_xpu_op_support_types('swish')
for stype in support_types:
    create_test_class(globals(), XPUTestSwishOP, stype)


def ref_swish(x):
    from scipy.special import expit
1169

L
Lijunhui 已提交
1170 1171 1172 1173
    out = x * expit(x)
    return out


1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
class XPUTestThresholdedReluOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'thresholded_relu'
        self.use_dynamic_create_class = False

    class XPUTestThresholdedRelu(TestActivationOPBase):
        def set_case(self):
            self.op_type = "thresholded_relu"
            self.dtype = self.in_type

            threshold = 1.0
            np.random.seed(1024)
            x = np.random.uniform(-20, 20, [10, 12]).astype(self.dtype)
            x[np.abs(x) < 0.005] = 0.02
            out = ref_thresholded_relu(x, threshold)

            self.inputs = {'X': x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}


support_types = get_xpu_op_support_types('thresholded_relu')
for stype in support_types:
    create_test_class(globals(), XPUTestThresholdedReluOP, stype)


def ref_thresholded_relu(x, threshold=1.0):
    out = (x > threshold) * x
    return out


1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
class XPUTestMishOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'mish'
        self.use_dynamic_create_class = False

    class XPUTestMishBase(TestActivationOPBase):
        def set_case(self):
            self.op_type = "mish"
            self.dtype = self.in_type

            self.init_config()
            threshold = np.random.uniform(0, 1)
            out = ref_mish(self.x, threshold)

            self.inputs = {'X': self.x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True, 'threshold': threshold}

        def init_config(self):
            self.x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)

1226 1227 1228 1229
    class XPUTestMish_ZeroDim(XPUTestMishBase):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, []).astype(self.dtype)

1230 1231 1232 1233 1234 1235
    class XPUTestMish2(XPUTestMishBase):
        def init_config(self):
            self.x = np.random.uniform(-2, 2, [1024, 8]).astype(self.dtype)

    class XPUTestMish3(XPUTestMishBase):
        def init_config(self):
1236 1237 1238
            self.x = np.random.uniform(-2, 2, [4, 512, 15, 15]).astype(
                self.dtype
            )
1239 1240 1241

    class XPUTestMish4(XPUTestMishBase):
        def init_config(self):
1242 1243 1244
            self.x = np.random.uniform(-2, 2, [4, 256, 22, 22]).astype(
                self.dtype
            )
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257


support_types = get_xpu_op_support_types('mish')
for stype in support_types:
    create_test_class(globals(), XPUTestMishOP, stype)


def ref_mish(x, threshold=20):
    sp = np.select([x <= threshold, x > threshold], [np.log(1 + np.exp(x)), x])
    out = x * np.tanh(sp)
    return out


1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
class XPUTestSinOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'sin'
        self.use_dynamic_create_class = False

    class XPUTestSinBase(TestActivationOPBase):
        def set_case(self):
            self.op_type = "sin"
            self.dtype = self.in_type

            self.init_config()
            out = np.sin(self.x)

            self.inputs = {'X': self.x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}

        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [11, 17]).astype(
                self.dtype
            )

    class XPUTestSin_ZeroDim(XPUTestSinBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, []).astype(self.dtype)

    class XPUTestSin2(XPUTestSinBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [1024, 8]).astype(
                self.dtype
            )

    class XPUTestSin3(XPUTestSinBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [4, 512, 15, 15]).astype(
                self.dtype
            )

    class XPUTestSin4(XPUTestSinBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [4, 256, 22, 22]).astype(
                self.dtype
            )


support_types = get_xpu_op_support_types('sin')
for stype in support_types:
    create_test_class(globals(), XPUTestSinOP, stype)


class XPUTestCosOP(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'cos'
        self.use_dynamic_create_class = False

    class XPUTestCosBase(TestActivationOPBase):
        def set_case(self):
            self.op_type = "cos"
            self.dtype = self.in_type

            self.init_config()
            out = np.cos(self.x)

            self.inputs = {'X': self.x}
            self.outputs = {'Out': out}
            self.attrs = {'use_xpu': True}

        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [11, 17]).astype(
                self.dtype
            )

    class XPUTestCos_ZeroDim(XPUTestCosBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, []).astype(self.dtype)

    class XPUTestCos2(XPUTestCosBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [1024, 8]).astype(
                self.dtype
            )

    class XPUTestCos3(XPUTestCosBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [4, 512, 15, 15]).astype(
                self.dtype
            )

    class XPUTestCos4(XPUTestCosBase):
        def init_config(self):
            self.x = np.random.uniform(-np.pi, np.pi, [4, 256, 22, 22]).astype(
                self.dtype
            )


support_types = get_xpu_op_support_types('cos')
for stype in support_types:
    create_test_class(globals(), XPUTestCosOP, stype)

1357 1358
if __name__ == "__main__":
    unittest.main()