test_activation_op.py 119.1 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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
import os
Q
qijun 已提交
16
import unittest
17
import warnings
J
joejiong 已提交
18

Q
qijun 已提交
19
import numpy as np
20
from op_test import OpTest, convert_float_to_uint16
21 22
from scipy.special import erf, expit

23
import paddle
J
joejiong 已提交
24 25
import paddle.fluid as fluid
import paddle.fluid.core as core
26
import paddle.nn.functional as F
27
from paddle.fluid import Program, program_guard
28
from paddle.fluid.layer_helper import LayerHelper
Q
qijun 已提交
29

30 31
paddle.enable_static()

Q
qijun 已提交
32

33
class TestSqrtOpError(unittest.TestCase):
Z
Zhaolong Xing 已提交
34 35 36 37
    def test_errors(self):
        with program_guard(Program(), Program()):
            # The input type of sqrt op must be Variable or numpy.ndarray.
            in1 = 1
38
            self.assertRaises(TypeError, paddle.sqrt, in1)
Z
Zhaolong Xing 已提交
39
            # The input dtype of sqrt op must be float16, float32, float64.
G
GGBond8488 已提交
40 41
            in2 = paddle.static.data(
                name='input2', shape=[-1, 12, 10], dtype="int32"
42
            )
43
            self.assertRaises(TypeError, paddle.sqrt, in2)
Z
Zhaolong Xing 已提交
44

G
GGBond8488 已提交
45 46
            in3 = paddle.static.data(
                name='input3', shape=[-1, 12, 10], dtype="float16"
47
            )
48
            paddle.sqrt(x=in3)
Z
Zhaolong Xing 已提交
49 50


C
chengduo 已提交
51
class TestActivation(OpTest):
Q
qijun 已提交
52 53
    def setUp(self):
        self.op_type = "exp"
54
        self.init_dtype()
55
        self.init_shape()
56
        self.init_kernel_type()
C
chentianyu03 已提交
57 58
        self.check_eager = True
        self.python_api = paddle.exp
59

60
        np.random.seed(2049)
61
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
62 63 64 65
        out = np.exp(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
Q
qijun 已提交
66 67

    def test_check_output(self):
68 69 70 71
        check_eager = False
        if hasattr(self, 'check_eager'):
            check_eager = self.check_eager
        self.check_output(check_eager=check_eager)
Q
qijun 已提交
72 73

    def test_check_grad(self):
74 75
        if self.dtype == np.float16:
            return
76 77 78 79
        check_eager = False
        if hasattr(self, 'check_eager'):
            check_eager = self.check_eager
        self.check_grad(['X'], 'Out', check_eager=check_eager)
Q
qijun 已提交
80

81
    def init_dtype(self):
82
        self.dtype = np.float64
83

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

87 88 89
    def init_kernel_type(self):
        pass

Q
qijun 已提交
90

91 92 93 94 95
class TestActivation_ZeroDim(TestActivation):
    def init_shape(self):
        self.shape = []


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 121 122 123 124 125
class TestExpPrimFp32(OpTest):
    def setUp(self):
        self.op_type = "exp"
        self.prim_op_type = "prim"
        self.init_dtype()
        self.init_shape()
        self.python_api = paddle.exp

        np.random.seed(2049)
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
        out = np.exp(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
        self.skip_cinn()
        self.set_only_prim()

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out', check_prim=True)

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

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

    def skip_cinn(self):
126
        self.enable_cinn = True
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    def set_only_prim(self):
        pass


class TestExpPrimFp64(TestExpPrimFp32):
    def init_dtype(self):
        self.dtype = np.float64


class TestExpPrimFp16(TestExpPrimFp32):
    def init_dtype(self):
        self.dtype = np.float16

    def set_only_prim(self):
        self.only_prim = True

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out', check_prim=True)

    def skip_cinn(self):
151
        self.enable_cinn = True
152 153 154 155 156 157 158 159 160 161


class TestExpPrim_ZeroDim(TestExpPrimFp32):
    def init_shape(self):
        self.shape = []

    def skip_cinn(self):
        self.enable_cinn = False


R
ronnywang 已提交
162 163 164
class TestExpm1(TestActivation):
    def setUp(self):
        self.op_type = "expm1"
165
        self.python_api = paddle.expm1
R
ronnywang 已提交
166
        self.init_dtype()
167
        self.init_shape()
R
ronnywang 已提交
168 169

        np.random.seed(2049)
170
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
R
ronnywang 已提交
171 172 173 174 175 176
        out = np.expm1(x)

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

    def test_check_grad(self):
177 178 179 180
        self.check_grad(['X'], 'Out', check_eager=True)

    def test_check_output(self):
        self.check_output(check_eager=True)
R
ronnywang 已提交
181 182


183 184 185 186 187
class TestExpm1_ZeroDim(TestExpm1):
    def init_shape(self):
        self.shape = []


R
ronnywang 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
class TestExpm1API(unittest.TestCase):
    def init_dtype(self):
        self.dtype = 'float64'
        self.shape = [11, 17]

    def setUp(self):
        self.init_dtype()
        self.x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
        self.out_ref = np.expm1(self.x)

        self.place = [paddle.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.place.append(paddle.CUDAPlace(0))

    def test_static_api(self):
        paddle.enable_static()

        def run(place):
            with paddle.static.program_guard(paddle.static.Program()):
                X = paddle.fluid.data('X', self.shape, dtype=self.dtype)
                out = paddle.expm1(X)
                exe = paddle.static.Executor(place)
                res = exe.run(feed={'X': self.x})
            for r in res:
212
                np.testing.assert_allclose(self.out_ref, r, rtol=1e-05)
R
ronnywang 已提交
213 214 215 216 217 218 219 220 221

        for place in self.place:
            run(place)

    def test_dygraph_api(self):
        def run(place):
            paddle.disable_static(place)
            X = paddle.to_tensor(self.x)
            out = paddle.expm1(X)
222
            np.testing.assert_allclose(self.out_ref, out.numpy(), rtol=1e-05)
R
ronnywang 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235
            paddle.enable_static()

        for place in self.place:
            run(place)

    def test_errors(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            X = paddle.fluid.data('X', self.shape, dtype='int32')
            self.assertRaises(TypeError, paddle.expm1, X)
        # The input dtype must be float16, float32, float64.


236
class TestParameter:
237 238
    def test_out_name(self):
        with fluid.program_guard(fluid.Program()):
239 240
            if paddle.fluid.framework.in_dygraph_mode():
                paddle.enable_static()
G
GGBond8488 已提交
241 242
            np_x = np.array([0.1]).astype('float32').reshape((-1, 1))
            data = paddle.static.data(name="X", shape=[-1, 1], dtype="float32")
W
WuHaobo 已提交
243
            out = eval("paddle.%s(data, name='Y')" % self.op_type)
244 245
            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
246
            (result,) = exe.run(feed={"X": np_x}, fetch_list=[out])
W
WuHaobo 已提交
247
            expected = eval("np.%s(np_x)" % self.op_type)
248
            np.testing.assert_allclose(result, expected, rtol=1e-05)
249 250 251 252 253 254 255

    def test_dygraph(self):
        with fluid.dygraph.guard():
            np_x = np.array([0.1])
            x = fluid.dygraph.to_variable(np_x)
            z = eval("paddle.%s(x).numpy()" % self.op_type)
            z_expected = eval("np.%s(np_x)" % self.op_type)
256
            np.testing.assert_allclose(z, z_expected, rtol=1e-05)
257 258


C
chengduo 已提交
259
class TestSigmoid(TestActivation):
Q
qijun 已提交
260 261
    def setUp(self):
        self.op_type = "sigmoid"
Z
zxcd 已提交
262 263 264
        self.prim_op_type = "comp"
        self.enable_cinn = False
        self.python_api = paddle.nn.functional.sigmoid
265
        self.init_dtype()
266
        self.init_shape()
267

268
        np.random.seed(1024)
269
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
270 271 272 273
        out = 1 / (1 + np.exp(-x))

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
Q
qijun 已提交
274

275 276 277
    def init_dtype(self):
        self.dtype = np.float32

278
    def test_check_grad(self):
279 280
        if self.dtype == np.float16:
            return
Z
zxcd 已提交
281
        self.check_grad(['X'], 'Out', max_relative_error=0.01, check_prim=True)
282

283

284 285 286 287 288
class TestSigmoid_ZeroDim(TestSigmoid):
    def init_shape(self):
        self.shape = []


Z
zxcd 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
class TestSigmoidFP16(TestActivation):
    def setUp(self):
        self.op_type = "sigmoid"
        self.prim_op_type = "comp"
        self.enable_cinn = False
        self.only_prim = True
        self.python_api = paddle.nn.functional.sigmoid
        self.init_dtype()
        self.init_shape()

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

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

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

    def test_check_grad(self):
        self.check_grad(['X'], 'Out', max_relative_error=0.01, check_prim=True)

    def test_check_output(self):
        check_eager = False
        if hasattr(self, 'check_eager'):
            check_eager = self.check_eager
        self.check_output(check_eager=check_eager, check_prim=True)


319 320 321
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
322 323 324
class TestSigmoidBF16(OpTest):
    def setUp(self):
        self.op_type = "sigmoid"
Z
zxcd 已提交
325 326 327
        self.prim_op_type = "comp"
        self.enable_cinn = False
        self.python_api = paddle.nn.functional.sigmoid
328
        self.init_dtype()
329
        self.init_shape()
330 331

        np.random.seed(1024)
332
        x = np.random.uniform(-1, 1, self.shape).astype(np.float32)
333 334 335 336 337 338 339 340 341 342
        out = 1 / (1 + np.exp(-x))

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

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

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

346 347
    def test_check_output(self):
        place = core.CUDAPlace(0)
Z
zxcd 已提交
348
        # elementwise_pow can not support bfloat16, skip check_prim = True.
349 350 351 352 353 354 355
        self.check_output_with_place(place)

    def test_check_grad(self):
        place = core.CUDAPlace(0)
        self.check_grad_with_place(place, ['X'], 'Out')


356 357 358 359 360 361 362 363
'''
class TestSigmoidBF16_ZeroDim(TestSigmoidBF16):

    def init_shape(self):
        self.shape = []
'''


M
minghaoBD 已提交
364 365 366
class TestSilu(TestActivation):
    def setUp(self):
        self.op_type = "silu"
Z
zxcd 已提交
367
        self.prim_op_type = "comp"
368
        self.enable_cinn = True
Z
zxcd 已提交
369
        self.python_api = paddle.nn.functional.silu
M
minghaoBD 已提交
370
        self.init_dtype()
371
        self.init_shape()
M
minghaoBD 已提交
372 373

        np.random.seed(1024)
374
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
M
minghaoBD 已提交
375 376 377 378 379 380 381 382 383 384 385
        out = x / (np.exp(-x) + 1)

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

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
Z
zxcd 已提交
386
        self.check_grad(['X'], 'Out', check_prim=True)
M
minghaoBD 已提交
387 388


389 390 391
class TestSilu_ZeroDim(TestSilu):
    def init_shape(self):
        self.shape = []
392
        self.enable_cinn = False
393 394


Z
zxcd 已提交
395 396 397 398
class TestSiluFP16(TestActivation):
    def setUp(self):
        self.op_type = "silu"
        self.prim_op_type = "comp"
399
        self.enable_cinn = True
Z
zxcd 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
        self.only_prim = True
        self.python_api = paddle.nn.functional.silu
        self.init_dtype()
        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}

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

    def test_check_grad(self):
        self.check_grad(['X'], 'Out', check_prim=True)

    def test_check_output(self):
        check_eager = False
        if hasattr(self, 'check_eager'):
            check_eager = self.check_eager
        self.check_output(check_eager=check_eager, check_prim=True)


M
minghaoBD 已提交
425 426 427 428
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')
429 430 431
        self.place = (
            paddle.CUDAPlace(0)
            if core.is_compiled_with_cuda()
M
minghaoBD 已提交
432
            else paddle.CPUPlace()
433
        )
M
minghaoBD 已提交
434 435 436 437 438 439 440 441 442 443 444 445

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            x = paddle.fluid.data('X', [11, 17])
            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:
446
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
M
minghaoBD 已提交
447 448 449 450 451 452 453 454 455

    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]:
456
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
M
minghaoBD 已提交
457 458 459 460 461 462 463
        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.
464 465 466
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[11, 17], dtype='int32'
            )
M
minghaoBD 已提交
467 468
            self.assertRaises(TypeError, F.silu, x_int32)
            # support the input dtype is float16
469 470 471
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[11, 17], dtype='float16'
            )
M
minghaoBD 已提交
472 473 474
            F.silu(x_fp16)


C
chengduo 已提交
475
class TestLogSigmoid(TestActivation):
476 477
    def setUp(self):
        self.op_type = "logsigmoid"
478
        self.init_dtype()
479
        self.init_shape()
480

481
        np.random.seed(2048)
482
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
483 484
        out = np.log(1 / (1 + np.exp(-x)))

485
        self.inputs = {'X': x}
486
        self.outputs = {'Out': out}
487 488

    def test_check_grad(self):
489 490
        if self.dtype == np.float16:
            return
F
fengjiayi 已提交
491
        self.check_grad(['X'], 'Out', max_relative_error=0.008)
492 493


494 495 496 497 498
class TestLogSigmoid_ZeroDim(TestLogSigmoid):
    def init_shape(self):
        self.shape = []


499
class TestLogSigmoidAPI(unittest.TestCase):
500
    # test paddle.nn.LogSigmoid, paddle.nn.functional.log_sigmoid
501
    def setUp(self):
502
        np.random.seed(1024)
503
        self.x_np = np.random.uniform(-1, 1, [11, 17]).astype('float32')
504 505 506
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
507
            else paddle.CPUPlace()
508
        )
509 510

    def test_static_api(self):
511
        paddle.enable_static()
512
        with paddle.static.program_guard(paddle.static.Program()):
513
            x = paddle.fluid.data('X', [11, 17])
514
            out1 = F.log_sigmoid(x)
515 516 517 518 519 520
            m = paddle.nn.LogSigmoid()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = np.log(1 / (1 + np.exp(-self.x_np)))
        for r in res:
521
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
522 523 524 525

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
526
        out1 = F.log_sigmoid(x)
527 528 529 530
        m = paddle.nn.LogSigmoid()
        out2 = m(x)
        out_ref = np.log(1 / (1 + np.exp(-self.x_np)))
        for r in [out1, out2]:
531
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
532 533 534
        paddle.enable_static()

    def test_errors(self):
535
        paddle.enable_static()
536 537
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
538
            self.assertRaises(TypeError, F.log_sigmoid, 1)
539
            # The input dtype must be float16, float32, float64.
540 541 542
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[11, 17], dtype='int32'
            )
543
            self.assertRaises(TypeError, F.log_sigmoid, x_int32)
544
            # support the input dtype is float16
545 546 547
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[11, 17], dtype='float16'
            )
548
            F.log_sigmoid(x_fp16)
549 550


551
class TestTanh(TestActivation, TestParameter):
552 553
    def setUp(self):
        self.op_type = "tanh"
554
        self.init_dtype()
555 556
        self.init_shape()

557
        np.random.seed(1024)
