activation_op.cc 50.3 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"
Q
qijun 已提交
27

A
Adam 已提交
28 29
DECLARE_bool(use_mkldnn);

Q
qijun 已提交
30 31 32
namespace paddle {
namespace operators {

33 34
using paddle::framework::Tensor;

35 36 37 38 39
template <typename GradFunctor>
static constexpr bool CanInplaceAct() {
  return GradFunctor::FwdDeps() == kDepOut || GradFunctor::FwdDeps() == kNoDeps;
}

40 41 42 43 44
#define REGISTER_ACTIVATION_OP_MAKER(OP_NAME, OP_COMMENT)                    \
  class OP_NAME##OpMaker                                                     \
      : public ::paddle::framework::OpProtoAndCheckerMaker {                 \
   public:                                                                   \
    void Make() override {                                                   \
45 46 47 48 49
      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.");     \
50 51 52 53 54 55 56 57 58
      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 已提交
59
  }
D
dzhwinter 已提交
60

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

 protected:
67
  void Apply(GradOpPtr<T> op) const override {
H
hong 已提交
68 69 70 71
    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());
72

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

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

88 89 90 91
framework::OpKernelType GetKernelType(const framework::ExecutionContext& ctx,
                                      const framework::OperatorWithKernel& oper,
                                      const std::string& name) {
  framework::LibraryType library{framework::LibraryType::kPlain};
M
mozga-intel 已提交
92
  framework::DataLayout layout = framework::DataLayout::kAnyLayout;
93
  auto data_type = oper.IndicateVarDataType(ctx, name);
94 95 96 97 98 99 100 101 102 103
// 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
104 105 106
#ifdef PADDLE_WITH_MKLDNN
  auto it = oper.Attrs().find("use_mkldnn");
  if (library == framework::LibraryType::kPlain && it != oper.Attrs().end() &&
107
      oper.CanMKLDNNBeUsed(ctx, data_type)) {
108
    library = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
109
    layout = framework::DataLayout::kMKLDNN;
110 111
  }
#endif
112
  return framework::OpKernelType(data_type, ctx.GetPlace(), layout, library);
113 114
}

Q
qijun 已提交
115 116 117 118
class ActivationOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

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

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

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

Q
qijun 已提交
141 142 143 144
class ActivationOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

145
  void InferShape(framework::InferShapeContext* ctx) const override {
146 147 148
    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 已提交
149
  }
150

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

D
dzhwinter 已提交
158
UNUSED constexpr char SigmoidDoc[] = R"DOC(
159
Sigmoid Activation Operator
K
Kexin Zhao 已提交
160

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

D
dzhwinter 已提交
163
)DOC";
Q
qijun 已提交
164

D
dzhwinter 已提交
165
UNUSED constexpr char LogSigmoidDoc[] = R"DOC(
166
Logsigmoid Activation Operator
K
Kexin Zhao 已提交
167

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

D
dzhwinter 已提交
170
)DOC";
171

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

175
$$out = e^x$$
K
Kexin Zhao 已提交
176

D
dzhwinter 已提交
177
)DOC";
Q
qijun 已提交
178

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

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

D
dzhwinter 已提交
184
)DOC";
K
Kexin Zhao 已提交
185

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

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

D
dzhwinter 已提交
191
)DOC";
192

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

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

D
dzhwinter 已提交
198
)DOC";
K
Kexin Zhao 已提交
199

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

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

205 206
**Note**:
  input value must be greater than or equal to zero.
K
Kexin Zhao 已提交
207

D
dzhwinter 已提交
208
)DOC";
209

Z
zhoukunsheng 已提交
210 211 212 213 214
UNUSED constexpr char RsqrtDoc[] = R"DOC(
Rsqrt Activation Operator.

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

215
$$out = \\frac{1}{\\sqrt{x}}$$
Z
zhoukunsheng 已提交
216 217 218

)DOC";

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

N
Noel 已提交
222
$$out = \\lceil x \\rceil$$
D
dzhwinter 已提交
223

D
dzhwinter 已提交
224
)DOC";
D
dzhwinter 已提交
225

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

N
Noel 已提交
229
$$out = \\lfloor x \\rfloor$$
D
dzhwinter 已提交
230

D
dzhwinter 已提交
231
)DOC";
D
dzhwinter 已提交
232

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

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

238
$$out = cos(x)$$
C
add cos  
chengduoZH 已提交
239

