activation_op_xpu.cc 24.4 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

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

#ifdef PADDLE_WITH_XPU

#include <string>
18 19 20

#include "paddle/fluid/operators/activation_op.h"
#include "paddle/fluid/platform/device/device_wrapper.h"
21
#include "paddle/fluid/platform/device/xpu/xpu_header.h"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

namespace paddle {
namespace operators {

using paddle::framework::Tensor;

template <typename Functor>
class XPUActivationKernel
    : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
 public:
  void Compute(const framework::ExecutionContext &context) const override {
    Functor functor;

    auto attrs = functor.GetAttrs();
    for (auto &attr : attrs) {
      *attr.second = context.Attr<float>(attr.first);
    }
    functor(context);
  }
};

template <typename Functor>
class XPUActivationGradKernel
    : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
 public:
  void Compute(const framework::ExecutionContext &context) const override {
    Functor functor;

    auto attrs = functor.GetAttrs();
    for (auto &attr : attrs) {
      *attr.second = context.Attr<float>(attr.first);
    }
    functor(context);
  }
};

58
template <typename DeviceContext, typename T, typename XPUT>
T
TTerror 已提交
59 60
void xpu_activation_forward(
    const framework::ExecutionContext &ctx,
61
    std::function<int(xpu::Context *, const XPUT *, XPUT *, int)> func) {
62 63
  const auto *x = ctx.Input<Tensor>("X");
  auto *y = ctx.Output<Tensor>("Out");
64 65
  const XPUT *x_data = reinterpret_cast<const XPUT *>(x->data<T>());
  XPUT *y_data = reinterpret_cast<XPUT *>(y->mutable_data<T>(ctx.GetPlace()));
P
procr 已提交
66

T
TTerror 已提交
67 68 69
  auto xpu_context = ctx.device_context<DeviceContext>().x_context();
  int r = func(xpu_context, x_data, y_data, x->numel());
  PADDLE_ENFORCE_EQ(
70 71
      r,
      xpu::Error_t::SUCCESS,
T
TTerror 已提交
72
      platform::errors::External("XPU activation op return wrong value[%d %s].",
73 74
                                 r,
                                 XPUAPIErrorMsg[r]));
75 76
}

77 78 79
template <typename DeviceContext, typename T, typename XPUT>
void xpu_activation_backward(
    const framework::ExecutionContext &ctx,
80 81
    std::function<int(
        xpu::Context *, const XPUT *, const XPUT *, const XPUT *, XPUT *, int)>
82
        func) {
83 84 85 86 87
  /* TODO: relu tanh sigmoid are inplace */
  const auto *x = ctx.Input<Tensor>("X");
  auto *y = ctx.Input<Tensor>("Out");
  auto *dOut = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
  auto *dX = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
88 89 90 91 92 93 94
  const XPUT *x_data = nullptr;
  const XPUT *y_data = nullptr;
  const XPUT *y_grad = nullptr;
  if (x != nullptr) x_data = reinterpret_cast<const XPUT *>(x->data<T>());
  if (y != nullptr) y_data = reinterpret_cast<const XPUT *>(y->data<T>());
  if (dOut != nullptr) y_grad = reinterpret_cast<const XPUT *>(dOut->data<T>());
  XPUT *x_grad = reinterpret_cast<XPUT *>(dX->mutable_data<T>(ctx.GetPlace()));
95
  auto xpu_context = ctx.device_context<DeviceContext>().x_context();
P
procr 已提交
96

T
TTerror 已提交
97
  int r = func(xpu_context, x_data, y_data, y_grad, x_grad, dX->numel());
98 99
  PADDLE_ENFORCE_EQ(r,
                    xpu::Error_t::SUCCESS,
100
                    platform::errors::External(
101 102
                        "XPU activation grad op return wrong value[%d %s].",
                        r,
T
TTerror 已提交
103
                        XPUAPIErrorMsg[r]));
104 105
}

T
TTerror 已提交
106
template <typename T>
107
struct XPUAbsFunctor : public BaseActivationFunctor<T> {
108
  using XPUType = typename XPUTypeTrait<T>::Type;
109
  void operator()(const framework::ExecutionContext &ctx) const {
110
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
111
        ctx, xpu::abs<XPUType>);
112 113 114
  }
};

