op_registry.cc 2.6 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

17 18
#include "glog/logging.h"

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

Y
Yu Yang 已提交
22
std::unique_ptr<OperatorBase> OpRegistry::CreateOp(
23 24 25 26
    const std::string& type,
    const VariableNameMap& inputs,
    const VariableNameMap& outputs,
    const AttributeMap& attrs,
27
    bool attr_check) {
Y
Yu Yang 已提交
28
  auto& info = OpInfoMap::Instance().Get(type);
H
hong 已提交
29
  if (attr_check && info.Checker() != nullptr) {
30 31 32 33
    auto tmp_attrs = attrs;
    info.Checker()->Check(&tmp_attrs);
    return std::unique_ptr<OperatorBase>(
        info.Creator()(type, inputs, outputs, tmp_attrs));
Y
Stash  
Yu Yang 已提交
34
  }
35 36
  return std::unique_ptr<OperatorBase>(
      info.Creator()(type, inputs, outputs, attrs));
37 38
}

Y
Yu Yang 已提交
39
static VariableNameMap ConvertOpDescVarsToVarNameMap(
40 41
    const google::protobuf::RepeatedPtrField<proto::OpDesc::Var>&
        op_desc_vars) {
Y
Yu Yang 已提交
42
  VariableNameMap ret_val;
43 44 45 46
  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()));
47 48
    std::copy(var_names_in_proto.begin(),
              var_names_in_proto.end(),
49 50 51 52 53
              std::back_inserter(var_names));
  }
  return ret_val;
}

54 55
std::unique_ptr<OperatorBase> OpRegistry::CreateOp(
    const proto::OpDesc& op_desc) {
M
minqiyang 已提交
56 57 58
  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 已提交
59 60 61 62
  VariableNameMap inputs = ConvertOpDescVarsToVarNameMap(op_desc.inputs());
  VariableNameMap outputs = ConvertOpDescVarsToVarNameMap(op_desc.outputs());
  AttributeMap attrs;
  for (auto& attr : op_desc.attrs()) {
63
    attrs[attr.name()] = GetAttrValue(attr);
Y
Yu Yang 已提交
64 65 66 67 68
  }

  return CreateOp(op_desc.type(), inputs, outputs, attrs);
}

Y
Yu Yang 已提交
69
std::unique_ptr<OperatorBase> OpRegistry::CreateOp(const OpDesc& op_desc) {
70 71 72
  return CreateOp(op_desc.Type(),
                  op_desc.Inputs(),
                  op_desc.Outputs(),
Y
Yu Yang 已提交
73
                  op_desc.GetAttrMap());
74 75 76
}

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