kernel_registry.h 69.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 License.

#pragma once

#include <cstring>
18
#include <string>
19 20 21 22 23
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <vector>

24
#include "paddle/phi/core/custom_kernel.h"
25
#include "paddle/phi/core/enforce.h"
26 27 28 29
#include "paddle/phi/core/kernel_factory.h"
#include "paddle/phi/core/kernel_utils.h"
#include "paddle/phi/core/macros.h"
#include "paddle/phi/core/type_defs.h"
30

31
namespace phi {
32

33 34 35
#define BACKEND(arg__) phi::Backend::arg__
#define DATALAYOUT(arg__) phi::DataLayout::arg__
#define DATATYPE(arg__) phi::DataType::arg__
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

template <typename Func>
struct KernelArgsParseFunctor;

template <typename Return_, typename... Args_>
struct KernelArgsParseFunctor<Return_ (*)(Args_...)> {
  using Args = std::tuple<Args_...>;
  enum : std::size_t { Arity = sizeof...(Args_) };
  using Indices = std::make_index_sequence<Arity>;
  template <std::size_t Index>
  using Arg = typename std::tuple_element<Index, Args>::type;

  static void Parse(const KernelKey& default_key, KernelArgsDef* args_def) {
    // TODO(chenweihang): The fluid Tensor's default layout is NCHW,
    // it is not same as kernel's layout, we should fix this error on
    // fluid Tensor
52 53
    auto default_tensor_layout = phi::DataLayout::NCHW;
    if (default_key.layout() != phi::DataLayout::ANY) {
54 55 56 57 58
      default_tensor_layout = default_key.layout();
    }
    auto args_type = ParseArgType(Indices{});
    for (auto arg_type : args_type) {
      if (arg_type == std::type_index(typeid(const CPUContext&))
59 60 61
#if defined(PADDLE_WITH_MKLDNN)
          || arg_type == std::type_index(typeid(const OneDNNContext&))
#endif
62
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
63
          || arg_type == std::type_index(typeid(const GPUContext&))) {
64
#elif defined(PADDLE_WITH_XPU)
65
          || arg_type == std::type_index(typeid(const XPUContext&))) {
66
#elif defined(PADDLE_WITH_CUSTOM_DEVICE)
67
          || arg_type == std::type_index(typeid(const CustomContext&))) {
68
#else
69

70
      ) {
71 72 73
#endif
        // do nothing, skip context arg now
      } else if (arg_type == std::type_index(typeid(const DenseTensor&))) {
H
hong 已提交
74 75 76 77
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
78
      } else if (arg_type == std::type_index(typeid(
79
                                 const paddle::optional<DenseTensor>&))) {
H
hong 已提交
80 81 82 83
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
84 85 86
      } else if (arg_type ==
                 std::type_index(typeid(const paddle::optional<
                                        std::vector<const DenseTensor*>>&))) {
87 88 89 90
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
H
hong 已提交
91
      } else if (arg_type == std::type_index(typeid(
92
                                 const paddle::optional<SelectedRows>&))) {
H
hong 已提交
93 94 95 96
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
97 98
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const DenseTensor*>&))) {
H
hong 已提交
99 100 101 102
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
103
      } else if (arg_type == std::type_index(typeid(const SelectedRows&))) {
H
hong 已提交
104 105 106 107
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
108 109 110 111 112
      } else if (arg_type == std::type_index(typeid(const StringTensor&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
Z
zhangkaihuo 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      } else if (arg_type == std::type_index(typeid(const SparseCooTensor&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const SparseCooTensor&>))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(const SparseCsrTensor&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const SparseCsrTensor&>))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
135 136 137 138 139
      } else if (arg_type == std::type_index(typeid(const TensorArray&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
140
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
H
hong 已提交
141 142 143 144
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
145 146
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
H
hong 已提交
147 148 149 150
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
151
      } else if (arg_type == std::type_index(typeid(SelectedRows*))) {
H
hong 已提交
152 153 154 155
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
156 157 158 159 160
      } else if (arg_type == std::type_index(typeid(TensorArray*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
Z
zhangkaihuo 已提交
161 162 163 164 165 166 167 168 169 170
      } else if (arg_type == std::type_index(typeid(SparseCooTensor*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
      } else if (arg_type == std::type_index(typeid(SparseCsrTensor*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
171 172 173 174 175
      } else if (arg_type == std::type_index(typeid(StringTensor*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
      } else if (arg_type == std::type_index(typeid(bool))) {
        args_def->AppendAttribute(AttributeType::BOOL);
      } else if (arg_type == std::type_index(typeid(int))) {
        args_def->AppendAttribute(AttributeType::INT32);
      } else if (arg_type == std::type_index(typeid(int64_t))) {
        args_def->AppendAttribute(AttributeType::INT64);
      } else if (arg_type == std::type_index(typeid(float))) {
        args_def->AppendAttribute(AttributeType::FLOAT32);
      } else if (arg_type == std::type_index(typeid(double))) {
        args_def->AppendAttribute(AttributeType::FLOAT64);
      } else if (arg_type == std::type_index(typeid(std::string))) {
        args_def->AppendAttribute(AttributeType::STRING);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<bool>&))) {
        args_def->AppendAttribute(AttributeType::BOOLS);
      } else if (arg_type == std::type_index(typeid(const std::vector<int>&))) {
        args_def->AppendAttribute(AttributeType::INT32S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<int64_t>&))) {
        args_def->AppendAttribute(AttributeType::INT64S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<float>&))) {
        args_def->AppendAttribute(AttributeType::FLOAT32S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<double>&))) {
        args_def->AppendAttribute(AttributeType::FLOAT64S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<std::string>&))) {
        args_def->AppendAttribute(AttributeType::STRINGS);
      } else if (arg_type == std::type_index(typeid(const Scalar&))) {
        args_def->AppendAttribute(AttributeType::SCALAR);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<Scalar>&))) {
        args_def->AppendAttribute(AttributeType::SCALARS);
      } else if (arg_type == std::type_index(typeid(const IntArray&))) {
        args_def->AppendAttribute(AttributeType::INT_ARRAY);
      } else if (arg_type == std::type_index(typeid(DataType))) {
        args_def->AppendAttribute(AttributeType::DATA_TYPE);
      } else if (arg_type == std::type_index(typeid(DataLayout))) {
        args_def->AppendAttribute(AttributeType::DATA_LAYOUT);
      } else if (arg_type == std::type_index(typeid(Place))) {
        args_def->AppendAttribute(AttributeType::PLACE);
218 219
      } else if (arg_type == std::type_index(typeid(RuntimeAttrs))) {
        // do nothing
220
      } else {
221 222
        PADDLE_THROW(phi::errors::Unavailable(
            "Unsupported kernel argument type `%s`.", arg_type.name()));
223 224 225 226 227 228 229 230 231 232 233 234
      }
    }
  }

 private:
  template <std::size_t... INDEX>
  static std::vector<std::type_index> ParseArgType(
      std::index_sequence<INDEX...>) {
    return {std::type_index(typeid(Arg<INDEX>))...};
  }
};

235
// NOTE: used for making a difference between inner or outer registration.
236
enum class RegType : uint8_t {
237 238
  INNER = 0,
  OUTER,
239 240
};

241 242
// TODO(chenweihang): Polish the kernel selection logic, support the selection
// of ALL_DTYPE kernel, and simplify the constructor
243 244
struct KernelRegistrar {
 public:
245 246 247
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
248 249 250 251
                  DataLayout layout,
                  DataType dtype,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
252 253
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
254 255 256
    ConstructKernel(reg_type,
                    kernel_name_cstr,
                    backend_cstr,
257 258 259 260
                    layout,
                    dtype,
                    args_parse_fn,
                    args_def_fn,
261 262
                    kernel_fn,
                    variadic_kernel_fn);
263 264
  }

265 266 267
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
268 269 270
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
271 272
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
273 274 275
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
276 277 278 279 280 281 282
      // NOTE(zhiqiu): why skip these types, because fluid kernel has no kernel
      // of these type.
      if (dtype == static_cast<size_t>(DataType::UINT32) ||
          dtype == static_cast<size_t>(DataType::UINT64) ||
          dtype == static_cast<size_t>(DataType::UINT16)) {
        continue;
      }
J
Jack Zhou 已提交
283 284 285 286 287 288 289 290
      // NOTE(zhoushunjie): Only the strings kernels can support pstring dtype
      constexpr char strings_kernels_prefix[] = "strings_";
      if (dtype == static_cast<size_t>(DataType::PSTRING) &&
          strncmp(kernel_name_cstr,
                  strings_kernels_prefix,
                  strlen(strings_kernels_prefix))) {
        continue;
      }
291 292 293
      ConstructKernel(reg_type,
                      kernel_name_cstr,
                      backend_cstr,
294 295 296 297
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
298 299
                      kernel_fn,
                      variadic_kernel_fn);
300 301 302 303
    }
  }

 private:
304 305 306
  void ConstructKernel(RegType reg_type,
                       const char* kernel_name_cstr,
                       const char* backend_cstr,
307 308 309 310
                       DataLayout layout,
                       DataType dtype,
                       KernelArgsParseFn args_parse_fn,
                       KernelArgsDefFn args_def_fn,
311 312
                       KernelFn kernel_fn,
                       void* variadic_kernel_fn) {
Y
YuanRisheng 已提交
313
    std::string kernel_name(kernel_name_cstr);
314 315
    KernelKey kernel_key(
        paddle::experimental::StringToBackend(backend_cstr), layout, dtype);
316
    Kernel kernel(kernel_fn, variadic_kernel_fn);
317
    args_parse_fn(kernel_key, kernel.mutable_args_def());
318
    args_def_fn(kernel_key, &kernel);
319
    if (reg_type == RegType::INNER) {
320 321
      KernelFactory::Instance().kernels()[kernel_name][kernel_key] = kernel;
    } else {
322 323
      CustomKernelMap::Instance().RegisterCustomKernel(
          kernel_name, kernel_key, kernel);
324
    }
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  }
};

/**
 * Reference:
 *
 *   https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
 *   https://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion?rq=1
 *   https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly
 *
 * Very carefully tiptoeing around an MSVC bug where it improperly expands
 * __VA_ARGS__ as a single token in argument lists.  See these URLs for details:
 *
 *   http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement
 *   http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644
 */
