activation_op.cc 46.0 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"
T
tink2123 已提交
16
#include <memory>
D
dzhwinter 已提交
17
#include <string>
18
#include <type_traits>
T
tink2123 已提交
19
#include <unordered_map>
20
#include <vector>
21
#include "paddle/fluid/operators/mkldnn/mkldnn_activation_op.h"
D
dzhwinter 已提交
22
#include "paddle/fluid/platform/port.h"
23 24 25
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/cudnn_helper.h"
#endif
Q
qijun 已提交
26

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

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

32 33
using paddle::framework::Tensor;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

D
dzhwinter 已提交
161
)DOC";
Q
qijun 已提交
162

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

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

D
dzhwinter 已提交
168
)DOC";
169

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

173
$$out = e^x$$
K
Kexin Zhao 已提交
174

D
dzhwinter 已提交
175
)DOC";
Q
qijun 已提交
176

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

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

D
dzhwinter 已提交
182
)DOC";
K
Kexin Zhao 已提交
183

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

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

D
dzhwinter 已提交
189
)DOC";
190

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

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

D
dzhwinter 已提交
196
)DOC";
K
Kexin Zhao 已提交
197

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

201
.. math:: out=\sqrt x=x^{1/2}
202

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

D
dzhwinter 已提交
206
)DOC";
207

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

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

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

)DOC";

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

220
$$out = |x|$$
K
Kexin Zhao 已提交
221

D
dzhwinter 已提交
222
)DOC";
223

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

227
$$out = \left \lceil x \right \rceil$$
D
dzhwinter 已提交
228

D
dzhwinter 已提交
229
)DOC";
D
dzhwinter 已提交
230

D
dzhwinter 已提交
231
UNUSED constexpr char FloorDoc[] = R"DOC(
D
dzhwinter 已提交
232 233
Floor Activation Operator.

234
$$out = \left \lfloor x \right \rfloor$$
D
dzhwinter 已提交
235

D
dzhwinter 已提交
236
)DOC";
D
dzhwinter 已提交
237

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

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

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

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

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

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

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

255 256 257 258 259 260 261 262 263
.. 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 已提交
264

D
dzhwinter 已提交
265
)DOC";
D
dzhwinter 已提交
266

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

270
$$out = \\frac{1}{x}$$
K
Kexin Zhao 已提交
271

D
dzhwinter 已提交
272
)DOC";
273

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

277
$$out = \ln(x)$$
K
Kexin Zhao 已提交
278 279 280

Natural logarithm of x.

D
dzhwinter 已提交
281 282
)DOC";

283 284 285 286 287 288 289 290 291
UNUSED constexpr char Log1pDoc[] = R"DOC(
Log Activation Operator.

$out = \ln(x+1)$

Natural logarithm of x.

)DOC";

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

295
$$out = x^2$$
296

D
dzhwinter 已提交
297 298
)DOC";

D
dzhwinter 已提交
299
UNUSED constexpr char SoftplusDoc[] = R"DOC(
D
dzhwinter 已提交
300 301
Softplus Activation Operator.

302
$$out = \ln(1 + e^{x})$$
D
dzhwinter 已提交
303 304 305

)DOC";

D
dzhwinter 已提交
306
UNUSED constexpr char SoftsignDoc[] = R"DOC(
D
dzhwinter 已提交
307 308
Softsign Activation Operator.

309
$$out = \\frac{x}{1 + \|x\|}$$
D
dzhwinter 已提交
310 311 312

)DOC";

T
tink2123 已提交
313 314 315 316 317 318
class AcosOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of acos operator");
    AddOutput("Out", "Output of acos operator");
    AddComment(R"DOC(
319 320
Arccosine Activation Operator.

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

T
tink2123 已提交
323 324 325
)DOC");
  }
};
326

T
tink2123 已提交
327 328 329 330 331 332
class AsinOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of asin operator");
    AddOutput("Out", "Output of asin operator");
    AddComment(R"DOC(
333 334
Arcsine Activation Operator.

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

T
tink2123 已提交
337 338 339
)DOC");
  }
};
340

T
tink2123 已提交
341 342 343 344 345 346
class AtanOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of atan operator");
    AddOutput("Out", "Output of atan operator");
    AddComment(R"DOC(
347 348
Arctanh Activation Operator.

T
tink2123 已提交
349
$$out = \tanh^{-1}(x)$$
350

T
tink2123 已提交
351 352 353
)DOC");
  }
};
354