D
dzhwinter 已提交
240
)DOC";
C
add cos  
chengduoZH 已提交
241

J
joejiong 已提交
242 243 244 245 246 247 248 249 250
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 已提交
251
UNUSED constexpr char SinDoc[] = R"DOC(
C
add sin  
chengduoZH 已提交
252 253
Sine Activation Operator.

254
$$out = sin(x)$$
C
add sin  
chengduoZH 已提交
255

D
dzhwinter 已提交
256
)DOC";
C
add sin  
chengduoZH 已提交
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271
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 已提交
272
UNUSED constexpr char RoundDoc[] = R"DOC(
273
The OP rounds the values in the input to the nearest integer value.
D
dzhwinter 已提交
274

N
Noel 已提交
275
.. code-block:: text
276 277 278 279 280 281 282 283

  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 已提交
284

D
dzhwinter 已提交
285
)DOC";
D
dzhwinter 已提交
286

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

290
$$out = \\frac{1}{x}$$
K
Kexin Zhao 已提交
291

D
dzhwinter 已提交
292
)DOC";
293

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

297
$$out = \ln(x)$$
K
Kexin Zhao 已提交
298 299 300

Natural logarithm of x.

D
dzhwinter 已提交
301 302
)DOC";

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

$$out = \log_2x$$

logarithm of x base to 2.

)DOC";

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

$$out = \log_10_x$$

logarithm of x base to 10.

)DOC";

321 322 323 324 325 326 327 328 329
UNUSED constexpr char Log1pDoc[] = R"DOC(
Log Activation Operator.

$out = \ln(x+1)$

Natural logarithm of x.

)DOC";

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

333
$$out = x^2$$
334

D
dzhwinter 已提交
335 336
)DOC";

D
dzhwinter 已提交
337
UNUSED constexpr char SoftsignDoc[] = R"DOC(
D
dzhwinter 已提交
338 339
Softsign Activation Operator.

340
$$out = \\frac{x}{1 + \|x\|}$$
D
dzhwinter 已提交
341 342 343

)DOC";

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

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

T
tink2123 已提交
354 355 356
)DOC");
  }
};
357

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

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

T
tink2123 已提交
370 371 372
)DOC");
  }
};
373

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

384
$$out = \tan^{-1}(x)$$
385

T
tink2123 已提交
386 387 388
)DOC");
  }
};
389

D
dzhwinter 已提交
390
class LeakyReluOpMaker : public framework::OpProtoAndCheckerMaker {
391
 public:
Y
Yu Yang 已提交
392
  void Make() override {
W
Wilber 已提交
393 394 395 396 397 398 399 400
    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 已提交
401 402 403
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
404
    AddComment(R"DOC(
D
dzhwinter 已提交
405
LeakyRelu Activation Operator.
K
Kexin Zhao 已提交
406

W
Wilber 已提交
407
$$out = \max(x, \alpha * x)$$
K
Kexin Zhao 已提交
408 409

)DOC");
410 411 412
  }
};

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

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

)DOC");
K
kexinzhao 已提交
460 461 462
  }
};

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

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

)DOC");
482 483 484
  }
};

485 486
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
487
  void Make() override {
488 489 490 491 492 493
    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``.");
494 495 496 497
    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 已提交
498
    AddComment(R"DOC(
K
kexinzhao 已提交
499
BRelu Activation Operator.
K
Kexin Zhao 已提交
500

501
$$out = \min(\max(x, t_{min}), t_{max})$$
K
Kexin Zhao 已提交
502 503

)DOC");
504 505 506 507 508
  }
};

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

517
$$out = \ln(1 + \exp(\max(\min(x, threshold), -threshold)))$$
K
Kexin Zhao 已提交
518 519

)DOC");
520 521 522
  }
};

523 524
class ELUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
525
  void Make() override {
526 527 528 529 530 531
    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``.");
532
    AddAttr<float>("alpha", "The alpha value of ELU").SetDefault(1.0f);
533
    AddComment(R"DOC(
K
kexinzhao 已提交
534
ELU Activation Operator.
K
Kexin Zhao 已提交
535 536 537 538

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

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

)DOC");
542 543 544
  }
};

