kernel_registry.h 95.5 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
#include "paddle/phi/core/extended_tensor.h"
27 28 29 30
#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"
31

32
namespace phi {
33

34
#define BACKEND(arg__) phi::Backend::arg__
35
#define DATA_LAYOUT(arg__) phi::DataLayout::arg__
36
#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
      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&))
60 61 62
#if defined(PADDLE_WITH_MKLDNN)
          || arg_type == std::type_index(typeid(const OneDNNContext&))
#endif
63
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
64
          || arg_type == std::type_index(typeid(const GPUContext&))
65
#elif defined(PADDLE_WITH_XPU) && !defined(PADDLE_WITH_XPU_KP)
66
          || arg_type == std::type_index(typeid(const XPUContext&))
67
#elif defined(PADDLE_WITH_XPU) && defined(PADDLE_WITH_XPU_KP)
68 69 70
          || arg_type == std::type_index(typeid(const KPSContext&))
#endif
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
71
          || arg_type == std::type_index(typeid(const CustomContext&))) {
72
#else
73
      ) {
74 75 76
#endif
        // do nothing, skip context arg now
      } else if (arg_type == std::type_index(typeid(const DenseTensor&))) {
H
hong 已提交
77 78 79 80
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
81
      } else if (arg_type == std::type_index(typeid(
82
                                 const paddle::optional<DenseTensor>&))) {
H
hong 已提交
83 84 85 86
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
87 88 89
      } else if (arg_type ==
                 std::type_index(typeid(const paddle::optional<
                                        std::vector<const DenseTensor*>>&))) {
90 91 92 93
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
H
hong 已提交
94
      } else if (arg_type == std::type_index(typeid(
95
                                 const paddle::optional<SelectedRows>&))) {
H
hong 已提交
96 97 98 99
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
100 101
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const DenseTensor*>&))) {
Y
YuanRisheng 已提交
102 103 104 105
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
106 107 108 109 110 111
      } else if (arg_type ==
                 std::type_index(typeid(const phi::ExtendedTensor&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
112 113 114 115 116 117
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const ExtendedTensor*>&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
Y
YuanRisheng 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const SelectedRows*>&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const TensorBase*>&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
      } else if (arg_type == std::type_index(typeid(
                                 const std::vector<const TensorArray*>&))) {
H
hong 已提交
132 133 134 135
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
136
      } else if (arg_type == std::type_index(typeid(const SelectedRows&))) {
H
hong 已提交
137 138 139 140
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
141 142 143 144 145
      } 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 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      } 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);
168 169 170 171 172
      } else if (arg_type == std::type_index(typeid(const TensorArray&))) {
        args_def->AppendInput(default_key.backend(),
                              default_tensor_layout,
                              default_key.dtype(),
                              arg_type);
173
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
H
hong 已提交
174 175 176 177
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
178 179
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
H
hong 已提交
180 181 182 183
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
184
      } else if (arg_type == std::type_index(typeid(SelectedRows*))) {
H
hong 已提交
185 186 187 188
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
189 190 191 192 193
      } else if (arg_type == std::type_index(typeid(TensorArray*))) {
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
Z
zhangkaihuo 已提交
194 195 196 197 198 199 200 201 202 203
      } 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);
204
      } else if (arg_type == std::type_index(typeid(StringTensor*))) {
205 206 207 208 209
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
      } else if (arg_type == std::type_index(typeid(ExtendedTensor*))) {
210 211 212 213
        args_def->AppendOutput(default_key.backend(),
                               default_tensor_layout,
                               default_key.dtype(),
                               arg_type);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
      } 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);
256
      } else {
257 258
        PADDLE_THROW(phi::errors::Unavailable(
            "Unsupported kernel argument type `%s`.", arg_type.name()));
259 260 261 262 263 264 265 266 267 268 269 270
      }
    }
  }

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

271
// NOTE: used for making a difference between inner or outer registration.
272
enum class RegType : uint8_t {
273 274
  INNER = 0,
  OUTER,
275 276
};

277 278
// TODO(chenweihang): Polish the kernel selection logic, support the selection
// of ALL_DTYPE kernel, and simplify the constructor
279 280
struct KernelRegistrar {
 public:
281 282 283
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
284 285 286 287
                  DataLayout layout,
                  DataType dtype,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
288 289
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
290 291 292
    ConstructKernel(reg_type,
                    kernel_name_cstr,
                    backend_cstr,
293 294 295 296
                    layout,
                    dtype,
                    args_parse_fn,
                    args_def_fn,
297 298
                    kernel_fn,
                    variadic_kernel_fn);
299 300
  }

301 302 303
  KernelRegistrar(RegType reg_type,
                  const char* kernel_name_cstr,
                  const char* backend_cstr,
304 305 306
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
307 308
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
309 310 311
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
312 313 314 315 316 317 318
      // 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 已提交
319 320 321 322 323 324 325 326
      // 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;
      }
