test_elementwise_max_op.py 16.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
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.

F
fengjiayi 已提交
15
import unittest
16

F
fengjiayi 已提交
17
import numpy as np
18 19
from op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci

20
import paddle
21
import paddle.fluid.core as core
F
fengjiayi 已提交
22 23 24


class TestElementwiseOp(OpTest):
25
    def init_data(self):
F
fengjiayi 已提交
26 27 28
        # If x and y have the same value, the max() is not differentiable.
        # So we generate test data by the following method
        # to avoid them being too close to each other.
29
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype("float64")
30
        sgn = np.random.choice([-1, 1], [13, 17]).astype("float64")
31 32 33 34 35 36 37 38 39 40
        self.y = self.x + sgn * np.random.uniform(0.1, 1, [13, 17]).astype(
            "float64"
        )

    def setUp(self):
        self.init_data()
        self.op_type = "elementwise_max"
        self.prim_op_type = "prim"
        self.enable_cinn = False
        self.python_api = paddle.maximum
41
        self.public_python_api = paddle.maximum
42
        self.inputs = {'X': self.x, 'Y': self.y}
F
fengjiayi 已提交
43 44 45
        self.outputs = {'Out': np.maximum(self.inputs['X'], self.inputs['Y'])}

    def test_check_output(self):
46 47 48 49
        if hasattr(self, 'attrs'):
            self.check_output(check_eager=False)
        else:
            self.check_output(check_eager=True)
F
fengjiayi 已提交
50 51

    def test_check_grad_normal(self):
52
        if hasattr(self, 'attrs'):
H
heyanru 已提交
53 54 55 56 57 58
            if self.attrs['axis'] == -1:
                self.check_grad(
                    ['X', 'Y'], 'Out', check_eager=False, check_prim=True
                )
            else:
                self.check_grad(['X', 'Y'], 'Out', check_eager=False)
59
        else:
H
heyanru 已提交
60 61 62
            self.check_grad(
                ['X', 'Y'], 'Out', check_eager=True, check_prim=True
            )
F
fengjiayi 已提交
63 64

    def test_check_grad_ingore_x(self):
H
heyanru 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        if hasattr(self, 'attrs') and self.attrs['axis'] != -1:
            self.check_grad(
                ['Y'],
                'Out',
                max_relative_error=0.005,
                no_grad_set=set("X"),
            )
        else:
            self.check_grad(
                ['Y'],
                'Out',
                max_relative_error=0.005,
                no_grad_set=set("X"),
                check_prim=True,
            )
F
fengjiayi 已提交
80 81

    def test_check_grad_ingore_y(self):
H
heyanru 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        if hasattr(self, 'attrs') and self.attrs['axis'] != -1:
            self.check_grad(
                ['X'],
                'Out',
                max_relative_error=0.005,
                no_grad_set=set('Y'),
            )
        else:
            self.check_grad(
                ['X'],
                'Out',
                max_relative_error=0.005,
                no_grad_set=set('Y'),
                check_prim=True,
            )
97 98


99 100 101 102 103 104 105 106 107
class TestElementwiseFP16Op(TestElementwiseOp):
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype(np.float16)
        sgn = np.random.choice([-1, 1], [13, 17]).astype(np.float16)
        self.y = self.x + sgn * np.random.uniform(0.1, 1, [13, 17]).astype(
            np.float16
        )


108
class TestElementwiseMaxOp_ZeroDim1(TestElementwiseOp):
109 110 111 112 113 114 115 116 117
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, []).astype("float64")
        self.y = np.random.uniform(0.1, 1, []).astype("float64")


class TestElementwiseMaxFP16Op_ZeroDim1(TestElementwiseOp):
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, []).astype("float16")
        self.y = np.random.uniform(0.1, 1, []).astype("float16")
118 119 120


class TestElementwiseMaxOp_ZeroDim2(TestElementwiseOp):
121 122 123 124 125 126 127 128 129
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype("float64")
        self.y = np.random.uniform(0.1, 1, []).astype("float64")


class TestElementwiseMaxFP16Op_ZeroDim2(TestElementwiseOp):
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype("float16")
        self.y = np.random.uniform(0.1, 1, []).astype("float16")
130 131 132


class TestElementwiseMaxOp_ZeroDim3(TestElementwiseOp):
133 134 135 136 137 138 139 140 141
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, []).astype("float64")
        self.y = np.random.uniform(0.1, 1, [13, 17]).astype("float64")