T
TTerror 已提交
115
template <typename T>
116
struct XPUAbsGradFunctor : public BaseActivationFunctor<T> {
117
  using XPUType = typename XPUTypeTrait<T>::Type;
118
  void operator()(const framework::ExecutionContext &ctx) const {
119 120
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::abs_grad<XPUType>);
121 122 123 124
  }
};

template <typename T>
125
struct XPUExpFunctor : public BaseActivationFunctor<T> {
126
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
127
  void operator()(const framework::ExecutionContext &ctx) const {
128
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
129
        ctx, xpu::exp<XPUType>);
T
TTerror 已提交
130 131 132
  }
};

133
template <typename T>
T
TTerror 已提交
134
struct XPULogFunctor : public BaseActivationFunctor<T> {
135
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
136
  void operator()(const framework::ExecutionContext &ctx) const {
137 138
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::log<XPUType>);
T
TTerror 已提交
139 140 141
  }
};

142
template <typename T>
143
struct XPUReciprocalFunctor : public BaseActivationFunctor<T> {
144
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
145
  void operator()(const framework::ExecutionContext &ctx) const {
146
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
147
        ctx, xpu::reciprocal<XPUType>);
T
TTerror 已提交
148 149 150
  }
};

151
template <typename T>
152
struct XPUReciprocalGradFunctor : public BaseActivationFunctor<T> {
153
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
154
  void operator()(const framework::ExecutionContext &ctx) const {
155 156
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::reciprocal_grad<XPUType>);
T
TTerror 已提交
157 158 159
  }
};

160
template <typename T>
161 162
struct XPUReluGradFunctor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
163
  void operator()(const framework::ExecutionContext &ctx) const {
164 165
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::relu_grad<XPUType>);
T
TTerror 已提交
166 167 168
  }
};

D
dongfangshenzhu 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
template <typename T>
struct XPURelu6Functor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
  void operator()(const framework::ExecutionContext &ctx) const {
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::relu6<XPUType>);
  }
};

template <typename T>
struct XPURelu6GradFunctor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
  void operator()(const framework::ExecutionContext &ctx) const {
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::relu6_grad<XPUType>);
  }
};

P
procr 已提交
187
template <typename T>
188
struct XPUSigmoidFunctor : public BaseActivationFunctor<T> {
189
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
190
  void operator()(const framework::ExecutionContext &ctx) const {
191
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
192
        ctx, xpu::sigmoid<XPUType>);
T
TTerror 已提交
193 194 195
  }
};

196
template <typename T>
197
struct XPUSigmoidGradFunctor : public BaseActivationFunctor<T> {
198
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
199
  void operator()(const framework::ExecutionContext &ctx) const {
200
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
201
        ctx, xpu::sigmoid_grad<XPUType>);
T
TTerror 已提交
202 203 204
  }
};

205
template <typename T>
206
struct XPUSqrtFunctor : public BaseActivationFunctor<T> {
207
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
208
  void operator()(const framework::ExecutionContext &ctx) const {
209 210
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::sqrt<XPUType>);
T
TTerror 已提交
211 212 213
  }
};

214
template <typename T>
215
struct XPUSqrtGradFunctor : public BaseActivationFunctor<T> {
216
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
217
  void operator()(const framework::ExecutionContext &ctx) const {
218
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
219
        ctx, xpu::sqrt_grad<XPUType>);
T
TTerror 已提交
220 221 222
  }
};

223
template <typename T>
224
struct XPUSquareFunctor : public BaseActivationFunctor<T> {
225
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
226
  void operator()(const framework::ExecutionContext &ctx) const {
227 228
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::square<XPUType>);
T
TTerror 已提交
229 230 231
  }
};

232
template <typename T>
T
TTerror 已提交
233
struct XPUSquareGradFunctor : public BaseActivationFunctor<T> {
234
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
235
  void operator()(const framework::ExecutionContext &ctx) const {
236 237
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::square_grad<XPUType>);
T
TTerror 已提交
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
template <typename T>
struct XPUTanhFunctor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
  void operator()(const framework::ExecutionContext &ctx) const {
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::tanh<XPUType>);
  }
};

