kernel_registry.h 67.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 26 27 28
#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"
29

30
#include "paddle/phi/core/enforce.h"
31

32
namespace phi {
33

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

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
53 54
    auto default_tensor_layout = phi::DataLayout::NCHW;
    if (default_key.layout() != phi::DataLayout::ANY) {
55 56 57 58 59 60 61
      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&))
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
          ||
62
          arg_type == std::type_index(typeid(const GPUContext&))) {
63 64 65
#elif defined(PADDLE_WITH_XPU)
          ||
          arg_type == std::type_index(typeid(const XPUContext&))) {
66 67 68
#elif defined(PADDLE_WITH_CUSTOM_DEVICE)
          ||
          arg_type == std::type_index(typeid(const CustomContext&))) {
69 70 71 72 73
#else
              ) {
#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 79
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const DenseTensor&>))) {
H
hong 已提交
80 81 82 83
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
84 85 86 87 88 89 90
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<
                                     const std::vector<const DenseTensor*>>))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
H
hong 已提交
91 92 93 94 95 96
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const SelectedRows&>))) {
        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
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
H
hong 已提交
136 137 138 139
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
140 141
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
H
hong 已提交
142 143 144 145
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
146
      } else if (arg_type == std::type_index(typeid(SelectedRows*))) {
H
hong 已提交
147 148 149 150
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
Z
zhangkaihuo 已提交
151 152 153 154 155 156 157 158 159 160
      } 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);
161 162 163 164 165
      } else if (arg_type == std::type_index(typeid(StringTensor*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      } else {
        // Attribute deal with
        // TODO(chenweihang): now here allow any types of attribute, maybe
        // should add limits here
        args_def->AppendAttribute(arg_type);
      }
    }
  }

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

183
// NOTE: used for making a difference between inner or outer registration.
184
enum class RegType : uint8_t {
185 186
  INNER = 0,
  OUTER,
187 188
};

189 190
// TODO(chenweihang): Polish the kernel selection logic, support the selection
// of ALL_DTYPE kernel, and simplify the constructor
191 192
struct KernelRegistrar {
 public:
193 194 195
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
196 197 198 199
                  DataLayout layout,
                  DataType dtype,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
200 201
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
202 203 204
    ConstructKernel(reg_type,
                    kernel_name_cstr,
                    backend_cstr,
205 206 207 208
                    layout,
                    dtype,
                    args_parse_fn,
                    args_def_fn,
209 210
                    kernel_fn,
                    variadic_kernel_fn);
211 212
  }

213 214 215
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
216 217 218
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
219 220
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
221 222 223
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
224 225 226 227 228 229 230
      // 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 已提交
231 232 233 234 235 236 237 238
      // 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;
      }
239 240 241
      ConstructKernel(reg_type,
                      kernel_name_cstr,
                      backend_cstr,
242 243 244 245
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
246 247
                      kernel_fn,
                      variadic_kernel_fn);
248 249 250 251
    }
  }

 private:
252 253 254
  void ConstructKernel(RegType reg_type,
                       const char* kernel_name_cstr,
                       const char* backend_cstr,
255 256 257 258
                       DataLayout layout,
                       DataType dtype,
                       KernelArgsParseFn args_parse_fn,
                       KernelArgsDefFn args_def_fn,
259 260
                       KernelFn kernel_fn,
                       void* variadic_kernel_fn) {
Y
YuanRisheng 已提交
261
    std::string kernel_name(kernel_name_cstr);
262 263
    KernelKey kernel_key(
        paddle::experimental::StringToBackend(backend_cstr), layout, dtype);
264
    Kernel kernel(kernel_fn, variadic_kernel_fn);
265
    args_parse_fn(kernel_key, kernel.mutable_args_def());
266
    args_def_fn(kernel_key, &kernel);
267
    if (reg_type == RegType::INNER) {
268 269
      KernelFactory::Instance().kernels()[kernel_name][kernel_key] = kernel;
    } else {
270 271
      CustomKernelMap::Instance().RegisterCustomKernel(
          kernel_name, kernel_key, kernel);
272
    }
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  }
};

/**
 * 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
 */
289 290 291
#define PD_NARGS(...) _PD_NARGS((__VA_ARGS__, _PD_RESQ_N()))
#define _PD_NARGS(...) _PD_ARG_N(__VA_ARGS__)
#define _PD_ARG_N_EXPAND(                                                     \
292 293
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
294 295
#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
296

