test_lod_reset_op.py 6.1 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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

17 18
import unittest
import numpy as np
19
from op_test import OpTest
20
from paddle.fluid import Program, program_guard
21 22 23 24 25


class TestLodResetOpByAttr(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
26
        x = np.random.random((10, 20)).astype("float64")
27 28 29 30 31
        lod = [[3, 2, 5]]
        # target_offset_lod and target_lod are the same lod info represented
        # in offset-based format and length-based format, respectively.
        target_offset_lod = [0, 7, 10]
        target_lod = [7, 3]
32
        self.inputs = {'X': (x, lod)}
33 34 35
        # The `target_lod` attribute is still based on offset
        self.attrs = {'target_lod': target_offset_lod}
        self.outputs = {'Out': (x, [target_lod])}
36 37

    def test_check_output(self):
38 39
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_output(check_dygraph=False)
40 41

    def test_check_grad(self):
42 43
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_grad(["X"], "Out", check_dygraph=False)
44 45 46 47 48


class TestLodResetOpByInput(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
49
        x = np.random.random((10, 20)).astype("float64")
50 51 52 53 54
        lod = [[3, 2, 5]]
        # target_offset_lod and target_lod are the same lod info represented
        # in offset-based format and length-based format, respectively.
        target_offset_lod = [0, 4, 7, 10]
        target_lod = [4, 3, 3]
55 56
        self.inputs = {
            'X': (x, lod),
57
            'Y': np.array([target_offset_lod]).astype('int32')
58
        }
59
        self.outputs = {'Out': (x, [target_lod])}
60 61

    def test_check_output(self):
62 63
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_output(check_dygraph=False)
64 65

    def test_check_grad(self):
66 67
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_grad(["X"], "Out", no_grad_set=set("Y"), check_dygraph=False)
68 69 70 71 72


class TestLodResetOpBoth(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
73
        x = np.random.random((10, 20)).astype("float64")
74 75 76 77
        lod = [[3, 2, 5]]
        target_offset_lod_attr = [0, 7, 10]
        target_offset_lod_in = [0, 4, 7, 10]
        target_lod_in = [4, 3, 3]
78 79
        self.inputs = {
            'X': (x, lod),
80
            'Y': np.array(target_offset_lod_in).astype('int32')
81
        }
82 83
        self.attrs = {'target_lod': target_offset_lod_attr}
        self.outputs = {'Out': (x, [target_lod_in])}
84 85

    def test_check_output(self):
86 87
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_output(check_dygraph=False)
88 89

    def test_check_grad(self):
90 91
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_grad(["X"], "Out", no_grad_set=set("Y"), check_dygraph=False)
92 93 94 95 96


class TestLodResetOpYIsLoDTensor(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
97
        x = np.random.random((10, 20)).astype("float64")
98
        lod = [[3, 2, 5]]
99
        y = np.random.random((10, 10)).astype("float64")
100 101 102
        target_lod = [[4, 3, 3]]
        self.inputs = {'X': (x, lod), 'Y': (y, target_lod)}
        self.outputs = {'Out': (x, target_lod)}
103 104

    def test_check_output(self):
105 106
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_output(check_dygraph=False)
107 108

    def test_check_grad(self):
109 110
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_grad(["X"], "Out", no_grad_set=set("Y"), check_dygraph=False)
111 112


113 114 115
class TestLodAppendOpByAttr(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
116
        x = np.random.random((10, 20)).astype("float64")
117 118 119 120 121 122 123 124 125 126 127
        lod = [[3, 2, 5]]
        # target_offset_lod and target_lod are the same lod info represented
        # in offset-based format and length-based format, respectively.
        target_offset_lod = [i for i in range(11)]
        self.inputs = {'X': (x, lod)}
        out_lod = [[3, 2, 5], [1] * 10]
        # The `target_lod` attribute is still based on offset
        self.attrs = {'target_lod': target_offset_lod, 'append': True}
        self.outputs = {'Out': (x, out_lod)}

    def test_check_output(self):
128 129
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_output(check_dygraph=False)
130 131

    def test_check_grad(self):
132 133
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_grad(["X"], "Out", check_dygraph=False)
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
class TestLodResetOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):

            def test_Variable():
                # The input must be Variable.
                x1 = fluid.create_lod_tensor(
                    np.ones([6]), [3, 3], fluid.CPUPlace())
                y1 = fluid.create_lod_tensor(
                    np.ones([6]), [2, 2, 2], fluid.CPUPlace())
                self.assertRaises(TypeError, fluid.layers.lod_reset, [x1, y1])

            def test_type():
                # dtype must be float32 or float64 or int32 or int64
                x2 = fluid.layers.data(shape=[4], dtype='uint8', name='x2')
                y2 = fluid.layers.data(
                    shape=[4], dtype='uint8', name='x2', lod_level=2)
                self.assertRaises(TypeError, fluid.layers.lod_reset, [x2, y2])

            def test_type2():
                # dtype must be int32 or int64
                x3 = fluid.layers.data(shape=[4], dtype='float32', name='x3')
                y3 = fluid.layers.data(
                    shape=[4], dtype='float32', name='x3', lod_level=0)
                self.assertRaises(TypeError, fluid.layers.lod_reset, [x3, y3])


163 164
if __name__ == '__main__':
    unittest.main()