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

#pragma once

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

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

31
namespace phi {
32

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

template <typename Func>
struct KernelArgsParseFunctor;

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

  static void Parse(const KernelKey& default_key, KernelArgsDef* args_def) {
    // TODO(chenweihang): The fluid Tensor's default layout is NCHW,
    // it is not same as kernel's layout, we should fix this error on
    // fluid Tensor
52 53
    auto default_tensor_layout = phi::DataLayout::NCHW;
    if (default_key.layout() != phi::DataLayout::ANY) {
54 55 56 57 58 59
      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)
60
          || arg_type == std::type_index(typeid(const GPUContext&))) {
61
#elif defined(PADDLE_WITH_XPU)
62
          || arg_type == std::type_index(typeid(const XPUContext&))) {
63
#elif defined(PADDLE_WITH_CUSTOM_DEVICE)
64
          || arg_type == std::type_index(typeid(const CustomContext&))) {
65
#else
66
      ) {
67 68 69
#endif
        // do nothing, skip context arg now
      } else if (arg_type == std::type_index(typeid(const DenseTensor&))) {
H
hong 已提交
70 71 72 73
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
74
      } else if (arg_type == std::type_index(typeid(
75
                                 const paddle::optional<DenseTensor>&))) {
H
hong 已提交
76 77 78 79
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
80 81 82
      } else if (arg_type ==
                 std::type_index(typeid(const paddle::optional<
                                        std::vector<const DenseTensor*>>&))) {
83 84 85 86
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
H
hong 已提交
87
      } else if (arg_type == std::type_index(typeid(
88
                                 const paddle::optional<SelectedRows>&))) {
H
hong 已提交
89 90 91 92
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
93 94
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const DenseTensor*>&))) {
H
hong 已提交
95 96 97 98
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
99
      } else if (arg_type == std::type_index(typeid(const SelectedRows&))) {
H
hong 已提交
100 101 102 103
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
104 105 106 107 108
      } else if (arg_type == std::type_index(typeid(const StringTensor&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
Z
zhangkaihuo 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      } else if (arg_type == std::type_index(typeid(const SparseCooTensor&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const SparseCooTensor&>))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(const SparseCsrTensor&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const SparseCsrTensor&>))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
131
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
H
hong 已提交
132 133 134 135
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
136 137
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
H
hong 已提交
138 139 140 141
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
142
      } else if (arg_type == std::type_index(typeid(SelectedRows*))) {
H
hong 已提交
143 144 145 146
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
Z
zhangkaihuo 已提交
147 148 149 150 151 152 153 154 155 156
      } else if (arg_type == std::type_index(typeid(SparseCooTensor*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
      } else if (arg_type == std::type_index(typeid(SparseCsrTensor*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
157 158 159 160 161
      } else if (arg_type == std::type_index(typeid(StringTensor*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
      } else if (arg_type == std::type_index(typeid(bool))) {
        args_def->AppendAttribute(AttributeType::BOOL);
      } else if (arg_type == std::type_index(typeid(int))) {
        args_def->AppendAttribute(AttributeType::INT32);
      } else if (arg_type == std::type_index(typeid(int64_t))) {
        args_def->AppendAttribute(AttributeType::INT64);
      } else if (arg_type == std::type_index(typeid(float))) {
        args_def->AppendAttribute(AttributeType::FLOAT32);
      } else if (arg_type == std::type_index(typeid(double))) {
        args_def->AppendAttribute(AttributeType::FLOAT64);
      } else if (arg_type == std::type_index(typeid(std::string))) {
        args_def->AppendAttribute(AttributeType::STRING);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<bool>&))) {
        args_def->AppendAttribute(AttributeType::BOOLS);
      } else if (arg_type == std::type_index(typeid(const std::vector<int>&))) {
        args_def->AppendAttribute(AttributeType::INT32S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<int64_t>&))) {
        args_def->AppendAttribute(AttributeType::INT64S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<float>&))) {
        args_def->AppendAttribute(AttributeType::FLOAT32S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<double>&))) {
        args_def->AppendAttribute(AttributeType::FLOAT64S);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<std::string>&))) {
        args_def->AppendAttribute(AttributeType::STRINGS);
      } else if (arg_type == std::type_index(typeid(const Scalar&))) {
        args_def->AppendAttribute(AttributeType::SCALAR);
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<Scalar>&))) {
        args_def->AppendAttribute(AttributeType::SCALARS);
      } else if (arg_type == std::type_index(typeid(const IntArray&))) {
        args_def->AppendAttribute(AttributeType::INT_ARRAY);
      } else if (arg_type == std::type_index(typeid(DataType))) {
        args_def->AppendAttribute(AttributeType::DATA_TYPE);
      } else if (arg_type == std::type_index(typeid(DataLayout))) {
        args_def->AppendAttribute(AttributeType::DATA_LAYOUT);
      } else if (arg_type == std::type_index(typeid(Place))) {
        args_def->AppendAttribute(AttributeType::PLACE);
204
      } else {
205 206
        PADDLE_THROW(phi::errors::Unavailable(
            "Unsupported kernel argument type `%s`.", arg_type.name()));
207 208 209 210 211 212 213 214 215 216 217 218
      }
    }
  }

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

