activation_op.h 21.9 KB
Newer Older
Q
qijun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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

   http://www.apache.org/licenses/LICENSE-2.0

   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. */

#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"

namespace paddle {
namespace operators {

22 23 24
template <typename Place, typename Functor>
class ActivationKernel
    : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
Q
qijun 已提交
25
 public:
26 27
  using T = typename Functor::ELEMENT_TYPE;

Q
qijun 已提交
28 29 30 31 32 33 34 35 36
  void Compute(const framework::ExecutionContext& context) const override {
    auto* X = context.Input<framework::Tensor>("X");
    auto* Y = context.Output<framework::Tensor>("Y");
    Y->mutable_data<T>(context.GetPlace());

    auto x = framework::EigenVector<T>::Flatten(*X);
    auto y = framework::EigenVector<T>::Flatten(*Y);
    auto place = context.GetEigenDevice<Place>();
    Functor functor;
37 38 39 40 41

    auto attrs = functor.GetAttrs();
    for (auto& attr : attrs) {
      *attr.second = context.Attr<float>(attr.first);
    }
Q
qijun 已提交
42 43 44 45
    functor(place, x, y);
  }
};

46 47 48
template <typename Place, typename Functor>
class ActivationGradKernel
    : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
Q
qijun 已提交
49
 public:
50
  using T = typename Functor::ELEMENT_TYPE;
Q
qijun 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
  void Compute(const framework::ExecutionContext& context) const override {
    auto* X = context.Input<framework::Tensor>("X");
    auto* Y = context.Input<framework::Tensor>("Y");
    auto* dY = context.Input<framework::Tensor>(framework::GradVarName("Y"));
    auto* dX = context.Output<framework::Tensor>(framework::GradVarName("X"));
    dX->mutable_data<T>(context.GetPlace());

    auto dy = framework::EigenVector<T>::Flatten(*dY);
    auto x = framework::EigenVector<T>::Flatten(*X);
    auto y = framework::EigenVector<T>::Flatten(*Y);
    auto dx = framework::EigenVector<T>::Flatten(*dX);
    auto place = context.GetEigenDevice<Place>();
    Functor functor;
64 65 66 67
    auto attrs = functor.GetAttrs();
    for (auto& attr : attrs) {
      *attr.second = context.Attr<float>(attr.first);
    }
Q
qijun 已提交
68 69 70 71
    functor(place, x, y, dy, dx);
  }
};

72 73 74 75 76 77 78 79 80
template <typename T>
struct BaseActivationFunctor {
  using ELEMENT_TYPE = T;

  using AttrPair = std::vector<std::pair<const char*, float*>>;

  AttrPair GetAttrs() { return AttrPair(); }
};

81
// sigmoid(x) = 1 / (1 + exp(-x))
Q
qijun 已提交
82
template <typename T>
83
struct SigmoidFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
84
  template <typename Device, typename X, typename Y>
85
  void operator()(Device d, X x, Y y) const {
86
    y.device(d) = static_cast<T>(1) / (static_cast<T>(1) + (-x).exp());
Q
qijun 已提交
87 88 89
  }
};

90
template <typename T>
91
struct SigmoidGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
92
  template <typename Device, typename X, typename Y, typename dY, typename dX>
93
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
94
    dx.device(d) = dy * y * (static_cast<T>(1) - y);
Q
qijun 已提交
95 96 97
  }
};

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
// Originally: logsigmoid(x) = -log (1 + exp(-x))
// For numerical stability, we can use the log-sum-exp trick:
// https://hips.seas.harvard.edu/blog/2013/01/09/computing-log-sum-exp/
// We can rewrite the above equation as:
// y = -log( exp(0) + exp(-x)) [since exp(0) = 1]
//   = -log( exp(max(-x, 0) - max(-x, 0)) + exp(-x + max(-x, 0) - max(-x, 0)))
//   = -log( exp(max(-x, 0)) * exp(-max(-x, 0)) - exp(max(-x, 0)) * exp(-x -
//           max(-x, 0)))
//   = -log( exp(max(-x, 0)) * (exp(-max(-x, 0)) + exp(-x - max(-x, 0))))
//   = -log( exp(max(-x, 0)) - log(exp(-max(-x, 0)) + exp(-x - max(-x, 0)))
//
// Hence, logsigmoid(x) = - (max(-x, 0) + log(exp(-max(-x, 0))
// + exp(-x - max(-x, 0))))
template <typename T>
struct LogSigmoidFunctor : public BaseActivationFunctor<T> {
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    auto temp = (-x).cwiseMax(static_cast<T>(0));  // temp = max(-x, 0)
    y.device(d) = -temp - (((-temp).exp() + (-x - temp).exp()).log());
  }
};