545 546
class Relu6OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
547
  void Make() override {
Z
zhupengyang 已提交
548 549 550 551 552 553 554 555
    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. ")
556
        .SetDefault(6.0f);
A
Adam 已提交
557 558 559
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
560
    AddComment(R"DOC(
K
kexinzhao 已提交
561
Relu6 Activation Operator.
K
Kexin Zhao 已提交
562

563
$$out = \min(\max(0, x), threshold)$$
K
Kexin Zhao 已提交
564 565

)DOC");
566 567 568
  }
};

569 570
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
571
  void Make() override {
572
    AddInput("X", "Input of Pow operator");
573 574 575 576 577
    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 已提交
578
    AddOutput("Out", "Output of Pow operator");
579
    AddAttr<float>("factor", "The exponential factor of Pow").SetDefault(1.0f);
K
Kexin Zhao 已提交
580
    AddComment(R"DOC(
K
kexinzhao 已提交
581
Pow Activation Operator.
K
Kexin Zhao 已提交
582

583
$$out = x^{factor}$$
K
Kexin Zhao 已提交
584 585

)DOC");
586 587 588 589 590
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
591
  void Make() override {
592 593
    AddInput("X",
             "Input of STanh operator."
N
Noel 已提交
594
             " A Tensor with type float32, float64.");
595 596 597
    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);
598 599
    AddAttr<float>("scale_b", "The scale parameter of b for the input")
        .SetDefault(1.7159f);
K
Kexin Zhao 已提交
600
    AddComment(R"DOC(
K
kexinzhao 已提交
601
STanh Activation Operator.
K
Kexin Zhao 已提交
602

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

)DOC");
Q
qijun 已提交
606 607 608
  }
};

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

Y
yuyang18 已提交
620
..  math::
K
Kexin Zhao 已提交
621

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

630 631
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
632
  void Make() override {
633 634 635 636 637
    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. ")
638
        .SetDefault(0.2f);
639 640 641
    AddAttr<float>(
        "offset",
        "The offset of the linear approximation of sigmoid. Default is 0.5. ")
642
        .SetDefault(0.5f);
643
    AddComment(R"DOC(
K
kexinzhao 已提交
644
HardSigmoid Activation Operator.
645

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

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

K
Kexin Zhao 已提交
651
)DOC");
652 653 654
  }
};

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

667
$$out = \\frac{x}{1 + e^{- \beta \ x}}$$
A
Abhinav Arora 已提交
668 669 670 671 672

)DOC");
  }
};

H
huangjun12 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
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).

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

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 已提交
699 700 701 702 703 704 705
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 已提交
706
REGISTER_ACTIVATION_OP_MAKER(Rsqrt, RsqrtDoc);
D
dzhwinter 已提交
707 708 709
REGISTER_ACTIVATION_OP_MAKER(Ceil, CeilDoc);
REGISTER_ACTIVATION_OP_MAKER(Floor, FloorDoc);
REGISTER_ACTIVATION_OP_MAKER(Cos, CosDoc);
J
joejiong 已提交
710
REGISTER_ACTIVATION_OP_MAKER(Tan, TanDoc);
D
dzhwinter 已提交
711
REGISTER_ACTIVATION_OP_MAKER(Sin, SinDoc);
712 713
REGISTER_ACTIVATION_OP_MAKER(Sinh, SinhDoc);
REGISTER_ACTIVATION_OP_MAKER(Cosh, CoshDoc);
D
dzhwinter 已提交
714 715 716
REGISTER_ACTIVATION_OP_MAKER(Round, RoundDoc);
REGISTER_ACTIVATION_OP_MAKER(Reciprocal, ReciprocalDoc);
REGISTER_ACTIVATION_OP_MAKER(Log, LogDoc);
J
joejiong 已提交
717
REGISTER_ACTIVATION_OP_MAKER(Log2, Log2Doc);
J
joejiong 已提交
718
REGISTER_ACTIVATION_OP_MAKER(Log10, Log10Doc);
719
REGISTER_ACTIVATION_OP_MAKER(Log1p, Log1pDoc);
D
dzhwinter 已提交
720 721 722
REGISTER_ACTIVATION_OP_MAKER(Square, SquareDoc);
REGISTER_ACTIVATION_OP_MAKER(Softsign, SoftsignDoc);

723
template <ActBwdOpFwdDeps kDepValue>
724 725 726 727 728
class ActivationOpDoubleGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
729
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
730
      if (ctx->HasOutput("DX")) {
731 732 733
        ctx->ShareDim("X", "DX");
        ctx->ShareLoD("X", "DX");
      }