template <typename T>
struct XPUTanhGradFunctor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
  void operator()(const framework::ExecutionContext &ctx) const {
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::tanh_grad<XPUType>);
  }
};

template <typename T>
struct XPUHardSwishFunctor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
  void operator()(const framework::ExecutionContext &ctx) const {
    float threshold = ctx.Attr<float>("threshold");
    float scale = ctx.Attr<float>("scale");
    float offset = ctx.Attr<float>("offset");
266 267
    PADDLE_ENFORCE_EQ(threshold,
                      6.0f,
268 269
                      platform::errors::External(
                          "Not support threshold [%f] in XPU", threshold));
270
    PADDLE_ENFORCE_EQ(
271 272
        scale,
        6.0f,
273
        platform::errors::External("Not support scale [%f] in XPU", scale));
274
    PADDLE_ENFORCE_EQ(
275 276
        offset,
        3.0f,
277 278 279 280 281 282
        platform::errors::External("Not support offset [%f] in XPU", offset));
    xpu_activation_forward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::hard_swish<XPUType>);
  }
};

283
template <typename T>
T
TTerror 已提交
284
struct XPUHardSwishGradFunctor : public BaseActivationFunctor<T> {
285
  using XPUType = typename XPUTypeTrait<T>::Type;
T
TTerror 已提交
286 287 288 289
  void operator()(const framework::ExecutionContext &ctx) const {
    float threshold = ctx.Attr<float>("threshold");
    float scale = ctx.Attr<float>("scale");
    float offset = ctx.Attr<float>("offset");
290 291
    PADDLE_ENFORCE_EQ(threshold,
                      6.0f,
T
TTerror 已提交
292 293
                      platform::errors::External(
                          "Not support threshold [%f] in XPU", threshold));
294
    PADDLE_ENFORCE_EQ(
295 296
        scale,
        6.0f,
297
        platform::errors::External("Not support scale [%f] in XPU", scale));
T
TTerror 已提交
298
    PADDLE_ENFORCE_EQ(
299 300
        offset,
        3.0f,
T
TTerror 已提交
301
        platform::errors::External("Not support offset [%f] in XPU", offset));
302 303
    xpu_activation_backward<paddle::platform::XPUDeviceContext, T, XPUType>(
        ctx, xpu::hard_swish_grad<XPUType>);
T
TTerror 已提交
304 305 306
  }
};

P
procr 已提交
307
template <typename T>
T
TTerror 已提交
308 309 310 311 312 313 314 315 316 317 318 319
struct XPULeakyReluFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *y = ctx.Output<Tensor>("Out");
    float alpha = ctx.Attr<float>("alpha");
    const T *x_data = x->data<T>();
    T *y_data = y->mutable_data<T>(ctx.GetPlace());

    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
    int r = xpu::leaky_relu(xpu_context, x_data, y_data, x->numel(), alpha);
    PADDLE_ENFORCE_EQ(
320 321 322 323
        r,
        xpu::Error_t::SUCCESS,
        platform::errors::External(
            "XPU leaky_relu return wrong value[%d %s].", r, XPUAPIErrorMsg[r]));
T
TTerror 已提交
324 325 326
  }
};

327
template <typename T>
T
TTerror 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
struct XPULeakyReluGradFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *dOut = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto *dX = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    float alpha = ctx.Attr<float>("alpha");
    const T *x_data = nullptr;
    const T *y_grad = nullptr;
    if (x != nullptr) x_data = x->data<T>();
    if (dOut != nullptr) y_grad = dOut->data<T>();
    T *x_grad = dX->mutable_data<T>(ctx.GetPlace());
    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();

    // The signs of x and y are the same,
    // y == nullptr here,
    // so we give 2 x to the api
