operator.cc 7.6 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
#include <algorithm>
Q
Qiao Longfei 已提交
17 18 19 20

namespace paddle {
namespace framework {

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

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

Q
Qiao Longfei 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
const Tensor* GetTensorFromVar(const Variable* var) {
  if (var->IsType<LoDTensor>()) {
    return &var->Get<LoDTensor>();
  }
  PADDLE_ENFORCE(var->IsType<Tensor>(),
                 "The Input must be LoDTensor or Tensor.");
  return &var->Get<Tensor>();
}

Tensor* GetTensorFromVar(Variable* var) {
  if (var->IsType<LoDTensor>()) {
    return var->GetMutable<LoDTensor>();
  }
  PADDLE_ENFORCE(var->IsType<Tensor>(),
                 "The Input must be LoDTensor or Tensor.");
  return var->GetMutable<Tensor>();
}

53
std::string OperatorBase::Input(const std::string& name) const {
Y
Yu Yang 已提交
54
  auto& ins = Inputs(name);
Y
Yu Yang 已提交
55 56 57 58
  PADDLE_ENFORCE_LE(ins.size(), 1UL,
                    "Op %s input %s should contain only one variable", type_,
                    name);
  return ins.empty() ? kEmptyVarName : ins[0];
Y
Yan Chunwei 已提交
59 60
}

Y
Yu Yang 已提交
61 62
const std::vector<std::string>& OperatorBase::Inputs(
    const std::string& name) const {
Y
Yu Yang 已提交
63 64 65 66
  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 已提交
67 68
}

69
std::string OperatorBase::Output(const std::string& name) const {
Y
Yu Yang 已提交
70
  auto& outs = Outputs(name);
Y
Yu Yang 已提交
71 72 73 74
  PADDLE_ENFORCE_LE(outs.size(), 1UL,
                    "Op %s output %s should contain only one variable", type_,
                    name);
  return outs.empty() ? kEmptyVarName : outs[0];
Y
Yan Chunwei 已提交
75 76
}

Y
Yu Yang 已提交
77 78
const std::vector<std::string>& OperatorBase::Outputs(
    const std::string& name) const {
Y
Yu Yang 已提交
79
  auto it = outputs_.find(name);
S
superjom 已提交
80 81
  PADDLE_ENFORCE(it != outputs_.end(), "Op %s does not have output called %s",
                 type_, name);
Y
Yu Yang 已提交
82
  return it->second;
Y
Yan Chunwei 已提交
83 84
}

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

D
dongzhihong 已提交
123 124
void OperatorBase::Rename(const std::string& old_name,
                          const std::string& new_name) {
Y
Yu Yang 已提交
125 126 127 128 129 130 131
  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 已提交
132 133
}

Y
Yu Yang 已提交
134
OperatorBase::OperatorBase(const std::string& type,
Y
Yu Yang 已提交
135 136
                           const VariableNameMap& inputs,
                           const VariableNameMap& outputs,
Y
Yu Yang 已提交
137 138
                           const AttributeMap& attrs)
    : type_(type), inputs_(inputs), outputs_(outputs), attrs_(attrs) {
139 140
  GenerateTemporaryNames();
  CheckAllInputOutputSet();
Y
Yu Yang 已提交
141
}
142

Q
qijun 已提交
143 144 145 146 147 148 149 150 151
std::vector<std::string> OperatorBase::InputVars() const {
  std::vector<std::string> 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 已提交
152 153 154 155 156 157 158 159 160 161
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 已提交
162
  auto& info = OpInfoMap::Instance().Get(Type());
Y
Yu Yang 已提交
163 164

  // get all OpProto::Var for outputs
Y
Yu Yang 已提交
165
  for (auto& o : info.Proto().outputs()) {
Y
Yu Yang 已提交
166 167 168 169 170 171 172 173 174
    // 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 已提交
175 176
}

177 178 179
void OperatorBase::CheckAllInputOutputSet() const {
  auto& info_map = OpInfoMap::Instance();
  auto* op_info = info_map.GetNullable(Type());
Y
Yu Yang 已提交
180
  if (op_info == nullptr || op_info->proto_ == nullptr) return;
181 182 183

  for (auto& in : op_info->Proto().inputs()) {
    PADDLE_ENFORCE(inputs_.find(in.name()) != inputs_.end(),
Y
Yu Yang 已提交
184
                   "Type %s's input %s is not set", Type(), in.name());
185 186 187 188
  }

  for (auto& out : op_info->Proto().outputs()) {
    PADDLE_ENFORCE(outputs_.find(out.name()) != outputs_.end(),
Y
Yu Yang 已提交
189
                   "Type %s's output %s is not set", Type(), out.name());
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  }
}

void OperatorBase::GenerateTemporaryNames() {
  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));
      }
    }
  }
}

206 207 208
template <>
const Tensor* InferShapeContext::Input<Tensor>(const std::string& name) const {
  auto* var = InputVar(name);
209
  return var == nullptr ? nullptr : GetTensorFromVar(var);
210 211 212 213 214 215 216 217
}

template <>
const std::vector<const Tensor*> InferShapeContext::MultiInput<Tensor>(
    const std::string& name) const {
  auto names = op().Inputs(name);
  std::vector<const Tensor*> res;
  res.reserve(names.size());
218 219 220 221 222
  std::transform(names.begin(), names.end(), std::back_inserter(res),
                 [&](const std::string& sub_name) {
                   auto var = scope_.FindVar(sub_name);
                   return var == nullptr ? nullptr : GetTensorFromVar(var);
                 });
223 224 225 226
  return res;
}

template <>
227 228 229
Tensor* InferShapeContext::Output<Tensor>(const std::string& name) const {
  auto var = OutputVar(name);
  return var == nullptr ? nullptr : var->GetMutable<LoDTensor>();
230 231 232
}

template <>
233
std::vector<Tensor*> InferShapeContext::MultiOutput<Tensor>(
234 235 236 237
    const std::string& name) const {
  auto names = op().Outputs(name);
  std::vector<Tensor*> res;
  res.reserve(names.size());
238 239
  std::transform(names.begin(), names.end(), std::back_inserter(res),
                 [&](const std::string& sub_name) {
240 241 242
                   auto var = scope_.FindVar(sub_name);
                   return var == nullptr ? nullptr
                                         : var->GetMutable<LoDTensor>();
243
                 });
244 245 246
  return res;
}

Q
Qiao Longfei 已提交
247
}  // namespace framework
L
liaogang 已提交
248
}  // namespace paddle