test_cumsum_op.py 11.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
E
emailweixu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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.

15 16
from __future__ import print_function

W
WangZhen 已提交
17
import os
E
emailweixu 已提交
18
import unittest
W
WangZhen 已提交
19
import tempfile
E
emailweixu 已提交
20
import numpy as np
21
from op_test import OpTest
22
import paddle
23 24 25
import paddle.fluid.core as core
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
W
WangZhen 已提交
26
import paddle.inference as paddle_infer
27 28 29


class TestCumsumOp(unittest.TestCase):
30

31 32
    def run_cases(self):
        data_np = np.arange(12).reshape(3, 4)
Z
Zhou Wei 已提交
33
        data = paddle.to_tensor(data_np)
34 35 36

        y = paddle.cumsum(data)
        z = np.cumsum(data_np)
37
        np.testing.assert_array_equal(z, y.numpy())
38 39 40

        y = paddle.cumsum(data, axis=0)
        z = np.cumsum(data_np, axis=0)
41
        np.testing.assert_array_equal(z, y.numpy())
42 43 44

        y = paddle.cumsum(data, axis=-1)
        z = np.cumsum(data_np, axis=-1)
45
        np.testing.assert_array_equal(z, y.numpy())
46 47 48 49 50 51 52 53 54

        y = paddle.cumsum(data, dtype='float64')
        self.assertTrue(y.dtype == core.VarDesc.VarType.FP64)

        y = paddle.cumsum(data, dtype=np.int32)
        self.assertTrue(y.dtype == core.VarDesc.VarType.INT32)

        y = paddle.cumsum(data, axis=-2)
        z = np.cumsum(data_np, axis=-2)
55
        np.testing.assert_array_equal(z, y.numpy())
56 57 58 59

    def run_static(self, use_gpu=False):
        with fluid.program_guard(fluid.Program()):
            data_np = np.random.random((100, 100)).astype(np.float32)
60
            x = paddle.static.data('X', [100, 100])
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            y = paddle.cumsum(x)
            y2 = paddle.cumsum(x, axis=0)
            y3 = paddle.cumsum(x, axis=-1)
            y4 = paddle.cumsum(x, dtype='float64')
            y5 = paddle.cumsum(x, dtype=np.int32)
            y6 = paddle.cumsum(x, axis=-2)

            place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())
            out = exe.run(feed={'X': data_np},
                          fetch_list=[
                              y.name, y2.name, y3.name, y4.name, y5.name,
                              y6.name
                          ])

            z = np.cumsum(data_np)
78
            np.testing.assert_allclose(z, out[0], rtol=1e-05)
79
            z = np.cumsum(data_np, axis=0)
80
            np.testing.assert_allclose(z, out[1], rtol=1e-05)
81
            z = np.cumsum(data_np, axis=-1)
82
            np.testing.assert_allclose(z, out[2], rtol=1e-05)
83 84 85
            self.assertTrue(out[3].dtype == np.float64)
            self.assertTrue(out[4].dtype == np.int32)
            z = np.cumsum(data_np, axis=-2)
86
            np.testing.assert_allclose(z, out[5], rtol=1e-05)
87 88

    def test_cpu(self):
89 90 91
        paddle.disable_static(paddle.fluid.CPUPlace())
        self.run_cases()
        paddle.enable_static()
92 93 94 95 96 97

        self.run_static()

    def test_gpu(self):
        if not fluid.core.is_compiled_with_cuda():
            return
98 99 100
        paddle.disable_static(paddle.fluid.CUDAPlace(0))
        self.run_cases()
        paddle.enable_static()
101 102 103 104 105

        self.run_static(use_gpu=True)

    def test_name(self):
        with fluid.program_guard(fluid.Program()):
106
            x = paddle.static.data('x', [3, 4])
107 108
            y = paddle.cumsum(x, name='out')
            self.assertTrue('out' in y.name)
