test_unique.py 11.5 KB
Newer Older
Z
zhoukunsheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2019 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

Z
zhoukunsheng 已提交
17
import numpy as np
W
wanghuancoder 已提交
18
from eager_op_test import OpTest, paddle_static_guard
19

Z
Zhang Ting 已提交
20
import paddle
21
from paddle.fluid import core
Z
zhoukunsheng 已提交
22 23 24 25 26 27 28 29


class TestUniqueOp(OpTest):
    def setUp(self):
        self.op_type = "unique"
        self.init_config()

    def test_check_output(self):
W
wanghuancoder 已提交
30 31 32
        self.check_output(
            check_dygraph=False
        )  # unique return sorted data in dygraph
Z
zhoukunsheng 已提交
33 34

    def init_config(self):
35 36 37
        self.inputs = {
            'X': np.array([2, 3, 3, 1, 5, 3], dtype='int64'),
        }
Z
zhoukunsheng 已提交
38 39
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT32)}
        self.outputs = {
40
            'Out': np.array([2, 3, 1, 5], dtype='int64'),
41
            'Index': np.array([0, 1, 1, 2, 3, 1], dtype='int32'),
Z
zhoukunsheng 已提交
42 43 44 45 46
        }


class TestOne(TestUniqueOp):
    def init_config(self):
47 48 49
        self.inputs = {
            'X': np.array([2], dtype='int64'),
        }
Z
zhoukunsheng 已提交
50 51
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT32)}
        self.outputs = {
52
            'Out': np.array([2], dtype='int64'),
53
            'Index': np.array([0], dtype='int32'),
Z
zhoukunsheng 已提交
54 55 56 57 58
        }


class TestRandom(TestUniqueOp):
    def init_config(self):
59
        self.inputs = {'X': np.random.randint(0, 100, (150,), dtype='int64')}
Z
zhoukunsheng 已提交
60
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT64)}
61 62 63
        np_unique, np_index, reverse_index = np.unique(
            self.inputs['X'], True, True
        )
Z
zhoukunsheng 已提交
64 65 66 67
        np_tuple = [(np_unique[i], np_index[i]) for i in range(len(np_unique))]
        np_tuple.sort(key=lambda x: x[1])
        target_out = np.array([i[0] for i in np_tuple], dtype='int64')
        target_index = np.array(
68 69
            [list(target_out).index(i) for i in self.inputs['X']], dtype='int64'
        )
Z
zhoukunsheng 已提交
70 71 72 73

        self.outputs = {'Out': target_out, 'Index': target_index}


74 75
class TestUniqueRaiseError(unittest.TestCase):
    def test_errors(self):
W
wanghuancoder 已提交
76
        with paddle_static_guard():
77

W
wanghuancoder 已提交
78 79
            def test_type():
                paddle.unique([10])
80

W
wanghuancoder 已提交
81
            self.assertRaises(TypeError, test_type)
82

W
wanghuancoder 已提交
83 84 85 86 87
            def test_dtype():
                data = paddle.static.data(
                    shape=[10], dtype="float16", name="input"
                )
                paddle.unique(data)
88

W
wanghuancoder 已提交
89
            self.assertRaises(TypeError, test_dtype)
90 91


92 93 94
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
95 96
class TestOneGPU(TestUniqueOp):
    def init_config(self):
97 98 99
        self.inputs = {
            'X': np.array([2], dtype='int64'),
        }
100 101
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT32)}
        self.outputs = {
102
            'Out': np.array([2], dtype='int64'),
103
            'Index': np.array([0], dtype='int32'),
104 105 106 107 108
        }

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            place = core.CUDAPlace(0)
W
wanghuancoder 已提交
109 110 111
            self.check_output_with_place(
                place, atol=1e-5, check_dygraph=False
            )  # unique return sorted data in dygraph
112 113


114 115 116
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
117 118
class TestRandomGPU(TestUniqueOp):
    def init_config(self):
119
        self.inputs = {'X': np.random.randint(0, 100, (150,), dtype='int64')}
120
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT64)}
121 122 123
        np_unique, np_index, reverse_index = np.unique(
            self.inputs['X'], True, True
        )
124 125 126 127
        np_tuple = [(np_unique[i], np_index[i]) for i in range(len(np_unique))]
        np_tuple.sort(key=lambda x: x[1])
        target_out = np.array([i[0] for i in np_tuple], dtype='int64')
        target_index = np.array(
128 129
            [list(target_out).index(i) for i in self.inputs['X']], dtype='int64'
        )