558
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
559 560 561 562
        out = np.tanh(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
563 564

    def test_check_grad(self):
565 566
        if self.dtype == np.float16:
            return
567
        self.check_grad(['X'], 'Out')
568

569
    def init_dtype(self):
570
        # TODO If dtype is float64, the output (Out) has diff at CPUPlace
571 572 573 574
        # when using and not using inplace. Therefore, set dtype as float32
        # for now.
        self.dtype = np.float32

575

576 577 578 579 580
class TestTanh_ZeroDim(TestTanh):
    def init_shape(self):
        self.shape = []


W
WangXi 已提交
581 582 583 584
class TestTanhAPI(unittest.TestCase):
    # test paddle.tanh, paddle.nn.tanh, paddle.nn.functional.tanh
    def setUp(self):
        self.dtype = 'float32'
585
        np.random.seed(1024)
W
WangXi 已提交
586
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(self.dtype)
587 588 589
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
W
WangXi 已提交
590
            else paddle.CPUPlace()
591
        )
592 593 594 595
        self.executed_api()

    def executed_api(self):
        self.tanh = F.tanh
W
WangXi 已提交
596 597

    def test_static_api(self):
598
        paddle.enable_static()
W
WangXi 已提交
599
        with paddle.static.program_guard(paddle.static.Program()):
600
            x = paddle.fluid.data('X', [10, 12], self.dtype)
601
            out1 = self.tanh(x)
W
WangXi 已提交
602 603 604 605 606 607
            th = paddle.nn.Tanh()
            out2 = th(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = np.tanh(self.x_np)
        for r in res:
608
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
W
WangXi 已提交
609 610 611

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
Z
Zhou Wei 已提交
612
        x = paddle.to_tensor(self.x_np)
W
WangXi 已提交
613 614 615 616 617 618
        out1 = F.tanh(x)
        out2 = paddle.tanh(x)
        th = paddle.nn.Tanh()
        out3 = th(x)
        out_ref = np.tanh(self.x_np)
        for r in [out1, out2, out3]:
619
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
W
WangXi 已提交
620 621 622
        paddle.enable_static()

    def test_errors(self):
623
        paddle.enable_static()
W
WangXi 已提交
624 625
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
626
            self.assertRaises(TypeError, self.tanh, 1)
W
WangXi 已提交
627
            # The input dtype must be float16, float32.
628 629 630
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
631
            self.assertRaises(TypeError, self.tanh, x_int32)
W
WangXi 已提交
632
            # support the input dtype is float16
633 634 635
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
636 637 638 639 640 641 642
            self.tanh(x_fp16)


class TestTanhInplaceAPI(TestTanhAPI):
    # test paddle.tanh_
    def executed_api(self):
        self.tanh = paddle.tanh_
W
WangXi 已提交
643 644


645
class TestAtan(TestActivation, TestParameter):
646 647 648
    def setUp(self):
        self.op_type = "atan"
        self.init_dtype()
649
        self.init_shape()
650

651
        np.random.seed(1024)
652
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
653 654 655 656 657 658 659 660
        out = np.arctan(x)

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
661
        self.check_grad(['X'], 'Out')
662

W
WuHaobo 已提交
663 664
    def test_out_name(self):
        with fluid.program_guard(fluid.Program()):
G
GGBond8488 已提交
665 666
            np_x = np.array([0.1]).astype('float32').reshape((-1, 1))
            data = paddle.static.data(name="X", shape=[-1, 1], dtype="float32")
W
WuHaobo 已提交
667 668 669
            out = paddle.atan(data, name='Y')
            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
670
            (result,) = exe.run(feed={"X": np_x}, fetch_list=[out])
W
WuHaobo 已提交
671 672 673
            expected = np.arctan(np_x)
            self.assertEqual(result, expected)

674 675 676 677 678 679 680 681
    def test_dygraph(self):
        with fluid.dygraph.guard():
            np_x = np.array([0.1])
            x = fluid.dygraph.to_variable(np_x)
            z = paddle.atan(x).numpy()
            z_expected = np.arctan(np_x)
            self.assertEqual(z, z_expected)

682

683 684 685 686 687
class TestAtan_ZeroDim(TestTanh):
    def init_shape(self):
        self.shape = []


688 689 690 691
class TestSinh(TestActivation):
    def setUp(self):
        self.op_type = "sinh"
        self.init_dtype()
692
        self.init_shape()
693

694
        np.random.seed(1024)
695
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
696 697 698 699 700 701 702 703 704 705
        out = np.sinh(x)

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out')

706 707 708 709 710 711 712

class TestSinh_ZeroDim(TestSinh):
    def init_shape(self):
        self.shape = []


class TestSinhAPI(unittest.TestCase):
713 714 715 716
    def test_dygraph(self):
        with fluid.dygraph.guard():
            np_x = np.array([0.1])
            x = fluid.dygraph.to_variable(np_x)
717
            z = paddle.sinh(x).numpy()
718
            z_expected = np.sinh(np_x)
719
            np.testing.assert_allclose(z, z_expected, rtol=1e-05)
720 721 722 723

    def test_api(self):
        test_data_shape = [11, 17]
        with fluid.program_guard(fluid.Program(), fluid.Program()):
724 725 726
            input_x = np.random.uniform(0.1, 1, test_data_shape).astype(
                "float32"
            )
G
GGBond8488 已提交
727
            data_x = paddle.static.data(
728 729 730 731
                name="data_x",
                shape=test_data_shape,
                dtype="float32",
            )
732

733
            pd_sinh_out = paddle.sinh(data_x)
734 735
            exe = fluid.Executor(place=fluid.CPUPlace())
            exe.run(fluid.default_startup_program())
736 737 738 739 740
            (np_sinh_res,) = exe.run(
                fluid.default_main_program(),
                feed={"data_x": input_x},
                fetch_list=[pd_sinh_out],
            )
741 742

        expected_res = np.sinh(input_x)
743
        np.testing.assert_allclose(np_sinh_res, expected_res, rtol=1e-05)
744 745 746 747

    def test_backward(self):
        test_data_shape = [11, 17]
        with fluid.dygraph.guard():
748 749 750
            input_x = np.random.uniform(0.1, 1, test_data_shape).astype(
                "float32"
            )
751 752
            var = fluid.dygraph.to_variable(input_x)
            var.stop_gradient = False
753
            loss = paddle.sinh(var)
754 755 756 757 758 759 760 761 762
            loss.backward()
            grad_var = var.gradient()
            self.assertEqual(grad_var.shape, input_x.shape)


class TestSinhOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program()):
            # The input type must be Variable.
763
            self.assertRaises(TypeError, paddle.sinh, 1)
764 765
            # The input dtype must be float16, float32, float64.
            x_int32 = fluid.data(name='x_int32', shape=[12, 10], dtype='int32')
766
            self.assertRaises(TypeError, paddle.sinh, x_int32)
767 768
            # support the input dtype is float16
            x_fp16 = fluid.data(name='x_fp16', shape=[12, 10], dtype='float16')
769
            paddle.sinh(x_fp16)
770 771 772 773 774 775


class TestCosh(TestActivation):
    def setUp(self):
        self.op_type = "cosh"
        self.init_dtype()
776
        self.init_shape()
777

778
        np.random.seed(1024)
779
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
780 781 782 783 784 785 786 787 788 789
        out = np.cosh(x)

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out')

790 791 792 793 794 795 796

class TestCosh_ZeroDim(TestCosh):
    def init_shape(self):
        self.shape = []


class TestCoshAPI(unittest.TestCase):
797 798 799 800
    def test_dygraph(self):
        with fluid.dygraph.guard():
            np_x = np.array([0.1])
            x = fluid.dygraph.to_variable(np_x)
801
            z = paddle.cosh(x).numpy()
802
            z_expected = np.cosh(np_x)
803
            np.testing.assert_allclose(z, z_expected, rtol=1e-05)
804 805 806 807

    def test_api(self):
        test_data_shape = [11, 17]
        with fluid.program_guard(fluid.Program(), fluid.Program()):
808 809 810
            input_x = np.random.uniform(0.1, 1, test_data_shape).astype(
                "float32"
            )
G
GGBond8488 已提交
811
            data_x = paddle.static.data(
812 813 814 815
                name="data_x",
                shape=test_data_shape,
                dtype="float32",
            )
816 817 818 819

            pd_cosh_out = paddle.cosh(data_x)
            exe = fluid.Executor(place=fluid.CPUPlace())
            exe.run(fluid.default_startup_program())
820 821 822 823 824
            (np_cosh_res,) = exe.run(
                fluid.default_main_program(),
                feed={"data_x": input_x},
                fetch_list=[pd_cosh_out],
            )
825 826

        expected_res = np.cosh(input_x)
827
        np.testing.assert_allclose(np_cosh_res, expected_res, rtol=1e-05)
828 829 830 831

    def test_backward(self):
        test_data_shape = [11, 17]
        with fluid.dygraph.guard():
832 833 834
            input_x = np.random.uniform(0.1, 1, test_data_shape).astype(
                "float32"
            )
835 836
            var = fluid.dygraph.to_variable(input_x)
            var.stop_gradient = False
837
            loss = paddle.cosh(var)
838 839 840 841 842 843 844 845 846
            loss.backward()
            grad_var = var.gradient()
            self.assertEqual(grad_var.shape, input_x.shape)


class TestCoshOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program()):
            # The input type must be Variable.
847
            self.assertRaises(TypeError, paddle.cosh, 1)
848 849
            # The input dtype must be float16, float32, float64.
            x_int32 = fluid.data(name='x_int32', shape=[12, 10], dtype='int32')
850
            self.assertRaises(TypeError, paddle.cosh, x_int32)
851 852
            # support the input dtype is float16
            x_fp16 = fluid.data(name='x_fp16', shape=[12, 10], dtype='float16')
853
            paddle.cosh(x_fp16)
854 855


856 857 858 859 860 861
def ref_tanhshrink(x):
    out = x - np.tanh(x)
    return out


class TestTanhshrink(TestActivation):
K
Kavya Srinet 已提交
862 863
    def setUp(self):
        self.op_type = "tanh_shrink"
864
        self.init_dtype()
865
        self.init_shape()
866

867
        np.random.seed(1024)
868
        x = np.random.uniform(10, 20, self.shape).astype(self.dtype)
869
        out = ref_tanhshrink(x)
870

871
        self.inputs = {'X': x}
872
        self.outputs = {'Out': out}
K
Kavya Srinet 已提交
873 874

    def test_check_grad(self):
875 876
        if self.dtype == np.float16:
            return
877
        self.check_grad(['X'], 'Out')
K
Kavya Srinet 已提交
878

879

880 881 882 883 884
class TestTanhshrink_ZeroDim(TestTanhshrink):
    def init_shape(self):
        self.shape = []


885 886 887
class TestTanhshrinkAPI(unittest.TestCase):
    # test paddle.nn.Tanhshrink, paddle.nn.functional.tanhshrink
    def setUp(self):
888
        np.random.seed(1024)
889
        self.x_np = np.random.uniform(10, 20, [10, 17]).astype(np.float64)
890 891 892
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
893
            else paddle.CPUPlace()
894
        )
895 896

    def test_static_api(self):
897
        paddle.enable_static()
898
        with paddle.static.program_guard(paddle.static.Program()):
899
            x = paddle.fluid.data('X', self.x_np.shape, self.x_np.dtype)
900 901 902 903 904 905 906
            out1 = F.tanhshrink(x)
            tanhshrink = paddle.nn.Tanhshrink()
            out2 = tanhshrink(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_tanhshrink(self.x_np)
        for r in res:
907
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
908 909 910 911 912 913 914 915 916

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.tanhshrink(x)
        tanhshrink = paddle.nn.Tanhshrink()
        out2 = tanhshrink(x)
        out_ref = ref_tanhshrink(self.x_np)
        for r in [out1, out2]:
917
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
918 919 920
        paddle.enable_static()

    def test_errors(self):
921
        paddle.enable_static()
922 923 924 925
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, F.tanhshrink, 1)
            # The input dtype must be float16, float32, float64.
926 927 928
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
929 930
            self.assertRaises(TypeError, F.tanhshrink, x_int32)
            # support the input dtype is float16
931 932 933
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
934 935 936
            F.tanhshrink(x_fp16)


937 938 939 940 941 942
def ref_hardshrink(x, threshold):
    out = np.copy(x)
    out[(out >= -threshold) & (out <= threshold)] = 0
    return out


C
chengduo 已提交
943
class TestHardShrink(TestActivation):
944 945
    def setUp(self):
        self.op_type = "hard_shrink"
946
        self.init_dtype()
947
        self.init_shape()
948

949 950
        self.threshold = 0.5
        self.set_attrs()
951
        np.random.seed(1024)
952
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) * 10
953
        out = ref_hardshrink(x, self.threshold)
954

955
        self.attrs = {'threshold': self.threshold}
956
        self.inputs = {'X': x}
957
        self.outputs = {'Out': out}
958

959 960 961
    def init_shape(self):
        self.shape = [10, 12]

962 963 964
    def set_attrs(self):
        pass

965
    def test_check_grad(self):
966 967
        if self.dtype == np.float16:
            return
968
        self.check_grad(['X'], 'Out')
969 970


971 972 973 974 975
class TestHardShrink_threshold_negative(TestHardShrink):
    def set_attrs(self):
        self.threshold = -0.1


976 977 978 979 980 981 982 983
'''
class TestHardShrink_ZeroDim(TestHardShrink):

    def init_shape(self):
        self.shape = []
'''


984 985 986
class TestHardShrinkAPI(unittest.TestCase):
    # test paddle.nn.Hardshrink, paddle.nn.functional.hardshrink
    def setUp(self):
987
        np.random.seed(1024)
988
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
989 990 991
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
992
            else paddle.CPUPlace()
993
        )
994 995

    def test_static_api(self):
996
        paddle.enable_static()
997
        with paddle.static.program_guard(paddle.static.Program()):
998
            x = paddle.fluid.data('X', [10, 12])
999 1000 1001 1002 1003 1004 1005
            out1 = F.hardshrink(x)
            hd = paddle.nn.Hardshrink()
            out2 = hd(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_hardshrink(self.x_np, 0.5)
        for r in res:
1006
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
1007 1008 1009

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
Z
Zhou Wei 已提交
1010
        x = paddle.to_tensor(self.x_np)
1011 1012 1013 1014 1015
        out1 = F.hardshrink(x)
        hd = paddle.nn.Hardshrink()
        out2 = hd(x)
        out_ref = ref_hardshrink(self.x_np, 0.5)
        for r in [out1, out2]:
1016
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1017 1018 1019 1020 1021 1022

        out1 = F.hardshrink(x, 0.6)
        hd = paddle.nn.Hardshrink(0.6)
        out2 = hd(x)
        out_ref = ref_hardshrink(self.x_np, 0.6)
        for r in [out1, out2]:
1023
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1024 1025
        paddle.enable_static()

1026
    def test_errors(self):
1027
        paddle.enable_static()
1028
        with paddle.static.program_guard(paddle.static.Program()):
1029
            # The input type must be Variable.
1030
            self.assertRaises(TypeError, F.hardshrink, 1)
1031
            # The input dtype must be float16, float32, float64.
1032 1033 1034
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
1035
            self.assertRaises(TypeError, F.hardshrink, x_int32)
1036
            # support the input dtype is float16
1037 1038 1039
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
1040
            F.hardshrink(x_fp16)
1041 1042


1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
def ref_hardtanh(x, min=-1.0, max=1.0):
    out = np.copy(x)
    out[np.abs(x - min) < 0.005] = min + 0.02
    out[np.abs(x - max) < 0.005] = max + 0.02
    out = np.minimum(np.maximum(x, min), max)
    return out


class TestHardtanhAPI(unittest.TestCase):
    # test paddle.nn.Hardtanh, paddle.nn.functional.hardtanh
    def setUp(self):
1054
        np.random.seed(1024)
1055
        self.x_np = np.random.uniform(-3, 3, [10, 12]).astype('float32')
1056 1057 1058
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
1059
            else paddle.CPUPlace()
1060
        )
