test_top_k_v2_op.py 16.4 KB
Newer Older
W
wawltor 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
16

W
wawltor 已提交
17
import numpy as np
18 19 20 21 22
from eager_op_test import (
    OpTest,
    convert_float_to_uint16,
    convert_uint16_to_float,
)
23

W
wawltor 已提交
24
import paddle
25
from paddle.fluid import core
W
wawltor 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51


def numpy_topk(x, k=1, axis=-1, largest=True):
    if axis < 0:
        axis = len(x.shape) + axis
    if largest:
        indices = np.argsort(-x, axis=axis)
    else:
        indices = np.argsort(x, axis=axis)
    if largest:
        value = -np.sort(-x, axis=axis)
    else:
        value = np.sort(x, axis=axis)
    indices = indices.take(indices=range(0, k), axis=axis)
    value = value.take(indices=range(0, k), axis=axis)
    return value, indices


class TestTopkOp(OpTest):
    def init_args(self):
        self.k = 3
        self.axis = 1
        self.largest = True

    def setUp(self):
        self.op_type = "top_k_v2"
Z
zqw_1997 已提交
52
        self.prim_op_type = "prim"
H
hong 已提交
53
        self.python_api = paddle.topk
54
        self.public_python_api = paddle.topk
W
wawltor 已提交
55 56 57
        self.dtype = np.float64
        self.input_data = np.random.rand(10, 20)
        self.init_args()
58
        self.if_enable_cinn()
W
wawltor 已提交
59 60
        self.inputs = {'X': self.input_data}
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
61 62 63
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
W
wawltor 已提交
64 65
        self.outputs = {'Out': output, 'Indices': indices}

66 67 68
    def if_enable_cinn(self):
        pass

W
wawltor 已提交
69
    def test_check_output(self):
W
wanghuancoder 已提交
70
        self.check_output()
W
wawltor 已提交
71 72

    def test_check_grad(self):
W
wanghuancoder 已提交
73
        self.check_grad(['X'], 'Out', check_prim=True)
W
wawltor 已提交
74 75


76
class TestTopkOp1(TestTopkOp):
W
wawltor 已提交
77 78 79 80 81 82
    def init_args(self):
        self.k = 3
        self.axis = 0
        self.largest = False


W
wawltor 已提交
83
class TestTopkOp2(TestTopkOp):
W
wawltor 已提交
84 85 86 87 88 89
    def init_args(self):
        self.k = 4
        self.axis = 0
        self.largest = False


Z
zqw_1997 已提交
90
class TestTopkOp3(TestTopkOp):
W
wawltor 已提交
91
    def init_args(self):
W
wawltor 已提交
92 93 94
        self.k = 6
        self.axis = 1
        self.largest = True
W
wawltor 已提交
95

W
wawltor 已提交
96 97
    def setUp(self):
        self.op_type = "top_k_v2"
Z
zqw_1997 已提交
98
        self.prim_op_type = "prim"
H
hong 已提交
99
        self.python_api = paddle.topk
100
        self.public_python_api = paddle.topk
W
wawltor 已提交
101 102 103 104 105
        self.dtype = np.float64
        self.input_data = np.random.rand(16, 100)
        self.init_args()
        self.inputs = {'X': self.input_data}
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
106 107 108
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
W
wawltor 已提交
109
        self.outputs = {'Out': output, 'Indices': indices}
W
wawltor 已提交
110

W
wawltor 已提交
111 112

class TestTopkOp4(TestTopkOp):
W
wawltor 已提交
113 114 115 116 117 118 119
    def init_args(self):
        self.k = 3
        self.axis = 1
        self.largest = True

    def setUp(self):
        self.op_type = "top_k_v2"
Z
zqw_1997 已提交
120
        self.prim_op_type = "prim"
H
hong 已提交
121
        self.python_api = paddle.topk
122
        self.public_python_api = paddle.topk
W
wawltor 已提交
123 124 125
        self.dtype = np.float64
        self.input_data = np.random.rand(10, 10, 5)
        self.init_args()
126
        self.if_enable_cinn()
W
wawltor 已提交
127 128
        self.inputs = {'X': self.input_data}
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
129 130 131
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
W
wawltor 已提交
132 133 134
        self.outputs = {'Out': output, 'Indices': indices}


