test_unique_consecutive_op.py 8.7 KB
Newer Older
D
duanboqiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#   Copyright (c) 2020 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
import numpy as np
from op_test import OpTest
import paddle.fluid.core as core

import paddle
import paddle.fluid as fluid


def reference_unique_consecutive(X, return_inverse=False, return_counts=False):
    """
    Reference unique_consecutive implementation using python.
    Args:
        x(Tensor): the input tensor, it's data type should be float32, float64, int32, int64.
        return_inverse(bool, optional): If True, also return the indices for where elements in
            the original input ended up in the returned unique consecutive tensor. Default is False.
        return_counts(bool, optional): If True, also return the counts for each unique consecutive element.
    """
    X = list(X)
    counts_vec = [1] * len(X)
    i = 0
    counts = 1
    last = 0
    inverse_vec = [0] * len(X)
    inverse_vec[last] = i
    cnt = 0
    while i < len(X) - 1:
        if X[i] == X[i + 1]:
            if return_counts:
                counts_vec[cnt] += 1
            del X[i]
        else:
            i += 1
            cnt += 1
        if return_inverse:
            last += 1
            inverse_vec[last] = i
    if return_counts:
        counts_vec = counts_vec[:len(X)]
    if return_inverse and return_counts:
        return X, np.array(inverse_vec), np.array(counts_vec)
    elif return_counts:
        return X, np.array(counts_vec)
    elif return_inverse:
        return X, np.array(inverse_vec)
    else:
        return X


class TestUniqueConsecutiveOp(OpTest):
    """case 1"""

    def config(self):
        self.x_size = 100
        self.x_range = 20
        self.return_inverse = False
        self.return_counts = False
72
        self.python_api = paddle.unique_consecutive
D
duanboqiang 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85

    def init_kernel_type(self):
        self.dtype = "float32" if core.is_compiled_with_rocm() else "float64"

    def setUp(self):
        self.init_kernel_type()
        self.config()
        self.op_type = "unique_consecutive"
        x = np.random.randint(self.x_range, size=self.x_size).astype(self.dtype)
        result = reference_unique_consecutive(x, self.return_inverse,
                                              self.return_counts)
        out = reference_unique_consecutive(x)
        out = np.array(out).astype(self.dtype)
86 87 88
        self.inputs = {
            'X': x,
        }
89
        self.python_out_sig = ["Out"]
D
duanboqiang 已提交
90
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT32)}
91 92 93
        self.outputs = {
            'Out': out,
        }
D
duanboqiang 已提交
94 95

    def test_check_output(self):
96
        self.check_output(check_eager=True)
D
duanboqiang 已提交
97 98 99 100 101 102 103 104 105 106


class TestUniqueConsecutiveOp2(TestUniqueConsecutiveOp):
    """case 2"""

    def config(self):
        self.x_size = 100
        self.x_range = 20
        self.return_inverse = True
        self.return_counts = False
107
        self.python_api = paddle.unique_consecutive
D
duanboqiang 已提交
108 109 110 111 112 113 114 115 116 117

    def setUp(self):
        self.init_kernel_type()
        self.config()
        self.op_type = "unique_consecutive"
        x = np.random.randint(self.x_range, size=self.x_size).astype(self.dtype)
        result, inverse = reference_unique_consecutive(x, self.return_inverse,
                                                       self.return_counts)
        result = np.array(result).astype(self.dtype)
        inverse = inverse.astype(self.dtype)
118 119 120
        self.inputs = {
            'X': x,
        }
D
duanboqiang 已提交
121 122 123 124
        self.attrs = {
            'return_inverse': self.return_inverse,
            'dtype': int(core.VarDesc.VarType.INT32)
        }
125
        self.python_out_sig = ["Out"]
D
duanboqiang 已提交
126 127 128 129 130 131 132 133 134 135 136
        self.outputs = {'Out': result, 'Index': inverse}


class TestUniqueConsecutiveOp3(TestUniqueConsecutiveOp):
    """case 3"""

    def config(self):
        self.x_size = 100
        self.x_range = 20
        self.return_inverse = False
        self.return_counts = True
137
        self.python_api = paddle.unique_consecutive
