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

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

#include <memory>
#include <string>

18
#include "paddle/fluid/framework/framework.pb.h"
19 20
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/operators/activation_op.h"
21
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"
22
#include "paddle/phi/core/ddim.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

template <typename DeviceContext, typename T>
class PowNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    auto factor = ctx.Attr<float>("factor");

    out->mutable_data<T>(ctx.GetPlace());

39 40 41
    const auto& runner = NpuOpRunner("Power",
                                     {*x},
                                     {*out},
L
Leo Chen 已提交
42 43 44
                                     {{"power", factor},
                                      {"scale", static_cast<float>(1.0)},
                                      {"shift", static_cast<float>(0.0)}});
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class PowGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto factor = ctx.Attr<float>("factor");

    auto x_dims = x->dims();

    auto place = ctx.GetPlace();
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    // NOTE(liym27): dx = dout * factor * x.pow(factor-1)

    // Step1: Compute x_pow = x.pow(factor-1)
    Tensor x_pow(x->type());
    x_pow.mutable_data<T>(x->dims(), place);
L
Leo Chen 已提交
74 75
    const auto& runner_pow = NpuOpRunner(
        "Power", {*x}, {x_pow}, {{"power", factor - static_cast<float>(1)}});
76 77 78 79 80
    runner_pow.Run(stream);

    // Step 2: Construct a broadcast factor, which has the same shape with x.

    // 2.1 Get a factor tensor with shape [1].
81
    Tensor factor_tensor(experimental::DataType::FLOAT32);
82
    factor_tensor.mutable_data<float>({1}, place);
83
    FillNpuTensorWithConstant<float>(&factor_tensor, factor);
84 85 86

    // 2.2 Get the factor which has the shape with x and the same value with
    // factor.
87
    Tensor factor_bc_tensor(experimental::DataType::FLOAT32);
88
    factor_bc_tensor.mutable_data<float>(x_dims, place);
89 90 91 92
    const auto& runner_bc = NpuOpRunner("FillD",
                                        {factor_tensor},
                                        {factor_bc_tensor},
                                        {{"dims", phi::vectorize(x_dims)}});
93 94 95 96 97
    runner_bc.Run(stream);

    // Step 3: Compute x_power_mul_factor = factor * x.pow(factor-1)
    Tensor x_power_mul_factor(x->type());
    x_power_mul_factor.mutable_data<T>(x->dims(), place);
L
Leo Chen 已提交
98
    const auto& runner_mul_1 =
99 100 101 102 103
        NpuOpRunner("Mul", {factor_bc_tensor, x_pow}, {x_power_mul_factor}, {});
    runner_mul_1.Run(stream);

    // Step 4: Compute dx = dout * factor * x.pow(factor-1)
    dx->mutable_data<T>(place);
L
Leo Chen 已提交
104
    const auto& runner_mul_2 =
105 106 107 108 109 110 111 112 113 114 115 116 117 118
        NpuOpRunner("Mul", {*dout, x_power_mul_factor}, {*dx}, {});
    runner_mul_2.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class ReluNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");

    out->mutable_data<T>(ctx.GetPlace());

L
Leo Chen 已提交
119 120 121 122
    const auto& runner = NpuOpRunner("Relu",
                                     {
                                         *x,
                                     },
123 124
                                     {*out},
                                     {});
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class ReluGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    dx->mutable_data<T>(ctx.GetPlace());
L
Leo Chen 已提交
146
    const auto& runner = NpuOpRunner("ReluGrad", {*dout, *out}, {*dx}, {});
147 148 149 150 151

    runner.Run(stream);
  }
};

152 153 154 155 156 157 158 159 160 161 162 163 164
template <typename DeviceContext, typename T>
class Relu6NPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");

    out->mutable_data<T>(ctx.GetPlace());

    const auto& runner = NpuOpRunner("Relu6",
                                     {
                                         *x,
                                     },
165 166
                                     {*out},
                                     {});
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class Relu6GradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    dx->mutable_data<T>(ctx.GetPlace());
    const auto& runner = NpuOpRunner("Relu6Grad", {*dout, *out}, {*dx}, {});

    runner.Run(stream);
  }
};

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
template <typename DeviceContext, typename T>
class SqrtNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");

    auto* out = ctx.Output<Tensor>("Out");

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