341 342 343
#define PD_NARGS(...) _PD_NARGS((__VA_ARGS__, _PD_RESQ_N()))
#define _PD_NARGS(...) _PD_ARG_N(__VA_ARGS__)
#define _PD_ARG_N_EXPAND(                                                     \
344 345
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
346 347
#define _PD_ARG_N(args) _PD_ARG_N_EXPAND args
#define _PD_RESQ_N() 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
348

349
/** PD_REGISTER_KERNEL
350 351 352 353 354
 *
 * The most frequently used kernel registration macro, used for kernel
 * registration with only data type as template parameter, and the function
 * pointer of the corresponding data type is automatically instantiated
 * during registration.
355
 *
356
 * Note: `2TA` means `2 template argument`
357
 */
358
#define PD_REGISTER_KERNEL(kernel_name, backend, layout, meta_kernel_fn, ...) \
359
  _PD_REGISTER_KERNEL(::phi::RegType::INNER,                                  \
360 361 362 363 364 365 366
                      kernel_name,                                            \
                      backend,                                                \
                      ::phi::backend##Context,                                \
                      layout,                                                 \
                      meta_kernel_fn,                                         \
                      __VA_ARGS__)

367
#define _PD_REGISTER_KERNEL(                                               \
368
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)  \
369
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
370 371
      PD_REGISTER_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_KERNEL must be called in global namespace.");           \
372
  PD_EXPAND(_PD_REGISTER_2TA_KERNEL(reg_type,                              \
373 374 375 376 377 378
                                    kernel_name,                           \
                                    backend,                               \
                                    context,                               \
                                    layout,                                \
                                    meta_kernel_fn,                        \
                                    __VA_ARGS__))
379

380
#ifndef _WIN32
381
#define _PD_REGISTER_2TA_KERNEL(                                            \
382
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
383 384
  PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, __VA_ARGS__);   \
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
385
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
386
  PD_KERNEL_REGISTRAR_INIT(                                                 \
387
      reg_type,                                                             \
388 389
      kernel_name,                                                          \
      backend,                                                              \
390
      context,                                                              \
391
      layout,                                                               \
392
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
393 394
      meta_kernel_fn,                                                       \
      __VA_ARGS__);                                                         \
395
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
396
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
397 398 399 400 401 402 403 404 405 406 407
#else
/**
 * `template decltype(fn) fn` can work on gcc and clang,
 * but msvc will failed, error like:
 *
 *   error C2206: typedef cannot be used for function definition
 *
 * reference:
 *
 *   https://stackoverflow.com/questions/63989585/explicit-instantiation-of-function-using-decltype-work-on-g-but-not-on-visua
 *
408
 * And msvc can work without template instantiation
409
 */
410
#define _PD_REGISTER_2TA_KERNEL(                                            \
411
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
412
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
413
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
414
  PD_EXPAND(PD_KERNEL_REGISTRAR_INIT(                                       \
415
      reg_type,                                                             \
416 417
      kernel_name,                                                          \
      backend,                                                              \
418
      context,                                                              \
419
      layout,                                                               \
420
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
421
      meta_kernel_fn,                                                       \
422
      __VA_ARGS__));                                                        \