297
/** PD_REGISTER_KERNEL
298 299 300 301 302
 *
 * 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.
303
 *
304
 * Note: `2TA` means `2 template argument`
305
 */
306
#define PD_REGISTER_KERNEL(kernel_name, backend, layout, meta_kernel_fn, ...) \
307
  _PD_REGISTER_KERNEL(::phi::RegType::INNER,                                  \
308 309 310 311 312 313 314
                      kernel_name,                                            \
                      backend,                                                \
                      ::phi::backend##Context,                                \
                      layout,                                                 \
                      meta_kernel_fn,                                         \
                      __VA_ARGS__)

315
#define _PD_REGISTER_KERNEL(                                               \
316
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)  \
317
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
318 319
      PD_REGISTER_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_KERNEL must be called in global namespace.");           \
320
  PD_EXPAND(_PD_REGISTER_2TA_KERNEL(reg_type,                              \
321 322 323 324 325 326
                                    kernel_name,                           \
                                    backend,                               \
                                    context,                               \
                                    layout,                                \
                                    meta_kernel_fn,                        \
                                    __VA_ARGS__))
327

328
#ifndef _WIN32
329
#define _PD_REGISTER_2TA_KERNEL(                                            \
330
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
331 332
  PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, __VA_ARGS__);   \
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
333
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
334
  PD_KERNEL_REGISTRAR_INIT(                                                 \
335
      reg_type,                                                             \
336 337
      kernel_name,                                                          \
      backend,                                                              \
338
      context,                                                              \
339
      layout,                                                               \
340
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
341 342
      meta_kernel_fn,                                                       \
      __VA_ARGS__);                                                         \
343
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
344
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
345 346 347 348 349 350 351 352 353 354 355
#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
 *
356
 * And msvc can work without template instantiation
357
 */
358
#define _PD_REGISTER_2TA_KERNEL(                                            \
359
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
360
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
361
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
362
  PD_EXPAND(PD_KERNEL_REGISTRAR_INIT(                                       \
363
      reg_type,                                                             \
364 365
      kernel_name,                                                          \
      backend,                                                              \
366
      context,                                                              \
367
      layout,                                                               \
368
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
369
      meta_kernel_fn,                                                       \
370
      __VA_ARGS__));                                                        \
371
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
372
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
373 374
#endif

375 376 377
#define PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, ...) \
  _PD_KERNEL_INSTANTIATION(                                            \
      PD_NARGS(__VA_ARGS__), meta_kernel_fn, backend, context, __VA_ARGS__)
378

379 380
#define _PD_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, context, ...) \
  PD_CONCATENATE(_PD_KERNEL_INSTANTIATION_, N)                             \
381 382
  (meta_kernel_fn, backend, context, __VA_ARGS__)

383
#define _PD_KERNEL_INSTANTIATION_1(              \
384 385 386
    meta_kernel_fn, backend, context, cpp_dtype) \
  template decltype(                             \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>
387
#define _PD_KERNEL_INSTANTIATION_2(                                           \
388 389 390
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
391
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_1(                                       \
392
      meta_kernel_fn, backend, context, __VA_ARGS__))
393
#define _PD_KERNEL_INSTANTIATION_3(                                           \
394 395 396
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
397
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_2(                                       \
398
      meta_kernel_fn, backend, context, __VA_ARGS__))
399
#define _PD_KERNEL_INSTANTIATION_4(                                           \
400 401 402
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
403
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_3(                                       \
404
      meta_kernel_fn, backend, context, __VA_ARGS__))
405
#define _PD_KERNEL_INSTANTIATION_5(                                           \
406 407 408
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
409
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_4(                                       \
410
      meta_kernel_fn, backend, context, __VA_ARGS__))
411
#define _PD_KERNEL_INSTANTIATION_6(                                           \
412 413 414
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
415
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_5(                                       \
416
      meta_kernel_fn, backend, context, __VA_ARGS__))
417
#define _PD_KERNEL_INSTANTIATION_7(                                           \
418 419 420
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
421
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_6(                                       \
422
      meta_kernel_fn, backend, context, __VA_ARGS__))
423
#define _PD_KERNEL_INSTANTIATION_8(                                           \
424 425 426
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
427
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_7(                                       \
428
      meta_kernel_fn, backend, context, __VA_ARGS__))