327 328 329
      ConstructKernel(reg_type,
                      kernel_name_cstr,
                      backend_cstr,
330 331 332 333
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
334 335
                      kernel_fn,
                      variadic_kernel_fn);
336 337 338 339
    }
  }

 private:
340 341 342
  void ConstructKernel(RegType reg_type,
                       const char* kernel_name_cstr,
                       const char* backend_cstr,
343 344 345 346
                       DataLayout layout,
                       DataType dtype,
                       KernelArgsParseFn args_parse_fn,
                       KernelArgsDefFn args_def_fn,
347 348
                       KernelFn kernel_fn,
                       void* variadic_kernel_fn) {
Y
YuanRisheng 已提交
349
    std::string kernel_name(kernel_name_cstr);
350 351
    KernelKey kernel_key(
        paddle::experimental::StringToBackend(backend_cstr), layout, dtype);
352
    Kernel kernel(kernel_fn, variadic_kernel_fn);
353 354 355
    if (kernel.GetKernelRegisteredType() == KernelRegisteredType::FUNCTION) {
      args_parse_fn(kernel_key, kernel.mutable_args_def());
    }
356
    args_def_fn(kernel_key, &kernel);
357
    if (reg_type == RegType::INNER) {
358 359
      KernelFactory::Instance().kernels()[kernel_name][kernel_key] = kernel;
    } else {
360 361
      CustomKernelMap::Instance().RegisterCustomKernel(
          kernel_name, kernel_key, kernel);
362
    }
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  }
};

/**
 * 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
 */
379 380 381
#define PD_NARGS(...) _PD_NARGS((__VA_ARGS__, _PD_RESQ_N()))
#define _PD_NARGS(...) _PD_ARG_N(__VA_ARGS__)
#define _PD_ARG_N_EXPAND(                                                     \
382 383
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
384 385
#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
386

387 388 389 390 391 392 393 394 395 396
// The macro for passing KernelArgsParseFunctor's function
#define ARG_PARSE_FUNCTOR(meta_kernel_fn, cpp_dtype, context) \
  ::phi::KernelArgsParseFunctor<                              \
      decltype(&meta_kernel_fn<cpp_dtype, context>)>::Parse

// The macro for instantiating function kernel
#define FUNCTION_KERNEL_INSTANTIATION(meta_kernel_fn, cpp_dtype, context) \
  template decltype(meta_kernel_fn<cpp_dtype, context>)                   \
      meta_kernel_fn<cpp_dtype, context>;

397
/** PD_REGISTER_KERNEL
398 399 400 401 402
 *
 * 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.
403
 *
404
 * Note: `2TA` means `2 template argument`
405
 */
406
#define PD_REGISTER_KERNEL(kernel_name, backend, layout, meta_kernel_fn, ...) \
407
  _PD_REGISTER_KERNEL(::phi::RegType::INNER,                                  \
408 409 410 411 412
                      kernel_name,                                            \
                      backend,                                                \
                      ::phi::backend##Context,                                \
                      layout,                                                 \
                      meta_kernel_fn,                                         \
413 414 415 416
                      FUNCTION_KERNEL_INSTANTIATION,                          \
                      ARG_PARSE_FUNCTOR,                                      \
                      PHI_KERNEL,                                             \
                      PHI_VARIADIC_KERNEL,                                    \
417 418
                      __VA_ARGS__)

419 420 421 422 423 424 425 426 427 428 429
#define _PD_REGISTER_KERNEL(reg_type,                                      \
                            kernel_name,                                   \
                            backend,                                       \
                            context,                                       \
                            layout,                                        \
                            meta_kernel_fn,                                \
                            kernel_instantiation_macro,                    \
                            arg_parse_functor_macro,                       \
                            kernel_unfold_macro,                           \
                            variadic_kernel_unfold_marco,                  \
                            ...)                                           \
430
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
431 432
      PD_REGISTER_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_KERNEL must be called in global namespace.");           \
433
  PD_EXPAND(_PD_REGISTER_2TA_KERNEL(reg_type,                              \
434 435 436 437 438
                                    kernel_name,                           \
                                    backend,                               \
                                    context,                               \
                                    layout,                                \
                                    meta_kernel_fn,                        \
439 440 441 442
                                    kernel_instantiation_macro,            \
                                    arg_parse_functor_macro,               \
                                    kernel_unfold_macro,                   \
                                    variadic_kernel_unfold_marco,          \
443
                                    __VA_ARGS__))
444

445
#ifndef _WIN32
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
#define _PD_REGISTER_2TA_KERNEL(reg_type,                                   \
                                kernel_name,                                \
                                backend,                                    \
                                context,                                    \
                                layout,                                     \
                                meta_kernel_fn,                             \
                                kernel_instantiation_macro,                 \
                                arg_parse_functor_macro,                    \
                                kernel_unfold_macro,                        \
                                variadic_kernel_unfold_marco,               \
                                ...)                                        \
  PD_KERNEL_INSTANTIATION(meta_kernel_fn,                                   \
                          backend,                                          \
                          context,                                          \
                          kernel_instantiation_macro,                       \
                          __VA_ARGS__);                                     \
