test_unary_op.py 5.2 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) 2022 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.

import unittest
18

19 20
import numpy as np
from op_mapper_test import OpMapperTest, logger
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35
import paddle


class TestUnaryOp(OpMapperTest):
    def init_input_data(self):
        self.feed_data = {'x': self.random([32, 64], "float32")}

    def set_op_type(self):
        return "sqrt"

    def set_op_inputs(self):
        x = paddle.static.data(
            name='x',
            shape=self.feed_data['x'].shape,
36 37
            dtype=self.feed_data['x'].dtype,
        )
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        return {'X': [x]}

    def set_op_attrs(self):
        return {}

    def set_op_outputs(self):
        return {'Out': [str(self.feed_data['x'].dtype)]}

    def test_check_results(self):
        self.check_outputs_and_grads()


class TestSqrtOp(TestUnaryOp):
    def set_op_type(self):
        return "sqrt"


class TestGeluOp(TestUnaryOp):
    def set_op_type(self):
        return "gelu"


class TestSigmoidOp(TestUnaryOp):
    def set_op_type(self):
        return "sigmoid"


class TestExpOp(TestUnaryOp):
    def set_op_type(self):
        return "exp"


class TestErfOp(TestUnaryOp):
    def set_op_type(self):
        return "erf"


class TestRsqrtOp(TestUnaryOp):
    def set_op_type(self):
        return "rsqrt"


class TestSinOp(TestUnaryOp):
    def set_op_type(self):
        return "sin"


class TestCosOp(TestUnaryOp):
    def set_op_type(self):
        return "cos"


class TestTanOp(TestUnaryOp):
    def set_op_type(self):
        return "tan"


class TestSinhOp(TestUnaryOp):
    def set_op_type(self):
        return "sinh"


class TestCoshOp(TestUnaryOp):
    def set_op_type(self):
        return "cosh"


class TestTanhOp(TestUnaryOp):
    def set_op_type(self):
        return "tanh"


class TestAsinOp(TestUnaryOp):
    def set_op_type(self):
        return "asin"


class TestAcosOp(TestUnaryOp):
    def set_op_type(self):
        return "acos"


class TestAtanOp(TestUnaryOp):
    def set_op_type(self):
        return "atan"


class TestAsinhOp(TestUnaryOp):
    def set_op_type(self):
        return "asinh"


class TestAcoshOp(TestUnaryOp):
    def init_input_data(self):
        self.feed_data = {'x': self.random([32, 64], "float32", 1.0, 10.0)}

    def set_op_type(self):
        return "acosh"


class TestAtanhOp(TestUnaryOp):
    def set_op_type(self):
        return "atanh"


class TestSignOp(TestUnaryOp):
    def set_op_type(self):
        return "sign"


class TestAbsOp(TestUnaryOp):
    def set_op_type(self):
        return "abs"


class TestReciprocalOp(TestUnaryOp):
    def set_op_type(self):
        return "reciprocal"


class TestFloorOp(TestUnaryOp):
    def init_input_data(self):
        self.feed_data = {'x': self.random([32, 64], "float32", -2.0, 2.0)}

    def set_op_type(self):
        return "floor"

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


class TestCeilOp(TestUnaryOp):
    def init_input_data(self):
        self.feed_data = {'x': self.random([32, 64], "float32", -2.0, 2.0)}

    def set_op_type(self):
        return "ceil"

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


class TestRoundOp(TestUnaryOp):
    def init_input_data(self):
        self.feed_data = {'x': self.random([32, 64], "float32", -2.0, 2.0)}

    def set_op_type(self):
        return "round"

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


class TestTruncOp(TestUnaryOp):
    def init_input_data(self):
        self.feed_data = {'x': self.random([32, 64], "float32", -2.0, 2.0)}

    def set_op_type(self):
        return "trunc"

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


class TestIsNanOp(TestUnaryOp):
    def set_op_type(self):
        return "isnan_v2"

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


class TestIsNanCase1(TestIsNanOp):
    def init_case(self):
        self.inputs = {"x": self.random([32, 64])}
        self.inputs["x"][0] = [np.nan] * 64


class TestIsFiniteOp(TestUnaryOp):
    def set_op_type(self):
        return "isfinite_v2"

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


class TestIsFiniteCase1(TestIsFiniteOp):
    def init_case(self):
        self.inputs = {"x": self.random([32, 64])}
        self.inputs["x"][0] = [np.inf] * 64


class TestIsInfOp(TestUnaryOp):
    def set_op_type(self):
        return "isinf_v2"

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


class TestIsInfCase1(TestIsInfOp):
    def init_case(self):
        self.inputs = {"x": self.random([32, 64])}
        self.inputs["x"][0] = [np.inf] * 64


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