W
wawltor 已提交
135
class TestTopkOp5(TestTopkOp):
W
wawltor 已提交
136 137 138 139 140 141 142
    def init_args(self):
        self.k = 3
        self.axis = 1
        self.largest = True

    def setUp(self):
        self.op_type = "top_k_v2"
Z
zqw_1997 已提交
143
        self.prim_op_type = "prim"
H
hong 已提交
144
        self.python_api = paddle.topk
145
        self.public_python_api = paddle.topk
W
wawltor 已提交
146 147 148
        self.dtype = np.float64
        self.input_data = np.random.rand(10, 10, 5)
        self.init_args()
149
        self.if_enable_cinn()
W
wawltor 已提交
150 151
        self.inputs = {'X': self.input_data}
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
152 153 154
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
155 156 157
        self.outputs = {'Out': output, 'Indices': indices}


Z
zqw_1997 已提交
158
class TestTopkOp6(TestTopkOp):
159
    def init_args(self):
Z
zqw_1997 已提交
160
        self.k = 3
161 162 163 164 165
        self.axis = 1
        self.largest = True

    def setUp(self):
        self.op_type = "top_k_v2"
Z
zqw_1997 已提交
166
        self.prim_op_type = "prim"
H
hong 已提交
167
        self.python_api = paddle.topk
168
        self.public_python_api = paddle.topk
Z
zqw_1997 已提交
169 170 171
        self.dtype = np.float32
        self.input_data = np.random.rand(10, 10, 5)
        self.init_args()
172
        self.if_enable_cinn()
Z
zqw_1997 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        self.inputs = {'X': self.input_data}
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
        self.outputs = {'Out': output, 'Indices': indices}


class TestTopkOp7(TestTopkOp):
    def init_args(self):
        self.k = 10
        self.axis = 1
        self.largest = True

    def setUp(self):
        self.op_type = "top_k_v2"
        self.prim_op_type = "prim"
        self.python_api = paddle.topk
191
        self.public_python_api = paddle.topk
Z
zqw_1997 已提交
192 193
        self.dtype = np.float16
        self.input_data = np.random.rand(10, 20, 10)
194
        self.init_args()
195
        self.if_enable_cinn()
196 197
        self.inputs = {'X': self.input_data}
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
198 199 200
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
W
wawltor 已提交
201 202 203
        self.outputs = {'Out': output, 'Indices': indices}


204 205 206 207 208 209 210 211 212
class TestTopkFP16Op(TestTopkOp):
    def setUp(self):
        self.op_type = "top_k_v2"
        self.python_api = paddle.topk
        self.public_python_api = paddle.topk
        self.dtype = np.float16
        self.prim_op_type = "prim"
        self.input_data = np.random.rand(10, 20).astype(self.dtype)
        self.init_args()
213
        self.if_enable_cinn()
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        self.inputs = {'X': self.input_data}
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
        self.outputs = {'Out': output, 'Indices': indices}


@unittest.skipIf(
    not core.is_compiled_with_cuda()
    or not core.is_bfloat16_supported(core.CUDAPlace(0)),
    "core is not compiled with CUDA or not support the bfloat16",
)
class TestTopkBF16Op(TestTopkOp):
    def setUp(self):
        self.op_type = "top_k_v2"
        self.python_api = paddle.topk
        self.public_python_api = paddle.topk
        self.dtype = np.uint16
        self.prim_op_type = "prim"
234
        self.input_data = np.random.random([10, 20]).astype(np.float32)
235
        self.init_args()
236
        self.if_enable_cinn()
237
        self.inputs = {'X': convert_float_to_uint16(self.input_data)}
238
        self.input_data = convert_uint16_to_float(self.inputs['X'])
239 240 241 242 243 244 245 246 247
        self.attrs = {'k': self.k, 'axis': self.axis, 'largest': self.largest}
        output, indices = numpy_topk(
            self.input_data, axis=self.axis, k=self.k, largest=self.largest
        )
        self.outputs = {
            'Out': convert_float_to_uint16(output),
            'Indices': indices,
        }

248 249 250
    def if_enable_cinn(self):
        self.enable_cinn = False

251 252
    def test_check_output(self):
        place = core.CUDAPlace(0)
253
        self.check_output_with_place(place)
254 255 256

    def test_check_grad(self):
        place = core.CUDAPlace(0)
