op_registry.h 8.5 KB
Newer Older
1
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14 15 16

#pragma once

17 18
#include <string>
#include <tuple>
S
sneaxiy 已提交
19
#include <type_traits>
M
minqiyang 已提交
20 21
#include <unordered_map>
#include <unordered_set>
22
#include <vector>
Y
Yi Wang 已提交
23
#include "paddle/fluid/framework/grad_op_desc_maker.h"
D
dzhwinter 已提交
24
#include "paddle/fluid/framework/inplace_op_inference.h"
S
sneaxiy 已提交
25
#include "paddle/fluid/framework/no_need_buffer_vars_inference.h"
Y
Yi Wang 已提交
26 27 28 29
#include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/framework/op_proto_maker.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/var_type_inference.h"
H
hong 已提交
30 31
#include "paddle/fluid/imperative/dygraph_grad_maker.h"
#include "paddle/fluid/imperative/type_defs.h"
32 33 34 35 36 37 38 39

namespace paddle {
namespace framework {
namespace details {

enum OpInfoFillType {
  kOperator = 0,
  kOpProtoAndCheckerMaker = 1,
Y
Yu Yang 已提交
40
  kGradOpDescMaker = 2,
41
  kVarTypeInference = 3,
D
dzhwinter 已提交
42
  kShapeInference = 4,
S
sneaxiy 已提交
43 44
  kInplaceOpInference = 5,
  kNoNeedBufferVarsInference = 6,
H
hong 已提交
45
  kGradOpBaseMaker = 7,
S
sneaxiy 已提交
46
  kUnknown = -1
47 48
};

S
sneaxiy 已提交
49 50 51 52 53 54 55 56 57 58 59
namespace internal {
template <typename T, OpInfoFillType kType>
struct TypePair {
  using Type = T;
  static constexpr OpInfoFillType kFillType = kType;
};

using OpRegistryClasses = std::tuple<                                // NOLINT
    TypePair<OperatorBase, kOperator>,                               // NOLINT
    TypePair<OpProtoAndCheckerMaker, kOpProtoAndCheckerMaker>,       // NOLINT
    TypePair<GradOpDescMakerBase, kGradOpDescMaker>,                 // NOLINT
H
hong 已提交
60
    TypePair<imperative::GradOpBaseMakerBase, kGradOpBaseMaker>,     // NOLINT
S
sneaxiy 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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
    TypePair<VarTypeInference, kVarTypeInference>,                   // NOLINT
    TypePair<InferShapeBase, kShapeInference>,                       // NOLINT
    TypePair<InplaceOpInference, kInplaceOpInference>,               // NOLINT
    TypePair<NoNeedBufferVarsInference, kNoNeedBufferVarsInference>  // NOLINT
    >;

static constexpr int kOpRegistryClassNumber =
    std::tuple_size<OpRegistryClasses>::value;

template <typename T, int kPos, bool kIsBounded /* = true*/>
struct IsMatchedBaseTypeImpl {
  using PairType = typename std::tuple_element<kPos, OpRegistryClasses>::type;
  static constexpr bool kValue =
      std::is_base_of<typename PairType::Type, T>::value;
};

template <typename T, int kPos>
struct IsMatchedBaseTypeImpl<T, kPos, false> {
  static constexpr bool kValue = false;
};

template <typename T, int kPos>
static inline constexpr bool IsMatchedBaseType() {
  return IsMatchedBaseTypeImpl<
      T, kPos, (kPos >= 0 && kPos < kOpRegistryClassNumber)>::kValue;
}

template <typename T, int kStart, int kEnd, bool kIsEnd, bool kIsMatched>
struct OpInfoFillTypeGetterImpl {};

// This case should not happen
template <typename T, int kStart, int kEnd>
struct OpInfoFillTypeGetterImpl<T, kStart, kEnd, true, true> {};

template <typename T, int kStart, int kEnd>
struct OpInfoFillTypeGetterImpl<T, kStart, kEnd, true, false> {
  static constexpr OpInfoFillType kType = kUnknown;
};

template <typename T, int kStart, int kEnd>
struct OpInfoFillTypeGetterImpl<T, kStart, kEnd, false, false> {
  static constexpr OpInfoFillType kType =
      OpInfoFillTypeGetterImpl<T, kStart + 1, kEnd, kStart + 1 == kEnd,
                               IsMatchedBaseType<T, kStart + 1>()>::kType;
};

template <typename T, int kStart, int kEnd>
struct OpInfoFillTypeGetterImpl<T, kStart, kEnd, false, true> {
  using PairType = typename std::tuple_element<kStart, OpRegistryClasses>::type;
  static constexpr OpInfoFillType kType = PairType::kFillType;
};

template <typename T>
using OpInfoFillTypeGetter =
    OpInfoFillTypeGetterImpl<T, 0, kOpRegistryClassNumber,
                             kOpRegistryClassNumber == 0,
                             IsMatchedBaseType<T, 0>()>;

}  // namespace internal

121 122 123
template <typename T>
struct OpInfoFillTypeID {
  static constexpr OpInfoFillType ID() {
S
sneaxiy 已提交
124
    return internal::OpInfoFillTypeGetter<T>::kType;
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 166 167
  }
};

template <typename T, OpInfoFillType = OpInfoFillTypeID<T>::ID()>
struct OpInfoFiller;

template <size_t I, bool at_end, typename... ARGS>
class OperatorRegistrarRecursive;

template <size_t I, typename... ARGS>
class OperatorRegistrarRecursive<I, false, ARGS...> {
 public:
  using T = typename std::tuple_element<I, std::tuple<ARGS...>>::type;
  OperatorRegistrarRecursive(const char* op_type, OpInfo* info) {
    OpInfoFiller<T> fill;
    fill(op_type, info);
    constexpr auto size = sizeof...(ARGS);
    OperatorRegistrarRecursive<I + 1, I + 1 == size, ARGS...> reg(op_type,
                                                                  info);
    (void)(reg);
  }
};

template <size_t I, typename... ARGS>
class OperatorRegistrarRecursive<I, true, ARGS...> {
 public:
  OperatorRegistrarRecursive(const char* op_type, OpInfo* info) {}
};

template <typename T>
struct OpInfoFiller<T, kOperator> {
  void operator()(const char* op_type, OpInfo* info) const {
    info->creator_ = [](const std::string& type, const VariableNameMap& inputs,
                        const VariableNameMap& outputs,
                        const AttributeMap& attrs) {
      return new T(type, inputs, outputs, attrs);
    };
  }
};

template <typename T>
struct OpInfoFiller<T, kOpProtoAndCheckerMaker> {
  void operator()(const char* op_type, OpInfo* info) const {
168
    info->proto_ = new proto::OpProto;
169
    info->checker_ = new OpAttrChecker();
Y
Yu Yang 已提交
170
    T maker;
Y
yuyang18 已提交
171
    maker(info->proto_, info->checker_);
172 173 174 175 176 177 178 179 180 181 182
    info->proto_->set_type(op_type);
    PADDLE_ENFORCE(
        info->proto_->IsInitialized(),
        "Fail to initialize %s's OpProto, because %s is not initialized",
        op_type, info->proto_->InitializationErrorString());
  }
};

template <typename T>
struct OpInfoFiller<T, kGradOpDescMaker> {
  void operator()(const char* op_type, OpInfo* info) const {
183
    info->grad_op_maker_ = [](
Y
Yu Yang 已提交
184
        const OpDesc& fwd_op,
185
        const std::unordered_set<std::string>& no_grad_set,
Y
Yu Yang 已提交
186
        std::unordered_map<std::string, std::string>* grad_to_var,
Y
Yu Yang 已提交
187
        const std::vector<BlockDesc*>& grad_block) {
Y
Yu Yang 已提交
188
      T maker(fwd_op, no_grad_set, grad_to_var, grad_block);
189 190
      return maker();
    };
S
sneaxiy 已提交
191 192

    info->use_default_grad_op_desc_maker_ =
H
hong 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        std::is_base_of<DefaultGradOpMaker<OpDesc, true>, T>::value ||
        std::is_base_of<DefaultGradOpMaker<OpDesc, false>, T>::value;
  }
};

template <typename T>
struct OpInfoFiller<T, kGradOpBaseMaker> {
  void operator()(const char* op_type, OpInfo* info) const {
    info->dygraph_grad_op_maker_ = [](
        const imperative::OpBase* fw_op_base,
        const imperative::NameVarBaseMap& var_base_map_in,
        const imperative::NameVarBaseMap& var_base_map_out) {
      T maker(fw_op_base, var_base_map_in, var_base_map_out);
      return maker();
    };
208 209
  }
};
Y
Yu Yang 已提交
210 211 212 213

template <typename T>
struct OpInfoFiller<T, kVarTypeInference> {
  void operator()(const char* op_type, OpInfo* info) const {
M
minqiyang 已提交
214
    info->infer_var_type_ = [](InferVarTypeContext* context) {
Y
Yu Yang 已提交
215
      T inference;
M
minqiyang 已提交
216
      inference(context);
Y
Yu Yang 已提交
217 218 219 220
    };
  }
};

221 222 223 224 225 226 227 228 229 230
template <typename T>
struct OpInfoFiller<T, kShapeInference> {
  void operator()(const char* op_type, OpInfo* info) const {
    info->infer_shape_ = [](InferShapeContext* ctx) {
      T inference;
      inference(ctx);
    };
  }
};

D
dzhwinter 已提交
231 232 233
template <typename T>
struct OpInfoFiller<T, kInplaceOpInference> {
  void operator()(const char* op_type, OpInfo* info) const {
234
    info->infer_inplace_ = [](const OpDesc& op_desc, bool use_cuda) {
D
dzhwinter 已提交
235
      T infer;
236
      return infer(op_desc, use_cuda);
D
dzhwinter 已提交
237 238 239 240
    };
  }
};

S
sneaxiy 已提交
241 242 243 244 245 246 247 248 249 250 251 252
template <typename T>
struct OpInfoFiller<T, kNoNeedBufferVarsInference> {
  void operator()(const char* op_type, OpInfo* info) const {
    info->infer_no_need_buffer_vars_ = [](const VariableNameMap& inputs,
                                          const VariableNameMap& outputs,
                                          const AttributeMap& attrs) {
      T infer(inputs, outputs, attrs);
      return infer();
    };
  }
};

253 254 255 256 257 258
// A fake OpInfoFiller of void
template <>
struct OpInfoFiller<void, kUnknown> {
  void operator()(const char* op_type, OpInfo* info) const {}
};

259 260 261 262
}  // namespace details

}  // namespace framework
}  // namespace paddle