L
Leo Chen 已提交
210
    const auto& runner = NpuOpRunner("Sqrt", {*x}, {*out}, {});
211 212 213 214
    runner.Run(stream);
  }
};

J
Jackwaterveg 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
template <typename DeviceContext, typename T>
class LeakyReluNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    auto alpha = ctx.Attr<float>("alpha");

    out->mutable_data<T>(ctx.GetPlace());

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& runner =
        NpuOpRunner("LeakyRelu", {*x}, {*out}, {{"negative_slope", alpha}});
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class LeakyReluGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto alpha = ctx.Attr<float>("alpha");

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    dx->mutable_data<T>(ctx.GetPlace());
249 250
    const auto& runner = NpuOpRunner(
        "LeakyReluGrad", {*dout, *x}, {*dx}, {{"negative_slope", alpha}});
J
Jackwaterveg 已提交
251 252 253 254 255

    runner.Run(stream);
  }
};

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
template <typename DeviceContext, typename T>
class SqrtGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));

    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto place = ctx.GetPlace();

    dx->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

L
Leo Chen 已提交
273 274
    const auto& runner_dx = NpuOpRunner("SqrtGrad", {*out, *dout}, {*dx}, {});
    runner_dx.Run(stream);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  }
};

template <typename DeviceContext, typename T>
class LogNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");

    auto* out = ctx.Output<Tensor>("Out");

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    Tensor one(x->type());
    one.mutable_data<T>(x->dims(), place);
L
Leo Chen 已提交
296 297
    const auto& runner_one = NpuOpRunner("OnesLike", {*x}, {one}, {});
    runner_one.Run(stream);
298 299 300

    Tensor sub(x->type());
    sub.mutable_data<T>(x->dims(), place);
L
Leo Chen 已提交
301 302
    const auto& runner_sub = NpuOpRunner("Sub", {*x, one}, {sub}, {});
    runner_sub.Run(stream);
303

L
Leo Chen 已提交
304 305
    const auto& runner_out = NpuOpRunner("Log1p", {sub}, {*out}, {});
    runner_out.Run(stream);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  }
};

template <typename DeviceContext, typename T>
class LogGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* x = ctx.Input<Tensor>("X");

    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto place = ctx.GetPlace();

    dx->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
L
Leo Chen 已提交
325
    const auto& runner = NpuOpRunner("DivNoNan", {*dout, *x}, {*dx}, {});
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class TanhNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");

    auto* out = ctx.Output<Tensor>("Out");

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

L
Leo Chen 已提交
346
    const auto& runner = NpuOpRunner("Tanh", {*x}, {*out}, {});
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class TanhGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* out = ctx.Input<Tensor>("Out");

    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto place = ctx.GetPlace();

    dx->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

L
Leo Chen 已提交
368 369
    const auto& runner_dx = NpuOpRunner("TanhGrad", {*out, *dout}, {*dx}, {});
    runner_dx.Run(stream);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  }
};

template <typename DeviceContext, typename T>
class SquareNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");

    auto* out = ctx.Output<Tensor>("Out");

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

L
Leo Chen 已提交
389
    const auto& runner = NpuOpRunner("Square", {*x}, {*out}, {});
390 391 392 393
    runner.Run(stream);
  }
};

J
Jackwaterveg 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
template <typename DeviceContext, typename T>
class SquareGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto factor = static_cast<float>(2.0);

    auto place = ctx.GetPlace();
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    // Step 1: Compute x_muls_factor = factor * x
    Tensor x_muls_factor(x->type());
    x_muls_factor.mutable_data<T>(x->dims(), place);
    const auto& runner_muls_1 =
        NpuOpRunner("Muls", {*x}, {x_muls_factor}, {{"value", factor}});
    runner_muls_1.Run(stream);

    // Step 2: Compute dx = dout * factor * x
    dx->mutable_data<T>(place);
    const auto& runner_mul_2 =
        NpuOpRunner("Mul", {*dout, x_muls_factor}, {*dx}, {});
    runner_mul_2.Run(stream);
  }
};

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
template <typename DeviceContext, typename T>
class SigmoidNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");

    auto* out = ctx.Output<Tensor>("Out");

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& runner = NpuOpRunner("Sigmoid", {*x}, {*out}, {});
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class SigmoidGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* out = ctx.Input<Tensor>("Out");

    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto place = ctx.GetPlace();

    dx->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& runner_dx =
        NpuOpRunner("SigmoidGrad", {*out, *dout}, {*dx}, {});
    runner_dx.Run(stream);
  }
};

