activation_op.cc 42.7 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/operators/common_infer_shape_functions.h"
24
#include "paddle/fluid/operators/mkldnn/mkldnn_activation_op.h"
D
dzhwinter 已提交
25
#include "paddle/fluid/platform/port.h"
26 27 28
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/cudnn_helper.h"
#endif
Q
qijun 已提交
29

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

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

35 36
using paddle::framework::Tensor;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

D
dzhwinter 已提交
172
)DOC";
173

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

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

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

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

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

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

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

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

D
dzhwinter 已提交
193
)DOC";
194

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

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

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

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

205
.. math:: out=\\sqrt{x}=x^{1/2}
206

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

D
dzhwinter 已提交
210
)DOC";
211

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

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

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

)DOC";

D
dzhwinter 已提交
221
UNUSED constexpr char AbsDoc[] = R"DOC(
K
kexinzhao 已提交
222
Abs Activation Operator.
K
Kexin Zhao 已提交
223

224
$$out = |x|$$
K
Kexin Zhao 已提交
225

D
dzhwinter 已提交
226
)DOC";
227

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

231
$$out = \\left \\lceil x \\right \\rceil$$
D
dzhwinter 已提交
232

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

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

238
$$out = \\left \\lfloor x \\right \\rfloor$$
D
dzhwinter 已提交
239

D
dzhwinter 已提交
240
)DOC";
D
dzhwinter 已提交
241

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

Y
Yang Zhang 已提交
245 246 247
Input range is `(-inf, inf)` and output range is `[-1,1]`.
Return `nan` if input is out of boundary.

248
$$out = cos(x)$$
C
add cos  
chengduoZH 已提交
249

D
dzhwinter 已提交
250
)DOC";
C
add cos  
chengduoZH 已提交
251

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

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

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

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

276 277 278 279 280 281 282 283 284
.. code-block:: python

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

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

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

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

D
dzhwinter 已提交
293
)DOC";
294

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

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

Natural logarithm of x.

D
dzhwinter 已提交
302 303
)DOC";

304 305 306 307 308 309 310 311 312
UNUSED constexpr char Log1pDoc[] = R"DOC(
Log Activation Operator.

$out = \ln(x+1)$

Natural logarithm of x.

)DOC";

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

316
$$out = x^2$$
317

D
dzhwinter 已提交
318 319
)DOC";

D
dzhwinter 已提交
320
UNUSED constexpr char SoftplusDoc[] = R"DOC(
D
dzhwinter 已提交
321 322
Softplus Activation Operator.

323
$$out = \ln(1 + e^{x})$$
D
dzhwinter 已提交
324 325 326

)DOC";

D
dzhwinter 已提交
327
UNUSED constexpr char SoftsignDoc[] = R"DOC(
D
dzhwinter 已提交
328 329
Softsign Activation Operator.

330
$$out = \\frac{x}{1 + \|x\|}$$
D
dzhwinter 已提交
331 332 333

)DOC";

T
tink2123 已提交
334 335 336 337 338 339
class AcosOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of acos operator");
    AddOutput("Out", "Output of acos operator");
    AddComment(R"DOC(
340 341
Arccosine Activation Operator.

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

T
tink2123 已提交
344 345 346
)DOC");
  }
};
347

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

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

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

T
tink2123 已提交
362 363 364 365 366 367
class AtanOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of atan operator");
    AddOutput("Out", "Output of atan operator");
    AddComment(R"DOC(
368
Arctangent Operator.
369

370
$$out = \tan^{-1}(x)$$
371

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

D
dzhwinter 已提交
376
class LeakyReluOpMaker : public framework::OpProtoAndCheckerMaker {
377
 public:
Y
Yu Yang 已提交
378
  void Make() override {
W
Wilber 已提交
379 380 381 382 383 384 385 386
    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 已提交
387 388 389
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
390
    AddComment(R"DOC(
D
dzhwinter 已提交
391
LeakyRelu Activation Operator.
K
Kexin Zhao 已提交
392

W
Wilber 已提交
393
$$out = \max(x, \alpha * x)$$
K
Kexin Zhao 已提交
394 395

)DOC");
396 397 398
  }
};

D
dzhwinter 已提交
399
class SoftShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
K
kexinzhao 已提交
400
 public:
Y
Yu Yang 已提交
401
  void Make() override {
D
dzhwinter 已提交
402 403 404
    AddInput("X", "Input of Softshrink operator");
    AddOutput("Out", "Output of Softshrink operator");
    AddAttr<float>("lambda", "non-negative offset").SetDefault(0.5f);
K
Kexin Zhao 已提交
405
    AddComment(R"DOC(
406 407 408
:strong:`Softshrink Activation Operator`

..  math::
409
    out = \begin{cases}
410 411 412 413
         x - \lambda, \text{if } x > \lambda \\
         x + \lambda, \text{if } x < -\lambda \\
         0,  \text{otherwise}
         \end{cases}
K
Kexin Zhao 已提交
414 415

)DOC");
K
kexinzhao 已提交
416 417 418
  }
};

