kernel_registry.h 64.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);
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);
101
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
H
hong 已提交
102 103 104 105
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
106 107
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
H
hong 已提交
108 109 110 111
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
112
      } else if (arg_type == std::type_index(typeid(SelectedRows*))) {
H
hong 已提交
113 114 115 116
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
      } 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>))...};
  }
};

134
// NOTE: used for making a difference between inner or outer registration.
135
enum class RegType : uint8_t {
136 137
  INNER = 0,
  OUTER,
138 139
};

140 141
// TODO(chenweihang): Polish the kernel selection logic, support the selection
// of ALL_DTYPE kernel, and simplify the constructor
142 143
struct KernelRegistrar {
 public:
144 145 146
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
147 148 149 150
                  DataLayout layout,
                  DataType dtype,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
151 152
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
153 154 155
    ConstructKernel(reg_type,
                    kernel_name_cstr,
                    backend_cstr,
156 157 158 159
                    layout,
                    dtype,
                    args_parse_fn,
                    args_def_fn,
160 161
                    kernel_fn,
                    variadic_kernel_fn);
162 163
  }

164 165 166
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
167 168 169
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
170 171
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
172 173 174
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
175 176 177 178 179 180 181
      // 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;
      }
182 183 184
      ConstructKernel(reg_type,
                      kernel_name_cstr,
                      backend_cstr,
185 186 187 188
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
189 190
                      kernel_fn,
                      variadic_kernel_fn);
191 192 193 194
    }
  }

 private:
195 196 197
  void ConstructKernel(RegType reg_type,
                       const char* kernel_name_cstr,
                       const char* backend_cstr,
198 199 200 201
                       DataLayout layout,
                       DataType dtype,
                       KernelArgsParseFn args_parse_fn,
                       KernelArgsDefFn args_def_fn,
202 203
                       KernelFn kernel_fn,
                       void* variadic_kernel_fn) {
Y
YuanRisheng 已提交
204
    std::string kernel_name(kernel_name_cstr);
205 206
    KernelKey kernel_key(
        paddle::experimental::StringToBackend(backend_cstr), layout, dtype);
207
    Kernel kernel(kernel_fn, variadic_kernel_fn);
208
    args_parse_fn(kernel_key, kernel.mutable_args_def());
209
    args_def_fn(kernel_key, &kernel);
210
    if (reg_type == RegType::INNER) {
211 212 213 214
      KernelFactory::Instance().kernels()[kernel_name][kernel_key] = kernel;
    } else {
      CustomKernelMap::Instance().Kernels()[kernel_name][kernel_key] = kernel;
    }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  }
};

/**
 * 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
 */
231 232 233
#define PD_NARGS(...) _PD_NARGS((__VA_ARGS__, _PD_RESQ_N()))
#define _PD_NARGS(...) _PD_ARG_N(__VA_ARGS__)
#define _PD_ARG_N_EXPAND(                                                     \
234 235
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
236 237
#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
238

239
/** PD_REGISTER_KERNEL
240 241 242 243 244
 *
 * 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.
245
 *
246
 * Note: `2TA` means `2 template argument`
247
 */
248
#define PD_REGISTER_KERNEL(kernel_name, backend, layout, meta_kernel_fn, ...) \
249
  _PD_REGISTER_KERNEL(::phi::RegType::INNER,                                  \
250 251 252 253 254 255 256
                      kernel_name,                                            \
                      backend,                                                \
                      ::phi::backend##Context,                                \
                      layout,                                                 \
                      meta_kernel_fn,                                         \
                      __VA_ARGS__)

257
#define _PD_REGISTER_KERNEL(                                               \
258
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)  \
259
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
260 261
      PD_REGISTER_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_KERNEL must be called in global namespace.");           \
262
  PD_EXPAND(_PD_REGISTER_2TA_KERNEL(reg_type,                              \
263 264 265 266 267 268
                                    kernel_name,                           \
                                    backend,                               \
                                    context,                               \
                                    layout,                                \
                                    meta_kernel_fn,                        \
                                    __VA_ARGS__))
269