// Originally: f' = exp(-x) / (1 + exp(-x))
// For numerical stability: f' = exp(-x - max(-x, 0)) / (exp(-max(-x, 0)) +
// exp(-x - max(-x, 0)))
template <typename T>
struct LogSigmoidGradFunctor : public BaseActivationFunctor<T> {
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    auto temp = (-x).cwiseMax(static_cast<T>(0));  // temp = max(-x, 0)
    dx.device(d) =
        dy * ((-x - temp).exp() / ((-temp).exp() + (-x - temp).exp()));
  }
};

Q
qijun 已提交
133
// exp(x) = e^x
134 135
template <typename T>
struct ExpFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
136
  template <typename Device, typename X, typename Y>
137
  void operator()(Device d, X x, Y y) const {
Q
qijun 已提交
138 139 140 141
    y.device(d) = x.exp();
  }
};

142 143
template <typename T>
struct ExpGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
144
  template <typename Device, typename X, typename Y, typename dY, typename dX>
145
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
Q
qijun 已提交
146
    dx.device(d) = dy * y;
Q
qijun 已提交
147 148 149
  }
};

Q
qijun 已提交
150
// relu(x) = max(x, 0)
Q
qijun 已提交
151
template <typename T>
152
struct ReluFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
153
  template <typename Device, typename X, typename Y>
154
  void operator()(Device d, X x, Y y) const {
Q
qijun 已提交
155 156 157
    y.device(d) = x.cwiseMax(static_cast<T>(0));
  }
};
Q
qijun 已提交
158

Q
qijun 已提交
159
template <typename T>
160
struct ReluGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
161
  template <typename Device, typename X, typename Y, typename dY, typename dX>
162
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
Q
qijun 已提交
163 164 165
    dx.device(d) = dy * (x > static_cast<T>(0)).template cast<T>();
  }
};
Q
qijun 已提交
166

167
// tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
168 169
template <typename T>
struct TanhFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
170
  template <typename Device, typename X, typename Y>
171
  void operator()(Device d, X x, Y y) const {
Q
qijun 已提交
172 173 174 175 176
    y.device(d) = x.tanh();
  }
};

template <typename T>
177
struct TanhGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
178
  template <typename Device, typename X, typename Y, typename dY, typename dX>
179
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
180
    dx.device(d) = dy * (static_cast<T>(1) - y * y);
Q
qijun 已提交
181 182 183
  }
};

K
Kavya Srinet 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
// tanhshrink(x) = x - tanh(x)
// where tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
template <typename T>
struct TanhShrinkFunctor : public BaseActivationFunctor<T> {
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) = x - x.tanh();
  }
};

template <typename T>
struct TanhShrinkGradFunctor : public BaseActivationFunctor<T> {
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    dx.device(d) = dy * (x.tanh() * x.tanh());
  }
};

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
// tanhshrink(x) = x - tanh(x)
// where tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
template <typename T>
struct HardShrinkFunctor : public BaseActivationFunctor<T> {
  float threshold;

  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    auto temp1 = (x < (threshold * -1)).template cast<T>().eval();
    auto temp2 = (x > threshold).template cast<T>().eval();
    y.device(d) = x * (temp1 + temp2);
  }
};

template <typename T>
struct HardShrinkGradFunctor : public BaseActivationFunctor<T> {
  float threshold;

  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }

  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    auto temp1 = (x < (threshold * -1)).template cast<T>().eval();
    auto temp2 = (x > threshold).template cast<T>().eval();
    dx.device(d) = dy * (temp1 + temp2).template cast<T>();
  }
};

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
// softshrink(x) = x - lambda, if x > lambda; x + lambda, if x < lambda; 0
// otherwise
template <typename T>
struct SoftShrinkFunctor : public BaseActivationFunctor<T> {
  float lambda;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"lambda", &lambda}};
  }

  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    auto temp1 = (x > lambda).template cast<T>().eval();
    auto temp2 = (x < -lambda).template cast<T>().eval();
    y.device(d) = temp1 * (x - lambda) + temp2 * (x + lambda);
  }
};

