kernel_registry.h 65.8 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);
H
hong 已提交
84 85 86 87 88 89
      } 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);
90 91
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const DenseTensor*>&))) {
H
hong 已提交
92 93 94 95
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
96
      } else if (arg_type == std::type_index(typeid(const SelectedRows&))) {
H
hong 已提交
97 98 99 100
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
Z
zhangkaihuo 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      } 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);
123
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
H
hong 已提交
124 125 126 127
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
128 129
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
H
hong 已提交
130 131 132 133
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
134
      } else if (arg_type == std::type_index(typeid(SelectedRows*))) {
H
hong 已提交
135 136 137 138
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
Z
zhangkaihuo 已提交
139 140 141 142 143 144 145 146 147 148
      } 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);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      } 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>))...};
  }
};

166
// NOTE: used for making a difference between inner or outer registration.
167
enum class RegType : uint8_t {
168 169
  INNER = 0,
  OUTER,
170 171
};

172 173
// TODO(chenweihang): Polish the kernel selection logic, support the selection
// of ALL_DTYPE kernel, and simplify the constructor
174 175
struct KernelRegistrar {
 public:
176 177 178
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
179 180 181 182
                  DataLayout layout,
                  DataType dtype,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
183 184
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
185 186 187
    ConstructKernel(reg_type,
                    kernel_name_cstr,
                    backend_cstr,
188 189 190 191
                    layout,
                    dtype,
                    args_parse_fn,
                    args_def_fn,
192 193
                    kernel_fn,
                    variadic_kernel_fn);
194 195
  }

196 197 198
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
199 200 201
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
202 203
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
204 205 206
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
207 208 209 210 211 212 213
      // 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;
      }
214 215 216
      ConstructKernel(reg_type,
                      kernel_name_cstr,
                      backend_cstr,
217 218 219 220
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
221 222
                      kernel_fn,
                      variadic_kernel_fn);
223 224 225 226
    }
  }

 private:
227 228 229
  void ConstructKernel(RegType reg_type,
                       const char* kernel_name_cstr,
                       const char* backend_cstr,
230 231 232 233
                       DataLayout layout,
                       DataType dtype,
                       KernelArgsParseFn args_parse_fn,
                       KernelArgsDefFn args_def_fn,
234 235
                       KernelFn kernel_fn,
                       void* variadic_kernel_fn) {
Y
YuanRisheng 已提交
236
    std::string kernel_name(kernel_name_cstr);
237 238
    KernelKey kernel_key(
        paddle::experimental::StringToBackend(backend_cstr), layout, dtype);
239
    Kernel kernel(kernel_fn, variadic_kernel_fn);
240
    args_parse_fn(kernel_key, kernel.mutable_args_def());
241
    args_def_fn(kernel_key, &kernel);
242
    if (reg_type == RegType::INNER) {
243 244
      KernelFactory::Instance().kernels()[kernel_name][kernel_key] = kernel;
    } else {
245 246
      CustomKernelMap::Instance().RegisterCustomKernel(
          kernel_name, kernel_key, kernel);
247
    }
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  }
};

/**
 * 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
 */
264 265 266
#define PD_NARGS(...) _PD_NARGS((__VA_ARGS__, _PD_RESQ_N()))
#define _PD_NARGS(...) _PD_ARG_N(__VA_ARGS__)
#define _PD_ARG_N_EXPAND(                                                     \
267 268
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
269 270
#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
271

272
/** PD_REGISTER_KERNEL
273 274 275 276 277
 *
 * 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.
278
 *
279
 * Note: `2TA` means `2 template argument`
280
 */
281
#define PD_REGISTER_KERNEL(kernel_name, backend, layout, meta_kernel_fn, ...) \
282
  _PD_REGISTER_KERNEL(::phi::RegType::INNER,                                  \
283 284 285 286 287 288 289
                      kernel_name,                                            \
                      backend,                                                \
                      ::phi::backend##Context,                                \
                      layout,                                                 \
                      meta_kernel_fn,                                         \
                      __VA_ARGS__)

290
#define _PD_REGISTER_KERNEL(                                               \
291
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)  \
292
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
293 294
      PD_REGISTER_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_KERNEL must be called in global namespace.");           \
295
  PD_EXPAND(_PD_REGISTER_2TA_KERNEL(reg_type,                              \
296 297 298 299 300 301
                                    kernel_name,                           \
                                    backend,                               \
                                    context,                               \
                                    layout,                                \
                                    meta_kernel_fn,                        \
                                    __VA_ARGS__))