D
dzhwinter 已提交
355
class LeakyReluOpMaker : public framework::OpProtoAndCheckerMaker {
356
 public:
Y
Yu Yang 已提交
357
  void Make() override {
W
Wilber 已提交
358 359 360 361 362 363 364 365
    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 已提交
366 367 368
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
K
Kexin Zhao 已提交
369
    AddComment(R"DOC(
D
dzhwinter 已提交
370
LeakyRelu Activation Operator.
K
Kexin Zhao 已提交
371

W
Wilber 已提交
372
$$out = \max(x, \alpha * x)$$
K
Kexin Zhao 已提交
373 374

)DOC");
375 376 377
  }
};

D
dzhwinter 已提交
378
class SoftShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
K
kexinzhao 已提交
379
 public:
Y
Yu Yang 已提交
380
  void Make() override {
D
dzhwinter 已提交
381 382 383
    AddInput("X", "Input of Softshrink operator");
    AddOutput("Out", "Output of Softshrink operator");
    AddAttr<float>("lambda", "non-negative offset").SetDefault(0.5f);
K
Kexin Zhao 已提交
384
    AddComment(R"DOC(
385 386 387
:strong:`Softshrink Activation Operator`

..  math::
388
    out = \begin{cases}
389 390 391 392
         x - \lambda, \text{if } x > \lambda \\
         x + \lambda, \text{if } x < -\lambda \\
         0,  \text{otherwise}
         \end{cases}
K
Kexin Zhao 已提交
393 394

)DOC");
K
kexinzhao 已提交
395 396 397
  }
};

D
dzhwinter 已提交
398
class HardShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
399
 public:
Y
Yu Yang 已提交
400
  void Make() override {
D
dzhwinter 已提交
401 402
    AddInput("X", "Input of HardShrink operator");
    AddOutput("Out", "Output of HardShrink operator");
Y
yuyang18 已提交
403 404
    AddAttr<float>("threshold",
                   "The value of threshold for HardShrink. [default: 0.5]")
D
dzhwinter 已提交
405
        .SetDefault(0.5f);
K
Kexin Zhao 已提交
406
    AddComment(R"DOC(
Y
yuyang18 已提交
407
:strong:`HardShrink activation operator`
K
Kexin Zhao 已提交
408

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

)DOC");
417 418 419
  }
};

420 421
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
422
  void Make() override {
423 424 425 426 427 428
    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``.");
429 430 431 432
    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 已提交
433
    AddComment(R"DOC(
K
kexinzhao 已提交
434
BRelu Activation Operator.
K
Kexin Zhao 已提交
435

436
$$out = \min(\max(x, t_{min}), t_{max})$$
K
Kexin Zhao 已提交
437 438

)DOC");
439 440 441 442 443
  }
};

class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
444
  void Make() override {
445
    AddInput("X", "Input of SoftRelu operator");
F
fengjiayi 已提交
446
    AddOutput("Out", "Output of SoftRelu operator");
447 448
    AddAttr<float>("threshold", "The threshold value of SoftRelu")
        .SetDefault(40.0f);
K
Kexin Zhao 已提交
449
    AddComment(R"DOC(
K
kexinzhao 已提交
450
SoftRelu Activation Operator.
K
Kexin Zhao 已提交
451

452
$$out = \ln(1 + \exp(\max(\min(x, threshold), -threshold)))$$
K
Kexin Zhao 已提交
453 454

)DOC");
455 456 457
  }
};

458 459
class ELUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
460
  void Make() override {
461 462 463 464 465 466
    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``.");
467
    AddAttr<float>("alpha", "The alpha value of ELU").SetDefault(1.0f);
468
    AddComment(R"DOC(
K
kexinzhao 已提交
469
ELU Activation Operator.
K
Kexin Zhao 已提交
470 471 472 473

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

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

)DOC");
477 478 479
  }
};

480 481
class Relu6OpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
482
  void Make() override {
Z
zhupengyang 已提交
483 484 485 486 487 488 489 490
    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. ")
491
        .SetDefault(6.0f);
K
Kexin Zhao 已提交
492
    AddComment(R"DOC(
K
kexinzhao 已提交
493
Relu6 Activation Operator.
K
Kexin Zhao 已提交
494

495
$$out = \min(\max(0, x), threshold)$$
K
Kexin Zhao 已提交
496 497

)DOC");
498 499 500
  }
};

