test_cumsum_op.py 3.9 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
E
emailweixu 已提交
20 21 22 23 24 25 26 27 28 29


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):
H
hong 已提交
30
        self.check_output(check_dygraph=False)
E
emailweixu 已提交
31 32

    def test_check_grad(self):
H
hong 已提交
33
        self.check_grad(['X'], 'Out', check_dygraph=False)
E
emailweixu 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47


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):
H
hong 已提交
48
        self.check_output(check_dygraph=False)
E
emailweixu 已提交
49 50

    def test_check_grad(self):
H
hong 已提交
51
        self.check_grad(['X'], 'Out', check_dygraph=False)
E
emailweixu 已提交
52 53 54 55 56 57 58 59 60 61


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):
H
hong 已提交
62
        self.check_output(check_dygraph=False)
E
emailweixu 已提交
63 64

    def test_check_grad(self):
H
hong 已提交
65
        self.check_grad(['X'], 'Out', check_dygraph=False)
E
emailweixu 已提交
66 67 68 69 70 71 72 73 74 75


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):
H
hong 已提交
76
        self.check_output(check_dygraph=False)
E
emailweixu 已提交
77 78

    def test_check_grad(self):
H
hong 已提交
79
        self.check_grad(['X'], 'Out', check_dygraph=False)
E
emailweixu 已提交
80 81 82 83 84 85 86 87 88


class TestSumOp5(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.inputs = {'X': np.random.random((5, 6)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=1)}

    def test_check_output(self):
H
hong 已提交
89
        self.check_output(check_dygraph=False)
E
emailweixu 已提交
90 91

    def test_check_grad(self):
H
hong 已提交
92
        self.check_grad(['X'], 'Out', check_dygraph=False)
E
emailweixu 已提交
93 94 95 96 97 98 99 100 101


class TestSumOp7(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.inputs = {'X': np.random.random((6)).astype("float64")}
        self.outputs = {'Out': self.inputs['X'].cumsum(axis=0)}

    def test_check_output(self):
H
hong 已提交
102
        self.check_output(check_dygraph=False)
E
emailweixu 已提交
103 104

    def test_check_grad(self):
H
hong 已提交
105
        self.check_grad(['X'], 'Out', check_dygraph=False)
E
emailweixu 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121


class TestSumOp8(OpTest):
    def setUp(self):
        self.op_type = "cumsum"
        self.attrs = {'axis': 2, "exclusive": True}
        a = np.random.random((5, 6, 3)).astype("float64")
        self.inputs = {'X': a}
        self.outputs = {
            'Out': np.concatenate(
                (np.zeros(
                    (5, 6, 1), dtype=np.float64), a[:, :, :-1].cumsum(axis=2)),
                axis=2)
        }

    def test_check_output(self):
H
hong 已提交
122
        self.check_output(check_dygraph=False)
E
emailweixu 已提交
123 124

    def test_check_grad(self):
H
hong 已提交
125
        self.check_grad(['X'], 'Out', check_dygraph=False)
E
emailweixu 已提交
126 127 128 129


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