429
#define _PD_KERNEL_INSTANTIATION_9(                                           \
430 431 432
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
433
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_8(                                       \
434
      meta_kernel_fn, backend, context, __VA_ARGS__))
435
#define _PD_KERNEL_INSTANTIATION_10(                                          \
436 437 438
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
439
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_9(                                       \
440
      meta_kernel_fn, backend, context, __VA_ARGS__))
441
#define _PD_KERNEL_INSTANTIATION_11(                                          \
442 443 444
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
445
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_10(                                      \
446
      meta_kernel_fn, backend, context, __VA_ARGS__))
447
#define _PD_KERNEL_INSTANTIATION_12(                                          \
448 449 450
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
451
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_11(                                      \
452
      meta_kernel_fn, backend, context, __VA_ARGS__))
453
#define _PD_KERNEL_INSTANTIATION_13(                                          \
454 455 456
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
457
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_12(                                      \
458
      meta_kernel_fn, backend, context, __VA_ARGS__))
459
#define _PD_KERNEL_INSTANTIATION_14(                                          \
460 461 462
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
463
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_13(                                      \
464
      meta_kernel_fn, backend, context, __VA_ARGS__))
465
#define _PD_KERNEL_INSTANTIATION_15(                                          \
466 467 468
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
469
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_14(                                      \
470 471
      meta_kernel_fn, backend, context, __VA_ARGS__))

472
#define PD_KERNEL_REGISTRAR_INIT(reg_type,                   \
473 474 475 476 477 478 479
                                 kernel_name,                \
                                 backend,                    \
                                 context,                    \
                                 layout,                     \
                                 args_def_fn,                \
                                 meta_kernel_fn,             \
                                 ...)                        \
480
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT(PD_NARGS(__VA_ARGS__), \
481 482 483 484 485 486 487
                                      reg_type,              \
                                      kernel_name,           \
                                      backend,               \
                                      context,               \
                                      layout,                \
                                      args_def_fn,           \
                                      meta_kernel_fn,        \
488
                                      __VA_ARGS__))
489 490 491 492 493

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
494
#define _PD_KERNEL_REGISTRAR_INIT(N,                       \
495
                                  reg_type,                \
496 497
                                  kernel_name,             \
                                  backend,                 \
498
                                  context,                 \
499 500 501 502
                                  layout,                  \
                                  args_def_fn,             \
                                  meta_kernel_fn,          \
                                  ...)                     \
503
  PD_EXPAND(PD_CONCATENATE(_PD_KERNEL_REGISTRAR_INIT_, N) ( \
504
    reg_type,                                              \
505 506
    kernel_name,                                           \
    backend,                                               \
507
    context,                                               \
508
    layout,                                                \
509
    PD_ID,                                                 \
510 511 512
    args_def_fn,                                           \
    meta_kernel_fn,                                        \
    __VA_ARGS__))
513 514 515

// clang-format on

516
#define _PD_KERNEL_REGISTRAR_INIT_1(reg_type,                                 \
517 518 519 520 521 522 523 524
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype)                                \
525
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
526 527 528 529 530 531 532 533 534
      __reg_pt_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,                                                            \
535 536
      PHI_KERNEL(meta_kernel_fn<cpp_dtype, context>),                         \
      PHI_VARIADIC_KERNEL(meta_kernel_fn<cpp_dtype, context>));               \
537
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() { return 0; }
538
#define _PD_KERNEL_REGISTRAR_INIT_2(reg_type,                                 \
539 540 541 542 543 544 545 546 547
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
548
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
549 550 551 552 553 554 555 556 557
      __reg_pt_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,                                                            \
558 559 560
      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,                             \
561 562 563 564
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
565
                                        PD_ID,                                \
566 567
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
568
                                        __VA_ARGS__))
569
#define _PD_KERNEL_REGISTRAR_INIT_3(reg_type,                                 \
570 571 572 573 574 575 576 577 578
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
579
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
580 581 582 583 584 585 586 587 588
      __reg_pt_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,                                                            \
589 590 591
      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,                             \
592 593 594 595
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
596
                                        PD_ID,                                \
597 598
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
599
                                        __VA_ARGS__))