501 502
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
503
  void Make() override {
504
    AddInput("X", "Input of Pow operator");
505 506 507 508 509
    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 已提交
510
    AddOutput("Out", "Output of Pow operator");
511
    AddAttr<float>("factor", "The exponential factor of Pow").SetDefault(1.0f);
K
Kexin Zhao 已提交
512
    AddComment(R"DOC(
K
kexinzhao 已提交
513
Pow Activation Operator.
K
Kexin Zhao 已提交
514

515
$$out = x^{factor}$$
K
Kexin Zhao 已提交
516 517

)DOC");
518 519 520 521 522
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
523
  void Make() override {
524 525 526 527 528 529
    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);
530 531
    AddAttr<float>("scale_b", "The scale parameter of b for the input")
        .SetDefault(1.7159f);
K
Kexin Zhao 已提交
532
    AddComment(R"DOC(
K
kexinzhao 已提交
533
STanh Activation Operator.
K
Kexin Zhao 已提交
534

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

)DOC");
Q
qijun 已提交
538 539 540
  }
};

541 542
class ThresholdedReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
543
  void Make() override {
544
    AddInput("X", "Input of ThresholdedRelu operator");
F
fengjiayi 已提交
545
    AddOutput("Out", "Output of ThresholdedRelu operator");
Y
yuyang18 已提交
546 547
    AddAttr<float>("threshold",
                   "The threshold location of activation. [default 1.0].")
548
        .SetDefault(1.0f);
K
Kexin Zhao 已提交
549
    AddComment(R"DOC(
Y
yuyang18 已提交
550
:strong:`ThresholdedRelu activation operator`
K
Kexin Zhao 已提交
551

Y
yuyang18 已提交
552
..  math::
K
Kexin Zhao 已提交
553

Y
yuyang18 已提交
554
    out = \begin{cases}
Y
yuyang18 已提交
555
             x,  \text{if } x > threshold \\
Y
yuyang18 已提交
556 557
             0,  \text{otherwise}
          \end{cases}
K
Kexin Zhao 已提交
558
)DOC");
559 560 561
  }
};

562 563
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
564
  void Make() override {
565 566 567 568 569
    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. ")
570
        .SetDefault(0.2f);
571 572 573
    AddAttr<float>(
        "offset",
        "The offset of the linear approximation of sigmoid. Default is 0.5. ")
574
        .SetDefault(0.5f);
575
    AddComment(R"DOC(
K
kexinzhao 已提交
576
HardSigmoid Activation Operator.
577

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

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

K
Kexin Zhao 已提交
583
)DOC");
584 585 586
  }
};

A
Abhinav Arora 已提交
587 588
class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
589
  void Make() override {
A
Abhinav Arora 已提交
590
    AddInput("X", "Input of Swish operator");
F
fengjiayi 已提交
591
    AddOutput("Out", "Output of Swish operator");
A
Abhinav Arora 已提交
592
    AddAttr<float>("beta", "Constant beta of swish operator").SetDefault(1.0f);
593 594 595
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
A
Abhinav Arora 已提交
596 597 598
    AddComment(R"DOC(
Swish Activation Operator.

599
$$out = \\frac{x}{1 + e^{- \beta \ x}}$$
A
Abhinav Arora 已提交
600 601 602 603 604

)DOC");
  }
};

H
huangjun12 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
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).

621
$$out = \frac{x * (min(max(0, x+offset), threshold))}{scale}$$
H
huangjun12 已提交
622 623 624 625 626 627 628 629 630

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 已提交
631 632 633 634 635 636 637
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 已提交
638
REGISTER_ACTIVATION_OP_MAKER(Rsqrt, RsqrtDoc);
D
dzhwinter 已提交
639 640 641 642 643 644 645 646
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);
REGISTER_ACTIVATION_OP_MAKER(Round, RoundDoc);
REGISTER_ACTIVATION_OP_MAKER(Reciprocal, ReciprocalDoc);
REGISTER_ACTIVATION_OP_MAKER(Log, LogDoc);
647
REGISTER_ACTIVATION_OP_MAKER(Log1p, Log1pDoc);
D
dzhwinter 已提交
648 649 650 651
REGISTER_ACTIVATION_OP_MAKER(Square, SquareDoc);
REGISTER_ACTIVATION_OP_MAKER(Softplus, SoftplusDoc);
REGISTER_ACTIVATION_OP_MAKER(Softsign, SoftsignDoc);