219
// NOTE: used for making a difference between inner or outer registration.
220
enum class RegType : uint8_t {
221 222
  INNER = 0,
  OUTER,
223 224
};

225 226
// TODO(chenweihang): Polish the kernel selection logic, support the selection
// of ALL_DTYPE kernel, and simplify the constructor
227 228
struct KernelRegistrar {
 public:
229 230 231
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
232 233 234 235
                  DataLayout layout,
                  DataType dtype,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
236 237
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
238 239 240
    ConstructKernel(reg_type,
                    kernel_name_cstr,
                    backend_cstr,
241 242 243 244
                    layout,
                    dtype,
                    args_parse_fn,
                    args_def_fn,
245 246
                    kernel_fn,
                    variadic_kernel_fn);
247 248
  }

249 250 251
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
252 253 254
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
255 256
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
257 258 259
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
260 261 262 263 264 265 266
      // NOTE(zhiqiu): why skip these types, because fluid kernel has no kernel
      // of these type.
      if (dtype == static_cast<size_t>(DataType::UINT32) ||
          dtype == static_cast<size_t>(DataType::UINT64) ||
          dtype == static_cast<size_t>(DataType::UINT16)) {
        continue;
      }
J
Jack Zhou 已提交
267 268 269 270 271 272 273 274
      // NOTE(zhoushunjie): Only the strings kernels can support pstring dtype
      constexpr char strings_kernels_prefix[] = "strings_";
      if (dtype == static_cast<size_t>(DataType::PSTRING) &&
          strncmp(kernel_name_cstr,
                  strings_kernels_prefix,
                  strlen(strings_kernels_prefix))) {
        continue;
      }
275 276 277
      ConstructKernel(reg_type,
                      kernel_name_cstr,
                      backend_cstr,
278 279 280 281
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
282 283
                      kernel_fn,
                      variadic_kernel_fn);
284 285 286 287
    }
  }

 private:
288 289 290
  void ConstructKernel(RegType reg_type,
                       const char* kernel_name_cstr,
                       const char* backend_cstr,
291 292 293 294
                       DataLayout layout,
                       DataType dtype,
                       KernelArgsParseFn args_parse_fn,
                       KernelArgsDefFn args_def_fn,
295 296
                       KernelFn kernel_fn,
                       void* variadic_kernel_fn) {
Y
YuanRisheng 已提交
297
    std::string kernel_name(kernel_name_cstr);
298 299
    KernelKey kernel_key(
        paddle::experimental::StringToBackend(backend_cstr), layout, dtype);
300
    Kernel kernel(kernel_fn, variadic_kernel_fn);
301
    args_parse_fn(kernel_key, kernel.mutable_args_def());
302
    args_def_fn(kernel_key, &kernel);
303
    if (reg_type == RegType::INNER) {
304 305
      KernelFactory::Instance().kernels()[kernel_name][kernel_key] = kernel;
    } else {
306 307
      CustomKernelMap::Instance().RegisterCustomKernel(
          kernel_name, kernel_key, kernel);
308
    }
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  }
};

/**
 * 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
 */
325 326 327
#define PD_NARGS(...) _PD_NARGS((__VA_ARGS__, _PD_RESQ_N()))
#define _PD_NARGS(...) _PD_ARG_N(__VA_ARGS__)
#define _PD_ARG_N_EXPAND(                                                     \
328 329
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
330 331
#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
332

333
/** PD_REGISTER_KERNEL
334 335 336 337 338
 *
 * 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.
339
 *
340
 * Note: `2TA` means `2 template argument`
341
 */
342
#define PD_REGISTER_KERNEL(kernel_name, backend, layout, meta_kernel_fn, ...) \
343
  _PD_REGISTER_KERNEL(::phi::RegType::INNER,                                  \
344 345 346 347 348 349 350
                      kernel_name,                                            \
                      backend,                                                \
                      ::phi::backend##Context,                                \
                      layout,                                                 \
                      meta_kernel_fn,                                         \
                      __VA_ARGS__)