R
ronnywang 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 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
// Swish = x * sigmoid(beta * x)
template <typename T>
class SwishNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    float beta = ctx.Attr<float>("beta");

    out->mutable_data<T>(ctx.GetPlace());
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& muls_runner =
        NpuOpRunner("Muls", {*x}, {*out}, {{"value", beta}});
    muls_runner.Run(stream);

    const auto& sigmoid_runner = NpuOpRunner("Sigmoid", {*out}, {*out}, {});
    sigmoid_runner.Run(stream);

    const auto& mul_runner = NpuOpRunner("Mul", {*x, *out}, {*out});
    mul_runner.Run(stream);
  }
};

template <typename T>
class SwishGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    float beta = ctx.Attr<float>("beta");

    dx->mutable_data<T>(ctx.GetPlace());
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    Tensor beta_x, sigmoid_out, swish_out;
    beta_x.mutable_data<T>(x->dims(), ctx.GetPlace());
    sigmoid_out.mutable_data<T>(x->dims(), ctx.GetPlace());
    swish_out.mutable_data<T>(x->dims(), ctx.GetPlace());
    const auto& muls_runner =
        NpuOpRunner("Muls", {*x}, {beta_x}, {{"value", beta}});
    muls_runner.Run(stream);

    const auto& sigmoid_runner =
        NpuOpRunner("Sigmoid", {beta_x}, {sigmoid_out}, {});
    sigmoid_runner.Run(stream);

    const auto& mul_runner =
        NpuOpRunner("Mul", {sigmoid_out, *x}, {swish_out}, {});
    mul_runner.Run(stream);
522 523 524
    const auto& muls_runner2 =
        NpuOpRunner("Muls", {swish_out}, {swish_out}, {{"value", beta}});
    muls_runner2.Run(stream);
R
ronnywang 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

    const auto& mul_runner1 =
        NpuOpRunner("Mul", {sigmoid_out, swish_out}, {*dx}, {});
    mul_runner1.Run(stream);

    const auto& sub_runner = NpuOpRunner("Sub", {swish_out, *dx}, {*dx}, {});
    sub_runner.Run(stream);

    const auto& add_runner = NpuOpRunner("Add", {sigmoid_out, *dx}, {*dx}, {});
    add_runner.Run(stream);

    const auto& mul_runner2 = NpuOpRunner("Mul", {*dout, *dx}, {*dx}, {});
    mul_runner2.Run(stream);
  }
};

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 573 574 575 576 577 578 579 580
// HardSwish = min(max(0, x+offset), threshold) * x / scale
template <typename T>
class HardSwishNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");

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

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    Tensor tensor_offset(x->type());
    tensor_offset.mutable_data<T>({1}, place);
    FillNpuTensorWithConstant<T>(&tensor_offset, static_cast<T>(offset));

    Tensor add_offset_val(x->type());
    add_offset_val.mutable_data<T>(x->dims(), place);
    const auto& runner_add =
        NpuOpRunner("AddV2", {*x, tensor_offset}, {add_offset_val});
    runner_add.Run(stream);

    Tensor tensor_threshold(x->type());
    tensor_threshold.mutable_data<T>({1}, place);
    FillNpuTensorWithConstant<T>(&tensor_threshold, static_cast<T>(threshold));

    Tensor tensor_zero(x->type());
    tensor_zero.mutable_data<T>({1}, place);
    FillNpuTensorWithConstant<T>(&tensor_zero, static_cast<T>(0.0));

    Tensor clip_val(x->type());
    clip_val.mutable_data<T>(x->dims(), place);
581 582 583 584
    const auto& runner_clip =
        NpuOpRunner("ClipByValue",
                    {add_offset_val, tensor_zero, tensor_threshold},
                    {clip_val});
