test_lod_reset_op.py 4.8 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
import unittest
16

17
import numpy as np
18
from op_test import OpTest
19

20 21 22 23

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

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

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


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

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

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


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

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

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


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

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

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


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

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


134 135
if __name__ == '__main__':
    unittest.main()