kernel_registry.h 63.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//   Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

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

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

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

32
namespace phi {
33

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

template <typename Func>
struct KernelArgsParseFunctor;

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

  static void Parse(const KernelKey& default_key, KernelArgsDef* args_def) {
    // TODO(chenweihang): The fluid Tensor's default layout is NCHW,
    // it is not same as kernel's layout, we should fix this error on
    // fluid Tensor
53 54
    auto default_tensor_layout = phi::DataLayout::NCHW;
    if (default_key.layout() != phi::DataLayout::ANY) {
55 56 57 58 59 60 61
      default_tensor_layout = default_key.layout();
    }
    auto args_type = ParseArgType(Indices{});
    for (auto arg_type : args_type) {
      if (arg_type == std::type_index(typeid(const CPUContext&))
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
          ||
62
          arg_type == std::type_index(typeid(const GPUContext&))) {
63 64 65
#elif defined(PADDLE_WITH_XPU)
          ||
          arg_type == std::type_index(typeid(const XPUContext&))) {
66 67 68
#elif defined(PADDLE_WITH_CUSTOM_DEVICE)
          ||
          arg_type == std::type_index(typeid(const CustomContext&))) {
69 70 71 72 73
#else
              ) {
#endif
        // do nothing, skip context arg now
      } else if (arg_type == std::type_index(typeid(const DenseTensor&))) {
H
hong 已提交
74 75 76 77
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
78 79
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const DenseTensor&>))) {
H
hong 已提交
80 81 82 83
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
84 85
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<DenseTensor>&))) {
H
hong 已提交
86 87 88 89
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
90
      } else if (arg_type == std::type_index(typeid(const SelectedRows&))) {
H
hong 已提交
91 92 93 94
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
95
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
H
hong 已提交
96 97 98 99
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
100 101
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
H
hong 已提交
102 103 104 105
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
106
      } else if (arg_type == std::type_index(typeid(SelectedRows*))) {
H
hong 已提交
107 108 109 110
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      } 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>))...};
  }
};

128
// NOTE: used for making a difference between inner or outer registration.
129
enum class RegType : uint8_t {
130 131
  INNER = 0,
  OUTER,
132 133
};

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

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

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

/**
 * 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
 */
#define PT_NARGS(...) _PT_NARGS((__VA_ARGS__, _PT_RESQ_N()))
#define _PT_NARGS(...) _PT_ARG_N(__VA_ARGS__)
227 228 229
#define _PT_ARG_N_EXPAND(                                                     \
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
230
#define _PT_ARG_N(args) _PT_ARG_N_EXPAND args
231
#define _PT_RESQ_N() 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
232

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

251
#define _PD_REGISTER_KERNEL(                                               \
252 253
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)  \
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
254 255 256
      PD_REGISTER_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_KERNEL must be called in global namespace.");           \
  PT_EXPAND(_PD_REGISTER_2TA_KERNEL(reg_type,                              \
257 258 259 260 261 262
                                    kernel_name,                           \
                                    backend,                               \
                                    context,                               \
                                    layout,                                \
                                    meta_kernel_fn,                        \
                                    __VA_ARGS__))
263

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

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
#define PT_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, ...) \
  _PT_KERNEL_INSTANTIATION(                                            \
      PT_NARGS(__VA_ARGS__), meta_kernel_fn, backend, context, __VA_ARGS__)

#define _PT_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, context, ...) \
  PT_CONCATENATE(_PT_KERNEL_INSTANTIATION_, N)                             \
  (meta_kernel_fn, backend, context, __VA_ARGS__)

