naive_executor.cc 6.6 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.

X
Xin Pan 已提交
15
#include "paddle/fluid/framework/naive_executor.h"
16

17
#include <string>
18

19
#include "paddle/fluid/framework/op_registry.h"
W
Wang Guibao 已提交
20
#include "paddle/fluid/framework/variable_helper.h"
21
#include "paddle/fluid/platform/denormal.h"
22 23 24
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
25
#ifdef PADDLE_WITH_TENSORRT
W
wenbin 已提交
26 27
#include "paddle/fluid/operators/tensorrt/tensorrt_engine_op.h"
#endif
28 29 30
#ifdef PADDLE_WITH_INFERENCE_NVTX
#include "paddle/fluid/platform/device/gpu/cuda/cuda_profiler.h"
#endif
31 32 33

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

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

void NaiveExecutor::Run() {
49 50
#ifdef PADDLE_WITH_MKLDNN
  platform::AttachPointerHashToMKLDNNKey(this, place_);
J
Jacek Czaja 已提交
51
  platform::RegisterModelLayout(ops_, place_);
52
#endif
53
  platform::ScopedFlushDenormal flush;
54 55 56
#ifdef PADDLE_WITH_INFERENCE_NVTX
  platform::CudaNvtxRangePush("model", platform::NvtxRangeColor::Yellow);
#endif
57
  for (auto &op : ops_) {
Y
Yan Chunwei 已提交
58 59
    VLOG(4) << std::this_thread::get_id() << " run "
            << op->DebugStringEx(scope_) << " on scope " << scope_;
60
    op->SetIsCalledByExecutor(false);
61 62 63
#ifdef PADDLE_WITH_INFERENCE_NVTX
    platform::CudaNvtxRangePush(op->Type(), platform::NvtxRangeColor::Green);
#endif
64
    op->Run(*scope_, place_);
65 66 67
#ifdef PADDLE_WITH_INFERENCE_NVTX
    platform::CudaNvtxRangePop();
#endif
68 69 70
    if (hookfunc_) {
      hookfunc_(op.get());
    }
71
  }
72 73 74
#ifdef PADDLE_WITH_INFERENCE_NVTX
  platform::CudaNvtxRangePop();
#endif
75 76
}

77 78 79 80
void NaiveExecutor::CreateVariables(const ProgramDesc &desc,
                                    int block_id,
                                    bool persistable,
                                    Scope *scope) {
81 82 83
  PADDLE_ENFORCE_NOT_NULL(scope,
                          platform::errors::InvalidArgument(
                              "The Scope to hold variables is nullptr."));
84

85 86
  auto &global_block = desc.Block(block_id);

87
  const auto *anc = scope;
88
  PADDLE_ENFORCE_NE(
89 90
      anc->parent(),
      anc,
91
      platform::errors::InvalidArgument("Input scope should be child scope."));
92 93
  while (anc->parent()) {
    anc = anc->parent();
94 95
  }

Y
Yan Chunwei 已提交
96
  int num_vars = 0;
97 98 99 100
  for (auto &var : global_block.AllVars()) {
    if (var->Name() == framework::kEmptyVarName) {
      continue;
    }
Y
Yan Chunwei 已提交
101
    num_vars++;
102 103 104 105 106 107 108 109 110 111 112 113 114

    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;
115 116 117 118
        InitializeVariable(ptr, var->GetType());
      }
    }
  }
Y
Yan Chunwei 已提交
119
  VLOG(4) << "naive executor create " << num_vars << " vars";
120 121
}

122 123
void NaiveExecutor::CreateOps(const ProgramDesc &desc,
                              int block_id,
124 125 126 127
                              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")) {
128 129
      LOG(INFO) << "---  skip [" << op_desc->Input("X")[0] << "], "
                << op_desc->Type() << " -> " << op_desc->Output("Out")[0];
130 131 132 133 134 135
      continue;
    }
    ops_.emplace_back(OpRegistry::CreateOp(*op_desc));
  }
}

136
phi::DenseTensor *NaiveExecutor::FindTensor(const std::string &name) {
137 138 139
  PADDLE_ENFORCE_NOT_NULL(scope_,
                          platform::errors::PreconditionNotMet(
                              "Need to init scope in NaiveExecutor firstly."));
140
  auto *var = scope_->FindVar(name);
141 142 143
  PADDLE_ENFORCE_NOT_NULL(
      var,
      platform::errors::NotFound("No variable [%s] in current scope.", name));
144
  auto *tensor = const_cast<phi::DenseTensor *>(&var->Get<phi::DenseTensor>());
145 146 147
  return tensor;
}

148 149
void NaiveExecutor::RegisterOutputHook(const HookFunc &hookfunc) {
  hookfunc_ = hookfunc;
150 151
}

152 153 154 155
NaiveExecutor::~NaiveExecutor() {
#ifdef PADDLE_WITH_MKLDNN
  // Clear mkl-dnn cache,
  // this is needed to have mkl-dnn unit tests working
156
  platform::ClearMKLDNNCache(place_, this);
157 158 159
#endif
}

W
wenbin 已提交
160
void NaiveExecutor::ResetTrtOps(int num) {
161
#ifdef PADDLE_WITH_TENSORRT
W
wenbin 已提交
162 163 164 165 166 167 168 169 170
  for (auto &op : ops_) {
    if (op->Type() == "tensorrt_engine") {
      operators::TensorRTEngineOp *trtop =
          dynamic_cast<operators::TensorRTEngineOp *>(op.get());
      if (!trtop) return;
      std::string engine_key = trtop->Attr<std::string>("engine_key");
      int engine_predictor_id = trtop->Attr<int>("predictor_id");
      std::string engine_name =
          engine_key + std::to_string(engine_predictor_id);
W
wenbin 已提交
171 172 173
      operators::TensorRTEngine *trt_engine = nullptr;
      // can't get trt engine if int8 calibration table data process.
      if (paddle::inference::Singleton<
W
wenbin 已提交
174
              inference::tensorrt::TRTEngineManager>::Global()
W
wenbin 已提交
175 176 177 178 179 180
              .Has(engine_name)) {
        trt_engine = paddle::inference::Singleton<
                         inference::tensorrt::TRTEngineManager>::Global()
                         .Get(engine_name);
      }
      if (trt_engine && trt_engine->with_dynamic_shape()) {
W
wenbin 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        LOG(INFO) << "rebuild trt engine, this may cost a lot of time!";
        trt_engine->ResetContext();
        trt_engine->ClearTensorMap();
        trt_engine->SetProfileNum(num);
        auto *anc = scope_->parent();
        while (anc && anc->parent()) {
          anc = anc->parent();
        }
        if (anc == nullptr) {
          anc = scope_;
        }
        trtop->PrepareTRTEngine(*anc, trt_engine);
      }
    }
  }
#endif
}
198

199 200
}  // namespace framework
}  // namespace paddle