1061 1062

    def test_static_api(self):
1063
        paddle.enable_static()
1064
        with paddle.static.program_guard(paddle.static.Program()):
1065
            x = paddle.fluid.data('X', [10, 12])
1066 1067 1068 1069 1070 1071 1072
            out1 = F.hardtanh(x)
            m = paddle.nn.Hardtanh()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_hardtanh(self.x_np)
        for r in res:
1073
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
1074 1075 1076

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
Z
Zhou Wei 已提交
1077
        x = paddle.to_tensor(self.x_np)
1078 1079 1080 1081 1082
        out1 = F.hardtanh(x)
        m = paddle.nn.Hardtanh()
        out2 = m(x)
        out_ref = ref_hardtanh(self.x_np)
        for r in [out1, out2]:
1083
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1084 1085 1086 1087 1088 1089

        out1 = F.hardtanh(x, -2.0, 2.0)
        m = paddle.nn.Hardtanh(-2.0, 2.0)
        out2 = m(x)
        out_ref = ref_hardtanh(self.x_np, -2.0, 2.0)
        for r in [out1, out2]:
1090
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1091 1092 1093
        paddle.enable_static()

    def test_errors(self):
1094
        paddle.enable_static()
1095 1096 1097 1098
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, F.hardtanh, 1)
            # The input dtype must be float16, float32, float64.
1099 1100 1101
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
1102 1103
            self.assertRaises(TypeError, F.hardtanh, x_int32)
            # support the input dtype is float16
1104 1105 1106
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
1107 1108 1109
            F.hardtanh(x_fp16)


1110 1111 1112
def ref_softshrink(x, threshold=0.5):
    out = np.copy(x)
    out = (out < -threshold) * (out + threshold) + (out > threshold) * (
1113 1114
        out - threshold
    )
1115 1116 1117 1118
    return out


class TestSoftshrink(TestActivation):
1119 1120
    def setUp(self):
        self.op_type = "softshrink"
1121 1122
        self.check_eager = True
        self.python_api = paddle.nn.functional.softshrink
1123
        self.init_dtype()
1124
        self.init_shape()
1125

1126
        threshold = 0.8
1127

1128
        np.random.seed(1023)
1129
        x = np.random.uniform(0.25, 10, self.shape).astype(self.dtype)
1130 1131 1132
        out = ref_softshrink(x, threshold)
        self.inputs = {'X': x}
        self.attrs = {"lambda": threshold}
1133
        self.outputs = {'Out': out}
1134 1135

    def test_check_grad(self):
1136 1137
        if self.dtype == np.float16:
            return
1138
        self.check_grad(['X'], 'Out', check_eager=True)
1139

1140

1141 1142 1143 1144 1145
class TestSoftshrink_ZeroDim(TestSoftshrink):
    def init_shape(self):
        self.shape = []


1146 1147 1148 1149
class TestSoftshrinkAPI(unittest.TestCase):
    # test paddle.nn.Softshrink, paddle.nn.functional.softshrink
    def setUp(self):
        self.threshold = 0.8
1150
        np.random.seed(1024)
1151
        self.x_np = np.random.uniform(0.25, 10, [10, 12]).astype(np.float64)
1152 1153 1154
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
1155
            else paddle.CPUPlace()
1156
        )
1157 1158

    def test_static_api(self):
1159
        paddle.enable_static()
1160
        with paddle.static.program_guard(paddle.static.Program()):
1161
            x = paddle.fluid.data('X', self.x_np.shape, self.x_np.dtype)
1162 1163 1164 1165 1166 1167 1168
            out1 = F.softshrink(x, self.threshold)
            softshrink = paddle.nn.Softshrink(self.threshold)
            out2 = softshrink(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_softshrink(self.x_np, self.threshold)
        for r in res:
1169
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
1170 1171 1172 1173 1174 1175 1176 1177 1178

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.softshrink(x, self.threshold)
        softshrink = paddle.nn.Softshrink(self.threshold)
        out2 = softshrink(x)
        out_ref = ref_softshrink(self.x_np, self.threshold)
        for r in [out1, out2]:
1179
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1180 1181
        paddle.enable_static()

1182
    def test_errors(self):
1183
        paddle.enable_static()
1184
        with paddle.static.program_guard(paddle.static.Program()):
1185
            # The input type must be Variable.
1186
            self.assertRaises(TypeError, F.softshrink, 1)
1187
            # The input dtype must be float16, float32, float64.
1188 1189 1190
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
1191
            self.assertRaises(TypeError, F.softshrink, x_int32)
1192
            # The threshold must be no less than zero
1193 1194 1195
            x_fp32 = paddle.fluid.data(
                name='x_fp32', shape=[12, 10], dtype='float32'
            )
1196
            self.assertRaises(ValueError, F.softshrink, x_fp32, -1.0)
1197
            # support the input dtype is float16
1198 1199 1200
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
1201
            F.softshrink(x_fp16)
1202 1203


1204
class TestSqrt(TestActivation, TestParameter):
1205 1206
    def setUp(self):
        self.op_type = "sqrt"
1207
        self.prim_op_type = "prim"
1208
        self.python_api = paddle.sqrt
1209
        self.init_dtype()
1210
        self.init_shape()
1211

1212
        np.random.seed(1023)
1213
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
1214 1215 1216 1217
        out = np.sqrt(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
1218
        self.enable_cinn = False
1219

1220
    # TODO(wanghao107) add prim test
1221
    def test_check_grad(self):
1222 1223
        if self.dtype == np.float16:
            return
1224 1225 1226 1227
        self.check_grad(['X'], 'Out', check_eager=True)

    def test_check_output(self):
        self.check_output(check_eager=True)
1228

1229

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
class TestSqrtPrimFp32(TestActivation):
    def setUp(self):
        self.op_type = "sqrt"
        self.prim_op_type = "prim"
        self.python_api = paddle.sqrt
        self.init_dtype()
        self.init_shape()
        np.random.seed(1023)
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
        out = np.sqrt(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
1243
        self.enable_cinn = True
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)

    def test_check_output(self):
        self.check_output(check_eager=True)

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


1257 1258 1259
class TestSqrt_ZeroDim(TestSqrt):
    def init_shape(self):
        self.shape = []
1260
        self.enable_cinn = False
1261 1262


1263 1264 1265
class TestSqrtPrim_ZeroDim(TestSqrt):
    def init_shape(self):
        self.shape = []
1266
        self.enable_cinn = False
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out', check_prim=True)


1277 1278 1279
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
1280 1281 1282
class TestSqrtBF16(OpTest):
    def setUp(self):
        self.op_type = "sqrt"
1283
        self.prim_op_type = "prim"
1284
        self.python_api = paddle.sqrt
1285
        self.init_dtype()
1286
        self.init_shape()
1287 1288

        np.random.seed(1023)
1289
        x = np.random.uniform(0.1, 1, self.shape).astype(np.float32)
1290 1291 1292 1293 1294 1295
        out = np.sqrt(x)

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(convert_float_to_uint16(x))
        }
        self.outputs = {'Out': convert_float_to_uint16(out)}
1296 1297
        # TODO(wanghao107): add prim test
        self.enable_cinn = False
1298 1299 1300 1301

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

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

1305 1306
    def test_check_output(self):
        place = core.CUDAPlace(0)
1307
        self.check_output_with_place(place, check_eager=True)
1308 1309 1310

    def test_check_grad(self):
        place = core.CUDAPlace(0)
1311
        self.check_grad_with_place(place, ['X'], 'Out', check_eager=True)
1312 1313


Z
zhoukunsheng 已提交
1314 1315 1316
class TestRsqrt(TestActivation):
    def setUp(self):
        self.op_type = "rsqrt"
Z
zyfncg 已提交
1317
        self.python_api = paddle.rsqrt
Z
zhoukunsheng 已提交
1318
        self.init_dtype()
1319
        self.init_shape()
Z
zhoukunsheng 已提交
1320

1321
        np.random.seed(1024)
1322
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) * 10
Z
zhoukunsheng 已提交
1323 1324 1325 1326 1327
        out = 1.0 / np.sqrt(x)

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

1328 1329 1330
    def init_shape(self):
        self.shape = [10, 12]

Z
zhoukunsheng 已提交
1331 1332 1333
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
1334 1335 1336
        self.check_grad(
            ['X'], 'Out', max_relative_error=0.0005, check_eager=True
        )
Z
zhoukunsheng 已提交
1337 1338


1339 1340 1341 1342 1343 1344 1345 1346
'''
class TestRsqrt_ZeroDim(TestRsqrt):

    def init_shape(self):
        self.shape = []
'''


C
chengduo 已提交
1347
class TestAbs(TestActivation):
1348 1349
    def setUp(self):
        self.op_type = "abs"
1350
        self.init_dtype()
1351
        self.init_shape()
1352

1353
        np.random.seed(1024)
1354
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
C
chengduo 已提交
1355
        # Because we set delta = 0.005 in calculating numeric gradient,
Q
qijun 已提交
1356
        # if x is too small, such as 0.002, x_neg will be -0.003
C
chengduo 已提交
1357
        # x_pos will be 0.007, so the numeric gradient is inaccurate.
Q
qijun 已提交
1358 1359
        # we should avoid this
        x[np.abs(x) < 0.005] = 0.02
1360 1361 1362 1363
        out = np.abs(x)

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

1365 1366 1367
    def init_shape(self):
        self.shape = [4, 25]

1368
    def test_check_grad(self):
1369 1370
        if self.dtype == np.float16:
            return
1371
        self.check_grad(['X'], 'Out', check_eager=False)
1372

1373

1374 1375 1376 1377 1378
class TestAbs_ZeroDim(TestAbs):
    def init_shape(self):
        self.shape = []


C
chengduo 已提交
1379
class TestCeil(TestActivation):
D
dzhwinter 已提交
1380 1381
    def setUp(self):
        self.op_type = "ceil"
1382 1383
        self.check_eager = True
        self.python_api = paddle.ceil
1384
        self.init_dtype()
1385
        self.init_shape()
1386

1387
        np.random.seed(1024)
1388
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
1389 1390 1391 1392
        out = np.ceil(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
D
dzhwinter 已提交
1393

1394 1395 1396
    def init_shape(self):
        self.shape = [10, 12]

D
dzhwinter 已提交
1397
    # The same reason with TestFloor
C
chengduo 已提交
1398
    def test_check_grad(self):
1399 1400 1401
        pass


1402 1403 1404 1405 1406
class TestCeil_ZeroDim(TestCeil):
    def init_shape(self):
        self.shape = []


C
chengduo 已提交
1407
class TestFloor(TestActivation):
D
dzhwinter 已提交
1408 1409
    def setUp(self):
        self.op_type = "floor"
1410 1411
        self.check_eager = True
        self.python_api = paddle.floor
1412
        self.init_dtype()
1413
        self.init_shape()
1414

1415
        np.random.seed(1024)
1416
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
1417 1418 1419 1420
        out = np.floor(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
D
dzhwinter 已提交
1421

1422 1423 1424
    def init_shape(self):
        self.shape = [10, 12]

D
dzhwinter 已提交
1425
    # the gradient on floor, ceil, round is undefined.
1426
    # we return zero as gradient, but the numpy return nan
C
chengduo 已提交
1427 1428
    # The same reason with TestFloor
    def test_check_grad(self):
1429 1430 1431
        pass


1432 1433 1434 1435 1436
class TestFloor_ZeroDim(TestFloor):
    def init_shape(self):
        self.shape = []


1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
class TestFloorPrim(TestActivation):
    def setUp(self):
        self.op_type = "floor"
        self.prim_op_type = "prim"

        # the gradient on floor, ceil, round is undefined.
        # we return zero as gradient, but the numpy return nan.
        # for prim, we compare result with eager python api,
        # so, we use only_prim flag to express we only test prim.
        self.only_prim = True
        self.check_eager = True
        self.python_api = paddle.floor
        self.init_dtype()
        self.init_shape()

        if len(self.shape) == 0:
            # for 0-D tensor, skip cinn testing
            self.enable_cinn = False

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

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

    def init_shape(self):
        self.shape = [10, 12]

    def test_check_grad(self):
        self.check_grad(['X'], 'Out', check_prim=True)


class TestFloorPrim_ZeroDim(TestFloorPrim):
    def init_shape(self):
        self.shape = []


class TestFloorPrimFp16(TestFloorPrim):
    def init_dtype(self):
        self.dtype = np.float16


C
chengduo 已提交
1480
class TestCos(TestActivation):
C
add cos  
chengduoZH 已提交
1481 1482
    def setUp(self):
        self.op_type = "cos"
1483
        self.init_dtype()
1484
        self.init_shape()
1485

1486
        np.random.seed(1024)
1487
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
1488 1489 1490 1491
        out = np.cos(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
C
add sin  
chengduoZH 已提交
1492

1493 1494 1495
    def init_shape(self):
        self.shape = [10, 12]

C
add sin  
chengduoZH 已提交
1496
    def test_check_grad(self):
1497 1498
        if self.dtype == np.float16:
            return
1499
        self.check_grad(['X'], 'Out')
C
add sin  
chengduoZH 已提交
1500

1501

1502 1503 1504 1505 1506
class TestCos_ZeroDim(TestCos):
    def init_shape(self):
        self.shape = []


J
joejiong 已提交
1507 1508 1509 1510 1511
class TestTan(TestActivation):
    def setUp(self):
        np.random.seed(1024)
        self.op_type = "tan"
        self.init_dtype()
1512 1513
        self.init_shape()

J
joejiong 已提交
1514
        self.dtype = 'float32'
1515
        self.x_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
1516 1517 1518
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
J
joejiong 已提交
1519
            else paddle.CPUPlace()
1520
        )
J
joejiong 已提交
1521 1522 1523 1524 1525 1526

        out = np.tan(self.x_np)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x_np)}
        self.outputs = {'Out': out}

1527 1528 1529
    def init_shape(self):
        self.shape = [10, 12]

J
joejiong 已提交
1530 1531 1532 1533 1534
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out')

1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

class TestTan_ZeroDim(TestTan):
    def init_shape(self):
        self.shape = []


class TestTanAPI(unittest.TestCase):
    def setUp(self):
        np.random.seed(1024)
        self.dtype = 'float32'
        self.x_np = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype)
1546 1547 1548
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
1549
            else paddle.CPUPlace()
1550
        )
1551

J
joejiong 已提交
1552 1553 1554 1555 1556
    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out_test = paddle.tan(x)
        out_ref = np.tan(self.x_np)
1557
        np.testing.assert_allclose(out_ref, out_test.numpy(), rtol=1e-05)
J
joejiong 已提交
1558 1559 1560 1561 1562
        paddle.enable_static()

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
1563
            x = paddle.static.data('X', [11, 17], self.dtype)
J
joejiong 已提交
1564 1565 1566 1567
            out = paddle.tan(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = np.tan(self.x_np)
1568
        np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)
J
joejiong 已提交
1569 1570 1571 1572

    def test_backward(self):
        test_data_shape = [11, 17]
        with fluid.dygraph.guard():
1573 1574 1575
            input_x = np.random.uniform(0.1, 1, test_data_shape).astype(
                "float32"
            )