class TestElementwiseMaxFP16Op_ZeroDim3(TestElementwiseOp):
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, []).astype("float16")
        self.y = np.random.uniform(0.1, 1, [13, 17]).astype("float16")
142 143


144 145 146 147 148 149 150 151
@unittest.skipIf(
    core.is_compiled_with_cuda()
    and (
        core.cudnn_version() < 8100
        or paddle.device.cuda.get_device_capability()[0] < 8
    ),
    "run test when gpu is availble and the minimum cudnn version is 8.1.0 and gpu's compute capability is at least 8.0.",
)
152
class TestElementwiseBF16Op(OpTest):
153 154 155 156 157 158 159 160 161 162
    def init_data(self):
        # If x and y have the same value, the max() is not differentiable.
        # So we generate test data by the following method
        # to avoid them being too close to each other.
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype(np.float32)
        sgn = np.random.choice([-1, 1], [13, 17]).astype(np.float32)
        self.y = self.x + sgn * np.random.uniform(0.1, 1, [13, 17]).astype(
            np.float32
        )

163
    def setUp(self):
164
        self.init_data()
165
        self.op_type = "elementwise_max"
166
        self.python_api = paddle.maximum
167
        self.public_python_api = paddle.maximum
H
heyanru 已提交
168 169
        self.prim_op_type = "prim"
        self.enable_cinn = False
170 171
        self.dtype = np.uint16
        self.inputs = {
172 173 174 175 176
            'X': convert_float_to_uint16(self.x),
            'Y': convert_float_to_uint16(self.y),
        }
        self.outputs = {
            'Out': convert_float_to_uint16(np.maximum(self.x, self.y))
177 178 179
        }

    def test_check_output(self):
180 181 182 183
        if hasattr(self, 'attrs'):
            self.check_output(check_eager=False)
        else:
            self.check_output(check_eager=True)
184 185

    def test_check_grad_normal(self):
186
        if hasattr(self, 'attrs'):
H
heyanru 已提交
187
            # check_prim=False, bfloat16 is not supported in `less_equal`
188 189 190
            self.check_grad(['X', 'Y'], 'Out', check_eager=False)
        else:
            self.check_grad(['X', 'Y'], 'Out', check_eager=True)
191 192 193 194 195 196 197 198

    def test_check_grad_ingore_x(self):
        self.check_grad(['Y'], 'Out', no_grad_set=set("X"))

    def test_check_grad_ingore_y(self):
        self.check_grad(['X'], 'Out', no_grad_set=set('Y'))


199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
class TestElementwiseMaxBF16Op_ZeroDim1(TestElementwiseBF16Op):
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, []).astype("float32")
        self.y = np.random.uniform(0.1, 1, []).astype("float32")

    def test_check_grad_normal(self):
        if hasattr(self, 'attrs'):
            self.check_grad(
                ['X', 'Y'], 'Out', numeric_grad_delta=0.05, check_eager=False
            )
        else:
            self.check_grad(
                ['X', 'Y'], 'Out', numeric_grad_delta=0.05, check_eager=True
            )

    def test_check_grad_ingore_x(self):
        self.check_grad(
            ['Y'], 'Out', numeric_grad_delta=0.05, no_grad_set=set("X")
        )

    def test_check_grad_ingore_y(self):
        self.check_grad(
            ['X'], 'Out', numeric_grad_delta=0.05, no_grad_set=set('Y')
        )


class TestElementwiseMaxBF16Op_scalar(TestElementwiseBF16Op):
    def init_data(self):
        self.x = np.random.random_integers(-5, 5, [2, 3, 20]).astype("float32")
        self.y = np.array([0.5]).astype("float32")
        self.__class__.no_need_check_grad = True


232
@skip_check_grad_ci(
233 234
    reason="[skip shape check] Use y_shape(1) to test broadcast."
)
235
class TestElementwiseMaxOp_scalar(TestElementwiseOp):
236 237 238 239 240 241 242 243 244
    def init_data(self):
        self.x = np.random.random_integers(-5, 5, [2, 3, 20]).astype("float64")
        self.y = np.array([0.5]).astype("float64")


class TestElementwiseMaxFP16Op_scalar(TestElementwiseMaxOp_scalar):
    def init_data(self):
        self.x = np.random.random_integers(-5, 5, [2, 3, 20]).astype("float16")
        self.y = np.array([0.5]).astype("float16")
