test_reverse_op.py 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/env python3

# Copyright (c) 2023 CINN 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.

from cinn.common import *
from cinn.frontend import *
from op_test import OpTest, OpTestTool
from op_test_helper import TestCaseHelper

22 23
import paddle

24

25 26 27
@OpTestTool.skip_if(
    not is_compiled_with_cuda(), "x86 test will be skipped due to timeout."
)
28 29 30 31 32 33 34 35 36 37 38 39 40 41
class TestReverseOp(OpTest):
    def setUp(self):
        print(f"\nRunning {self.__class__.__name__}: {self.case}")
        self.inputs = {}
        self.prepare_inputs()

    def prepare_inputs(self):
        dims = len(self.case["shape"])
        axes = self.case["axes"].copy()
        for i in range(len(axes)):
            axes[i] = min(axes[i], dims - 1)
            axes[i] = max(axes[i], -dims)
        self.inputs = {
            "x": self.random(self.case["shape"], self.case["dtype"]),
42
            "axes": axes,
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        }
        self.net_builder_api = self.case["net_builder_api"]

    def build_paddle_program(self, target):
        x = paddle.to_tensor(self.inputs["x"], stop_gradient=True)
        if self.net_builder_api == "reverse":
            out = paddle.reverse(x, self.inputs["axes"])
        elif self.net_builder_api == "flip":
            out = paddle.flip(x, self.inputs["axes"])
        else:
            raise NotImplementedError
        self.paddle_outputs = [out]

    def build_cinn_program(self, target):
        builder = NetBuilder("reverse")
        x = builder.create_input(
            self.nptype2cinntype(self.inputs["x"].dtype),
60 61 62
            self.inputs["x"].shape,
            "x",
        )
63 64 65 66 67 68 69 70
        if self.net_builder_api == "reverse":
            out = builder.reverse(x, self.inputs["axes"])
        elif self.net_builder_api == "flip":
            out = builder.flip(x, self.inputs["axes"])
        else:
            raise NotImplementedError

        prog = builder.build()
71
        res = self.get_cinn_output(prog, target, [x], [self.inputs["x"]], [out])
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

        self.cinn_outputs = res

    def test_check_results(self):
        self.check_outputs_and_grads(all_equal=True)


class TestReverseOpShape(TestCaseHelper):
    def init_attrs(self):
        self.class_name = "TestReverseOpShape"
        self.cls = TestReverseOp
        self.inputs = [
            {
                "shape": [10],
            },
            {
                "shape": [8, 5],
            },
            {
                "shape": [10, 3, 5],
            },
            {
                "shape": [80, 40, 5, 7],
            },
            {
                "shape": [80, 1, 5, 7],
            },
            {
                "shape": [80, 3, 1024, 7],
            },
            {
                "shape": [10, 5, 1024, 2048],
            },
            {
                "shape": [1],
            },
            {
                "shape": [512],
            },
            {
                "shape": [1024],
            },
            {
                "shape": [2048],
            },
            {
                "shape": [65536],
            },
            {
                "shape": [131072],
            },
            {
                "shape": [1, 1, 1, 1],
            },
        ]
        self.dtypes = [
128
            {"dtype": "float32"},
129 130
        ]
        self.attrs = [
131
            {"axes": [0]},
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
        ]
        net_builder_api_attrs = [
            {
                "net_builder_api": "reverse",
            },
            {
                "net_builder_api": "flip",
            },
        ]
        self._register_custom_attrs(net_builder_api_attrs)


class TestReverseOpDtype(TestCaseHelper):
    def init_attrs(self):
        self.class_name = "TestReverseOpDtype"
        self.cls = TestReverseOp
        self.inputs = [
            {
                "shape": [1],
            },
            {
                "shape": [5, 10],
            },
            {
                "shape": [80, 40, 5, 7],
            },
        ]
        self.dtypes = [
160 161 162 163 164 165
            {"dtype": "bool"},
            {"dtype": "int32"},
            {"dtype": "int64"},
            {"dtype": "float16"},
            {"dtype": "float32"},
            {"dtype": "float64"},
166 167
        ]
        self.attrs = [
168
            {"axes": [0]},
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
        ]
        net_builder_api_attrs = [
            {
                "net_builder_api": "reverse",
            },
            {
                "net_builder_api": "flip",
            },
        ]
        self._register_custom_attrs(net_builder_api_attrs)


class TestReverseOpAxis(TestCaseHelper):
    def init_attrs(self):
        self.class_name = "TestReverseOpAxis"
        self.cls = TestReverseOp
        self.inputs = [
            {
                "shape": [8, 4, 2, 16],
            },
            {
                "shape": [1, 1, 1, 1],
            },
        ]
        self.dtypes = [
194
            {"dtype": "float32"},
195 196
        ]
        self.attrs = [
197 198 199 200 201 202 203 204
            {"axes": [0]},
            {"axes": [1]},
            {"axes": [2]},
            {"axes": [3]},
            {"axes": [-1]},
            {"axes": [-2]},
            {"axes": [-3]},
            {"axes": [-4]},
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        ]
        net_builder_api_attrs = [
            {
                "net_builder_api": "reverse",
            },
            {
                "net_builder_api": "flip",
            },
        ]
        self._register_custom_attrs(net_builder_api_attrs)


class TestReverseOpMultiAxis(TestCaseHelper):
    def init_attrs(self):
        self.class_name = "TestReverseOpMultiAxis"
        self.cls = TestReverseOp
        self.inputs = [
            {
                "shape": [8, 4, 2, 16],
            },
            {
                "shape": [1, 1, 1, 1],
            },
        ]
        self.dtypes = [
230
            {"dtype": "float32"},
231 232
        ]
        self.attrs = [
233 234 235 236 237 238 239 240 241
            {"axes": []},
            {"axes": [0]},
            {"axes": [1, 2]},
            {"axes": [2, -1, 3]},
            {"axes": [0, -3, 3, 1]},
            {"axes": [-1]},
            {"axes": [-2, -1]},
            {"axes": [-3, -2, 3]},
            {"axes": [0, 3, -3, -2]},
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        ]
        net_builder_api_attrs = [
            {
                "net_builder_api": "reverse",
            },
            {
                "net_builder_api": "flip",
            },
        ]
        self._register_custom_attrs(net_builder_api_attrs)


if __name__ == "__main__":
    TestReverseOpShape().run()
    TestReverseOpDtype().run()
    TestReverseOpAxis().run()
    TestReverseOpMultiAxis().run()