D
dzhwinter 已提交
419
class HardShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
420
 public:
Y
Yu Yang 已提交
421
  void Make() override {
D
dzhwinter 已提交
422 423
    AddInput("X", "Input of HardShrink operator");
    AddOutput("Out", "Output of HardShrink operator");
Y
yuyang18 已提交
424 425
    AddAttr<float>("threshold",
                   "The value of threshold for HardShrink. [default: 0.5]")
D
dzhwinter 已提交
426
        .SetDefault(0.5f);
K
Kexin Zhao 已提交
427
    AddComment(R"DOC(
Y
yuyang18 已提交
428
:strong:`HardShrink activation operator`
K
Kexin Zhao 已提交
429

Y
yuyang18 已提交
430 431 432 433 434 435
..  math::
    out = \begin{cases}
            x, \text{if } x > \lambda \\
            x, \text{if } x < -\lambda \\
            0,  \text{otherwise}
          \end{cases}
K
Kexin Zhao 已提交
436 437

)DOC");
438 439 440
  }
};

441 442
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
443
  void Make() override {
444 445 446 447 448 449
    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``.");
450 451 452 453
    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 已提交
454
    AddComment(R"DOC(
K
kexinzhao 已提交
455
BRelu Activation Operator.
K
Kexin Zhao 已提交
456

457
$$out = \min(\max(x, t_{min}), t_{max})$$
K
Kexin Zhao 已提交
458 459

)DOC");
460 461 462 463 464
  }
};

class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
465
  void Make() override {
466
    AddInput("X", "Input of SoftRelu operator");
F
fengjiayi 已提交
467
    AddOutput("Out", "Output of SoftRelu operator");
468 469
    AddAttr<float>("threshold", "The threshold value of SoftRelu")
        .SetDefault(40.0f);
K
Kexin Zhao 已提交
470
    AddComment(R"DOC(
K
kexinzhao 已提交
471
SoftRelu Activation Operator.
K
Kexin Zhao 已提交
472

473
$$out = \ln(1 + \exp(\max(\min(x, threshold), -threshold)))$$
K
Kexin Zhao 已提交
474 475

)DOC");
476 477 478
  }
};

479 480
class ELUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
481
  void Make() override {
482 483 484 485 486 487
    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``.");
488
    AddAttr<float>("alpha", "The alpha value of ELU").SetDefault(1.0f);
489
    AddComment(R"DOC(
K
kexinzhao 已提交
490
ELU Activation Operator.
K
Kexin Zhao 已提交
491 492 493 494

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

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

)DOC");
498 499 500
  }
};

501 502
class Relu6OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
503
  void Make() override {
Z
zhupengyang 已提交
504 505 506 507 508 509 510 511
    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. ")
512
        .SetDefault(6.0f);
A
Adam 已提交
513 514 515
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
516
    AddComment(R"DOC(
K
kexinzhao 已提交
517
Relu6 Activation Operator.
K
Kexin Zhao 已提交
518

519
$$out = \min(\max(0, x), threshold)$$
K
Kexin Zhao 已提交
520 521

)DOC");
522 523 524
  }
};