245 246


F
fengjiayi 已提交
247
class TestElementwiseMaxOp_Vector(TestElementwiseOp):
248 249
    def init_data(self):
        self.x = np.random.random((100,)).astype("float64")
250
        sgn = np.random.choice([-1, 1], (100,)).astype("float64")
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        self.y = self.x + sgn * np.random.uniform(0.1, 1, (100,)).astype(
            "float64"
        )


class TestElementwiseMaxFP16Op_Vector(TestElementwiseOp):
    def init_data(self):
        self.x = np.random.random((100,)).astype("float16")
        sgn = np.random.choice([-1, 1], (100,)).astype("float16")
        self.y = self.x + sgn * np.random.uniform(0.1, 1, (100,)).astype(
            "float16"
        )


class TestElementwiseMaxBF16Op_Vector(TestElementwiseBF16Op):
    def init_data(self):
        self.x = np.random.random((100,)).astype("float32")
        sgn = np.random.choice([-1, 1], (100,)).astype("float32")
        self.y = self.x + sgn * np.random.uniform(0.1, 1, (100,)).astype(
            "float32"
        )
F
fengjiayi 已提交
272 273 274 275 276


class TestElementwiseMaxOp_broadcast_0(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
277
        self.python_api = paddle.maximum
278
        self.public_python_api = paddle.maximum
H
heyanru 已提交
279
        self.prim_op_type = "prim"
280
        x = np.random.uniform(0.5, 1, (100, 5, 2)).astype(np.float64)
281 282 283 284
        sgn = np.random.choice([-1, 1], (100,)).astype(np.float64)
        y = x[:, 0, 0] + sgn * np.random.uniform(1, 2, (100,)).astype(
            np.float64
        )
F
fengjiayi 已提交
285 286 287 288
        self.inputs = {'X': x, 'Y': y}

        self.attrs = {'axis': 0}
        self.outputs = {
289 290 291
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(100, 1, 1)
            )
F
fengjiayi 已提交
292 293 294
        }


295 296 297 298
class TestElementwiseMaxFP16Op_broadcast_0(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
        self.python_api = paddle.maximum
299
        self.public_python_api = paddle.maximum
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        self.prim_op_type = "prim"
        x = np.random.uniform(0.5, 1, (100, 5, 2)).astype(np.float16)
        sgn = np.random.choice([-1, 1], (100,)).astype(np.float16)
        y = x[:, 0, 0] + sgn * np.random.uniform(1, 2, (100,)).astype(
            np.float16
        )
        self.inputs = {'X': x, 'Y': y}

        self.attrs = {'axis': 0}
        self.outputs = {
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(100, 1, 1)
            )
        }


F
fengjiayi 已提交
316 317 318
class TestElementwiseMaxOp_broadcast_1(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
319
        self.python_api = paddle.maximum
320
        self.public_python_api = paddle.maximum
H
heyanru 已提交
321
        self.prim_op_type = "prim"
322
        x = np.random.uniform(0.5, 1, (2, 100, 3)).astype(np.float64)
323 324 325 326
        sgn = np.random.choice([-1, 1], (100,)).astype(np.float64)
        y = x[0, :, 0] + sgn * np.random.uniform(1, 2, (100,)).astype(
            np.float64
        )
F
fengjiayi 已提交
327 328 329 330
        self.inputs = {'X': x, 'Y': y}

        self.attrs = {'axis': 1}
        self.outputs = {
331 332 333
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(1, 100, 1)
            )
F
fengjiayi 已提交
334 335 336
        }


337 338 339 340
class TestElementwiseMaxFP16Op_broadcast_1(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
        self.python_api = paddle.maximum
341
        self.public_python_api = paddle.maximum
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        self.prim_op_type = "prim"
        x = np.random.uniform(0.5, 1, (2, 100, 3)).astype(np.float16)
        sgn = np.random.choice([-1, 1], (100,)).astype(np.float16)
        y = x[0, :, 0] + sgn * np.random.uniform(1, 2, (100,)).astype(
            np.float16
        )
        self.inputs = {'X': x, 'Y': y}

        self.attrs = {'axis': 1}
        self.outputs = {
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(1, 100, 1)
            )
        }