345 346 347 348 349 350 351 352 353
    int r = xpu::leaky_relu_grad(xpu_context,
                                 reinterpret_cast<const float *>(x_data),
                                 reinterpret_cast<const float *>(x_data),
                                 reinterpret_cast<const float *>(y_grad),
                                 reinterpret_cast<float *>(x_grad),
                                 dX->numel(),
                                 alpha);
    PADDLE_ENFORCE_EQ(r,
                      xpu::Error_t::SUCCESS,
T
TTerror 已提交
354
                      platform::errors::External(
355 356
                          "XPU leaky_relu_grad return wrong value[%d %s].",
                          r,
T
TTerror 已提交
357 358 359 360
                          XPUAPIErrorMsg[r]));
  }
};

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 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
template <typename T>
struct XPULogGradFunctor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *dOut = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto *dX = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    const T *x_data = nullptr;
    const T *y_grad = nullptr;
    if (x != nullptr) x_data = x->data<T>();
    if (dOut != nullptr) y_grad = dOut->data<T>();
    T *x_grad = dX->mutable_data<T>(ctx.GetPlace());
    auto dev_ctx =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
    const auto x_dims = x->dims();
    auto xshape = phi::vectorize<int>(x_dims);
    int len = x->dims()[x_dims.size() - 1];
    std::vector<int> yshape(1, len);

    xpu::ctx_guard RAII_GUARD(dev_ctx);
    T *y_data = RAII_GUARD.alloc_l3_or_gm<T>(len);
    PADDLE_ENFORCE_XDNN_NOT_NULL(y_data);
    T *tmp_grad = RAII_GUARD.alloc_l3_or_gm<T>(x->numel());
    PADDLE_ENFORCE_XDNN_NOT_NULL(tmp_grad);
    int r = xpu::constant<T>(dev_ctx, y_data, len, static_cast<T>(1.0));
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "constant");

    // dx.device(d) = dout * (static_cast<T>(1) / x);
    r = xpu::broadcast_div(dev_ctx,
                           reinterpret_cast<const float *>(y_data),
                           reinterpret_cast<const float *>(x_data),
                           reinterpret_cast<float *>(tmp_grad),
                           yshape,
                           xshape);
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "broadcast_div");

    r = xpu::broadcast_mul(dev_ctx,
                           reinterpret_cast<const float *>(y_grad),
                           reinterpret_cast<const float *>(tmp_grad),
                           reinterpret_cast<float *>(x_grad),
                           xshape,
                           xshape);
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "broadcast_mul");
  }
};

407 408 409 410 411 412 413 414 415
template <typename T>
struct XPUPowFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *y = ctx.Output<Tensor>("Out");
    auto pow_factor = ctx.Attr<float>("factor");
    const T *x_data = x->data<T>();
    T *y_data = y->mutable_data<T>(ctx.GetPlace());

416
    // allocate temp memory for factor on xpu
417 418
    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
419 420 421 422 423
    xpu::ctx_guard RAII_GUARD(xpu_context);
    T *factor_data = RAII_GUARD.alloc_l3_or_gm<T>(1);
    PADDLE_ENFORCE_NOT_NULL(
        factor_data,
        platform::errors::External("XPU alloc_l3_or_gm returns nullptr"));
424 425 426 427
    memory::Copy(ctx.GetPlace(),
                 static_cast<void *>(factor_data),
                 platform::CPUPlace(),
                 static_cast<void *>(&pow_factor),
428 429 430 431 432
                 sizeof(T));

    // broadcast_pow(Context* ctx, const T* x, const T* y, T* z, const
    // std::vector<int>& xshape, const std::vector<int>& yshape);
    auto x_dims = phi::vectorize<int>(x->dims());
433 434
    int r = xpu::broadcast_pow(
        xpu_context, x_data, factor_data, y_data, x_dims, {1});
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "broadcast_pow");
  }
};

template <typename T>
struct XPUPowGradFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *dOut = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto *dX = ctx.Output<framework::Tensor>(framework::GradVarName("X"));

    const T *x_data = x->data<T>();
    const T *y_grad = dOut->data<T>();
    T *x_grad = dX->mutable_data<T>(ctx.GetPlace());

    // check dims: all dims should equal
    auto x_dims = phi::vectorize<int>(x->dims());
    auto dy_dims = phi::vectorize<int>(dOut->dims());
    auto dx_dims = phi::vectorize<int>(dX->dims());