#define _PT_KERNEL_INSTANTIATION_1(              \
    meta_kernel_fn, backend, context, cpp_dtype) \
  template decltype(                             \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>
#define _PT_KERNEL_INSTANTIATION_2(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_1(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_3(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_2(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_4(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_3(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_5(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_4(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_6(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_5(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_7(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_6(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_8(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_7(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_9(                                           \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_8(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_10(                                          \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_9(                                       \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_11(                                          \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_10(                                      \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_12(                                          \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_11(                                      \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_13(                                          \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_12(                                      \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_14(                                          \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_13(                                      \
      meta_kernel_fn, backend, context, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_15(                                          \
    meta_kernel_fn, backend, context, cpp_dtype, ...)                         \
  template decltype(                                                          \
      meta_kernel_fn<cpp_dtype, context>) meta_kernel_fn<cpp_dtype, context>; \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_14(                                      \
      meta_kernel_fn, backend, context, __VA_ARGS__))

#define PT_KERNEL_REGISTRAR_INIT(reg_type,                   \
                                 kernel_name,                \
                                 backend,                    \
                                 context,                    \
                                 layout,                     \
                                 args_def_fn,                \
                                 meta_kernel_fn,             \
                                 ...)                        \
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT(PT_NARGS(__VA_ARGS__), \
                                      reg_type,              \
                                      kernel_name,           \
                                      backend,               \
                                      context,               \
                                      layout,                \
                                      args_def_fn,           \
                                      meta_kernel_fn,        \
424
                                      __VA_ARGS__))
425 426 427 428 429

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
430
#define _PT_KERNEL_REGISTRAR_INIT(N,                       \
431
                                  reg_type,                \
432 433
                                  kernel_name,             \
                                  backend,                 \
434
                                  context,                 \
435 436 437 438 439
                                  layout,                  \
                                  args_def_fn,             \
                                  meta_kernel_fn,          \
                                  ...)                     \
  PT_EXPAND(PT_CONCATENATE(_PT_KERNEL_REGISTRAR_INIT_, N) ( \
440
    reg_type,                                              \
441 442
    kernel_name,                                           \
    backend,                                               \
443
    context,                                               \
444 445 446 447 448
    layout,                                                \
    PT_ID,                                                 \
    args_def_fn,                                           \
    meta_kernel_fn,                                        \
    __VA_ARGS__))
449 450 451

// clang-format on

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

914
#define PD_REGISTER_GENERAL_KERNEL(                 \
915
    kernel_name, backend, layout, kernel_fn, dtype) \
916
  _PD_REGISTER_GENERAL_KERNEL(                      \
917
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn, dtype)
918

919
#define _PD_REGISTER_GENERAL_KERNEL(                                         \
920
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)                \
921
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
922 923 924
      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(                                              \
925
      reg_type, kernel_name, backend, layout, kernel_fn, dtype)
926 927

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

970
/** PD_DECLARE_KERNEL
971 972 973 974
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
975
#define PD_DECLARE_KERNEL(kernel_name, backend, layout)                   \
976
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                      \
977 978
      PD_DECLARE_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_DECLARE_KERNEL must be called in global namespace.");           \
979 980 981 982
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
983

984
/** PD_REGISTER_BUILTIN_KERNEL
985 986 987 988
 *
 * Used to register kernels for built-in backends.
 * Support CPU GPU XPU.
 */
989 990
#define PD_REGISTER_BUILTIN_KERNEL(                    \
    kernel_name, backend, layout, meta_kernel_fn, ...) \
991
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
992 993 994 995 996
                      kernel_name,                     \
                      backend,                         \
                      ::phi::backend##Context,         \
                      layout,                          \
                      meta_kernel_fn,                  \
997 998
                      __VA_ARGS__)

999
/** PD_REGISTER_PLUGIN_KERNEL
1000 1001 1002 1003
 *
 * Used to register kernels for plug-in backends.
 * Support user-defined backend such as 'Ascend910'.
 */
1004
#define PD_REGISTER_PLUGIN_KERNEL(                     \
1005
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1006
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1007 1008 1009 1010 1011 1012 1013
                      kernel_name,                     \
                      backend,                         \
                      ::phi::CustomContext,            \
                      layout,                          \
                      meta_kernel_fn,                  \
                      __VA_ARGS__)

1014
}  // namespace phi