template <typename T>
struct SoftShrinkGradFunctor : public BaseActivationFunctor<T> {
  float lambda;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"lambda", &lambda}};
  }
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    auto temp1 = (x > lambda).template cast<T>().eval();
    auto temp2 = (x < -lambda).template cast<T>().eval();
    dx.device(d) = dy * (temp1 + temp2).template cast<T>();
  }
};

Q
qijun 已提交
266
// sqrt(x) = x^(1/2)
267 268
template <typename T>
struct SqrtFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
269
  template <typename Device, typename X, typename Y>
270
  void operator()(Device d, X x, Y y) const {
Q
qijun 已提交
271 272 273 274 275
    y.device(d) = x.sqrt();
  }
};

template <typename T>
276
struct SqrtGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
277
  template <typename Device, typename X, typename Y, typename dY, typename dX>
278
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
279
    const Y y_conj = Eigen::numext::conj(y);
Q
qijun 已提交
280 281 282 283
    dx.device(d) = static_cast<T>(0.5) * dy / y_conj;
  }
};

Q
qijun 已提交
284
// abs(x) = |x|
285 286
template <typename T>
struct AbsFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
287
  template <typename Device, typename X, typename Y>
288
  void operator()(Device d, X x, Y y) const {
Q
qijun 已提交
289 290 291 292
    y.device(d) = x.abs();
  }
};

293 294
template <typename T>
struct AbsGradFunctor : public BaseActivationFunctor<T> {
295
  template <typename Device, typename X, typename Y, typename dY, typename dX>
296
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
297 298 299 300
    dx.device(d) = dy * x.sign();
  }
};

Q
qijun 已提交
301 302
// reciprocal(x) = 1 / x
template <typename T>
303
struct ReciprocalFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
304
  template <typename Device, typename X, typename Y>
305
  void operator()(Device d, X x, Y y) const {
306
    y.device(d) = static_cast<T>(1) / x;
Q
qijun 已提交
307 308 309
  }
};

310
template <typename T>
311
struct ReciprocalGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
312
  template <typename Device, typename X, typename Y, typename dY, typename dX>
313
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
314
    dx.device(d) = dy * static_cast<T>(-1) * y * y;
Q
qijun 已提交
315 316 317 318
  }
};

// log(x) = natural logarithm of x
319 320
template <typename T>
struct LogFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
321
  template <typename Device, typename X, typename Y>
322
  void operator()(Device d, X x, Y y) const {
Q
qijun 已提交
323 324 325 326
    y.device(d) = x.log();
  }
};

327
template <typename T>
328
struct LogGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
329
  template <typename Device, typename X, typename Y, typename dY, typename dX>
330
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
331
    dx.device(d) = dy * (static_cast<T>(1) / x);
Q
qijun 已提交
332 333 334 335
  }
};

// square(x) = x^2
336 337
template <typename T>
struct SquareFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
338
  template <typename Device, typename X, typename Y>
339
  void operator()(Device d, X x, Y y) const {
Q
qijun 已提交
340 341
    y.device(d) = x.square();
  }
342
};
Q
qijun 已提交
343

344
template <typename T>
345
struct SquareGradFunctor : public BaseActivationFunctor<T> {
Q
qijun 已提交
346
  template <typename Device, typename X, typename Y, typename dY, typename dX>
347
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
348 349 350 351
    dx.device(d) = dy * static_cast<T>(2) * x;
  }
};

352 353 354 355 356 357 358 359 360 361
template <typename T>
struct BReluFunctor : public BaseActivationFunctor<T> {
  float t_min;
  float t_max;

  // NOTE: Explicit hides the `BaseActivationFunctor<T>::GetAttrs`
  // not polymorphism for speed.
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"t_min", &t_min}, {"t_max", &t_max}};
  }
362

363 364 365
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) = x.cwiseMax(t_min).cwiseMin(t_max);
366 367 368
  }
};

369 370 371 372 373 374 375 376 377 378
template <typename T>
struct BReluGradFunctor : public BaseActivationFunctor<T> {
  float t_min;
  float t_max;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"t_min", &t_min}, {"t_max", &t_max}};
  }
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    dx.device(d) = dy * ((x > t_min) * (x < t_max)).template cast<T>();
379 380 381
  }
};

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
// relu6(x) = min(max(0, x), 6)
template <typename T>
struct Relu6Functor : public BaseActivationFunctor<T> {
  float threshold;

  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }

  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) = x.cwiseMax(static_cast<T>(0)).cwiseMin(threshold);
  }
};

template <typename T>
struct Relu6GradFunctor : public BaseActivationFunctor<T> {
  float threshold;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    dx.device(d) =
        dy * ((x > static_cast<T>(0)) * (x < threshold)).template cast<T>();
  }
};

