nonlinear_fuc_ops.h 9.5 KB
Newer Older
L
lujiale 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/**
 * Copyright 2019-2020 Huawei Technologies Co., Ltd
 *
 * 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.
 */

#ifndef GE_OP_NONLINEAR_FUC_OPS_H
#define GE_OP_NONLINEAR_FUC_OPS_H

#include "../graph/operator_reg.h"

namespace ge {
/**
*@brief Computes the for the gelu of "x".

*@par Inputs:
*Two inputs, including:
* @li x: A Tensor. Must be one of the following types: float16, float32

*@par Outputs:
*y: A Tensor. Has the same type as "x".
*/
REG_OP(Gelu)
    .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
    .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
    .OP_END_FACTORY_REG(Gelu)

/**
*@brief Computes the gradient for the gelu of "x".

*@par Inputs:
*Two inputs, including:
* @li dy: A Tensor. Must be one of the following types: float16, float32
* @li x: A Tensor of the same type as "dy".
* @li y: A Tensor of the same type as "dy".

*@par Outputs:
*z: A Tensor. Has the same type as "dy".
*/
REG_OP(GeluGrad)
    .INPUT(dy, TensorType({DT_FLOAT16, DT_FLOAT}))
    .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
    .INPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
    .OUTPUT(z, TensorType({DT_FLOAT16, DT_FLOAT}))
    .OP_END_FACTORY_REG(GeluGrad)

/**
*@brief Computes the gradient for the tanh of "x".

*@par Inputs:
*Two inputs, including:
* @li y: A Tensor. Must be one of the following types: float16, float32,
*     double, complex64, complex128.
* @li dy: A Tensor of the same type as "y".

*@par Outputs:
*z: A Tensor. Has the same type as "y".
*/
REG_OP(TanhGrad)
    .INPUT(y, TensorType::UnaryDataType())
    .INPUT(dy, TensorType::UnaryDataType())
    .OUTPUT(z, TensorType::UnaryDataType())
    .OP_END_FACTORY_REG(TanhGrad)

REG_OP(Tanh)
    .INPUT(x, TensorType::UnaryDataType())
    .OUTPUT(y, TensorType::UnaryDataType())
    .OP_END_FACTORY_REG(Tanh)

/**
* @brief Computes rectified linear: "max(x, 0)".
*
* @par Inputs:
* x: A tensor. Must be one of the following types: float32, float64, int32, uint8,\n
*     int16, int8, int64, uint16, float16, qint8.
*
* @par Outputs:
* y: A tensor. Has the same type as "x".
*
*/
REG_OP(Relu)
    .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE,
                          DT_INT8, DT_INT32, DT_INT16, DT_INT64,
                          DT_UINT8, DT_UINT16, DT_QINT8}))
    .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE,
                           DT_INT8, DT_INT32, DT_INT16, DT_INT64,
                           DT_UINT8, DT_UINT16, DT_QINT8}))
    .OP_END_FACTORY_REG(Relu)

/**
* @brief Computes rectified linear 6.
* activations = min(max(features, 0), 6).

* @par Inputs:
* features: A Tensor of type RealNumberType.

* @par Outputs:
* activations: A Tensor of type RealNumberType.
*/
REG_OP(Relu6)
    .INPUT(features, TensorType::RealNumberType())
    .OUTPUT(activations, TensorType::RealNumberType())
    .OP_END_FACTORY_REG(Relu6)

/**
* @brief Computes rectified linear 6 gradients for a Relu6 operation.
*     z = dy * (y > 0) * (y < 6).

* @par Inputs:
* @li y: A Tensor of type RealNumberType.
* @li dy: A Tensor of type RealNumberType.

* @par Outputs:
* z: A Tensor of type RealNumberType.
*/
REG_OP(Relu6Grad)
    .INPUT(y, TensorType::RealNumberType())
    .INPUT(dy, TensorType::RealNumberType())
    .OUTPUT(z, TensorType::RealNumberType())
    .OP_END_FACTORY_REG(Relu6Grad)

/**
* @brief Compute sigmoid of "x" element-wise.

* @par Inputs:
* A Tensor of type UnaryDataType.

* @par Outputs:
* A Tensor. Has the same type as "x".

* @attention Constraints:
* @li "x" is with shape (D1, D2, ..., DK), where, D1 * D2... * Dn <= 2^31-1,
* Di <= 1000000, n <= 8.
* @li Ascend 310 provides only 1?? accuracy for the result.

* @see Relu()
*/
REG_OP(Sigmoid)
    .INPUT(x, TensorType(UnaryDataType))
    .OUTPUT(y, TensorType(UnaryDataType))
    .OP_END_FACTORY_REG(Sigmoid)

/**
* @brief Computes z = (y - y*y)*dy.

* @par Inputs:
* @li y: the input is tensor , dtype is UnaryDataType.
* @li dy the input is tensor , dtype is UnaryDataType.

* @par Outputs:
* z: the shape of output, dtype is UnaryDataType.
*/
REG_OP(SigmoidGrad)
    .INPUT(y, TensorType(UnaryDataType))
    .INPUT(dy, TensorType(UnaryDataType))
    .OUTPUT(z, TensorType(UnaryDataType))
    .OP_END_FACTORY_REG(SigmoidGrad)

REG_OP(Activation)
    .INPUT(x, TensorType::ALL())
    .OUTPUT(y, TensorType::ALL())
    /*
       0:sigmod, 1:relu, 2:tanh, 3:clipped ReLU, 4:Elu,
       5:leaky relu, 6:abs, 7:relu1, 8:softsign, 9:softplus
    */
    .ATTR(mode, Int, 1)
    .ATTR(coef, Float, 0)
    .OP_END_FACTORY_REG(Activation)

