executor.cc 5.5 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 <set>
Y
Yang Yang 已提交
18

Y
Yang Yu 已提交
19
#include "gflags/gflags.h"
Q
QI JUN 已提交
20
#include "paddle/framework/feed_fetch_type.h"
Y
Yu Yang 已提交
21
#include "paddle/framework/lod_rank_table.h"
Y
Yu Yang 已提交
22
#include "paddle/framework/lod_tensor_array.h"
Q
qijun 已提交
23
#include "paddle/framework/op_registry.h"
Y
Yang Yu 已提交
24
#include "paddle/platform/place.h"
25
#include "paddle/platform/profiler.h"
Y
Yang Yu 已提交
26

27
DECLARE_bool(do_memory_benchmark);
Y
Yang Yu 已提交
28 29 30
DEFINE_bool(check_nan_inf, false,
            "Checking whether operator produce NAN/INF or not. It will be "
            "extremely slow so please use this flag wisely.");
Q
qijun 已提交
31 32 33 34

namespace paddle {
namespace framework {

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

D
dzhwinter 已提交
38
Executor::Executor(const platform::Place& place) : place_(place) {}
Q
qijun 已提交
39

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

Y
Yang Yu 已提交
66 67
static void CheckTensorNANOrInf(const std::string& name,
                                const framework::Tensor& tensor) {
Y
Yang Yu 已提交
68
  if (tensor.memory_size() == 0) {
Y
Yang Yu 已提交
69 70
    return;
  }
Y
Yang Yu 已提交
71 72
  if (tensor.type().hash_code() != typeid(float).hash_code() &&
      tensor.type().hash_code() != typeid(double).hash_code()) {
Y
Yang Yu 已提交
73 74 75 76 77 78
    return;
  }
  PADDLE_ENFORCE(!framework::HasInf(tensor), "Tensor %s has Inf", name);
  PADDLE_ENFORCE(!framework::HasNAN(tensor), "Tensor %s has NAN", name);
}

Y
Yu Yang 已提交
79
void Executor::Run(const ProgramDesc& pdesc, Scope* scope, int block_id,
T
typhoonzero 已提交
80
                   bool create_local_scope, bool create_vars) {
Y
Yang Yang 已提交
81
  // TODO(tonyyang-svail):
Y
Yang Yang 已提交
82
  //    - only runs on the first device (i.e. no interdevice communication)
Y
Yang Yang 已提交
83
  //    - will change to use multiple blocks for RNN op and Cond Op
84
  PADDLE_ENFORCE_LT(static_cast<size_t>(block_id), pdesc.Size());
85
  auto& block = pdesc.Block(block_id);
Y
Yang Yang 已提交
86

Y
Yu Yang 已提交
87
  Scope* local_scope = scope;
T
typhoonzero 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  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;
        }
107
      }
T
typhoonzero 已提交
108 109
    } else {
      for (auto& var : block.AllVars()) {
Y
Yu Yang 已提交
110 111
        auto* ptr = local_scope->Var(var->Name());
        CreateTensor(ptr, var->GetType());
T
typhoonzero 已提交
112 113
        VLOG(3) << "Create variable " << var->Name() << ", which pointer is "
                << ptr;
Y
Yu Yang 已提交
114
      }
T
typhoonzero 已提交
115 116
    }  // if (create_local_scope)
  }    // if (create_vars)
Y
Yang Yang 已提交
117

118 119
  for (auto& op_desc : block.AllOps()) {
    auto op = paddle::framework::OpRegistry::CreateOp(*op_desc);
Q
Qiao Longfei 已提交
120
    VLOG(4) << op->DebugStringEx(local_scope);
121 122

    platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
D
dangqingqing 已提交
123
    platform::RecordEvent record_event(op->Type(), pool.Get(place_));
124

D
dzhwinter 已提交
125
    op->Run(*local_scope, place_);
Q
Qiao Longfei 已提交
126
    VLOG(3) << op->DebugStringEx(local_scope);
127 128 129 130
    if (FLAGS_do_memory_benchmark) {
      VLOG(2) << "Memory used after operator " + op->Type() + " running: "
              << memory::memory_usage(place_);
    }
Y
Yang Yu 已提交
131 132 133 134 135 136 137 138 139
    if (FLAGS_check_nan_inf) {
      for (auto& vname : op->OutputVars(true)) {
        auto* var = local_scope->FindVar(vname);
        if (var == nullptr) continue;
        if (var->IsType<framework::LoDTensor>()) {
          CheckTensorNANOrInf(vname, var->Get<framework::LoDTensor>());
        }
      }
    }
Y
Yu Yang 已提交
140
  }
G
gongweibao 已提交
141
  if (create_vars && create_local_scope) {
Y
Yu Yang 已提交
142
    scope->DeleteScope(local_scope);
Q
qijun 已提交
143
  }
144 145 146 147 148 149
  if (FLAGS_do_memory_benchmark) {
    VLOG(2) << "-------------------------------------------------------";
    VLOG(2) << "Memory used after deleting local scope: "
            << memory::memory_usage(place_);
    VLOG(2) << "-------------------------------------------------------";
  }
Q
qijun 已提交
150 151 152 153
}

}  // namespace framework
}  // namespace paddle