elementwise_functor.h 20.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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 License. */

#pragma once

17
#include "paddle/phi/common/bfloat16.h"
18
#include "paddle/phi/common/complex.h"
19 20 21
#include "paddle/phi/common/float16.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/hostdevice.h"
22
#include "paddle/phi/core/macros.h"
23 24
#if defined(__xpu__)
#include <xpu/runtime.h>
25

C
Chen Weihang 已提交
26
#include "xpu/kernel/math_xpu2.h"  // pow()
27
#endif
28
#include "paddle/phi/common/amp_type_traits.h"
29

30
namespace phi {
31 32 33
namespace funcs {

// Define the binary functors used in elementwise ops.
34
// Note: InverseXxxFunctor is needed when calling ElementwiseComputeEx on CPU.
35 36 37 38

// Add
template <typename T>
struct AddFunctor {
39
  inline HOSTDEVICE T operator()(const T a, const T b) const { return a + b; }
40 41 42
};
template <typename T>
struct InverseAddFunctor {
43
  inline HOSTDEVICE T operator()(const T a, const T b) const { return b + a; }
44 45 46 47 48
};

// Subtract
template <typename T>
struct SubtractFunctor {
49
  inline HOSTDEVICE T operator()(const T a, const T b) const { return a - b; }
50 51 52
};
template <typename T>
struct InverseSubtractFunctor {
53
  inline HOSTDEVICE T operator()(const T a, const T b) const { return b - a; }
54 55 56 57 58
};

// Multiply
template <typename T>
struct MultiplyFunctor {
59
  inline HOSTDEVICE T operator()(const T a, const T b) const { return a * b; }
60
};
61 62 63 64 65 66
template <>
struct MultiplyFunctor<bool> {
  inline HOSTDEVICE bool operator()(const bool a, const bool b) const {
    return a && b;
  }
};
67 68
template <typename T>
struct InverseMultiplyFunctor {
69
  inline HOSTDEVICE T operator()(const T a, const T b) const { return b * a; }
70
};
71 72 73 74 75 76
template <>
struct InverseMultiplyFunctor<bool> {
  inline HOSTDEVICE bool operator()(const bool a, const bool b) const {
    return b && a;
  }
};
77

78 79 80 81 82
template <typename T>
struct IsZeroFunctor {
  HOSTDEVICE bool operator()(T x) const { return x == static_cast<T>(0); }
};

83 84 85 86 87 88 89
// Divide
#define DIV_ERROR_INFO                                             \
  "InvalidArgumentError: Integer division by zero encountered in " \
  "(floor) divide. Please check the input value."

template <typename T, typename Enable = void>
struct DivideFunctor {
90
  inline HOSTDEVICE T operator()(const T a, const T b) const { return a / b; }
91 92 93 94 95 96
};

template <typename T>
struct DivideFunctor<
    T,
    typename std::enable_if<std::is_integral<T>::value>::type> {
97
  inline HOSTDEVICE T operator()(const T a, const T b) const {
98 99 100 101 102 103 104 105
    // For int32/int64, need to check whether the divison is zero.
    PADDLE_ENFORCE(b != 0, DIV_ERROR_INFO);
    return a / b;
  }
};

template <typename T, typename Enable = void>
struct InverseDivideFunctor {
106
  inline HOSTDEVICE T operator()(const T a, const T b) const { return b / a; }
107 108
};

109 110 111 112 113 114 115 116 117 118 119 120
template <typename T>
using ComplexType = phi::dtype::complex<T>;

template <typename InT, typename OutT>
struct DivGradXYFunctor {
  inline HOSTDEVICE phi::Array<OutT, 2> operator()(const InT a,
                                                   const InT b,
                                                   const InT c) {
    // dx = dout / y
    // dy = - dout * out / y
    phi::Array<OutT, 2> outs;
    outs[0] = a / c;
121
    outs[1] = -a * ((b / c) / c);
122 123 124 125 126 127 128 129 130 131 132 133
    return outs;
  }
};

template <typename InT, typename OutT>
struct DivGradXYFunctor<ComplexType<InT>, ComplexType<OutT>> {
  inline HOSTDEVICE phi::Array<ComplexType<OutT>, 2> operator()(
      const ComplexType<InT> a,
      const ComplexType<InT> b,
      const ComplexType<InT> c) {
    phi::Array<ComplexType<OutT>, 2> outs;
    ComplexType<InT> c_conj(c.real, -c.imag);
134
    ComplexType<InT> out_div_c_conj(((b / c) / c).real, -((b / c) / c).imag);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    outs[0] = a / c_conj;
    outs[1] = -a * out_div_c_conj;
    return outs;
  }
};

// Float div grad
template <typename T>
struct DivGradXFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const { return a / b; }
};

// ComplexType div grad
template <typename T>
struct DivGradXFunctor<ComplexType<T>> {
  inline HOSTDEVICE ComplexType<T> operator()(const ComplexType<T> a,
                                              const ComplexType<T> b) const {
    ComplexType<T> b_conj(b.real, -b.imag);
    return a / b_conj;
  }
};

// Float mul and div
template <typename T>
struct DivGradYFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b, const T c) const {
161
    return -a * ((b / c) / c);
162 163 164 165 166 167 168 169 170
  }
};