652
template <ActBwdOpFwdDeps kDepValue>
653 654 655 656 657
class ActivationOpDoubleGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
658
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
659
      if (ctx->HasOutput("DX")) {
660 661 662
        ctx->ShareDim("X", "DX");
        ctx->ShareLoD("X", "DX");
      }
663
      if (ctx->HasOutput("DDOut")) {
664 665 666
        ctx->ShareDim("X", "DDOut");
        ctx->ShareLoD("X", "DDOut");
      }
667
    }
668
    if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
669
      if (ctx->HasOutput("DOut")) {
670 671 672
        ctx->ShareDim("Out", "DOut");
        ctx->ShareLoD("Out", "DOut");
      }
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
      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")) {
701 702 703
        ctx->ShareDim("Out", "DDOut");
        ctx->ShareLoD("Out", "DDOut");
      }
704 705 706 707 708 709 710 711 712 713
    }
  }

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

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
// AbsGrad: dx=dy if x >=0 else -dy
// AbsDoubleGrad: ddy = ddx if x >=0 else -ddx
template <typename T>
class AbsDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
 public:
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;

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

734 735
// ReluGrad: dx = dy if y >= 0 else 0
// ReluGradGrad: ddy = ddx if y >= 0 else 0
H
hong 已提交
736 737
template <typename T>
class ReluDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
738
 public:
H
hong 已提交
739
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
740 741

 protected:
742
  void Apply(GradOpPtr<T> op) const override {
743 744
    op->SetType("relu_grad_grad");
    // input1: Out
H
hong 已提交
745
    op->SetInput("Out", this->Input("Out"));
Q
qingqing01 已提交
746
    // input2: ddx
H
hong 已提交
747 748
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
749
    // output: ddy
H
hong 已提交
750
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
751 752 753
  }
};

754 755
// leaky_relu Grad: dx=dy if y>=0 else alpha * dy
// leaky_relu GradGrad: ddy=ddx if y>=0 else alpha * ddx
H
hong 已提交
756
template <typename T>
757
class LeakyReluDoubleGradMaker
H
hong 已提交
758
    : public ::paddle::framework::SingleGradOpMaker<T> {
759
 public:
H
hong 已提交
760
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
761 762

 protected:
763
  void Apply(GradOpPtr<T> op) const override {
764
    op->SetType("leaky_relu_grad_grad");
Z
Zeng Jinle 已提交
765
    // input1: Out
H
hong 已提交
766
    op->SetInput("Out", this->Input("Out"));
767
    // X@GRAD@GRAD: ddx
H
hong 已提交
768 769
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
    op->SetAttrMap(this->Attrs());
770
    // Out@GRAD@GRAD: ddy
H
hong 已提交
771
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
772 773 774
  }
};

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

 protected:
806
  void Apply(GradOpPtr<T> op) const override {
L
lvmengsi 已提交
807
    op->SetType("sqrt_grad_grad");
H
hong 已提交
808 809 810 811 812 813
    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 已提交
814 815 816
  }
};

817 818
// square Grad: dx=2x*dy
// square GradGrad: ddy=2x*ddx, dx=2dy*ddx
H
hong 已提交
819 820
template <typename T>
class SquareDoubleGradMaker : public ::paddle::framework::SingleGradOpMaker<T> {
821
 public:
H
hong 已提交
822
  using ::paddle::framework::SingleGradOpMaker<T>::SingleGradOpMaker;
823 824

 protected:
825
  void Apply(GradOpPtr<T> op) const override {
826
    op->SetType("square_grad_grad");
H
hong 已提交
827
    op->SetInput("X", this->Input("X"));
828
    // Out@GRAD: dy
H
hong 已提交
829
    op->SetInput("DOut", this->Input(framework::GradVarName("Out")));
830
    // X@GRAD@GRAD: ddx
H
hong 已提交
831
    op->SetInput("DDX", this->OutputGrad(framework::GradVarName("X")));
832

H
hong 已提交
833
    op->SetAttrMap(this->Attrs());
834 835

    // X@GRAD: dx
H
hong 已提交
836
    op->SetOutput("DX", this->InputGrad("X"));
837
    // Out@GRAD@GRAD: ddy
H
hong 已提交
838
    op->SetOutput("DDOut", this->InputGrad(framework::GradVarName("Out")));
839 840 841
  }
};

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
// 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")));
  }
};