525 526
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
527
  void Make() override {
528
    AddInput("X", "Input of Pow operator");
529 530 531 532 533
    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 已提交
534
    AddOutput("Out", "Output of Pow operator");
535
    AddAttr<float>("factor", "The exponential factor of Pow").SetDefault(1.0f);
K
Kexin Zhao 已提交
536
    AddComment(R"DOC(
K
kexinzhao 已提交
537
Pow Activation Operator.
K
Kexin Zhao 已提交
538

539
$$out = x^{factor}$$
K
Kexin Zhao 已提交
540 541

)DOC");
542 543 544 545 546
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
547
  void Make() override {
548 549 550 551 552 553
    AddInput("X",
             "Input of STanh operator."
             " A LoDTensor or Tensor with type float32, float64.");
    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);
554 555
    AddAttr<float>("scale_b", "The scale parameter of b for the input")
        .SetDefault(1.7159f);
K
Kexin Zhao 已提交
556
    AddComment(R"DOC(
K
kexinzhao 已提交
557
STanh Activation Operator.
K
Kexin Zhao 已提交
558

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

)DOC");
Q
qijun 已提交
562 563 564
  }
};

565 566
class ThresholdedReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
567
  void Make() override {
568
    AddInput("X", "Input of ThresholdedRelu operator");
F
fengjiayi 已提交
569
    AddOutput("Out", "Output of ThresholdedRelu operator");
Y
yuyang18 已提交
570 571
    AddAttr<float>("threshold",
                   "The threshold location of activation. [default 1.0].")
572
        .SetDefault(1.0f);
K
Kexin Zhao 已提交
573
    AddComment(R"DOC(
Y
yuyang18 已提交
574
:strong:`ThresholdedRelu activation operator`
K
Kexin Zhao 已提交
575

Y
yuyang18 已提交
576
..  math::
K
Kexin Zhao 已提交
577

Y
yuyang18 已提交
578
    out = \begin{cases}
Y
yuyang18 已提交
579
             x,  \text{if } x > threshold \\
Y
yuyang18 已提交
580 581
             0,  \text{otherwise}
          \end{cases}
K
Kexin Zhao 已提交
582
)DOC");
583 584 585
  }
};

586 587
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
588
  void Make() override {
589 590 591 592 593
    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. ")
594
        .SetDefault(0.2f);
595 596 597
    AddAttr<float>(
        "offset",
        "The offset of the linear approximation of sigmoid. Default is 0.5. ")
598
        .SetDefault(0.5f);
599
    AddComment(R"DOC(
K
kexinzhao 已提交
600
HardSigmoid Activation Operator.
601

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

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

K
Kexin Zhao 已提交
607
)DOC");
608 609 610
  }
};

A
Abhinav Arora 已提交
611 612
class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
613
  void Make() override {
A
Abhinav Arora 已提交
614
    AddInput("X", "Input of Swish operator");
F
fengjiayi 已提交
615
    AddOutput("Out", "Output of Swish operator");
A
Abhinav Arora 已提交
616
    AddAttr<float>("beta", "Constant beta of swish operator").SetDefault(1.0f);
617 618 619
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
A
Abhinav Arora 已提交
620 621 622
    AddComment(R"DOC(
Swish Activation Operator.

623
$$out = \\frac{x}{1 + e^{- \beta \ x}}$$
A
Abhinav Arora 已提交
624 625 626 627 628

)DOC");
  }
};

H
huangjun12 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
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).

645
$$out = \frac{x * (min(max(0, x+offset), threshold))}{scale}$$
H
huangjun12 已提交
646 647 648 649 650 651 652 653 654

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 已提交
655 656 657 658 659 660 661
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 已提交
662
REGISTER_ACTIVATION_OP_MAKER(Rsqrt, RsqrtDoc);
D
dzhwinter 已提交
663 664 665 666 667
REGISTER_ACTIVATION_OP_MAKER(Abs, AbsDoc);
REGISTER_ACTIVATION_OP_MAKER(Ceil, CeilDoc);
REGISTER_ACTIVATION_OP_MAKER(Floor, FloorDoc);
REGISTER_ACTIVATION_OP_MAKER(Cos, CosDoc);
REGISTER_ACTIVATION_OP_MAKER(Sin, SinDoc);
668 669
REGISTER_ACTIVATION_OP_MAKER(Sinh, SinhDoc);
REGISTER_ACTIVATION_OP_MAKER(Cosh, CoshDoc);
D
dzhwinter 已提交
670 671 672
REGISTER_ACTIVATION_OP_MAKER(Round, RoundDoc);
REGISTER_ACTIVATION_OP_MAKER(Reciprocal, ReciprocalDoc);
REGISTER_ACTIVATION_OP_MAKER(Log, LogDoc);
673
REGISTER_ACTIVATION_OP_MAKER(Log1p, Log1pDoc);
D
dzhwinter 已提交
674 675 676 677
REGISTER_ACTIVATION_OP_MAKER(Square, SquareDoc);
REGISTER_ACTIVATION_OP_MAKER(Softplus, SoftplusDoc);
REGISTER_ACTIVATION_OP_MAKER(Softsign, SoftsignDoc);