462
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
463
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
464
  PD_KERNEL_REGISTRAR_INIT(                                                 \
465
      reg_type,                                                             \
466 467
      kernel_name,                                                          \
      backend,                                                              \
468
      context,                                                              \
469
      layout,                                                               \
470
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
471
      meta_kernel_fn,                                                       \
472 473 474
      arg_parse_functor_macro,                                              \
      kernel_unfold_macro,                                                  \
      variadic_kernel_unfold_marco,                                         \
475
      __VA_ARGS__);                                                         \
476
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
477
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
478 479 480 481 482 483 484 485 486 487 488
#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
 *
489
 * And msvc can work without template instantiation
490
 */
491 492 493 494 495 496 497 498 499 500 501
#define _PD_REGISTER_2TA_KERNEL(reg_type,                                   \
                                kernel_name,                                \
                                backend,                                    \
                                context,                                    \
                                layout,                                     \
                                meta_kernel_fn,                             \
                                kernel_instantiation_macro,                 \
                                arg_parse_functor_macro,                    \
                                kernel_unfold_macro,                        \
                                variadic_kernel_unfold_marco,               \
                                ...)                                        \
502
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
503
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
504
  PD_EXPAND(PD_KERNEL_REGISTRAR_INIT(                                       \
505
      reg_type,                                                             \
506 507
      kernel_name,                                                          \
      backend,                                                              \
508
      context,                                                              \
509
      layout,                                                               \
510
      &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
511
      meta_kernel_fn,                                                       \
512 513 514
      arg_parse_functor_macro,                                              \
      kernel_unfold_macro,                                                  \
      variadic_kernel_unfold_marco,                                         \
515
      __VA_ARGS__));                                                        \
516
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
517
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
518 519
#endif

520 521 522 523 524 525 526 527
#define PD_KERNEL_INSTANTIATION(                                       \
    meta_kernel_fn, backend, context, kernel_instantiation_macro, ...) \
  _PD_KERNEL_INSTANTIATION(PD_NARGS(__VA_ARGS__),                      \
                           meta_kernel_fn,                             \
                           backend,                                    \
                           context,                                    \
                           kernel_instantiation_macro,                 \
                           __VA_ARGS__)
528

529 530 531 532
#define _PD_KERNEL_INSTANTIATION(                                         \
    N, meta_kernel_fn, backend, context, kernel_instantiation_macro, ...) \
  PD_CONCATENATE(_PD_KERNEL_INSTANTIATION_, N)                            \
  (meta_kernel_fn, backend, context, kernel_instantiation_macro, __VA_ARGS__)
533

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
#define _PD_KERNEL_INSTANTIATION_1(                                          \
    meta_kernel_fn, backend, context, kernel_instantiation_macro, cpp_dtype) \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)
#define _PD_KERNEL_INSTANTIATION_2(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_1(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_3(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_2(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_4(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_3(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_5(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_4(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_6(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_5(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_7(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_6(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_8(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_7(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_9(meta_kernel_fn,                     \
                                   backend,                            \
                                   context,                            \
                                   kernel_instantiation_macro,         \
                                   cpp_dtype,                          \
                                   ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_8(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_10(meta_kernel_fn,                    \
                                    backend,                           \
                                    context,                           \
                                    kernel_instantiation_macro,        \
                                    cpp_dtype,                         \
                                    ...)                               \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)       \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_9(meta_kernel_fn,             \
                                           backend,                    \
                                           context,                    \
                                           kernel_instantiation_macro, \
                                           __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_11(meta_kernel_fn,                     \
                                    backend,                            \
                                    context,                            \
                                    kernel_instantiation_macro,         \
                                    cpp_dtype,                          \
                                    ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)        \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_10(meta_kernel_fn,             \
                                            backend,                    \
                                            context,                    \
                                            kernel_instantiation_macro, \
                                            __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_12(meta_kernel_fn,                     \
                                    backend,                            \
                                    context,                            \
                                    kernel_instantiation_macro,         \
                                    cpp_dtype,                          \
                                    ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)        \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_11(meta_kernel_fn,             \
                                            backend,                    \
                                            context,                    \
                                            kernel_instantiation_macro, \
                                            __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_13(meta_kernel_fn,                     \
                                    backend,                            \
                                    context,                            \
                                    kernel_instantiation_macro,         \
                                    cpp_dtype,                          \
                                    ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)        \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_12(meta_kernel_fn,             \
                                            backend,                    \
                                            context,                    \
                                            kernel_instantiation_macro, \
                                            __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_14(meta_kernel_fn,                     \
                                    backend,                            \
                                    context,                            \
                                    kernel_instantiation_macro,         \
                                    cpp_dtype,                          \
                                    ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)        \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_13(meta_kernel_fn,             \
                                            backend,                    \
                                            context,                    \
                                            kernel_instantiation_macro, \
                                            __VA_ARGS__))
#define _PD_KERNEL_INSTANTIATION_15(meta_kernel_fn,                     \
                                    backend,                            \
                                    context,                            \
                                    kernel_instantiation_macro,         \
                                    cpp_dtype,                          \
                                    ...)                                \
  kernel_instantiation_macro(meta_kernel_fn, cpp_dtype, context)        \
      PD_EXPAND(_PD_KERNEL_INSTANTIATION_14(meta_kernel_fn,             \
                                            backend,                    \
                                            context,                    \
                                            kernel_instantiation_macro, \
                                            __VA_ARGS__))
705

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
#define PD_KERNEL_REGISTRAR_INIT(reg_type,                          \
                                 kernel_name,                       \
                                 backend,                           \
                                 context,                           \
                                 layout,                            \
                                 args_def_fn,                       \
                                 meta_kernel_fn,                    \
                                 arg_parse_functor_macro,           \
                                 kernel_unfold_macro,               \
                                 variadic_kernel_unfold_marco,      \
                                 ...)                               \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT(PD_NARGS(__VA_ARGS__),        \
                                      reg_type,                     \
                                      kernel_name,                  \
                                      backend,                      \
                                      context,                      \
                                      layout,                       \
                                      args_def_fn,                  \
                                      meta_kernel_fn,               \
                                      arg_parse_functor_macro,      \
                                      kernel_unfold_macro,          \
                                      variadic_kernel_unfold_marco, \
728
                                      __VA_ARGS__))
