test_relu_op.py 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env python3

# Copyright (c) 2021 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 *
18
from cinn.frontend import *
19 20 21
from op_test import OpTest, OpTestTool
from op_test_helper import TestCaseHelper

22 23 24
import paddle
import paddle.nn.functional as F

25

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

    def prepare_inputs(self):
        self.inputs = {
37 38 39 40
            "x": self.random(self.case["shape"], self.case["dtype"], -1.0, 1.0),
            "dout": self.random(
                self.case["shape"], self.case["dtype"], -1.0, 1.0
            ),
41 42 43 44 45 46
        }

    def build_paddle_program(self, target):
        x = paddle.to_tensor(self.inputs["x"], stop_gradient=False)
        out = F.relu(x)
        self.paddle_outputs = [out]
47 48 49
        self.paddle_grads = self.get_paddle_grads(
            [out], [x], [self.inputs["dout"]]
        )
50 51 52 53 54

    def build_cinn_program(self, target):
        builder = NetBuilder("relu")
        x = builder.create_input(
            self.nptype2cinntype(self.inputs["x"].dtype),
55 56 57
            self.inputs["x"].shape,
            "x",
        )
58 59 60 61
        out = builder.relu(x)

        dout = builder.create_input(
            self.nptype2cinntype(self.inputs["dout"].dtype),
62 63 64
            self.inputs["dout"].shape,
            "dout",
        )
65 66 67 68 69
        x_grad = builder.relu_grad(dout, out)

        prog = builder.build()
        res = self.get_cinn_output(
            prog,
70 71 72
            target,
            [x, dout],
            [self.inputs["x"], self.inputs["dout"]],
73
            [out, x_grad],
74 75
            passes=[],
        )
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

        self.cinn_outputs = [res[0]]
        self.cinn_grads = [res[1]]

    def test_check_results(self):
        self.check_outputs_and_grads()


class TestReluOpShape(TestCaseHelper):
    def init_attrs(self):
        self.class_name = "TestReluOpShape"
        self.cls = TestReluOp
        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": [1, 1, 1, 1],
            },
        ]
        self.dtypes = [
127
            {"dtype": "float32"},
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        ]
        self.attrs = []


class TestReluOpDtype(TestCaseHelper):
    def init_attrs(self):
        self.class_name = "TestReluOpDtype"
        self.cls = TestReluOp
        self.inputs = [
            {
                "shape": [1],
            },
            {
                "shape": [5],
            },
            {
                "shape": [80, 40, 5, 7],
            },
        ]
        self.dtypes = [
148 149 150
            {"dtype": "float16"},
            {"dtype": "float32"},
            {"dtype": "float64"},
151 152 153 154 155 156 157
        ]
        self.attrs = []


if __name__ == "__main__":
    TestReluOpShape().run()
    TestReluOpDtype().run()