734
      if (ctx->HasOutput("DDOut")) {
735 736 737
        ctx->ShareDim("X", "DDOut");
        ctx->ShareLoD("X", "DDOut");
      }
738
    }
739
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
740
      if (ctx->HasOutput("DOut")) {
741 742 743
        ctx->ShareDim("Out", "DOut");
        ctx->ShareLoD("Out", "DOut");
      }
744 745 746 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
      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")) {
772 773 774
        ctx->ShareDim("Out", "DDOut");
        ctx->ShareLoD("Out", "DDOut");
      }
775 776 777 778 779 780 781 782 783 784
    }
  }

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

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
template <typename T>
class TanhDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
 public:
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("tanh_grad_grad");
    // input1: Out
    op->SetInput("Out", this->Input("Out"));
    // input2: ddx
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetInput("DOut", this->Input(framework::GradVarName("Out")));
    op->SetAttrMap(this->Attrs());
    // output: ddy
    op->SetOutput("DOutNew", this->InputGrad("Out"));
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
  }
};

805 806
// ReluGrad: dx = dy if y >= 0 else 0
// ReluGradGrad: ddy = ddx if y >= 0 else 0
H
hong 已提交
807 808
template <typename T>
class ReluDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
809
 public:
H
hong 已提交
810
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
811 812

 protected:
813
  void Apply(GradOpPtr<T> op) const override {
814 815
    op->SetType("relu_grad_grad");
    // input1: Out
H
hong 已提交
816
    op->SetInput("Out", this->Input("Out"));
Q
qingqing01 已提交
817
    // input2: ddx
H
hong 已提交
818 819
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
820
    // output: ddy
H
hong 已提交
821
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
822 823 824
  }
};

825 826
// leaky_relu Grad: dx=dy if x>=0 else alpha * dy
// leaky_relu GradGrad: ddy=ddx if x>=0 else alpha * ddx
H
hong 已提交
827
template <typename T>
828
class LeakyReluDoubleGradMaker
H
hong 已提交
829
    : public ::paddle::framework::SingleGradOpMaker<T> {
830
 public:
H
hong 已提交
831
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
832 833

 protected:
834
  void Apply(GradOpPtr<T> op) const override {
835
    op->SetType("leaky_relu_grad_grad");
836 837
    // input1: X
    op->SetInput("X", this->Input("X"));
838
    // X@GRAD@GRAD: ddx
H
hong 已提交
839 840
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
841
    // Out@GRAD@GRAD: ddy
H
hong 已提交
842
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
843 844 845
  }
};

D
Double_V 已提交
846 847 848 849 850 851 852 853
// 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:
854
  void Apply(GradOpPtr<T> op) const override {
D
Double_V 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868
    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 已提交
869 870
// sqrt Grad: dx = 0.5 * dy / y
// sqrt GradGrad: ddy = 0.5 * ddx / y, dy = -1 * dx * ddx
H
hong 已提交
871 872
template <typename T>
class SqrtDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
L
lvmengsi 已提交
873
 public:
H
hong 已提交
874
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
L
lvmengsi 已提交
875 876

 protected:
877
  void Apply(GradOpPtr<T> op) const override {
L
lvmengsi 已提交
878
    op->SetType("sqrt_grad_grad");
H
hong 已提交
879 880 881 882 883 884
    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 已提交
885 886 887
  }
};

W
whs 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
// 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")));
  }
};

907 908
// square Grad: dx=2x*dy
// square GradGrad: ddy=2x*ddx, dx=2dy*ddx
H
hong 已提交
909 910
template <typename T>
class SquareDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
911
 public:
H
hong 已提交
912
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
913 914

 protected:
915
  void Apply(GradOpPtr<T> op) const override {
916
    op->SetType("square_grad_grad");
H
hong 已提交
917
    op->SetInput("X", this->Input("X"));
918
    // Out@GRAD: dy
H
hong 已提交
919
    op->SetInput("DOut", this->Input(framework::GradVarName("Out")));
920
    // X@GRAD@GRAD: ddx
H
hong 已提交
921
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
922

H
hong 已提交
923
    op->SetAttrMap(this->Attrs());
924 925

    // X@GRAD: dx
H
hong 已提交
926
    op->SetOutput("DX", this->InputGrad("X"));
927
    // Out@GRAD@GRAD: ddy
H
hong 已提交
928
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
929 930 931
  }
};

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
// 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")));
  }
};