864 865 866
DECLARE_INPLACE_OP_INFERER(ActivationGradOpInplaceInference,
                           {framework::GradVarName("Out"),
                            framework::GradVarName("X")});
867 868
DECLARE_INPLACE_OP_INFERER(ActivationDoubleGradOpInplaceInference,
                           {"DDX", "DDOut"});
869

H
hong 已提交
870 871
template <typename T>
class PowGradOpMaker : public framework::SingleGradOpMaker<T> {
872
 public:
H
hong 已提交
873
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
874 875

 protected:
876
  void Apply(GradOpPtr<T> op) const override {
877
    op->SetType("pow_grad");
H
hong 已提交
878 879 880 881 882
    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());
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 923 924 925 926 927 928 929 930 931 932 933 934 935 936
  }
};
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());
  }
};
937
DECLARE_INPLACE_OP_INFERER(ActFwdInplaceInferer, {"X", "Out"});
Q
qijun 已提交
938 939 940 941
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
942
namespace plat = paddle::platform;
943

944 945 946 947
#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 已提交
948 949 950 951
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::framework::OpDesc>,                \
      ops::ActivationGradOpMaker<ops::grad_functor<float>::FwdDeps(),       \
                                 paddle::imperative::OpBase>,               \
952
      std::conditional<ops::CanInplaceAct<ops::grad_functor<float>>(),      \
953
                       ops::ActFwdInplaceInferer, void>::type);             \
954 955
  REGISTER_OPERATOR(KERNEL_TYPE##_grad, ops::ActivationOpGrad,              \
                    ops::ActivationGradOpInplaceInference);
956 957 958

#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, op_name, functor,        \
                                       grad_functor)                      \
Q
QI JUN 已提交
959 960 961 962 963 964 965 966 967 968
  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 已提交
969
                                ops::grad_functor<double>>);
970

971 972
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_OP);
FOR_EACH_ACTIVATION_OP(REGISTER_ACTIVATION_CPU_KERNEL);
973

974
/* ==========================    relu register  ============================= */
975 976
REGISTER_OPERATOR(
    relu, ops::ActivationOp, ops::ReluOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
977 978 979 980
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::ReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
981
    ops::ActFwdInplaceInferer);
982
REGISTER_OPERATOR(relu_grad, ops::ActivationOpGrad,
983
                  ops::ActivationGradOpInplaceInference,
H
hong 已提交
984 985
                  ops::ReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ReluDoubleGradMaker<paddle::imperative::OpBase>);
986 987
REGISTER_OPERATOR(
    relu_grad_grad,
988 989
    ops::ActivationOpDoubleGrad2<ops::ReluGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInference);
990 991 992 993 994 995 996 997 998 999 1000

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

1003
/* ======================== leaky relu register  ============================ */
1004 1005 1006
REGISTER_OPERATOR(
    leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
1007 1008 1009 1010
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::LeakyReluGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1011
    ops::ActFwdInplaceInferer);
1012
REGISTER_OPERATOR(leaky_relu_grad, ops::ActivationOpGrad,
1013
                  ops::ActivationGradOpInplaceInference,
H
hong 已提交
1014 1015
                  ops::LeakyReluDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::LeakyReluDoubleGradMaker<paddle::imperative::OpBase>);
1016 1017
REGISTER_OPERATOR(
    leaky_relu_grad_grad,
1018 1019
    ops::ActivationOpDoubleGrad2<ops::LeakyReluGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInference);
1020

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
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>>);
1031 1032
/* ========================================================================== */

D
Double_V 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
/* ========================    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,
                  ops::ActivationGradOpInplaceInference,
                  ops::ELUDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::ELUDoubleGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
    elu_grad_grad,
    ops::ActivationOpDoubleGrad<ops::ELUGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInference);

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 已提交
1061 1062 1063
/* ===========================   sqrt register  ============================= */
REGISTER_OPERATOR(
    sqrt, ops::ActivationOp, ops::SqrtOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1064 1065 1066 1067
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SqrtGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1068
    ops::ActFwdInplaceInferer);