// ComplexType mul and div
template <typename T>
struct DivGradYFunctor<ComplexType<T>> {
  inline HOSTDEVICE ComplexType<T> operator()(const ComplexType<T> a,
                                              const ComplexType<T> b,
                                              const ComplexType<T> c) const {
171
    ComplexType<T> out_div_c_conj(((b / c) / c).real, -((b / c) / c).imag);
172 173 174
    return -a * out_div_c_conj;
  }
};
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
// Fmin
template <typename T>
struct FMinFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    return std::fmin(a, b);
  }
};

template <>
struct FMinFunctor<dtype::float16> {
  inline HOSTDEVICE dtype::float16 operator()(const dtype::float16 a,
                                              const dtype::float16 b) const {
    float float_a = static_cast<float>(a);
    float float_b = static_cast<float>(b);
    auto result = std::fmin(float_a, float_b);
    return static_cast<dtype::float16>(result);
  }
};

194 195 196 197 198 199 200 201 202 203 204
template <>
struct FMinFunctor<dtype::bfloat16> {
  inline HOSTDEVICE dtype::bfloat16 operator()(const dtype::bfloat16 a,
                                               const dtype::bfloat16 b) const {
    float float_a = static_cast<float>(a);
    float float_b = static_cast<float>(b);
    auto result = std::fmin(float_a, float_b);
    return static_cast<dtype::bfloat16>(result);
  }
};

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 235 236 237 238 239 240 241 242 243
template <>
struct FMinFunctor<int> {
  inline HOSTDEVICE int operator()(const int a, const int b) const {
    float float_a = static_cast<float>(a);
    float float_b = static_cast<float>(b);
    auto result = std::fmin(float_a, float_b);
    return std::lrint(result);
  }
};

template <>
struct FMinFunctor<int64_t> {
  inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const {
    double double_a = static_cast<double>(a);
    double double_b = static_cast<double>(b);
    auto result = std::fmin(double_a, double_b);
    return std::llrint(result);
  }
};

// Fmax
template <typename T>
struct FMaxFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    return std::fmax(a, b);
  }
};

template <>
struct FMaxFunctor<dtype::float16> {
  inline HOSTDEVICE dtype::float16 operator()(const dtype::float16 a,
                                              const dtype::float16 b) const {
    float float_a = static_cast<float>(a);
    float float_b = static_cast<float>(b);
    auto result = std::fmax(float_a, float_b);
    return static_cast<dtype::float16>(result);
  }
};

244 245 246 247 248 249 250 251 252 253 254
template <>
struct FMaxFunctor<dtype::bfloat16> {
  inline HOSTDEVICE dtype::bfloat16 operator()(const dtype::bfloat16 a,
                                               const dtype::bfloat16 b) const {
    float float_a = static_cast<float>(a);
    float float_b = static_cast<float>(b);
    auto result = std::fmax(float_a, float_b);
    return static_cast<dtype::bfloat16>(result);
  }
};

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
template <>
struct FMaxFunctor<int> {
  inline HOSTDEVICE int operator()(const int a, const int b) const {
    float float_a = static_cast<float>(a);
    float float_b = static_cast<float>(b);
    auto result = std::fmax(float_a, float_b);
    return std::lrint(result);
  }
};

template <>
struct FMaxFunctor<int64_t> {
  inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const {
    double double_a = static_cast<double>(a);
    double double_b = static_cast<double>(b);
    auto result = std::fmax(double_a, double_b);
    return std::llrint(result);
  }
};

template <typename T>
struct FMaxGradDx {
  HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
    return dout * static_cast<T>((x >= y) || isnan(y));
  }
};