954
DECLARE_INPLACE_OP_INFERER(ActivationGradOpInplaceInferer,
955 956
                           {framework::GradVarName("Out"),
                            framework::GradVarName("X")});
957
DECLARE_INPLACE_OP_INFERER(ActivationDoubleGradOpInplaceInferer,
958
                           {"DDX", "DDOut"});
959

H
hong 已提交
960 961
template <typename T>
class PowGradOpMaker : public framework::SingleGradOpMaker<T> {
962
 public:
H
hong 已提交
963
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
964 965

 protected:
966
  void Apply(GradOpPtr<T> op) const override {
967
    op->SetType("pow_grad");
H
hong 已提交
968 969 970 971 972
    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());
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 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  }
};
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());
  }
};
1027
DECLARE_INPLACE_OP_INFERER(ActFwdInplaceInferer, {"X", "Out"});
Q
qijun 已提交
1028 1029 1030 1031
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
1032
namespace plat = paddle::platform;
1033

1034 1035 1036 1037
#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 已提交
1038 1039 1040 1041
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::framework::OpDesc>,                \
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::imperative::OpBase>,               \
1042
      std::conditional<ops::CanInplaceAct<ops::grad_functor<float>>(),      \
1043
                       ops::ActFwdInplaceInferer, void>::type);             \
1044
  REGISTER_OPERATOR(KERNEL_TYPE##_grad, ops::ActivationOpGrad,              \
1045
                    ops::ActivationGradOpInplaceInferer);
1046 1047 1048

#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, op_name, functor,        \
                                       grad_functor)                      \
Q
QI JUN 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  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 已提交
1059
                                ops::grad_functor<double>>);
1060

1061 1062
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_OP);
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_CPU_KERNEL);
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
/* ==========================    tanh register  ============================= */
REGISTER_OPERATOR(
    tanh, ops::ActivationOp, ops::TanhOpMaker, ops::ActivationOpInferVarType,
    ops::ActivationGradOpMaker<ops::TanhGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::TanhGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
    std::conditional<ops::CanInplaceAct<ops::TanhGradFunctor<float>>(),
                     ops::ActFwdInplaceInferer, void>::type);
REGISTER_OPERATOR(tanh_grad, ops::ActivationOpGrad,
                  ops::ActivationGradOpInplaceInferer,
                  ops::TanhDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::TanhDoubleGradMaker<paddle::imperative::OpBase>)
REGISTER_OPERATOR(
    tanh_grad_grad,
    ops::ActivationOpDoubleGrad<ops::TanhGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInferer);

REGISTER_ACTIVATION_CPU_KERNEL(tanh, Tanh, TanhFunctor, TanhGradFunctor);
REGISTER_OP_CPU_KERNEL(
    tanh_grad_grad, ops::TanhDoubleGradKernel<plat::CPUDeviceContext,
                                              ops::TanhGradGradFunctor<float>>,
    ops::TanhDoubleGradKernel<plat::CPUDeviceContext,
                              ops::TanhGradGradFunctor<double>>,
    ops::TanhDoubleGradKernel<plat::CPUDeviceContext,
                              ops::TanhGradGradFunctor<plat::float16>>);
/* ========================================================================== */

1092
/* ==========================    relu register  ============================= */
1093 1094
REGISTER_OPERATOR(
    relu, ops::ActivationOp, ops::ReluOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1095 1096 1097 1098
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1099
    ops::ActFwdInplaceInferer);
1100
REGISTER_OPERATOR(relu_grad, ops::ActivationOpGrad,
1101
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1102 1103
                  ops::ReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ReluDoubleGradMaker<paddle::imperative::OpBase>);
1104 1105
REGISTER_OPERATOR(
    relu_grad_grad,
1106
    ops::ActivationOpDoubleGrad2<ops::ReluGradFunctor<float>::FwdDeps()>,
1107
    ops::ActivationDoubleGradOpInplaceInferer);
1108

1109
REGISTER_ACTIVATION_CPU_KERNEL(relu, Relu, ReluCPUFunctor, ReluGradFunctor);
1110 1111 1112 1113 1114 1115 1116 1117 1118

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>>);
1119
/* ========================================================================== */
1120