D
duanboqiang 已提交
138 139 140 141 142 143 144 145 146 147

    def setUp(self):
        self.init_kernel_type()
        self.config()
        self.op_type = "unique_consecutive"
        x = np.random.randint(self.x_range, size=self.x_size).astype(self.dtype)
        result, counts = reference_unique_consecutive(x, self.return_inverse,
                                                      self.return_counts)
        result = np.array(result).astype(self.dtype)
        counts = counts.astype(self.dtype)
148 149 150
        self.inputs = {
            'X': x,
        }
D
duanboqiang 已提交
151 152 153 154
        self.attrs = {
            'return_counts': self.return_counts,
            'dtype': int(core.VarDesc.VarType.INT32)
        }
155
        self.python_out_sig = ["Out"]
D
duanboqiang 已提交
156 157 158 159 160 161 162 163 164 165 166
        self.outputs = {'Out': result, 'Counts': counts}


class TestUniqueConsecutiveOp4(TestUniqueConsecutiveOp):
    """case 4"""

    def config(self):
        self.x_size = 100
        self.x_range = 20
        self.return_inverse = True
        self.return_counts = True
167
        self.python_api = paddle.unique_consecutive
D
duanboqiang 已提交
168 169 170 171 172 173 174 175 176 177 178

    def setUp(self):
        self.init_kernel_type()
        self.config()
        self.op_type = "unique_consecutive"
        x = np.random.randint(self.x_range, size=self.x_size).astype(self.dtype)
        result, inverse, counts = reference_unique_consecutive(
            x, self.return_inverse, self.return_counts)
        result = np.array(result).astype(self.dtype)
        inverse = inverse.astype(self.dtype)
        counts = counts.astype(self.dtype)
179 180 181
        self.inputs = {
            'X': x,
        }
D
duanboqiang 已提交
182 183 184 185 186
        self.attrs = {
            'return_inverse': self.return_inverse,
            'return_counts': self.return_counts,
            'dtype': int(core.VarDesc.VarType.INT32)
        }
187
        self.python_out_sig = ["Out"]
D
duanboqiang 已提交
188 189 190 191
        self.outputs = {'Out': result, 'Index': inverse, 'Counts': counts}


class TestUniqueConsecutiveAPI(unittest.TestCase):
192

D
duanboqiang 已提交
193 194 195 196 197 198 199 200
    def setUp(self):
        self.places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.places.append(fluid.CUDAPlace(0))

    def check_static_result(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            paddle.enable_static()
201 202 203
            input_x = fluid.data(name="input_x", shape=[
                100,
            ], dtype="float32")
D
duanboqiang 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
            result = paddle.unique_consecutive(input_x)
            x_np = np.random.randint(20, size=100).astype("float32")
            exe = fluid.Executor(place)
            fetches = exe.run(fluid.default_main_program(),
                              feed={"input_x": x_np},
                              fetch_list=[result])

    def test_static(self):
        for place in self.places:
            self.check_static_result(place=place)

    def test_dygraph(self):
        for place in self.places:
            with fluid.dygraph.guard(place):
                input_x = np.random.randint(20, size=100).astype("float64")
                x = paddle.to_tensor(input_x)
                result = paddle.unique_consecutive(x)


class TestUniqueConsecutiveCase2API(unittest.TestCase):
224

D
duanboqiang 已提交
225 226 227 228 229 230 231 232
    def setUp(self):
        self.places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            self.places.append(fluid.CUDAPlace(0))

    def check_static_result(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            paddle.enable_static()
233 234 235
            input_x = fluid.data(name="input_x", shape=[
                100,
            ], dtype="float32")
D
duanboqiang 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
            result, inverse, counts = paddle.unique_consecutive(
                input_x, return_inverse=True, return_counts=True)
            x_np = np.random.randint(20, size=100).astype("float32")
            exe = fluid.Executor(place)
            fetches = exe.run(fluid.default_main_program(),
                              feed={"input_x": x_np},
                              fetch_list=[result])

    def test_static(self):
        for place in self.places:
            self.check_static_result(place=place)

    def test_dygraph(self):
        for place in self.places:
            with fluid.dygraph.guard(place):
                input_x = np.random.randint(20, size=100).astype("float64")
                x = paddle.to_tensor(input_x)
                result, inverse, counts = paddle.unique_consecutive(
                    x, return_inverse=True, return_counts=True)


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