729 730 731 732 733

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
734
#define _PD_KERNEL_REGISTRAR_INIT(N,                       \
735
                                  reg_type,                \
736 737
                                  kernel_name,             \
                                  backend,                 \
738
                                  context,                 \
739 740 741
                                  layout,                  \
                                  args_def_fn,             \
                                  meta_kernel_fn,          \
742 743 744
                                  arg_parse_functor_macro,       \
                                  kernel_unfold_macro,               \
                                  variadic_kernel_unfold_marco,      \
745
                                  ...)                     \
746
  PD_EXPAND(PD_CONCATENATE(_PD_KERNEL_REGISTRAR_INIT_, N) ( \
747
    reg_type,                                              \
748 749
    kernel_name,                                           \
    backend,                                               \
750
    context,                                               \
751
    layout,                                                \
752
    PD_ID,                                                 \
753 754
    args_def_fn,                                           \
    meta_kernel_fn,                                        \
755 756 757
    arg_parse_functor_macro,                                     \
    kernel_unfold_macro,                                             \
    variadic_kernel_unfold_marco,                                    \
758
    __VA_ARGS__))
759 760 761

// clang-format on

762
#define _PD_CREATE_REGISTRAR_OBJECT(reg_type,                                  \
763 764 765 766 767 768 769
                                    kernel_name,                               \
                                    backend,                                   \
                                    context,                                   \
                                    layout,                                    \
                                    registrar_id,                              \
                                    args_def_fn,                               \
                                    meta_kernel_fn,                            \
770 771 772
                                    arg_parse_functor_macro,                   \
                                    kernel_unfold_macro,                       \
                                    variadic_kernel_unfold_marco,              \
773 774 775 776 777 778
                                    cpp_dtype)                                 \
  static const ::phi::KernelRegistrar PD_CONCATENATE(                          \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      reg_type,                                                                \
      #kernel_name,                                                            \
      #backend,                                                                \
779
      DATA_LAYOUT(layout),                                                     \
780
      ::phi::CppTypeToDataType<cpp_dtype>::Type(),                             \
781
      arg_parse_functor_macro(meta_kernel_fn, cpp_dtype, context),             \
782
      args_def_fn,                                                             \
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
      kernel_unfold_macro(meta_kernel_fn<cpp_dtype, context>),                 \
      variadic_kernel_unfold_marco(meta_kernel_fn<cpp_dtype, context>));

#define _PD_KERNEL_REGISTRAR_INIT_1(reg_type,                     \
                                    kernel_name,                  \
                                    backend,                      \
                                    context,                      \
                                    layout,                       \
                                    registrar_id,                 \
                                    args_def_fn,                  \
                                    meta_kernel_fn,               \
                                    arg_parse_functor_macro,      \
                                    kernel_unfold_macro,          \
                                    variadic_kernel_unfold_marco, \
                                    cpp_dtype)                    \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                           \
                              kernel_name,                        \
                              backend,                            \
                              context,                            \
                              layout,                             \
                              registrar_id,                       \
                              args_def_fn,                        \
                              meta_kernel_fn,                     \
                              arg_parse_functor_macro,            \
                              kernel_unfold_macro,                \
                              variadic_kernel_unfold_marco,       \
                              cpp_dtype)                          \
