layer.cc 9.4 KB
Newer Older
J
Jiabin Yang 已提交
1
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
//
// 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/fluid/imperative/layer.h"
16
#include <algorithm>
J
Jiabin Yang 已提交
17
#include <queue>
18 19
#include <utility>
#include "paddle/fluid/framework/op_registry.h"
J
Jiabin Yang 已提交
20 21 22
#include "paddle/fluid/framework/variable_helper.h"
#include "paddle/fluid/imperative/prepared_operator.h"
#include "paddle/fluid/operators/math/math_function.h"
M
minqiyang 已提交
23
#include "paddle/fluid/platform/device_context.h"
J
Jiabin Yang 已提交
24
#include "paddle/fluid/platform/enforce.h"
C
chengduo 已提交
25
#include "paddle/fluid/platform/profiler.h"
26 27 28 29

namespace paddle {
namespace imperative {

J
Jiabin Yang 已提交
30
using framework::Variable;
Z
Zeng Jinle 已提交
31 32 33 34 35 36 37 38
void ThreadSafeNameSet::Insert(const std::string& name) {
  std::lock_guard<std::mutex> guard(mtx_);
  set_.insert(name);
}

void ThreadSafeNameSet::Remove(const std::string& name) {
  std::lock_guard<std::mutex> guard(mtx_);
  auto iter = set_.find(name);
J
Jiabin Yang 已提交
39
  PADDLE_ENFORCE_EQ(iter != set_.end(), true, "%s does not exist", name);
Z
Zeng Jinle 已提交
40 41 42 43 44 45 46 47 48 49 50 51
  set_.erase(iter);
}

std::vector<std::string> ThreadSafeNameSet::Names() const {
  std::lock_guard<std::mutex> guard(mtx_);
  return std::vector<std::string>(set_.begin(), set_.end());
}

ThreadSafeNameSet VarBase::name_set_;

std::vector<std::string> VarBase::AliveVarNames() { return name_set_.Names(); }

J
Jiabin Yang 已提交
52 53 54 55
static framework::VariableNameMap CreateVarNameMap(
    const framework::OpInfo& op_info, const std::string& op_type,
    const NameVarBaseMap& varbase_map, bool is_input) {
  if (op_info.proto_ == nullptr) {
H
hong 已提交
56 57 58 59 60 61 62 63 64 65 66 67
    framework::VariableNameMap result;

    for (auto& it : varbase_map) {
      auto& var_vector = it.second;
      std::vector<std::string> args;
      args.reserve(var_vector.size());
      for (auto& var_base : var_vector) {
        args.emplace_back(var_base->Name());
      }
      result[it.first] = std::move(args);
    }
    return result;
M
minqiyang 已提交
68 69
  }

J
Jiabin Yang 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  framework::VariableNameMap result;

  for (auto& var :
       is_input ? op_info.Proto().inputs() : op_info.Proto().outputs()) {
    auto it = varbase_map.find(var.name());
    if (it == varbase_map.end()) {
      PADDLE_ENFORCE_EQ(
          var.dispensable(), true,
          "Var: %s not dispensable and there are no such var in inputs",
          var.name());
      result[var.name()] = {};
    } else {
      auto& var_vector = it->second;
      std::vector<std::string> args;
      args.reserve(var_vector.size());
      for (auto& var_base : var_vector) {
        args.emplace_back(var_base->Name());
      }
      result[var.name()] = std::move(args);
    }
M
minqiyang 已提交
90
  }
J
Jiabin Yang 已提交
91 92
  return result;
}
M
minqiyang 已提交
93

J
Jiabin Yang 已提交
94 95 96 97 98 99 100 101 102
static framework::RuntimeContext PrepareRuntimeContext(
    const NameVarBaseMap& ins, const NameVarBaseMap& outs) {
  framework::VariableValueMap inputs, outputs;
  for (auto& in_pair : ins) {
    auto& in_ctx = inputs[in_pair.first];
    in_ctx.reserve(in_pair.second.size());
    for (auto& in_var : in_pair.second) {
      in_ctx.emplace_back(in_var->MutableVar());
    }
M
minqiyang 已提交
103 104
  }

J
Jiabin Yang 已提交
105 106 107 108 109
  for (auto& out_pair : outs) {
    auto& out_ctx = outputs[out_pair.first];
    out_ctx.reserve(out_pair.second.size());
    for (auto& out_var : out_pair.second) {
      out_ctx.emplace_back(out_var->MutableVar());
110
    }
J
Jiabin Yang 已提交
111 112 113 114 115 116 117 118 119
  }
  return framework::RuntimeContext(std::move(inputs), std::move(outputs));
}

static std::string DebugString(
    const std::string& name,
    const std::vector<std::shared_ptr<VarBase>>& vars) {
  std::stringstream ss;
  ss << name << "{";
M
minqiyang 已提交
120

J
Jiabin Yang 已提交
121 122
  for (size_t i = 0; i < vars.size(); ++i) {
    if (i > 0) ss << ", ";
M
minqiyang 已提交
123

J
Jiabin Yang 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    if (vars[i] == nullptr) {
      ss << "NULL";
      continue;
    }
    ss << vars[i]->Name() << "[";
    auto& var = vars[i]->Var();
    if (!var.IsInitialized()) {
      ss << "NOT_INITED_VAR";
    } else if (var.IsType<framework::LoDTensor>()) {
      auto& tensor = var.Get<framework::LoDTensor>();
      ss << "LoDTensor<";
      if (tensor.IsInitialized()) {
        ss << framework::DataTypeToString(tensor.type()) << ", ";
        ss << tensor.place() << ", ";
        ss << "(" << tensor.dims() << ")";
      } else {
        ss << "NOT_INITED";
      }
      ss << ">";
    } else {
      ss << "UNRESOLVED_TYPE";
    }
    ss << "]";
147
  }
148

J
Jiabin Yang 已提交
149 150
  ss << "}";
  return ss.str();
151 152
}

J
Jiabin Yang 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
std::string LayerDebugString(const std::string& op_type,
                             const NameVarBaseMap& ins,
                             const NameVarBaseMap& outs) {
  std::stringstream ss;
  ss << "Op(" << op_type << "): ";

  ss << "Inputs: ";

  size_t i = 0;
  for (auto& pair : ins) {
    if (i > 0) ss << ", ";
    ss << DebugString(pair.first, pair.second);
    ++i;
166 167
  }

J
Jiabin Yang 已提交
168 169 170 171 172 173 174 175 176
  ss << ",   Outputs: ";
  i = 0;
  for (auto& pair : outs) {
    if (i > 0) ss << ", ";
    ss << DebugString(pair.first, pair.second);
    ++i;
  }
  return ss.str();
}
177

J
Jiabin Yang 已提交
178 179 180 181 182 183
void VarBase::AddGradOps(const std::weak_ptr<OpBase>& op) {
  if (op.lock() == nullptr) {
    return;
  }
  for (const auto& cur_op : grad_ops_) {
    if (cur_op.lock() == op.lock()) {
184 185
      return;
    }
186
  }
J
Jiabin Yang 已提交
187 188
  grad_ops_.emplace_back(op);
}
189

J
Jiabin Yang 已提交
190 191 192 193 194 195 196
void VarBase::ClearGradient() {
  if (grad_var_) {
    auto* grad_t = grad_var_->var_.GetMutable<framework::LoDTensor>();
    if (grad_t->IsInitialized()) {
      auto* dev_ctx =
          platform::DeviceContextPool::Instance().Get(grad_t->place());
      operators::math::set_constant(*dev_ctx, grad_t, 0.0);
197 198
    }
  }
J
Jiabin Yang 已提交
199
}
200

J
Jiabin Yang 已提交
201
std::shared_ptr<VarBase> VarBase::NewVarBase(const platform::Place& dst_place,
M
minqiyang 已提交
202
                                             const bool blocking) const {
J
Jiabin Yang 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  PADDLE_ENFORCE_EQ(var_.IsInitialized() && var_.IsType<framework::LoDTensor>(),
                    true,
                    "Variable must be initialized and type of LoDTensor when "
                    "getting numpy tensor");

  auto& src_tensor = var_.Get<framework::LoDTensor>();

  // TODO(Jiabin): change this after move unique_name generator to CXX
  auto new_var = std::make_shared<VarBase>(
      false, "Itmp" + std::to_string(copied_counter_++));

  auto* dst_tensor = new_var->var_.GetMutable<framework::LoDTensor>();
  dst_tensor->set_lod(src_tensor.lod());

  framework::TensorCopy(src_tensor, dst_place, dst_tensor);
P
Paddle CI 已提交
218
  if (blocking) {
219 220 221 222 223
    platform::DeviceContextPool::Instance().Get(dst_place)->Wait();
    auto src_place = src_tensor.place();
    if (!(src_place == dst_place)) {
      platform::DeviceContextPool::Instance().Get(src_place)->Wait();
    }
P
Paddle CI 已提交
224 225 226
  }

  if (platform::is_gpu_place(dst_place)) {
227
    VLOG(3) << "copy tensor " << Name() << " from gpu";
M
minqiyang 已提交
228 229
  }

P
Paddle CI 已提交
230
  return new_var;
M
minqiyang 已提交
231
}
J
Jiabin Yang 已提交
232 233
// create OpBase from optype
OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins,
H
hong 已提交
234
               const NameVarBaseMap& outs, const framework::AttributeMap& attrs,
J
Jiabin Yang 已提交
235
               const platform::Place& place)
H
hong 已提交
236
    : id_(id), place_(place), attrs_(attrs) {
J
Jiabin Yang 已提交
237 238 239 240
  const auto& info = framework::OpInfoMap::Instance().Get(type);

  // Step 1: Run forward
  if (info.Checker() != nullptr) {
H
hong 已提交
241
    info.Checker()->Check(&attrs_);
J
Jiabin Yang 已提交
242
  }
M
minqiyang 已提交
243

J
Jiabin Yang 已提交
244 245 246
  auto input_name_map = CreateVarNameMap(info, type, ins, true);
  auto output_name_map = CreateVarNameMap(info, type, outs, false);
  op_ = framework::OpRegistry::CreateOp(type, std::move(input_name_map),
H
hong 已提交
247
                                        std::move(output_name_map), attrs);
J
Jiabin Yang 已提交
248
  VLOG(3) << "Construct Op: " << type << std::endl;
249 250
}

J
Jiabin Yang 已提交
251 252 253 254 255 256
// create OpBase from opdesc
OpBase::OpBase(size_t id, const framework::OpDesc& op_desc,
               const platform::Place& place)
    : id_(id), op_(framework::OpRegistry::CreateOp(op_desc)), place_(place) {
  VLOG(3) << "Construct Op: " << op_desc.Type() << std::endl;
}
257

H
hong 已提交
258 259 260 261 262 263 264 265 266 267 268 269
void OpBase::CreateOperatorBase() {
  const auto& info = framework::OpInfoMap::Instance().Get(type_);
  if (info.Checker() != nullptr) {
    info.Checker()->Check(&attrs_);
  }

  auto input_name_map = CreateVarNameMap(info, type_, ins_, true);
  auto output_name_map = CreateVarNameMap(info, type_, outs_, false);
  op_ = framework::OpRegistry::CreateOp(type_, std::move(input_name_map),
                                        std::move(output_name_map), attrs_);
}

J
Jiabin Yang 已提交
270 271 272 273 274 275 276
void OpBase::Run(const NameVarBaseMap& ins, const NameVarBaseMap& outs) {
  auto* op_kernel = dynamic_cast<framework::OperatorWithKernel*>(op_.get());
  PADDLE_ENFORCE_NOT_NULL(op_kernel, "only support op with kernel");
  auto& info = op_->Info();
  if (info.infer_var_type_) {
    RuntimeInferVarTypeContext infer_var_type_ctx(ins, &outs, op_->Attrs());
    info.infer_var_type_(&infer_var_type_ctx);
X
Xin Pan 已提交
277
  }
X
Xin Pan 已提交
278

J
Jiabin Yang 已提交
279 280 281 282
  // Initialize output var type
  for (auto& var_pair : outs) {
    for (auto& var : var_pair.second) {
      InitializeVariable(var->MutableVar(), var->Type());
283 284
    }
  }
X
Xin Pan 已提交
285

J
Jiabin Yang 已提交
286 287 288
  VLOG(3) << "Running Op " << Type();
  VLOG(5) << LayerDebugString(Type(), ins, outs);
  auto runtime_ctx = PrepareRuntimeContext(ins, outs);
289

290 291
  VLOG(6) << "start preparing op: " << Type();
  auto prepared_op = PreparedOp::Prepare(runtime_ctx, *op_kernel, place(), ins);
292

293
  VLOG(6) << "finish preparing op: " << Type();
J
Jiabin Yang 已提交
294
  prepared_op.Run();
295

J
Jiabin Yang 已提交
296
  VLOG(4) << LayerDebugString(Type(), ins, outs);
297 298
}

J
Jiabin Yang 已提交
299 300 301 302
void OpBase::ClearBackwardTrace() {
  grad_pending_ops_.clear();
  ins_.clear();
  outs_.clear();
303 304 305 306
}

}  // namespace imperative
}  // namespace paddle