kernel_registry.h 52.6 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 24 25 26 27
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <vector>

#include "paddle/pten/core/kernel_def.h"
#include "paddle/pten/core/kernel_factory.h"
#include "paddle/pten/core/kernel_utils.h"

28 29
#include "paddle/fluid/platform/enforce.h"

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
namespace pten {

#define BACKEND(arg__) pten::Backend::arg__
#define DATALAYOUT(arg__) pten::DataLayout::arg__
#define DATATYPE(arg__) pten::DataType::arg__

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
    auto default_tensor_layout = pten::DataLayout::NCHW;
    if (default_key.layout() != pten::DataLayout::ANY) {
      default_tensor_layout = default_key.layout();
    }
    auto args_type = ParseArgType(Indices{});
    for (auto arg_type : args_type) {
      if (arg_type == std::type_index(typeid(const CPUContext&))
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
          ||
60
          arg_type == std::type_index(typeid(const GPUContext&))) {
61 62 63 64 65 66 67
#else
              ) {
#endif
        // do nothing, skip context arg now
      } else if (arg_type == std::type_index(typeid(const DenseTensor&))) {
        args_def->AppendInput(
            default_key.backend(), default_tensor_layout, default_key.dtype());
68 69 70 71
      } else if (arg_type == std::type_index(typeid(
                                 paddle::optional<const DenseTensor&>))) {
        args_def->AppendInput(
            default_key.backend(), default_tensor_layout, default_key.dtype());
72 73 74 75
      } else if (arg_type ==
                 std::type_index(typeid(const std::vector<DenseTensor>&))) {
        args_def->AppendInput(
            default_key.backend(), default_tensor_layout, default_key.dtype());
76 77 78
      } else if (arg_type == std::type_index(typeid(DenseTensor*))) {
        args_def->AppendOutput(
            default_key.backend(), default_tensor_layout, default_key.dtype());
79 80 81 82
      } else if (arg_type ==
                 std::type_index(typeid(std::vector<DenseTensor*>))) {
        args_def->AppendOutput(
            default_key.backend(), default_tensor_layout, default_key.dtype());
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      } else {
        // Attribute deal with
        // TODO(chenweihang): now here allow any types of attribute, maybe
        // should add limits here
        args_def->AppendAttribute(arg_type);
      }
    }
  }

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

100 101
// TODO(chenweihang): Polish the kernel selection logic, support the selection
// of ALL_DTYPE kernel, and simplify the constructor
102 103 104 105 106 107 108 109
struct KernelRegistrar {
 public:
  KernelRegistrar(const char* kernel_name_cstr,
                  Backend backend,
                  DataLayout layout,
                  DataType dtype,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
110 111
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
112 113 114 115 116 117
    ConstructKernel(kernel_name_cstr,
                    backend,
                    layout,
                    dtype,
                    args_parse_fn,
                    args_def_fn,
118 119
                    kernel_fn,
                    variadic_kernel_fn);
120 121 122 123 124 125 126
  }

  KernelRegistrar(const char* kernel_name_cstr,
                  Backend backend,
                  DataLayout layout,
                  KernelArgsParseFn args_parse_fn,
                  KernelArgsDefFn args_def_fn,
127 128
                  KernelFn kernel_fn,
                  void* variadic_kernel_fn) {
129 130 131 132 133 134 135 136 137
    for (size_t dtype = static_cast<size_t>(DataType::BOOL);
         dtype != static_cast<size_t>(DataType::NUM_DATA_TYPES);
         dtype++) {
      ConstructKernel(kernel_name_cstr,
                      backend,
                      layout,
                      static_cast<DataType>(dtype),
                      args_parse_fn,
                      args_def_fn,
138 139
                      kernel_fn,
                      variadic_kernel_fn);
140 141 142 143 144 145 146 147 148 149
    }
  }

