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

namespace paddle {
namespace operators {

28 29
using paddle::framework::Tensor;

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#define REGISTER_ACTIVATION_OP_MAKER(OP_NAME, OP_COMMENT)                    \
  class OP_NAME##OpMaker                                                     \
      : public ::paddle::framework::OpProtoAndCheckerMaker {                 \
   public:                                                                   \
    void Make() override {                                                   \
      AddInput("X", "Input of " #OP_NAME " operator");                       \
      AddOutput("Out", "Output of " #OP_NAME " operator");                   \
      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);                                                \
      AddAttr<bool>(                                                         \
          "is_test",                                                         \
          "(bool, default false) Set to true for inference only, false "     \
          "for training. Some layers may run faster when this is true.")     \
          .SetDefault(false);                                                \
      AddComment(OP_COMMENT);                                                \
    }                                                                        \
D
dzhwinter 已提交
51
  }
D
dzhwinter 已提交
52 53 54 55 56 57 58 59 60

#define REGISTER_ACTIVATION_OP_GRAD_MAKER(OP_NAME, KERNEL_TYPE)              \
  class OP_NAME##GradMaker                                                   \
      : public ::paddle::framework::SingleGradOpDescMaker {                  \
   public:                                                                   \
    using ::paddle::framework::SingleGradOpDescMaker::SingleGradOpDescMaker; \
                                                                             \
   protected:                                                                \
    std::unique_ptr<::paddle::framework::OpDesc> Apply() const override {    \
61
      auto* op = new ::paddle::framework::OpDesc();                          \
D
dzhwinter 已提交
62 63 64 65 66 67 68 69 70 71
      op->SetType(#KERNEL_TYPE "_grad");                                     \
      op->SetInput("Out", Output("Out"));                                    \
      op->SetInput(::paddle::framework::GradVarName("Out"),                  \
                   OutputGrad("Out"));                                       \
                                                                             \
      op->SetAttrMap(Attrs());                                               \
                                                                             \
      op->SetOutput(::paddle::framework::GradVarName("X"), InputGrad("X"));  \
      return std::unique_ptr<::paddle::framework::OpDesc>(op);               \
    }                                                                        \
D
dzhwinter 已提交
72
  }
D
dzhwinter 已提交
73

74 75 76 77
framework::OpKernelType GetKernelType(const framework::ExecutionContext& ctx,
                                      const framework::OperatorWithKernel& oper,
                                      const std::string& name) {
  framework::LibraryType library{framework::LibraryType::kPlain};
M
mozga-intel 已提交
78
  framework::DataLayout layout = framework::DataLayout::kAnyLayout;
79 80 81 82 83 84 85 86 87 88
// 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
89 90 91 92 93
#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 已提交
94
    layout = framework::DataLayout::kMKLDNN;
95 96 97
  }
#endif
  return framework::OpKernelType(
C
chengduo 已提交
98 99
      framework::GetDataTypeOfVar(ctx.InputVar(name)), ctx.GetPlace(), layout,
      library);
100 101
}

Q
qijun 已提交
102 103 104 105
class ActivationOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

106
  void InferShape(framework::InferShapeContext* ctx) const override {
107
    ctx->ShareDim("X", /*->*/ "Out");
F
fengjiayi 已提交
108
    ctx->ShareLoD("X", /*->*/ "Out");
Q
qijun 已提交
109
  }
110

111
 protected:
112 113 114 115
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return GetKernelType(ctx, *this, "X");
  }
Q
qijun 已提交
116 117
};

C
chengduo 已提交
118 119 120 121 122 123
class ActivationOpInferVarType
    : public framework::PassInDtypeAndVarTypeToOutput {
 protected:
  std::unordered_map<std::string, std::string> GetInputOutputWithSameType()
      const override {
    return std::unordered_map<std::string, std::string>{{"X", /*->*/ "Out"}};
124 125 126
  }
};

Q
qijun 已提交
127 128 129 130
class ActivationOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

131
  void InferShape(framework::InferShapeContext* ctx) const override {
132 133
    ctx->ShareDim("Out", framework::GradVarName("X"));
    ctx->ShareLoD("Out", framework::GradVarName("X"));
Q
qijun 已提交
134
  }
135

136
 protected:
137 138 139 140
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return GetKernelType(ctx, *this, "Out");
  }
Q
qijun 已提交
141 142
};

D
dzhwinter 已提交
143
UNUSED constexpr char SigmoidDoc[] = R"DOC(
144
Sigmoid Activation Operator
K
Kexin Zhao 已提交
145

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