L
lvmengsi 已提交
1069
REGISTER_OPERATOR(sqrt_grad, ops::ActivationOpGrad,
1070
                  ops::ActivationGradOpInplaceInference,
H
hong 已提交
1071 1072
                  ops::SqrtDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SqrtDoubleGradMaker<paddle::imperative::OpBase>);
L
lvmengsi 已提交
1073 1074
REGISTER_OPERATOR(
    sqrt_grad_grad,
1075 1076 1077
    ops::ActivationOpDoubleGrad<ops::SqrtGradGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInference);

L
lvmengsi 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
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>>);
/* ========================================================================== */

1088 1089 1090 1091
/* ==========================   square register  ============================ */
REGISTER_OPERATOR(
    square, ops::ActivationOp, ops::SquareOpMaker,
    ops::ActivationOpInferVarType,
H
hong 已提交
1092 1093 1094 1095
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::framework::OpDesc>,
    ops::ActivationGradOpMaker<ops::SquareGradFunctor<float>::FwdDeps(),
                               paddle::imperative::OpBase>,
1096
    ops::ActFwdInplaceInferer);
1097
REGISTER_OPERATOR(square_grad, ops::ActivationOpGrad,
1098
                  ops::ActivationGradOpInplaceInference,
H
hong 已提交
1099 1100
                  ops::SquareDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::SquareDoubleGradMaker<paddle::imperative::OpBase>);
1101 1102
REGISTER_OPERATOR(
    square_grad_grad,
1103 1104
    ops::ActivationOpDoubleGrad<ops::SquareGradGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInference);
1105

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
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>>);
1124 1125 1126 1127 1128 1129 1130 1131

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,
1132 1133 1134 1135 1136
                                ops::SquareGradGradFunctor<plat::float16>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int>>,
    ops::SquareDoubleGradKernel<plat::CPUDeviceContext,
                                ops::SquareGradGradFunctor<int64_t>>);
1137
/* ========================================================================== */
1138 1139 1140 1141 1142

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

REGISTER_OPERATOR(
    pow, ops::PowOp, ops::PowOpMaker, ops::ActivationOpInferVarType,
H
hong 已提交
1143 1144
    ops::PowGradOpMaker<paddle::framework::OpDesc>,
    ops::PowGradOpMaker<paddle::imperative::OpBase>,
1145
    std::conditional<ops::CanInplaceAct<ops::PowGradFunctor<float>>(),
1146
                     ops::ActFwdInplaceInferer, void>::type);
1147 1148 1149 1150 1151
REGISTER_OPERATOR(pow_grad, ops::PowOpGrad,
                  ops::ActivationGradOpInplaceInference);

REGISTER_OP_CPU_KERNEL(
    pow, ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<float>>,
1152 1153 1154
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<double>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int>>,
    ops::PowKernel<plat::CPUDeviceContext, ops::PowFunctor<int64_t>>);
1155 1156 1157
REGISTER_OP_CPU_KERNEL(
    pow_grad,
    ops::PowGradKernel<plat::CPUDeviceContext, ops::PowGradFunctor<float>>,
1158 1159 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 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    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,
                  ops::ActivationGradOpInplaceInference);

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,
1205 1206 1207 1208 1209 1210 1211
                  ops::ActivationGradOpInplaceInference,
                  ops::AbsDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::AbsDoubleGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(
    abs_grad_grad,
    ops::ActivationOpDoubleGrad<ops::AbsGradGradFunctor<float>::FwdDeps()>,
    ops::ActivationDoubleGradOpInplaceInference);
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

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>>);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
REGISTER_OP_CPU_KERNEL(
    abs_grad_grad,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::AbsGradGradFunctor<float>>,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::AbsGradGradFunctor<double>>,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::AbsGradGradFunctor<plat::float16>>,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::AbsGradGradFunctor<int>>,
    ops::ActivationDoubleGradKernel<plat::CPUDeviceContext,
                                    ops::AbsGradGradFunctor<int64_t>>);
1243
/* ========================================================================== */
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272

/* ==========================  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::ActivationGradOpInplaceInference,
                  ops::LogDoubleGradMaker<paddle::framework::OpDesc>,
                  ops::LogDoubleGradMaker<paddle::imperative::OpBase>);

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

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