test_slice_op_xpu.py 6.9 KB
Newer Older
T
Thunderbrook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
import unittest
16

17
import numpy as np
R
RedContritio 已提交
18
from get_test_cover_info import (
19
    XPUOpTestWrapper,
20 21 22
    create_test_class,
    get_xpu_op_support_types,
)
R
RedContritio 已提交
23 24 25
from op_test_xpu import XPUOpTest

import paddle
26 27

paddle.enable_static()
T
Thunderbrook 已提交
28 29 30 31


# Situation 1: starts(list, no tensor), ends(list, no tensor)
# 1.1 without attr(decrease)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
class XPUTestSliceOp(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'slice'
        self.use_dynamic_create_class = False

    class TestSliceOp(XPUOpTest):
        def setUp(self):
            self.dtype = self.in_type
            self.place = paddle.XPUPlace(0)
            self.op_type = "slice"
            self.config()
            self.inputs = {'Input': self.input}
            self.outputs = {'Out': self.out}
            self.attrs = {
                'axes': self.axes,
                'starts': self.starts,
                'ends': self.ends,
                'infer_flags': self.infer_flags,
50
                "use_xpu": True,
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
            }

        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [1, 0, 2]
            self.ends = [3, 3, 4]
            self.axes = [0, 1, 2]
            self.infer_flags = [1, 1, 1]
            self.out = self.input[1:3, 0:3, 2:4, :]

        def test_check_grad_normal(self):
            if self.dtype == np.float16:
                self.check_grad_with_place(self.place, ['Input'], 'Out')
            else:
                user_defined_grad_outputs = np.random.random(
66 67
                    self.out.shape
                ).astype(self.dtype)
68
                self.check_grad_with_place(
69 70
                    self.place,
                    ['Input'],
71
                    'Out',
72 73
                    user_defined_grad_outputs=user_defined_grad_outputs,
                )
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    class TestCase1(TestSliceOp):
        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [-3, 0, 2]
            self.ends = [3, 100, -1]
            self.axes = [0, 1, 2]
            self.infer_flags = [1, 1, 1]
            self.out = self.input[-3:3, 0:100, 2:-1, :]

    class TestCase2(TestSliceOp):
        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [-3, 0, 2]
            self.ends = [3, 100, -1]
            self.axes = [0, 1, 3]
            self.infer_flags = [1, 1, 1]
            self.out = self.input[-3:3, 0:100, :, 2:-1]
T
Thunderbrook 已提交
92 93 94


# 1.2 with attr(decrease)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
class XPUTestSliceOp_decs_dim(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'slice'
        self.use_dynamic_create_class = False

    class TestSliceOp_decs_dim(XPUOpTest):
        def setUp(self):
            self.dtype = self.in_type
            self.place = paddle.XPUPlace(0)
            self.op_type = "slice"
            self.config()
            self.inputs = {'Input': self.input}
            self.outputs = {'Out': self.out}
            self.attrs = {
                'axes': self.axes,
                'starts': self.starts,
                'ends': self.ends,
                'infer_flags': self.infer_flags,
                'decrease_axis': self.decrease_axis,
114
                "use_xpu": True,
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            }

        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [1, 0, 2]
            self.ends = [2, 3, 4]
            self.axes = [0, 1, 2]
            self.decrease_axis = [0]
            self.infer_flags = [1, 1, 1]
            self.out = self.input[1, 0:3, 2:4, :]

        def test_check_output(self):
            self.check_output_with_place(self.place)

        def test_check_grad_normal(self):
            if self.dtype == np.float16:
                self.check_grad_with_place(self.place, ['Input'], 'Out')
            else:
                user_defined_grad_outputs = np.random.random(
134 135
                    self.out.shape
                ).astype(self.dtype)
136
                self.check_grad_with_place(
137 138
                    self.place,
                    ['Input'],
139
                    'Out',
140 141
                    user_defined_grad_outputs=user_defined_grad_outputs,
                )
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

    class TestSliceOp_decs_dim_2(TestSliceOp_decs_dim):
        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [1, 0, 2]
            self.ends = [2, 1, 4]
            self.axes = [0, 1, 2]
            self.decrease_axis = [0, 1]
            self.infer_flags = [1, 1, 1]
            self.out = self.input[1, 0, 2:4, :]

    class TestSliceOp_decs_dim_3(TestSliceOp_decs_dim):
        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [-1, 0, 2]
            self.ends = [1000000, 1, 4]
            self.axes = [0, 1, 2]
            self.decrease_axis = [0, 1]
            self.infer_flags = [1, 1, 1]
            self.out = self.input[-1, 0, 2:4, :]

    class TestSliceOp_decs_dim_4(TestSliceOp_decs_dim):
        def config(self):
            self.input = np.random.random([3, 4, 5, 7]).astype(self.dtype)
            self.starts = [0, 1, 2, 3]
            self.ends = [1, 2, 3, 4]
            self.axes = [0, 1, 2, 3]
169
            self.decrease_axis = [0, 1, 2]
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            self.infer_flags = [1, 1, 1]
            self.out = self.input[0, 1, 2, 3:4]

    class TestSliceOp_decs_dim_5(TestSliceOp_decs_dim):
        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [-1]
            self.ends = [1000000]
            self.axes = [3]
            self.decrease_axis = [3]
            self.infer_flags = [1, 1, 1]
            self.out = self.input[:, :, :, -1]

    class TestSliceOp_decs_dim_6(TestSliceOp_decs_dim):
        def config(self):
            self.input = np.random.random([3, 4, 5, 6]).astype(self.dtype)
            self.starts = [0, 1, 2, 3]
            self.ends = [1, 2, 3, 4]
            self.axes = [0, 1, 2, 3]
            self.decrease_axis = [0, 1, 2, 3]
            self.infer_flags = [1, 1, 1]
191
            self.out = self.input[0, 1, 2, 3]
192 193 194 195 196 197


support_types = get_xpu_op_support_types('slice')
for stype in support_types:
    create_test_class(globals(), XPUTestSliceOp, stype)
    create_test_class(globals(), XPUTestSliceOp_decs_dim, stype)
T
Thunderbrook 已提交
198 199 200

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