D
dzhwinter 已提交
148
)DOC";
Q
qijun 已提交
149

D
dzhwinter 已提交
150
UNUSED constexpr char LogSigmoidDoc[] = R"DOC(
151
Logsigmoid Activation Operator
K
Kexin Zhao 已提交
152

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

D
dzhwinter 已提交
155
)DOC";
156

D
dzhwinter 已提交
157
UNUSED constexpr char ExpDoc[] = R"DOC(
K
kexinzhao 已提交
158
Exp Activation Operator.
K
Kexin Zhao 已提交
159

F
fengjiayi 已提交
160
$out = e^x$
K
Kexin Zhao 已提交
161

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

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

F
fengjiayi 已提交
167
$out = \max(x, 0)$
K
Kexin Zhao 已提交
168

D
dzhwinter 已提交
169
)DOC";
K
Kexin Zhao 已提交
170

C
Clementine 已提交
171 172 173 174 175 176 177
UNUSED constexpr char GeluDoc[] = R"DOC(
Gelu Activation Operator.

$out = \\frac{1 + erf(\\frac{x}{\\sqrt{2}})}{2} x$

)DOC";

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

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

D
dzhwinter 已提交
183
)DOC";
184

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

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

D
dzhwinter 已提交
190
)DOC";
K
Kexin Zhao 已提交
191

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

195 196 197
Please make sure legal input, when input a negative value closed to zero,
you should add a small epsilon(1e-12) to avoid negative number caused by numerical errors.

F
fengjiayi 已提交
198
$out = \sqrt{x}$
K
Kexin Zhao 已提交
199

D
dzhwinter 已提交
200
)DOC";
201

Z
zhoukunsheng 已提交
202 203 204 205 206 207 208 209 210
UNUSED constexpr char RsqrtDoc[] = R"DOC(
Rsqrt Activation Operator.

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

$out = \frac{1}{\sqrt{x}}$

)DOC";

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

F
fengjiayi 已提交
214
$out = |x|$
K
Kexin Zhao 已提交
215

D
dzhwinter 已提交
216
)DOC";
217

D
dzhwinter 已提交
218
UNUSED constexpr char CeilDoc[] = R"DOC(
D
dzhwinter 已提交
219 220
Ceil Activation Operator.

221
$out = \left \lceil x \right \rceil$
D
dzhwinter 已提交
222

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

D
dzhwinter 已提交
225
UNUSED constexpr char FloorDoc[] = R"DOC(
D
dzhwinter 已提交
226 227
Floor Activation Operator.

228
$out = \left \lfloor x \right \rfloor$
D
dzhwinter 已提交
229

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

D
dzhwinter 已提交
232
UNUSED constexpr char CosDoc[] = R"DOC(
C
add sin  
chengduoZH 已提交
233
Cosine Activation Operator.
C
add cos  
chengduoZH 已提交
234 235 236

$out = cos(x)$

D
dzhwinter 已提交
237
)DOC";
C
add cos  
chengduoZH 已提交
238

D
dzhwinter 已提交
239
UNUSED constexpr char SinDoc[] = R"DOC(
C
add sin  
chengduoZH 已提交
240 241 242 243
Sine Activation Operator.

$out = sin(x)$

D
dzhwinter 已提交
244
)DOC";
C
add sin  
chengduoZH 已提交
245

D
dzhwinter 已提交
246
UNUSED constexpr char RoundDoc[] = R"DOC(
D
dzhwinter 已提交
247 248
Round Activation Operator.

F
fengjiayi 已提交
249
$out = [x]$
D
dzhwinter 已提交
250

D
dzhwinter 已提交
251
)DOC";
D
dzhwinter 已提交
252

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

256
$$out = \\frac{1}{x}$$
K
Kexin Zhao 已提交
257

D
dzhwinter 已提交
258
)DOC";
259

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

F
fengjiayi 已提交
263
$out = \ln(x)$
K
Kexin Zhao 已提交
264 265 266

Natural logarithm of x.

D
dzhwinter 已提交
267 268
)DOC";

D
dzhwinter 已提交
269
UNUSED constexpr char SquareDoc[] = R"DOC(
D
dzhwinter 已提交
270 271 272
Square Activation Operator.

$out = x^2$
273

D
dzhwinter 已提交
274 275
)DOC";

D
dzhwinter 已提交
276
UNUSED constexpr char SoftplusDoc[] = R"DOC(
D
dzhwinter 已提交
277 278 279 280 281 282
Softplus Activation Operator.