 private:
  void ConstructKernel(const char* kernel_name_cstr,
                       Backend backend,
                       DataLayout layout,
                       DataType dtype,
                       KernelArgsParseFn args_parse_fn,
                       KernelArgsDefFn args_def_fn,
150 151
                       KernelFn kernel_fn,
                       void* variadic_kernel_fn) {
Y
YuanRisheng 已提交
152
    std::string kernel_name(kernel_name_cstr);
153
    KernelKey kernel_key(backend, layout, dtype);
154
    Kernel kernel(kernel_fn, variadic_kernel_fn);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    args_parse_fn(kernel_key, kernel.mutable_args_def());
    args_def_fn(&kernel);
    KernelFactory::Instance().kernels()[kernel_name][kernel_key] = kernel;
  }
};

#define PT_STATIC_ASSERT_GLOBAL_NAMESPACE(uniq_name, msg) \
  _PT_STATIC_ASSERT_GLOBAL_NAMESPACE(uniq_name, msg)

#define _PT_STATIC_ASSERT_GLOBAL_NAMESPACE(uniq_name, msg)                    \
  struct __test_global_namespace_##uniq_name##__ {};                          \
  static_assert(std::is_same<::__test_global_namespace_##uniq_name##__,       \
                             __test_global_namespace_##uniq_name##__>::value, \
                msg)

#ifdef __COUNTER__
#define PT_ID __COUNTER__
#else
#define PT_ID __LINE__
#endif

#if defined(_WIN32)
#define UNUSED
#define __builtin_expect(EXP, C) (EXP)
#else
#define UNUSED __attribute__((unused))
#endif

#define PT_CONCATENATE(arg1, arg2) PT_CONCATENATE1(arg1, arg2)
#define PT_CONCATENATE1(arg1, arg2) PT_CONCATENATE2(arg1, arg2)
#define PT_CONCATENATE2(arg1, arg2) arg1##arg2
#define PT_EXPAND(x) x

/**
 * Reference:
 *
 *   https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
 *   https://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion?rq=1
 *   https://stackoverflow.com/questions/5134523/msvc-doesnt-expand-va-args-correctly
 *
 * Very carefully tiptoeing around an MSVC bug where it improperly expands
 * __VA_ARGS__ as a single token in argument lists.  See these URLs for details:
 *
 *   http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement
 *   http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644
 */
#define PT_NARGS(...) _PT_NARGS((__VA_ARGS__, _PT_RESQ_N()))
#define _PT_NARGS(...) _PT_ARG_N(__VA_ARGS__)
203 204 205
#define _PT_ARG_N_EXPAND(                                                     \
    _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, N, ...) \
  N
206
#define _PT_ARG_N(args) _PT_ARG_N_EXPAND args
207
#define _PT_RESQ_N() 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
208

209 210 211 212 213 214
/** PT_REGISTER_KERNEL
 *
 * 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.
215
 *
216
 * Note: `2TA` means `2 template argument`
217
 */
218 219 220 221 222
#define PT_REGISTER_KERNEL(                                                \
    kernel_name, backend, layout, meta_kernel_fn, cpp_dtype, ...)          \
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                       \
      pt_register_tp_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PT_REGISTER_KERNEL must be called in global namespace.");           \
223
  _PT_REGISTER_2TA_KERNEL(                                                 \
224 225
      kernel_name, backend, layout, meta_kernel_fn, cpp_dtype, __VA_ARGS__)

226
#ifndef _WIN32
227
#define _PT_REGISTER_2TA_KERNEL(                                            \
228
    kernel_name, backend, layout, meta_kernel_fn, cpp_dtype, ...)           \
229
  PT_KERNEL_INSTANTIATION(meta_kernel_fn, backend, cpp_dtype, __VA_ARGS__); \
230 231 232 233 234 235 236 237 238 239 240 241
  static void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
      ::pten::Kernel*);                                                     \
  PT_KERNEL_REGISTRAR_INIT(                                                 \
      kernel_name,                                                          \
      backend,                                                              \
      layout,                                                               \
      &__PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
      meta_kernel_fn,                                                       \
      cpp_dtype,                                                            \
      __VA_ARGS__);                                                         \
  void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
      ::pten::Kernel* kernel)
242 243 244 245 246 247 248 249 250 251 252
#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
 *
253
 * And msvc can work without template instantiation
254
 */
