activation_op.cc 48.2 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2

L
Luo Tao 已提交
3 4 5
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
Q
qijun 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Q
qijun 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Q
qijun 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/activation_op.h"
16

T
tink2123 已提交
17
#include <memory>
D
dzhwinter 已提交
18
#include <string>
19
#include <type_traits>
T
tink2123 已提交
20
#include <unordered_map>
21
#include <vector>
22

23
#include "paddle/fluid/framework/op_version_registry.h"
24
#include "paddle/fluid/operators/common_infer_shape_functions.h"
25
#include "paddle/fluid/operators/mkldnn/mkldnn_activation_op.h"
D
dzhwinter 已提交
26
#include "paddle/fluid/platform/port.h"
27 28 29
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/cudnn_helper.h"
#endif
Q
qijun 已提交
30

A
Adam 已提交
31 32
DECLARE_bool(use_mkldnn);

Q
qijun 已提交
33 34 35
namespace paddle {
namespace operators {

36 37
using paddle::framework::Tensor;

38 39 40 41 42
template <typename GradFunctor>
static constexpr bool CanInplaceAct() {
  return GradFunctor::FwdDeps() == kDepOut || GradFunctor::FwdDeps() == kNoDeps;
}

43 44 45 46 47
#define REGISTER_ACTIVATION_OP_MAKER(OP_NAME, OP_COMMENT)                    \
  class OP_NAME##OpMaker                                                     \
      : public ::paddle::framework::OpProtoAndCheckerMaker {                 \
   public:                                                                   \
    void Make() override {                                                   \
48 49 50 51 52
      AddInput("X", "Input of " #OP_NAME                                     \
                    " operator, an N-D Tensor, with data type float32, "     \
                    "float64 or float16.");                                  \
      AddOutput("Out", "Output of " #OP_NAME                                 \
                       " operator, a Tensor with shape same as input.");     \
53 54 55 56 57 58 59 60 61
      AddAttr<bool>("use_mkldnn",                                            \
                    "(bool, default false) Only used in mkldnn kernel")      \
          .SetDefault(false);                                                \
      AddAttr<bool>("use_cudnn",                                             \
                    "(bool, default false) Only used in cudnn kernel, need " \
                    "install cudnn")                                         \
          .SetDefault(false);                                                \
      AddComment(OP_COMMENT);                                                \
    }                                                                        \
D
dzhwinter 已提交
62
  }
D
dzhwinter 已提交
63

H
hong 已提交
64 65
template <ActBwdOpFwdDeps kDepValue, typename T>
class ActivationGradOpMaker : public framework::SingleGradOpMaker<T> {
66
 public:
H
hong 已提交
67
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
68 69

 protected:
70
  void Apply(GradOpPtr<T> op) const override {
H
hong 已提交
71 72 73 74
    op->SetType(this->ForwardOpType() + "_grad");
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
75

A
Adam 已提交
76 77
    if ((static_cast<int>(kDepValue) &
         static_cast<int>(ActBwdOpFwdDeps::kDepX)) ||
78 79 80
        FLAGS_use_mkldnn ||
        (op->HasAttr("use_mkldnn") &&
         BOOST_GET_CONST(bool, op->GetAttr("use_mkldnn")))) {
H
hong 已提交
81
      op->SetInput("X", this->Input("X"));
82 83 84 85
    }

    if (static_cast<int>(kDepValue) &
        static_cast<int>(ActBwdOpFwdDeps::kDepOut)) {
H
hong 已提交
86
      op->SetInput("Out", this->Output("Out"));
87
    }
D
dzhwinter 已提交
88
  }
89
};
D
dzhwinter 已提交
90

91 92 93 94
framework::OpKernelType GetKernelType(const framework::ExecutionContext& ctx,
                                      const framework::OperatorWithKernel& oper,
                                      const std::string& name) {
  framework::LibraryType library{framework::LibraryType::kPlain};
M
mozga-intel 已提交
95
  framework::DataLayout layout = framework::DataLayout::kAnyLayout;
96
  auto data_type = oper.IndicateVarDataType(ctx, name);
97 98 99 100 101 102 103 104 105 106
// FIXME(liuwei1031) temporarily disable the code to unblock users
// TODO(liuwei1031) figure out the reason behind
// https://github.com/PaddlePaddle/Paddle/issues/16096
// and re-enable this in the future
// #ifdef PADDLE_WITH_CUDA
//   auto it1 = oper.Attrs().find("use_cudnn");
//   if (it1 != oper.Attrs().end() && platform::CanCUDNNBeUsed(ctx)) {
//     library = framework::LibraryType::kCUDNN;
//   }
// #endif
107 108 109
#ifdef PADDLE_WITH_MKLDNN
  auto it = oper.Attrs().find("use_mkldnn");
  if (library == framework::LibraryType::kPlain && it != oper.Attrs().end() &&
110
      oper.CanMKLDNNBeUsed(ctx, data_type)) {
111
    library = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
112
    layout = framework::DataLayout::kMKLDNN;
113 114
  }
#endif
115
  return framework::OpKernelType(data_type, ctx.GetPlace(), layout, library);
116 117
}

Q
qijun 已提交
118 119 120 121
class ActivationOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

122
  void InferShape(framework::InferShapeContext* ctx) const override {
123
    ctx->ShareDim("X", /*->*/ "Out");
F
fengjiayi 已提交
124
    ctx->ShareLoD("X", /*->*/ "Out");
Q
qijun 已提交
125
  }
126

127
 protected:
128 129 130 131
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return GetKernelType(ctx, *this, "X");
  }
Q
qijun 已提交
132 133
};

C
chengduo 已提交
134 135 136
class ActivationOpInferVarType
    : public framework::PassInDtypeAndVarTypeToOutput {
 protected:
137
  std::unordered_map<std::string, std::string>& GetInputOutputWithSameType()
C
chengduo 已提交
138
      const override {
139 140
    static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Out"}};
    return m;
141 142 143
  }
};

Q
qijun 已提交
144 145 146 147
class ActivationOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

148
  void InferShape(framework::InferShapeContext* ctx) const override {
149 150 151
    auto out_grad_name = framework::GradVarName("Out");
    ctx->ShareDim(out_grad_name, framework::GradVarName("X"));
    ctx->ShareLoD(out_grad_name, framework::GradVarName("X"));
Q
qijun 已提交
152
  }
153

154
 protected:
155 156
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
157
    return GetKernelType(ctx, *this, framework::GradVarName("Out"));
158
  }
Q
qijun 已提交
159 160
};

D
dzhwinter 已提交
161
UNUSED constexpr char SigmoidDoc[] = R"DOC(
162
Sigmoid Activation Operator
K
Kexin Zhao 已提交
163

164
$$out = \\frac{1}{1 + e^{-x}}$$
K
Kexin Zhao 已提交
165

D
dzhwinter 已提交
166
)DOC";
Q
qijun 已提交
167