130 131 132 133 134 135

        self.outputs = {'Out': target_out, 'Index': target_index}

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            place = core.CUDAPlace(0)
W
wanghuancoder 已提交
136 137 138
            self.check_output_with_place(
                place, atol=1e-5, check_dygraph=False
            )  # unique return sorted data in dygraph
139 140


Z
Zhang Ting 已提交
141 142 143
class TestSortedUniqueOp(TestUniqueOp):
    def init_config(self):
        self.inputs = {'X': np.array([2, 3, 3, 1, 5, 3], dtype='int64')}
144 145 146 147 148 149 150
        unique, indices, inverse, count = np.unique(
            self.inputs['X'],
            return_index=True,
            return_inverse=True,
            return_counts=True,
            axis=None,
        )
Z
Zhang Ting 已提交
151 152 153 154 155 156
        self.attrs = {
            'dtype': int(core.VarDesc.VarType.INT32),
            "return_index": True,
            "return_inverse": True,
            "return_counts": True,
            "axis": None,
157
            "is_sorted": True,
Z
Zhang Ting 已提交
158 159 160 161 162 163 164 165 166 167 168 169
        }
        self.outputs = {
            'Out': unique,
            'Indices': indices,
            "Index": inverse,
            "Counts": count,
        }


class TestUniqueOpAxisNone(TestUniqueOp):
    def init_config(self):
        self.inputs = {'X': np.random.random((4, 7, 10)).astype('float64')}
170 171 172 173 174 175 176
        unique, indices, inverse, counts = np.unique(
            self.inputs['X'],
            return_index=True,
            return_inverse=True,
            return_counts=True,
            axis=None,
        )
Z
Zhang Ting 已提交
177 178 179 180 181 182
        self.attrs = {
            'dtype': int(core.VarDesc.VarType.INT32),
            "return_index": True,
            "return_inverse": True,
            "return_counts": True,
            "axis": None,
183
            "is_sorted": True,
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        }
        self.outputs = {
            'Out': unique,
            'Indices': indices,
            "Index": inverse,
            "Counts": counts,
        }


class TestUniqueOpAxisNeg(TestUniqueOp):
    def init_config(self):
        self.inputs = {'X': np.random.random((6, 1, 8)).astype('float64')}
        unique, indices, inverse, counts = np.unique(
            self.inputs['X'],
            return_index=True,
            return_inverse=True,
            return_counts=True,
            axis=-1,
        )
        self.attrs = {
            'dtype': int(core.VarDesc.VarType.INT32),
            "return_index": True,
            "return_inverse": True,
            "return_counts": True,
            "axis": [-1],
            "is_sorted": True,
Z
Zhang Ting 已提交
210 211 212 213 214 215 216 217 218 219 220 221
        }
        self.outputs = {
            'Out': unique,
            'Indices': indices,
            "Index": inverse,
            "Counts": counts,
        }


class TestUniqueOpAxis1(TestUniqueOp):
    def init_config(self):
        self.inputs = {'X': np.random.random((3, 8, 8)).astype('float64')}
222 223 224 225 226 227 228
        unique, indices, inverse, counts = np.unique(
            self.inputs['X'],
            return_index=True,
            return_inverse=True,
            return_counts=True,
            axis=1,
        )
Z
Zhang Ting 已提交
229 230 231 232 233 234
        self.attrs = {
            'dtype': int(core.VarDesc.VarType.INT32),
            "return_index": True,
            "return_inverse": True,
            "return_counts": True,
            "axis": [1],
235
            "is_sorted": True,
Z
Zhang Ting 已提交
236 237 238 239 240 241 242 243 244 245 246
        }
        self.outputs = {
            'Out': unique,
            'Indices': indices,
            "Index": inverse,
            "Counts": counts,
        }


class TestUniqueAPI(unittest.TestCase):
    def test_dygraph_api_out(self):
W
wanghuancoder 已提交
247
        paddle.disable_static()
Z
Zhang Ting 已提交
248 249 250 251 252 253 254
        x_data = x_data = np.random.randint(0, 10, (120))
        x = paddle.to_tensor(x_data)
        out = paddle.unique(x)
        expected_out = np.unique(x_data)
        self.assertTrue((out.numpy() == expected_out).all(), True)

    def test_dygraph_api_attr(self):
W
wanghuancoder 已提交
255
        paddle.disable_static()
Z
Zhang Ting 已提交
256 257
        x_data = np.random.random((3, 5, 5)).astype("float32")
        x = paddle.to_tensor(x_data)
258 259 260 261 262 263 264 265 266 267 268 269 270 271
        out, index, inverse, counts = paddle.unique(
            x,
            return_index=True,
            return_inverse=True,
            return_counts=True,
            axis=0,
        )
        np_out, np_index, np_inverse, np_counts = np.unique(
            x_data,
            return_index=True,
            return_inverse=True,
            return_counts=True,
            axis=0,
        )