255
#define _PT_REGISTER_2TA_KERNEL(                                            \
256 257 258 259 260 261 262 263 264 265 266 267 268
    kernel_name, backend, layout, meta_kernel_fn, cpp_dtype, ...)           \
  static void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
      ::pten::Kernel*);                                                     \
  PT_KERNEL_REGISTRAR_INIT(                                                 \
      kernel_name,                                                          \
      backend,                                                              \
      layout,                                                               \
      &__PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,        \
      meta_kernel_fn,                                                       \
      cpp_dtype,                                                            \
      __VA_ARGS__);                                                         \
  void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
      ::pten::Kernel* kernel)
269 270
#endif

271 272 273 274 275
#define PT_KERNEL_INSTANTIATION(meta_kernel_fn, backend, cpp_dtype, ...) \
  _PT_KERNEL_INSTANTIATION(PT_NARGS(cpp_dtype, __VA_ARGS__),             \
                           meta_kernel_fn,                               \
                           backend,                                      \
                           cpp_dtype,                                    \
276 277
                           __VA_ARGS__)

278 279 280
#define _PT_KERNEL_INSTANTIATION(N, meta_kernel_fn, backend, cpp_dtype, ...) \
  PT_CONCATENATE(_PT_KERNEL_INSTANTIATION_, N)                               \
  (meta_kernel_fn, backend, cpp_dtype, __VA_ARGS__)
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
#define _PT_KERNEL_INSTANTIATION_1(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>
#define _PT_KERNEL_INSTANTIATION_2(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_1(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_3(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_2(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_4(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_3(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_5(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_4(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_6(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_5(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_7(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_6(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_8(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_7(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_9(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)    \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                  \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_8(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_10(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)     \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                   \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_9(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_11(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)     \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                   \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_10(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_12(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)     \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                   \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_11(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_13(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)     \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                   \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_12(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_14(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)     \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                   \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_13(meta_kernel_fn, backend, __VA_ARGS__))
#define _PT_KERNEL_INSTANTIATION_15(meta_kernel_fn, backend, cpp_dtype, ...) \
  template decltype(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)     \
      meta_kernel_fn<cpp_dtype, ::pten::backend##Context>;                   \
  PT_EXPAND(_PT_KERNEL_INSTANTIATION_14(meta_kernel_fn, backend, __VA_ARGS__))
341

342 343 344 345 346 347 348 349 350
#define PT_KERNEL_REGISTRAR_INIT(                                              \
    kernel_name, backend, layout, args_def_fn, meta_kernel_fn, cpp_dtype, ...) \
  _PT_KERNEL_REGISTRAR_INIT(PT_NARGS(cpp_dtype, __VA_ARGS__),                  \
                            kernel_name,                                       \
                            backend,                                           \
                            layout,                                            \
                            args_def_fn,                                       \
                            meta_kernel_fn,                                    \
                            cpp_dtype,                                         \
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
                            __VA_ARGS__)

// clang-format off

/* The =pre-commit always treats this macro into the wrong format,
  and multi-line macros cannot be skipped with NOLINT.*/
#define _PT_KERNEL_REGISTRAR_INIT(N,              \
                                  kernel_name,    \
                                  backend,        \
                                  layout,         \
                                  args_def_fn,    \
                                  meta_kernel_fn, \
                                  cpp_dtype,      \
                                  ...)            \
  PT_CONCATENATE(_PT_KERNEL_REGISTRAR_INIT_, N) ( \
    kernel_name,                                  \
    backend,                                      \
    layout,                                       \
369
    PT_ID,                                        \
370 371 372 373 374 375 376
    args_def_fn,                                  \
    meta_kernel_fn,                               \
    cpp_dtype,                                    \
    __VA_ARGS__)

// clang-format on

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
#define _PT_KERNEL_REGISTRAR_INIT_1(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
392
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
393
      args_def_fn,                                                            \
394 395 396
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() { return 0; }
#define _PT_KERNEL_REGISTRAR_INIT_2(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
413
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
414
      args_def_fn,                                                            \
415 416 417
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
418 419 420 421 422 423
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_1(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
424
                                        __VA_ARGS__))
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
#define _PT_KERNEL_REGISTRAR_INIT_3(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
440
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
441
      args_def_fn,                                                            \