K
kexinzhao 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
// softplus(x) = log(1 + exp(x))
// When x is a very large positive number, exp(x) may explode to inf,
// Using trick below for numerical stability
// https://hips.seas.harvard.edu/blog/2013/01/09/computing-log-sum-exp/
// Then: softplus(x) = max(x, 0) + log(exp(-max(x, 0)) + exp(x - max(x, 0)))
template <typename T>
struct SoftplusFunctor : public BaseActivationFunctor<T> {
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) {
    auto temp = x.cwiseMax(static_cast<T>(0));  // temp = max(x, 0)
    y.device(d) = temp + (((-temp).exp() + (x - temp).exp()).log());
  }
};

// d(softplus(x))/dx = exp(x) / (1 + exp(x))
// For numerical stability:
// d(softplus(x))/dx = exp(x - max(x, 0)) / (exp(-max(x, 0)) +
// exp(x - max(x, 0)))
template <typename T>
struct SoftplusGradFunctor : public BaseActivationFunctor<T> {
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) {
    auto temp = x.cwiseMax(static_cast<T>(0));  // temp = max(x, 0)
    dx.device(d) = dy * ((x - temp).exp() / ((-temp).exp() + (x - temp).exp()));
  }
};

437 438
// softsign(x) = x / (1 + |x|)
template <typename T>
439
struct SoftsignFunctor : public BaseActivationFunctor<T> {
440 441 442 443 444 445 446 447 448
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) {
    y.device(d) = x / (static_cast<T>(1) + x.abs());
  }
};

// d(softsign(x))/dx = 1 / (1 + |x|)^2
// Taken from https://en.wikipedia.org/wiki/Activation_function
template <typename T>
449
struct SoftsignGradFunctor : public BaseActivationFunctor<T> {
450 451 452 453 454 455 456
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) {
    dx.device(d) =
        dy * (static_cast<T>(1) / (static_cast<T>(1) + x.abs()).square());
  }
};

457 458 459 460 461 462
template <typename T>
struct SoftReluFunctor : public BaseActivationFunctor<T> {
  float threshold;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }
463

464 465 466 467
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    auto temp = x.cwiseMax(-threshold).cwiseMin(threshold);
    y.device(d) = (static_cast<T>(1) + temp.exp()).log();
468 469 470
  }
};

471 472 473 474 475 476 477 478
template <typename T>
struct SoftReluGradFunctor : public BaseActivationFunctor<T> {
  float threshold;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
479
    auto temp = ((x > -threshold) * (x < threshold)).template cast<T>().eval();
480
    dx.device(d) = dy * (static_cast<T>(1) - (-y).exp()) * temp;
481 482 483
  }
};

K
Kavya Srinet 已提交
484 485 486 487 488 489
template <typename T>
struct LeakyReluFunctor : public BaseActivationFunctor<T> {
  float alpha;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"alpha", &alpha}};
  }
490

K
Kavya Srinet 已提交
491 492 493
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) = x.cwiseMax(alpha * x);
494 495 496
  }
};

K
Kavya Srinet 已提交
497 498 499 500 501 502 503 504 505 506 507
template <typename T>
struct LeakyReluGradFunctor : public BaseActivationFunctor<T> {
  float alpha;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"alpha", &alpha}};
  }
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    auto temp1 = alpha * (x < static_cast<T>(0)).template cast<T>().eval();
    auto temp2 = (x >= static_cast<T>(0)).template cast<T>().eval();
    dx.device(d) = dy * (temp1 + temp2).template cast<T>();
508 509 510
  }
};

511 512 513 514 515 516
template <typename T>
struct ELUFunctor : public BaseActivationFunctor<T> {
  float alpha;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"alpha", &alpha}};
  }
517

518 519 520
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) =
521 522 523 524 525
        x.cwiseMax(static_cast<T>(0)) +
        (alpha * (x.exp() - static_cast<T>(1))).cwiseMin(static_cast<T>(0));
  }
};

526 527 528 529 530 531 532 533 534
template <typename T>
struct ELUGradFunctor : public BaseActivationFunctor<T> {
  float alpha;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"alpha", &alpha}};
  }
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    dx.device(d) =
535 536 537 538 539
        dy * (x > static_cast<T>(0)).template cast<T>() +
        dy * (y + alpha) * (x < static_cast<T>(0)).template cast<T>();
  }
};