454
    PADDLE_ENFORCE_EQ(
455 456
        x_dims,
        dy_dims,
457 458
        platform::errors::PreconditionNotMet("x_dims should match dy_dims."));
    PADDLE_ENFORCE_EQ(
459 460
        x_dims,
        dx_dims,
461
        platform::errors::PreconditionNotMet("x_dims should match dx_dims."));
462 463 464 465 466 467
    float pow_factor = ctx.Attr<float>("factor");

    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
    // int pow_grad(Context* ctx, const T* x, const T* dy, T* dx, int len, float
    // factor);
468 469
    int r = xpu::pow_grad(
        xpu_context, x_data, y_grad, x_grad, x->numel(), pow_factor);
470
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "pow_grad");
471 472 473
  }
};

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
template <typename T>
struct XPUReluFunctor : public BaseActivationFunctor<T> {
  using XPUType = typename XPUTypeTrait<T>::Type;
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *y = ctx.Output<Tensor>("Out");
    const XPUType *x_data = reinterpret_cast<const XPUType *>(x->data<T>());
    XPUType *y_data =
        reinterpret_cast<XPUType *>(y->mutable_data<T>(ctx.GetPlace()));

    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
    int r =
        xpu::relu(xpu_context, x_data, y_data, x->numel(), nullptr, nullptr);
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "relu");
  }
};

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
template <typename T>
struct XPUSoftPlusFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *y = ctx.Output<Tensor>("Out");
    const T *x_data = x->data<T>();
    T *y_data = y->mutable_data<T>(ctx.GetPlace());

    float beta = ctx.Attr<float>("beta");
    float threshold = ctx.Attr<float>("threshold");

    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
    int r =
        xpu::softplus(xpu_context, x_data, y_data, x->numel(), beta, threshold);
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "softplus");
  }
};

template <typename T>
struct XPUSoftPlusGradFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *dOut = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto *dX = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    const T *x_data = x->data<T>();
    const T *y_grad = dOut->data<T>();
    T *x_grad = dX->mutable_data<T>(ctx.GetPlace());

    float beta = ctx.Attr<float>("beta");
    float threshold = ctx.Attr<float>("threshold");

    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
526 527 528 529 530 531 532 533 534
    int r = xpu::softplus_grad(xpu_context,
                               reinterpret_cast<const float *>(x_data),
                               reinterpret_cast<const float *>(
                                   x_data),  // softplus_grad do not need y_data
                               reinterpret_cast<const float *>(y_grad),
                               reinterpret_cast<float *>(x_grad),
                               dX->numel(),
                               beta,
                               threshold);
535 536 537 538
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "softplus_grad");
  }
};

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
template <typename T>
struct XPUSwishFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *y = ctx.Output<Tensor>("Out");
    const T *x_data = x->data<T>();
    T *y_data = y->mutable_data<T>(ctx.GetPlace());

    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
    // int swish(Context* ctx, const T* x, T* y, int len);
    int r = xpu::swish(xpu_context, x_data, y_data, x->numel());
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "swish");
  }
};

template <typename T>
struct XPUSwishGradFunctor : public BaseActivationFunctor<T> {
  void operator()(const framework::ExecutionContext &ctx) const {
    const auto *x = ctx.Input<Tensor>("X");
    auto *dOut = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto *dX = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    const T *x_data = x->data<T>();
    const T *y_grad = dOut->data<T>();
    T *x_grad = dX->mutable_data<T>(ctx.GetPlace());

    auto xpu_context =
        ctx.device_context<paddle::platform::XPUDeviceContext>().x_context();
    // int swish_grad(Context* ctx, const T* x, const T* dy, T* dx, int len);
    int r = xpu::swish_grad(xpu_context, x_data, y_grad, x_grad, dX->numel());
    PADDLE_ENFORCE_XDNN_SUCCESS(r, "swish_grad");
  }
};

573 574 575 576 577 578 579 580 581 582 583 584
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