D
dzhwinter 已提交
168
UNUSED constexpr char LogSigmoidDoc[] = R"DOC(
169
Logsigmoid Activation Operator
K
Kexin Zhao 已提交
170

171
$$out = \\log \\frac{1}{1 + e^{-x}}$$
K
Kexin Zhao 已提交
172

D
dzhwinter 已提交
173
)DOC";
174

D
dzhwinter 已提交
175
UNUSED constexpr char ExpDoc[] = R"DOC(
176
Exp Operator. Computes exp of x element-wise with a natural number :math:`e` as the base.
K
Kexin Zhao 已提交
177

178
$$out = e^x$$
K
Kexin Zhao 已提交
179

D
dzhwinter 已提交
180
)DOC";
Q
qijun 已提交
181

D
dzhwinter 已提交
182
UNUSED constexpr char ReluDoc[] = R"DOC(
K
kexinzhao 已提交
183
Relu Activation Operator.
K
Kexin Zhao 已提交
184

185
$$out = \max(x, 0)$$
K
Kexin Zhao 已提交
186

D
dzhwinter 已提交
187
)DOC";
K
Kexin Zhao 已提交
188

D
dzhwinter 已提交
189
UNUSED constexpr char TanhDoc[] = R"DOC(
K
kexinzhao 已提交
190
Tanh Activation Operator.
K
Kexin Zhao 已提交
191

Q
update  
qiaolongfei 已提交
192
$$out = \\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$
K
Kexin Zhao 已提交
193

D
dzhwinter 已提交
194
)DOC";
195

D
dzhwinter 已提交
196
UNUSED constexpr char TanhShrinkDoc[] = R"DOC(
K
kexinzhao 已提交
197
TanhShrink Activation Operator.
K
Kexin Zhao 已提交
198

Y
Yan Chunwei 已提交
199
$$out = x - \\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$
K
Kexin Zhao 已提交
200

D
dzhwinter 已提交
201
)DOC";
K
Kexin Zhao 已提交
202

D
dzhwinter 已提交
203
UNUSED constexpr char SqrtDoc[] = R"DOC(
K
kexinzhao 已提交
204
Sqrt Activation Operator.
K
Kexin Zhao 已提交
205

N
Noel 已提交
206
$$out=\\sqrt{x}=x^{1/2}$$
207

208 209
**Note**:
  input value must be greater than or equal to zero.
K
Kexin Zhao 已提交
210

D
dzhwinter 已提交
211
)DOC";
212

Z
zhoukunsheng 已提交
213 214 215 216 217
UNUSED constexpr char RsqrtDoc[] = R"DOC(
Rsqrt Activation Operator.

Please make sure input is legal in case of numeric errors.

218
$$out = \\frac{1}{\\sqrt{x}}$$
Z
zhoukunsheng 已提交
219 220 221

)DOC";

D
dzhwinter 已提交
222
UNUSED constexpr char CeilDoc[] = R"DOC(
223
Ceil Operator. Computes ceil of x element-wise.
D
dzhwinter 已提交
224

N
Noel 已提交
225
$$out = \\lceil x \\rceil$$
D
dzhwinter 已提交
226

D
dzhwinter 已提交
227
)DOC";
D
dzhwinter 已提交
228

D
dzhwinter 已提交
229
UNUSED constexpr char FloorDoc[] = R"DOC(
230
Floor Activation Operator. Computes floor of x element-wise.
D
dzhwinter 已提交
231

N
Noel 已提交
232
$$out = \\lfloor x \\rfloor$$
D
dzhwinter 已提交
233

D
dzhwinter 已提交
234
)DOC";
D
dzhwinter 已提交
235

D
dzhwinter 已提交
236
UNUSED constexpr char CosDoc[] = R"DOC(
237
Cosine Operator. Computes cosine of x element-wise.
C
add cos  
chengduoZH 已提交
238

Y
Yang Zhang 已提交
239 240
Input range is `(-inf, inf)` and output range is `[-1,1]`.

241
$$out = cos(x)$$
C
add cos  
chengduoZH 已提交
242

D
dzhwinter 已提交
243
)DOC";
C
add cos  
chengduoZH 已提交
244

J
joejiong 已提交
245 246 247 248 249 250 251 252 253
UNUSED constexpr char TanDoc[] = R"DOC(
Tangent Operator. Computes tangent of x element-wise.

Input range is `(k*pi-pi/2, k*pi+pi/2)` and output range is `(-inf, inf)`.

$$out = tan(x)$$

)DOC";

D
dzhwinter 已提交
254
UNUSED constexpr char SinDoc[] = R"DOC(
C
add sin  
chengduoZH 已提交
255 256
Sine Activation Operator.

257
$$out = sin(x)$$
C
add sin  
chengduoZH 已提交
258

D
dzhwinter 已提交
259
)DOC";
C
add sin  
chengduoZH 已提交
260

261 262 263 264 265 266 267 268 269 270 271 272 273 274
UNUSED constexpr char SinhDoc[] = R"DOC(
Sinh Activation Operator.

$$out = sinh(x)$$

)DOC";

UNUSED constexpr char CoshDoc[] = R"DOC(
Cosh Activation Operator.

$$out = cosh(x)$$

)DOC";

D
dzhwinter 已提交
275
UNUSED constexpr char RoundDoc[] = R"DOC(
276
The OP rounds the values in the input to the nearest integer value.
D
dzhwinter 已提交
277

N
Noel 已提交
278
.. code-block:: text
279 280 281 282 283 284 285 286

  input:
    x.shape = [4]
    x.data = [1.2, -0.9, 3.4, 0.9]

  output:
    out.shape = [4]
    out.data = [1., -1., 3., 1.]
D
dzhwinter 已提交
287

D
dzhwinter 已提交
288
)DOC";
D
dzhwinter 已提交
289

D
dzhwinter 已提交
290
UNUSED constexpr char ReciprocalDoc[] = R"DOC(
K
kexinzhao 已提交
291
Reciprocal Activation Operator.
K
Kexin Zhao 已提交
292

293
$$out = \\frac{1}{x}$$
K
Kexin Zhao 已提交
294

D
dzhwinter 已提交
295
)DOC";
296

D
dzhwinter 已提交
297
UNUSED constexpr char LogDoc[] = R"DOC(
K
kexinzhao 已提交
298
Log Activation Operator.
K
Kexin Zhao 已提交
299

300
$$out = \ln(x)$$
K
Kexin Zhao 已提交
301 302 303

Natural logarithm of x.

D
dzhwinter 已提交
304 305
)DOC";

J
joejiong 已提交
306 307 308 309 310 311 312 313 314
UNUSED constexpr char Log2Doc[] = R"DOC(
Log2 Activation Operator.

$$out = \log_2x$$

logarithm of x base to 2.

)DOC";

J
joejiong 已提交
315 316 317 318 319 320 321 322 323
UNUSED constexpr char Log10Doc[] = R"DOC(
Log10 Activation Operator.