423
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
424
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
425 426
#endif

427 428 429
#define PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, ...) \
  _PD_KERNEL_INSTANTIATION(                                            \
      PD_NARGS(__VA_ARGS__), meta_kernel_fn, backend, context, __VA_ARGS__)
430

431 432
#define _PD_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, context, ...) \
  PD_CONCATENATE(_PD_KERNEL_INSTANTIATION_, N)                             \
433 434
  (meta_kernel_fn, backend, context, __VA_ARGS__)

435 436 437 438 439 440 441 442 443
#define _PD_KERNEL_INSTANTIATION_1(                     \
    meta_kernel_fn, backend, context, cpp_dtype)        \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>
#define _PD_KERNEL_INSTANTIATION_2(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_1(                 \
444
      meta_kernel_fn, backend, context, __VA_ARGS__))
445 446 447 448 449
#define _PD_KERNEL_INSTANTIATION_3(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_2(                 \
450
      meta_kernel_fn, backend, context, __VA_ARGS__))
451 452 453 454 455
#define _PD_KERNEL_INSTANTIATION_4(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_3(                 \
456
      meta_kernel_fn, backend, context, __VA_ARGS__))
457 458 459 460 461
#define _PD_KERNEL_INSTANTIATION_5(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_4(                 \
462
      meta_kernel_fn, backend, context, __VA_ARGS__))
463 464 465 466 467
#define _PD_KERNEL_INSTANTIATION_6(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_5(                 \
468
      meta_kernel_fn, backend, context, __VA_ARGS__))
469 470 471 472 473
#define _PD_KERNEL_INSTANTIATION_7(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_6(                 \
474
      meta_kernel_fn, backend, context, __VA_ARGS__))
475 476 477 478 479
#define _PD_KERNEL_INSTANTIATION_8(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_7(                 \
480
      meta_kernel_fn, backend, context, __VA_ARGS__))
481 482 483 484 485
#define _PD_KERNEL_INSTANTIATION_9(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_8(                 \
486
      meta_kernel_fn, backend, context, __VA_ARGS__))
487 488 489 490 491
#define _PD_KERNEL_INSTANTIATION_10(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_9(                 \
492
      meta_kernel_fn, backend, context, __VA_ARGS__))
493 494 495 496 497
#define _PD_KERNEL_INSTANTIATION_11(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_10(                \
498
      meta_kernel_fn, backend, context, __VA_ARGS__))
499 500 501 502 503
#define _PD_KERNEL_INSTANTIATION_12(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_11(                \
504
      meta_kernel_fn, backend, context, __VA_ARGS__))
505 506 507 508 509
#define _PD_KERNEL_INSTANTIATION_13(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_12(                \
510
      meta_kernel_fn, backend, context, __VA_ARGS__))
511 512 513 514 515
#define _PD_KERNEL_INSTANTIATION_14(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_13(                \
516
      meta_kernel_fn, backend, context, __VA_ARGS__))
517 518 519 520 521
#define _PD_KERNEL_INSTANTIATION_15(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_14(                \
522 523
      meta_kernel_fn, backend, context, __VA_ARGS__))

524
#define PD_KERNEL_REGISTRAR_INIT(reg_type,                   \
525 526 527 528 529 530 531
                                 kernel_name,                \
                                 backend,                    \
                                 context,                    \
                                 layout,                     \
                                 args_def_fn,                \
                                 meta_kernel_fn,             \
                                 ...)                        \
532
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT(PD_NARGS(__VA_ARGS__), \
533 534 535 536 537 538 539
                                      reg_type,              \
                                      kernel_name,           \
                                      backend,               \
                                      context,               \
                                      layout,                \
                                      args_def_fn,           \
                                      meta_kernel_fn,        \
540
                                      __VA_ARGS__))
541 542 543 544 545

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
546
#define _PD_KERNEL_REGISTRAR_INIT(N,                       \
547
                                  reg_type,                \
548 549
                                  kernel_name,             \
                                  backend,                 \
550
                                  context,                 \
551 552 553 554
                                  layout,                  \
                                  args_def_fn,             \
                                  meta_kernel_fn,          \
                                  ...)                     \
555
  PD_EXPAND(PD_CONCATENATE(_PD_KERNEL_REGISTRAR_INIT_, N) ( \
556
    reg_type,                                              \
557 558
    kernel_name,                                           \
    backend,                                               \
559
    context,                                               \
560
    layout,                                                \
561
    PD_ID,                                                 \
562 563 564
    args_def_fn,                                           \
    meta_kernel_fn,                                        \
    __VA_ARGS__))