$out = \ln(1 + e^{x})$

)DOC";

D
dzhwinter 已提交
283
UNUSED constexpr char SoftsignDoc[] = R"DOC(
D
dzhwinter 已提交
284 285
Softsign Activation Operator.

286
$$out = \\frac{x}{1 + \|x\|}$$
D
dzhwinter 已提交
287 288 289

)DOC";

T
tink2123 已提交
290 291 292 293 294 295
class AcosOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of acos operator");
    AddOutput("Out", "Output of acos operator");
    AddComment(R"DOC(
296 297
Arccosine Activation Operator.

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

T
tink2123 已提交
300 301 302
)DOC");
  }
};
303

T
tink2123 已提交
304 305 306 307 308 309
class AsinOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of asin operator");
    AddOutput("Out", "Output of asin operator");
    AddComment(R"DOC(
310 311
Arcsine Activation Operator.

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

T
tink2123 已提交
314 315 316
)DOC");
  }
};
317

T
tink2123 已提交
318 319 320 321 322 323
class AtanOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "Input of atan operator");
    AddOutput("Out", "Output of atan operator");
    AddComment(R"DOC(
324 325
Arctanh Activation Operator.

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

T
tink2123 已提交
328 329 330
)DOC");
  }
};
331

D
dzhwinter 已提交
332
class LeakyReluOpMaker : public framework::OpProtoAndCheckerMaker {
333
 public:
Y
Yu Yang 已提交
334
  void Make() override {
D
dzhwinter 已提交
335 336 337
    AddInput("X", "Input of LeakyRelu operator");
    AddOutput("Out", "Output of LeakyRelu operator");
    AddAttr<float>("alpha", "The small negative slope").SetDefault(0.02f);
K
Kexin Zhao 已提交
338
    AddComment(R"DOC(
D
dzhwinter 已提交
339
LeakyRelu Activation Operator.
K
Kexin Zhao 已提交
340

D
dzhwinter 已提交
341
$out = \max(x, \alpha * x)$
K
Kexin Zhao 已提交
342 343

)DOC");
344 345 346
  }
};

D
dzhwinter 已提交
347
class SoftShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
K
kexinzhao 已提交
348
 public:
Y
Yu Yang 已提交
349
  void Make() override {
D
dzhwinter 已提交
350 351 352
    AddInput("X", "Input of Softshrink operator");
    AddOutput("Out", "Output of Softshrink operator");
    AddAttr<float>("lambda", "non-negative offset").SetDefault(0.5f);
K
Kexin Zhao 已提交
353
    AddComment(R"DOC(
354 355 356
:strong:`Softshrink Activation Operator`

..  math::
357
    out = \begin{cases}
358 359 360 361
         x - \lambda, \text{if } x > \lambda \\
         x + \lambda, \text{if } x < -\lambda \\
         0,  \text{otherwise}
         \end{cases}
K
Kexin Zhao 已提交
362 363

)DOC");
K
kexinzhao 已提交
364 365 366
  }
};

D
dzhwinter 已提交
367
class HardShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
368
 public:
Y
Yu Yang 已提交
369
  void Make() override {
D
dzhwinter 已提交
370 371
    AddInput("X", "Input of HardShrink operator");
    AddOutput("Out", "Output of HardShrink operator");
Y
yuyang18 已提交
372 373
    AddAttr<float>("threshold",
                   "The value of threshold for HardShrink. [default: 0.5]")
D
dzhwinter 已提交
374
        .SetDefault(0.5f);
K
Kexin Zhao 已提交
375
    AddComment(R"DOC(
Y
yuyang18 已提交
376
:strong:`HardShrink activation operator`
K
Kexin Zhao 已提交
377

Y
yuyang18 已提交
378 379 380 381 382 383
..  math::
    out = \begin{cases}
            x, \text{if } x > \lambda \\
            x, \text{if } x < -\lambda \\
            0,  \text{otherwise}
          \end{cases}
K
Kexin Zhao 已提交
384 385

)DOC");
386 387 388
  }
};

389 390
class BReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
391
  void Make() override {
392
    AddInput("X", "Input of BRelu operator");
F
fengjiayi 已提交
393
    AddOutput("Out", "Output of BRelu operator");
394 395 396 397
    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 已提交
398
    AddComment(R"DOC(
K
kexinzhao 已提交
399
BRelu Activation Operator.
K
Kexin Zhao 已提交
400

F
fengjiayi 已提交
401
$out = \max(\min(x, t_{min}), t_{max})$
K
Kexin Zhao 已提交
402 403

)DOC");
404 405 406 407 408
  }
};