J
joejiong 已提交
1576 1577 1578 1579 1580 1581 1582 1583
            var = paddle.to_tensor(input_x)
            var.stop_gradient = False
            loss = paddle.tan(var)
            loss.backward()
            grad_var = var.gradient()
            self.assertEqual(grad_var.shape, input_x.shape)


1584 1585 1586 1587
class TestAcos(TestActivation):
    def setUp(self):
        self.op_type = "acos"
        self.init_dtype()
1588
        self.init_shape()
1589

1590
        np.random.seed(1024)
1591
        x = np.random.uniform(-0.95, 0.95, self.shape).astype(self.dtype)
1592 1593 1594 1595 1596
        out = np.arccos(x)

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

1597 1598 1599
    def init_shape(self):
        self.shape = [10, 12]

1600 1601 1602
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
1603
        self.check_grad(['X'], 'Out')
1604 1605


1606 1607 1608 1609 1610
class TestAcos_ZeroDim(TestAcos):
    def init_shape(self):
        self.shape = []


1611
class TestSin(TestActivation, TestParameter):
C
add sin  
chengduoZH 已提交
1612 1613
    def setUp(self):
        self.op_type = "sin"
1614
        self.init_dtype()
1615
        self.init_shape()
1616 1617
        # prim not support now
        self.enable_cinn = False
1618

1619
        np.random.seed(1024)
1620
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
1621 1622 1623 1624
        out = np.sin(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
C
add cos  
chengduoZH 已提交
1625

1626 1627 1628
    def init_shape(self):
        self.shape = [10, 12]

C
add cos  
chengduoZH 已提交
1629
    def test_check_grad(self):
1630 1631
        if self.dtype == np.float16:
            return
1632
        self.check_grad(['X'], 'Out')
C
add cos  
chengduoZH 已提交
1633 1634


1635 1636 1637 1638 1639
class TestSin_ZeroDim(TestSin):
    def init_shape(self):
        self.shape = []


1640 1641 1642 1643
class TestAsin(TestActivation):
    def setUp(self):
        self.op_type = "asin"
        self.init_dtype()
1644
        self.init_shape()
1645

1646
        np.random.seed(2048)
1647
        x = np.random.uniform(-0.95, 0.95, self.shape).astype(self.dtype)
1648 1649 1650 1651 1652
        out = np.arcsin(x)

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

1653 1654 1655
    def init_shape(self):
        self.shape = [10, 12]

1656 1657 1658
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
1659
        self.check_grad(['X'], 'Out')
1660 1661


1662 1663 1664 1665 1666
class TestAsin_ZeroDim(TestAsin):
    def init_shape(self):
        self.shape = []


X
xiaoting 已提交
1667 1668 1669 1670
class TestAcosh(TestActivation):
    def setUp(self):
        self.op_type = "acosh"
        self.init_dtype()
1671
        self.init_shape()
X
xiaoting 已提交
1672 1673

        np.random.seed(1024)
1674
        x = np.random.uniform(2, 3, self.shape).astype(self.dtype)
X
xiaoting 已提交
1675 1676 1677 1678 1679
        out = np.arccosh(x)

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

1680 1681 1682
    def init_shape(self):
        self.shape = [10, 12]

X
xiaoting 已提交
1683 1684 1685 1686 1687 1688
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out')


1689 1690 1691 1692 1693
class TestAcosh_ZeroDim(TestAcosh):
    def init_shape(self):
        self.shape = []


X
xiaoting 已提交
1694 1695 1696 1697
class TestAsinh(TestActivation):
    def setUp(self):
        self.op_type = "asinh"
        self.init_dtype()
1698
        self.init_shape()
X
xiaoting 已提交
1699 1700

        np.random.seed(1024)
1701
        x = np.random.uniform(1, 2, self.shape).astype(self.dtype)
X
xiaoting 已提交
1702 1703 1704 1705 1706
        out = np.arcsinh(x)

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

1707 1708 1709
    def init_shape(self):
        self.shape = [10, 12]

X
xiaoting 已提交
1710 1711 1712 1713 1714 1715
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out')


1716 1717 1718 1719 1720
class TestAsinh_ZeroDim(TestAsinh):
    def init_shape(self):
        self.shape = []


X
xiaoting 已提交
1721 1722 1723 1724
class TestAtanh(TestActivation):
    def setUp(self):
        self.op_type = "atanh"
        self.init_dtype()
1725
        self.init_shape()
X
xiaoting 已提交
1726 1727

        np.random.seed(400)
1728
        x = np.random.uniform(-0.9, 0.9, self.shape).astype(self.dtype)
X
xiaoting 已提交
1729 1730 1731 1732 1733
        out = np.arctanh(x)

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

1734 1735 1736
    def init_shape(self):
        self.shape = [10, 12]

X
xiaoting 已提交
1737 1738 1739 1740 1741 1742
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out')


1743 1744 1745 1746 1747
class TestAtanh_ZeroDim(TestAtanh):
    def init_shape(self):
        self.shape = []


C
chengduo 已提交
1748
class TestRound(TestActivation):
D
dzhwinter 已提交
1749 1750
    def setUp(self):
        self.op_type = "round"
1751 1752
        self.check_eager = True
        self.python_api = paddle.round
1753
        self.init_dtype()
1754
        self.init_shape()
1755

1756
        np.random.seed(1024)
1757
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
1758 1759 1760 1761
        out = np.round(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
D
dzhwinter 已提交
1762

1763 1764 1765
    def init_shape(self):
        self.shape = [10, 12]

C
chengduo 已提交
1766
    def test_check_grad(self):
1767 1768 1769
        pass


1770 1771 1772 1773 1774
class TestRound_ZeroDim(TestRound):
    def init_shape(self):
        self.shape = []


C
chengduo 已提交
1775
class TestRelu(TestActivation):
1776
    def setUp(self):
Q
qijun 已提交
1777
        self.op_type = "relu"
K
Kexin Zhao 已提交
1778
        self.init_dtype()
1779
        self.init_shape()
K
Kexin Zhao 已提交
1780

1781
        np.random.seed(1024)
1782
        if self.dtype == np.uint16:
1783
            x = np.random.uniform(-1, 1, self.shape).astype(np.float32)
1784 1785 1786 1787 1788
            # The same reason with TestAbs
            x[np.abs(x) < 0.005] = 0.02
            out = convert_float_to_uint16(np.maximum(x, 0))
            self.inputs = {'X': convert_float_to_uint16(x)}
        else:
1789
            x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
1790 1791 1792 1793
            # The same reason with TestAbs
            x[np.abs(x) < 0.005] = 0.02
            out = np.maximum(x, 0)
            self.inputs = {'X': x}
K
Kexin Zhao 已提交
1794 1795

        self.outputs = {'Out': out}
1796 1797

    def test_check_grad(self):
K
Kexin Zhao 已提交
1798 1799
        if self.dtype == np.float16:
            return
1800
        self.check_grad(['X'], 'Out')
A
Adam 已提交
1801 1802


1803 1804 1805 1806 1807
class TestRelu_ZeroDim(TestRelu):
    def init_shape(self):
        self.shape = []


1808 1809 1810
class TestReluAPI(unittest.TestCase):
    # test paddle.nn.ReLU, paddle.nn.functional.relu
    def setUp(self):
1811
        np.random.seed(1024)
1812
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
1813 1814 1815
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
1816
            else paddle.CPUPlace()
1817
        )
1818 1819 1820 1821
        self.executed_api()

    def executed_api(self):
        self.relu = F.relu
1822 1823

    def test_static_api(self):
1824
        paddle.enable_static()
1825
        with paddle.static.program_guard(paddle.static.Program()):
1826
            x = paddle.fluid.data('X', [10, 12])
1827
            out1 = self.relu(x)
1828 1829 1830 1831 1832 1833
            m = paddle.nn.ReLU()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = np.maximum(self.x_np, 0)
        for r in res:
1834
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
1835 1836 1837 1838 1839

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        m = paddle.nn.ReLU()
1840 1841
        out1 = m(x)
        out2 = self.relu(x)
1842 1843
        out_ref = np.maximum(self.x_np, 0)
        for r in [out1, out2]:
1844
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1845 1846
        paddle.enable_static()

1847
    def test_errors(self):
1848
        paddle.enable_static()
1849
        with paddle.static.program_guard(paddle.static.Program()):
1850
            # The input type must be Variable.
1851
            self.assertRaises(TypeError, self.relu, 1)
1852
            # The input dtype must be float16, float32, float64.
1853 1854 1855
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[10, 12], dtype='int32'
            )
1856
            self.assertRaises(TypeError, self.relu, x_int32)
1857
            # support the input dtype is float16
1858 1859 1860
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[10, 12], dtype='float16'
            )
1861 1862 1863 1864 1865 1866 1867
            self.relu(x_fp16)


class TestReluInplaceAPI(TestReluAPI):
    # test paddle.nn.functional.relu_
    def executed_api(self):
        self.relu = F.relu_
1868 1869


1870 1871 1872 1873 1874 1875
def ref_leaky_relu(x, alpha=0.01):
    out = np.copy(x)
    out[out < 0] *= alpha
    return out


A
Adam 已提交
1876
class TestLeakyRelu(TestActivation):
1877 1878 1879
    def get_alpha(self):
        return 0.02

A
Adam 已提交
1880 1881 1882
    def setUp(self):
        self.op_type = "leaky_relu"
        self.init_dtype()
1883
        self.init_shape()
1884
        alpha = self.get_alpha()
A
Adam 已提交
1885

1886
        np.random.seed(1024)
1887
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
A
Adam 已提交
1888
        # The same reason with TestAbs
1889 1890
        x[np.abs(x) < 0.005] = 0.05
        out = ref_leaky_relu(x, alpha)
A
Adam 已提交
1891

1892
        self.inputs = {'X': x}
A
Adam 已提交
1893
        self.outputs = {'Out': out}
1894
        self.attrs = {'alpha': alpha}
A
Adam 已提交
1895 1896 1897 1898

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
1899
        self.check_grad(['X'], 'Out')
1900 1901


1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
class TestLeakyReluAlpha1(TestLeakyRelu):
    def get_alpha(self):
        return 2


class TestLeakyReluAlpha2(TestLeakyRelu):
    def get_alpha(self):
        return -0.01


class TestLeakyReluAlpha3(TestLeakyRelu):
    def get_alpha(self):
        return -2.0


1917 1918 1919 1920 1921
class TestLeakyRelu_ZeroDim(TestLeakyRelu):
    def init_shape(self):
        self.shape = []


1922 1923 1924
class TestLeakyReluAPI(unittest.TestCase):
    # test paddle.nn.LeakyReLU, paddle.nn.functional.leaky_relu,
    def setUp(self):
1925
        np.random.seed(1024)
1926
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
1927 1928 1929
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
1930
            else paddle.CPUPlace()
1931
        )
1932 1933

    def test_static_api(self):
1934
        paddle.enable_static()
1935
        with paddle.static.program_guard(paddle.static.Program()):
1936
            x = paddle.fluid.data('X', [10, 12])
1937 1938 1939 1940 1941 1942 1943
            out1 = F.leaky_relu(x)
            m = paddle.nn.LeakyReLU()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_leaky_relu(self.x_np)
        for r in res:
1944
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
1945 1946 1947

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
Z
Zhou Wei 已提交
1948
        x = paddle.to_tensor(self.x_np)
1949 1950 1951 1952 1953
        out1 = F.leaky_relu(x)
        m = paddle.nn.LeakyReLU()
        out2 = m(x)
        out_ref = ref_leaky_relu(self.x_np)
        for r in [out1, out2]:
1954
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1955 1956 1957 1958 1959 1960

        out1 = F.leaky_relu(x, 0.6)
        m = paddle.nn.LeakyReLU(0.6)
        out2 = m(x)
        out_ref = ref_leaky_relu(self.x_np, 0.6)
        for r in [out1, out2]:
1961
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
1962 1963
        paddle.enable_static()

1964
    def test_errors(self):
1965
        paddle.enable_static()
1966
        with paddle.static.program_guard(paddle.static.Program()):
1967
            # The input type must be Variable.
1968
            self.assertRaises(TypeError, F.leaky_relu, 1)
1969
            # The input dtype must be float16, float32, float64.
1970 1971 1972
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
1973 1974
            self.assertRaises(TypeError, F.leaky_relu, x_int32)
            # support the input dtype is float16
1975 1976 1977
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
1978
            F.leaky_relu(x_fp16)
1979 1980


1981 1982
def gelu(x, approximate):
    if approximate:
1983 1984 1985 1986 1987 1988 1989 1990
        y_ref = (
            0.5
            * x
            * (
                1.0
                + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3)))
            )
        )
1991 1992 1993 1994 1995 1996
    else:
        y_ref = 0.5 * x * (1 + erf(x / np.sqrt(2)))
    return y_ref.astype(x.dtype)


class TestGeluApproximate(TestActivation):
C
Clementine 已提交
1997 1998 1999
    def setUp(self):
        self.op_type = "gelu"
        self.init_dtype()
2000
        self.init_shape()
2001
        approximate = True
2002
        np.random.seed(1024)
2003
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
2004
        out = gelu(x, approximate)
C
Clementine 已提交
2005

2006
        self.inputs = {'X': x}
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
        self.outputs = {'Out': out}
        self.attrs = {"approximate": approximate}

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
        self.check_grad(['X'], 'Out')


class TestGelu(TestActivation):
    def setUp(self):
        self.op_type = "gelu"
        self.init_dtype()
2020
        self.init_shape()
2021
        approximate = False
2022
        np.random.seed(2048)
2023
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
2024
        out = gelu(x, approximate)
C
Clementine 已提交
2025

2026
        self.inputs = {'X': x}
C
Clementine 已提交
2027
        self.outputs = {'Out': out}
2028
        self.attrs = {"approximate": approximate}
C
Clementine 已提交
2029 2030 2031 2032

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
2033
        self.check_grad(['X'], 'Out')
C
Clementine 已提交
2034 2035


2036 2037 2038 2039 2040
class TestGelu_ZeroDim(TestGelu):
    def init_shape(self):
        self.shape = []


2041 2042 2043
class TestGELUAPI(unittest.TestCase):
    # test paddle.nn.GELU, paddle.nn.functional.gelu
    def setUp(self):
2044
        np.random.seed(1024)
2045
        self.x_np = np.random.uniform(-1, 1, [11, 17]).astype('float32')
2046 2047 2048
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
2049
            else paddle.CPUPlace()
2050
        )
2051 2052

    def test_static_api(self):
2053
        paddle.enable_static()
2054
        with paddle.static.program_guard(paddle.static.Program()):
2055
            x = paddle.fluid.data('X', [11, 17])
2056 2057 2058 2059 2060 2061 2062
            out1 = F.gelu(x)
            m = paddle.nn.GELU()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = gelu(self.x_np, False)
        for r in res:
2063
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
2064 2065 2066 2067 2068 2069 2070 2071 2072

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.gelu(x)
        m = paddle.nn.GELU()
        out2 = m(x)
        out_ref = gelu(self.x_np, False)
        for r in [out1, out2]:
2073
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2074 2075 2076 2077 2078 2079

        out1 = F.gelu(x, True)
        m = paddle.nn.GELU(True)
        out2 = m(x)
        out_ref = gelu(self.x_np, True)
        for r in [out1, out2]:
2080
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2081 2082 2083
        paddle.enable_static()

    def test_errors(self):
2084
        paddle.enable_static()
2085 2086 2087 2088
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, F.gelu, 1)
            # The input dtype must be float16, float32, float64.
2089 2090 2091
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[11, 17], dtype='int32'
            )
2092 2093
            self.assertRaises(TypeError, F.gelu, x_int32)
            # support the input dtype is float16
