op_utils.h 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2022 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 <mutex>

#include "paddle/pten/core/compat/arg_map_context.h"
20
#include "paddle/pten/core/enforce.h"
21 22
#include "paddle/pten/core/infermeta_utils.h"
#include "paddle/pten/core/macros.h"
23
#include "paddle/pten/core/type_defs.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "paddle/utils/flat_hash_map.h"

namespace pten {

class DefaultKernelSignatureMap {
 public:
  static DefaultKernelSignatureMap& Instance();

  bool Has(const std::string& op_type) const { return map_.count(op_type) > 0; }

  const KernelSignature& Get(const std::string& op_type) const {
    auto it = map_.find(op_type);
    PADDLE_ENFORCE_NE(
        it,
        map_.end(),
39
        pten::errors::NotFound(
40 41 42 43 44 45 46 47
            "Operator `%s`'s kernel signature is not registered.", op_type));
    return it->second;
  }

  void Insert(std::string op_type, KernelSignature signature) {
    PADDLE_ENFORCE_NE(
        Has(op_type),
        true,
48
        pten::errors::AlreadyExists(
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            "Operator (%s)'s Kernel Siginature has been registered.", op_type));
    map_.insert({std::move(op_type), std::move(signature)});
  }

 private:
  DefaultKernelSignatureMap() = default;

  paddle::flat_hash_map<std::string, KernelSignature> map_;

  DISABLE_COPY_AND_ASSIGN(DefaultKernelSignatureMap);
};

class OpUtilsMap {
 public:
  static OpUtilsMap& Instance();

  bool Contains(const std::string& op_type) const {
    return name_map_.count(op_type) || arg_mapping_fn_map_.count(op_type);
  }

  void InsertApiName(std::string op_type, std::string api_name) {
    PADDLE_ENFORCE_EQ(
        name_map_.count(op_type),
        0UL,
73
        pten::errors::AlreadyExists(
74 75 76 77 78 79 80 81
            "Operator (%s)'s api name has been registered.", op_type));
    name_map_.insert({std::move(op_type), std::move(api_name)});
  }

  void InsertArgumentMappingFn(std::string op_type, ArgumentMappingFn fn) {
    PADDLE_ENFORCE_EQ(
        arg_mapping_fn_map_.count(op_type),
        0UL,
82
        pten::errors::AlreadyExists(
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            "Operator (%s)'s argu,emt mapping function has been registered.",
            op_type));
    arg_mapping_fn_map_.insert({std::move(op_type), std::move(fn)});
  }

  std::string GetApiName(const std::string& op_type) const {
    auto it = name_map_.find(op_type);
    if (it == name_map_.end()) {
      return "deprecated";
    } else {
      return it->second;
    }
  }

  ArgumentMappingFn GetArgumentMappingFn(const std::string& op_type) const {
    auto it = arg_mapping_fn_map_.find(op_type);
    if (it == arg_mapping_fn_map_.end()) {
      auto func =
          [op_type](const ArgumentMappingContext& ctx) -> KernelSignature {
        return DefaultKernelSignatureMap::Instance().Get(op_type);
      };
      return func;
    } else {
      return it->second;
    }
  }

 private:
  OpUtilsMap() = default;

  paddle::flat_hash_map<std::string, std::string> name_map_;
  paddle::flat_hash_map<std::string, ArgumentMappingFn> arg_mapping_fn_map_;

  DISABLE_COPY_AND_ASSIGN(OpUtilsMap);
};

struct ApiNameRegistrar {
  ApiNameRegistrar(const char* op_type, const char* api_name) {
    OpUtilsMap::Instance().InsertApiName(op_type, api_name);
  }
};

struct ArgumentMappingFnRegistrar {
  ArgumentMappingFnRegistrar(const char* op_type,
                             ArgumentMappingFn arg_mapping_fn) {
    OpUtilsMap::Instance().InsertArgumentMappingFn(op_type,
                                                   std::move(arg_mapping_fn));
  }
};

#define PT_REGISTER_API_NAME(op_type, api_name)                             \
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                        \
      pt_register_api_name_ns_check_##op_type,                              \
      "PT_REGISTER_API_NAME must be called in global namespace.");          \
  static const ::pten::ApiNameRegistrar __registrar_api_name_for_##op_type( \
      #op_type, #api_name);                                                 \
  int TouchApiNameSymbol_##op_type() { return 0; }

#define PT_DECLARE_API_NAME(op_type)                              \
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                              \
      pt_declare_ai_name_ns_check_##op_type,                      \
      "PT_DECLARE_API_NAME must be called in global namespace."); \
  extern int TouchApiNameSymbol_##op_type();                      \
  UNUSED static int __declare_api_name_symbol_for_##op_type =     \
      TouchApiNameSymbol_##op_type()

#define PT_REGISTER_ARG_MAPPING_FN(op_type, arg_mapping_fn)              \
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                     \
      pt_register_arg_map_fn_ns_check_##op_type,                         \
      "PT_REGISTER_ARG_MAPPING_FN must be called in global namespace."); \
  static const ::pten::ArgumentMappingFnRegistrar                        \
      __registrar_arg_map_fn_for_##op_type(#op_type, arg_mapping_fn);    \
  int TouchArgumentMappingFnSymbol_##op_type() { return 0; }

#define PT_DECLARE_ARG_MAPPING_FN(op_type)                              \
  PT_STATIC_ASSERT_GLOBAL_NAMESPACE(                                    \
      pt_declare_arg_map_fn_ns_check_##op_type,                         \
      "PT_DECLARE_ARG_MAPPING_FN must be called in global namespace."); \
  extern int TouchArgumentMappingFnSymbol_##op_type();                  \
  UNUSED static int __declare_arg_map_fn_symbol_for_##op_type =         \
      TouchArgumentMappingFnSymbol_##op_type()

}  // namespace pten