test_lod_append_op.py 2.7 KB
Newer Older
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# 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.fluid as fluid
18
from paddle.fluid import Program
19 20 21 22 23 24 25


class TestLoDAppendAPI(unittest.TestCase):
    def test_api(self, use_cuda=False):
        main_program = Program()
        with fluid.program_guard(main_program):
            x = fluid.layers.data(name='x', shape=[6], dtype='float32')
26 27 28
            level = fluid.layers.data(
                name='level', shape=[3], dtype='int32', lod_level=0
            )
29
            result = fluid.layers.lod_append(x, level)
30 31

            x_i = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0]).astype("float32")
32
            level_i = np.array([0, 2, 6]).astype("int32")
33 34 35 36 37 38

            for use_cuda in [False, True]:
                if use_cuda and not fluid.core.is_compiled_with_cuda():
                    return
                place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
                exe = fluid.Executor(place)
39 40 41 42 43 44
                [out] = exe.run(
                    fluid.default_main_program(),
                    feed={'x': x_i, 'level': level_i},
                    fetch_list=[result],
                    return_numpy=False,
                )
45 46 47 48
                self.assertEqual(out.recursive_sequence_lengths(), [[2, 4]])


class TestLodAppendOpError(unittest.TestCase):
49 50 51 52 53
    def test_error(self):
        # The input(x) must be Variable.
        x1 = np.array([0.9383, 0.1983, 3.2, 1.2]).astype("float64")
        level1 = [0, 2, 4]
        self.assertRaises(TypeError, fluid.layers.lod_append, x1, level1)
54

55
        # The input(level) must be Variable or list.
56 57
        x2 = fluid.layers.data(name='x2', shape=[4], dtype='float32')
        self.assertRaises(ValueError, fluid.layers.lod_append, x2, 2)
58

59 60 61
        # Input(x) dtype must be float32 or float64 or int32 or int64
        for dtype in ["bool", "float16"]:
            x3 = fluid.layers.data(name='x3_' + dtype, shape=[4], dtype=dtype)
62 63 64
            level3 = fluid.layers.data(
                name='level3' + dtype, shape=[4], dtype='int32', lod_level=2
            )
65
            self.assertRaises(TypeError, fluid.layers.lod_append, x3, level3)
66 67 68 69


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