600
#define _PD_KERNEL_REGISTRAR_INIT_4(reg_type,                                 \
601 602 603 604 605 606 607 608 609
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
610
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
611 612 613 614 615 616 617 618 619
      __reg_pt_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,                                                            \
620 621 622
      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,                             \
623 624 625 626
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
627
                                        PD_ID,                                \
628 629
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
630
                                        __VA_ARGS__))
631
#define _PD_KERNEL_REGISTRAR_INIT_5(reg_type,                                 \
632 633 634 635 636 637 638 639 640
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
641
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
642 643 644 645 646 647 648 649 650
      __reg_pt_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,                                                            \
651 652 653
      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,                             \
654 655 656 657
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
658
                                        PD_ID,                                \
659 660
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
661
                                        __VA_ARGS__))
662
#define _PD_KERNEL_REGISTRAR_INIT_6(reg_type,                                 \
663 664 665 666 667 668 669 670 671
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
672
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
673 674 675 676 677 678 679 680 681
      __reg_pt_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,                                                            \
682 683 684
      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,                             \
685 686 687 688
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
689
                                        PD_ID,                                \
690 691
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
692
                                        __VA_ARGS__))
693
#define _PD_KERNEL_REGISTRAR_INIT_7(reg_type,                                 \
694 695 696 697 698 699 700 701 702
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
703
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
704 705 706 707 708 709 710 711 712
      __reg_pt_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,                                                            \
713 714 715
      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,                             \
716 717 718 719
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
720
                                        PD_ID,                                \
721 722
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
723
                                        __VA_ARGS__))
724
#define _PD_KERNEL_REGISTRAR_INIT_8(reg_type,                                 \
725 726 727 728 729 730 731 732 733
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
734
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
735 736 737 738 739 740 741 742 743
      __reg_pt_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,                                                            \
744 745 746
      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,                             \
747 748 749 750
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
751
                                        PD_ID,                                \
752 753
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
754
                                        __VA_ARGS__))
755
#define _PD_KERNEL_REGISTRAR_INIT_9(reg_type,                                 \
756 757 758 759 760 761 762 763 764
                                    kernel_name,                              \
                                    backend,                                  \
                                    context,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
765
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
766 767 768 769 770 771 772 773 774
      __reg_pt_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,                                                            \
775 776 777
      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,                             \
778 779 780 781
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
782
                                        PD_ID,                                \
783 784
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
785
                                        __VA_ARGS__))
786
#define _PD_KERNEL_REGISTRAR_INIT_10(reg_type,                                \
787 788 789 790 791 792 793 794 795
                                     kernel_name,                             \
                                     backend,                                 \
                                     context,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
796
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
797 798 799 800 801 802 803 804 805
      __reg_pt_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,                                                            \
806 807 808
      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,                             \
809 810 811 812
                                        kernel_name,                          \
                                        backend,                              \
                                        context,                              \
                                        layout,                               \
813
                                        PD_ID,                                \
814 815
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
816
                                        __VA_ARGS__))
817
#define _PD_KERNEL_REGISTRAR_INIT_11(reg_type,                                \
818 819 820 821 822 823 824 825 826
                                     kernel_name,                             \
                                     backend,                                 \
                                     context,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
827
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
828 829 830 831 832 833 834 835 836
      __reg_pt_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,                                                            \
837 838 839
      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,                            \
840 841 842 843
                                         kernel_name,                         \
                                         backend,                             \
                                         context,                             \
                                         layout,                              \
844
                                         PD_ID,                               \
845 846
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
847
                                         __VA_ARGS__))
848
#define _PD_KERNEL_REGISTRAR_INIT_12(reg_type,                                \
849 850 851 852 853 854 855 856 857
                                     kernel_name,                             \
                                     backend,                                 \
                                     context,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
858
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
859 860 861 862 863 864 865 866 867
      __reg_pt_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,                                                            \
868 869 870
      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,                            \
871 872 873 874
                                         kernel_name,                         \
                                         backend,                             \
                                         context,                             \
                                         layout,                              \
875
                                         PD_ID,                               \
876 877
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
878
                                         __VA_ARGS__))
879
#define _PD_KERNEL_REGISTRAR_INIT_13(reg_type,                                \
880 881 882 883 884 885 886 887 888
                                     kernel_name,                             \
                                     backend,                                 \
                                     context,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
889
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
890 891 892 893 894 895 896 897 898
      __reg_pt_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,                                                            \