540 541 542 543 544 545 546 547 548
template <typename T>
struct PowFunctor : public BaseActivationFunctor<T> {
  float factor;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"factor", &factor}};
  }
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) = x.pow(factor);
549 550 551
  }
};

552 553 554 555 556 557 558 559 560
template <typename T>
struct PowGradFunctor : public BaseActivationFunctor<T> {
  float factor;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"factor", &factor}};
  }
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    dx.device(d) = dy * factor * x.pow(factor - static_cast<T>(1));
561 562 563
  }
};

564 565 566 567 568 569 570
template <typename T>
struct STanhFunctor : public BaseActivationFunctor<T> {
  float scale_a;
  float scale_b;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"scale_a", &scale_a}, {"scale_b", &scale_b}};
  }
571

572 573 574
  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) = scale_b * (scale_a * x).tanh();
575 576 577
  }
};

578 579 580 581 582 583 584
template <typename T>
struct STanhGradFunctor : public BaseActivationFunctor<T> {
  float scale_a;
  float scale_b;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"scale_a", &scale_a}, {"scale_b", &scale_b}};
  }
585

586 587
  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
588
    auto temp = (scale_a * x).tanh() * (scale_a * x).tanh();
589
    dx.device(d) = dy * scale_a * scale_b * (static_cast<T>(1) - temp);
Q
qijun 已提交
590 591 592
  }
};

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
template <typename T>
struct ThresholdedReluFunctor : public BaseActivationFunctor<T> {
  float threshold;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }

  template <typename Device, typename X, typename Y>
  void operator()(Device d, X x, Y y) const {
    y.device(d) = (x > static_cast<T>(threshold)).template cast<T>() * x;
  }
};

template <typename T>
struct ThresholdedReluGradFunctor : public BaseActivationFunctor<T> {
  float threshold;
  typename BaseActivationFunctor<T>::AttrPair GetAttrs() {
    return {{"threshold", &threshold}};
  }

  template <typename Device, typename X, typename Y, typename dY, typename dX>
  void operator()(Device d, X x, Y y, dY dy, dX dx) const {
    dx.device(d) = dy * (x > static_cast<T>(threshold)).template cast<T>();
  }
};

Q
qijun 已提交
619 620
}  // namespace operators
}  // namespace paddle
621

622 623
#define FOR_EACH_KERNEL_FUNCTOR(__macro)                          \
  __macro(sigmoid, SigmoidFunctor, SigmoidGradFunctor);           \
624
  __macro(logsigmoid, LogSigmoidFunctor, LogSigmoidGradFunctor);  \
625 626 627
  __macro(exp, ExpFunctor, ExpGradFunctor);                       \
  __macro(relu, ReluFunctor, ReluGradFunctor);                    \
  __macro(tanh, TanhFunctor, TanhGradFunctor);                    \
628
  __macro(softshrink, SoftShrinkFunctor, SoftShrinkGradFunctor);  \
629 630 631 632 633 634 635 636 637
  __macro(sqrt, SqrtFunctor, SqrtGradFunctor);                    \
  __macro(abs, AbsFunctor, AbsGradFunctor);                       \
  __macro(reciprocal, ReciprocalFunctor, ReciprocalGradFunctor);  \
  __macro(log, LogFunctor, LogGradFunctor);                       \
  __macro(square, SquareFunctor, SquareGradFunctor);              \
  __macro(brelu, BReluFunctor, BReluGradFunctor);                 \
  __macro(soft_relu, SoftReluFunctor, SoftReluGradFunctor);       \
  __macro(pow, PowFunctor, PowGradFunctor);                       \
  __macro(stanh, STanhFunctor, STanhGradFunctor);                 \
K
kexinzhao 已提交
638
  __macro(softplus, SoftplusFunctor, SoftplusGradFunctor);        \
639
  __macro(softsign, SoftsignFunctor, SoftsignGradFunctor);        \
Z
zhouxiao-coder 已提交
640
  __macro(relu6, Relu6Functor, Relu6GradFunctor);                 \
641
  __macro(leaky_relu, LeakyReluFunctor, LeakyReluGradFunctor);    \
642
  __macro(tanh_shrink, TanhShrinkFunctor, TanhShrinkGradFunctor); \
643
  __macro(elu, ELUFunctor, ELUGradFunctor);                       \
644 645
  __macro(hard_shrink, HardShrinkFunctor, HardShrinkGradFunctor); \
  __macro(thresholded_relu, ThresholdedReluFunctor, ThresholdedReluGradFunctor);