test_number_count_op.py 2.7 KB
Newer Older
R
Roc 已提交
1
#   Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
R
Roc 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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
wanghuancoder 已提交
17
import eager_op_test
18 19
import numpy as np

R
Roc 已提交
20 21
import paddle
from paddle.distributed.models.moe import utils
22
from paddle.fluid import core
R
Roc 已提交
23 24


R
Roc 已提交
25
def count(x, upper_num):
26
    res = np.zeros((upper_num,)).astype(int)
R
Roc 已提交
27 28 29 30 31 32
    for i in x.reshape(-1):
        if i >= 0 and i < len(res):
            res[i] += 1
    return res


33 34 35 36
def number_count_wrapper(numbers, upper_num):
    return paddle._legacy_C_ops.number_count(numbers, 'upper_range', upper_num)


37 38 39
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
W
wanghuancoder 已提交
40
class TestNumberCountOpInt64(eager_op_test.OpTest):
R
Roc 已提交
41
    def setUp(self):
R
Roc 已提交
42
        upper_num = 16
R
Roc 已提交
43
        self.op_type = "number_count"
44
        self.python_api = number_count_wrapper
R
Roc 已提交
45 46 47 48
        x = np.random.randint(-1, upper_num, size=(1000, 2)).astype('int64')
        self.inputs = {'numbers': x}
        self.outputs = {'Out': count(x, upper_num)}
        self.attrs = {"upper_range": upper_num}
R
Roc 已提交
49 50 51 52 53

    def test_forward(self):
        self.check_output_with_place(paddle.CUDAPlace(0))


54 55 56
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
R
Roc 已提交
57
class TestNumberCountAPI(unittest.TestCase):
R
Roc 已提交
58
    def setUp(self):
R
Roc 已提交
59
        self.upper_num = 320
60 61 62
        self.x = np.random.randint(-1, self.upper_num, size=(6000, 200)).astype(
            'int64'
        )
R
Roc 已提交
63
        self.out = count(self.x, self.upper_num)
R
Roc 已提交
64 65 66 67 68
        self.place = paddle.CUDAPlace(0)

    def test_api_static(self):
        paddle.enable_static()
        with paddle.static.program_guard(paddle.static.Program()):
69
            x = paddle.static.data('x', self.x.shape, dtype="int64")
R
Roc 已提交
70
            out = utils._number_count(x, self.upper_num)
R
Roc 已提交
71 72 73 74
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'x': self.x}, fetch_list=[out])
            assert np.allclose(res, self.out)

75
    def test_api_dygraph(self):
R
Roc 已提交
76 77
        paddle.disable_static()
        x = paddle.to_tensor(self.x)
R
Roc 已提交
78
        out = utils._number_count(x, self.upper_num)
79
        np.testing.assert_allclose(out.numpy(), self.out)
R
Roc 已提交
80 81 82 83 84


if __name__ == '__main__':
    paddle.enable_static()
    unittest.main()