2094 2095 2096
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[11, 17], dtype='float16'
            )
2097 2098 2099
            F.gelu(x_fp16)


C
chengduo 已提交
2100
class TestBRelu(TestActivation):
2101 2102
    def setUp(self):
        self.op_type = "brelu"
2103 2104
        self.init_dtype()

2105
        np.random.seed(1024)
Z
zhupengyang 已提交
2106
        x = np.random.uniform(-5, 10, [10, 12]).astype(self.dtype)
Y
Yang Yang(Tony) 已提交
2107 2108
        t_min = 1.0
        t_max = 4.0
Q
qijun 已提交
2109 2110
        # The same with TestAbs
        x[np.abs(x - t_min) < 0.005] = t_min + 0.02
Q
qijun 已提交
2111
        x[np.abs(x - t_max) < 0.005] = t_max + 0.02
2112 2113 2114
        t = np.copy(x)
        t[t < t_min] = t_min
        t[t > t_max] = t_max
2115 2116 2117

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.attrs = {'t_min': t_min, 't_max': t_max}
F
fengjiayi 已提交
2118
        self.outputs = {'Out': t}
2119 2120

    def test_check_grad(self):
2121 2122
        if self.dtype == np.float16:
            return
2123
        self.check_grad(['X'], 'Out')
2124

2125

2126 2127 2128 2129 2130 2131 2132
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


C
chengduo 已提交
2133
class TestRelu6(TestActivation):
K
Kavya Srinet 已提交
2134
    def setUp(self):
2135
        self.op_type = "relu6"
2136
        self.init_dtype()
2137
        self.init_shape()
2138
        self.python_api = paddle.nn.functional.relu6
2139

2140
        np.random.seed(1024)
2141
        x = np.random.uniform(-1, 10, self.shape).astype(self.dtype)
2142
        x[np.abs(x) < 0.005] = 0.02
2143
        out = ref_relu6(x)
2144

2145 2146
        self.inputs = {'X': x}
        self.attrs = {'threshold': 6.0}
2147
        self.outputs = {'Out': out}
K
Kavya Srinet 已提交
2148

2149 2150 2151
    def init_shape(self):
        self.shape = [10, 12]

2152 2153 2154
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
2155
        self.check_grad(['X'], 'Out', check_eager=True)
2156 2157


2158 2159 2160 2161 2162
class TestRelu6_ZeroDim(TestRelu6):
    def init_shape(self):
        self.shape = []


2163 2164 2165
class TestRelu6API(unittest.TestCase):
    # test paddle.nn.ReLU6, paddle.nn.functional.relu6
    def setUp(self):
2166
        np.random.seed(1024)
2167 2168
        self.x_np = np.random.uniform(-1, 10, [10, 12]).astype(np.float64)
        self.x_np[np.abs(self.x_np) < 0.005] = 0.02
2169 2170 2171
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
2172
            else paddle.CPUPlace()
2173
        )
2174 2175

    def test_static_api(self):
2176
        paddle.enable_static()
2177
        with paddle.static.program_guard(paddle.static.Program()):
2178
            x = paddle.fluid.data('X', self.x_np.shape, self.x_np.dtype)
2179 2180 2181 2182 2183 2184 2185
            out1 = F.relu6(x)
            relu6 = paddle.nn.ReLU6()
            out2 = relu6(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_relu6(self.x_np)
        for r in res:
2186
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
2187 2188 2189 2190 2191 2192 2193 2194 2195

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.relu6(x)
        relu6 = paddle.nn.ReLU6()
        out2 = relu6(x)
        out_ref = ref_relu6(self.x_np)
        for r in [out1, out2]:
2196
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2197 2198 2199
        paddle.enable_static()

    def test_fluid_api(self):
2200
        paddle.enable_static()
2201 2202
        with fluid.program_guard(fluid.Program()):
            x = fluid.data('X', self.x_np.shape, self.x_np.dtype)
2203
            out = paddle.nn.functional.relu6(x)
2204 2205 2206
            exe = fluid.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = ref_relu6(self.x_np)
2207
        np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)
2208

2209
    def test_errors(self):
2210
        paddle.enable_static()
2211
        with paddle.static.program_guard(paddle.static.Program()):
2212
            # The input type must be Variable.
2213
            self.assertRaises(TypeError, F.relu6, 1)
2214
            # The input dtype must be float16, float32, float64.
2215 2216 2217
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
2218
            self.assertRaises(TypeError, F.relu6, x_int32)
2219
            # support the input dtype is float16
2220 2221 2222
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
2223
            F.relu6(x_fp16)
2224 2225


2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
class TestRelu6APIWarnings(unittest.TestCase):
    def test_warnings(self):
        with warnings.catch_warnings(record=True) as context:
            warnings.simplefilter("always")

            paddle.enable_static()
            helper = LayerHelper("relu6")
            data = paddle.static.data(
                name='data', shape=[None, 3, 32, 32], dtype='float32'
            )
            out = helper.create_variable_for_type_inference(dtype=data.dtype)
            os.environ['FLAGS_print_extra_attrs'] = "1"
            helper.append_op(
                type="relu6",
                inputs={'X': data},
                outputs={'Out': out},
                attrs={'threshold': 6.0},
            )
            self.assertTrue(
                "op relu6 use extra_attr: threshold" in str(context[-1].message)
            )
            os.environ['FLAGS_print_extra_attrs'] = "0"


2250
def ref_hardswish(x, threshold=6.0, scale=6.0, offset=3.0):
Z
Zhang Ting 已提交
2251 2252 2253 2254
    x_dtype = x.dtype
    if x_dtype == 'float16':
        x_dtype = 'float16'
        x = x.astype('float32')
2255 2256 2257
    return (
        x * np.minimum(np.maximum(x + offset, 0.0), threshold) / scale
    ).astype(x_dtype)
2258 2259


H
huangjun12 已提交
2260 2261 2262 2263
class TestHardSwish(TestActivation):
    def setUp(self):
        self.op_type = 'hard_swish'
        self.init_dtype()
2264
        self.init_shape()
R
Roc 已提交
2265
        self.prim_op_type = "comp"
2266
        self.python_api = paddle.nn.functional.hardswish
J
jakpiase 已提交
2267

2268
        np.random.seed(1024)
2269
        x = np.random.uniform(-6, 6, self.shape).astype(self.dtype)
H
huangjun12 已提交
2270 2271 2272
        threshold = 6.0
        scale = 6.0
        offset = 3.0
2273
        # the same with TestAbs
H
huangjun12 已提交
2274 2275
        x[np.abs(x + offset) < 0.005] = 0.02
        x[np.abs(x - threshold + offset) < 0.005] = threshold - offset + 0.02
2276
        out = ref_hardswish(x, threshold, scale, offset)
H
huangjun12 已提交
2277

2278
        self.inputs = {'X': x}
H
huangjun12 已提交
2279 2280
        self.attrs = {'threshold': threshold, 'scale': scale, 'offset': offset}
        self.outputs = {'Out': out}
R
Roc 已提交
2281
        self.enable_cinn = False
H
huangjun12 已提交
2282

2283 2284 2285
    def init_shape(self):
        self.shape = [10, 12]

H
huangjun12 已提交
2286
    def test_check_grad(self):
R
Roc 已提交
2287
        self.check_grad(['X'], 'Out', check_eager=True, check_prim=True)
2288 2289

    def test_check_output(self):
R
Roc 已提交
2290
        self.check_output(check_eager=True, check_prim=True)
H
huangjun12 已提交
2291 2292


2293
class TestHardSwish_ZeroDim(TestHardSwish):
R
Roc 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
    def setUp(self):
        super().setUp()
        self.enable_cinn = False

    def init_shape(self):
        self.shape = []


class TestHardSwishFP16(TestHardSwish):
    def setUp(self):
        super().setUp()
        self.only_prim = True
        self.enable_cinn = False

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


class TestHardSwish_ZeroDim_FP16(TestHardSwishFP16):
    def setUp(self):
        super().setUp()
        self.enable_cinn = False

2317 2318 2319 2320
    def init_shape(self):
        self.shape = []


2321 2322 2323 2324
class TestHardswishAPI(unittest.TestCase):
    # test paddle.nn.Hardswish, paddle.nn.functional.hardswish
    def setUp(self):
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
2325 2326 2327
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
2328
            else paddle.CPUPlace()
2329
        )
2330 2331 2332

    def test_static_api(self):
        with paddle.static.program_guard(paddle.static.Program()):
2333
            x = paddle.fluid.data('X', self.x_np.shape, self.x_np.dtype)
2334 2335 2336 2337 2338 2339 2340
            out1 = F.hardswish(x)
            m = paddle.nn.Hardswish()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_hardswish(self.x_np)
        for r in res:
2341
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
2342 2343 2344

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
2345
        x = paddle.to_tensor([11648.0, 11448.0])
2346 2347 2348
        out1 = F.hardswish(x)
        m = paddle.nn.Hardswish()
        out2 = m(x)
2349
        out_ref = [11648.0, 11448.0]
2350
        for r in [out1, out2]:
2351
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2352
        paddle.enable_static()
2353 2354 2355 2356

    def test_fluid_api(self):
        with fluid.program_guard(fluid.Program()):
            x = fluid.data('X', self.x_np.shape, self.x_np.dtype)
2357
            out = paddle.nn.functional.hardswish(x)
2358 2359 2360
            exe = fluid.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = ref_hardswish(self.x_np)
2361
        np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)
2362 2363 2364

        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
2365
        out = paddle.nn.functional.hardswish(x)
2366
        np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05)
2367 2368 2369 2370
        paddle.enable_static()

    def test_errors(self):
        with paddle.static.program_guard(paddle.static.Program()):
2371
            # The input type must be Variable.
2372
            self.assertRaises(TypeError, F.hardswish, 1)
2373
            # The input dtype must be float16, float32, float64.
2374 2375 2376
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
2377
            self.assertRaises(TypeError, F.hardswish, x_int32)
2378
            # support the input dtype is float16
2379 2380 2381
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
2382
            F.hardswish(x_fp16)
2383 2384


C
chengduo 已提交
2385
class TestSoftRelu(TestActivation):
2386 2387
    def setUp(self):
        self.op_type = "soft_relu"
2388 2389
        self.init_dtype()

2390
        np.random.seed(4096)
2391
        x = np.random.uniform(-3, 3, [4, 4]).astype(self.dtype)
Y
Yang Yang(Tony) 已提交
2392
        threshold = 2.0
Q
qijun 已提交
2393 2394
        # The same reason with TestAbs
        x[np.abs(x - threshold) < 0.005] = threshold + 0.02
Z
zhupengyang 已提交
2395
        x[np.abs(x + threshold) < 0.005] = -threshold - 0.02
2396 2397 2398
        t = np.copy(x)
        t[t < -threshold] = -threshold
        t[t > threshold] = threshold
2399 2400 2401 2402 2403
        out = np.log((np.exp(t) + 1))

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.attrs = {'threshold': threshold}
        self.outputs = {'Out': out}
2404 2405

    def test_check_grad(self):
2406 2407
        if self.dtype == np.float16:
            return
F
fengjiayi 已提交
2408
        self.check_grad(['X'], 'Out', max_relative_error=0.02)
2409

2410

2411
def elu(x, alpha):
Z
zhupengyang 已提交
2412
    out_ref = np.where(x > 0, x, alpha * (np.exp(x) - 1))
2413 2414 2415
    return out_ref.astype(x.dtype)


C
chengduo 已提交
2416
class TestELU(TestActivation):
2417 2418
    def setUp(self):
        self.op_type = "elu"
2419
        self.init_dtype()
2420
        self.init_shape()
2421

2422
        np.random.seed(1024)
2423
        x = np.random.uniform(-3, 3, self.shape).astype(self.dtype)
Z
zhupengyang 已提交
2424
        alpha = self.get_alpha()
2425
        out = elu(x, alpha)
2426 2427 2428 2429
        # Note: unlike other Relu extensions, point 0 on standard ELU function (i.e. alpha = 1)
        # is differentiable, so we can skip modifications like x[np.abs(x) < 0.005] = 0.02 here
        self.inputs = {'X': x}
        self.attrs = {'alpha': alpha}
2430
        self.outputs = {'Out': out}
2431

2432 2433 2434
    def init_shape(self):
        self.shape = [10, 12]

2435
    def test_check_grad(self):
2436 2437
        if self.dtype == np.float16:
            return
2438
        self.check_grad(['X'], 'Out')
2439

Z
zhupengyang 已提交
2440
    def get_alpha(self):
2441
        return 1.0
Z
zhupengyang 已提交
2442 2443 2444 2445 2446 2447


class TestELUAlpha(TestELU):
    def get_alpha(self):
        return -0.2

2448

2449 2450 2451 2452 2453
class TestELU_ZeroDim(TestELU):
    def init_shape(self):
        self.shape = []


2454 2455 2456
class TestELUAPI(unittest.TestCase):
    # test paddle.nn.ELU, paddle.nn.functional.elu
    def setUp(self):
2457
        np.random.seed(1024)
2458
        self.x_np = np.random.uniform(-3, 3, [10, 12]).astype('float32')
2459 2460 2461
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
2462
            else paddle.CPUPlace()
2463
        )
2464 2465 2466 2467
        self.executed_api()

    def executed_api(self):
        self.elu = F.elu
2468 2469

    def test_static_api(self):
2470
        paddle.enable_static()
2471
        with paddle.static.program_guard(paddle.static.Program()):
2472
            x = paddle.fluid.data('X', [10, 12])
2473
            out1 = self.elu(x)
2474 2475 2476 2477 2478 2479
            m = paddle.nn.ELU()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = elu(self.x_np, 1.0)
        for r in res:
2480
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
2481 2482 2483 2484

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
2485 2486
        out1 = self.elu(x)
        x = paddle.to_tensor(self.x_np)
2487 2488 2489 2490
        m = paddle.nn.ELU()
        out2 = m(x)
        out_ref = elu(self.x_np, 1.0)
        for r in [out1, out2]:
2491
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2492

2493 2494
        out1 = self.elu(x, 0.2)
        x = paddle.to_tensor(self.x_np)
2495 2496 2497 2498
        m = paddle.nn.ELU(0.2)
        out2 = m(x)
        out_ref = elu(self.x_np, 0.2)
        for r in [out1, out2]:
2499
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2500 2501
        paddle.enable_static()

2502
    def test_errors(self):
2503
        paddle.enable_static()
2504 2505
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
2506
            self.assertRaises(TypeError, self.elu, 1)
2507
            # The input dtype must be float16, float32, float64.
2508 2509 2510
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[10, 12], dtype='int32'
            )
2511
            self.assertRaises(TypeError, self.elu, x_int32)
2512
            # support the input dtype is float16
2513 2514 2515
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[10, 12], dtype='float16'
            )
2516 2517 2518
            self.elu(x_fp16)