351
#define _PD_REGISTER_KERNEL(                                               \
352
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)  \
353
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
354 355
      PD_REGISTER_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_KERNEL must be called in global namespace.");           \
356
  PD_EXPAND(_PD_REGISTER_2TA_KERNEL(reg_type,                              \
357 358 359 360 361 362
                                    kernel_name,                           \
                                    backend,                               \
                                    context,                               \
                                    layout,                                \
                                    meta_kernel_fn,                        \
                                    __VA_ARGS__))
363

364
#ifndef _WIN32
365
#define _PD_REGISTER_2TA_KERNEL(                                            \
366
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
367 368
  PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, __VA_ARGS__);   \
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
369
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
370
  PD_KERNEL_REGISTRAR_INIT(                                                 \
371
      reg_type,                                                             \
372 373
      kernel_name,                                                          \
      backend,                                                              \
374
      context,                                                              \
375
      layout,                                                               \
376
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
377 378
      meta_kernel_fn,                                                       \
      __VA_ARGS__);                                                         \
379
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
380
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
381 382 383 384 385 386 387 388 389 390 391
#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
 *
392
 * And msvc can work without template instantiation
393
 */
394
#define _PD_REGISTER_2TA_KERNEL(                                            \
395
    reg_type, kernel_name, backend, context, layout, meta_kernel_fn, ...)   \
396
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
397
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
398
  PD_EXPAND(PD_KERNEL_REGISTRAR_INIT(                                       \
399
      reg_type,                                                             \
400 401
      kernel_name,                                                          \
      backend,                                                              \
402
      context,                                                              \
403
      layout,                                                               \
404
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
405
      meta_kernel_fn,                                                       \
406
      __VA_ARGS__));                                                        \
407
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
408
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
409 410
#endif

411 412 413
#define PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, ...) \
  _PD_KERNEL_INSTANTIATION(                                            \
      PD_NARGS(__VA_ARGS__), meta_kernel_fn, backend, context, __VA_ARGS__)
414

415 416
#define _PD_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, context, ...) \
  PD_CONCATENATE(_PD_KERNEL_INSTANTIATION_, N)                             \
417 418
  (meta_kernel_fn, backend, context, __VA_ARGS__)

419 420 421 422 423 424 425 426 427
#define _PD_KERNEL_INSTANTIATION_1(                     \
    meta_kernel_fn, backend, context, cpp_dtype)        \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>
#define _PD_KERNEL_INSTANTIATION_2(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_1(                 \
428
      meta_kernel_fn, backend, context, __VA_ARGS__))
429 430 431 432 433
#define _PD_KERNEL_INSTANTIATION_3(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_2(                 \
434
      meta_kernel_fn, backend, context, __VA_ARGS__))
435 436 437 438 439
#define _PD_KERNEL_INSTANTIATION_4(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_3(                 \
440
      meta_kernel_fn, backend, context, __VA_ARGS__))
441 442 443 444 445
#define _PD_KERNEL_INSTANTIATION_5(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_4(                 \
446
      meta_kernel_fn, backend, context, __VA_ARGS__))
447 448 449 450 451
#define _PD_KERNEL_INSTANTIATION_6(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_5(                 \
452
      meta_kernel_fn, backend, context, __VA_ARGS__))
453 454 455 456 457
#define _PD_KERNEL_INSTANTIATION_7(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_6(                 \
458
      meta_kernel_fn, backend, context, __VA_ARGS__))
459 460 461 462 463
#define _PD_KERNEL_INSTANTIATION_8(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_7(                 \
464
      meta_kernel_fn, backend, context, __VA_ARGS__))
465 466 467 468 469
#define _PD_KERNEL_INSTANTIATION_9(                     \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_8(                 \
470
      meta_kernel_fn, backend, context, __VA_ARGS__))
471 472 473 474 475
#define _PD_KERNEL_INSTANTIATION_10(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_9(                 \
476
      meta_kernel_fn, backend, context, __VA_ARGS__))
477 478 479 480 481
#define _PD_KERNEL_INSTANTIATION_11(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_10(                \
482
      meta_kernel_fn, backend, context, __VA_ARGS__))
483 484 485 486 487
#define _PD_KERNEL_INSTANTIATION_12(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_11(                \
488
      meta_kernel_fn, backend, context, __VA_ARGS__))
489 490 491 492 493
#define _PD_KERNEL_INSTANTIATION_13(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_12(                \
494
      meta_kernel_fn, backend, context, __VA_ARGS__))
495 496 497 498 499
#define _PD_KERNEL_INSTANTIATION_14(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_13(                \
500
      meta_kernel_fn, backend, context, __VA_ARGS__))