class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
409
  void Make() override {
410
    AddInput("X", "Input of SoftRelu operator");
F
fengjiayi 已提交
411
    AddOutput("Out", "Output of SoftRelu operator");
412 413
    AddAttr<float>("threshold", "The threshold value of SoftRelu")
        .SetDefault(40.0f);
K
Kexin Zhao 已提交
414
    AddComment(R"DOC(
K
kexinzhao 已提交
415
SoftRelu Activation Operator.
K
Kexin Zhao 已提交
416

F
fengjiayi 已提交
417
$out = \ln(1 + \exp(\max(\min(x, threshold), threshold))$
K
Kexin Zhao 已提交
418 419

)DOC");
420 421 422
  }
};

423 424
class ELUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
425
  void Make() override {
K
Kexin Zhao 已提交
426
    AddInput("X", "Input of ELU operator");
F
fengjiayi 已提交
427
    AddOutput("Out", "Output of ELU operator");
428
    AddAttr<float>("alpha", "The alpha value of ELU").SetDefault(1.0f);
429
    AddComment(R"DOC(
K
kexinzhao 已提交
430
ELU Activation Operator.
K
Kexin Zhao 已提交
431 432 433 434

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

F
fengjiayi 已提交
435
$out = \max(0, x) + \min(0, \alpha * (e^x - 1))$
K
Kexin Zhao 已提交
436 437

)DOC");
438 439 440
  }
};

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

F
fengjiayi 已提交
451
$out = \min(\max(0, x), 6)$
K
Kexin Zhao 已提交
452 453

)DOC");
454 455 456
  }
};

457 458
class PowOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
459
  void Make() override {
460
    AddInput("X", "Input of Pow operator");
F
fengjiayi 已提交
461
    AddOutput("Out", "Output of Pow operator");
462
    AddAttr<float>("factor", "The exponential factor of Pow").SetDefault(1.0f);
K
Kexin Zhao 已提交
463
    AddComment(R"DOC(
K
kexinzhao 已提交
464
Pow Activation Operator.
K
Kexin Zhao 已提交
465

F
fengjiayi 已提交
466
$out = x^{factor}$
K
Kexin Zhao 已提交
467 468

)DOC");
469 470 471 472 473
  }
};

class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
474
  void Make() override {
475
    AddInput("X", "Input of STanh operator");
F
fengjiayi 已提交
476
    AddOutput("Out", "Output of STanh operator");
477 478 479 480
    AddAttr<float>("scale_a", "The scale parameter of a for the input")
        .SetDefault(2.0f / 3.0f);
    AddAttr<float>("scale_b", "The scale parameter of b for the input")
        .SetDefault(1.7159f);
K
Kexin Zhao 已提交
481
    AddComment(R"DOC(
K
kexinzhao 已提交
482
STanh Activation Operator.
K
Kexin Zhao 已提交
483

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

)DOC");
Q
qijun 已提交
487 488 489
  }
};

490 491
class ThresholdedReluOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
492
  void Make() override {
493
    AddInput("X", "Input of ThresholdedRelu operator");
F
fengjiayi 已提交
494
    AddOutput("Out", "Output of ThresholdedRelu operator");
Y
yuyang18 已提交
495 496
    AddAttr<float>("threshold",
                   "The threshold location of activation. [default 1.0].")
497
        .SetDefault(1.0f);
K
Kexin Zhao 已提交
498
    AddComment(R"DOC(
Y
yuyang18 已提交
499
:strong:`ThresholdedRelu activation operator`
K
Kexin Zhao 已提交
500

Y
yuyang18 已提交
501
..  math::
K
Kexin Zhao 已提交
502

Y
yuyang18 已提交
503
    out = \begin{cases}
Y
yuyang18 已提交
504
             x,  \text{if } x > threshold \\
Y
yuyang18 已提交
505 506
             0,  \text{otherwise}
          \end{cases}
K
Kexin Zhao 已提交
507
)DOC");
508 509 510
  }
};