442 443 444
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
445 446 447 448 449 450
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_2(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
451
                                        __VA_ARGS__))
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
#define _PT_KERNEL_REGISTRAR_INIT_4(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
467
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
468
      args_def_fn,                                                            \
469 470 471
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
472 473 474 475 476 477
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_3(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
478
                                        __VA_ARGS__))
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
#define _PT_KERNEL_REGISTRAR_INIT_5(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
494
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
495
      args_def_fn,                                                            \
496 497 498
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
499 500 501 502 503 504
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_4(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
505
                                        __VA_ARGS__))
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
#define _PT_KERNEL_REGISTRAR_INIT_6(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
521
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
522
      args_def_fn,                                                            \
523 524 525
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
526 527 528 529 530 531
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_5(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
532
                                        __VA_ARGS__))
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
#define _PT_KERNEL_REGISTRAR_INIT_7(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
548
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
549
      args_def_fn,                                                            \
550 551 552
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
553 554 555 556 557 558
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_6(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
559
                                        __VA_ARGS__))
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
#define _PT_KERNEL_REGISTRAR_INIT_8(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
575
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
576
      args_def_fn,                                                            \
577 578 579
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
580 581 582 583 584 585
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_7(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
586
                                        __VA_ARGS__))
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
#define _PT_KERNEL_REGISTRAR_INIT_9(kernel_name,                              \
                                    backend,                                  \
                                    layout,                                   \
                                    registrar_id,                             \
                                    args_def_fn,                              \
                                    meta_kernel_fn,                           \
                                    cpp_dtype,                                \
                                    ...)                                      \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
602
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
603
      args_def_fn,                                                            \
604 605 606
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
607 608 609 610 611 612
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_8(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
613
                                        __VA_ARGS__))
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
#define _PT_KERNEL_REGISTRAR_INIT_10(kernel_name,                             \
                                     backend,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
629
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
630
      args_def_fn,                                                            \
631 632 633
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
634 635 636 637 638 639
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_9(kernel_name,                          \
                                        backend,                              \
                                        layout,                               \
                                        PT_ID,                                \
                                        args_def_fn,                          \
                                        meta_kernel_fn,                       \
640
                                        __VA_ARGS__))
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
#define _PT_KERNEL_REGISTRAR_INIT_11(kernel_name,                             \
                                     backend,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
656
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
657
      args_def_fn,                                                            \
658 659 660
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
661 662 663 664 665 666
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_10(kernel_name,                         \
                                         backend,                             \
                                         layout,                              \
                                         PT_ID,                               \
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
667
                                         __VA_ARGS__))
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
#define _PT_KERNEL_REGISTRAR_INIT_12(kernel_name,                             \
                                     backend,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
683
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
684
      args_def_fn,                                                            \
685 686 687
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
688 689 690 691 692 693
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_11(kernel_name,                         \
                                         backend,                             \
                                         layout,                              \
                                         PT_ID,                               \
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
694
                                         __VA_ARGS__))
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
#define _PT_KERNEL_REGISTRAR_INIT_13(kernel_name,                             \
                                     backend,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
710
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
711
      args_def_fn,                                                            \
712 713 714
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
715 716 717 718 719 720
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_12(kernel_name,                         \
                                         backend,                             \
                                         layout,                              \
                                         PT_ID,                               \
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
721
                                         __VA_ARGS__))
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
#define _PT_KERNEL_REGISTRAR_INIT_14(kernel_name,                             \
                                     backend,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
737
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
738
      args_def_fn,                                                            \
