op_registry.h 5.0 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 19
#include <string>
#include <tuple>
#include <vector>
Y
Yi Wang 已提交
20 21 22 23 24
#include "paddle/fluid/framework/grad_op_desc_maker.h"
#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"
25 26 27 28 29 30 31 32

namespace paddle {
namespace framework {
namespace details {

enum OpInfoFillType {
  kOperator = 0,
  kOpProtoAndCheckerMaker = 1,
Y
Yu Yang 已提交
33
  kGradOpDescMaker = 2,
34
  kVarTypeInference = 3,
Y
Yu Yang 已提交
35 36 37
  kShapeInference = 4,
  kEstimateFlops = 5,
  kUnknown = -1
38 39 40 41 42 43 44 45 46 47 48
};

template <typename T>
struct OpInfoFillTypeID {
  static constexpr OpInfoFillType ID() {
    return std::is_base_of<OperatorBase, T>::value
               ? kOperator
               : (std::is_base_of<OpProtoAndCheckerMaker, T>::value
                      ? kOpProtoAndCheckerMaker
                      : (std::is_base_of<GradOpDescMakerBase, T>::value
                             ? kGradOpDescMaker
Y
Yu Yang 已提交
49 50
                             : (std::is_base_of<VarTypeInference, T>::value
                                    ? kVarTypeInference
51 52
                                    : (std::is_base_of<InferShapeBase, T>::value
                                           ? kShapeInference
Y
Yu Yang 已提交
53 54 55 56
                                           : (std::is_base_of<EstimateFlopsBase,
                                                              T>::value
                                                  ? kEstimateFlops
                                                  : kUnknown)))));
57 58 59 60 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
  }
};

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 {
100
    info->proto_ = new proto::OpProto;
101
    info->checker_ = new OpAttrChecker();
Y
Yu Yang 已提交
102
    T maker;
Y
yuyang18 已提交
103
    maker(info->proto_, info->checker_);
104 105 106 107 108 109 110 111 112 113 114
    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 {
115
    info->grad_op_maker_ = [](
Y
Yu Yang 已提交
116
        const OpDesc& fwd_op,
117
        const std::unordered_set<std::string>& no_grad_set,
Y
Yu Yang 已提交
118
        std::unordered_map<std::string, std::string>* grad_to_var,
Y
Yu Yang 已提交
119
        const std::vector<BlockDesc*>& grad_block) {
Y
Yu Yang 已提交
120
      T maker(fwd_op, no_grad_set, grad_to_var, grad_block);
121 122
      return maker();
    };
123 124
  }
};
Y
Yu Yang 已提交
125 126 127 128

template <typename T>
struct OpInfoFiller<T, kVarTypeInference> {
  void operator()(const char* op_type, OpInfo* info) const {
Y
Yu Yang 已提交
129
    info->infer_var_type_ = [](const OpDesc& fwd_op, BlockDesc* block) {
Y
Yu Yang 已提交
130 131 132 133 134 135
      T inference;
      inference(fwd_op, block);
    };
  }
};

136 137 138 139 140 141 142 143 144 145
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);
    };
  }
};

Y
Yu Yang 已提交
146 147 148 149 150 151 152 153 154 155
template <typename T>
struct OpInfoFiller<T, kEstimateFlops> {
  void operator()(const char* op_tpe, OpInfo* info) const {
    info->estimate_flops_ = [](InferShapeContext* ctx) {
      T estimate_flops;
      return estimate_flops(ctx);
    };
  }
};

156 157 158 159
}  // namespace details

}  // namespace framework
}  // namespace paddle