$$out = \log_10_x$$

logarithm of x base to 10.

)DOC";

324 325 326 327 328 329 330 331 332
UNUSED constexpr char Log1pDoc[] = R"DOC(
Log Activation Operator.

$out = \ln(x+1)$

Natural logarithm of x.

)DOC";

D
dzhwinter 已提交
333
UNUSED constexpr char SquareDoc[] = R"DOC(
334
The OP square each elements of the inputs.
D
dzhwinter 已提交
335

336
$$out = x^2$$
337

D
dzhwinter 已提交
338 339
)DOC";

D
dzhwinter 已提交
340
UNUSED constexpr char SoftsignDoc[] = R"DOC(
D
dzhwinter 已提交
341 342
Softsign Activation Operator.

343
$$out = \\frac{x}{1 + \|x\|}$$
D
dzhwinter 已提交
344 345 346

)DOC";

T
tink2123 已提交
347 348 349 350 351 352
class AcosOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of acos operator");
    AddOutput("Out", "Output of acos operator");
    AddComment(R"DOC(
353
Arccosine Operator.
354

T
tink2123 已提交
355
$$out = \cos^{-1}(x)$$
356

T
tink2123 已提交
357 358 359
)DOC");
  }
};
360

T
tink2123 已提交
361 362 363
class AsinOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
W
wawltor 已提交
364 365 366
    AddInput("X",
             "Input of asin operator, an N-D Tensor, with data type float32, "
             "float64 or float16.");
T
tink2123 已提交
367 368
    AddOutput("Out", "Output of asin operator");
    AddComment(R"DOC(
369
Arcsine Operator.
370

T
tink2123 已提交
371
$$out = \sin^{-1}(x)$$
372

T
tink2123 已提交
373 374 375
)DOC");
  }
};
376

T
tink2123 已提交
377 378 379
class AtanOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
W
wawltor 已提交
380 381 382
    AddInput("X",
             "Input of atan operator, an N-D Tensor, with data type float32, "
             "float64 or float16.");
T
tink2123 已提交
383 384
    AddOutput("Out", "Output of atan operator");
    AddComment(R"DOC(
385
Arctangent Operator.
386

387
$$out = \tan^{-1}(x)$$
388

T
tink2123 已提交
389 390 391
)DOC");
  }
};
392

D
dzhwinter 已提交
393
class LeakyReluOpMaker : public framework::OpProtoAndCheckerMaker {
394
 public:
Y
Yu Yang 已提交
395
  void Make() override {
W
Wilber 已提交
396 397 398 399 400 401 402 403
    AddInput("X",
             "A LoDTensor or Tensor representing preactivation values. Must be "
             "one of the following types: float32, float64.");
    AddOutput(
        "Out",
        "A LoDTensor or Tensor with the same type and size as that of x.");
    AddAttr<float>("alpha", "Slope of the activation function at x < 0.")
        .SetDefault(0.02f);
A
Adam 已提交
404 405 406
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
407
    AddComment(R"DOC(
D
dzhwinter 已提交
408
LeakyRelu Activation Operator.
K
Kexin Zhao 已提交
409

W
Wilber 已提交
410
$$out = \max(x, \alpha * x)$$
K
Kexin Zhao 已提交
411 412

)DOC");
413 414 415
  }
};

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
class SoftplusOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "Input of Softplus operator, an N-D Tensor, with data type "
             "float32, float64 or float16.");
    AddOutput(
        "Out",
        "Output of Softplus operator, a Tensor with shape same as input.");
    AddAttr<float>("beta", "The value of beta for Softplus.").SetDefault(1.0f);
    AddAttr<float>("threshold", "The value of threshold for Softplus.")
        .SetDefault(20.0f);
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel.")
        .SetDefault(false);
    AddAttr<bool>(
        "use_cudnn",
        "(bool, default false) Only used in cudnn kernel, need install cudnn.")
        .SetDefault(false);
    AddComment(R"DOC(
:strong:`Softplus Activation Operator`

..  math::
    out = \frac{1}{\beta} * \log(1 + \exp(\beta * x)) \\
    \text{For numerical stability, the implementation reverts to the linear function when :}\,x \times \beta > threshold.

)DOC");
  }
};

D
dzhwinter 已提交
446
class SoftShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
K
kexinzhao 已提交
447
 public:
Y
Yu Yang 已提交
448
  void Make() override {
D
dzhwinter 已提交
449 450 451
    AddInput("X", "Input of Softshrink operator");
    AddOutput("Out", "Output of Softshrink operator");
    AddAttr<float>("lambda", "non-negative offset").SetDefault(0.5f);
K
Kexin Zhao 已提交
452
    AddComment(R"DOC(
453 454 455
:strong:`Softshrink Activation Operator`

..  math::
456
    out = \begin{cases}
457 458 459 460
         x - \lambda, \text{if } x > \lambda \\
         x + \lambda, \text{if } x < -\lambda \\
         0,  \text{otherwise}
         \end{cases}
K
Kexin Zhao 已提交
461 462

)DOC");
K
kexinzhao 已提交
463 464 465
  }
};

D
dzhwinter 已提交
466
class HardShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
467
 public:
Y
Yu Yang 已提交
468
  void Make() override {
D
dzhwinter 已提交
469 470
    AddInput("X", "Input of HardShrink operator");
    AddOutput("Out", "Output of HardShrink operator");
Y
yuyang18 已提交
471 472
    AddAttr<float>("threshold",
                   "The value of threshold for HardShrink. [default: 0.5]")
D
dzhwinter 已提交
473
        .SetDefault(0.5f);
K
Kexin Zhao 已提交
474
    AddComment(R"DOC(
Y
yuyang18 已提交
475
:strong:`HardShrink activation operator`
K
Kexin Zhao 已提交
476

Y
yuyang18 已提交
477 478 479 480 481 482
..  math::
    out = \begin{cases}
            x, \text{if } x > \lambda \\
            x, \text{if } x < -\lambda \\
            0,  \text{otherwise}
          \end{cases}
K
Kexin Zhao 已提交
483 484

)DOC");
485 486 487
  }
};

488 489
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
490
  void Make() override {
491 492 493 494 495 496
    AddInput("X",
             "The input is a multi-dimensional Tensor. The data type is "
             "float32, float64.");
    AddOutput("Out",
              "The output is a multi-dimensional Tensor which has same "
              "dimension and data type as the ``X``.");
497 498 499 500
    AddAttr<float>("t_min", "The min marginal value of BRelu")
        .SetDefault(static_cast<float>(0));
    AddAttr<float>("t_max", "The max marginal value of BRelu")
        .SetDefault(static_cast<float>(24));
K
Kexin Zhao 已提交
501
    AddComment(R"DOC(
K
kexinzhao 已提交
502
BRelu Activation Operator.
K
Kexin Zhao 已提交
503

504
$$out = \min(\max(x, t_{min}), t_{max})$$
K
Kexin Zhao 已提交
505 506

)DOC");
507 508 509 510 511
  }
};