template <>
struct FMaxGradDx<dtype::float16> {
  HOSTDEVICE dtype::float16 operator()(dtype::float16 x,
                                       dtype::float16 y,
286
                                       dtype::float16 out UNUSED,
287 288 289 290 291 292 293
                                       dtype::float16 dout) const {
    return dout * static_cast<dtype::float16>((x >= y) || dtype::isnan(y));
  }
};

template <>
struct FMaxGradDx<int> {
294
  HOSTDEVICE int operator()(int x, int y, int out UNUSED, int dout) const {
295 296 297 298 299 300 301 302
    return dout * static_cast<int>((x >= y));
  }
};

template <>
struct FMaxGradDx<int64_t> {
  HOSTDEVICE int64_t operator()(int64_t x,
                                int64_t y,
303
                                int64_t out UNUSED,
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
                                int64_t dout) const {
    return dout * static_cast<int64_t>((x >= y));
  }
};

template <typename T>
struct FMaxGradDy {
  HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
    return dout * static_cast<T>(!((x >= y) || isnan(y)));
  }
};

template <>
struct FMaxGradDy<dtype::float16> {
  HOSTDEVICE dtype::float16 operator()(dtype::float16 x,
                                       dtype::float16 y,
320
                                       dtype::float16 out UNUSED,
321 322 323 324 325 326 327 328 329
                                       dtype::float16 dout) const {
    return dout * static_cast<dtype::float16>(!((x >= y) || dtype::isnan(y)));
  }
};

template <>
struct FMaxGradDy<int64_t> {
  HOSTDEVICE int64_t operator()(int64_t x,
                                int64_t y,
330
                                int64_t out UNUSED,
331 332 333 334 335 336 337
                                int64_t dout) const {
    return dout * static_cast<int64_t>(!((x >= y)));
  }
};

template <>
struct FMaxGradDy<int> {
338
  HOSTDEVICE int operator()(int x, int y, int out UNUSED, int dout) const {
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    return dout * static_cast<int>(!((x >= y)));
  }
};

template <typename T>
struct FMinGradDx {
  HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
    return dout * static_cast<T>((x <= y) || isnan(y));
  }
};

template <>
struct FMinGradDx<dtype::float16> {
  HOSTDEVICE dtype::float16 operator()(dtype::float16 x,
                                       dtype::float16 y,
354
                                       dtype::float16 out UNUSED,
355 356 357 358 359 360 361
                                       dtype::float16 dout) const {
    return dout * static_cast<dtype::float16>((x <= y) || dtype::isnan(y));
  }
};

template <>
struct FMinGradDx<int> {
362
  HOSTDEVICE int operator()(int x, int y, int out UNUSED, int dout) const {
363 364 365 366 367 368 369 370
    return dout * static_cast<int>((x <= y));
  }
};

template <>
struct FMinGradDx<int64_t> {
  HOSTDEVICE int64_t operator()(int64_t x,
                                int64_t y,
371
                                int64_t out UNUSED,
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
                                int64_t dout) const {
    return dout * static_cast<int64_t>((x <= y));
  }
};

template <typename T>
struct FMinGradDy {
  HOSTDEVICE T operator()(T x, T y, T out, T dout) const {
    return dout * static_cast<T>(!((x <= y) || isnan(y)));
  }
};

template <>
struct FMinGradDy<dtype::float16> {
  HOSTDEVICE dtype::float16 operator()(dtype::float16 x,
                                       dtype::float16 y,
388
                                       dtype::float16 out UNUSED,
389 390 391 392 393 394 395
                                       dtype::float16 dout) const {
    return dout * static_cast<dtype::float16>(!((x <= y) || dtype::isnan(y)));
  }
};

template <>
struct FMinGradDy<int> {
396
  HOSTDEVICE int operator()(int x, int y, int out UNUSED, int dout) const {
397 398 399 400 401 402 403 404
    return dout * static_cast<int>(!((x <= y)));
  }
};

template <>
struct FMinGradDy<int64_t> {
  HOSTDEVICE int64_t operator()(int64_t x,
                                int64_t y,
405
                                int64_t out UNUSED,
406 407 408 409
                                int64_t dout) const {
    return dout * static_cast<int64_t>(!((x <= y)));
  }
};
410

Y
YuanRisheng 已提交
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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
template <typename T>
struct MultiplyGradFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const { return a * b; }
};
template <typename T>
struct MultiplyGradFunctor<ComplexType<T>> {
  inline HOSTDEVICE ComplexType<T> operator()(const ComplexType<T> a,
                                              const ComplexType<T> b) const {
    ComplexType<T> b_conj(b.real, -b.imag);
    return a * b_conj;
  }
};