#define REGISTER_ACTIVATION_XPU_KERNEL(act_type, functor, grad_functor)  \
  REGISTER_OP_XPU_KERNEL(act_type,                                       \
                         ops::XPUActivationKernel<ops::functor<float>>); \
  REGISTER_OP_XPU_KERNEL(                                                \
      act_type##_grad,                                                   \
      ops::XPUActivationGradKernel<ops::grad_functor<float>>);

585
REGISTER_ACTIVATION_XPU_KERNEL(abs, XPUAbsFunctor, XPUAbsGradFunctor)
586 587
REGISTER_ACTIVATION_XPU_KERNEL(hard_swish,
                               XPUHardSwishFunctor,
588
                               XPUHardSwishGradFunctor)
589 590
REGISTER_ACTIVATION_XPU_KERNEL(leaky_relu,
                               XPULeakyReluFunctor,
591
                               XPULeakyReluGradFunctor)
592 593
REGISTER_ACTIVATION_XPU_KERNEL(reciprocal,
                               XPUReciprocalFunctor,
594
                               XPUReciprocalGradFunctor)
595 596
REGISTER_ACTIVATION_XPU_KERNEL(sigmoid,
                               XPUSigmoidFunctor,
597 598
                               XPUSigmoidGradFunctor)
REGISTER_ACTIVATION_XPU_KERNEL(sqrt, XPUSqrtFunctor, XPUSqrtGradFunctor)
T
TTerror 已提交
599
REGISTER_ACTIVATION_XPU_KERNEL(square, XPUSquareFunctor, XPUSquareGradFunctor)
600 601
REGISTER_ACTIVATION_XPU_KERNEL(softplus,
                               XPUSoftPlusFunctor,
602
                               XPUSoftPlusGradFunctor)
603 604
REGISTER_ACTIVATION_XPU_KERNEL(swish, XPUSwishFunctor, XPUSwishGradFunctor)
REGISTER_ACTIVATION_XPU_KERNEL(pow, XPUPowFunctor, XPUPowGradFunctor)
605

606
REGISTER_OP_XPU_KERNEL(
607 608
    relu,
    ops::XPUActivationKernel<ops::XPUReluFunctor<float>>,
609 610
    ops::XPUActivationKernel<ops::XPUReluFunctor<paddle::platform::float16>>);
REGISTER_OP_XPU_KERNEL(
611 612
    relu_grad,
    ops::XPUActivationGradKernel<ops::XPUReluGradFunctor<float>>,
613 614
    ops::XPUActivationGradKernel<
        ops::XPUReluGradFunctor<paddle::platform::float16>>);
D
dongfangshenzhu 已提交
615 616 617 618
REGISTER_OP_XPU_KERNEL(relu6,
                       ops::XPUActivationKernel<ops::XPURelu6Functor<float>>);
REGISTER_OP_XPU_KERNEL(
    relu6_grad, ops::XPUActivationKernel<ops::XPURelu6GradFunctor<float>>);
619
REGISTER_OP_XPU_KERNEL(
620 621
    tanh,
    ops::XPUActivationKernel<ops::XPUTanhFunctor<float>>,
622 623
    ops::XPUActivationKernel<ops::XPUTanhFunctor<paddle::platform::float16>>);
REGISTER_OP_XPU_KERNEL(
624 625
    tanh_grad,
    ops::XPUActivationGradKernel<ops::XPUTanhGradFunctor<float>>,
626 627 628
    ops::XPUActivationGradKernel<
        ops::XPUTanhGradFunctor<paddle::platform::float16>>);

629 630
REGISTER_OP_XPU_KERNEL(exp,
                       ops::XPUActivationKernel<ops::XPUExpFunctor<float>>);
631 632
REGISTER_OP_XPU_KERNEL(log,
                       ops::XPUActivationKernel<ops::XPULogFunctor<float>>);
633 634
REGISTER_OP_XPU_KERNEL(
    log_grad, ops::XPUActivationGradKernel<ops::XPULogGradFunctor<float>>);
635
#endif  // PADDLE_WITH_XPU