501 502 503 504 505
#define _PD_KERNEL_INSTANTIATION_15(                    \
    meta_kernel_fn, backend, context, cpp_dtype, ...)   \
  template decltype(meta_kernel_fn<cpp_dtype, context>) \
      meta_kernel_fn<cpp_dtype, context>;               \
  PD_EXPAND(_PD_KERNEL_INSTANTIATION_14(                \
506 507
      meta_kernel_fn, backend, context, __VA_ARGS__))

508
#define PD_KERNEL_REGISTRAR_INIT(reg_type,                   \
509 510 511 512 513 514 515
                                 kernel_name,                \
                                 backend,                    \
                                 context,                    \
                                 layout,                     \
                                 args_def_fn,                \
                                 meta_kernel_fn,             \
                                 ...)                        \
516
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT(PD_NARGS(__VA_ARGS__), \
517 518 519 520 521 522 523
                                      reg_type,              \
                                      kernel_name,           \
                                      backend,               \
                                      context,               \
                                      layout,                \
                                      args_def_fn,           \
                                      meta_kernel_fn,        \
524
                                      __VA_ARGS__))
525 526 527 528 529

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
530
#define _PD_KERNEL_REGISTRAR_INIT(N,                       \
531
                                  reg_type,                \
532 533
                                  kernel_name,             \
                                  backend,                 \
534
                                  context,                 \
535 536 537 538
                                  layout,                  \
                                  args_def_fn,             \
                                  meta_kernel_fn,          \
                                  ...)                     \
539
  PD_EXPAND(PD_CONCATENATE(_PD_KERNEL_REGISTRAR_INIT_, N) ( \
540
    reg_type,                                              \
541 542
    kernel_name,                                           \
    backend,                                               \
543
    context,                                               \
544
    layout,                                                \
545
    PD_ID,                                                 \
546 547 548
    args_def_fn,                                           \
    meta_kernel_fn,                                        \
    __VA_ARGS__))
549 550 551

// clang-format on

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

1014
#define PD_REGISTER_GENERAL_KERNEL(                 \
1015
    kernel_name, backend, layout, kernel_fn, dtype) \
1016
  _PD_REGISTER_GENERAL_KERNEL(                      \
1017
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn, dtype)
1018

1019
#define _PD_REGISTER_GENERAL_KERNEL(                                         \
1020
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)                \
1021
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
1022 1023 1024
      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(                                              \
1025
      reg_type, kernel_name, backend, layout, kernel_fn, dtype)
1026 1027

#ifndef _WIN32
1028
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
1029
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
1030
  template decltype(kernel_fn) kernel_fn;                                   \
1031
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
1032 1033
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
1034
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
1035
          reg_type,                                                         \
1036
          #kernel_name,                                                     \
1037
          #backend,                                                         \
1038
          DATALAYOUT(layout),                                               \
1039
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1040 1041 1042
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1043 1044 1045
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1046
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1047
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1048
#else
1049
#define __PD_REGISTER_GENERAL_KERNEL(                                       \
1050
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)               \
1051
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
1052 1053
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
1054
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
1055
          reg_type,                                                         \
1056
          #kernel_name,                                                     \
1057
          #backend,                                                         \
1058
          DATALAYOUT(layout),                                               \
1059
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1060 1061 1062
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1063 1064 1065
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1066
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1067
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1068 1069
#endif

1070
/** PD_DECLARE_KERNEL
1071 1072 1073 1074
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
1075
#define PD_DECLARE_KERNEL(kernel_name, backend, layout)                   \
1076
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                      \
1077 1078
      PD_DECLARE_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_DECLARE_KERNEL must be called in global namespace.");           \
1079 1080 1081 1082
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
1083

1084
/** PD_REGISTER_BUILTIN_KERNEL
1085 1086 1087 1088
 *
 * Used to register kernels for built-in backends.
 * Support CPU GPU XPU.
 */
1089 1090
#define PD_REGISTER_BUILTIN_KERNEL(                    \
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1091
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1092 1093 1094 1095 1096
                      kernel_name,                     \
                      backend,                         \
                      ::phi::backend##Context,         \
                      layout,                          \
                      meta_kernel_fn,                  \
1097 1098
                      __VA_ARGS__)

1099
/** PD_REGISTER_PLUGIN_KERNEL
1100 1101 1102 1103
 *
 * Used to register kernels for plug-in backends.
 * Support user-defined backend such as 'Ascend910'.
 */
1104
#define PD_REGISTER_PLUGIN_KERNEL(                     \
1105
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1106
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1107 1108 1109 1110 1111 1112 1113
                      kernel_name,                     \
                      backend,                         \
                      ::phi::CustomContext,            \
                      layout,                          \
                      meta_kernel_fn,                  \
                      __VA_ARGS__)

1114
}  // namespace phi