template <typename InT, typename OutT>
struct MultiplyGradXYFunctor {
  inline HOSTDEVICE phi::Array<OutT, 2> operator()(const InT a,
                                                   const InT b,
                                                   const InT c) {
    phi::Array<OutT, 2> outs;
    // dx = dout * y
    outs[0] = a * b;
    // dy = dout * x
    outs[1] = a * c;
    return outs;
  }
};

template <typename InT, typename OutT>
struct MultiplyGradXYFunctor<ComplexType<InT>, ComplexType<OutT>> {
  inline HOSTDEVICE phi::Array<ComplexType<OutT>, 2> operator()(
      const ComplexType<InT> a,
      const ComplexType<InT> b,
      const ComplexType<InT> c) {
    phi::Array<ComplexType<OutT>, 2> outs;
    // dx = dout * y
    ComplexType<InT> b_conj(b.real, -b.imag);
    outs[0] = a * b_conj;
    // dy = dout * x
    ComplexType<InT> c_conj(c.real, -c.imag);
    outs[1] = a * c_conj;
    return outs;
  }
};

455 456 457 458 459 460 461 462 463 464 465 466 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 522 523 524 525 526
// Maximum
template <typename T>
struct MaximumFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    return a > b ? a : b;
  }
};

template <typename T>
struct MaxGradXFunctor {
  inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const {
    return dout * static_cast<T>(x > y);
  }
};

template <typename T>
struct MaxGradYFunctor {
  inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const {
    return dout * static_cast<T>(x <= y);
  }
};

template <typename InT, typename OutT>
struct MaxGradXYFunctor {
  inline HOSTDEVICE phi::Array<OutT, 2> operator()(const InT x,
                                                   const InT y,
                                                   const InT dout) {
    phi::Array<OutT, 2> outs;
    // dx = dout * (x > y)
    outs[0] = static_cast<OutT>(dout * static_cast<InT>(x > y));
    // dy = dout * (x <= y)
    outs[1] = static_cast<OutT>(dout * static_cast<InT>(x <= y));
    return outs;
  }
};

// Minimum
template <typename T>
struct MinimumFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    return a < b ? a : b;
  }
};
template <typename T>
struct MinGradXFunctor {
  inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const {
    return dout * static_cast<T>(x < y);
  }
};
template <typename T>
struct MinGradYFunctor {
  inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const {
    return dout * static_cast<T>(x >= y);
  }
};

template <typename InT, typename OutT>
struct MinGradXYFunctor {
  inline HOSTDEVICE phi::Array<OutT, 2> operator()(const InT x,
                                                   const InT y,
                                                   const InT dout) {
    phi::Array<OutT, 2> outs;
    // dx = dout * (x < y)
    outs[0] = static_cast<OutT>(dout * static_cast<InT>(x < y));
    // dy = dout * (x >= y)
    outs[1] = static_cast<OutT>(dout * static_cast<InT>(x >= y));
    return outs;
  }
};

// Modulo
template <typename T, typename Enable = void>
C
Chen Weihang 已提交
527
struct RemainderFunctor {
528
  inline HOSTDEVICE T operator()(const T a, const T b) const {
529
    PADDLE_ENFORCE(b != 0, DIV_ERROR_INFO);
530 531 532 533 534 535 536 537 538 539
    T res = a % b;

    // Accoding to #PR26732: in dividen % divsor
    // remainder shall have the same sign as divsor.
    if ((res != 0) && ((b ^ res) < 0)) res += b;
    return res;
  }
};

template <typename T>
C
Chen Weihang 已提交
540
struct RemainderFunctor<
541 542 543 544 545 546 547 548 549 550 551 552
    T,
    typename std::enable_if_t<std::is_floating_point<T>::value>> {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    T res = fmod(a, b);

    // Accoding to #PR26732: in dividen % divsor
    // remainder shall have the same sign as divsor.
    if ((res != 0) && ((res < 0) != (b < 0))) res += b;
    return res;
  }
};

553 554 555 556 557 558 559 560 561 562 563 564 565
template <>
struct RemainderFunctor<dtype::float16> {
  inline HOSTDEVICE dtype::float16 operator()(const dtype::float16 a,
                                              const dtype::float16 b) const {
    float b_float = static_cast<float>(b);
    float res = fmod(static_cast<float>(a), b_float);
    // Accoding to #PR26732: in dividen % divsor
    // remainder shall have the same sign as divsor.
    if ((res != 0.0f) && ((res < 0.0f) != (b_float < 0.0f))) res += b_float;
    return static_cast<dtype::float16>(res);
  }
};