739 740 741
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
742 743 744 745 746 747
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_13(kernel_name,                         \
                                         backend,                             \
                                         layout,                              \
                                         PT_ID,                               \
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
748
                                         __VA_ARGS__))
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
#define _PT_KERNEL_REGISTRAR_INIT_15(kernel_name,                             \
                                     backend,                                 \
                                     layout,                                  \
                                     registrar_id,                            \
                                     args_def_fn,                             \
                                     meta_kernel_fn,                          \
                                     cpp_dtype,                               \
                                     ...)                                     \
  static const ::pten::KernelRegistrar PT_CONCATENATE(                        \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout##_, registrar_id)( \
      #kernel_name,                                                           \
      BACKEND(backend),                                                       \
      DATALAYOUT(layout),                                                     \
      ::paddle::experimental::CppTypeToDataType<cpp_dtype>::Type(),           \
      ::pten::KernelArgsParseFunctor<decltype(                                \
764
          &meta_kernel_fn<cpp_dtype, ::pten::backend##Context>)>::Parse,      \
765
      args_def_fn,                                                            \
766 767 768
      PT_KERNEL(meta_kernel_fn<cpp_dtype, ::pten::backend##Context>),         \
      PT_VARIADIC_KERNEL(                                                     \
          meta_kernel_fn<cpp_dtype, ::pten::backend##Context>));              \
769 770 771 772 773 774
  PT_EXPAND(_PT_KERNEL_REGISTRAR_INIT_14(kernel_name,                         \
                                         backend,                             \
                                         layout,                              \
                                         PT_ID,                               \
                                         args_def_fn,                         \
                                         meta_kernel_fn,                      \
775
                                         __VA_ARGS__))
776

777 778 779 780 781 782 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 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/** PT_REGISTER_GENERAL_KERNEL
 *
 * Basic Kernel register marco, used to register a instantiated kernel function
 * with one template argument.
 */

#define PT_REGISTER_GENERAL_KERNEL(                                          \
    kernel_name, backend, layout, kernel_fn, dtype)                          \
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                         \
      pt_register_no_t_kernel_ns_check_##kernel_name##_##backend##_##layout, \
      "PT_REGISTER_NO_TEMPLATE_KERNEL must be called in global namespace."); \
  _PT_REGISTER_GENERAL_KERNEL(kernel_name, backend, layout, kernel_fn, dtype)

#ifndef _WIN32
#define _PT_REGISTER_GENERAL_KERNEL(                                        \
    kernel_name, backend, layout, kernel_fn, dtype)                         \
  template decltype(kernel_fn) kernel_fn;                                   \
  static void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
      ::pten::Kernel*);                                                     \
  static const ::pten::KernelRegistrar                                      \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
          #kernel_name,                                                     \
          BACKEND(backend),                                                 \
          DATALAYOUT(layout),                                               \
          ::pten::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,      \
          &__PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PT_KERNEL(kernel_fn),                                             \
          PT_VARIADIC_KERNEL(kernel_fn));                                   \
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
  void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
      ::pten::Kernel* kernel)
#else
#define _PT_REGISTER_GENERAL_KERNEL(                                        \
    kernel_name, backend, layout, kernel_fn, dtype)                         \
  static void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout( \
      ::pten::Kernel*);                                                     \
  static const ::pten::KernelRegistrar                                      \
      __reg_pt_kernel_##kernel_name##_##backend##_##layout(                 \
          #kernel_name,                                                     \
          BACKEND(backend),                                                 \
          DATALAYOUT(layout),                                               \
          ::pten::KernelArgsParseFunctor<decltype(&kernel_fn)>::Parse,      \
          &__PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout,    \
          PT_KERNEL(kernel_fn),                                             \
          PT_VARIADIC_KERNEL(kernel_fn));                                   \
  int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout() {         \
    return 0;                                                               \
  }                                                                         \
  void __PT_KERNEL_args_def_FN_##kernel_name##_##backend##_##layout(        \
      ::pten::Kernel* kernel)
#endif

831 832 833 834 835
/** PT_DECLARE_KERNEL
 *
 * Used to export the symbols of the file where the kernel is located,
 * to avoid being removed by linker
 */
836 837 838 839 840
#define PT_DECLARE_KERNEL(kernel_name, backend, layout)                   \
  extern int TouchKernelSymbolFor_##kernel_name##_##backend##_##layout(); \
  UNUSED static int                                                       \
      __declare_kernel_symbol_for_##kernel_name##_##backend##_##layout =  \
          TouchKernelSymbolFor_##kernel_name##_##backend##_##layout()
841

842
}  // namespace pten