565 566 567

// clang-format on

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
#define _PD_KERNEL_REGISTRAR_INIT_1(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype)                                 \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
589
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() { return 0; }
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
#define _PD_KERNEL_REGISTRAR_INIT_2(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_1(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
620
                                        __VA_ARGS__))
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
#define _PD_KERNEL_REGISTRAR_INIT_3(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_2(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
651
                                        __VA_ARGS__))
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
#define _PD_KERNEL_REGISTRAR_INIT_4(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_3(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
682
                                        __VA_ARGS__))
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
#define _PD_KERNEL_REGISTRAR_INIT_5(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_4(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
713
                                        __VA_ARGS__))
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
#define _PD_KERNEL_REGISTRAR_INIT_6(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_5(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
744
                                        __VA_ARGS__))
745 746 747 748 749 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
#define _PD_KERNEL_REGISTRAR_INIT_7(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_6(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
775
                                        __VA_ARGS__))
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
#define _PD_KERNEL_REGISTRAR_INIT_8(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_7(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
806
                                        __VA_ARGS__))
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
#define _PD_KERNEL_REGISTRAR_INIT_9(reg_type,                                  \
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
                                    cpp_dtype,                                 \
                                    ...)                                       \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_8(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
837
                                        __VA_ARGS__))
838 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
#define _PD_KERNEL_REGISTRAR_INIT_10(reg_type,                                 \
                                     kernel_name,                              \
                                     backend,                                  \
                                     context,                                  \
                                     layout,                                   \
                                     registrar_id,                             \
                                     args_def_fn,                              \
                                     meta_kernel_fn,                           \
                                     cpp_dtype,                                \
                                     ...)                                      \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_9(reg_type,                              \
                                        kernel_name,                           \
                                        backend,                               \
                                        context,                               \
                                        layout,                                \
                                        PD_ID,                                 \
                                        args_def_fn,                           \
                                        meta_kernel_fn,                        \
868
                                        __VA_ARGS__))
869 870 871 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
#define _PD_KERNEL_REGISTRAR_INIT_11(reg_type,                                 \
                                     kernel_name,                              \
                                     backend,                                  \
                                     context,                                  \
                                     layout,                                   \
                                     registrar_id,                             \
                                     args_def_fn,                              \
                                     meta_kernel_fn,                           \
                                     cpp_dtype,                                \
                                     ...)                                      \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_10(reg_type,                             \
                                         kernel_name,                          \
                                         backend,                              \
                                         context,                              \
                                         layout,                               \
                                         PD_ID,                                \
                                         args_def_fn,                          \
                                         meta_kernel_fn,                       \
899
                                         __VA_ARGS__))
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
#define _PD_KERNEL_REGISTRAR_INIT_12(reg_type,                                 \
                                     kernel_name,                              \
                                     backend,                                  \
                                     context,                                  \
                                     layout,                                   \
                                     registrar_id,                             \
                                     args_def_fn,                              \
                                     meta_kernel_fn,                           \
                                     cpp_dtype,                                \
                                     ...)                                      \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_11(reg_type,                             \
                                         kernel_name,                          \
                                         backend,                              \
                                         context,                              \
                                         layout,                               \
                                         PD_ID,                                \
                                         args_def_fn,                          \
                                         meta_kernel_fn,                       \
930
                                         __VA_ARGS__))
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
#define _PD_KERNEL_REGISTRAR_INIT_13(reg_type,                                 \
                                     kernel_name,                              \
                                     backend,                                  \
                                     context,                                  \
                                     layout,                                   \
                                     registrar_id,                             \
                                     args_def_fn,                              \
                                     meta_kernel_fn,                           \
                                     cpp_dtype,                                \
                                     ...)                                      \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_12(reg_type,                             \
                                         kernel_name,                          \
                                         backend,                              \
                                         context,                              \
                                         layout,                               \
                                         PD_ID,                                \
                                         args_def_fn,                          \
                                         meta_kernel_fn,                       \
961
                                         __VA_ARGS__))
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
#define _PD_KERNEL_REGISTRAR_INIT_14(reg_type,                                 \
                                     kernel_name,                              \
                                     backend,                                  \
                                     context,                                  \
                                     layout,                                   \
                                     registrar_id,                             \
                                     args_def_fn,                              \
                                     meta_kernel_fn,                           \
                                     cpp_dtype,                                \
                                     ...)                                      \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_13(reg_type,                             \
                                         kernel_name,                          \
                                         backend,                              \
                                         context,                              \
                                         layout,                               \
                                         PD_ID,                                \
                                         args_def_fn,                          \
                                         meta_kernel_fn,                       \
992
                                         __VA_ARGS__))
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
#define _PD_KERNEL_REGISTRAR_INIT_15(reg_type,                                 \
                                     kernel_name,                              \
                                     backend,                                  \
                                     context,                                  \
                                     layout,                                   \
                                     registrar_id,                             \
                                     args_def_fn,                              \
                                     meta_kernel_fn,                           \
                                     cpp_dtype,                                \
                                     ...)                                      \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
      DATALAYOUT(layout),                                                      \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),            \
      ::phi::KernelArgsParseFunctor<                                           \
          decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse,               \
      args_def_fn,                                                             \
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                          \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));                \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_14(reg_type,                             \
                                         kernel_name,                          \
                                         backend,                              \
                                         context,                              \
                                         layout,                               \
                                         PD_ID,                                \
                                         args_def_fn,                          \
                                         meta_kernel_fn,                       \
1023
                                         __VA_ARGS__))