class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
512
  void Make() override {
513
    AddInput("X", "Input of SoftRelu operator");
F
fengjiayi 已提交
514
    AddOutput("Out", "Output of SoftRelu operator");
515 516
    AddAttr<float>("threshold", "The threshold value of SoftRelu")
        .SetDefault(40.0f);
K
Kexin Zhao 已提交
517
    AddComment(R"DOC(
K
kexinzhao 已提交
518
SoftRelu Activation Operator.
K
Kexin Zhao 已提交
519

520
$$out = \ln(1 + \exp(\max(\min(x, threshold), -threshold)))$$
K
Kexin Zhao 已提交
521 522

)DOC");
523 524 525
  }
};

526 527
class ELUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
528
  void Make() override {
529 530 531 532 533 534
    AddInput("X",
             "The input is a multi-dimensional Tensor. The data type is "
             "float32 or float64.");
    AddOutput("Out",
              "The output is a multi-dimensional Tensor which has same "
              "dimension and data type as the ``x``.");
535
    AddAttr<float>("alpha", "The alpha value of ELU").SetDefault(1.0f);
536
    AddComment(R"DOC(
K
kexinzhao 已提交
537
ELU Activation Operator.
K
Kexin Zhao 已提交
538 539 540 541

Applies the following element-wise computation on the input according to
https://arxiv.org/abs/1511.07289.

542
$$out = \max(0, x) + \min(0, \alpha * (e^x - 1))$$
K
Kexin Zhao 已提交
543 544

)DOC");
545 546 547
  }
};

548 549
class Relu6OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
550
  void Make() override {
Z
zhupengyang 已提交
551 552 553 554 555 556 557 558
    AddInput("X",
             "Input of relu6 operator, an N-D Tensor, "
             "with data type float32, float64.");
    AddOutput(
        "Out",
        "Output of relu6 operator, a Tensor with the same shape as input.");
    AddAttr<float>("threshold",
                   "The threshold value of Relu6. Default is 6.0. ")
559
        .SetDefault(6.0f);
A
Adam 已提交
560 561 562
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
563
    AddComment(R"DOC(
K
kexinzhao 已提交
564
Relu6 Activation Operator.
K
Kexin Zhao 已提交
565

566
$$out = \min(\max(0, x), threshold)$$
K
Kexin Zhao 已提交
567 568

)DOC");
569 570 571
  }
};

572 573
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
574
  void Make() override {
575
    AddInput("X", "Input of Pow operator");
576 577 578 579 580
    AddInput("FactorTensor",
             "(Tensor<float>, optional). If provided, pow will use this"
             "The shape of FactorTensor MUST BE [1]."
             "it has higher priority than attr(factor).")
        .AsDispensable();
F
fengjiayi 已提交
581
    AddOutput("Out", "Output of Pow operator");
582
    AddAttr<float>("factor", "The exponential factor of Pow").SetDefault(1.0f);
K
Kexin Zhao 已提交
583
    AddComment(R"DOC(
K
kexinzhao 已提交
584
Pow Activation Operator.
K
Kexin Zhao 已提交
585

586
$$out = x^{factor}$$
K
Kexin Zhao 已提交
587 588

)DOC");
589 590 591 592 593
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
594
  void Make() override {
595 596
    AddInput("X",
             "Input of STanh operator."
N
Noel 已提交
597
             " A Tensor with type float32, float64.");
598 599 600
    AddOutput("Out", "Output of STanh operator. A Tensor with type float32.");
    AddAttr<float>("scale_a", "The scale parameter of a for the input. ")
        .SetDefault(0.67f);
601 602
    AddAttr<float>("scale_b", "The scale parameter of b for the input")
        .SetDefault(1.7159f);
K
Kexin Zhao 已提交
603
    AddComment(R"DOC(
K
kexinzhao 已提交
604
STanh Activation Operator.
K
Kexin Zhao 已提交
605

Y
Yan Chunwei 已提交
606
$$out = b * \\frac{e^{a * x} - e^{-a * x}}{e^{a * x} + e^{-a * x}}$$
K
Kexin Zhao 已提交
607 608

)DOC");
Q
qijun 已提交
609 610 611
  }
};

612 613
class ThresholdedReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
614
  void Make() override {
615
    AddInput("X", "Input of ThresholdedRelu operator");
F
fengjiayi 已提交
616
    AddOutput("Out", "Output of ThresholdedRelu operator");
Y
yuyang18 已提交
617 618
    AddAttr<float>("threshold",
                   "The threshold location of activation. [default 1.0].")
619
        .SetDefault(1.0f);
K
Kexin Zhao 已提交
620
    AddComment(R"DOC(
Y
yuyang18 已提交
621
:strong:`ThresholdedRelu activation operator`
K
Kexin Zhao 已提交
622

Y
yuyang18 已提交
623
..  math::
K
Kexin Zhao 已提交
624

Y
yuyang18 已提交
625
    out = \begin{cases}
Y
yuyang18 已提交
626
             x,  \text{if } x > threshold \\
Y
yuyang18 已提交
627 628
             0,  \text{otherwise}
          \end{cases}
K
Kexin Zhao 已提交
629
)DOC");
630 631 632
  }
};

633 634
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
635
  void Make() override {
636 637 638 639 640
    AddInput("X", "An N-D Tensor with data type float32, float64. ");
    AddOutput("Out", "A Tensor with the same shape as input. ");
    AddAttr<float>("slope",
                   "The slope of the linear approximation of sigmoid. Its "
                   "value MUST BE positive. Default is 0.2. ")
641
        .SetDefault(0.2f);
642 643 644
    AddAttr<float>(
        "offset",
        "The offset of the linear approximation of sigmoid. Default is 0.5. ")
645
        .SetDefault(0.5f);
646
    AddComment(R"DOC(
K
kexinzhao 已提交
647
HardSigmoid Activation Operator.
648

649
A 3-part piecewise linear approximation of sigmoid(https://arxiv.org/abs/1603.00391),
K
Kexin Zhao 已提交
650
which is much faster than sigmoid.
651

652
$$out = \max(0, \min(1, slope * x + offset))$$
653

K
Kexin Zhao 已提交
654
)DOC");
655 656 657
  }
};

A
Abhinav Arora 已提交
658 659
class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
660
  void Make() override {
A
Abhinav Arora 已提交
661
    AddInput("X", "Input of Swish operator");
F
fengjiayi 已提交
662
    AddOutput("Out", "Output of Swish operator");
A
Abhinav Arora 已提交
663
    AddAttr<float>("beta", "Constant beta of swish operator").SetDefault(1.0f);
664 665 666
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
A
Abhinav Arora 已提交
667 668 669
    AddComment(R"DOC(
Swish Activation Operator.

670
$$out = \\frac{x}{1 + e^{- \beta \ x}}$$
A
Abhinav Arora 已提交
671 672 673 674 675

)DOC");
  }
};