Z
Zhang Ting 已提交
272 273 274 275 276
        self.assertTrue((out.numpy() == np_out).all(), True)
        self.assertTrue((index.numpy() == np_index).all(), True)
        self.assertTrue((inverse.numpy() == np_inverse).all(), True)
        self.assertTrue((counts.numpy() == np_counts).all(), True)

Z
Zhang Ting 已提交
277
    def test_dygraph_attr_dtype(self):
W
wanghuancoder 已提交
278
        paddle.disable_static()
Z
Zhang Ting 已提交
279 280
        x_data = x_data = np.random.randint(0, 10, (120))
        x = paddle.to_tensor(x_data)
281 282 283 284 285 286 287
        out, indices, inverse, counts = paddle.unique(
            x,
            return_index=True,
            return_inverse=True,
            return_counts=True,
            dtype="int32",
        )
Z
Zhang Ting 已提交
288
        expected_out, np_indices, np_inverse, np_counts = np.unique(
289 290
            x_data, return_index=True, return_inverse=True, return_counts=True
        )
Z
Zhang Ting 已提交
291 292 293 294 295
        self.assertTrue((out.numpy() == expected_out).all(), True)
        self.assertTrue((indices.numpy() == np_indices).all(), True)
        self.assertTrue((inverse.numpy() == np_inverse).all(), True)
        self.assertTrue((counts.numpy() == np_counts).all(), True)

Z
Zhang Ting 已提交
296
    def test_static_graph(self):
W
wanghuancoder 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310
        with paddle_static_guard():
            with paddle.static.program_guard(
                paddle.static.Program(), paddle.static.Program()
            ):
                x = paddle.static.data(name='x', shape=[3, 2], dtype='float64')
                unique, inverse, counts = paddle.unique(
                    x, return_inverse=True, return_counts=True, axis=0
                )
                place = paddle.CPUPlace()
                exe = paddle.static.Executor(place)
                x_np = np.array([[1, 2], [3, 4], [1, 2]]).astype('float64')
                result = exe.run(
                    feed={"x": x_np}, fetch_list=[unique, inverse, counts]
                )
Z
Zhang Ting 已提交
311 312 313 314 315


class TestUniqueError(unittest.TestCase):
    def test_input_dtype(self):
        def test_x_dtype():
W
wanghuancoder 已提交
316 317 318 319 320 321 322 323
            with paddle_static_guard():
                with paddle.static.program_guard(
                    paddle.static.Program(), paddle.static.Program()
                ):
                    x = paddle.static.data(
                        name='x', shape=[10, 10], dtype='float16'
                    )
                    result = paddle.unique(x)
Z
Zhang Ting 已提交
324

W
wanghuancoder 已提交
325
                self.assertRaises(TypeError, test_x_dtype)
Z
Zhang Ting 已提交
326 327

    def test_attr(self):
W
wanghuancoder 已提交
328 329
        with paddle_static_guard():
            x = paddle.static.data(name='x', shape=[10, 10], dtype='float64')
Z
Zhang Ting 已提交
330

W
wanghuancoder 已提交
331 332
            def test_return_index():
                result = paddle.unique(x, return_index=0)
Z
Zhang Ting 已提交
333

W
wanghuancoder 已提交
334
            self.assertRaises(TypeError, test_return_index)
Z
Zhang Ting 已提交
335

W
wanghuancoder 已提交
336 337
            def test_return_inverse():
                result = paddle.unique(x, return_inverse='s')
Z
Zhang Ting 已提交
338

W
wanghuancoder 已提交
339
            self.assertRaises(TypeError, test_return_inverse)
Z
Zhang Ting 已提交
340

W
wanghuancoder 已提交
341 342
            def test_return_counts():
                result = paddle.unique(x, return_counts=3)
Z
Zhang Ting 已提交
343

W
wanghuancoder 已提交
344
            self.assertRaises(TypeError, test_return_counts)
Z
Zhang Ting 已提交
345

W
wanghuancoder 已提交
346 347
            def test_axis():
                result = paddle.unique(x, axis='12')
Z
Zhang Ting 已提交
348

W
wanghuancoder 已提交
349 350
            def test_dtype():
                result = paddle.unique(x, dtype='float64')
Z
Zhang Ting 已提交
351

W
wanghuancoder 已提交
352
            self.assertRaises(TypeError, test_axis)
Z
Zhang Ting 已提交
353 354


Z
zhoukunsheng 已提交
355 356
if __name__ == "__main__":
    unittest.main()