678
template <ActBwdOpFwdDeps kDepValue>
679 680 681 682 683
class ActivationOpDoubleGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
684
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
685
      if (ctx->HasOutput("DX")) {
686 687 688
        ctx->ShareDim("X", "DX");
        ctx->ShareLoD("X", "DX");
      }
689
      if (ctx->HasOutput("DDOut")) {
690 691 692
        ctx->ShareDim("X", "DDOut");
        ctx->ShareLoD("X", "DDOut");
      }
693
    }
694
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
695
      if (ctx->HasOutput("DOut")) {
696 697 698
        ctx->ShareDim("Out", "DOut");
        ctx->ShareLoD("Out", "DOut");
      }
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
      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")) {
727 728 729
        ctx->ShareDim("Out", "DDOut");
        ctx->ShareLoD("Out", "DDOut");
      }
730 731 732 733 734 735 736 737 738 739
    }
  }

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

740 741 742 743
//
// ReluGrad: dx = dy if y >= 0 else 0
// ReluGradGrad: ddy = ddx if y >= 0 else 0
//
H
hong 已提交
744 745
template <typename T>
class ReluDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
746
 public:
H
hong 已提交
747
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
748 749

 protected:
750
  void Apply(GradOpPtr<T> op) const override {
751 752
    op->SetType("relu_grad_grad");
    // input1: Out
H
hong 已提交
753
    op->SetInput("Out", this->Input("Out"));
Q
qingqing01 已提交
754
    // input2: ddx
H
hong 已提交
755 756
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
757
    // output: ddy
H
hong 已提交
758
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
759 760 761
  }
};

762 763
// leaky_relu Grad: dx=dy if y>=0 else alpha * dy
// leaky_relu GradGrad: ddy=ddx if y>=0 else alpha * ddx
H
hong 已提交
764
template <typename T>
765
class LeakyReluDoubleGradMaker
H
hong 已提交
766
    : public ::paddle::framework::SingleGradOpMaker<T> {
767
 public:
H
hong 已提交
768
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
769 770

 protected:
771
  void Apply(GradOpPtr<T> op) const override {
772
    op->SetType("leaky_relu_grad_grad");
Z
Zeng Jinle 已提交
773
    // input1: Out
H
hong 已提交
774
    op->SetInput("Out", this->Input("Out"));
775
    // X@GRAD@GRAD: ddx
H
hong 已提交
776 777
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
778
    // Out@GRAD@GRAD: ddy
H
hong 已提交
779
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
780 781 782
  }
};

D
Double_V 已提交
783 784 785 786 787 788 789 790
// 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:
791
  void Apply(GradOpPtr<T> op) const override {
D
Double_V 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805
    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 已提交
806 807
// sqrt Grad: dx = 0.5 * dy / y
// sqrt GradGrad: ddy = 0.5 * ddx / y, dy = -1 * dx * ddx
H
hong 已提交
808 809
template <typename T>
class SqrtDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
L
lvmengsi 已提交
810
 public:
H
hong 已提交
811
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
L
lvmengsi 已提交
812 813

 protected:
814
  void Apply(GradOpPtr<T> op) const override {
L
lvmengsi 已提交
815
    op->SetType("sqrt_grad_grad");
H
hong 已提交
816 817 818 819 820 821
    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 已提交
822 823 824
  }
};

825 826
// square Grad: dx=2x*dy
// square GradGrad: ddy=2x*ddx, dx=2dy*ddx
H
hong 已提交
827 828
template <typename T>
class SquareDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
829
 public:
H
hong 已提交
830
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
831 832

 protected:
833
  void Apply(GradOpPtr<T> op) const override {
834
    op->SetType("square_grad_grad");
H
hong 已提交
835
    op->SetInput("X", this->Input("X"));
836
    // Out@GRAD: dy
H
hong 已提交
837
    op->SetInput("DOut", this->Input(framework::GradVarName("Out")));
838
    // X@GRAD@GRAD: ddx
H
hong 已提交
839
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
840

H
hong 已提交
841
    op->SetAttrMap(this->Attrs());
842 843

    // X@GRAD: dx
H
hong 已提交
844
    op->SetOutput("DX", this->InputGrad("X"));
845
    // Out@GRAD@GRAD: ddy
H
hong 已提交
846
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
847 848 849
  }
};

850
DECLARE_INPLACE_OP_INFERER(ActivationGradOpInplaceInferer,
851 852
                           {framework::GradVarName("Out"),
                            framework::GradVarName("X")});
853
DECLARE_INPLACE_OP_INFERER(ActivationDoubleGradOpInplaceInferer,
854
                           {"DDX", "DDOut"});
855

H
hong 已提交
856 857
template <typename T>
class PowGradOpMaker : public framework::SingleGradOpMaker<T> {
858
 public:
H
hong 已提交
859
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
860 861

 protected:
862
  void Apply(GradOpPtr<T> op) const override {
863
    op->SetType("pow_grad");
H
hong 已提交
864 865 866 867 868
    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());
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
  }
};
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());
  }
};
923
DECLARE_INPLACE_OP_INFERER(ActFwdInplaceInferer, {"X", "Out"});
Q
qijun 已提交
924 925 926 927
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
928
namespace plat = paddle::platform;
929

930 931 932 933
#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 已提交
934 935 936 937
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::framework::OpDesc>,                \
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::imperative::OpBase>,               \
938
      std::conditional<ops::CanInplaceAct<ops::grad_functor<float>>(),      \
939
                       ops::ActFwdInplaceInferer, void>::type);             \
940
  REGISTER_OPERATOR(KERNEL_TYPE##_grad, ops::ActivationOpGrad,              \
941
                    ops::ActivationGradOpInplaceInferer);
942 943 944

#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, op_name, functor,        \
                                       grad_functor)                      \
Q
QI JUN 已提交
945 946 947 948 949 950 951 952 953 954
  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 已提交
955
                                ops::grad_functor<double>>);
956

957 958
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_OP);
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_CPU_KERNEL);
959

960
/* ==========================    relu register  ============================= */
961 962
REGISTER_OPERATOR(
    relu, ops::ActivationOp, ops::ReluOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
963 964 965 966
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
967
    ops::ActFwdInplaceInferer);
968
REGISTER_OPERATOR(relu_grad, ops::ActivationOpGrad,
969
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
970 971
                  ops::ReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ReluDoubleGradMaker<paddle::imperative::OpBase>);
972 973
REGISTER_OPERATOR(
    relu_grad_grad,
974
    ops::ActivationOpDoubleGrad2<ops::ReluGradFunctor<float>::FwdDeps()>,
975
    ops::ActivationDoubleGradOpInplaceInferer);
976 977 978 979 980 981 982 983 984 985 986

REGISTER_ACTIVATION_CPU_KERNEL(relu, Relu, ReluFunctor, ReluGradFunctor);

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

989
/* ======================== leaky relu register  ============================ */
990 991 992
REGISTER_OPERATOR(
    leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
993 994 995 996
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
997
    ops::ActFwdInplaceInferer);
998
REGISTER_OPERATOR(leaky_relu_grad, ops::ActivationOpGrad,
999
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1000 1001
                  ops::LeakyReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::LeakyReluDoubleGradMaker<paddle::imperative::OpBase>);
1002 1003
REGISTER_OPERATOR(
    leaky_relu_grad_grad,
1004
    ops::ActivationOpDoubleGrad2<ops::LeakyReluGradFunctor<float>::FwdDeps()>,
1005
    ops::ActivationDoubleGradOpInplaceInferer);
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
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>>);
1017 1018
/* ========================================================================== */

D
Double_V 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027
/* ========================    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,
1028
                  ops::ActivationGradOpInplaceInferer,
D
Double_V 已提交
1029 1030 1031 1032 1033
                  ops::ELUDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ELUDoubleGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
    elu_grad_grad,
    ops::ActivationOpDoubleGrad<ops::ELUGradFunctor<float>::FwdDeps()>,
1034
    ops::ActivationDoubleGradOpInplaceInferer);
D
Double_V 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

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 已提交
1047 1048 1049
/* ===========================   sqrt register  ============================= */
REGISTER_OPERATOR(
    sqrt, ops::ActivationOp, ops::SqrtOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1050 1051 1052 1053
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1054
    ops::ActFwdInplaceInferer);