H
huangjun12 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
class HardSwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of HardSwish operator");
    AddOutput("Out", "Output of HardSwish operator");
    AddAttr<float>("threshold", "The threshold parameter of HardSwish operator")
        .SetDefault(6.0f);
    AddAttr<float>("scale", "The scale parameter of HardSwish operator")
        .SetDefault(6.0f);
    AddAttr<float>("offset", "The offset parameter of HardSwish operator")
        .SetDefault(3.0f);
    AddComment(R"DOC(
HardSwish Activation Operator.

The hard version of swish(https://arxiv.org/pdf/1905.02244.pdf).

692
$$out = \frac{x * (min(max(0, x+offset), threshold))}{scale}$$
H
huangjun12 已提交
693 694 695 696 697 698 699 700 701

The threshold and scale should be positive. The offset can be either positive or negative.
The default parameters are set according to the above reference.
It is recommended to use the defaults for this activation.

)DOC");
  }
};

D
dzhwinter 已提交
702 703 704 705 706 707 708
REGISTER_ACTIVATION_OP_MAKER(Sigmoid, SigmoidDoc);
REGISTER_ACTIVATION_OP_MAKER(LogSigmoid, LogSigmoidDoc);
REGISTER_ACTIVATION_OP_MAKER(Exp, ExpDoc);
REGISTER_ACTIVATION_OP_MAKER(Relu, ReluDoc);
REGISTER_ACTIVATION_OP_MAKER(Tanh, TanhDoc);
REGISTER_ACTIVATION_OP_MAKER(TanhShrink, TanhShrinkDoc);
REGISTER_ACTIVATION_OP_MAKER(Sqrt, SqrtDoc);
Z
zhoukunsheng 已提交
709
REGISTER_ACTIVATION_OP_MAKER(Rsqrt, RsqrtDoc);
D
dzhwinter 已提交
710 711 712
REGISTER_ACTIVATION_OP_MAKER(Ceil, CeilDoc);
REGISTER_ACTIVATION_OP_MAKER(Floor, FloorDoc);
REGISTER_ACTIVATION_OP_MAKER(Cos, CosDoc);
J
joejiong 已提交
713
REGISTER_ACTIVATION_OP_MAKER(Tan, TanDoc);
D
dzhwinter 已提交
714
REGISTER_ACTIVATION_OP_MAKER(Sin, SinDoc);
715 716
REGISTER_ACTIVATION_OP_MAKER(Sinh, SinhDoc);
REGISTER_ACTIVATION_OP_MAKER(Cosh, CoshDoc);
D
dzhwinter 已提交
717 718 719
REGISTER_ACTIVATION_OP_MAKER(Round, RoundDoc);
REGISTER_ACTIVATION_OP_MAKER(Reciprocal, ReciprocalDoc);
REGISTER_ACTIVATION_OP_MAKER(Log, LogDoc);
J
joejiong 已提交
720
REGISTER_ACTIVATION_OP_MAKER(Log2, Log2Doc);
J
joejiong 已提交
721
REGISTER_ACTIVATION_OP_MAKER(Log10, Log10Doc);
722
REGISTER_ACTIVATION_OP_MAKER(Log1p, Log1pDoc);
D
dzhwinter 已提交
723 724 725
REGISTER_ACTIVATION_OP_MAKER(Square, SquareDoc);
REGISTER_ACTIVATION_OP_MAKER(Softsign, SoftsignDoc);

726
template <ActBwdOpFwdDeps kDepValue>
727 728 729 730 731
class ActivationOpDoubleGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
732
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
733
      if (ctx->HasOutput("DX")) {
734 735 736
        ctx->ShareDim("X", "DX");
        ctx->ShareLoD("X", "DX");
      }
737
      if (ctx->HasOutput("DDOut")) {
738 739 740
        ctx->ShareDim("X", "DDOut");
        ctx->ShareLoD("X", "DDOut");
      }
741
    }
742
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
743
      if (ctx->HasOutput("DOut")) {
744 745 746
        ctx->ShareDim("Out", "DOut");
        ctx->ShareLoD("Out", "DOut");
      }
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
      if (ctx->HasOutput("DDOut")) {
        ctx->ShareDim("Out", "DDOut");
        ctx->ShareLoD("Out", "DDOut");
      }
    }
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return GetKernelType(ctx, *this, "DDX");
  }
};

template <ActBwdOpFwdDeps kDepValue>
class ActivationOpDoubleGrad2 : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
      if (ctx->HasOutput("DDOut")) {
        ctx->ShareDim("X", "DDOut");
        ctx->ShareLoD("X", "DDOut");
      }
    }
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
      if (ctx->HasOutput("DDOut")) {
775 776 777
        ctx->ShareDim("Out", "DDOut");
        ctx->ShareLoD("Out", "DDOut");
      }
778 779 780 781 782 783 784 785 786 787
    }
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return GetKernelType(ctx, *this, "DDX");
  }
};

788 789
// ReluGrad: dx = dy if y >= 0 else 0
// ReluGradGrad: ddy = ddx if y >= 0 else 0
H
hong 已提交
790 791
template <typename T>
class ReluDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
792
 public:
H
hong 已提交
793
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
794 795

 protected:
796
  void Apply(GradOpPtr<T> op) const override {
797 798
    op->SetType("relu_grad_grad");
    // input1: Out
H
hong 已提交
799
    op->SetInput("Out", this->Input("Out"));
Q
qingqing01 已提交
800
    // input2: ddx
H
hong 已提交
801 802
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
803
    // output: ddy
H
hong 已提交
804
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
805 806 807
  }
};

808 809
// leaky_relu Grad: dx=dy if x>=0 else alpha * dy
// leaky_relu GradGrad: ddy=ddx if x>=0 else alpha * ddx
H
hong 已提交
810
template <typename T>
811
class LeakyReluDoubleGradMaker
H
hong 已提交
812
    : public ::paddle::framework::SingleGradOpMaker<T> {
813
 public:
H
hong 已提交
814
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
815 816

 protected:
817
  void Apply(GradOpPtr<T> op) const override {
818
    op->SetType("leaky_relu_grad_grad");
819 820
    // input1: X
    op->SetInput("X", this->Input("X"));
821
    // X@GRAD@GRAD: ddx
H
hong 已提交
822 823
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
824
    // Out@GRAD@GRAD: ddy
H
hong 已提交
825
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
826 827 828
  }
};

