executor.cc 4.0 KB
Newer Older
Q
qijun 已提交
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/executor.h"
Y
Yang Yang 已提交
16

Y
Yang Yang 已提交
17
#include <algorithm>
Y
Yang Yang 已提交
18
#include <iostream>
Q
qijun 已提交
19
#include <memory>
Y
Yang Yang 已提交
20
#include <set>
Y
Yang Yang 已提交
21
#include <vector>
Y
Yang Yang 已提交
22

Q
QI JUN 已提交
23
#include "paddle/framework/feed_fetch_type.h"
Y
Yu Yang 已提交
24
#include "paddle/framework/lod_rank_table.h"
Y
Yang Yang 已提交
25
#include "paddle/framework/lod_tensor.h"
Y
Yu Yang 已提交
26
#include "paddle/framework/lod_tensor_array.h"
Q
qijun 已提交
27
#include "paddle/framework/op_registry.h"
Q
qijun 已提交
28
#include "paddle/framework/scope.h"
Q
qijun 已提交
29 30 31 32

namespace paddle {
namespace framework {

Y
Yang Yang 已提交
33 34 35
const std::string kFeedOpType = "feed";
const std::string kFetchOpType = "fetch";

D
dzhwinter 已提交
36
DeviceContextPool* DeviceContextPool::pool = nullptr;
Q
qijun 已提交
37

D
dzhwinter 已提交
38 39 40 41
Executor::Executor(const std::vector<platform::Place>& places) {
  DeviceContextPool& pool = DeviceContextPool::Get();
  auto borrowed_contexts = pool.Borrow(places);
  device_contexts_.swap(borrowed_contexts);
Q
qijun 已提交
42 43
}

44 45
static void CreateTensor(Variable* var, proto::VarDesc::VarType var_type) {
  if (var_type == proto::VarDesc::LOD_TENSOR) {
Q
QI JUN 已提交
46
    var->GetMutable<LoDTensor>();
47
  } else if (var_type == proto::VarDesc::SELECTED_ROWS) {
Q
QI JUN 已提交
48
    var->GetMutable<SelectedRows>();
49
  } else if (var_type == proto::VarDesc::FEED_MINIBATCH) {
Q
QI JUN 已提交
50
    var->GetMutable<FeedFetchList>();
51
  } else if (var_type == proto::VarDesc::FETCH_LIST) {
Q
QI JUN 已提交
52
    var->GetMutable<FeedFetchList>();
53
  } else if (var_type == proto::VarDesc::STEP_SCOPES) {
Y
Yu Yang 已提交
54
    var->GetMutable<std::vector<framework::Scope>>();
55
  } else if (var_type == proto::VarDesc::LOD_RANK_TABLE) {
Y
Yu Yang 已提交
56
    var->GetMutable<LoDRankTable>();
57
  } else if (var_type == proto::VarDesc::LOD_TENSOR_ARRAY) {
Y
Yu Yang 已提交
58
    var->GetMutable<LoDTensorArray>();
Q
QI JUN 已提交
59 60
  } else {
    PADDLE_THROW(
Y
Yu Yang 已提交
61
        "Variable type %d is not in "
Y
Yu Yang 已提交
62
        "[LoDTensor, SelectedRows, FEED_MINIBATCH, FETCH_LIST, LOD_RANK_TABLE]",
Y
Yu Yang 已提交
63
        var_type);
Q
QI JUN 已提交
64 65 66
  }
}

Y
Yu Yang 已提交
67
void Executor::Run(const ProgramDesc& pdesc, Scope* scope, int block_id,
T
typhoonzero 已提交
68
                   bool create_local_scope, bool create_vars) {
Y
Yang Yang 已提交
69
  // TODO(tonyyang-svail):
Y
Yang Yang 已提交
70
  //    - only runs on the first device (i.e. no interdevice communication)
Y
Yang Yang 已提交
71
  //    - will change to use multiple blocks for RNN op and Cond Op
72
  PADDLE_ENFORCE_LT(static_cast<size_t>(block_id), pdesc.Size());
73
  auto& block = pdesc.Block(block_id);
Y
Yang Yang 已提交
74
  auto& device = device_contexts_[0];
Y
Yang Yang 已提交
75

Y
Yu Yang 已提交
76
  Scope* local_scope = scope;
T
typhoonzero 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  if (create_vars) {
    if (create_local_scope) {
      local_scope = &scope->NewScope();
      for (auto& var : block.AllVars()) {
        if (var->Name() == framework::kEmptyVarName) {
          continue;
        }

        if (var->Persistable()) {
          auto* ptr = scope->Var(var->Name());
          CreateTensor(ptr, var->GetType());
          VLOG(3) << "Create Variable " << var->Name()
                  << " global, which pointer is " << ptr;
        } else {
          auto* ptr = local_scope->Var(var->Name());
          CreateTensor(ptr, var->GetType());
          VLOG(3) << "Create Variable " << var->Name()
                  << " locally, which pointer is " << ptr;
        }
96
      }
T
typhoonzero 已提交
97 98
    } else {
      for (auto& var : block.AllVars()) {
Y
Yu Yang 已提交
99 100
        auto* ptr = local_scope->Var(var->Name());
        CreateTensor(ptr, var->GetType());
T
typhoonzero 已提交
101 102
        VLOG(3) << "Create variable " << var->Name() << ", which pointer is "
                << ptr;
Y
Yu Yang 已提交
103
      }
T
typhoonzero 已提交
104 105
    }  // if (create_local_scope)
  }    // if (create_vars)
Y
Yang Yang 已提交
106

107 108
  for (auto& op_desc : block.AllOps()) {
    auto op = paddle::framework::OpRegistry::CreateOp(*op_desc);
109
    VLOG(3) << op->DebugString();
Y
Yu Yang 已提交
110 111 112 113
    op->Run(*local_scope, *device);
  }
  if (create_local_scope) {
    scope->DeleteScope(local_scope);
Q
qijun 已提交
114
  }
Q
qijun 已提交
115 116 117 118
}

}  // namespace framework
}  // namespace paddle