E
emailweixu 已提交
109 110 111


class TestSumOp1(OpTest):
112

E
emailweixu 已提交
113 114 115 116 117 118 119
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=2)}

    def test_check_output(self):
120
        self.check_output()
E
emailweixu 已提交
121 122

    def test_check_grad(self):
123
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
124 125 126


class TestSumOp2(OpTest):
127

E
emailweixu 已提交
128 129 130 131 132
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': -1, 'reverse': True}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {
133 134
            'Out': np.flip(np.flip(self.inputs['X'], axis=2).cumsum(axis=2),
                           axis=2)
E
emailweixu 已提交
135 136 137
        }

    def test_check_output(self):
138
        self.check_output()
E
emailweixu 已提交
139 140

    def test_check_grad(self):
141
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
142 143 144


class TestSumOp3(OpTest):
145

E
emailweixu 已提交
146 147 148 149 150 151 152
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 1}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}

    def test_check_output(self):
153
        self.check_output()
E
emailweixu 已提交
154 155

    def test_check_grad(self):
156
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
157 158 159


class TestSumOp4(OpTest):
160

E
emailweixu 已提交
161 162 163 164 165 166 167
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 0}
        self.inputs = {'X': np.random.random((5, 6, 10)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}

    def test_check_output(self):
168
        self.check_output()
E
emailweixu 已提交
169 170

    def test_check_grad(self):
171
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
172 173 174


class TestSumOp5(OpTest):
175

E
emailweixu 已提交
176 177
    def setUp(self):
        self.op_type = "cumsum"
Z
zhupengyang 已提交
178
        self.inputs = {'X': np.random.random((5, 20)).astype("float64")}
E
emailweixu 已提交
179 180 181
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}

    def test_check_output(self):
182
        self.check_output()
E
emailweixu 已提交
183 184

    def test_check_grad(self):
185
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
186 187 188


class TestSumOp7(OpTest):
189

E
emailweixu 已提交
190 191
    def setUp(self):
        self.op_type = "cumsum"
Z
zhupengyang 已提交
192
        self.inputs = {'X': np.random.random((100)).astype("float64")}
E
emailweixu 已提交
193 194 195
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}

    def test_check_output(self):
196
        self.check_output()
E
emailweixu 已提交
197 198

    def test_check_grad(self):
199
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
200 201


202
class TestSumOpExclusive1(OpTest):
203

E
emailweixu 已提交
204 205 206
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
207
        a = np.random.random((4, 5, 65)).astype("float64")
