test_elementwise_max_op.py 11.9 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
W
wanghuancoder 已提交
18
from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci
19

20
import paddle
21
from paddle.fluid import 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
        if hasattr(self, 'attrs'):
W
wanghuancoder 已提交
47
            self.check_output(check_dygraph=False)
48
        else:
W
wanghuancoder 已提交
49
            self.check_output()
F
fengjiayi 已提交
50 51

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

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

    def test_check_grad_ingore_y(self):
H
heyanru 已提交
81 82 83 84 85 86
        if hasattr(self, 'attrs') and self.attrs['axis'] != -1:
            self.check_grad(
                ['X'],
                'Out',
                max_relative_error=0.005,
                no_grad_set=set('Y'),
W
wanghuancoder 已提交
87
                check_dygraph=False,
H
heyanru 已提交
88 89 90 91 92 93 94 95 96
            )
        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
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
        )

107 108 109 110 111 112 113 114 115 116 117
    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
        self.dtype = np.float16
        self.public_python_api = paddle.maximum
        self.inputs = {'X': self.x, 'Y': self.y}
        self.outputs = {'Out': np.maximum(self.inputs['X'], self.inputs['Y'])}

118

119
class TestElementwiseMaxOp_ZeroDim1(TestElementwiseOp):
120 121 122 123 124
    def init_data(self):
        self.x = np.random.uniform(0.1, 1, []).astype("float64")
        self.y = np.random.uniform(0.1, 1, []).astype("float64")


125
class TestElementwiseMaxFP16Op_ZeroDim1(TestElementwiseFP16Op):
126
    def init_data(self):
127 128
        self.x = np.random.uniform(0.1, 1, []).astype(np.float16)
        self.y = np.random.uniform(0.1, 1, []).astype(np.float16)
129 130 131


class TestElementwiseMaxOp_ZeroDim2(TestElementwiseOp):
132 133 134 135 136
    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")


137
class TestElementwiseMaxFP16Op_ZeroDim2(TestElementwiseFP16Op):
138
    def init_data(self):
139 140
        self.x = np.random.uniform(0.1, 1, [13, 17]).astype(np.float16)
        self.y = np.random.uniform(0.1, 1, []).astype(np.float16)
141 142 143


class TestElementwiseMaxOp_ZeroDim3(TestElementwiseOp):
144 145 146 147 148
    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")


149
class TestElementwiseMaxFP16Op_ZeroDim3(TestElementwiseFP16Op):
150
    def init_data(self):
151 152
        self.x = np.random.uniform(0.1, 1, []).astype(np.float16)
        self.y = np.random.uniform(0.1, 1, [13, 17]).astype(np.float16)
153 154


155 156 157 158 159 160 161 162
@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.",
)
163
class TestElementwiseBF16Op(OpTest):
164 165 166 167 168 169 170 171 172 173
    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
        )

174
    def setUp(self):
175
        self.init_data()
176
        self.op_type = "elementwise_max"
177
        self.python_api = paddle.maximum
178
        self.public_python_api = paddle.maximum
H
heyanru 已提交
179 180
        self.prim_op_type = "prim"
        self.enable_cinn = False
181 182
        self.dtype = np.uint16
        self.inputs = {
183 184 185 186 187
            '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))
188 189 190
        }

    def test_check_output(self):
191
        if hasattr(self, 'attrs'):
W
wanghuancoder 已提交
192
            self.check_output(check_dygraph=False)
193
        else:
194
            self.check_output(check_dygraph=True)
195 196

    def test_check_grad_normal(self):
197
        if hasattr(self, 'attrs'):
H
heyanru 已提交
198
            # check_prim=False, bfloat16 is not supported in `less_equal`
199
            self.check_grad(
W
wanghuancoder 已提交
200
                ['X', 'Y'], 'Out', numeric_grad_delta=0.05, check_dygraph=False
201 202
            )
        else:
W
wanghuancoder 已提交
203
            self.check_grad(['X', 'Y'], 'Out', numeric_grad_delta=0.05)
204 205 206 207 208 209 210 211 212 213 214 215

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


216 217 218 219 220 221
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")


222 223 224 225 226 227 228
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


229
@skip_check_grad_ci(
230 231
    reason="[skip shape check] Use y_shape(1) to test broadcast."
)
232
class TestElementwiseMaxOp_scalar(TestElementwiseOp):
233 234 235 236 237
    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")


238 239 240 241
@skip_check_grad_ci(
    reason="[skip shape check] Use y_shape(1) to test broadcast."
)
class TestElementwiseMaxFP16Op_scalar(TestElementwiseFP16Op):
242
    def init_data(self):
243 244
        self.x = np.random.random_integers(-5, 5, [2, 3, 20]).astype(np.float16)
        self.y = np.array([0.5]).astype(np.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
        self.y = self.x + sgn * np.random.uniform(0.1, 1, (100,)).astype(
            "float64"
        )


256
class TestElementwiseMaxFP16Op_Vector(TestElementwiseFP16Op):
257
    def init_data(self):
258 259
        self.x = np.random.random((100,)).astype(np.float16)
        sgn = np.random.choice([-1, 1], (100,)).astype(np.float16)
260
        self.y = self.x + sgn * np.random.uniform(0.1, 1, (100,)).astype(
261
            np.float16
262 263 264 265 266 267 268 269 270 271
        )


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_2(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, (1, 3, 100)).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
        self.inputs = {'X': x, 'Y': y}

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


294
class TestElementwiseMaxFP16Op_broadcast_2(TestElementwiseFP16Op):
295 296 297
    def setUp(self):
        self.op_type = "elementwise_max"
        self.python_api = paddle.maximum
298
        self.public_python_api = paddle.maximum
299
        self.prim_op_type = "prim"
300
        self.dtype = np.float16
301 302 303 304 305 306 307 308 309 310 311 312 313 314
        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)
            )
        }


315 316 317
class TestElementwiseMaxOp_broadcast_4(TestElementwiseOp):
    def setUp(self):
        self.op_type = "elementwise_max"
318
        self.python_api = paddle.maximum
319
        self.public_python_api = paddle.maximum
H
heyanru 已提交
320
        self.prim_op_type = "prim"
321 322
        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)
323
        y = x + sgn * np.random.uniform(1, 2, (2, 3, 1, 5)).astype(np.float64)
324 325 326 327 328
        self.inputs = {'X': x, 'Y': y}

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


329
class TestElementwiseFP16Op_broadcast_4(TestElementwiseFP16Op):
330 331 332
    def setUp(self):
        self.op_type = "elementwise_max"
        self.python_api = paddle.maximum
333
        self.public_python_api = paddle.maximum
334
        self.prim_op_type = "prim"
335
        self.dtype = np.float16
336 337 338 339 340 341 342
        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 已提交
343 344
if __name__ == '__main__':
    unittest.main()