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

#pragma once

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

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

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

32
namespace phi {
33

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

template <typename Func>
struct KernelArgsParseFunctor;

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

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

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

223
// NOTE: used for making a difference between inner or outer registration.
224
enum class RegType : uint8_t {
225 226
  INNER = 0,
  OUTER,
227 228
};

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

253 254 255
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
256 257 258
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
259 260
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
261 262 263
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
264 265 266 267 268 269 270
      // 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 已提交
271 272 273 274 275 276 277 278
      // 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;
      }
279 280 281
      ConstructKernel(reg_type,
                      kernel_name_cstr,
                      backend_cstr,
282 283 284 285
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
286 287
                      kernel_fn,
                      variadic_kernel_fn);
288 289 290 291
    }
  }

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

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

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

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

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

415 416 417
#define PD_KERNEL_INSTANTIATION(meta_kernel_fn, backend, context, ...) \
  _PD_KERNEL_INSTANTIATION(                                            \
      PD_NARGS(__VA_ARGS__), meta_kernel_fn, backend, context, __VA_ARGS__)
418

419 420
#define _PD_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, context, ...) \
  PD_CONCATENATE(_PD_KERNEL_INSTANTIATION_, N)                             \
421 422
  (meta_kernel_fn, backend, context, __VA_ARGS__)

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

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

// clang-format off

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

// clang-format on

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

1018
#define PD_REGISTER_GENERAL_KERNEL(                 \
1019
    kernel_name, backend, layout, kernel_fn, dtype) \
1020
  _PD_REGISTER_GENERAL_KERNEL(                      \
1021
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn, dtype)
1022

1023
#define _PD_REGISTER_GENERAL_KERNEL(                                         \
1024
    reg_type, kernel_name, backend, layout, kernel_fn, dtype)                \
1025
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
1026 1027 1028
      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(                                              \
1029
      reg_type, kernel_name, backend, layout, kernel_fn, dtype)
1030 1031

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

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

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

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

1118
}  // namespace phi