302

303
#ifndef _WIN32
304
#define _PD_REGISTER_2TA_KERNEL(                                            \
305
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
306 307
  PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, __VA_ARGS__);   \
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
308
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
309
  PD_KERNEL_REGISTRAR_INIT(                                                 \
310
      reg_type,                                                             \
311 312
      kernel_name,                                                          \
      backend,                                                              \
313
      context,                                                              \
314
      layout,                                                               \
315
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
316 317
      meta_kernel_fn,                                                       \
      __VA_ARGS__);                                                         \
318
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
319
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
320 321 322 323 324 325 326 327 328 329 330
#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
 *
331
 * And msvc can work without template instantiation
332
 */
333
#define _PD_REGISTER_2TA_KERNEL(                                            \
334
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
335
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
336
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
337
  PD_EXPAND(PD_KERNEL_REGISTRAR_INIT(                                       \
338
      reg_type,                                                             \
339 340
      kernel_name,                                                          \
      backend,                                                              \
341
      context,                                                              \
342
      layout,                                                               \
343
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
344
      meta_kernel_fn,                                                       \
345
      __VA_ARGS__));                                                        \
346
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
347
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
348 349
#endif

350 351 352
#define PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, ...) \
  _PD_KERNEL_INSTANTIATION(                                            \
      PD_NARGS(__VA_ARGS__), meta_kernel_fn, backend, context, __VA_ARGS__)
353

354 355
#define _PD_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, context, ...) \
  PD_CONCATENATE(_PD_KERNEL_INSTANTIATION_, N)                             \
356 357
  (meta_kernel_fn, backend, context, __VA_ARGS__)

358
#define _PD_KERNEL_INSTANTIATION_1(              \
359 360 361
    meta_kernel_fn, backend, context, cpp_dtype) \
  template decltype(                             \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>
362
#define _PD_KERNEL_INSTANTIATION_2(                                           \
363 364 365
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
366
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_1(                                       \
367
      meta_kernel_fn, backend, context, __VA_ARGS__))
368
#define _PD_KERNEL_INSTANTIATION_3(                                           \
369 370 371
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
372
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_2(                                       \
373
      meta_kernel_fn, backend, context, __VA_ARGS__))
374
#define _PD_KERNEL_INSTANTIATION_4(                                           \
375 376 377
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
378
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_3(                                       \
379
      meta_kernel_fn, backend, context, __VA_ARGS__))
380
#define _PD_KERNEL_INSTANTIATION_5(                                           \
381 382 383
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
384
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_4(                                       \
385
      meta_kernel_fn, backend, context, __VA_ARGS__))
386
#define _PD_KERNEL_INSTANTIATION_6(                                           \
387 388 389
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
390
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_5(                                       \
391
      meta_kernel_fn, backend, context, __VA_ARGS__))
392
#define _PD_KERNEL_INSTANTIATION_7(                                           \
393 394 395
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
396
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_6(                                       \
397
      meta_kernel_fn, backend, context, __VA_ARGS__))
398
#define _PD_KERNEL_INSTANTIATION_8(                                           \
399 400 401
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
402
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_7(                                       \
403
      meta_kernel_fn, backend, context, __VA_ARGS__))
404
#define _PD_KERNEL_INSTANTIATION_9(                                           \
405 406 407
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
408
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_8(                                       \
409
      meta_kernel_fn, backend, context, __VA_ARGS__))
410
#define _PD_KERNEL_INSTANTIATION_10(                                          \
411 412 413
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
414
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_9(                                       \
415
      meta_kernel_fn, backend, context, __VA_ARGS__))
416
#define _PD_KERNEL_INSTANTIATION_11(                                          \
417 418 419
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
420
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_10(                                      \
421
      meta_kernel_fn, backend, context, __VA_ARGS__))
422
#define _PD_KERNEL_INSTANTIATION_12(                                          \
423 424 425
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
426
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_11(                                      \
427
      meta_kernel_fn, backend, context, __VA_ARGS__))
428
#define _PD_KERNEL_INSTANTIATION_13(                                          \
429 430 431
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
432
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_12(                                      \
433
      meta_kernel_fn, backend, context, __VA_ARGS__))
434
#define _PD_KERNEL_INSTANTIATION_14(                                          \
435 436 437
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
438
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_13(                                      \
439
      meta_kernel_fn, backend, context, __VA_ARGS__))