270
#ifndef _WIN32
271
#define _PD_REGISTER_2TA_KERNEL(                                            \
272
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
273 274
  PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, __VA_ARGS__);   \
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
275
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
276
  PD_KERNEL_REGISTRAR_INIT(                                                 \
277
      reg_type,                                                             \
278 279
      kernel_name,                                                          \
      backend,                                                              \
280
      context,                                                              \
281
      layout,                                                               \
282
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
283 284
      meta_kernel_fn,                                                       \
      __VA_ARGS__);                                                         \
285
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
286
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
287 288 289 290 291 292 293 294 295 296 297
#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
 *
298
 * And msvc can work without template instantiation
299
 */
300
#define _PD_REGISTER_2TA_KERNEL(                                            \
301
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
302
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
303
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
304
  PD_EXPAND(PD_KERNEL_REGISTRAR_INIT(                                       \
305
      reg_type,                                                             \
306 307
      kernel_name,                                                          \
      backend,                                                              \
308
      context,                                                              \
309
      layout,                                                               \
310
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
311
      meta_kernel_fn,                                                       \
312
      __VA_ARGS__));                                                        \
313
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
314
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
315 316
#endif

317 318 319
#define PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, ...) \
  _PD_KERNEL_INSTANTIATION(                                            \
      PD_NARGS(__VA_ARGS__), meta_kernel_fn, backend, context, __VA_ARGS__)
320

321 322
#define _PD_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, context, ...) \
  PD_CONCATENATE(_PD_KERNEL_INSTANTIATION_, N)                             \
323 324
  (meta_kernel_fn, backend, context, __VA_ARGS__)

325
#define _PD_KERNEL_INSTANTIATION_1(              \
326 327 328
    meta_kernel_fn, backend, context, cpp_dtype) \
  template decltype(                             \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>
329
#define _PD_KERNEL_INSTANTIATION_2(                                           \
330 331 332
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
333
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_1(                                       \
334
      meta_kernel_fn, backend, context, __VA_ARGS__))
335
#define _PD_KERNEL_INSTANTIATION_3(                                           \
336 337 338
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
339
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_2(                                       \
340
      meta_kernel_fn, backend, context, __VA_ARGS__))
341
#define _PD_KERNEL_INSTANTIATION_4(                                           \
342 343 344
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
345
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_3(                                       \
346
      meta_kernel_fn, backend, context, __VA_ARGS__))
347
#define _PD_KERNEL_INSTANTIATION_5(                                           \
348 349 350
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
351
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_4(                                       \
352
      meta_kernel_fn, backend, context, __VA_ARGS__))
353
#define _PD_KERNEL_INSTANTIATION_6(                                           \
354 355 356
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
357
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_5(                                       \
358
      meta_kernel_fn, backend, context, __VA_ARGS__))
359
#define _PD_KERNEL_INSTANTIATION_7(                                           \
360 361 362
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
363
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_6(                                       \
364
      meta_kernel_fn, backend, context, __VA_ARGS__))
365
#define _PD_KERNEL_INSTANTIATION_8(                                           \
366 367 368
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
369
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_7(                                       \
370
      meta_kernel_fn, backend, context, __VA_ARGS__))
371
#define _PD_KERNEL_INSTANTIATION_9(                                           \
372 373 374
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
375
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_8(                                       \
376
      meta_kernel_fn, backend, context, __VA_ARGS__))
377
#define _PD_KERNEL_INSTANTIATION_10(                                          \
378 379 380
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
381
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_9(                                       \
382
      meta_kernel_fn, backend, context, __VA_ARGS__))
383
#define _PD_KERNEL_INSTANTIATION_11(                                          \
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
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_10(                                      \
388
      meta_kernel_fn, backend, context, __VA_ARGS__))
389
#define _PD_KERNEL_INSTANTIATION_12(                                          \
390 391 392
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
393
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_11(                                      \
394
      meta_kernel_fn, backend, context, __VA_ARGS__))
395
#define _PD_KERNEL_INSTANTIATION_13(                                          \
396 397 398
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
399
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_12(                                      \
400
      meta_kernel_fn, backend, context, __VA_ARGS__))
401
#define _PD_KERNEL_INSTANTIATION_14(                                          \
402 403 404
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
405
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_13(                                      \
406
      meta_kernel_fn, backend, context, __VA_ARGS__))
407
#define _PD_KERNEL_INSTANTIATION_15(                                          \
408 409 410
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
411
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_14(                                      \
412 413
      meta_kernel_fn, backend, context, __VA_ARGS__))