Z
zhupengyang 已提交
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
class TestELUInplaceAPI(TestELUAPI):
    # test paddle.nn.functional.elu_
    def executed_api(self):
        self.elu = F.elu_

    def test_alpha_error(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        self.assertRaises(Exception, F.elu_, x, -0.2)
        paddle.enable_static()


2531 2532 2533 2534 2535 2536 2537 2538 2539
def 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 TestCELU(TestActivation):
    def setUp(self):
        self.op_type = "celu"
        self.init_dtype()
2540
        self.init_shape()
2541

2542
        self.python_api = paddle.nn.functional.celu
2543
        np.random.seed(1024)
2544
        x = np.random.uniform(-3, 3, self.shape).astype(self.dtype)
2545 2546 2547 2548 2549 2550
        alpha = 1.5
        out = celu(x, alpha)
        self.inputs = {'X': x}
        self.attrs = {'alpha': alpha}
        self.outputs = {'Out': out}

2551 2552 2553
    def init_shape(self):
        self.shape = [10, 12]

2554 2555 2556
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
2557
        self.check_grad(['X'], 'Out', check_eager=True)
2558 2559


2560 2561 2562 2563 2564
class TestCELU_ZeroDim(TestCELU):
    def init_shape(self):
        self.shape = []


2565 2566 2567 2568 2569
class TestCELUAPI(unittest.TestCase):
    # test paddle.nn.CELU, paddle.nn.functional.celu
    def setUp(self):
        np.random.seed(1024)
        self.x_np = np.random.uniform(-3, 3, [10, 12]).astype('float32')
2570 2571 2572
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
2573
            else paddle.CPUPlace()
2574
        )
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
        self.executed_api()

    def executed_api(self):
        self.celu = F.celu

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            x = paddle.fluid.data('X', [10, 12])
            out1 = self.celu(x, 1.5)
            m = paddle.nn.CELU(1.5)
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = celu(self.x_np, 1.5)
        for r in res:
2591
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = self.celu(x, 1.5)
        x = paddle.to_tensor(self.x_np)
        m = paddle.nn.CELU(1.5)
        out2 = m(x)
        out_ref = celu(self.x_np, 1.5)
        for r in [out1, out2]:
2602
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2603 2604 2605 2606 2607 2608 2609

        out1 = self.celu(x, 0.2)
        x = paddle.to_tensor(self.x_np)
        m = paddle.nn.CELU(0.2)
        out2 = m(x)
        out_ref = celu(self.x_np, 0.2)
        for r in [out1, out2]:
2610
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
2611 2612 2613 2614 2615 2616 2617 2618
        paddle.enable_static()

    def test_errors(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, self.celu, 1)
            # The input dtype must be float16, float32, float64.
2619 2620 2621
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[10, 12], dtype='int32'
            )
2622 2623
            self.assertRaises(TypeError, self.celu, x_int32)
            # The alpha must be not equal 0
2624 2625 2626
            x_fp32 = paddle.fluid.data(
                name='x_fp32', shape=[10, 12], dtype='float32'
            )
2627 2628
            self.assertRaises(ZeroDivisionError, F.celu, x_fp32, 0)
            # support the input dtype is float16
2629 2630 2631
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[10, 12], dtype='float16'
            )
2632 2633 2634
            self.celu(x_fp16)


C
chengduo 已提交
2635
class TestReciprocal(TestActivation):
Q
qijun 已提交
2636 2637
    def setUp(self):
        self.op_type = "reciprocal"
2638
        self.python_api = paddle.reciprocal
2639
        self.init_dtype()
2640
        self.init_shape()
2641

2642
        np.random.seed(1024)
2643
        x = np.random.uniform(1, 2, self.shape).astype(self.dtype)
2644 2645 2646 2647
        out = np.reciprocal(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
Q
qijun 已提交
2648 2649

    def test_check_grad(self):
2650 2651
        if self.dtype == np.float16:
            return
2652 2653 2654 2655
        self.check_grad(['X'], 'Out', max_relative_error=0.01, check_eager=True)

    def test_check_output(self):
        self.check_output(check_eager=True)
Q
qijun 已提交
2656 2657


2658 2659 2660 2661 2662
class TestReciprocal_ZeroDim(TestReciprocal):
    def init_shape(self):
        self.shape = []


C
chengduo 已提交
2663
class TestLog(TestActivation):
Q
qijun 已提交
2664 2665
    def setUp(self):
        self.op_type = "log"
2666 2667
        self.check_eager = True
        self.python_api = paddle.log
2668
        self.init_dtype()
2669
        self.init_shape()
2670

2671
        np.random.seed(1024)
2672
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
2673 2674 2675 2676
        out = np.log(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
Q
qijun 已提交
2677 2678

    def test_check_grad(self):
2679 2680
        if self.dtype == np.float16:
            return
2681
        self.check_grad(['X'], 'Out', check_eager=True)
Q
qijun 已提交
2682

2683
    def test_error(self):
G
GGBond8488 已提交
2684 2685
        in1 = paddle.static.data(name="in1", shape=[11, 17], dtype="int32")
        in2 = paddle.static.data(name="in2", shape=[11, 17], dtype="int64")
2686

2687 2688
        self.assertRaises(TypeError, paddle.log, in1)
        self.assertRaises(TypeError, paddle.log, in2)
2689

2690

2691 2692 2693 2694 2695
class TestLog_ZeroDim(TestLog):
    def init_shape(self):
        self.shape = []


J
joejiong 已提交
2696 2697 2698
class TestLog2(TestActivation):
    def setUp(self):
        self.op_type = "log2"
2699 2700
        self.check_eager = True
        self.python_api = paddle.log2
J
joejiong 已提交
2701
        self.init_dtype()
2702
        self.init_shape()
J
joejiong 已提交
2703

2704
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
J
joejiong 已提交
2705 2706 2707 2708 2709 2710 2711 2712
        out = np.log2(x)

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
2713
        self.check_grad(['X'], 'Out', check_eager=True)
J
joejiong 已提交
2714 2715 2716 2717 2718 2719 2720 2721 2722

    def test_error(self):
        in1 = paddle.static.data(name="in1", shape=[11, 17], dtype="int32")
        in2 = paddle.static.data(name="in2", shape=[11, 17], dtype="int64")

        self.assertRaises(TypeError, paddle.log2, in1)
        self.assertRaises(TypeError, paddle.log2, in2)

    def test_api(self):
2723 2724 2725
        with paddle.static.program_guard(
            paddle.static.Program(), paddle.static.Program()
        ):
J
joejiong 已提交
2726
            input_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64")
2727 2728 2729
            data_x = paddle.static.data(
                name="data_x", shape=[11, 17], dtype="float64"
            )
J
joejiong 已提交
2730 2731 2732 2733

            out1 = paddle.log2(data_x)
            exe = paddle.static.Executor(place=fluid.CPUPlace())
            exe.run(paddle.static.default_startup_program())
2734 2735 2736 2737 2738
            (res1,) = exe.run(
                paddle.static.default_main_program(),
                feed={"data_x": input_x},
                fetch_list=[out1],
            )
J
joejiong 已提交
2739
        expected_res = np.log2(input_x)
2740
        np.testing.assert_allclose(res1, expected_res, rtol=1e-05)
J
joejiong 已提交
2741 2742 2743 2744 2745 2746 2747 2748

        # dygraph
        with fluid.dygraph.guard():
            np_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64")
            data_x = paddle.to_tensor(np_x)
            z = paddle.log2(data_x)
            np_z = z.numpy()
            z_expected = np.array(np.log2(np_x))
2749
        np.testing.assert_allclose(np_z, z_expected, rtol=1e-05)
J
joejiong 已提交
2750 2751


2752 2753 2754 2755 2756
class TestLog2_ZeroDim(TestLog2):
    def init_shape(self):
        self.shape = []


J
joejiong 已提交
2757 2758 2759
class TestLog10(TestActivation):
    def setUp(self):
        self.op_type = "log10"
2760 2761
        self.check_eager = True
        self.python_api = paddle.log10
J
joejiong 已提交
2762
        self.init_dtype()
2763
        self.init_shape()
J
joejiong 已提交
2764

2765
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
J
joejiong 已提交
2766 2767 2768 2769 2770 2771 2772 2773
        out = np.log10(x)

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
2774
        self.check_grad(['X'], 'Out', check_eager=True)
J
joejiong 已提交
2775

2776 2777 2778 2779 2780 2781 2782

class TestLog10_ZeroDim(TestLog10):
    def init_shape(self):
        self.shape = []


class TestLog10API(unittest.TestCase):
J
joejiong 已提交
2783 2784 2785 2786 2787 2788 2789 2790
    def test_error(self):
        in1 = paddle.static.data(name="in1", shape=[11, 17], dtype="int32")
        in2 = paddle.static.data(name="in2", shape=[11, 17], dtype="int64")

        self.assertRaises(TypeError, paddle.log10, in1)
        self.assertRaises(TypeError, paddle.log10, in2)

    def test_api(self):
2791 2792 2793
        with paddle.static.program_guard(
            paddle.static.Program(), paddle.static.Program()
        ):
J
joejiong 已提交
2794
            input_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64")
2795 2796 2797
            data_x = paddle.static.data(
                name="data_x", shape=[11, 17], dtype="float64"
            )
J
joejiong 已提交
2798 2799 2800 2801

            out1 = paddle.log10(data_x)
            exe = paddle.static.Executor(place=paddle.CPUPlace())
            exe.run(paddle.static.default_startup_program())
2802 2803 2804 2805 2806
            (res1,) = exe.run(
                paddle.static.default_main_program(),
                feed={"data_x": input_x},
                fetch_list=[out1],
            )
J
joejiong 已提交
2807
        expected_res = np.log10(input_x)
2808
        np.testing.assert_allclose(res1, expected_res, rtol=1e-05)
J
joejiong 已提交
2809 2810 2811 2812 2813 2814 2815 2816

        # dygraph
        with fluid.dygraph.guard():
            np_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64")
            data_x = paddle.to_tensor(np_x)
            z = paddle.log10(data_x)
            np_z = z.numpy()
            z_expected = np.array(np.log10(np_x))
2817
        np.testing.assert_allclose(np_z, z_expected, rtol=1e-05)
J
joejiong 已提交
2818 2819


2820 2821 2822
class TestLog1p(TestActivation):
    def setUp(self):
        self.op_type = "log1p"
2823 2824
        self.check_eager = True
        self.python_api = paddle.log1p
2825
        self.init_dtype()
2826
        self.init_shape()
2827

2828
        np.random.seed(1024)
2829
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
2830 2831 2832 2833 2834 2835 2836 2837
        out = np.log1p(x)

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

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
2838
        self.check_grad(['X'], 'Out', check_eager=True)
2839

2840 2841 2842 2843 2844 2845 2846

class TestLog1p_ZeroDim(TestLog1p):
    def init_shape(self):
        self.shape = []


class TestLog1pAPI(unittest.TestCase):
2847 2848 2849
    def test_api(self):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            input_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64")
G
GGBond8488 已提交
2850
            data_x = paddle.static.data(
2851 2852 2853 2854
                name="data_x",
                shape=[11, 17],
                dtype="float64",
            )
2855 2856 2857 2858

            out1 = paddle.log1p(data_x)
            exe = fluid.Executor(place=fluid.CPUPlace())
            exe.run(fluid.default_startup_program())
2859 2860 2861 2862 2863
            (res1,) = exe.run(
                fluid.default_main_program(),
                feed={"data_x": input_x},
                fetch_list=[out1],
            )
2864
        expected_res = np.log1p(input_x)
2865
        np.testing.assert_allclose(res1, expected_res, rtol=1e-05)
2866 2867 2868 2869 2870 2871 2872 2873

        # dygraph
        with fluid.dygraph.guard():
            np_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64")
            data_x = fluid.dygraph.to_variable(np_x)
            z = paddle.log1p(data_x)
            np_z = z.numpy()
            z_expected = np.array(np.log1p(np_x))
2874
        np.testing.assert_allclose(np_z, z_expected, rtol=1e-05)
2875 2876


C
chengduo 已提交
2877
class TestSquare(TestActivation):
Q
qijun 已提交
2878 2879
    def setUp(self):
        self.op_type = "square"
2880
        self.python_api = paddle.square
2881
        self.init_dtype()
2882
        self.init_shape()
2883

2884
        np.random.seed(1024)
2885
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
2886 2887 2888 2889
        out = np.square(x)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
        self.outputs = {'Out': out}
Q
qijun 已提交
2890 2891

    def test_check_grad(self):
2892 2893
        if self.dtype == np.float16:
            return
2894 2895 2896
        self.check_grad(
            ['X'], 'Out', max_relative_error=0.007, check_eager=True
        )
2897 2898 2899

    def test_check_output(self):
        self.check_output(check_eager=True)
Q
qijun 已提交
2900

2901

2902 2903 2904 2905 2906
class TestSquare_ZeroDim(TestSquare):
    def init_shape(self):
        self.shape = []


2907 2908 2909
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
2910 2911 2912
class TestSquareBF16(OpTest):
    def setUp(self):
        self.op_type = "square"
2913
        self.python_api = paddle.square
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
        self.init_dtype()

        np.random.seed(1024)
        x = np.random.uniform(0.1, 1, [11, 17]).astype(np.float32)
        out = np.square(x)

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

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

    def test_check_output(self):
        place = core.CUDAPlace(0)
2930
        self.check_output_with_place(place, check_eager=True)
2931 2932 2933

    def test_check_grad(self):
        place = core.CUDAPlace(0)
2934 2935 2936
        self.check_grad_with_place(
            place, ['X'], 'Out', numeric_grad_delta=0.5, check_eager=True
        )
2937 2938


C
chengduo 已提交
2939
class TestPow(TestActivation):
2940 2941
    def setUp(self):
        self.op_type = "pow"
2942
        self.python_api = paddle.pow
2943
        self.check_eager = True
2944
        self.init_dtype()
2945
        self.init_shape()
2946

2947
        np.random.seed(1024)
2948
        x = np.random.uniform(1, 2, self.shape).astype(self.dtype)
2949 2950 2951
        out = np.power(x, 3)

        self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
Y
Yang Yang(Tony) 已提交
2952
        self.attrs = {'factor': 3.0}
2953
        self.outputs = {'Out': out}
2954

2955 2956 2957
    def test_check_output(self):
        self.check_output(check_eager=self.check_eager)

2958
    def test_check_grad(self):
2959 2960
        if self.dtype == np.float16:
            return
2961
        self.check_grad(['X'], 'Out', check_eager=self.check_eager)
2962

2963

2964 2965 2966 2967 2968
class TestPow_ZeroDim(TestPow):
    def init_shape(self):
        self.shape = []


2969 2970 2971
class TestPow_factor_tensor(TestActivation):
    def setUp(self):
        self.op_type = "pow"
2972 2973
        self.check_eager = False
        self.python_api = paddle.pow
2974 2975
        self.init_dtype()

2976
        np.random.seed(1024)
2977 2978 2979 2980 2981
        x = np.random.uniform(1, 2, [11, 17]).astype(self.dtype)
        out = np.power(x, 3)

        self.inputs = {
            'X': OpTest.np_dtype_to_fluid_dtype(x),
2982
            'FactorTensor': np.array([3.0]).astype("float32"),
2983 2984 2985 2986 2987 2988
        }

        self.attrs = {}
        self.outputs = {'Out': out}

    def test_check_output(self):
2989
        self.check_output(check_eager=self.check_eager)
2990 2991 2992 2993

    def test_check_grad(self):
        if self.dtype == np.float16:
            return
2994
        self.check_grad(['X'], 'Out', check_eager=self.check_eager)
2995 2996 2997

    def test_api(self):
        input = np.random.uniform(1, 2, [11, 17]).astype("float32")
G
GGBond8488 已提交
2998 2999
        x = paddle.static.data(name="x", shape=[11, 17], dtype="float32")
        res = paddle.static.data(name="res", shape=[11, 17], dtype="float32")
3000 3001 3002

        factor_1 = 2.0
        factor_2 = fluid.layers.fill_constant([1], "float32", 3.0)
3003 3004
        out_1 = paddle.pow(x, factor_1)
        out_2 = paddle.pow(x, factor_2)
3005 3006 3007
        out_4 = paddle.pow(x, factor_1, name='pow_res')
        out_6 = paddle.pow(x, factor_2)
        self.assertEqual(('pow_res' in out_4.name), True)
3008 3009

        exe = fluid.Executor(place=fluid.CPUPlace())
W
WuHaobo 已提交
3010
        res_1, res_2, res, res_6 = exe.run(
3011 3012
            fluid.default_main_program(),
            feed={"x": input},
3013 3014
            fetch_list=[out_1, out_2, res, out_6],
        )
3015

3016 3017 3018
        assert np.allclose(res_1, np.power(input, 2))
        assert np.allclose(res_2, np.power(input, 3))
        assert np.allclose(res_6, np.power(input, 3))
3019 3020


3021 3022 3023 3024 3025
def ref_stanh(x, scale_a=0.67, scale_b=1.7159):
    out = scale_b * np.tanh(x * scale_a)
    return out


C
chengduo 已提交
3026
class TestSTanh(TestActivation):
3027 3028 3029 3030 3031 3032
    def get_scale_a(self):
        return 0.67

    def get_scale_b(self):
        return 1.7159

3033 3034
    def setUp(self):
        self.op_type = "stanh"
3035
        self.init_dtype()
3036 3037
        self.init_shape()

3038 3039
        scale_a = self.get_scale_a()
        scale_b = self.get_scale_b()
3040

3041
        np.random.seed(1024)
3042
        x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype)
3043 3044
        # The same reason with TestAbs
        out = ref_stanh(x, scale_a, scale_b)
3045

3046
        self.inputs = {'X': x}
3047
        self.attrs = {'scale_a': scale_a, 'scale_b': scale_b}
3048
        self.outputs = {'Out': out}
3049

Q
qijun 已提交
3050
    def test_check_grad(self):
3051 3052
        if self.dtype == np.float16:
            return
3053
        self.check_grad(['X'], 'Out')
Q
qijun 已提交
3054

3055

3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
class TestSTanhScaleA(TestSTanh):
    def get_scale_a(self):
        return 2.0


class TestSTanhScaleB(TestSTanh):
    def get_scale_b(self):
        return 0.5


3066 3067 3068 3069 3070
class TestSTanh_ZeroDim(TestSTanh):
    def init_shape(self):
        self.shape = []


3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
class TestSTanhAPI(unittest.TestCase):
    # test paddle.nn.stanh
    def get_scale_a(self):
        return 0.67

    def get_scale_b(self):
        return 1.7159

    def setUp(self):
        np.random.seed(1024)
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype('float32')
        self.scale_a = self.get_scale_a()
        self.scale_b = self.get_scale_b()
3084 3085 3086
        self.place = (
            paddle.CUDAPlace(0)
            if core.is_compiled_with_cuda()
3087
            else paddle.CPUPlace()
3088
        )
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            x = paddle.fluid.data('X', [10, 12])
            out = paddle.stanh(x, self.scale_a, self.scale_b)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = ref_stanh(self.x_np, self.scale_a, self.scale_b)
        for r in res:
3099
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
3100 3101 3102 3103 3104 3105 3106

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out = paddle.stanh(x, self.scale_a, self.scale_b)
        out_ref = ref_stanh(self.x_np, self.scale_a, self.scale_b)
        for r in [out]:
3107
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
3108 3109 3110 3111 3112 3113
        paddle.enable_static()

    def test_fluid_api(self):
        paddle.enable_static()
        with fluid.program_guard(fluid.Program()):
            x = fluid.data('X', [10, 12])
3114
            out = paddle.stanh(x, self.scale_a, self.scale_b)
3115 3116 3117
            exe = fluid.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = ref_stanh(self.x_np, self.scale_a, self.scale_b)
3118
        np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)