899 900 901
      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,                            \
902 903 904 905
                                         kernel_name,                         \
                                         backend,                             \
                                         context,                             \
                                         layout,                              \
906
                                         PD_ID,                               \
907 908
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
909
                                         __VA_ARGS__))
910
#define _PD_KERNEL_REGISTRAR_INIT_14(reg_type,                                \
911 912 913 914 915 916 917 918 919
                                     kernel_name,                             \
                                     backend,                                 \
                                     context,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
920
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
921 922 923 924 925 926 927 928 929
      __reg_pt_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,                                                            \
930 931 932
      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,                            \
933 934 935 936
                                         kernel_name,                         \
                                         backend,                             \
                                         context,                             \
                                         layout,                              \
937
                                         PD_ID,                               \
938 939
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
940
                                         __VA_ARGS__))
941
#define _PD_KERNEL_REGISTRAR_INIT_15(reg_type,                                \
942 943 944 945 946 947 948 949 950
                                     kernel_name,                             \
                                     backend,                                 \
                                     context,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
951
  static const ::phi::KernelRegistrar PD_CONCATENATE(                         \
952 953 954 955 956 957 958 959 960
      __reg_pt_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,                                                            \
961 962 963
      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,                            \
964 965 966 967
                                         kernel_name,                         \
                                         backend,                             \
                                         context,                             \
                                         layout,                              \
968
                                         PD_ID,                               \
969 970
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
971
                                         __VA_ARGS__))
972
/** PD_REGISTER_GENERAL_KERNEL
973 974 975 976 977
 *
 * Basic Kernel register marco, used to register a instantiated kernel function
 * with one template argument.
 */

978
#define PD_REGISTER_GENERAL_KERNEL(                 \
979
    kernel_name, backend, layout, kernel_fn, dtype) \
980
  _PD_REGISTER_GENERAL_KERNEL(                      \
981
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn, dtype)
982

983
#define _PD_REGISTER_GENERAL_KERNEL(                                         \
984
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)                \
985
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
986 987 988
      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(                                              \
989
      reg_type, kernel_name, backend, layout, kernel_fn, dtype)
990 991

#ifndef _WIN32
992
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
993
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
994
  template decltype(kernel_fn) kernel_fn;                                   \
995
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
996 997
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
998
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
999
          reg_type,                                                         \
1000
          #kernel_name,                                                     \
1001
          #backend,                                                         \
1002
          DATALAYOUT(layout),                                               \
1003
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1004 1005 1006
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1007 1008 1009
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1010
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1011
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1012
#else
1013
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
1014
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
1015
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
1016 1017
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
1018
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
1019
          reg_type,                                                         \
1020
          #kernel_name,                                                     \
1021
          #backend,                                                         \
1022
          DATALAYOUT(layout),                                               \
1023
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1024 1025 1026
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1027 1028 1029
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1030
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1031
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1032 1033
#endif

1034
/** PD_DECLARE_KERNEL
1035 1036 1037 1038
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
1039
#define PD_DECLARE_KERNEL(kernel_name, backend, layout)                   \
1040
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                      \
1041 1042
      PD_DECLARE_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_DECLARE_KERNEL must be called in global namespace.");           \
1043 1044 1045 1046
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
1047

1048
/** PD_REGISTER_BUILTIN_KERNEL
1049 1050 1051 1052
 *
 * Used to register kernels for built-in backends.
 * Support CPU GPU XPU.
 */
1053 1054
#define PD_REGISTER_BUILTIN_KERNEL(                    \
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1055
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1056 1057 1058 1059 1060
                      kernel_name,                     \
                      backend,                         \
                      ::phi::backend##Context,         \
                      layout,                          \
                      meta_kernel_fn,                  \
1061 1062
                      __VA_ARGS__)

1063
/** PD_REGISTER_PLUGIN_KERNEL
1064 1065 1066 1067
 *
 * Used to register kernels for plug-in backends.
 * Support user-defined backend such as 'Ascend910'.
 */
1068
#define PD_REGISTER_PLUGIN_KERNEL(                     \
1069
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1070
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1071 1072 1073 1074 1075 1076 1077
                      kernel_name,                     \
                      backend,                         \
                      ::phi::CustomContext,            \
                      layout,                          \
                      meta_kernel_fn,                  \
                      __VA_ARGS__)

1078
}  // namespace phi