1121
/* ======================== leaky relu register  ============================ */
1122 1123 1124
REGISTER_OPERATOR(
    leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
1125 1126 1127 1128
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1129
    ops::ActFwdInplaceInferer);
1130
REGISTER_OPERATOR(leaky_relu_grad, ops::ActivationOpGrad,
1131
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1132 1133
                  ops::LeakyReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::LeakyReluDoubleGradMaker<paddle::imperative::OpBase>);
1134 1135
REGISTER_OPERATOR(
    leaky_relu_grad_grad,
1136
    ops::ActivationOpDoubleGrad2<ops::LeakyReluGradFunctor<float>::FwdDeps()>,
1137
    ops::ActivationDoubleGradOpInplaceInferer);
1138

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
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>>);
1149 1150
/* ========================================================================== */

D
Double_V 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159
/* ========================    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,
1160
                  ops::ActivationGradOpInplaceInferer,
D
Double_V 已提交
1161 1162 1163 1164 1165
                  ops::ELUDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ELUDoubleGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
    elu_grad_grad,
    ops::ActivationOpDoubleGrad<ops::ELUGradFunctor<float>::FwdDeps()>,
1166
    ops::ActivationDoubleGradOpInplaceInferer);
D
Double_V 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

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 已提交
1179 1180 1181
/* ===========================   sqrt register  ============================= */
REGISTER_OPERATOR(
    sqrt, ops::ActivationOp, ops::SqrtOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1182 1183 1184 1185
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1186
    ops::ActFwdInplaceInferer);
L
lvmengsi 已提交
1187
REGISTER_OPERATOR(sqrt_grad, ops::ActivationOpGrad,
1188
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1189 1190
                  ops::SqrtDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SqrtDoubleGradMaker<paddle::imperative::OpBase>);
L
lvmengsi 已提交
1191 1192
REGISTER_OPERATOR(
    sqrt_grad_grad,
1193
    ops::ActivationOpDoubleGrad<ops::SqrtGradGradFunctor<float>::FwdDeps()>,
1194
    ops::ActivationDoubleGradOpInplaceInferer);
1195

L
lvmengsi 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
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 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
/* ===========================   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>>);
/* ========================================================================== */

1235 1236 1237 1238
/* ==========================   square register  ============================ */
REGISTER_OPERATOR(
    square, ops::ActivationOp, ops::SquareOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
1239 1240 1241 1242
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1243
    ops::ActFwdInplaceInferer);
1244
REGISTER_OPERATOR(square_grad, ops::ActivationOpGrad,
1245
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1246 1247
                  ops::SquareDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SquareDoubleGradMaker<paddle::imperative::OpBase>);
1248 1249
REGISTER_OPERATOR(
    square_grad_grad,
1250
    ops::ActivationOpDoubleGrad<ops::SquareGradGradFunctor<float>::FwdDeps()>,
1251
    ops::ActivationDoubleGradOpInplaceInferer);
1252

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
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>>);
1271 1272 1273 1274 1275 1276 1277 1278

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,
1279 1280 1281 1282 1283
                                ops::SquareGradGradFunctor<plat::float16>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int64_t>>);
1284
/* ========================================================================== */
1285 1286 1287 1288 1289

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

REGISTER_OPERATOR(
    pow, ops::PowOp, ops::PowOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1290 1291
    ops::PowGradOpMaker<paddle::framework::OpDesc>,
    ops::PowGradOpMaker<paddle::imperative::OpBase>,
1292
    std::conditional<ops::CanInplaceAct<ops::PowGradFunctor<float>>(),
1293
                     ops::ActFwdInplaceInferer, void>::type);
1294
REGISTER_OPERATOR(pow_grad, ops::PowOpGrad,
1295
                  ops::ActivationGradOpInplaceInferer);
1296 1297 1298

REGISTER_OP_CPU_KERNEL(
    pow, ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<float>>,
1299 1300 1301
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<double>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int64_t>>);
1302 1303 1304
REGISTER_OP_CPU_KERNEL(
    pow_grad,
    ops::PowGradKernel<plat::CPUDeviceContext, ops::PowGradFunctor<float>>,
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    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,
1320
                  ops::ActivationGradOpInplaceInferer);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340

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>>);
/* ========================================================================== */
1341

1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
/* ==========================  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>>);
/* ========================================================================== */

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
/* ==========================  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)"));

1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
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));

1400
/* ========================================================================== */