1024
/** PD_REGISTER_GENERAL_KERNEL
1025 1026 1027 1028 1029
 *
 * Basic Kernel register marco, used to register a instantiated kernel function
 * with one template argument.
 */

1030
#define PD_REGISTER_GENERAL_KERNEL(                 \
1031
    kernel_name, backend, layout, kernel_fn, dtype) \
1032
  _PD_REGISTER_GENERAL_KERNEL(                      \
1033
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn, dtype)
1034

1035
#define _PD_REGISTER_GENERAL_KERNEL(                                         \
1036
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)                \
1037
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
1038 1039 1040
      PD_REGISTER_no_t_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_NO_TEMPLATE_KERNEL must be called in global namespace."); \
  __PD_REGISTER_GENERAL_KERNEL(                                              \
1041
      reg_type, kernel_name, backend, layout, kernel_fn, dtype)
1042 1043

#ifndef _WIN32
1044
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
1045
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
1046
  template decltype(kernel_fn) kernel_fn;                                   \
1047
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
1048 1049
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
1050
      __reg_phi_kernel_##kernel_name##_##backend##_##layout(                \
1051
          reg_type,                                                         \
1052
          #kernel_name,                                                     \
1053
          #backend,                                                         \
1054
          DATALAYOUT(layout),                                               \
1055
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1056 1057 1058
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1059 1060 1061
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1062
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1063
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1064
#else
1065
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
1066
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
1067
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
1068 1069
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
1070
      __reg_phi_kernel_##kernel_name##_##backend##_##layout(                \
1071
          reg_type,                                                         \
1072
          #kernel_name,                                                     \
1073
          #backend,                                                         \
1074
          DATALAYOUT(layout),                                               \
1075
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1076 1077 1078
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1079 1080 1081
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1082
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1083
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1084 1085
#endif

1086
/** PD_DECLARE_KERNEL
1087 1088 1089 1090
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
1091
#define PD_DECLARE_KERNEL(kernel_name, backend, layout)                   \
1092
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                      \
1093 1094
      PD_DECLARE_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_DECLARE_KERNEL must be called in global namespace.");           \
1095 1096 1097 1098
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
1099

1100
/** PD_REGISTER_BUILTIN_KERNEL
1101 1102 1103 1104
 *
 * Used to register kernels for built-in backends.
 * Support CPU GPU XPU.
 */
1105 1106
#define PD_REGISTER_BUILTIN_KERNEL(                    \
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1107
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1108 1109 1110 1111 1112
                      kernel_name,                     \
                      backend,                         \
                      ::phi::backend##Context,         \
                      layout,                          \
                      meta_kernel_fn,                  \
1113 1114
                      __VA_ARGS__)

1115
/** PD_REGISTER_PLUGIN_KERNEL
1116 1117 1118 1119
 *
 * Used to register kernels for plug-in backends.
 * Support user-defined backend such as 'Ascend910'.
 */
1120
#define PD_REGISTER_PLUGIN_KERNEL(                     \
1121
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1122
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1123 1124 1125 1126 1127 1128 1129
                      kernel_name,                     \
                      backend,                         \
                      ::phi::CustomContext,            \
                      layout,                          \
                      meta_kernel_fn,                  \
                      __VA_ARGS__)

1130
}  // namespace phi