F
fengjiayi 已提交
358 359 360
class TestElementwiseMaxOp_broadcast_2(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
361
        self.python_api = paddle.maximum
362
        self.public_python_api = paddle.maximum
H
heyanru 已提交
363
        self.prim_op_type = "prim"
364
        x = np.random.uniform(0.5, 1, (1, 3, 100)).astype(np.float64)
365 366 367 368
        sgn = np.random.choice([-1, 1], (100,)).astype(np.float64)
        y = x[0, 0, :] + sgn * np.random.uniform(1, 2, (100,)).astype(
            np.float64
        )
F
fengjiayi 已提交
369 370 371
        self.inputs = {'X': x, 'Y': y}

        self.outputs = {
372 373 374
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(1, 1, 100)
            )
F
fengjiayi 已提交
375 376 377
        }


378 379 380 381
class TestElementwiseMaxFP16Op_broadcast_2(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
        self.python_api = paddle.maximum
382
        self.public_python_api = paddle.maximum
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        self.prim_op_type = "prim"
        x = np.random.uniform(0.5, 1, (1, 3, 100)).astype(np.float16)
        sgn = np.random.choice([-1, 1], (100,)).astype(np.float16)
        y = x[0, 0, :] + sgn * np.random.uniform(1, 2, (100,)).astype(
            np.float16
        )
        self.inputs = {'X': x, 'Y': y}

        self.outputs = {
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(1, 1, 100)
            )
        }


F
fengjiayi 已提交
398 399 400
class TestElementwiseMaxOp_broadcast_3(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
401
        self.python_api = paddle.maximum
402
        self.public_python_api = paddle.maximum
H
heyanru 已提交
403
        self.prim_op_type = "prim"
404 405
        x = np.random.uniform(0.5, 1, (2, 50, 2, 1)).astype(np.float64)
        sgn = np.random.choice([-1, 1], (50, 2)).astype(np.float64)
406 407 408
        y = x[0, :, :, 0] + sgn * np.random.uniform(1, 2, (50, 2)).astype(
            np.float64
        )
F
fengjiayi 已提交
409 410 411 412
        self.inputs = {'X': x, 'Y': y}

        self.attrs = {'axis': 1}
        self.outputs = {
413 414 415
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(1, 50, 2, 1)
            )
F
fengjiayi 已提交
416 417 418
        }


419 420 421 422
class TestElementwiseMaxFP16Op_broadcast_3(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
        self.python_api = paddle.maximum
423
        self.public_python_api = paddle.maximum
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
        self.prim_op_type = "prim"
        x = np.random.uniform(0.5, 1, (2, 50, 2, 1)).astype(np.float16)
        sgn = np.random.choice([-1, 1], (50, 2)).astype(np.float16)
        y = x[0, :, :, 0] + sgn * np.random.uniform(1, 2, (50, 2)).astype(
            np.float16
        )
        self.inputs = {'X': x, 'Y': y}

        self.attrs = {'axis': 1}
        self.outputs = {
            'Out': np.maximum(
                self.inputs['X'], self.inputs['Y'].reshape(1, 50, 2, 1)
            )
        }


440 441 442
class TestElementwiseMaxOp_broadcast_4(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
443
        self.python_api = paddle.maximum
444
        self.public_python_api = paddle.maximum
H
heyanru 已提交
445
        self.prim_op_type = "prim"
446 447
        x = np.random.uniform(0.5, 1, (2, 3, 4, 5)).astype(np.float64)
        sgn = np.random.choice([-1, 1], (2, 3, 1, 5)).astype(np.float64)
448
        y = x + sgn * np.random.uniform(1, 2, (2, 3, 1, 5)).astype(np.float64)
449 450 451 452 453
        self.inputs = {'X': x, 'Y': y}

        self.outputs = {'Out': np.maximum(self.inputs['X'], self.inputs['Y'])}


454 455 456 457
class TestElementwiseFP16Op_broadcast_4(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
        self.python_api = paddle.maximum
458
        self.public_python_api = paddle.maximum
459 460 461 462 463 464 465 466
        self.prim_op_type = "prim"
        x = np.random.uniform(0.5, 1, (2, 3, 4, 5)).astype(np.float16)
        sgn = np.random.choice([-1, 1], (2, 3, 1, 5)).astype(np.float16)
        y = x + sgn * np.random.uniform(1, 2, (2, 3, 1, 5)).astype(np.float16)
        self.inputs = {'X': x, 'Y': y}
        self.outputs = {'Out': np.maximum(self.inputs['X'], self.inputs['Y'])}


F
fengjiayi 已提交
467 468
if __name__ == '__main__':
    unittest.main()