D
Double_V 已提交
829 830 831 832 833 834 835 836
// elu grad: dx=dy if y>0 else alpha*dy*x.exp()
// elu gradgrad: ddx=ddy if y>0 else alpha*ddy*x.exp()
template <typename T>
class ELUDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
 public:
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
837
  void Apply(GradOpPtr<T> op) const override {
D
Double_V 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851
    op->SetType("elu_grad_grad");

    op->SetInput("X", this->Input("X"));
    op->SetInput("DOut", this->Input(framework::GradVarName("Out")));
    // X@GRAD@GRAD: ddx
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());

    // Out@GRAD@GRAD: ddy
    op->SetOutput("DX", this->InputGrad("X"));
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
  }
};

L
lvmengsi 已提交
852 853
// sqrt Grad: dx = 0.5 * dy / y
// sqrt GradGrad: ddy = 0.5 * ddx / y, dy = -1 * dx * ddx
H
hong 已提交
854 855
template <typename T>
class SqrtDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
L
lvmengsi 已提交
856
 public:
H
hong 已提交
857
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
L
lvmengsi 已提交
858 859

 protected:
860
  void Apply(GradOpPtr<T> op) const override {
L
lvmengsi 已提交
861
    op->SetType("sqrt_grad_grad");
H
hong 已提交
862 863 864 865 866 867
    op->SetInput("Out", this->Input("Out"));
    op->SetInput("DX", this->Output(framework::GradVarName("X")));
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
    op->SetOutput("DOut", this->InputGrad("Out"));
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
L
lvmengsi 已提交
868 869 870
  }
};

W
whs 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
// rsqrt Grad: dx = -0.5 * dy * y * y * y
// rsqrt GradGrad: ddy = -0.5 * ddx * y * y * y, dy = (3/y) * ddx
template <typename T>
class RsqrtDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
 public:
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("rsqrt_grad_grad");
    op->SetInput("Out", this->Input("Out"));
    op->SetInput("DX", this->Output(framework::GradVarName("X")));
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
    op->SetOutput("DOut", this->InputGrad("Out"));
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
  }
};

890 891
// square Grad: dx=2x*dy
// square GradGrad: ddy=2x*ddx, dx=2dy*ddx
H
hong 已提交
892 893
template <typename T>
class SquareDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
894
 public:
H
hong 已提交
895
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
896 897

 protected:
898
  void Apply(GradOpPtr<T> op) const override {
899
    op->SetType("square_grad_grad");
H
hong 已提交
900
    op->SetInput("X", this->Input("X"));
901
    // Out@GRAD: dy
H
hong 已提交
902
    op->SetInput("DOut", this->Input(framework::GradVarName("Out")));
903
    // X@GRAD@GRAD: ddx
H
hong 已提交
904
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
905

H
hong 已提交
906
    op->SetAttrMap(this->Attrs());
907 908

    // X@GRAD: dx
H
hong 已提交
909
    op->SetOutput("DX", this->InputGrad("X"));
910
    // Out@GRAD@GRAD: ddy
H
hong 已提交
911
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
912 913 914
  }
};

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
// log Grad: dx = dout / x
// log Grad Grad: ddout = ddx / x; dx = -(dout / x) * (ddx / x)
template <typename T>
class LogDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
 public:
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("log_grad_grad");
    op->SetInput("X", this->Input("X"));
    // X@GRAD@GRAD: ddx
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetInput("DOut", this->Input(framework::GradVarName("Out")));
    op->SetAttrMap(this->Attrs());
    // X@GRAD: dx
    op->SetOutput("DX", this->InputGrad("X"));
    // Out@GRAD@GRAD: ddy
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
  }
};

937
DECLARE_INPLACE_OP_INFERER(ActivationGradOpInplaceInferer,
938 939
                           {framework::GradVarName("Out"),
                            framework::GradVarName("X")});
940
DECLARE_INPLACE_OP_INFERER(ActivationDoubleGradOpInplaceInferer,
941
                           {"DDX", "DDOut"});
942

H
hong 已提交
943 944
template <typename T>
class PowGradOpMaker : public framework::SingleGradOpMaker<T> {
945
 public:
H
hong 已提交
946
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
947 948

 protected:
949
  void Apply(GradOpPtr<T> op) const override {
950
    op->SetType("pow_grad");
H
hong 已提交
951 952 953 954 955
    op->SetInput("X", this->Input("X"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetInput("FactorTensor", this->Input("FactorTensor"));
    op->SetAttrMap(this->Attrs());
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
  }
};
class PowOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    ctx->ShareDim("X", /*->*/ "Out");
    ctx->ShareLoD("X", /*->*/ "Out");
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return GetKernelType(ctx, *this, "X");
  }

  framework::OpKernelType GetKernelTypeForVar(
      const std::string& var_name, const Tensor& tensor,
      const framework::OpKernelType& expected_kernel_type) const override {
    if (var_name == "FactorTensor") {
      return expected_kernel_type;
    }
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   tensor.place(), tensor.layout());
  }
};

class PowOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    auto out_grad_name = framework::GradVarName("Out");
    ctx->ShareDim(out_grad_name, framework::GradVarName("X"));
    ctx->ShareLoD(out_grad_name, framework::GradVarName("X"));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return GetKernelType(ctx, *this, framework::GradVarName("Out"));
  }

  framework::OpKernelType GetKernelTypeForVar(
      const std::string& var_name, const Tensor& tensor,
      const framework::OpKernelType& expected_kernel_type) const override {
    if (var_name == "FactorTensor") {
      return expected_kernel_type;
    }
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   tensor.place(), tensor.layout());
  }
};
1010
DECLARE_INPLACE_OP_INFERER(ActFwdInplaceInferer, {"X", "Out"});
Q
qijun 已提交
1011 1012 1013 1014
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
1015
namespace plat = paddle::platform;
1016

1017 1018 1019 1020
#define REGISTER_ACTIVATION_OP(KERNEL_TYPE, OP_NAME, functor, grad_functor) \
  REGISTER_OPERATOR(                                                        \
      KERNEL_TYPE, ops::ActivationOp, ops::OP_NAME##OpMaker,                \
      ops::ActivationOpInferVarType,                                        \
H
hong 已提交
1021 1022 1023 1024
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::framework::OpDesc>,                \
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::imperative::OpBase>,               \
1025
      std::conditional<ops::CanInplaceAct<ops::grad_functor<float>>(),      \
1026
                       ops::ActFwdInplaceInferer, void>::type);             \
1027
  REGISTER_OPERATOR(KERNEL_TYPE##_grad, ops::ActivationOpGrad,              \
1028
                    ops::ActivationGradOpInplaceInferer);
1029 1030 1031

#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, op_name, functor,        \
                                       grad_functor)                      \
Q
QI JUN 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
  REGISTER_OP_CPU_KERNEL(                                                 \
      act_type, ops::ActivationKernel<paddle::platform::CPUDeviceContext, \
                                      ops::functor<float>>,               \
      ops::ActivationKernel<paddle::platform::CPUDeviceContext,           \
                            ops::functor<double>>);                       \
  REGISTER_OP_CPU_KERNEL(                                                 \
      act_type##_grad,                                                    \
      ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,       \
                                ops::grad_functor<float>>,                \
      ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,       \
Y
Yu Yang 已提交
1042
                                ops::grad_functor<double>>);