585 586 587 588 589 590 591 592
    runner_clip.Run(stream);

    Tensor tensor_scale_tmp(x->type());
    tensor_scale_tmp.mutable_data<T>({1}, place);
    FillNpuTensorWithConstant<T>(&tensor_scale_tmp, static_cast<T>(scale));
    Tensor tensor_scale(x->type());
    tensor_scale.mutable_data<T>(x->dims(), place);
    const auto& runner_fill =
593 594 595
        NpuOpRunner("FillD",
                    {tensor_scale_tmp},
                    {tensor_scale},
596
                    {{"dims", phi::vectorize(x->dims())}});
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    runner_fill.Run(stream);

    Tensor div_val(x->type());
    div_val.mutable_data<T>(x->dims(), place);
    const auto& runner_div =
        NpuOpRunner("Div", {clip_val, tensor_scale}, {div_val});
    runner_div.Run(stream);

    const auto& runner_mul = NpuOpRunner("Mul", {*x, div_val}, {*out});
    runner_mul.Run(stream);
  }
};

template <typename T>
class HardSwishGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

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

    auto place = ctx.GetPlace();

    dx->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    Tensor tensor_offset(x->type());
    tensor_offset.mutable_data<T>({1}, place);
    FillNpuTensorWithConstant<T>(&tensor_offset, static_cast<T>(offset));

    Tensor add_offset_val(x->type());
    add_offset_val.mutable_data<T>(x->dims(), place);
    const auto& runner_add =
        NpuOpRunner("AddV2", {*x, tensor_offset}, {add_offset_val});
    runner_add.Run(stream);

    Tensor tmp1(x->type());
    tmp1.mutable_data<T>(x->dims(), place);
642 643
    const auto& runner_pow1 = NpuOpRunner(
        "Power", {*x}, {tmp1}, {{"scale", 2.0f}, {"shift", offset}});
644 645 646 647 648
    runner_pow1.Run(stream);

    Tensor tmp2(x->type());
    tmp2.mutable_data<T>(x->dims(), place);
    const auto& runner_ht_grad =
649 650 651
        NpuOpRunner("HardtanhGrad",
                    {add_offset_val, tmp1},
                    {tmp2},
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
                    {{"min_val", 0.0f}, {"max_val", threshold}});
    runner_ht_grad.Run(stream);

    Tensor tmp3(x->type());
    tmp3.mutable_data<T>(x->dims(), place);
    const auto& runner_pow2 = NpuOpRunner(
        "Power", {tmp2}, {tmp3}, {{"scale", 1.0f / scale}, {"shift", 1.0f}});
    runner_pow2.Run(stream);

    Tensor tensor_threshold_tmp(x->type());
    tensor_threshold_tmp.mutable_data<T>({1}, place);
    FillNpuTensorWithConstant<T>(&tensor_threshold_tmp,
                                 static_cast<T>(threshold));
    Tensor tensor_threshold(x->type());
    tensor_threshold.mutable_data<T>(x->dims(), place);
    const auto& runner_fill =
668 669 670
        NpuOpRunner("FillD",
                    {tensor_threshold_tmp},
                    {tensor_threshold},
671
                    {{"dims", phi::vectorize(x->dims())}});
672 673
    runner_fill.Run(stream);

674
    Tensor tmp_bool(experimental::DataType::BOOL);
675 676 677 678 679 680
    tmp_bool.mutable_data<bool>(x->dims(), place);
    const auto& runner_less =
        NpuOpRunner("Less", {add_offset_val, tensor_threshold}, {tmp_bool});
    runner_less.Run(stream);
    Tensor tmp4(x->type());
    tmp4.mutable_data<T>(x->dims(), place);
681 682
    auto dst_dtype =
        ConvertToNpuDtype(framework::TransToProtoVarType(x->type()));
683
    const auto& runner_cast =
684 685 686
        NpuOpRunner("Cast",
                    {tmp_bool},
                    {tmp4},
687 688 689 690 691 692 693 694 695 696 697 698 699
                    {{"dst_type", static_cast<int>(dst_dtype)}});
    runner_cast.Run(stream);

    Tensor tmp5(x->type());
    tmp5.mutable_data<T>(x->dims(), place);
    const auto& runner_sub = NpuOpRunner("Sub", {tmp3, tmp4}, {tmp5});
    runner_sub.Run(stream);

    const auto& runner_final = NpuOpRunner("Mul", {tmp5, *dout}, {*dx});
    runner_final.Run(stream);
  }
};

