test_lod_reset_op.py 5.7 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
import paddle.fluid as fluid
20
from op_test import OpTest
21
from paddle.fluid import Program, program_guard
22 23 24 25 26


class TestLodResetOpByAttr(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
27
        x = np.random.random((10, 20)).astype("float64")
28 29 30 31 32
        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]
33
        self.inputs = {'X': (x, lod)}
34 35 36
        # The `target_lod` attribute is still based on offset
        self.attrs = {'target_lod': target_offset_lod}
        self.outputs = {'Out': (x, [target_lod])}
37 38

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

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


class TestLodResetOpByInput(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
50
        x = np.random.random((10, 20)).astype("float64")
51 52 53 54 55
        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]
56 57
        self.inputs = {
            'X': (x, lod),
58
            'Y': np.array([target_offset_lod]).astype('int32')
59
        }
60
        self.outputs = {'Out': (x, [target_lod])}
61 62

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

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


class TestLodResetOpBoth(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
74
        x = np.random.random((10, 20)).astype("float64")
75 76 77 78
        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]
79 80
        self.inputs = {
            'X': (x, lod),
81
            'Y': np.array(target_offset_lod_in).astype('int32')
82
        }
83 84
        self.attrs = {'target_lod': target_offset_lod_attr}
        self.outputs = {'Out': (x, [target_lod_in])}
85 86

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

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


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

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

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


114 115 116
class TestLodAppendOpByAttr(OpTest):
    def setUp(self):
        self.op_type = "lod_reset"
117
        x = np.random.random((10, 20)).astype("float64")
118 119 120 121 122 123 124 125 126 127 128
        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):
129 130
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_output(check_dygraph=False)
131 132

    def test_check_grad(self):
133 134
        # TODO(wangzhongpu): support lod in dygraph mode
        self.check_grad(["X"], "Out", check_dygraph=False)
135 136


137 138 139
class TestLodResetOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
140 141 142 143 144 145 146 147 148
            # The input must be Variable.
            x1 = np.array([0.9383, 0.1983, 3.2, 1.2]).astype("float64")
            target_lod = [2, 2]
            self.assertRaises(TypeError, fluid.layers.lod_reset, x1, target_lod)

            # Input(x) dtype must be float32 or float64 or int32 or int64
            for dtype in ["bool", "float16"]:
                x2 = fluid.layers.data(
                    name='x2' + dtype, shape=[4], dtype=dtype)
149
                y2 = fluid.layers.data(
150 151
                    name='y2' + dtype, shape=[4], dtype='int32', lod_level=2)
                self.assertRaises(TypeError, fluid.layers.lod_reset, x2, y2)
152 153


154 155
if __name__ == '__main__':
    unittest.main()