test_unique.py 12.0 KB
Newer Older
Z
zhoukunsheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#   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.

from __future__ import print_function

import unittest
import numpy as np
from op_test import OpTest
Z
Zhang Ting 已提交
20
import paddle
21
import paddle.fluid as fluid
Z
zhoukunsheng 已提交
22 23
import paddle.fluid.core as core
from paddle.fluid.op import Operator
24
from paddle.fluid.framework import _test_eager_guard
Z
zhoukunsheng 已提交
25 26 27


class TestUniqueOp(OpTest):
28

Z
zhoukunsheng 已提交
29 30 31 32 33 34 35 36
    def setUp(self):
        self.op_type = "unique"
        self.init_config()

    def test_check_output(self):
        self.check_output()

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


class TestOne(TestUniqueOp):
48

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


class TestRandom(TestUniqueOp):
61

Z
zhoukunsheng 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    def init_config(self):
        self.inputs = {'X': np.random.randint(0, 100, (150, ), dtype='int64')}
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT64)}
        np_unique, np_index, reverse_index = np.unique(self.inputs['X'], True,
                                                       True)
        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(
            [list(target_out).index(i) for i in self.inputs['X']],
            dtype='int64')

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


77
class TestUniqueRaiseError(unittest.TestCase):
78

79
    def test_errors(self):
80

81 82 83 84 85 86 87 88 89 90 91 92
        def test_type():
            fluid.layers.unique([10])

        self.assertRaises(TypeError, test_type)

        def test_dtype():
            data = fluid.data(shape=[10], dtype="float16", name="input")
            fluid.layers.unique(data)

        self.assertRaises(TypeError, test_dtype)


93 94 95
@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestOneGPU(TestUniqueOp):
96

97
    def init_config(self):
98 99 100
        self.inputs = {
            'X': np.array([2], dtype='int64'),
        }
101 102
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT32)}
        self.outputs = {
103 104
            'Out': np.array([2], dtype='int64'),
            'Index': np.array([0], dtype='int32')
105 106 107 108 109 110 111 112 113 114 115
        }

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)


@unittest.skipIf(not core.is_compiled_with_cuda(),
                 "core is not compiled with CUDA")
class TestRandomGPU(TestUniqueOp):
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    def init_config(self):
        self.inputs = {'X': np.random.randint(0, 100, (150, ), dtype='int64')}
        self.attrs = {'dtype': int(core.VarDesc.VarType.INT64)}
        np_unique, np_index, reverse_index = np.unique(self.inputs['X'], True,
                                                       True)
        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(
            [list(target_out).index(i) for i in self.inputs['X']],
            dtype='int64')

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

    def test_check_output(self):
        if core.is_compiled_with_cuda():
            place = core.CUDAPlace(0)
            self.check_output_with_place(place, atol=1e-5)


Z
Zhang Ting 已提交
137
class TestSortedUniqueOp(TestUniqueOp):
138

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


class TestUniqueOpAxisNone(TestUniqueOp):
163

Z
Zhang Ting 已提交
164 165
    def init_config(self):
        self.inputs = {'X': np.random.random((4, 7, 10)).astype('float64')}
166 167 168 169 170
        unique, indices, inverse, counts = np.unique(self.inputs['X'],
                                                     return_index=True,
                                                     return_inverse=True,
                                                     return_counts=True,
                                                     axis=None)
Z
Zhang Ting 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        self.attrs = {
            'dtype': int(core.VarDesc.VarType.INT32),
            "return_index": True,
            "return_inverse": True,
            "return_counts": True,
            "axis": None,
            "is_sorted": True
        }
        self.outputs = {
            'Out': unique,
            'Indices': indices,
            "Index": inverse,
            "Counts": counts,
        }


class TestUniqueOpAxis1(TestUniqueOp):
188

Z
Zhang Ting 已提交
189 190
    def init_config(self):
        self.inputs = {'X': np.random.random((3, 8, 8)).astype('float64')}
191 192 193 194 195
        unique, indices, inverse, counts = np.unique(self.inputs['X'],
                                                     return_index=True,
                                                     return_inverse=True,
                                                     return_counts=True,
                                                     axis=1)