810
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() { return 0; }
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
#define _PD_KERNEL_REGISTRAR_INIT_2(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_1(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
847
                                        __VA_ARGS__))
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
#define _PD_KERNEL_REGISTRAR_INIT_3(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_2(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
884
                                        __VA_ARGS__))
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
#define _PD_KERNEL_REGISTRAR_INIT_4(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_3(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
921
                                        __VA_ARGS__))
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
#define _PD_KERNEL_REGISTRAR_INIT_5(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_4(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
958
                                        __VA_ARGS__))
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
#define _PD_KERNEL_REGISTRAR_INIT_6(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_5(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
995
                                        __VA_ARGS__))
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
#define _PD_KERNEL_REGISTRAR_INIT_7(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_6(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
1032
                                        __VA_ARGS__))
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
#define _PD_KERNEL_REGISTRAR_INIT_8(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_7(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
1069
                                        __VA_ARGS__))
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
#define _PD_KERNEL_REGISTRAR_INIT_9(reg_type,                         \
                                    kernel_name,                      \
                                    backend,                          \
                                    context,                          \
                                    layout,                           \
                                    registrar_id,                     \
                                    args_def_fn,                      \
                                    meta_kernel_fn,                   \
                                    arg_parse_functor_macro,          \
                                    kernel_unfold_macro,              \
                                    variadic_kernel_unfold_marco,     \
                                    cpp_dtype,                        \
                                    ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_8(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
1106
                                        __VA_ARGS__))
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
#define _PD_KERNEL_REGISTRAR_INIT_10(reg_type,                        \
                                     kernel_name,                     \
                                     backend,                         \
                                     context,                         \
                                     layout,                          \
                                     registrar_id,                    \
                                     args_def_fn,                     \
                                     meta_kernel_fn,                  \
                                     arg_parse_functor_macro,         \
                                     kernel_unfold_macro,             \
                                     variadic_kernel_unfold_marco,    \
                                     cpp_dtype,                       \
                                     ...)                             \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                               \
                              kernel_name,                            \
                              backend,                                \
                              context,                                \
                              layout,                                 \
                              registrar_id,                           \
                              args_def_fn,                            \
                              meta_kernel_fn,                         \
                              arg_parse_functor_macro,                \
                              kernel_unfold_macro,                    \
                              variadic_kernel_unfold_marco,           \
                              cpp_dtype)                              \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_9(reg_type,                     \
                                        kernel_name,                  \
                                        backend,                      \
                                        context,                      \
                                        layout,                       \
                                        PD_ID,                        \
                                        args_def_fn,                  \
                                        meta_kernel_fn,               \
                                        arg_parse_functor_macro,      \
                                        kernel_unfold_macro,          \
                                        variadic_kernel_unfold_marco, \
1143
                                        __VA_ARGS__))
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
#define _PD_KERNEL_REGISTRAR_INIT_11(reg_type,                         \
                                     kernel_name,                      \
                                     backend,                          \
                                     context,                          \
                                     layout,                           \
                                     registrar_id,                     \
                                     args_def_fn,                      \
                                     meta_kernel_fn,                   \
                                     arg_parse_functor_macro,          \
                                     kernel_unfold_macro,              \
                                     variadic_kernel_unfold_marco,     \
                                     cpp_dtype,                        \
                                     ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                                \
                              kernel_name,                             \
                              backend,                                 \
                              context,                                 \
                              layout,                                  \
                              registrar_id,                            \
                              args_def_fn,                             \
                              meta_kernel_fn,                          \
                              arg_parse_functor_macro,                 \
                              kernel_unfold_macro,                     \
                              variadic_kernel_unfold_marco,            \
                              cpp_dtype)                               \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_10(reg_type,                     \
                                         kernel_name,                  \
                                         backend,                      \
                                         context,                      \
                                         layout,                       \
                                         PD_ID,                        \
                                         args_def_fn,                  \
                                         meta_kernel_fn,               \
                                         arg_parse_functor_macro,      \
                                         kernel_unfold_macro,          \
                                         variadic_kernel_unfold_marco, \
1180
                                         __VA_ARGS__))
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
#define _PD_KERNEL_REGISTRAR_INIT_12(reg_type,                         \
                                     kernel_name,                      \
                                     backend,                          \
                                     context,                          \
                                     layout,                           \
                                     registrar_id,                     \
                                     args_def_fn,                      \
                                     meta_kernel_fn,                   \
                                     arg_parse_functor_macro,          \
                                     kernel_unfold_macro,              \
                                     variadic_kernel_unfold_marco,     \
                                     cpp_dtype,                        \
                                     ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                                \
                              kernel_name,                             \
                              backend,                                 \
                              context,                                 \
                              layout,                                  \
                              registrar_id,                            \
                              args_def_fn,                             \
                              meta_kernel_fn,                          \
                              arg_parse_functor_macro,                 \
                              kernel_unfold_macro,                     \
                              variadic_kernel_unfold_marco,            \
                              cpp_dtype)                               \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_11(reg_type,                     \
                                         kernel_name,                  \
                                         backend,                      \
                                         context,                      \
                                         layout,                       \
                                         PD_ID,                        \
                                         args_def_fn,                  \
                                         meta_kernel_fn,               \
                                         arg_parse_functor_macro,      \
                                         kernel_unfold_macro,          \
                                         variadic_kernel_unfold_marco, \
1217
                                         __VA_ARGS__))
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
#define _PD_KERNEL_REGISTRAR_INIT_13(reg_type,                         \
                                     kernel_name,                      \
                                     backend,                          \
                                     context,                          \
                                     layout,                           \
                                     registrar_id,                     \
                                     args_def_fn,                      \
                                     meta_kernel_fn,                   \
                                     arg_parse_functor_macro,          \
                                     kernel_unfold_macro,              \
                                     variadic_kernel_unfold_marco,     \
                                     cpp_dtype,                        \
                                     ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                                \
                              kernel_name,                             \
                              backend,                                 \
                              context,                                 \
                              layout,                                  \
                              registrar_id,                            \
                              args_def_fn,                             \
                              meta_kernel_fn,                          \
                              arg_parse_functor_macro,                 \
                              kernel_unfold_macro,                     \
                              variadic_kernel_unfold_marco,            \
                              cpp_dtype)                               \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_12(reg_type,                     \
                                         kernel_name,                  \
                                         backend,                      \
                                         context,                      \
                                         layout,                       \
                                         PD_ID,                        \
                                         args_def_fn,                  \
                                         meta_kernel_fn,               \
                                         arg_parse_functor_macro,      \
                                         kernel_unfold_macro,          \
                                         variadic_kernel_unfold_marco, \
1254
                                         __VA_ARGS__))
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
#define _PD_KERNEL_REGISTRAR_INIT_14(reg_type,                         \
                                     kernel_name,                      \
                                     backend,                          \
                                     context,                          \
                                     layout,                           \
                                     registrar_id,                     \
                                     args_def_fn,                      \
                                     meta_kernel_fn,                   \
                                     arg_parse_functor_macro,          \
                                     kernel_unfold_macro,              \
                                     variadic_kernel_unfold_marco,     \
                                     cpp_dtype,                        \
                                     ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                                \
                              kernel_name,                             \
                              backend,                                 \
                              context,                                 \
                              layout,                                  \
                              registrar_id,                            \
                              args_def_fn,                             \
                              meta_kernel_fn,                          \
                              arg_parse_functor_macro,                 \
                              kernel_unfold_macro,                     \
                              variadic_kernel_unfold_marco,            \
                              cpp_dtype)                               \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_13(reg_type,                     \
                                         kernel_name,                  \
                                         backend,                      \
                                         context,                      \
                                         layout,                       \
                                         PD_ID,                        \
                                         args_def_fn,                  \
                                         meta_kernel_fn,               \
                                         arg_parse_functor_macro,      \
                                         kernel_unfold_macro,          \
                                         variadic_kernel_unfold_marco, \
1291
                                         __VA_ARGS__))
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
#define _PD_KERNEL_REGISTRAR_INIT_15(reg_type,                         \
                                     kernel_name,                      \
                                     backend,                          \
                                     context,                          \
                                     layout,                           \
                                     registrar_id,                     \
                                     args_def_fn,                      \
                                     meta_kernel_fn,                   \
                                     arg_parse_functor_macro,          \
                                     kernel_unfold_macro,              \
                                     variadic_kernel_unfold_marco,     \
                                     cpp_dtype,                        \
                                     ...)                              \
  _PD_CREATE_REGISTRAR_OBJECT(reg_type,                                \
                              kernel_name,                             \
                              backend,                                 \
                              context,                                 \
                              layout,                                  \
                              registrar_id,                            \
                              args_def_fn,                             \
                              meta_kernel_fn,                          \
                              arg_parse_functor_macro,                 \
                              kernel_unfold_macro,                     \
                              variadic_kernel_unfold_marco,            \
                              cpp_dtype)                               \
  PD_EXPAND(_PD_KERNEL_REGISTRAR_INIT_14(reg_type,                     \
                                         kernel_name,                  \
                                         backend,                      \
                                         context,                      \
                                         layout,                       \
                                         PD_ID,                        \
                                         args_def_fn,                  \
                                         meta_kernel_fn,               \
                                         arg_parse_functor_macro,      \
                                         kernel_unfold_macro,          \
                                         variadic_kernel_unfold_marco, \
1328
                                         __VA_ARGS__))
