|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | +#include <functional> |
| 17 | +#include <map> |
| 18 | +#include <string> |
| 19 | +#include <unordered_map> |
| 20 | + |
| 21 | +#include "paddle/framework/attribute.h" |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace framework { |
| 25 | +class OperatorBase; |
| 26 | +using VariableNameMap = std::map<std::string, std::vector<std::string>>; |
| 27 | + |
| 28 | +using OpCreator = std::function<OperatorBase*( |
| 29 | + const std::string& /*type*/, const VariableNameMap& /*inputs*/, |
| 30 | + const VariableNameMap& /*outputs*/, const AttributeMap& /*attrs*/)>; |
| 31 | + |
| 32 | +struct OpInfo { |
| 33 | + OpCreator creator_; |
| 34 | + std::string grad_op_type_; |
| 35 | + OpProto* proto_; |
| 36 | + OpAttrChecker* checker_; |
| 37 | + |
| 38 | + bool HasOpProtoAndChecker() const { |
| 39 | + return proto_ != nullptr && checker_ != nullptr; |
| 40 | + } |
| 41 | + |
| 42 | + const OpProto& Proto() const { |
| 43 | + PADDLE_ENFORCE_NOT_NULL(proto_, "Operator Proto has not been registered"); |
| 44 | + PADDLE_ENFORCE(proto_->IsInitialized(), |
| 45 | + "Operator Proto must be initialized in op info"); |
| 46 | + return *proto_; |
| 47 | + } |
| 48 | + |
| 49 | + const OpAttrChecker& Checker() const { |
| 50 | + PADDLE_ENFORCE_NOT_NULL(checker_, |
| 51 | + "Operator Checker has not been registered"); |
| 52 | + return *checker_; |
| 53 | + } |
| 54 | + |
| 55 | + const OpCreator& Creator() const { |
| 56 | + PADDLE_ENFORCE_NOT_NULL(creator_, |
| 57 | + "Operator Creator has not been registered"); |
| 58 | + return creator_; |
| 59 | + } |
| 60 | + |
| 61 | + bool HasGradientOp() const { return !grad_op_type_.empty(); } |
| 62 | +}; |
| 63 | + |
| 64 | +class OpInfoMap { |
| 65 | + public: |
| 66 | + static OpInfoMap& Instance(); |
| 67 | + |
| 68 | + OpInfoMap(const OpInfoMap& o) = delete; |
| 69 | + OpInfoMap(OpInfoMap&& o) = delete; |
| 70 | + OpInfoMap& operator=(const OpInfoMap& o) = delete; |
| 71 | + OpInfoMap& operator=(OpInfoMap&& o) = delete; |
| 72 | + |
| 73 | + bool Has(const std::string& op_type) const { |
| 74 | + return map_.find(op_type) != map_.end(); |
| 75 | + } |
| 76 | + |
| 77 | + void Insert(const std::string& type, const OpInfo& info) { |
| 78 | + PADDLE_ENFORCE(!Has(type), "Operator %s has been registered", type); |
| 79 | + map_.insert({type, info}); |
| 80 | + } |
| 81 | + |
| 82 | + const OpInfo& Get(const std::string& type) const { |
| 83 | + auto it = map_.find(type); |
| 84 | + PADDLE_ENFORCE(it != map_.end(), "Operator %s are not found", type); |
| 85 | + return it->second; |
| 86 | + } |
| 87 | + |
| 88 | + template <typename Callback> |
| 89 | + void IterAllInfo(Callback callback) { |
| 90 | + for (auto& it : map_) { |
| 91 | + callback(it.first, it.second); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private: |
| 96 | + OpInfoMap() = default; |
| 97 | + std::unordered_map<std::string, const OpInfo> map_; |
| 98 | +}; |
| 99 | + |
| 100 | +} // namespace framework |
| 101 | +} // namespace paddle |
0 commit comments