Z
Zhang Ting 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        self.attrs = {
            'dtype': int(core.VarDesc.VarType.INT32),
            "return_index": True,
            "return_inverse": True,
            "return_counts": True,
            "axis": [1],
            "is_sorted": True
        }
        self.outputs = {
            'Out': unique,
            'Indices': indices,
            "Index": inverse,
            "Counts": counts,
        }


class TestUniqueAPI(unittest.TestCase):
213

Z
Zhang Ting 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
    def test_dygraph_api_out(self):
        paddle.disable_static()
        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)
        paddle.enable_static()

    def test_dygraph_api_attr(self):
        paddle.disable_static()
        x_data = np.random.random((3, 5, 5)).astype("float32")
        x = paddle.to_tensor(x_data)
227 228 229 230 231 232 233 234 235 236
        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 已提交
237 238 239 240 241 242
        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)
        paddle.enable_static()

Z
Zhang Ting 已提交
243 244 245 246
    def test_dygraph_attr_dtype(self):
        paddle.disable_static()
        x_data = x_data = np.random.randint(0, 10, (120))
        x = paddle.to_tensor(x_data)
247 248 249 250 251
        out, indices, inverse, counts = paddle.unique(x,
                                                      return_index=True,
                                                      return_inverse=True,
                                                      return_counts=True,
                                                      dtype="int32")
Z
Zhang Ting 已提交
252 253 254 255 256 257 258 259
        expected_out, np_indices, np_inverse, np_counts = np.unique(
            x_data, return_index=True, return_inverse=True, return_counts=True)
        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)
        paddle.enable_static()

260 261 262 263 264 265
    def test_dygraph_final_state_api(self):
        with _test_eager_guard():
            self.test_dygraph_api_out()
            self.test_dygraph_api_attr()
            self.test_dygraph_attr_dtype()

Z
Zhang Ting 已提交
266 267 268
    def test_static_graph(self):
        with paddle.static.program_guard(paddle.static.Program(),
                                         paddle.static.Program()):
269
            x = paddle.fluid.data(name='x', shape=[3, 2], dtype='float64')
270 271 272 273
            unique, inverse, counts = paddle.unique(x,
                                                    return_inverse=True,
                                                    return_counts=True,
                                                    axis=0)
Z
Zhang Ting 已提交
274 275 276 277 278
            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])
279 280 281 282
        np_unique, np_inverse, np_counts = np.unique(x_np,
                                                     return_inverse=True,
                                                     return_counts=True,
                                                     axis=0)
Z
Zhang Ting 已提交
283 284 285 286 287 288
        self.assertTrue(np.allclose(result[0], np_unique))
        self.assertTrue(np.allclose(result[1], np_inverse))
        self.assertTrue(np.allclose(result[2], np_counts))


class TestUniqueError(unittest.TestCase):
289

Z
Zhang Ting 已提交
290
    def test_input_dtype(self):
291

Z
Zhang Ting 已提交
292 293 294
        def test_x_dtype():
            with paddle.static.program_guard(paddle.static.Program(),
                                             paddle.static.Program()):
295
                x = paddle.fluid.data(name='x', shape=[10, 10], dtype='float16')
Z
Zhang Ting 已提交
296 297 298 299 300
                result = paddle.unique(x)

            self.assertRaises(TypeError, test_x_dtype)

    def test_attr(self):
301
        x = paddle.fluid.data(name='x', shape=[10, 10], dtype='float64')
Z
Zhang Ting 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

        def test_return_index():
            result = paddle.unique(x, return_index=0)

        self.assertRaises(TypeError, test_return_index)

        def test_return_inverse():
            result = paddle.unique(x, return_inverse='s')

        self.assertRaises(TypeError, test_return_inverse)

        def test_return_counts():
            result = paddle.unique(x, return_counts=3)

        self.assertRaises(TypeError, test_return_counts)

        def test_axis():
            result = paddle.unique(x, axis='12')

Z
Zhang Ting 已提交
321 322 323
        def test_dtype():
            result = paddle.unique(x, dtype='float64')

Z
Zhang Ting 已提交
324 325 326
        self.assertRaises(TypeError, test_axis)


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