3119

3120
    def test_errors(self):
3121 3122
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
3123
            # The input type must be Variable.
3124
            self.assertRaises(TypeError, paddle.stanh, 1)
3125
            # The input dtype must be float16, float32, float64.
3126 3127 3128
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
3129
            self.assertRaises(TypeError, paddle.stanh, x_int32)
3130
            # support the input dtype is float16
3131 3132 3133
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
            paddle.stanh(x_fp16)


class TestSTanhAPIScaleA(TestSTanhAPI):
    def get_scale_a(self):
        return 2.0


class TestSTanhAPIScaleB(TestSTanhAPI):
    def get_scale_b(self):
        return 0.5
3145 3146


3147 3148
def ref_softplus(x, beta=1, threshold=20):
    x_beta = beta * x
3149 3150 3151 3152
    out = np.select(
        [x_beta <= threshold, x_beta > threshold],
        [np.log(1 + np.exp(x_beta)) / beta, x],
    )
3153 3154 3155
    return out


C
chengduo 已提交
3156
class TestSoftplus(TestActivation):
K
kexinzhao 已提交
3157 3158
    def setUp(self):
        self.op_type = "softplus"
W
Wang Bojun 已提交
3159
        self.python_api = paddle.nn.functional.softplus
3160
        self.init_dtype()
3161
        self.init_shape()
3162

3163 3164
        beta = 2
        threshold = 15
3165

3166
        np.random.seed(1024)
3167
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
3168 3169 3170
        out = ref_softplus(x, beta, threshold)
        self.inputs = {'X': x}
        self.attrs = {'beta': beta, "threshold": threshold}
3171
        self.outputs = {'Out': out}
K
kexinzhao 已提交
3172

W
Wang Bojun 已提交
3173 3174
        self.check_eager = True

3175 3176 3177
    def init_shape(self):
        self.shape = [10, 12]

K
kexinzhao 已提交
3178
    def test_check_grad(self):
3179 3180
        if self.dtype == np.float16:
            return
W
Wang Bojun 已提交
3181 3182 3183
        if hasattr(self, 'check_eager'):
            check_eager = self.check_eager
        self.check_grad(['X'], 'Out', check_eager=check_eager)
K
kexinzhao 已提交
3184

3185

3186 3187 3188 3189 3190
class TestSoftplus_ZeroDim(TestSoftplus):
    def init_shape(self):
        self.shape = []


3191 3192 3193
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
class TestSoftplusBF16(OpTest):
    def setUp(self):
        self.op_type = "softplus"
        self.init_dtype()

        beta = 2
        threshold = 15

        np.random.seed(1024)
        x = np.random.uniform(-1, 1, [10, 12]).astype(np.float32)
        out = ref_softplus(x, beta, threshold)
        self.inputs = {'X': convert_float_to_uint16(x)}
        self.attrs = {'beta': beta, "threshold": threshold}
        self.outputs = {'Out': convert_float_to_uint16(out)}

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

    def test_check_output(self):
        place = core.CUDAPlace(0)
        self.check_output_with_place(place)

    def test_check_grad(self):
        place = core.CUDAPlace(0)
        self.check_grad_with_place(place, ['X'], 'Out', numeric_grad_delta=0.05)


3221 3222 3223 3224 3225
class TestSoftplusAPI(unittest.TestCase):
    # test paddle.nn.Softplus, paddle.nn.functional.softplus
    def setUp(self):
        self.beta = 2
        self.threshold = 15
3226
        np.random.seed(1024)
3227
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
3228 3229 3230
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
3231
            else paddle.CPUPlace()
3232
        )
3233 3234

    def test_static_api(self):
3235
        paddle.enable_static()
3236
        with paddle.static.program_guard(paddle.static.Program()):
3237
            x = paddle.fluid.data('X', self.x_np.shape, self.x_np.dtype)
3238 3239 3240 3241 3242 3243 3244
            out1 = F.softplus(x, self.beta, self.threshold)
            softplus = paddle.nn.Softplus(self.beta, self.threshold)
            out2 = softplus(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_softplus(self.x_np, self.beta, self.threshold)
        for r in res:
3245
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
3246 3247 3248 3249 3250 3251 3252 3253 3254

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.softplus(x, self.beta, self.threshold)
        softplus = paddle.nn.Softplus(self.beta, self.threshold)
        out2 = softplus(x)
        out_ref = ref_softplus(self.x_np, self.beta, self.threshold)
        for r in [out1, out2]:
3255
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
3256 3257 3258
        paddle.enable_static()

    def test_errors(self):
3259
        paddle.enable_static()
3260 3261 3262 3263
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, F.softplus, 1)
            # The input dtype must be float16, float32, float64.
3264 3265 3266
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
3267 3268
            self.assertRaises(TypeError, F.softplus, x_int32)
            # support the input dtype is float16
3269 3270 3271
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
3272 3273 3274 3275 3276 3277 3278 3279
            F.softplus(x_fp16)


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


C
chengduo 已提交
3280
class TestSoftsign(TestActivation):
3281 3282
    def setUp(self):
        self.op_type = "softsign"
3283
        self.init_dtype()
3284 3285
        self.init_shape()

3286
        self.python_api = paddle.nn.functional.softsign
3287

3288
        np.random.seed(1024)
3289
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
3290 3291
        out = ref_softsign(x)
        self.inputs = {'X': x}
3292
        self.outputs = {'Out': out}
3293

3294 3295 3296
    def init_shape(self):
        self.shape = [10, 12]

3297
    def test_check_grad(self):
3298 3299
        if self.dtype == np.float16:
            return
3300
        self.check_grad(['X'], 'Out', check_eager=True)
3301 3302


3303 3304 3305 3306 3307
class TestSoftsign_ZeroDim(TestSoftsign):
    def init_shape(self):
        self.shape = []


3308 3309 3310
class TestSoftsignAPI(unittest.TestCase):
    # test paddle.nn.Softsign, paddle.nn.functional.softsign
    def setUp(self):
3311
        np.random.seed(1024)
3312
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
3313 3314 3315
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
3316
            else paddle.CPUPlace()
3317
        )
3318 3319

    def test_static_api(self):
3320
        paddle.enable_static()
3321
        with paddle.static.program_guard(paddle.static.Program()):
3322
            x = paddle.fluid.data('X', self.x_np.shape, self.x_np.dtype)
3323 3324 3325 3326 3327 3328 3329
            out1 = F.softsign(x)
            softsign = paddle.nn.Softsign()
            out2 = softsign(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_softsign(self.x_np)
        for r in res:
3330
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
3331 3332 3333 3334 3335 3336 3337 3338 3339

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.softsign(x)
        softsign = paddle.nn.Softsign()
        out2 = softsign(x)
        out_ref = ref_softsign(self.x_np)
        for r in [out1, out2]:
3340
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
3341 3342 3343
        paddle.enable_static()

    def test_errors(self):
3344
        paddle.enable_static()
3345 3346 3347 3348
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, F.softsign, 1)
            # The input dtype must be float16, float32, float64.
3349 3350 3351
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
3352 3353
            self.assertRaises(TypeError, F.softsign, x_int32)
            # support the input dtype is float16
3354 3355 3356
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
3357 3358 3359
            F.softsign(x_fp16)


3360 3361 3362 3363 3364
def ref_thresholded_relu(x, threshold=1.0):
    out = (x > threshold) * x
    return out


C
chengduo 已提交
3365
class TestThresholdedRelu(TestActivation):
3366 3367
    def setUp(self):
        self.op_type = "thresholded_relu"
3368
        self.init_dtype()
3369
        self.init_shape()
3370

3371
        threshold = 15
3372

3373
        np.random.seed(1024)
3374
        x = np.random.uniform(-20, 20, self.shape).astype(self.dtype)
3375 3376 3377 3378
        x[np.abs(x) < 0.005] = 0.02
        out = ref_thresholded_relu(x, threshold)
        self.inputs = {'X': x}
        self.attrs = {"threshold": threshold}
3379
        self.outputs = {'Out': out}
3380

3381 3382 3383
    def init_shape(self):
        self.shape = [10, 12]

3384
    def test_check_grad(self):
3385 3386
        if self.dtype == np.float16:
            return
3387
        self.check_grad(['X'], 'Out')
3388 3389


3390 3391 3392 3393 3394
class TestThresholdedRelu_ZeroDim(TestThresholdedRelu):
    def init_shape(self):
        self.shape = []


3395 3396 3397 3398 3399 3400 3401
class TestThresholdedReluAPI(unittest.TestCase):
    # test paddle.nn.ThresholdedReLU, paddle.nn.functional.thresholded_relu
    def setUp(self):
        self.threshold = 15
        np.random.seed(1024)
        self.x_np = np.random.uniform(-20, 20, [10, 12]).astype(np.float64)
        self.x_np[np.abs(self.x_np) < 0.005] = 0.02
3402 3403 3404
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
3405
            else paddle.CPUPlace()
3406
        )
3407 3408 3409 3410

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
3411
            x = paddle.fluid.data('X', self.x_np.shape, self.x_np.dtype)
3412 3413 3414 3415 3416 3417 3418
            out1 = F.thresholded_relu(x, self.threshold)
            thresholded_relu = paddle.nn.ThresholdedReLU(self.threshold)
            out2 = thresholded_relu(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_thresholded_relu(self.x_np, self.threshold)
        for r in res:
3419
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
3420 3421 3422 3423 3424 3425 3426 3427 3428

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.thresholded_relu(x, self.threshold)
        thresholded_relu = paddle.nn.ThresholdedReLU(self.threshold)
        out2 = thresholded_relu(x)
        out_ref = ref_thresholded_relu(self.x_np, self.threshold)
        for r in [out1, out2]:
3429
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
3430 3431
        paddle.enable_static()

3432
    def test_errors(self):
3433 3434
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
3435
            # The input type must be Variable.
3436
            self.assertRaises(TypeError, F.thresholded_relu, 1)
3437
            # The input dtype must be float16, float32, float64.
3438 3439 3440
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
3441
            self.assertRaises(TypeError, F.thresholded_relu, x_int32)
3442
            # support the input dtype is float16
3443 3444 3445
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
3446
            F.thresholded_relu(x_fp16)
3447 3448


3449
def ref_hardsigmoid(x, slope=0.166666666666667, offset=0.5):
3450
    return np.maximum(np.minimum(x * slope + offset, 1.0), 0.0).astype(x.dtype)
3451 3452


C
chengduo 已提交
3453
class TestHardSigmoid(TestActivation):
3454 3455
    def setUp(self):
        self.op_type = "hard_sigmoid"
3456 3457 3458 3459
        self.dtype = 'float64'
        self.slope = 0.166666666666667
        self.offset = 0.5
        self.set_attrs()
3460
        self.init_shape()
3461

3462
        x = np.random.uniform(-5, 5, self.shape).astype(self.dtype)
3463
        lower_threshold = -self.offset / self.slope
3464
        upper_threshold = (1.0 - self.offset) / self.slope
Z
zhupengyang 已提交
3465

3466
        # Same reason as TestAbs
3467 3468 3469
        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
3470

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

3473 3474
        self.attrs = {'slope': self.slope, 'offset': self.offset}
        self.inputs = {'X': x}
3475
        self.outputs = {'Out': out}
3476

3477 3478 3479
    def init_shape(self):
        self.shape = [10, 12]

3480 3481
    def set_attrs(self):
        pass
3482

3483

3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
class TestHardSigmoidFP32(TestHardSigmoid):
    def set_attrs(self):
        self.dtype = 'float32'


class TestHardSigmoidSlopeOffset(TestHardSigmoid):
    def set_attrs(self):
        self.slope = 0.2
        self.offset = 0.4


3495 3496 3497 3498 3499
class TestHardSigmoid_ZeroDim(TestHardSigmoid):
    def init_shape(self):
        self.shape = []


3500 3501 3502 3503
class TestHardsigmoidAPI(unittest.TestCase):
    # test paddle.nn.Hardsigmoid, paddle.nn.functional.hardsigmoid
    def setUp(self):
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
3504 3505 3506
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
3507
            else paddle.CPUPlace()
3508
        )