1043

1044 1045
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_OP);
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_CPU_KERNEL);
1046

1047
/* ==========================    relu register  ============================= */
1048 1049
REGISTER_OPERATOR(
    relu, ops::ActivationOp, ops::ReluOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1050 1051 1052 1053
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1054
    ops::ActFwdInplaceInferer);
1055
REGISTER_OPERATOR(relu_grad, ops::ActivationOpGrad,
1056
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1057 1058
                  ops::ReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ReluDoubleGradMaker<paddle::imperative::OpBase>);
1059 1060
REGISTER_OPERATOR(
    relu_grad_grad,
1061
    ops::ActivationOpDoubleGrad2<ops::ReluGradFunctor<float>::FwdDeps()>,
1062
    ops::ActivationDoubleGradOpInplaceInferer);
1063

1064
REGISTER_ACTIVATION_CPU_KERNEL(relu, Relu, ReluCPUFunctor, ReluGradFunctor);
1065 1066 1067 1068 1069 1070 1071 1072 1073

REGISTER_OP_CPU_KERNEL(
    relu_grad_grad,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::ReluGradGradFunctor<float>>,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::ReluGradGradFunctor<double>>,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::ReluGradGradFunctor<plat::float16>>);
1074
/* ========================================================================== */
1075

1076
/* ======================== leaky relu register  ============================ */
1077 1078 1079
REGISTER_OPERATOR(
    leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
1080 1081 1082 1083
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1084
    ops::ActFwdInplaceInferer);
1085
REGISTER_OPERATOR(leaky_relu_grad, ops::ActivationOpGrad,
1086
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1087 1088
                  ops::LeakyReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::LeakyReluDoubleGradMaker<paddle::imperative::OpBase>);
1089 1090
REGISTER_OPERATOR(
    leaky_relu_grad_grad,
1091
    ops::ActivationOpDoubleGrad2<ops::LeakyReluGradFunctor<float>::FwdDeps()>,
1092
    ops::ActivationDoubleGradOpInplaceInferer);
1093

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
REGISTER_ACTIVATION_CPU_KERNEL(leaky_relu, LeakyRelu, LeakyReluFunctor,
                               LeakyReluGradFunctor);
REGISTER_OP_CPU_KERNEL(
    leaky_relu_grad_grad,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::LeakyReluGradGradFunctor<float>>,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::LeakyReluGradGradFunctor<double>>,
    ops::ActivationDoubleGradKernel<
        plat::CPUDeviceContext, ops::LeakyReluGradGradFunctor<plat::float16>>);
1104 1105
/* ========================================================================== */

D
Double_V 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114
/* ========================    elu  register     ============================ */
REGISTER_OPERATOR(
    elu, ops::ActivationOp, ops::ELUOpMaker, ops::ActivationOpInferVarType,
    ops::ActivationGradOpMaker<ops::ELUGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::ELUGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
    ops::ActFwdInplaceInferer);
REGISTER_OPERATOR(elu_grad, ops::ActivationOpGrad,
1115
                  ops::ActivationGradOpInplaceInferer,
D
Double_V 已提交
1116 1117 1118 1119 1120
                  ops::ELUDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ELUDoubleGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
    elu_grad_grad,
    ops::ActivationOpDoubleGrad<ops::ELUGradFunctor<float>::FwdDeps()>,
1121
    ops::ActivationDoubleGradOpInplaceInferer);
D
Double_V 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

REGISTER_ACTIVATION_CPU_KERNEL(elu, ELU, ELUFunctor, ELUGradFunctor);
REGISTER_OP_CPU_KERNEL(
    elu_grad_grad, ops::ELUDoubleGradKernel<plat::CPUDeviceContext,
                                            ops::ELUGradGradFunctor<float>>,
    ops::ELUDoubleGradKernel<plat::CPUDeviceContext,
                             ops::ELUGradGradFunctor<double>>,
    ops::ELUDoubleGradKernel<plat::CPUDeviceContext,
                             ops::ELUGradGradFunctor<plat::float16>>);

/* ========================================================================== */

L
lvmengsi 已提交
1134 1135 1136
/* ===========================   sqrt register  ============================= */
REGISTER_OPERATOR(
    sqrt, ops::ActivationOp, ops::SqrtOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1137 1138 1139 1140
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1141
    ops::ActFwdInplaceInferer);
L
lvmengsi 已提交
1142
REGISTER_OPERATOR(sqrt_grad, ops::ActivationOpGrad,
1143
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1144 1145
                  ops::SqrtDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SqrtDoubleGradMaker<paddle::imperative::OpBase>);
L
lvmengsi 已提交
1146 1147
REGISTER_OPERATOR(
    sqrt_grad_grad,
1148
    ops::ActivationOpDoubleGrad<ops::SqrtGradGradFunctor<float>::FwdDeps()>,
1149
    ops::ActivationDoubleGradOpInplaceInferer);
1150

L
lvmengsi 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
REGISTER_ACTIVATION_CPU_KERNEL(sqrt, Sqrt, SqrtFunctor, SqrtGradFunctor);
REGISTER_OP_CPU_KERNEL(
    sqrt_grad_grad, ops::SqrtDoubleGradKernel<plat::CPUDeviceContext,
                                              ops::SqrtGradGradFunctor<float>>,
    ops::SqrtDoubleGradKernel<plat::CPUDeviceContext,
                              ops::SqrtGradGradFunctor<double>>,
    ops::SqrtDoubleGradKernel<plat::CPUDeviceContext,
                              ops::SqrtGradGradFunctor<plat::float16>>);
/* ========================================================================== */

W
whs 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
/* ===========================   rsqrt register  =============================
 */
REGISTER_OPERATOR(
    rsqrt, ops::ActivationOp, ops::RsqrtOpMaker, ops::ActivationOpInferVarType,
    ops::ActivationGradOpMaker<ops::RsqrtGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::RsqrtGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
    ops::ActFwdInplaceInferer);
REGISTER_OPERATOR(rsqrt_grad, ops::ActivationOpGrad,
                  ops::ActivationGradOpInplaceInferer,
                  ops::RsqrtDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::RsqrtDoubleGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
    rsqrt_grad_grad,
    ops::ActivationOpDoubleGrad<ops::RsqrtGradGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInferer);

REGISTER_ACTIVATION_CPU_KERNEL(rsqrt, Rsqrt, RsqrtFunctor, RsqrtGradFunctor);
REGISTER_OP_CPU_KERNEL(
    rsqrt_grad_grad,
    ops::RsqrtDoubleGradKernel<plat::CPUDeviceContext,
                               ops::RsqrtGradGradFunctor<float>>,
    ops::RsqrtDoubleGradKernel<plat::CPUDeviceContext,
                               ops::RsqrtGradGradFunctor<double>>,
    ops::RsqrtDoubleGradKernel<plat::CPUDeviceContext,
                               ops::RsqrtGradGradFunctor<plat::float16>>);