L
lvmengsi 已提交
1055
REGISTER_OPERATOR(sqrt_grad, ops::ActivationOpGrad,
1056
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1057 1058
                  ops::SqrtDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SqrtDoubleGradMaker<paddle::imperative::OpBase>);
L
lvmengsi 已提交
1059 1060
REGISTER_OPERATOR(
    sqrt_grad_grad,
1061
    ops::ActivationOpDoubleGrad<ops::SqrtGradGradFunctor<float>::FwdDeps()>,
1062
    ops::ActivationDoubleGradOpInplaceInferer);
1063

L
lvmengsi 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
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>>);
/* ========================================================================== */

1074 1075 1076 1077
/* ==========================   square register  ============================ */
REGISTER_OPERATOR(
    square, ops::ActivationOp, ops::SquareOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
1078 1079 1080 1081
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1082
    ops::ActFwdInplaceInferer);
1083
REGISTER_OPERATOR(square_grad, ops::ActivationOpGrad,
1084
                  ops::ActivationGradOpInplaceInferer,
H
hong 已提交
1085 1086
                  ops::SquareDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SquareDoubleGradMaker<paddle::imperative::OpBase>);
1087 1088
REGISTER_OPERATOR(
    square_grad_grad,
1089
    ops::ActivationOpDoubleGrad<ops::SquareGradGradFunctor<float>::FwdDeps()>,
1090
    ops::ActivationDoubleGradOpInplaceInferer);
1091

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
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>>);
1110 1111 1112 1113 1114 1115 1116 1117

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,
1118 1119 1120 1121 1122
                                ops::SquareGradGradFunctor<plat::float16>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int64_t>>);
1123
/* ========================================================================== */
1124 1125 1126 1127 1128

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

REGISTER_OPERATOR(
    pow, ops::PowOp, ops::PowOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1129 1130
    ops::PowGradOpMaker<paddle::framework::OpDesc>,
    ops::PowGradOpMaker<paddle::imperative::OpBase>,
1131
    std::conditional<ops::CanInplaceAct<ops::PowGradFunctor<float>>(),
1132
                     ops::ActFwdInplaceInferer, void>::type);
1133
REGISTER_OPERATOR(pow_grad, ops::PowOpGrad,
1134
                  ops::ActivationGradOpInplaceInferer);
1135 1136 1137

REGISTER_OP_CPU_KERNEL(
    pow, ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<float>>,
1138 1139 1140
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<double>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int64_t>>);
1141 1142 1143
REGISTER_OP_CPU_KERNEL(
    pow_grad,
    ops::PowGradKernel<plat::CPUDeviceContext, ops::PowGradFunctor<float>>,
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
    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,
1159
                  ops::ActivationGradOpInplaceInferer);
1160 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 1190

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

/* ==========================   abs register  ============================ */
REGISTER_OPERATOR(
    abs, ops::ActivationOp, ops::AbsOpMaker, ops::ActivationOpInferVarType,
    ops::ActivationGradOpMaker<ops::AbsGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::AbsGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
    std::conditional<ops::CanInplaceAct<ops::AbsGradFunctor<float>>(),
                     ops::ActFwdInplaceInferer, void>::type);
REGISTER_OPERATOR(abs_grad, ops::ActivationOpGrad,
1191
                  ops::ActivationGradOpInplaceInferer);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

REGISTER_OP_CPU_KERNEL(abs,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::AbsFunctor<float>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::AbsFunctor<double>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::AbsFunctor<int>>,
                       ops::ActivationKernel<paddle::platform::CPUDeviceContext,
                                             ops::AbsFunctor<int64_t>>);
REGISTER_OP_CPU_KERNEL(
    abs_grad, ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                                        ops::AbsGradFunctor<float>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::AbsGradFunctor<double>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::AbsGradFunctor<int>>,
    ops::ActivationGradKernel<paddle::platform::CPUDeviceContext,
                              ops::AbsGradFunctor<int64_t>>);
1211
/* ========================================================================== */