414
#define PD_KERNEL_REGISTRAR_INIT(reg_type,                   \
415 416 417 418 419 420 421
                                 kernel_name,                \
                                 backend,                    \
                                 context,                    \
                                 layout,                     \
                                 args_def_fn,                \
                                 meta_kernel_fn,             \
                                 ...)                        \
422
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT(PD_NARGS(__VA_ARGS__), \
423 424 425 426 427 428 429
                                      reg_type,              \
                                      kernel_name,           \
                                      backend,               \
                                      context,               \
                                      layout,                \
                                      args_def_fn,           \
                                      meta_kernel_fn,        \
430
                                      __VA_ARGS__))
431 432 433 434 435

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
436
#define _PD_KERNEL_REGISTRAR_INIT(N,                       \
437
                                  reg_type,                \
438 439
                                  kernel_name,             \
                                  backend,                 \
440
                                  context,                 \
441 442 443 444
                                  layout,                  \
                                  args_def_fn,             \
                                  meta_kernel_fn,          \
                                  ...)                     \
445
  PD_EXPAND(PD_CONCATENATE(_PD_KERNEL_REGISTRAR_INIT_, N) ( \
446
    reg_type,                                              \
447 448
    kernel_name,                                           \
    backend,                                               \
449
    context,                                               \
450
    layout,                                                \
451
    PD_ID,                                                 \
452 453 454
    args_def_fn,                                           \
    meta_kernel_fn,                                        \
    __VA_ARGS__))
455 456 457

// clang-format on

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

920
#define PD_REGISTER_GENERAL_KERNEL(                 \
921
    kernel_name, backend, layout, kernel_fn, dtype) \
922
  _PD_REGISTER_GENERAL_KERNEL(                      \
923
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn, dtype)
924

925
#define _PD_REGISTER_GENERAL_KERNEL(                                         \
926
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)                \
927
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
928 929 930
      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(                                              \
931
      reg_type, kernel_name, backend, layout, kernel_fn, dtype)
932 933

#ifndef _WIN32
934
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
935
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
936
  template decltype(kernel_fn) kernel_fn;                                   \
937
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
938 939
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
940
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
941
          reg_type,                                                         \
942
          #kernel_name,                                                     \
943
          #backend,                                                         \
944
          DATALAYOUT(layout),                                               \
945
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
946 947 948
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
949 950 951
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
952
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
953
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
954
#else
955
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
956
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
957
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
958 959
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
960
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
961
          reg_type,                                                         \
962
          #kernel_name,                                                     \
963
          #backend,                                                         \
964
          DATALAYOUT(layout),                                               \
965
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
966 967 968
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
969 970 971
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
972
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
973
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
974 975
#endif

976
/** PD_DECLARE_KERNEL
977 978 979 980
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
981
#define PD_DECLARE_KERNEL(kernel_name, backend, layout)                   \
982
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                      \
983 984
      PD_DECLARE_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_DECLARE_KERNEL must be called in global namespace.");           \
985 986 987 988
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
989

990
/** PD_REGISTER_BUILTIN_KERNEL
991 992 993 994
 *
 * Used to register kernels for built-in backends.
 * Support CPU GPU XPU.
 */
995 996
#define PD_REGISTER_BUILTIN_KERNEL(                    \
    kernel_name, backend, layout, meta_kernel_fn, ...) \
997
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
998 999 1000 1001 1002
                      kernel_name,                     \
                      backend,                         \
                      ::phi::backend##Context,         \
                      layout,                          \
                      meta_kernel_fn,                  \
1003 1004
                      __VA_ARGS__)

1005
/** PD_REGISTER_PLUGIN_KERNEL
1006 1007 1008 1009
 *
 * Used to register kernels for plug-in backends.
 * Support user-defined backend such as 'Ascend910'.
 */
1010
#define PD_REGISTER_PLUGIN_KERNEL(                     \
1011
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1012
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1013 1014 1015 1016 1017 1018 1019
                      kernel_name,                     \
                      backend,                         \
                      ::phi::CustomContext,            \
                      layout,                          \
                      meta_kernel_fn,                  \
                      __VA_ARGS__)

1020
}  // namespace phi