1329
/** PD_REGISTER_KERNEL_FOR_ALL_DTYPE
1330 1331 1332 1333 1334
 *
 * Basic Kernel register marco, used to register a instantiated kernel function
 * with one template argument.
 */

1335 1336 1337 1338
#define PD_REGISTER_KERNEL_FOR_ALL_DTYPE(    \
    kernel_name, backend, layout, kernel_fn) \
  _PD_REGISTER_KERNEL_FOR_ALL_DTYPE(         \
      ::phi::RegType::INNER, kernel_name, backend, layout, kernel_fn)
1339

1340 1341
#define _PD_REGISTER_KERNEL_FOR_ALL_DTYPE(                                   \
    reg_type, kernel_name, backend, layout, kernel_fn)                       \
1342
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
1343 1344
      PD_REGISTER_no_t_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_REGISTER_NO_TEMPLATE_KERNEL must be called in global namespace."); \
1345 1346
  __PD_REGISTER_KERNEL_FOR_ALL_DTYPE(                                        \
      reg_type, kernel_name, backend, layout, kernel_fn)
1347 1348

#ifndef _WIN32
1349 1350
#define __PD_REGISTER_KERNEL_FOR_ALL_DTYPE(                                 \
    reg_type, kernel_name, backend, layout, kernel_fn)                      \
