test_cumsum_op.py 6.4 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

E
emailweixu 已提交
17 18
import unittest
import numpy as np
19
from op_test import OpTest
20 21 22
import paddle.fluid.core as core
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
E
emailweixu 已提交
23 24 25 26 27 28 29 30 31 32


class TestSumOp1(OpTest):
    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):
33
        self.check_output()
E
emailweixu 已提交
34 35

    def test_check_grad(self):
36
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50


class TestSumOp2(OpTest):
    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 = {
            'Out': np.flip(
                np.flip(
                    self.inputs['X'], axis=2).cumsum(axis=2), axis=2)
        }

    def test_check_output(self):
51
        self.check_output()
E
emailweixu 已提交
52 53

    def test_check_grad(self):
54
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
55 56 57 58 59 60 61 62 63 64


class TestSumOp3(OpTest):
    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):
65
        self.check_output()
E
emailweixu 已提交
66 67

    def test_check_grad(self):
68
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
69 70 71 72 73 74 75 76 77 78


class TestSumOp4(OpTest):
    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):
79
        self.check_output()
E
emailweixu 已提交
80 81

    def test_check_grad(self):
82
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
83 84 85 86 87


class TestSumOp5(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
Z
zhupengyang 已提交
88
        self.inputs = {'X': np.random.random((5, 20)).astype("float64")}
E
emailweixu 已提交
89 90 91
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}

    def test_check_output(self):
92
        self.check_output()
E
emailweixu 已提交
93 94

    def test_check_grad(self):
95
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
96 97 98 99 100


class TestSumOp7(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
Z
zhupengyang 已提交
101
        self.inputs = {'X': np.random.random((100)).astype("float64")}
E
emailweixu 已提交
102 103 104
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}

    def test_check_output(self):
105
        self.check_output()
E
emailweixu 已提交
106 107

    def test_check_grad(self):
108
        self.check_grad(['X'], 'Out')
E
emailweixu 已提交
109 110


111
class TestSumOpExclusive1(OpTest):
E
emailweixu 已提交
112 113 114
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
115
        a = np.random.random((4, 5, 65)).astype("float64")
E
emailweixu 已提交
116 117 118 119
        self.inputs = {'X': a}
        self.outputs = {
            'Out': np.concatenate(
                (np.zeros(
120
                    (4, 5, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
E
emailweixu 已提交
121 122 123 124
                axis=2)
        }

    def test_check_output(self):
125
        self.check_output()
E
emailweixu 已提交
126

127 128 129 130 131 132 133 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

class TestSumOpExclusive2(OpTest):
    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 = {
            'Out': np.concatenate(
                (np.zeros(
                    (1, 1, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                axis=2)
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpExclusive3(OpTest):
    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 = {
            'Out': np.concatenate(
                (np.zeros(
                    (4, 5, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                axis=2)
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpExclusive4(OpTest):
    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 = {
            'Out': np.concatenate(
                (np.zeros(
                    (1, 1, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                axis=2)
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpExclusive5(OpTest):
    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 = {
            'Out': np.concatenate(
                (np.zeros(
                    (4, 5, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                axis=2)
        }

    def test_check_output(self):
        self.check_output()


class TestSumOpReverseExclusive(OpTest):
    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 = {
            'Out': np.concatenate(
                (np.flip(
                    a[:, :, :-1].cumsum(axis=2), axis=2), np.zeros(
                        (4, 5, 1), dtype=np.float64)),
                axis=2)
        }

    def test_check_output(self):
        self.check_output()
E
emailweixu 已提交
213 214


215 216 217 218 219
class BadInputTest(unittest.TestCase):
    def test_error(self):
        with fluid.program_guard(fluid.Program()):

            def test_bad_x():
220
                data = [1, 2, 4]
221 222 223 224 225
                result = fluid.layers.cumsum(data, axis=0)

            self.assertRaises(TypeError, test_bad_x)


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