F
furnace 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
template <typename DeviceContext, typename T>
class HardSigmoidNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    float slope = ctx.Attr<float>("slope");
    float offset = ctx.Attr<float>("offset");

    out->mutable_data<T>(ctx.GetPlace());

    framework::NPUAttributeMap attr_input = {{"alpha", slope},
                                             {"beta", offset}};

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& runner = NpuOpRunner("HardSigmoid", {*x}, {*out}, attr_input);
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class HardSigmoidGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* out = ctx.Input<Tensor>("Out");

    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    float slope = ctx.Attr<float>("slope");
    float offset = ctx.Attr<float>("offset");

    dx->mutable_data<T>(ctx.GetPlace());

    framework::NPUAttributeMap attr_input = {{"alpha", slope},
                                             {"beta", offset}};

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& runner_dx =
        NpuOpRunner("HardSigmoidGrad", {*dout, *out}, {*dx}, attr_input);
    runner_dx.Run(stream);
  }
};

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
template <typename DeviceContext, typename T>
class ReciprocalNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    auto place = ctx.GetPlace();
    out->mutable_data<T>(place);
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    const auto& runner = NpuOpRunner("Reciprocal", {*x}, {*out}, {});
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class ReciprocalGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto place = ctx.GetPlace();
    dx->mutable_data<T>(place);
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    const auto& runner_dx =
        NpuOpRunner("ReciprocalGrad", {*out, *dout}, {*dx}, {});
    runner_dx.Run(stream);
  }
};

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
template <typename DeviceContext, typename T>
class CosNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");

    auto place = ctx.GetPlace();
    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& runner = NpuOpRunner("Cos", {*x}, {*out}, {});
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class CosGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* x = ctx.Input<Tensor>("X");
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));

    auto place = ctx.GetPlace();
    dx->mutable_data<T>(place);

    Tensor sin_out(x->type());  // Temporary Tensor
    sin_out.Resize(x->dims());
    sin_out.mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    const auto& runner = NpuOpRunner("Sin", {*x}, {sin_out}, {});
    runner.Run(stream);

    const auto& runner_dx = NpuOpRunner("Mul", {*dout, sin_out}, {*dx}, {});
    runner_dx.Run(stream);

    Tensor tmp(x->type());  // Temporary Tensor
828
    tmp.Resize(phi::make_ddim({1, 1}));
829 830 831 832 833 834 835 836 837 838
    tmp.mutable_data<T>(place);
    float factor = -1.;
    FillNpuTensorWithConstant<T>(&tmp, static_cast<T>(factor));

    const auto& runner_dx_ = NpuOpRunner("Xdivy", {*dx, tmp}, {*dx}, {});
    runner_dx_.Run(stream);
    // dx = -dout * Sine(x);
  }
};

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 864 865 866 867 868 869 870 871
template <typename DeviceContext, typename T>
class AtanNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* out = ctx.Output<Tensor>("Out");
    auto place = ctx.GetPlace();
    out->mutable_data<T>(place);
    const auto& runner = NpuOpRunner("Atan", {*x}, {*out}, {});
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class AtanGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* x = ctx.Input<Tensor>("X");
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto place = ctx.GetPlace();
    dx->mutable_data<T>(place);
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    const auto& runner_dx = NpuOpRunner("AtanGrad", {*x, *dout}, {*dx}, {});
    runner_dx.Run(stream);
  }
};

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
template <typename DeviceContext, typename T>
class ExpNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<framework::Tensor>("X");
    auto* out = ctx.Output<framework::Tensor>("Out");
    out->mutable_data<T>(ctx.GetPlace());
    const auto& runner = NpuOpRunner("Exp", {*x}, {*out}, {});
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    runner.Run(stream);
  }
};

template <typename DeviceContext, typename T>
class ExpGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* out = ctx.Input<Tensor>("Out");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    dx->mutable_data<T>(ctx.GetPlace());
    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();
    const auto& runner = NpuOpRunner("Mul", {*dout, *out}, {*dx}, {});
    runner.Run(stream);
  }
};

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
template <typename DeviceContext, typename T>
class SinNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");

    auto* out = ctx.Output<Tensor>("Out");

    auto place = ctx.GetPlace();

    out->mutable_data<T>(place);

    auto stream =
        ctx.template device_context<paddle::platform::NPUDeviceContext>()
            .stream();

    const auto& runner = NpuOpRunner("Sin", {*x}, {*out}, {});
    runner.Run(stream);
  }
};