1351
  template decltype(kernel_fn) kernel_fn;                                   \
1352
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
1353 1354
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
1355
      __reg_phi_kernel_##kernel_name##_##backend##_##layout(                \
1356
          reg_type,                                                         \
1357
          #kernel_name,                                                     \
1358
          #backend,                                                         \
1359
          DATA_LAYOUT(layout),                                              \
1360
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1361 1362 1363
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1364 1365 1366
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1367
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1368
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1369
#else
1370 1371
#define __PD_REGISTER_KERNEL_FOR_ALL_DTYPE(                                 \
    reg_type, kernel_name, backend, layout, kernel_fn)                      \
1372
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
1373 1374
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel);           \
  static const ::phi::KernelRegistrar                                       \
1375
      __reg_phi_kernel_##kernel_name##_##backend##_##layout(                \
1376
          reg_type,                                                         \
1377
          #kernel_name,                                                     \
1378
          #backend,                                                         \
1379
          DATA_LAYOUT(layout),                                              \
1380
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,       \
1381 1382 1383
          &__PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PHI_KERNEL(kernel_fn),                                            \
          PHI_VARIADIC_KERNEL(kernel_fn));                                  \
1384 1385 1386
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
1387
  void __PD_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
1388
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel)
1389 1390
#endif

Z
zhangyuqin1998 已提交
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
/** PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE
 *
 * Used to register a instantiated kernel function
 * for all backend with one template argument.
 */
#define PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(                  \
    kernel_name, layout, meta_kernel_fn)                           \
  _PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(::phi::RegType::INNER, \
                                            kernel_name,           \
                                            layout,                \
                                            meta_kernel_fn,        \
                                            BACKEND_LIST)

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
#define _DEVICE GPU,
#elif defined(PADDLE_WITH_XPU)
#define _DEVICE XPU,
#else
#define _DEVICE
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
#define _CUSTOM Custom,
#else
#define _CUSTOM
#endif

#define BACKEND_LIST _DEVICE _CUSTOM CPU

#define _PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(                            \
    reg_type, kernel_name, layout, meta_kernel_fn, ...)                       \
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                          \
      PD_REGISTER_nt_kernel_ns_check_##kernel_name##_##layout,                \
      "PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE must be called in global "    \
      "namespace.");                                                          \
  PD_EXPAND(__PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(reg_type,              \
                                                       kernel_name,           \
                                                       layout,                \
                                                       meta_kernel_fn,        \
                                                       PD_NARGS(__VA_ARGS__), \
                                                       __VA_ARGS__))

#define __PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(               \
    reg_type, kernel_name, layout, meta_kernel_fn, N, ...)        \
  static void __PD_KERNEL_args_def_FN_##kernel_name##_##layout(   \
      const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel); \
  PD_EXPAND(                                                      \
      PD_CONCATENATE(_PD_FOR_ALL_BACKEND_DTYPE_, N)(              \
          reg_type,                                               \
          kernel_name,                                            \
          layout,                                                 \
          meta_kernel_fn,                                         \
          __PD_KERNEL_args_def_FN_##kernel_name##_##layout,       \
          __VA_ARGS__) void                                       \
          __PD_KERNEL_args_def_FN_##kernel_name##_##layout(       \
              const ::phi::KernelKey& kernel_key, ::phi::Kernel* kernel))