257
        self.check_grad_with_place(place, ['X'], 'Out', check_prim=True)
258 259


W
wawltor 已提交
260 261 262 263 264 265 266
class TestTopKAPI(unittest.TestCase):
    def setUp(self):
        np.random.seed(123)
        self.input_data = np.random.rand(6, 7, 8)
        self.large_input_data = np.random.rand(2, 1030)

    def run_dygraph(self, place):
H
hong 已提交
267 268 269 270 271 272
        with paddle.fluid.dygraph.guard(place):
            input_tensor = paddle.to_tensor(self.input_data)
            large_input_tensor = paddle.to_tensor(self.large_input_data)
            # test case for basic test case 1
            paddle_result = paddle.topk(input_tensor, k=2)
            numpy_result = numpy_topk(self.input_data, k=2)
273 274 275 276 277 278
            np.testing.assert_allclose(
                paddle_result[0].numpy(), numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[1].numpy(), numpy_result[1], rtol=1e-05
            )
H
hong 已提交
279 280 281
            # test case for basic test case 2 with axis
            paddle_result = paddle.topk(input_tensor, k=2, axis=1)
            numpy_result = numpy_topk(self.input_data, k=2, axis=1)
282 283 284 285 286 287
            np.testing.assert_allclose(
                paddle_result[0].numpy(), numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[1].numpy(), numpy_result[1], rtol=1e-05
            )
H
hong 已提交
288 289 290 291
            # test case for basic test case 3 with tensor K
            k_tensor = paddle.to_tensor(np.array([2]))
            paddle_result = paddle.topk(input_tensor, k=k_tensor, axis=1)
            numpy_result = numpy_topk(self.input_data, k=2, axis=1)
292 293 294 295 296 297
            np.testing.assert_allclose(
                paddle_result[0].numpy(), numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[1].numpy(), numpy_result[1], rtol=1e-05
            )
H
hong 已提交
298 299
            # test case for basic test case 4 with tensor largest
            k_tensor = paddle.to_tensor(np.array([2]))
300 301 302 303 304 305 306 307 308 309 310 311
            paddle_result = paddle.topk(
                input_tensor, k=2, axis=1, largest=False
            )
            numpy_result = numpy_topk(
                self.input_data, k=2, axis=1, largest=False
            )
            np.testing.assert_allclose(
                paddle_result[0].numpy(), numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[1].numpy(), numpy_result[1], rtol=1e-05
            )
H
hong 已提交
312 313
            # test case for basic test case 5 with axis -1
            k_tensor = paddle.to_tensor(np.array([2]))
314 315 316 317 318 319 320 321 322 323 324 325
            paddle_result = paddle.topk(
                input_tensor, k=2, axis=-1, largest=False
            )
            numpy_result = numpy_topk(
                self.input_data, k=2, axis=-1, largest=False
            )
            np.testing.assert_allclose(
                paddle_result[0].numpy(), numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[1].numpy(), numpy_result[1], rtol=1e-05
            )
326
            # test case for basic test case 6 for the partial sort
H
hong 已提交
327 328
            paddle_result = paddle.topk(large_input_tensor, k=1, axis=-1)
            numpy_result = numpy_topk(self.large_input_data, k=1, axis=-1)
329 330 331 332 333 334
            np.testing.assert_allclose(
                paddle_result[0].numpy(), numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[1].numpy(), numpy_result[1], rtol=1e-05
            )
335
            # test case for basic test case 7 for the unsorted
H
hong 已提交
336
            paddle_result = paddle.topk(input_tensor, k=2, axis=1, sorted=False)
337 338 339
            sort_paddle = numpy_topk(
                np.array(paddle_result[0].numpy()), axis=1, k=2
            )
H
hong 已提交
340
            numpy_result = numpy_topk(self.input_data, k=2, axis=1)
341 342 343
            np.testing.assert_allclose(
                sort_paddle[0], numpy_result[0], rtol=1e-05
            )
W
wawltor 已提交
344 345 346

    def run_static(self, place):
        paddle.enable_static()
347 348 349 350 351 352 353 354 355
        with paddle.static.program_guard(
            paddle.static.Program(), paddle.static.Program()
        ):
            input_tensor = paddle.static.data(
                name="x", shape=[6, 7, 8], dtype="float64"
            )
            large_input_tensor = paddle.static.data(
                name="large_x", shape=[2, 1030], dtype="float64"
            )