REG_OP(ActivationGrad)
    .INPUT(dy, TensorType{DT_FLOAT})
    .INPUT(x, TensorType{DT_FLOAT})
    .OUTPUT(dx, TensorType{DT_FLOAT})
    .ATTR(mode, Int, 1)
    .OP_END_FACTORY_REG(ActivationGrad)

REG_OP(Softplus)
    .INPUT(features, TensorType::FloatingDataType())
    .OUTPUT(activations, TensorType::FloatingDataType())
    .OP_END_FACTORY_REG(Softplus)

REG_OP(SoftplusGrad)
    .INPUT(gradients, TensorType::FloatingDataType())
    .INPUT(features, TensorType::FloatingDataType())
    .OUTPUT(backprops, TensorType::FloatingDataType())
    .OP_END_FACTORY_REG(SoftplusGrad)

REG_OP(Softsign)
    .INPUT(features, TensorType::FloatingDataType())
    .OUTPUT(activations, TensorType::FloatingDataType())
    .OP_END_FACTORY_REG(Softsign)

REG_OP(Selu)
    .INPUT(features, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
                                 DT_INT8,DT_INT32}))
    .OUTPUT(activations, TensorType({DT_FLOAT16,DT_FLOAT,DT_DOUBLE,
                                     DT_INT8,DT_INT32}))
    .OP_END_FACTORY_REG(Selu)

REG_OP(ReluGrad)
    .INPUT(gradients, TensorType::RealNumberType())
    .INPUT(features, TensorType::RealNumberType())
    .OUTPUT(backprops, TensorType::RealNumberType())
    .OP_END_FACTORY_REG(ReluGrad)

/**
*@brief Computes rectified linear gradients for a ReLU operation.

*@par Inputs:
* Two inputs, including:
*@li gradients: A Tensor. Must be one of the following types: float32, double, int32, int8, int16,\n int8, int64, uint16, float16, uint32, uint64
*@li mask: A Tensor. Must be the following types: uint8

*@par Outputs:
*backprops: A Tensor. Must have the same type as"gradients".

*@attention Constraints:
* The corresponding Relu operator needs to be called before using this operator on the network.

*@see Relu
*/
REG_OP(ReluGradV2)
    .INPUT(gradients, TensorType::RealNumberType())
    .INPUT(mask, TensorType({DT_UINT8}))
    .OUTPUT(backprops, TensorType::RealNumberType())
    .OP_END_FACTORY_REG(ReluGradV2)

/**
*@brief Computes rectified linear: `max(x, 0)`.
*
*@attention Constraints:\n
* The last dim must be mutiply of 8
* The second output `mask` is the result of `y` use 'gt' compare with 0.
*
*@par Inputs:
* x: A tensor. Must be one of the following types: float32, float64, int32, uint8,
*     int16, int8, int64, uint16, float16, qint8.
*
*@par Outputs:
*@li y : A `Tensor`. Has the same type as `x`.
*@li mask : A `Tensor`. Must be the type : `uint8`.
*
*/
REG_OP(ReluV2)
    .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
    .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, DT_INT32, DT_INT16, DT_INT64, DT_UINT8, DT_UINT16, DT_QINT8}))
    .OUTPUT(mask, TensorType({DT_UINT8}))
    .OP_END_FACTORY_REG(ReluV2)

REG_OP(PRelu)
    .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16}))
    .INPUT(weight, TensorType({DT_FLOAT, DT_FLOAT16}))
    .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16}))
    .OP_END_FACTORY_REG(PRelu)

REG_OP(PReluGrad)
    .INPUT(input_gradients, TensorType({DT_FLOAT16, DT_FLOAT}))
    .INPUT(input_features, TensorType({DT_FLOAT16, DT_FLOAT}))
    .INPUT(input_weights, TensorType({DT_FLOAT16, DT_FLOAT}))
    .OUTPUT(output_backprops_dx, TensorType({DT_FLOAT16, DT_FLOAT}))
    .OUTPUT(output_backprops_da, TensorType({DT_FLOAT16, DT_FLOAT}))
    .OP_END_FACTORY_REG(PReluGrad)

/**
*@brief Computes exponential linear: `exp(x) - 1` if < 0, `x` otherwise.
*
*@par Inputs:
* x : A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`.
*
*@par Outputs:
* y : A `Tensor`. Has the same type as `x`.
*
*/
REG_OP(Elu)
    .INPUT(x, TensorType::FloatingDataType())
    .OUTPUT(y, TensorType::FloatingDataType())
    .ATTR(alpha, Float, 1.0)
    .OP_END_FACTORY_REG(Elu)

/**
*@brief Computes gradients for the exponential linear (Elu) operation.
*
*@par Inputs:
*@li grads : A `Tensor`. Must be one of the following types: `float16`, `float32`, `float64`.
*     The backpropagated gradients to the corresponding Elu operation.
*@li activations : A `Tensor`. Must have the same type as `grads`.
*     The outputs of the corresponding Elu operation.
*
*@par Outputs:
* y : A `Tensor`. Has the same type as `grads`.
*
*/
REG_OP(EluGrad)
    .INPUT(grads, TensorType::FloatingDataType())
    .INPUT(activations, TensorType::FloatingDataType())
    .OUTPUT(y, TensorType::FloatingDataType())
    .OP_END_FACTORY_REG(EluGrad)

REG_OP(LeakyRelu)
    .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_INT8}))
    .ATTR(negative_slope, Float, 0.0)
    .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT32, DT_INT8}))
    .OP_END_FACTORY_REG(LeakyRelu)

} // namespace ge

#endif // GE_OP_NONLINEAR_FUC_OPS_H