440
#define _PD_KERNEL_INSTANTIATION_15(                                          \
441 442 443
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
444
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_14(                                      \
445 446
      meta_kernel_fn, backend, context, __VA_ARGS__))

447
#define PD_KERNEL_REGISTRAR_INIT(reg_type,                   \
448 449 450 451 452 453 454
                                 kernel_name,                \
                                 backend,                    \
                                 context,                    \
                                 layout,                     \
                                 args_def_fn,                \
                                 meta_kernel_fn,             \
                                 ...)                        \
455
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT(PD_NARGS(__VA_ARGS__), \
456 457 458 459 460 461 462
                                      reg_type,              \
                                      kernel_name,           \
                                      backend,               \
                                      context,               \
                                      layout,                \
                                      args_def_fn,           \
                                      meta_kernel_fn,        \
463
                                      __VA_ARGS__))
464 465 466 467 468

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
469
#define _PD_KERNEL_REGISTRAR_INIT(N,                       \
470
                                  reg_type,                \
471 472
                                  kernel_name,             \
                                  backend,                 \
473
                                  context,                 \
474 475 476 477
                                  layout,                  \
                                  args_def_fn,             \
                                  meta_kernel_fn,          \
                                  ...)                     \
478
  PD_EXPAND(PD_CONCATENATE(_PD_KERNEL_REGISTRAR_INIT_, N) ( \
479
    reg_type,                                              \
480 481
    kernel_name,                                           \
    backend,                                               \
482
    context,                                               \
483
    layout,                                                \
484
    PD_ID,                                                 \
485 486 487
    args_def_fn,                                           \
    meta_kernel_fn,                                        \
    __VA_ARGS__))
488 489 490

// clang-format on

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

953
#define PD_REGISTER_GENERAL_KERNEL(                 \
954
    kernel_name, backend, layout, kernel_fn, dtype) \
955
  _PD_REGISTER_GENERAL_KERNEL(                      \
956
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn, dtype)
957

958
#define _PD_REGISTER_GENERAL_KERNEL(                                         \
959
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)                \
960
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
961 962 963
      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(                                              \
964
      reg_type, kernel_name, backend, layout, kernel_fn, dtype)
965 966

#ifndef _WIN32
967
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
968
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
969
  template decltype(kernel_fn) kernel_fn;                                   \
970
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
971 972
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
973
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
974
          reg_type,                                                         \
975
          #kernel_name,                                                     \
976
          #backend,                                                         \
977
          DATALAYOUT(layout),                                               \
978
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
979 980 981
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
982 983 984
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
985
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
986
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
987
#else
988
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
989
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
990
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
991 992
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
993
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
994
          reg_type,                                                         \
995
          #kernel_name,                                                     \
996
          #backend,                                                         \
997
          DATALAYOUT(layout),                                               \
998
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
999 1000 1001
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1002 1003 1004
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1005
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1006
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1007 1008
#endif

1009
/** PD_DECLARE_KERNEL
1010 1011 1012 1013
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
1014
#define PD_DECLARE_KERNEL(kernel_name, backend, layout)                   \
1015
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                      \
1016 1017
      PD_DECLARE_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_DECLARE_KERNEL must be called in global namespace.");           \
1018 1019 1020 1021
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
1022

1023
/** PD_REGISTER_BUILTIN_KERNEL
1024 1025 1026 1027
 *
 * Used to register kernels for built-in backends.
 * Support CPU GPU XPU.
 */
1028 1029
#define PD_REGISTER_BUILTIN_KERNEL(                    \
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1030
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1031 1032 1033 1034 1035
                      kernel_name,                     \
                      backend,                         \
                      ::phi::backend##Context,         \
                      layout,                          \
                      meta_kernel_fn,                  \
1036 1037
                      __VA_ARGS__)

1038
/** PD_REGISTER_PLUGIN_KERNEL
1039 1040 1041 1042
 *
 * Used to register kernels for plug-in backends.
 * Support user-defined backend such as 'Ascend910'.
 */
1043
#define PD_REGISTER_PLUGIN_KERNEL(                     \
1044
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1045
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1046 1047 1048 1049 1050 1051 1052
                      kernel_name,                     \
                      backend,                         \
                      ::phi::CustomContext,            \
                      layout,                          \
                      meta_kernel_fn,                  \
                      __VA_ARGS__)

1053
}  // namespace phi