W
wawltor 已提交
356 357 358 359
            k_tensor = paddle.static.data(name="k", shape=[1], dtype="int32")
            result1 = paddle.topk(input_tensor, k=2)
            result2 = paddle.topk(input_tensor, k=2, axis=-1)
            result3 = paddle.topk(input_tensor, k=k_tensor, axis=1)
360 361
            self.assertEqual(result3[0].shape, (6, -1, 8))
            self.assertEqual(result3[1].shape, (6, -1, 8))
W
wawltor 已提交
362 363 364 365 366 367 368
            result4 = paddle.topk(input_tensor, k=2, axis=1, largest=False)
            result5 = paddle.topk(input_tensor, k=2, axis=-1, largest=False)
            result6 = paddle.topk(large_input_tensor, k=1, axis=-1)
            result7 = paddle.topk(input_tensor, k=2, axis=1, sorted=False)
            exe = paddle.static.Executor(place)
            input_data = np.random.rand(10, 20).astype("float64")
            large_input_data = np.random.rand(2, 100).astype("float64")
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
            paddle_result = exe.run(
                feed={
                    "x": self.input_data,
                    "large_x": self.large_input_data,
                    "k": np.array([2]).astype("int32"),
                },
                fetch_list=[
                    result1[0],
                    result1[1],
                    result2[0],
                    result2[1],
                    result3[0],
                    result3[1],
                    result4[0],
                    result4[1],
                    result5[0],
                    result5[1],
                    result6[0],
                    result6[1],
                    result7[0],
                    result7[1],
                ],
            )
W
wawltor 已提交
392
            numpy_result = numpy_topk(self.input_data, k=2)
393 394 395 396 397 398
            np.testing.assert_allclose(
                paddle_result[0], numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[1], numpy_result[1], rtol=1e-05
            )
W
wawltor 已提交
399
            numpy_result = numpy_topk(self.input_data, k=2, axis=-1)
400 401 402 403 404 405
            np.testing.assert_allclose(
                paddle_result[2], numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[3], numpy_result[1], rtol=1e-05
            )
W
wawltor 已提交
406
            numpy_result = numpy_topk(self.input_data, k=2, axis=1)
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
            np.testing.assert_allclose(
                paddle_result[4], numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[5], numpy_result[1], rtol=1e-05
            )
            numpy_result = numpy_topk(
                self.input_data, k=2, axis=1, largest=False
            )
            np.testing.assert_allclose(
                paddle_result[6], numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[7], numpy_result[1], rtol=1e-05
            )
            numpy_result = numpy_topk(
                self.input_data, k=2, axis=-1, largest=False
            )
            np.testing.assert_allclose(
                paddle_result[8], numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[9], numpy_result[1], rtol=1e-05
            )
W
wawltor 已提交
431
            numpy_result = numpy_topk(self.large_input_data, k=1, axis=-1)
432 433 434 435 436 437
            np.testing.assert_allclose(
                paddle_result[10], numpy_result[0], rtol=1e-05
            )
            np.testing.assert_allclose(
                paddle_result[11], numpy_result[1], rtol=1e-05
            )
W
wawltor 已提交
438 439
            sort_paddle = numpy_topk(paddle_result[12], axis=1, k=2)
            numpy_result = numpy_topk(self.input_data, k=2, axis=1)
440 441 442
            np.testing.assert_allclose(
                sort_paddle[0], numpy_result[0], rtol=1e-05
            )
W
wawltor 已提交
443 444 445 446 447 448 449 450 451

    def test_cases(self):
        places = [core.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(core.CUDAPlace(0))
        for place in places:
            self.run_dygraph(place)
            self.run_static(place)

452
    def test_errors(self):
H
hong 已提交
453 454 455 456
        with paddle.fluid.dygraph.guard():
            x = paddle.to_tensor([1, 2, 3])
            with self.assertRaises(BaseException):
                paddle.topk(x, k=-1)
457

H
hong 已提交
458 459
            with self.assertRaises(BaseException):
                paddle.topk(x, k=0)
460

W
wawltor 已提交
461 462

if __name__ == "__main__":
H
hong 已提交
463
    paddle.enable_static()
W
wawltor 已提交
464
    unittest.main()