E
emailweixu 已提交
208 209
        self.inputs = {'X': a}
        self.outputs = {
210 211 212 213
            'Out':
            np.concatenate((np.zeros(
                (4, 5, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                           axis=2)
E
emailweixu 已提交
214 215 216
        }

    def test_check_output(self):
217
        self.check_output()
E
emailweixu 已提交
218

219 220

class TestSumOpExclusive2(OpTest):
221

222 223 224 225 226 227
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
        a = np.random.random((1, 1, 888)).astype("float64")
        self.inputs = {'X': a}
        self.outputs = {
228 229 230 231
            'Out':
            np.concatenate((np.zeros(
                (1, 1, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                           axis=2)
232 233 234 235 236 237 238
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpExclusive3(OpTest):
239

240 241 242 243 244 245
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
        a = np.random.random((4, 5, 888)).astype("float32")
        self.inputs = {'X': a}
        self.outputs = {
246 247 248 249
            'Out':
            np.concatenate((np.zeros(
                (4, 5, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                           axis=2)
250 251 252 253 254 255 256
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpExclusive4(OpTest):
257

258 259 260 261 262 263
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
        a = np.random.random((1, 1, 3049)).astype("float64")
        self.inputs = {'X': a}
        self.outputs = {
264 265 266 267
            'Out':
            np.concatenate((np.zeros(
                (1, 1, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                           axis=2)
268 269 270 271 272 273 274
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpExclusive5(OpTest):
275

276 277 278 279 280 281
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
        a = np.random.random((4, 5, 3096)).astype("float64")
        self.inputs = {'X': a}
        self.outputs = {
282 283 284 285
            'Out':
            np.concatenate((np.zeros(
                (4, 5, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                           axis=2)
286 287 288 289 290 291 292
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpReverseExclusive(OpTest):
293

294 295 296 297 298 299 300
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, 'reverse': True, "exclusive": True}
        a = np.random.random((4, 5, 6)).astype("float64")
        self.inputs = {'X': a}
        a = np.flip(a, axis=2)
        self.outputs = {
301 302 303 304
            'Out':
            np.concatenate(
                (np.flip(a[:, :, :-1].cumsum(axis=2),
                         axis=2), np.zeros((4, 5, 1), dtype=np.float64)),
305 306 307 308 309
                axis=2)
        }

    def test_check_output(self):
        self.check_output()
E
emailweixu 已提交
310 311


312
class BadInputTest(unittest.TestCase):
313

314 315 316 317
    def test_error(self):
        with fluid.program_guard(fluid.Program()):

            def test_bad_x():
318
                data = [1, 2, 4]
319 320 321 322 323
                result = fluid.layers.cumsum(data, axis=0)

            self.assertRaises(TypeError, test_bad_x)


W
WangZhen 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
class TestTensorAxis(unittest.TestCase):

    def setUp(self):
        paddle.seed(2022)
        self.temp_dir = tempfile.TemporaryDirectory()
        self.save_path = os.path.join(self.temp_dir.name, 'tensor_axis_cumsum')
        self.place = paddle.CUDAPlace(
            0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace()

    def test_dygraph(self):
        paddle.disable_static()
        x = np.random.randn(5, 6)
        axis = 1
        np_out = np.cumsum(x, axis)
        pd_out = paddle.cumsum(paddle.to_tensor(x),
                               axis=paddle.to_tensor([axis], dtype='int32'))
        np.testing.assert_allclose(np_out, pd_out.numpy())

    def test_static_and_infer(self):
        paddle.enable_static()
        np_x = np.random.randn(9, 10, 11).astype('float32')
        main_prog = paddle.static.Program()
        starup_prog = paddle.static.Program()
        with paddle.static.program_guard(main_prog, starup_prog):
            # run static
            x = paddle.static.data(shape=np_x.shape, name='x', dtype=np_x.dtype)
            print(x)
            linear = paddle.nn.Linear(np_x.shape[-1], np_x.shape[-1])
            linear_out = linear(x)
            relu_out = paddle.nn.functional.relu(linear_out)
            axis = paddle.full([1], 2, dtype='int64')
            out = paddle.cumsum(relu_out, axis=axis)

            exe = paddle.static.Executor(self.place)
            exe.run(starup_prog)
            static_out = exe.run(feed={'x': np_x}, fetch_list=[out])

            # run infer
            paddle.static.save_inference_model(self.save_path, [x], [out], exe)
            config = paddle_infer.Config(self.save_path + '.pdmodel',
                                         self.save_path + '.pdiparams')
            if paddle.is_compiled_with_cuda():
                config.enable_use_gpu(100, 0)
            else:
                config.disable_gpu()

            predictor = paddle_infer.create_predictor(config)
            input_names = predictor.get_input_names()
            input_handle = predictor.get_input_handle(input_names[0])
            fake_input = np_x
            input_handle.reshape(np_x.shape)
            input_handle.copy_from_cpu(fake_input)
            predictor.run()
            output_names = predictor.get_output_names()
            output_handle = predictor.get_output_handle(output_names[0])
            infer_out = output_handle.copy_to_cpu()
            np.testing.assert_allclose(static_out[0], infer_out)


E
emailweixu 已提交
383 384
if __name__ == '__main__':
    unittest.main()