naive_executor.cc 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

15
#include <memory>
X
Xin Pan 已提交
16
#include <string>
17
#include <utility>
X
Xin Pan 已提交
18 19
#include <vector>

20 21 22
#include "paddle/fluid/framework/feed_fetch_method.h"
#include "paddle/fluid/framework/lod_rank_table.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
X
Xin Pan 已提交
23
#include "paddle/fluid/framework/naive_executor.h"
24 25
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/reader.h"
W
Wang Guibao 已提交
26
#include "paddle/fluid/framework/variable_helper.h"
27
#include "paddle/fluid/platform/denormal.h"
28
#include "paddle/fluid/string/pretty_log.h"
29 30 31
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
32 33 34

namespace paddle {
namespace framework {
35 36 37
void NaiveExecutor::Prepare(Scope *scope, const ProgramDesc &program_desc,
                            int block_id, bool with_feed_fetch_ops) {
  if (!scope) {
38 39
    scope_ = new framework::Scope;
  } else {
40
    scope_ = scope;
41
  }
42 43

  VLOG(3) << "NaiveExecutor init with scope " << scope;
44 45 46 47
  CreateOps(program_desc, block_id, with_feed_fetch_ops);
}

void NaiveExecutor::Run() {
48 49 50
#ifdef PADDLE_WITH_MKLDNN
  platform::AttachPointerHashToMKLDNNKey(this, place_);
#endif
51
  platform::ScopedFlushDenormal flush;
52
  for (auto &op : ops_) {
Y
Yan Chunwei 已提交
53 54
    VLOG(4) << std::this_thread::get_id() << " run "
            << op->DebugStringEx(scope_) << " on scope " << scope_;
55
    op->SetIsCalledByExecutor(false);
56 57 58 59
    op->Run(*scope_, place_);
  }
}

60 61
void NaiveExecutor::CreateVariables(const ProgramDesc &desc, int block_id,
                                    bool persistable, Scope *scope) {
62 63 64
  PADDLE_ENFORCE_NOT_NULL(scope,
                          platform::errors::InvalidArgument(
                              "The Scope to hold variables is nullptr."));
65

66 67
  auto &global_block = desc.Block(block_id);

68
  const auto *anc = scope;
69 70 71
  PADDLE_ENFORCE_NE(
      anc->parent(), anc,
      platform::errors::InvalidArgument("Input scope should be child scope."));
72 73
  while (anc->parent()) {
    anc = anc->parent();
74 75
  }

Y
Yan Chunwei 已提交
76
  int num_vars = 0;
77 78 79 80
  for (auto &var : global_block.AllVars()) {
    if (var->Name() == framework::kEmptyVarName) {
      continue;
    }
Y
Yan Chunwei 已提交
81
    num_vars++;
82 83 84 85 86 87 88 89 90 91 92 93 94

    if (persistable == var->Persistable()) {
      if (persistable) {
        if (!anc->FindVar(var->Name())) {
          auto *ptr = const_cast<Scope *>(anc)->Var(var->Name());
          VLOG(3) << scope << " Create persistable variable " << var->Name()
                  << ", which pointer is " << ptr;
          InitializeVariable(ptr, var->GetType());
        }
      } else {
        auto *ptr = const_cast<Scope *>(scope)->Var(var->Name());
        VLOG(3) << scope << " Create variable " << var->Name()
                << ", which pointer is " << ptr;
95 96 97 98
        InitializeVariable(ptr, var->GetType());
      }
    }
  }
Y
Yan Chunwei 已提交
99
  VLOG(4) << "naive executor create " << num_vars << " vars";
100 101 102 103 104 105 106
}

void NaiveExecutor::CreateOps(const ProgramDesc &desc, int block_id,
                              bool with_feed_fetch_ops) {
  for (const auto &op_desc : desc.Block(block_id).AllOps()) {
    if (!with_feed_fetch_ops &&
        (op_desc->Type() == "feed" || op_desc->Type() == "fetch")) {
107 108
      LOG(INFO) << "---  skip [" << op_desc->Input("X")[0] << "], "
                << op_desc->Type() << " -> " << op_desc->Output("Out")[0];
109 110 111 112 113 114 115
      continue;
    }
    ops_.emplace_back(OpRegistry::CreateOp(*op_desc));
  }
}

LoDTensor *NaiveExecutor::FindTensor(const std::string &name) {
116 117 118
  PADDLE_ENFORCE_NOT_NULL(scope_,
                          platform::errors::PreconditionNotMet(
                              "Need to init scope in NaiveExecutor firstly."));
119
  auto *var = scope_->FindVar(name);
120 121
  PADDLE_ENFORCE_NOT_NULL(var, platform::errors::NotFound(
                                   "No variable [%s] in current scope.", name));
122 123 124 125 126 127 128 129 130 131 132 133 134 135
  auto *tensor = const_cast<LoDTensor *>(&var->Get<LoDTensor>());
  return tensor;
}

void NaiveExecutor::CleanFeedFetchOps() {
  std::vector<std::unique_ptr<OperatorBase>> ops;
  for (auto &op : ops_) {
    if (op->Type() != "feed" && op->Type() != "fetch") {
      ops.emplace_back(std::move(op));
    }
  }
  ops_.swap(ops);
}

136 137 138 139
NaiveExecutor::~NaiveExecutor() {
#ifdef PADDLE_WITH_MKLDNN
  // Clear mkl-dnn cache,
  // this is needed to have mkl-dnn unit tests working
140
  ClearMKLDNNCache(place_);
141 142 143
#endif
}

144 145
}  // namespace framework
}  // namespace paddle