566 567 568 569 570 571 572 573 574 575 576 577 578 579
template <>
struct RemainderFunctor<dtype::bfloat16> {
  inline HOSTDEVICE dtype::bfloat16 operator()(const dtype::bfloat16 a,
                                               const dtype::bfloat16 b) const {
    float b_float = static_cast<float>(b);
    float res = fmod(static_cast<float>(a), b_float);

    // Accoding to #PR26732: in dividen % divsor
    // remainder shall have the same sign as divsor.
    if ((res != 0.0f) && ((res < 0.0f) != (b_float < 0.0f))) res += b_float;
    return static_cast<dtype::bfloat16>(res);
  }
};

580
template <typename T, typename Enable = void>
C
Chen Weihang 已提交
581
struct InverseRemainderFunctor {
582 583 584 585 586 587 588 589
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    T res = b % a;
    if ((res != 0) && ((res < 0) != (a < 0))) res += a;
    return res;
  }
};

template <typename T>
C
Chen Weihang 已提交
590
struct InverseRemainderFunctor<
591 592 593 594 595 596 597 598
    T,
    typename std::enable_if_t<std::is_floating_point<T>::value>> {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    T res = fmod(b, a);
    if ((res != 0) && ((a < 0) != (res < 0))) res += a;
    return res;
  }
};
599

600 601 602
template <typename T>
struct ElementwiseHeavisideFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
603
    return a == static_cast<T>(0) ? b : static_cast<T>(a > static_cast<T>(0));
604 605 606
  }
};

607 608 609
template <typename T>
struct FloorDivideFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
610
#ifndef PADDLE_WITH_XPU_KP
611
    PADDLE_ENFORCE(b != 0, DIV_ERROR_INFO);
612
#endif
613
    return static_cast<T>(a / b);
614 615 616 617 618 619
  }
};

template <typename T>
struct InverseFloorDivideFunctor {
  inline HOSTDEVICE T operator()(const T a, const T b) const {
620
#ifndef PADDLE_WITH_XPU_KP
621
    PADDLE_ENFORCE(a != 0, DIV_ERROR_INFO);
622
#endif
623
    return static_cast<T>(b / a);
624 625 626 627
  }
};

#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
template <typename T, typename MPType>
inline HOSTDEVICE typename std::enable_if<std::is_integral<T>::value, T>::type
compute_pow(const T a, const T b) {
  // TODO(wujionghao): A potential speed improvement is supporting different
  // types in C++.
  // On CUDAPlace, std::pow(3, 1) calls pow(float, float), and
  // it will return a float number like 2.99... , which floor to 2
  // when cast to int by default and it is wrong.
  // Use llrint to cast it to the nearest integer, which is 3.
  return std::llrint(std::pow(static_cast<double>(a), static_cast<double>(b)));
}
template <typename T, typename MPType>
inline HOSTDEVICE typename std::enable_if<!std::is_integral<T>::value, T>::type
compute_pow(const T a, const T b) {
  MPType a_val = static_cast<MPType>(a);
  MPType b_val = static_cast<MPType>(b);
#ifdef PADDLE_WITH_XPU_KP
  return static_cast<T>(pow(a_val, b_val));
646
#endif
647 648 649 650 651 652 653
  return static_cast<T>(std::pow(a_val, b_val));
}
#else
template <typename T, typename MPType>
inline HOSTDEVICE T compute_pow(const T a, const T b) {
  MPType a_val = static_cast<MPType>(a);
  MPType b_val = static_cast<MPType>(b);
654
#ifdef PADDLE_WITH_XPU_KP
655 656 657 658
  return static_cast<T>(pow(a_val, b_val));
#endif
  return static_cast<T>(std::pow(a_val, b_val));
}
659
#endif
660

661
template <typename T>
662 663
struct ElementwisePowFunctor {
  using MPType = typename phi::dtype::MPTypeTrait<T>::Type;
664
  inline HOSTDEVICE T operator()(const T a, const T b) const {
665
    return compute_pow<T, MPType>(a, b);
666 667 668
  }
};

669 670 671 672 673
template <typename T>
struct ElementwiseInversePowFunctor {
  using MPType = typename phi::dtype::MPTypeTrait<T>::Type;
  inline HOSTDEVICE T operator()(const T a, const T b) const {
    return compute_pow<T, MPType>(b, a);
674 675 676
  }
};

677
}  // namespace funcs
678
}  // namespace phi