/* ========================================================================== */

1190 1191 1192 1193
/* ==========================   square register  ============================ */
REGISTER_OPERATOR(
    square, ops::ActivationOp, ops::SquareOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
1194 1195 1196 1197
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1198
    ops::ActFwdInplaceInferer);
1199
REGISTER_OPERATOR(square_grad, ops::ActivationOpGrad,
1200
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1201 1202
                  ops::SquareDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SquareDoubleGradMaker<paddle::imperative::OpBase>);
1203 1204
REGISTER_OPERATOR(
    square_grad_grad,
1205
    ops::ActivationOpDoubleGrad<ops::SquareGradGradFunctor<float>::FwdDeps()>,
1206
    ops::ActivationDoubleGradOpInplaceInferer);
1207

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
REGISTER_OP_CPU_KERNEL(square,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::SquareFunctor<float>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::SquareFunctor<double>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::SquareFunctor<int>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::SquareFunctor<int64_t>>);
REGISTER_OP_CPU_KERNEL(
    square_grad, ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                                           ops::SquareGradFunctor<float>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::SquareGradFunctor<double>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::SquareGradFunctor<int>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::SquareGradFunctor<int64_t>>);
1226 1227 1228 1229 1230 1231 1232 1233

REGISTER_OP_CPU_KERNEL(
    square_grad_grad,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<float>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<double>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
1234 1235 1236 1237 1238
                                ops::SquareGradGradFunctor<plat::float16>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int64_t>>);
1239
/* ========================================================================== */
1240 1241 1242 1243 1244

/* ==========================   pow register  ============================ */

REGISTER_OPERATOR(
    pow, ops::PowOp, ops::PowOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1245 1246
    ops::PowGradOpMaker<paddle::framework::OpDesc>,
    ops::PowGradOpMaker<paddle::imperative::OpBase>,
1247
    std::conditional<ops::CanInplaceAct<ops::PowGradFunctor<float>>(),
1248
                     ops::ActFwdInplaceInferer, void>::type);
1249
REGISTER_OPERATOR(pow_grad, ops::PowOpGrad,
1250
                  ops::ActivationGradOpInplaceInferer);
1251 1252 1253

REGISTER_OP_CPU_KERNEL(
    pow, ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<float>>,
1254 1255 1256
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<double>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int64_t>>);
1257 1258 1259
REGISTER_OP_CPU_KERNEL(
    pow_grad,
    ops::PowGradKernel<plat::CPUDeviceContext, ops::PowGradFunctor<float>>,
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    ops::PowGradKernel<plat::CPUDeviceContext, ops::PowGradFunctor<double>>,
    ops::PowGradKernel<plat::CPUDeviceContext, ops::PowGradFunctor<int>>,
    ops::PowGradKernel<plat::CPUDeviceContext, ops::PowGradFunctor<int64_t>>);
/* ========================================================================== */

/* ==========================   exp register  ============================ */
REGISTER_OPERATOR(
    exp, ops::ActivationOp, ops::ExpOpMaker, ops::ActivationOpInferVarType,
    ops::ActivationGradOpMaker<ops::ExpGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::ExpGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
    std::conditional<ops::CanInplaceAct<ops::ExpGradFunctor<float>>(),
                     ops::ActFwdInplaceInferer, void>::type);
REGISTER_OPERATOR(exp_grad, ops::ActivationOpGrad,
1275
                  ops::ActivationGradOpInplaceInferer);
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

REGISTER_OP_CPU_KERNEL(exp,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::ExpFunctor<float>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::ExpFunctor<double>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::ExpFunctor<int>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::ExpFunctor<int64_t>>);
REGISTER_OP_CPU_KERNEL(
    exp_grad, ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                                        ops::ExpGradFunctor<float>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::ExpGradFunctor<double>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::ExpGradFunctor<int>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::ExpGradFunctor<int64_t>>);
/* ========================================================================== */
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
/* ==========================  Log register ==================================*/
REGISTER_OPERATOR(
    log, ops::ActivationOp, ops::LogOpMaker, ops::ActivationOpInferVarType,
    ops::ActivationGradOpMaker<ops::LogGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::LogGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
    ops::ActFwdInplaceInferer);
REGISTER_OPERATOR(log_grad, ops::ActivationOpGrad,
                  ops::ActivationGradOpInplaceInferer,
                  ops::LogDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::LogDoubleGradMaker<paddle::imperative::OpBase>);

REGISTER_OPERATOR(
    log_grad_grad,
    ops::ActivationOpDoubleGrad<ops::LogGradGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInferer);

REGISTER_ACTIVATION_CPU_KERNEL(log, Log, LogFunctor, LogGradFunctor);

REGISTER_OP_CPU_KERNEL(
    log_grad_grad, ops::LogDoubleGradKernel<plat::CPUDeviceContext,
                                            ops::LogGradGradFunctor<float>>,
    ops::LogDoubleGradKernel<plat::CPUDeviceContext,
                             ops::LogGradGradFunctor<double>>,
    ops::LogDoubleGradKernel<plat::CPUDeviceContext,
                             ops::LogGradGradFunctor<plat::float16>>);
/* ========================================================================== */

1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
/* ==========================  register checkpoint ===========================*/
REGISTER_OP_VERSION(leaky_relu)
    .AddCheckpoint(
        R"ROC(fix leaky_relu, bahavior changed when alpha < 0 or alpha > 1)ROC",
        paddle::framework::compatible::OpVersionDesc()
            .BugfixWithBehaviorChanged(
                "leaky_relu calculate formula before checkponit: out = max(x, "
                "alpha * x); after checkpoint: out = x if x > 0 else alpha * "
                "x"));

REGISTER_OP_VERSION(hard_shrink)
    .AddCheckpoint(
        R"ROC(fix hard_shrink, bahavior changed when threshold<0)ROC",
        paddle::framework::compatible::OpVersionDesc()
            .BugfixWithBehaviorChanged(
                "hard_shrink calculate formula before checkponit: out = x * "
                "((x < -threshold) + (x > threshold)); after checkpoint: out = "
                "x * (((x < -threshold) + (x > threshold)) > 0)"));

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
REGISTER_OP_VERSION(softplus)
    .AddCheckpoint(
        R"ROC(add new attributes [beta] and [threshold], and the formula is changed to "
         " softplus(x) = \\frac{1}{beta} * \\log(1 + e^{beta * x}) \\\\ \\text{For numerical"
         " stability, the implementation reverts to the linear function when: beta * x > threshold.})ROC",
        paddle::framework::compatible::OpVersionDesc()
            .NewAttr("beta", "The beta value of the new formula", 1.0f)
            .NewAttr("threshold", "The threshold value of the new formula",
                     20.0f));

1355
/* ========================================================================== */