924 925 926 927 928 929
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OP_NPU_KERNEL(
930 931
    pow,
    ops::PowNPUKernel<paddle::platform::NPUDeviceContext, float>,
932 933 934 935
    ops::PowNPUKernel<paddle::platform::NPUDeviceContext,
                      paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
936 937
    pow_grad,
    ops::PowGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
938 939 940 941
    ops::PowGradNPUKernel<paddle::platform::NPUDeviceContext,
                          paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
942 943
    relu,
    ops::ReluNPUKernel<paddle::platform::NPUDeviceContext, float>,
944 945 946 947 948 949 950 951 952
    ops::ReluNPUKernel<paddle::platform::NPUDeviceContext,
                       paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    relu_grad,
    ops::ReluGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::ReluGradNPUKernel<paddle::platform::NPUDeviceContext,
                           paddle::platform::float16>);

953
REGISTER_OP_NPU_KERNEL(
954 955
    relu6,
    ops::Relu6NPUKernel<paddle::platform::NPUDeviceContext, float>,
956 957 958 959 960 961 962 963 964
    ops::Relu6NPUKernel<paddle::platform::NPUDeviceContext,
                        paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    relu6_grad,
    ops::Relu6GradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::Relu6GradNPUKernel<paddle::platform::NPUDeviceContext,
                            paddle::platform::float16>);

J
Jackwaterveg 已提交
965 966 967 968 969 970 971 972 973 974 975 976
REGISTER_OP_NPU_KERNEL(
    leaky_relu,
    ops::LeakyReluNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::LeakyReluNPUKernel<paddle::platform::NPUDeviceContext,
                            paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    leaky_relu_grad,
    ops::LeakyReluGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::LeakyReluGradNPUKernel<paddle::platform::NPUDeviceContext,
                                paddle::platform::float16>);

977
REGISTER_OP_NPU_KERNEL(
978 979
    sqrt,
    ops::SqrtNPUKernel<paddle::platform::NPUDeviceContext, float>,
980 981 982 983 984 985 986 987 988 989
    ops::SqrtNPUKernel<paddle::platform::NPUDeviceContext,
                       paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    sqrt_grad,
    ops::SqrtGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::SqrtGradNPUKernel<paddle::platform::NPUDeviceContext,
                           paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
990 991
    log,
    ops::LogNPUKernel<paddle::platform::NPUDeviceContext, float>,
992 993 994 995
    ops::LogNPUKernel<paddle::platform::NPUDeviceContext,
                      paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
996 997
    log_grad,
    ops::LogGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
998 999 1000 1001
    ops::LogGradNPUKernel<paddle::platform::NPUDeviceContext,
                          paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
1002 1003
    tanh,
    ops::TanhNPUKernel<paddle::platform::NPUDeviceContext, float>,
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    ops::TanhNPUKernel<paddle::platform::NPUDeviceContext,
                       paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    tanh_grad,
    ops::TanhGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::TanhGradNPUKernel<paddle::platform::NPUDeviceContext,
                           paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
1014 1015
    square,
    ops::SquareNPUKernel<paddle::platform::NPUDeviceContext, float>,
1016 1017 1018
    ops::SquareNPUKernel<paddle::platform::NPUDeviceContext,
                         paddle::platform::float16>,
    ops::SquareNPUKernel<paddle::platform::NPUDeviceContext, int>);
1019

J
Jackwaterveg 已提交
1020 1021 1022 1023 1024 1025
REGISTER_OP_NPU_KERNEL(
    square_grad,
    ops::SquareGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::SquareNPUKernel<paddle::platform::NPUDeviceContext,
                         paddle::platform::float16>);