511 512
class HardSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
513
  void Make() override {
514
    AddInput("X", "Input of HardSigmoid operator");
F
fengjiayi 已提交
515
    AddOutput("Out", "Output of HardSigmoid operator");
516 517 518 519
    AddAttr<float>("slope", "Slope for linear approximation of sigmoid")
        .SetDefault(0.2f);
    AddAttr<float>("offset", "Offset for linear approximation of sigmoid")
        .SetDefault(0.5f);
520
    AddComment(R"DOC(
K
kexinzhao 已提交
521
HardSigmoid Activation Operator.
522

523
Segment-wise linear approximation of sigmoid(https://arxiv.org/abs/1603.00391),
K
Kexin Zhao 已提交
524
which is much faster than sigmoid.
525

F
fengjiayi 已提交
526
$out = \max(0, \min(1, slope * x + shift))$
527 528

The slope should be positive. The offset can be either positive or negative.
K
Kexin Zhao 已提交
529
The default slope and shift are set according to the above reference.
530 531
It is recommended to use the defaults for this activation.

K
Kexin Zhao 已提交
532
)DOC");
533 534 535
  }
};

A
Abhinav Arora 已提交
536 537
class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
538
  void Make() override {
A
Abhinav Arora 已提交
539
    AddInput("X", "Input of Swish operator");
F
fengjiayi 已提交
540
    AddOutput("Out", "Output of Swish operator");
A
Abhinav Arora 已提交
541 542 543 544
    AddAttr<float>("beta", "Constant beta of swish operator").SetDefault(1.0f);
    AddComment(R"DOC(
Swish Activation Operator.

F
fengjiayi 已提交
545
$$out = \\frac{x}{1 + e^{- \beta x}}$$
A
Abhinav Arora 已提交
546 547 548 549 550

)DOC");
  }
};

D
dzhwinter 已提交
551 552 553 554
REGISTER_ACTIVATION_OP_MAKER(Sigmoid, SigmoidDoc);
REGISTER_ACTIVATION_OP_MAKER(LogSigmoid, LogSigmoidDoc);
REGISTER_ACTIVATION_OP_MAKER(Exp, ExpDoc);
REGISTER_ACTIVATION_OP_MAKER(Relu, ReluDoc);
C
Clementine 已提交
555
REGISTER_ACTIVATION_OP_MAKER(Gelu, GeluDoc);
D
dzhwinter 已提交
556 557 558
REGISTER_ACTIVATION_OP_MAKER(Tanh, TanhDoc);
REGISTER_ACTIVATION_OP_MAKER(TanhShrink, TanhShrinkDoc);
REGISTER_ACTIVATION_OP_MAKER(Sqrt, SqrtDoc);
Z
zhoukunsheng 已提交
559
REGISTER_ACTIVATION_OP_MAKER(Rsqrt, RsqrtDoc);
D
dzhwinter 已提交
560 561 562 563 564 565 566 567 568 569 570 571
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);
REGISTER_ACTIVATION_OP_MAKER(Square, SquareDoc);
REGISTER_ACTIVATION_OP_MAKER(Softplus, SoftplusDoc);
REGISTER_ACTIVATION_OP_MAKER(Softsign, SoftsignDoc);

D
dzhwinter 已提交
572 573
REGISTER_ACTIVATION_OP_GRAD_MAKER(Sigmoid, sigmoid);
REGISTER_ACTIVATION_OP_GRAD_MAKER(Relu, relu);
C
Clementine 已提交
574
REGISTER_ACTIVATION_OP_GRAD_MAKER(Gelu, gelu);
D
dzhwinter 已提交
575
REGISTER_ACTIVATION_OP_GRAD_MAKER(Exp, exp);
D
dzhwinter 已提交
576 577 578
REGISTER_ACTIVATION_OP_GRAD_MAKER(Tanh, tanh);
REGISTER_ACTIVATION_OP_GRAD_MAKER(Ceil, ceil);
REGISTER_ACTIVATION_OP_GRAD_MAKER(Floor, floor);
D
dzhwinter 已提交
579
REGISTER_ACTIVATION_OP_GRAD_MAKER(Sqrt, sqrt);
Z
zhoukunsheng 已提交
580
REGISTER_ACTIVATION_OP_GRAD_MAKER(Rsqrt, rsqrt);
D
dzhwinter 已提交
581
REGISTER_ACTIVATION_OP_GRAD_MAKER(SoftRelu, soft_relu);
D
dzhwinter 已提交
582 583
REGISTER_ACTIVATION_OP_GRAD_MAKER(Relu6, relu6);
REGISTER_ACTIVATION_OP_GRAD_MAKER(Reciprocal, reciprocal);
D
dzhwinter 已提交
584
REGISTER_ACTIVATION_OP_GRAD_MAKER(HardSigmoid, hard_sigmoid);
Q
qijun 已提交
585 586 587 588
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
589

