op_registry.cc 4.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/op_registry.h"
16
#include "paddle/fluid/operators/ops_extra_info.h"
17

18 19
#include "glog/logging.h"

Y
Yi Wang 已提交
20
namespace paddle {
21 22
namespace framework {

Y
Yu Yang 已提交
23
std::unique_ptr<OperatorBase> OpRegistry::CreateOp(
24 25 26 27
    const std::string& type,
    const VariableNameMap& inputs,
    const VariableNameMap& outputs,
    const AttributeMap& attrs,
28
    bool attr_check) {
29 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
  AttributeMap standard_attrs;
  AttributeMap runtime_attrs =
      paddle::operators::ExtraInfoUtils::Instance().GetExtraAttrsMap(type);
  for (auto& attr : attrs) {
    auto it = runtime_attrs.find(attr.first);
    if (it != runtime_attrs.end()) {
      it->second = attr.second;
    } else {
      standard_attrs[attr.first] = attr.second;
    }
  }
  auto& info = OpInfoMap::Instance().Get(type);
  if (attr_check && info.Checker() != nullptr) {
    info.Checker()->Check(&standard_attrs);
  }
  auto op_base = std::unique_ptr<OperatorBase>(
      info.Creator()(type, inputs, outputs, standard_attrs));
  op_base->SetRuntimeAttributeMap(runtime_attrs);
  return op_base;
}

std::unique_ptr<OperatorBase> OpRegistry::CreateOp(
    const std::string& type,
    const VariableNameMap& inputs,
    const VariableNameMap& outputs,
    const AttributeMap& attrs,
    const AttributeMap& runtime_attrs,
    bool attr_check) {
  std::unique_ptr<OperatorBase> op_base;
Y
Yu Yang 已提交
58
  auto& info = OpInfoMap::Instance().Get(type);
H
hong 已提交
59
  if (attr_check && info.Checker() != nullptr) {
60 61
    auto tmp_attrs = attrs;
    info.Checker()->Check(&tmp_attrs);
62
    op_base = std::unique_ptr<OperatorBase>(
63
        info.Creator()(type, inputs, outputs, tmp_attrs));
64 65 66
  } else {
    op_base = std::unique_ptr<OperatorBase>(
        info.Creator()(type, inputs, outputs, attrs));
Y
Stash  
Yu Yang 已提交
67
  }
68 69
  op_base->SetRuntimeAttributeMap(runtime_attrs);
  return op_base;
70 71
}

Y
Yu Yang 已提交
72
static VariableNameMap ConvertOpDescVarsToVarNameMap(
73 74
    const google::protobuf::RepeatedPtrField<proto::OpDesc::Var>&
        op_desc_vars) {
Y
Yu Yang 已提交
75
  VariableNameMap ret_val;
76 77 78 79
  for (auto& var : op_desc_vars) {
    auto& var_names = ret_val[var.parameter()];
    auto& var_names_in_proto = var.arguments();
    var_names.reserve(static_cast<size_t>(var_names_in_proto.size()));
80 81
    std::copy(var_names_in_proto.begin(),
              var_names_in_proto.end(),
82 83 84 85 86
              std::back_inserter(var_names));
  }
  return ret_val;
}

87 88
std::unique_ptr<OperatorBase> OpRegistry::CreateOp(
    const proto::OpDesc& op_desc) {
M
minqiyang 已提交
89 90 91
  VLOG(1) << "CreateOp directly from OpDesc is deprecated. It should only be"
             "used in unit tests. Use CreateOp(const OpDesc& op_desc) "
             "instead.";
Y
Yu Yang 已提交
92 93 94
  VariableNameMap inputs = ConvertOpDescVarsToVarNameMap(op_desc.inputs());
  VariableNameMap outputs = ConvertOpDescVarsToVarNameMap(op_desc.outputs());
  AttributeMap attrs;
95 96 97
  AttributeMap extra_attrs =
      paddle::operators::ExtraInfoUtils::Instance().GetExtraAttrsMap(
          op_desc.type());
Y
Yu Yang 已提交
98
  for (auto& attr : op_desc.attrs()) {
99 100 101 102 103 104
    auto it = extra_attrs.find(attr.name());
    if (it != extra_attrs.end()) {
      it->second = GetAttrValue(attr);
    } else {
      attrs[attr.name()] = GetAttrValue(attr);
    }
Y
Yu Yang 已提交
105 106
  }

107
  return CreateOp(op_desc.type(), inputs, outputs, attrs, extra_attrs);
Y
Yu Yang 已提交
108 109
}

Y
Yu Yang 已提交
110
std::unique_ptr<OperatorBase> OpRegistry::CreateOp(const OpDesc& op_desc) {
111 112 113
  return CreateOp(op_desc.Type(),
                  op_desc.Inputs(),
                  op_desc.Outputs(),
114 115
                  op_desc.GetAttrMap(),
                  op_desc.GetRuntimeAttrMap());
116 117 118
}

}  // namespace framework
L
liaogang 已提交
119
}  // namespace paddle