test_unflatten.py 9.8 KB
Newer Older
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
# Copyright (c) 2023 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

import paddle


def numpy_unflatten(x, axis, shape):
    if isinstance(shape, (list, tuple)):
        if len(shape) == 0:
            raise ValueError("The input for shape cannot be empty.")
        if isinstance(shape, list) or isinstance(shape, tuple):
            if np.min(shape) < -1:
                raise ValueError(f"invalid shape dimension {np.min(shape)}.")
            if shape.count(-1) > 1:
                raise ValueError("The shape can contain only one -1.")
            elif shape.count(-1) == 1:
                list(shape)[shape.index(-1)] = x.shape[axis] / abs(
                    np.prod(shape)
                )
            else:
                sizes = np.prod(shape)
                if sizes != x.shape[axis]:
                    raise ValueError(
                        "The product of the elements in shape{} is not equal to {}.".format(
                            shape, x.shape[axis]
                        )
                    )
    else:
        raise TypeError(
            "The data type of x should be one of ['List', 'Tuple', 'Tensor'], but got {}".format(
                type(shape)
            )
        )
    length = len(x.shape)
    if axis < 0:
        axis = axis + length
    new_shape = x.shape[:axis] + tuple(shape) + x.shape[axis + 1 :]
    x = x.reshape(new_shape)
    return x


class TestUnflattenAPI(unittest.TestCase):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16)
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False

    def get_output(self):
        self.output = self.ref_api(self.x, self.axis, self.shape)

    def set_api(self):
        self.ref_api = numpy_unflatten
        self.paddle_api = paddle.unflatten

    def setUp(self):
        self.set_api()
        self.set_args()
        self.get_output()
        self.places = [paddle.CPUPlace()]
        if paddle.device.is_compiled_with_cuda():
            self.places.append(paddle.CUDAPlace(0))

    def func_dygraph(self):
        for place in self.places:
            paddle.disable_static()
            x = paddle.to_tensor(self.x, place=place)
            if self.shape_is_tensor:
                shape = paddle.to_tensor(self.shape)
            else:
                shape = self.shape
            out = self.paddle_api(x=x, axis=self.axis, shape=shape)
            np.testing.assert_allclose(out, self.output, rtol=1e-05)

    def test_dygraph(self):
        self.setUp()
        self.func_dygraph()

    def test_static(self):
        paddle.enable_static()
        places = [paddle.CPUPlace()]
        if paddle.device.is_compiled_with_cuda():
            places.append(paddle.CUDAPlace(0))
        for place in places:
            with paddle.static.program_guard(
                paddle.static.Program(), paddle.static.Program()
            ):
                x = paddle.static.data(
                    name="x", shape=self.x.shape, dtype=self.x.dtype
                )
                if self.shape_is_tensor:
                    shape = np.array(self.shape)
                    shape = paddle.static.data(
                        name='shape', shape=shape.shape, dtype=shape.dtype
                    )
                else:
                    shape = self.shape
                exe = paddle.static.Executor(place)
                out = self.paddle_api(x=x, axis=self.axis, shape=shape)
                fetches = exe.run(
                    paddle.static.default_main_program(),
                    feed={
                        "x": self.x,
                        "axis": self.axis,
                        "shape": self.shape,
                    },
                    fetch_list=[out],
                )

                np.testing.assert_allclose(fetches[0], self.output, rtol=1e-05)


# check the data type of the input x
class TestUnflattenInputInt16(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('int16')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


class TestUnflattenInputInt32(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('int32')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


class TestUnflattenInputInt64(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('int64')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


class TestUnflattenInputFloat16(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float16')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


class TestUnflattenInputFloat32(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


class TestUnflattenInputFloat64(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float64')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


class TestUnflattenInputbool(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('bool')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


# check the data type and edge cases of shape
class TestUnflattenShapeList1(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 0
        self.shape = [2, 2]
        self.shape_is_tensor = False


class TestUnflattenShapeList2(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = -1
        self.shape = [-1, 2]
        self.shape_is_tensor = False


class TestUnflattenShapeList3(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 0
        self.shape = [-1]
        self.shape_is_tensor = False


class TestUnflattenTupleShape1(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 0
        self.shape = (2, 2)
        self.shape_is_tensor = False


class TestUnflattenTupleShape2(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 0
        self.shape = (-1, 2)
        self.shape_is_tensor = False


class TestUnflattenTupleShape3(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 0
        self.shape = (-1,)
        self.shape_is_tensor = False


class TestUnflattenShapeTensorInt32(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 0
        self.shape = tuple(np.array((-1, 4)).astype('int32'))
        self.shape_is_tensor = True


# check the value of axis
class TestUnflattenAxis1(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = 1
        self.shape = (2, 3)
        self.shape_is_tensor = False


class TestUnflattenAxis2(TestUnflattenAPI):
    def set_args(self):
        self.x = np.random.rand(4, 6, 16).astype('float32')
        self.axis = -1
        self.shape = (2, 8)
        self.shape_is_tensor = False


class TestLayer(unittest.TestCase):
    def set_args(self):
        self.x = np.random.randn(3, 4, 4, 5).astype('float32')
        self.axis = 1
        self.shape = [2, 2]

    def setUp(self):
        self.set_args()
        self.places = [paddle.CPUPlace()]
        if paddle.device.is_compiled_with_cuda():
            self.places.append(paddle.CUDAPlace(0))

    def test_layer(self):
        paddle.enable_static()
        places = [paddle.CPUPlace()]
        if paddle.device.is_compiled_with_cuda():
            places.append(paddle.CUDAPlace(0))
        for place in places:
            with paddle.static.program_guard(
                paddle.static.Program(), paddle.static.Program()
            ):
                x = paddle.static.data(
                    name="x", dtype=self.x.dtype, shape=self.x.shape
                )
                exe = paddle.static.Executor(place)
                unflatten = paddle.nn.Unflatten(self.axis, self.shape)
                out = unflatten(x)
                static_ret = exe.run(
                    paddle.static.default_main_program(),
                    feed={"x": self.x, "axis": self.axis, "shape": self.shape},
                    fetch_list=[out],
                )[0]
        for place in self.places:
            paddle.disable_static()
            x = paddle.to_tensor(self.x, dtype='float32', place=place)
            unflatten = paddle.nn.Unflatten(self.axis, self.shape)
            dy_ret_value = unflatten(self.x)
        np.testing.assert_array_equal(static_ret, dy_ret_value)


class TestLayerName(unittest.TestCase):
    def test_name(self):
        self.x = np.random.randn(3, 4, 4, 5).astype('float32')
        self.axis = 1
        self.shape = [2, 2]
        self.name = 'unflatten'
        unflatten = paddle.nn.Unflatten(self.axis, self.shape, self.name)
        _name = unflatten.extra_repr()


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