D
dzhwinter 已提交
590
#define FOR_EACH_INPLACE_OP_FUNCTOR(__macro) \
D
dzhwinter 已提交
591
  __macro(Sigmoid, sigmoid);                 \
592
  __macro(Relu, relu);                       \
D
dzhwinter 已提交
593
  __macro(Exp, exp);                         \
594
  __macro(Tanh, tanh);                       \
D
dzhwinter 已提交
595 596
  __macro(Ceil, ceil);                       \
  __macro(Floor, floor);                     \
597
  __macro(Sqrt, sqrt);                       \
Z
zhoukunsheng 已提交
598
  __macro(Rsqrt, rsqrt);                     \
D
dzhwinter 已提交
599 600 601 602
  __macro(SoftRelu, soft_relu);              \
  __macro(Relu6, relu6);                     \
  __macro(Reciprocal, reciprocal);           \
  __macro(HardSigmoid, hard_sigmoid);
D
dzhwinter 已提交
603 604

#define FOR_EACH_OP_FUNCTOR(__macro) \
D
dzhwinter 已提交
605 606
  __macro(LogSigmoid, logsigmoid);   \
  __macro(SoftShrink, softshrink);   \
607
  __macro(Abs, abs);                 \
D
dzhwinter 已提交
608
  __macro(Cos, cos);                 \
609
  __macro(Acos, acos);               \
D
dzhwinter 已提交
610
  __macro(Sin, sin);                 \
611 612
  __macro(Asin, asin);               \
  __macro(Atan, atan);               \
D
dzhwinter 已提交
613 614 615
  __macro(Round, round);             \
  __macro(Log, log);                 \
  __macro(Square, square);           \
C
Clementine 已提交
616
  __macro(Gelu, gelu);               \
D
dzhwinter 已提交
617 618 619 620 621 622 623 624 625 626 627 628
  __macro(BRelu, brelu);             \
  __macro(Pow, pow);                 \
  __macro(STanh, stanh);             \
  __macro(Softplus, softplus);       \
  __macro(Softsign, softsign);       \
  __macro(LeakyRelu, leaky_relu);    \
  __macro(TanhShrink, tanh_shrink);  \
  __macro(ELU, elu);                 \
  __macro(HardShrink, hard_shrink);  \
  __macro(Swish, swish);             \
  __macro(ThresholdedRelu, thresholded_relu);

D
dzhwinter 已提交
629 630 631 632 633 634 635 636
#define REGISTER_INPLACE_ACTIVATION_OP(OP_NAME, KERNEL_TYPE)                   \
  REGISTER_OPERATOR(KERNEL_TYPE, ::paddle::operators::ActivationOp,            \
                    ::paddle::operators::OP_NAME##OpMaker,                     \
                    ::paddle::operators::ActivationOpInferVarType,             \
                    ::paddle::operators::OP_NAME##GradMaker,                   \
                    ::paddle::framework::SingleOpInplaceInToOut);              \
  REGISTER_OPERATOR(KERNEL_TYPE##_grad, ::paddle::operators::ActivationOpGrad, \
                    ::paddle::framework::SingleOpInplaceInToOut)
D
dzhwinter 已提交
637

D
dzhwinter 已提交
638 639 640
#define REGISTER_ACTIVATION_OP(OP_NAME, KERNEL_TYPE)                    \
  REGISTER_OPERATOR(KERNEL_TYPE, ::paddle::operators::ActivationOp,     \
                    ::paddle::operators::OP_NAME##OpMaker,              \
641
                    ::paddle::operators::ActivationOpInferVarType,      \
D
dzhwinter 已提交
642 643
                    ::paddle::framework::DefaultGradOpDescMaker<true>); \
  REGISTER_OPERATOR(KERNEL_TYPE##_grad, ::paddle::operators::ActivationOpGrad)
A
Abhinav Arora 已提交
644

Q
QI JUN 已提交
645 646 647 648 649 650 651 652 653 654 655
#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, functor, grad_functor)   \
  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 已提交
656
                                ops::grad_functor<double>>);
657

D
dzhwinter 已提交
658
FOR_EACH_OP_FUNCTOR(REGISTER_ACTIVATION_OP);
D
dzhwinter 已提交
659
FOR_EACH_INPLACE_OP_FUNCTOR(REGISTER_INPLACE_ACTIVATION_OP);
660
FOR_EACH_KERNEL_FUNCTOR(REGISTER_ACTIVATION_CPU_KERNEL);