3509 3510 3511

    def test_static_api(self):
        with paddle.static.program_guard(paddle.static.Program()):
J
joejiong 已提交
3512
            x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype)
3513 3514 3515 3516 3517 3518 3519
            out1 = F.hardsigmoid(x)
            m = paddle.nn.Hardsigmoid()
            out2 = m(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_hardsigmoid(self.x_np)
        for r in res:
3520
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
3521 3522 3523 3524 3525 3526 3527 3528 3529

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.hardsigmoid(x)
        m = paddle.nn.Hardsigmoid()
        out2 = m(x)
        out_ref = ref_hardsigmoid(self.x_np)
        for r in [out1, out2]:
3530
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
3531
        paddle.enable_static()
3532 3533 3534 3535

    def test_fluid_api(self):
        with fluid.program_guard(fluid.Program()):
            x = fluid.data('X', self.x_np.shape, self.x_np.dtype)
3536
            out = paddle.nn.functional.hardsigmoid(x, slope=0.2)
3537 3538 3539
            exe = fluid.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = ref_hardsigmoid(self.x_np, 0.2, 0.5)
3540
        np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)
3541 3542 3543

        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
3544
        out = paddle.nn.functional.hardsigmoid(x, slope=0.2)
3545
        np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05)
3546 3547 3548 3549
        paddle.enable_static()

    def test_errors(self):
        with paddle.static.program_guard(paddle.static.Program()):
3550
            # The input type must be Variable.
3551
            self.assertRaises(TypeError, F.hardsigmoid, 1)
3552
            # The input dtype must be float16, float32, float64.
3553 3554 3555
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
3556
            self.assertRaises(TypeError, F.hardsigmoid, x_int32)
3557
            # support the input dtype is float16
3558 3559 3560
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
3561
            F.hardsigmoid(x_fp16)
3562 3563


3564 3565 3566 3567 3568
def ref_swish(x):
    out = x * expit(x)
    return out


C
chengduo 已提交
3569
class TestSwish(TestActivation):
A
Abhinav Arora 已提交
3570 3571
    def setUp(self):
        self.op_type = "swish"
3572
        self.python_api = paddle.nn.functional.swish
3573
        self.init_dtype()
3574 3575
        self.init_shape()

3576
        self.check_eager = True
3577

3578
        np.random.seed(1024)
3579
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
3580 3581
        out = ref_swish(x)
        self.inputs = {'X': x}
H
hong19860320 已提交
3582
        self.attrs = {'beta': 1.0}
3583
        self.outputs = {'Out': out}
A
Abhinav Arora 已提交
3584

3585 3586 3587
    def init_shape(self):
        self.shape = [10, 12]

A
Abhinav Arora 已提交
3588
    def test_check_grad(self):
3589 3590
        if self.dtype == np.float16:
            return
3591 3592 3593 3594
        check_eager = False
        if hasattr(self, 'check_eager'):
            check_eager = self.check_eager
        self.check_grad(['X'], 'Out', check_eager=check_eager)
3595

A
Abhinav Arora 已提交
3596

3597 3598 3599 3600 3601
class TestSwish_ZeroDim(TestSwish):
    def init_shape(self):
        self.shape = []


3602 3603 3604 3605 3606
class TestSwishAPI(unittest.TestCase):
    # test paddle.nn.Swish, paddle.nn.functional.swish
    def setUp(self):
        np.random.seed(1024)
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
3607 3608 3609
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
3610
            else paddle.CPUPlace()
3611
        )
3612 3613 3614 3615

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
J
joejiong 已提交
3616
            x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype)
3617 3618 3619 3620 3621 3622 3623
            out1 = F.swish(x)
            swish = paddle.nn.Swish()
            out2 = swish(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_swish(self.x_np)
        for r in res:
3624
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
3625

3626
    def test_dygraph_api(self):
3627 3628 3629 3630 3631 3632 3633
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.swish(x)
        swish = paddle.nn.Swish()
        out2 = swish(x)
        out_ref = ref_swish(self.x_np)
        for r in [out1, out2]:
3634
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
3635 3636 3637 3638 3639 3640
        paddle.enable_static()

    def test_fluid_api(self):
        paddle.enable_static()
        with fluid.program_guard(fluid.Program()):
            x = fluid.data('X', self.x_np.shape, self.x_np.dtype)
3641
            out = paddle.nn.functional.swish(x)
3642 3643 3644
            exe = fluid.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = ref_swish(self.x_np)
3645
        np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)
3646

3647
    def test_errors(self):
3648 3649
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
3650
            # The input type must be Variable.
3651
            self.assertRaises(TypeError, F.swish, 1)
3652
            # The input dtype must be float16, float32, float64.
3653 3654 3655
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
3656
            self.assertRaises(TypeError, F.swish, x_int32)
3657
            # support the input dtype is float16
3658 3659 3660
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
3661
            F.swish(x_fp16)
3662 3663


3664 3665 3666 3667
def ref_mish(x, threshold=20.0):
    softplus = np.select(
        [x <= threshold, x > threshold], [np.log(1 + np.exp(x)), x]
    )
3668 3669 3670 3671 3672 3673
    return x * np.tanh(softplus)


class TestMish(TestActivation):
    def setUp(self):
        self.op_type = "mish"
3674
        self.python_api = paddle.nn.functional.mish
3675
        self.init_dtype()
3676
        self.init_shape()
3677 3678

        np.random.seed(1024)
3679
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
3680 3681 3682 3683
        out = ref_mish(x)
        self.inputs = {'X': x}
        self.outputs = {'Out': out}

3684 3685 3686
    def init_shape(self):
        self.shape = [10, 12]

3687 3688 3689
    def test_check_output(self):
        self.check_output(check_eager=True)

3690 3691 3692
    def test_check_grad(self):
        if self.dtype == np.float16:
            return
3693
        self.check_grad(['X'], 'Out', check_eager=True)
3694 3695


3696 3697 3698 3699 3700
class TestMish_ZeroDim(TestMish):
    def init_shape(self):
        self.shape = []


3701 3702 3703 3704 3705
class TestMishAPI(unittest.TestCase):
    # test paddle.nn.Mish, paddle.nn.functional.mish
    def setUp(self):
        np.random.seed(1024)
        self.x_np = np.random.uniform(-1, 1, [10, 12]).astype(np.float64)
3706 3707 3708
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.is_compiled_with_cuda()
3709
            else paddle.CPUPlace()
3710
        )
3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722

    def test_static_api(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype)
            out1 = F.mish(x)
            mish = paddle.nn.Mish()
            out2 = mish(x)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
        out_ref = ref_mish(self.x_np)
        for r in res:
3723
            np.testing.assert_allclose(out_ref, r, rtol=1e-05)
3724 3725 3726 3727 3728 3729 3730 3731 3732

    def test_dygraph_api(self):
        paddle.disable_static(self.place)
        x = paddle.to_tensor(self.x_np)
        out1 = F.mish(x)
        mish = paddle.nn.Mish()
        out2 = mish(x)
        out_ref = ref_mish(self.x_np)
        for r in [out1, out2]:
3733
            np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05)
3734 3735 3736 3737 3738 3739
        paddle.enable_static()

    def test_fluid_api(self):
        paddle.enable_static()
        with fluid.program_guard(fluid.Program()):
            x = fluid.data('X', self.x_np.shape, self.x_np.dtype)
3740
            out = paddle.nn.functional.mish(x)
3741 3742 3743
            exe = fluid.Executor(self.place)
            res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
        out_ref = ref_mish(self.x_np)
3744
        np.testing.assert_allclose(out_ref, res[0], rtol=1e-05)
3745 3746 3747 3748 3749 3750 3751

    def test_errors(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
            # The input type must be Variable.
            self.assertRaises(TypeError, F.mish, 1)
            # The input dtype must be float16, float32, float64.
3752 3753 3754
            x_int32 = paddle.fluid.data(
                name='x_int32', shape=[12, 10], dtype='int32'
            )
3755 3756
            self.assertRaises(TypeError, F.mish, x_int32)
            # support the input dtype is float16
3757 3758 3759
            x_fp16 = paddle.fluid.data(
                name='x_fp16', shape=[12, 10], dtype='float16'
            )
3760 3761 3762
            F.mish(x_fp16)


3763
# ------------------ Test Cudnn Activation----------------------
3764
def create_test_act_cudnn_class(parent, atol=1e-3, grad_atol=1e-3):
3765 3766 3767
    @unittest.skipIf(
        not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
    )
3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
    class TestActCudnn(parent):
        def init_kernel_type(self):
            self.attrs = {"use_cudnn": True}

    cls_name = "{0}_{1}".format(parent.__name__, "cudnn")
    TestActCudnn.__name__ = cls_name
    globals()[cls_name] = TestActCudnn


create_test_act_cudnn_class(TestRelu)
create_test_act_cudnn_class(TestRelu6)
create_test_act_cudnn_class(TestSigmoid)
create_test_act_cudnn_class(TestTanh)


3783 3784 3785 3786 3787 3788 3789
# ------------------ Test Fp16 ----------------------
def create_test_act_fp16_class(
    parent, atol=1e-3, grad_check=True, grad_atol=0.80
):
    @unittest.skipIf(
        not paddle.is_compiled_with_cuda(), "core is not compiled with CUDA"
    )
C
chengduo 已提交
3790 3791 3792
    class TestActFp16(parent):
        def init_dtype(self):
            self.dtype = np.float16
3793

C
chengduo 已提交
3794
        def test_check_output(self):
3795
            place = core.CUDAPlace(0)
C
chengduo 已提交
3796 3797 3798
            support_fp16 = core.is_float16_supported(place)
            if support_fp16:
                self.check_output_with_place(place, atol=atol)
3799

C
chengduo 已提交
3800 3801 3802 3803
        def test_check_grad(self):
            place = core.CUDAPlace(0)
            support_fp16 = core.is_float16_supported(place)
            if support_fp16 and grad_check:
3804 3805 3806
                self.check_grad_with_place(
                    place, ['X'], 'Out', max_relative_error=grad_atol
                )
C
chengduo 已提交
3807 3808 3809 3810 3811 3812 3813

    cls_name = "{0}_{1}".format(parent.__name__, "fp16")
    TestActFp16.__name__ = cls_name
    globals()[cls_name] = TestActFp16


create_test_act_fp16_class(TestActivation)
R
ronnywang 已提交
3814
create_test_act_fp16_class(TestExpm1)
C
chengduo 已提交
3815
create_test_act_fp16_class(TestSigmoid)
Z
zxcd 已提交
3816
create_test_act_fp16_class(TestSigmoidFP16)
M
minghaoBD 已提交
3817
create_test_act_fp16_class(TestSilu)
Z
zxcd 已提交
3818
create_test_act_fp16_class(TestSiluFP16)
C
chengduo 已提交
3819 3820
create_test_act_fp16_class(TestLogSigmoid)
create_test_act_fp16_class(TestTanh)
3821
create_test_act_fp16_class(TestTanhshrink)
C
chengduo 已提交
3822
create_test_act_fp16_class(TestHardShrink)
3823
create_test_act_fp16_class(TestSoftshrink)
C
chengduo 已提交
3824 3825 3826 3827 3828
create_test_act_fp16_class(TestSqrt)
create_test_act_fp16_class(TestAbs)
create_test_act_fp16_class(TestCeil, grad_check=False)
create_test_act_fp16_class(TestFloor, grad_check=False)
create_test_act_fp16_class(TestCos, grad_atol=0.85)
J
joejiong 已提交
3829
create_test_act_fp16_class(TestTan, grad_atol=0.85)
3830
create_test_act_fp16_class(TestCosh, grad_atol=0.85)
3831
create_test_act_fp16_class(TestAcos, grad_atol=0.85)
C
chengduo 已提交
3832
create_test_act_fp16_class(TestSin)
3833
create_test_act_fp16_class(TestSinh)
3834 3835
create_test_act_fp16_class(TestAsin)
create_test_act_fp16_class(TestAtan)
X
xiaoting 已提交
3836 3837 3838
create_test_act_fp16_class(TestAcosh, grad_atol=0.85)
create_test_act_fp16_class(TestAsinh, grad_atol=0.85)
create_test_act_fp16_class(TestAtanh, grad_atol=0.85)
C
chengduo 已提交
3839 3840
create_test_act_fp16_class(TestRound, grad_check=False)
create_test_act_fp16_class(TestRelu)
C
Clementine 已提交
3841
create_test_act_fp16_class(TestGelu)
C
chengduo 已提交
3842 3843
create_test_act_fp16_class(TestBRelu)
create_test_act_fp16_class(TestRelu6)
3844
create_test_act_fp16_class(TestSoftRelu, grad_atol=0.85)
C
chengduo 已提交
3845
create_test_act_fp16_class(TestELU)
3846
create_test_act_fp16_class(TestCELU)
C
chengduo 已提交
3847 3848
create_test_act_fp16_class(TestReciprocal)
create_test_act_fp16_class(TestLog)
3849 3850 3851 3852
if core.is_compiled_with_rocm():
    create_test_act_fp16_class(TestLog2, atol=5e-2, grad_atol=0.85)
else:
    create_test_act_fp16_class(TestLog2, atol=5e-2)
J
joejiong 已提交
3853
create_test_act_fp16_class(TestLog10, atol=5e-2)
3854
create_test_act_fp16_class(TestLog1p, grad_atol=0.9)
C
chengduo 已提交
3855 3856
create_test_act_fp16_class(TestSquare)
create_test_act_fp16_class(TestPow, atol=5e-2)
3857
create_test_act_fp16_class(TestPow_factor_tensor, atol=5e-2)
C
chengduo 已提交
3858 3859 3860 3861 3862
create_test_act_fp16_class(TestSTanh, grad_atol=0.9)
create_test_act_fp16_class(TestSoftplus)
create_test_act_fp16_class(TestSoftsign)
create_test_act_fp16_class(TestThresholdedRelu)
create_test_act_fp16_class(TestHardSigmoid)
3863
create_test_act_fp16_class(TestSwish, grad_atol=0.85)
H
huangjun12 已提交
3864
create_test_act_fp16_class(TestHardSwish)
3865
create_test_act_fp16_class(TestMish, grad_atol=0.9)
A
Abhinav Arora 已提交
3866

3867

3868 3869 3870 3871 3872 3873
def create_test_act_bf16_class(
    parent, atol=1e-2, grad_check=True, grad_atol=0.80
):
    @unittest.skipIf(
        not paddle.is_compiled_with_cuda(), "core is not compiled with CUDA"
    )
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
    class TestActBF16(parent):
        def init_dtype(self):
            self.dtype = np.uint16

        def test_check_output(self):
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=atol)

        def test_check_grad(self):
            place = core.CUDAPlace(0)
3884 3885 3886
            self.check_grad_with_place(
                place, ['X'], 'Out', max_relative_error=grad_atol
            )
3887 3888 3889 3890 3891 3892 3893

    cls_name = "{0}_{1}".format(parent.__name__, "bf16")
    TestActBF16.__name__ = cls_name
    globals()[cls_name] = TestActBF16


create_test_act_bf16_class(TestRelu)
3894
create_test_act_bf16_class(TestAbs)
3895

Q
qijun 已提交
3896 3897
if __name__ == "__main__":
    unittest.main()