#ifndef _WIN32
#define ___PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(                  \
    reg_type, kernel_name, backend, layout, kernel_fn, args_def_fn)   \
  template decltype(kernel_fn) kernel_fn;                             \
  static const ::phi::KernelRegistrar                                 \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout(          \
          reg_type,                                                   \
          #kernel_name,                                               \
          #backend,                                                   \
          DATA_LAYOUT(layout),                                        \
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse, \
          &args_def_fn,                                               \
          PHI_KERNEL(kernel_fn),                                      \
          PHI_VARIADIC_KERNEL(kernel_fn));                            \
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() { return 0; }
#else
#define ___PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(                  \
    reg_type, kernel_name, backend, layout, kernel_fn, args_def_fn)   \
  static const ::phi::KernelRegistrar                                 \
      __reg_phi_kernel_##kernel_name##_##backend##_##layout(          \
          reg_type,                                                   \
          #kernel_name,                                               \
          #backend,                                                   \
          DATA_LAYOUT(layout),                                        \
          ::phi::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse, \
          &args_def_fn,                                               \
          PHI_KERNEL(kernel_fn),                                      \
          PHI_VARIADIC_KERNEL(kernel_fn));                            \
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() { return 0; }
#endif
#define _PD_FOR_ALL_BACKEND_DTYPE_1(                                     \
    reg_type, kernel_name, layout, meta_kernel_fn, args_def_fn, backend) \
  ___PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(                           \
      reg_type,                                                          \
      kernel_name,                                                       \
      backend,                                                           \
      layout,                                                            \
      meta_kernel_fn<::phi::backend##Context>,                           \
      args_def_fn)

#define _PD_FOR_ALL_BACKEND_DTYPE_2(                                          \
    reg_type, kernel_name, layout, meta_kernel_fn, args_def_fn, backend, ...) \
  ___PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(                                \
      reg_type,                                                               \
      kernel_name,                                                            \
      backend,                                                                \
      layout,                                                                 \
      meta_kernel_fn<::phi::backend##Context>,                                \
      args_def_fn)                                                            \
  PD_EXPAND(_PD_FOR_ALL_BACKEND_DTYPE_1(reg_type,                             \
                                        kernel_name,                          \
                                        layout,                               \
                                        meta_kernel_fn,                       \
                                        args_def_fn,                          \
                                        __VA_ARGS__))
#define _PD_FOR_ALL_BACKEND_DTYPE_3(                                          \
    reg_type, kernel_name, layout, meta_kernel_fn, args_def_fn, backend, ...) \
  ___PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE(                                \
      reg_type,                                                               \
      kernel_name,                                                            \
      backend,                                                                \
      layout,                                                                 \
      meta_kernel_fn<::phi::backend##Context>,                                \
      args_def_fn)                                                            \
  PD_EXPAND(_PD_FOR_ALL_BACKEND_DTYPE_2(reg_type,                             \
                                        kernel_name,                          \
                                        layout,                               \
                                        meta_kernel_fn,                       \
                                        args_def_fn,                          \
                                        __VA_ARGS__))

1517
/** PD_DECLARE_KERNEL
1518 1519 1520 1521
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
1522
#define PD_DECLARE_KERNEL(kernel_name, backend, layout)                   \
1523
  PD_STATIC_ASSERT_GLOBAL_NAMESPACE(                                      \
1524 1525
      PD_DECLARE_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PD_DECLARE_KERNEL must be called in global namespace.");           \
1526 1527 1528 1529
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
1530

1531
/** PD_REGISTER_BUILTIN_KERNEL
1532 1533 1534 1535
 *
 * Used to register kernels for built-in backends.
 * Support CPU GPU XPU.
 */
1536 1537
#define PD_REGISTER_BUILTIN_KERNEL(                    \
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1538
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1539 1540 1541 1542 1543
                      kernel_name,                     \
                      backend,                         \
                      ::phi::backend##Context,         \
                      layout,                          \
                      meta_kernel_fn,                  \
1544 1545 1546 1547
                      FUNCTION_KERNEL_INSTANTIATION,   \
                      ARG_PARSE_FUNCTOR,               \
                      PHI_KERNEL,                      \
                      PHI_VARIADIC_KERNEL,             \
1548 1549
                      __VA_ARGS__)

1550
/** PD_REGISTER_PLUGIN_KERNEL
1551 1552 1553 1554
 *
 * Used to register kernels for plug-in backends.
 * Support user-defined backend such as 'Ascend910'.
 */
1555
#define PD_REGISTER_PLUGIN_KERNEL(                     \
1556
    kernel_name, backend, layout, meta_kernel_fn, ...) \
1557
  _PD_REGISTER_KERNEL(::phi::RegType::OUTER,           \
1558 1559 1560 1561 1562
                      kernel_name,                     \
                      backend,                         \
                      ::phi::CustomContext,            \
                      layout,                          \
                      meta_kernel_fn,                  \
1563 1564 1565 1566
                      FUNCTION_KERNEL_INSTANTIATION,   \
                      ARG_PARSE_FUNCTOR,               \
                      PHI_KERNEL,                      \
                      PHI_VARIADIC_KERNEL,             \
1567 1568
                      __VA_ARGS__)

1569
}  // namespace phi