operator.cc 6.0 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

#include "paddle/framework/operator.h"
16 17
#include <algorithm>
#include "paddle/framework/op_registry.h"
Q
Qiao Longfei 已提交
18 19 20 21

namespace paddle {
namespace framework {

Q
qijun 已提交
22
template <>
23
Eigen::DefaultDevice& ExecutionContext::GetEigenDevice<
Q
qijun 已提交
24
    platform::CPUPlace, Eigen::DefaultDevice>() const {
D
dongzhihong 已提交
25
  return *device_context_->get_eigen_device<Eigen::DefaultDevice>();
Q
qijun 已提交
26 27 28 29
}

#ifndef PADDLE_ONLY_CPU
template <>
30
Eigen::GpuDevice&
31
ExecutionContext::GetEigenDevice<platform::GPUPlace, Eigen::GpuDevice>() const {
D
dongzhihong 已提交
32
  return *device_context_->get_eigen_device<Eigen::GpuDevice>();
Q
qijun 已提交
33 34 35
}
#endif

36
std::string OperatorBase::Input(const std::string& name) const {
Y
Yu Yang 已提交
37
  auto& ins = Inputs(name);
38 39 40 41 42 43 44 45 46 47
  switch (ins.size()) {
    case 0:
      return kEmptyVarName;
    case 1:
      return ins[0];
    default:
      PADDLE_THROW("Op %s input %s should contain only one variable", type_,
                   name);
      return "";
  }
Y
Yan Chunwei 已提交
48 49
}

Y
Yu Yang 已提交
50 51
const std::vector<std::string>& OperatorBase::Inputs(
    const std::string& name) const {
Y
Yu Yang 已提交
52 53 54 55
  auto it = inputs_.find(name);
  PADDLE_ENFORCE(it != inputs_.end(), "Op %s do not have input %s", type_,
                 name);
  return it->second;
Y
Yan Chunwei 已提交
56 57
}

58
std::string OperatorBase::Output(const std::string& name) const {
Y
Yu Yang 已提交
59
  auto& outs = Outputs(name);
60 61 62 63 64 65 66 67 68 69
  switch (outs.size()) {
    case 0:
      return kEmptyVarName;
    case 1:
      return outs[0];
    default:
      PADDLE_THROW("Op %s output %s should contain only one variable", type_,
                   name);
      return "";
  }
Y
Yan Chunwei 已提交
70 71
}

Y
Yu Yang 已提交
72 73
const std::vector<std::string>& OperatorBase::Outputs(
    const std::string& name) const {
Y
Yu Yang 已提交
74 75 76 77
  auto it = outputs_.find(name);
  PADDLE_ENFORCE(it != outputs_.end(), "Op %s does not have output %s", type_,
                 name);
  return it->second;
Y
Yan Chunwei 已提交
78 79
}

Q
Qiao Longfei 已提交
80 81
std::string OperatorBase::DebugString() const {
  std::stringstream ss;
Y
Yu Yang 已提交
82
  ss << "Op(" << type_ << "), inputs:{";
Y
Yu Yang 已提交
83 84
  for (auto it = inputs_.begin(); it != inputs_.end();) {
    auto& input = *it;
Y
Yu Yang 已提交
85 86 87 88 89 90
    ss << input.first << "[";
    for (size_t i = 0; i < input.second.size(); ++i) {
      ss << input.second[i];
      if (i != input.second.size() - 1) {
        ss << ", ";
      }
91
    }
Y
Yu Yang 已提交
92
    ss << "]";
Y
Yu Yang 已提交
93 94
    ++it;
    if (it != inputs_.end()) {
95 96
      ss << ", ";
    }
Q
Qiao Longfei 已提交
97
  }
Y
Yu Yang 已提交
98
  ss << "}, outputs:{";
Y
Yu Yang 已提交
99 100
  for (auto it = outputs_.begin(); it != outputs_.end();) {
    auto& output = *it;
Y
Yu Yang 已提交
101 102 103 104 105 106
    ss << output.first << "[";
    for (size_t i = 0; i < output.second.size(); ++i) {
      ss << output.second[i];
      if (i != output.second.size() - 1) {
        ss << ", ";
      }
107
    }
Y
Yu Yang 已提交
108
    ss << "]";
Y
Yu Yang 已提交
109 110
    ++it;
    if (it != outputs_.end()) {
111 112
      ss << ", ";
    }
Q
Qiao Longfei 已提交
113
  }
Y
Yu Yang 已提交
114
  ss << "}.";
Q
Qiao Longfei 已提交
115 116 117
  return ss.str();
}

D
dongzhihong 已提交
118 119
void OperatorBase::Rename(const std::string& old_name,
                          const std::string& new_name) {
Y
Yu Yang 已提交
120 121 122 123 124 125 126
  for (auto& input : inputs_) {
    std::replace(input.second.begin(), input.second.end(), old_name, new_name);
  }
  for (auto& output : outputs_) {
    std::replace(output.second.begin(), output.second.end(), old_name,
                 new_name);
  }
D
dongzhihong 已提交
127 128
}

Y
Yu Yang 已提交
129
OperatorBase::OperatorBase(const std::string& type,
Y
Yu Yang 已提交
130 131
                           const VariableNameMap& inputs,
                           const VariableNameMap& outputs,
Y
Yu Yang 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
                           const AttributeMap& attrs)
    : type_(type), inputs_(inputs), outputs_(outputs), attrs_(attrs) {
  static std::atomic<size_t> gUniqId(0UL);
  for (auto& output : outputs_) {
    for (auto& output_name : output.second) {
      if (output_name == kTempVarName) {
        output_name += type_;
        output_name += "@";
        output_name += std::to_string(gUniqId.fetch_add(1));
      }
    }
  }
}
145

Y
Yu Yang 已提交
146 147 148 149 150 151 152 153 154 155
std::vector<std::string> OperatorBase::OutputVars(bool has_intermediate) const {
  std::vector<std::string> ret_val;
  if (has_intermediate) {
    // push all outputs into ret_val
    for (auto& o : outputs_) {
      ret_val.reserve(ret_val.size() + o.second.size());
      ret_val.insert(ret_val.end(), o.second.begin(), o.second.end());
    }
    return ret_val;
  }
Y
Yu Yang 已提交
156
  auto& info = OpInfoMap::Instance().Get(Type());
Y
Yu Yang 已提交
157 158

  // get all OpProto::Var for outputs
Y
Yu Yang 已提交
159
  for (auto& o : info.Proto().outputs()) {
Y
Yu Yang 已提交
160 161 162 163 164 165 166 167 168
    // ignore all intermediate output
    if (o.intermediate()) continue;
    auto out = outputs_.find(o.name());
    if (out != outputs_.end()) {
      ret_val.reserve(ret_val.size() + out->second.size());
      ret_val.insert(ret_val.end(), out->second.begin(), out->second.end());
    }
  }
  return ret_val;
D
dongzhihong 已提交
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
void OpProtoAndCheckerMaker::Validate() {
  validated_ = true;
  CheckNoDuplicatedInOutAttrs();
}

OpProtoAndCheckerMaker::VariableBuilder OpProtoAndCheckerMaker::AddInput(
    const std::string& name, const std::string& comment) {
  auto* input = proto_->add_inputs();
  input->set_name(name);
  input->set_comment(comment);
  return OpProtoAndCheckerMaker::VariableBuilder{input};
}

OpProtoAndCheckerMaker::VariableBuilder OpProtoAndCheckerMaker::AddOutput(
    const std::string& name, const std::string& comment) {
  auto* output = proto_->add_outputs();
  output->set_name(name);
  output->set_comment(comment);
  return OpProtoAndCheckerMaker::VariableBuilder{output};
}

void OpProtoAndCheckerMaker::CheckNoDuplicatedInOutAttrs() {
  std::unordered_set<std::string> names;
  auto checker = [&](const std::string& name) {
    PADDLE_ENFORCE(!names.count(name), "[%s] is duplicated", name);
    names.insert(name);
  };
  for (auto& attr : proto_->attrs()) {
    checker(attr.name());
  }
  for (auto& input : proto_->inputs()) {
    checker(input.name());
  }
  for (auto& output : proto_->outputs()) {
    checker(output.name());
  }
}

Q
Qiao Longfei 已提交
209
}  // namespace framework
L
liaogang 已提交
210
}  // namespace paddle