1026
REGISTER_OP_NPU_KERNEL(
1027 1028
    sigmoid,
    ops::SigmoidNPUKernel<paddle::platform::NPUDeviceContext, float>,
1029 1030 1031 1032 1033 1034 1035 1036
    ops::SigmoidNPUKernel<paddle::platform::NPUDeviceContext,
                          paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    sigmoid_grad,
    ops::SigmoidGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::SigmoidGradNPUKernel<paddle::platform::NPUDeviceContext,
                              paddle::platform::float16>);
F
furnace 已提交
1037

1038 1039
REGISTER_OP_NPU_KERNEL(swish,
                       ops::SwishNPUKernel<float>,
R
ronnywang 已提交
1040 1041
                       ops::SwishNPUKernel<paddle::platform::float16>);

1042 1043
REGISTER_OP_NPU_KERNEL(swish_grad,
                       ops::SwishGradNPUKernel<float>,
R
ronnywang 已提交
1044 1045
                       ops::SwishGradNPUKernel<paddle::platform::float16>);

1046 1047
REGISTER_OP_NPU_KERNEL(hard_swish,
                       ops::HardSwishNPUKernel<float>,
1048 1049
                       ops::HardSwishNPUKernel<paddle::platform::float16>);

1050 1051
REGISTER_OP_NPU_KERNEL(hard_swish_grad,
                       ops::HardSwishGradNPUKernel<float>,
1052 1053
                       ops::HardSwishGradNPUKernel<paddle::platform::float16>);

F
furnace 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
REGISTER_OP_NPU_KERNEL(
    hard_sigmoid,
    ops::HardSigmoidNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::HardSigmoidNPUKernel<paddle::platform::NPUDeviceContext,
                              paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    hard_sigmoid_grad,
    ops::HardSigmoidGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::HardSigmoidGradNPUKernel<paddle::platform::NPUDeviceContext,
                                  paddle::platform::float16>);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

REGISTER_OP_NPU_KERNEL(
    reciprocal,
    ops::ReciprocalNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::ReciprocalNPUKernel<paddle::platform::NPUDeviceContext, double>,
    ops::ReciprocalNPUKernel<paddle::platform::NPUDeviceContext,
                             paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    reciprocal_grad,
    ops::ReciprocalGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::ReciprocalGradNPUKernel<paddle::platform::NPUDeviceContext, double>,
    ops::ReciprocalGradNPUKernel<paddle::platform::NPUDeviceContext,
                                 paddle::platform::float16>);
1079 1080

REGISTER_OP_NPU_KERNEL(
1081 1082
    cos,
    ops::CosNPUKernel<paddle::platform::NPUDeviceContext, float>,
1083 1084 1085 1086
    ops::CosNPUKernel<paddle::platform::NPUDeviceContext,
                      paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
1087 1088
    cos_grad,
    ops::CosGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
1089 1090
    ops::CosGradNPUKernel<paddle::platform::NPUDeviceContext,
                          paddle::platform::float16>);
1091 1092

REGISTER_OP_NPU_KERNEL(
1093 1094
    atan,
    ops::AtanNPUKernel<paddle::platform::NPUDeviceContext, float>,
1095 1096 1097 1098 1099 1100 1101 1102
    ops::AtanNPUKernel<paddle::platform::NPUDeviceContext,
                       paddle::platform::float16>);

REGISTER_OP_NPU_KERNEL(
    atan_grad,
    ops::AtanGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
    ops::AtanGradNPUKernel<paddle::platform::NPUDeviceContext,
                           paddle::platform::float16>);
1103 1104

REGISTER_OP_NPU_KERNEL(
1105 1106
    exp,
    ops::ExpNPUKernel<paddle::platform::NPUDeviceContext, float>,
1107 1108 1109
    ops::ExpNPUKernel<paddle::platform::NPUDeviceContext, double>);

REGISTER_OP_NPU_KERNEL(
1110 1111
    exp_grad,
    ops::ExpGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
1112
    ops::ExpGradNPUKernel<paddle::platform::NPUDeviceContext, double>);
1113 1114

REGISTER_OP_NPU_KERNEL(
1115 1116
    sin,
    ops::SinNPUKernel<paddle::platform::NPUDeviceContext, float>,
1117 1118 1119
    ops::SinNPUKernel<paddle::platform::NPUDeviceContext, double>,
    ops::SinNPUKernel<paddle::platform::NPUDeviceContext,
                      paddle::platform::float16>);