analyzer.cc 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include "paddle/fluid/inference/analysis/analyzer.h"
G
gongweibao 已提交
16
#include <string>
L
luotao1 已提交
17
#include <vector>
X
Xin Pan 已提交
18

19 20 21
#include "paddle/fluid/inference/analysis/data_flow_graph_to_fluid_pass.h"
#include "paddle/fluid/inference/analysis/dfg_graphviz_draw_pass.h"
#include "paddle/fluid/inference/analysis/fluid_to_data_flow_graph_pass.h"
22
#include "paddle/fluid/inference/analysis/fluid_to_ir_pass.h"
Y
Yan Chunwei 已提交
23
#include "paddle/fluid/inference/analysis/model_store_pass.h"
24 25 26 27
#include "paddle/fluid/inference/analysis/pass_manager.h"
#include "paddle/fluid/inference/analysis/tensorrt_subgraph_node_mark_pass.h"
#include "paddle/fluid/inference/analysis/tensorrt_subgraph_pass.h"

28
DEFINE_bool(IA_enable_tensorrt_subgraph_engine, false,
29 30
            "Enable subgraph to TensorRT engine for acceleration");

31 32 33
DEFINE_bool(IA_enable_ir, false, "Turn on IR support");

DEFINE_string(IA_graphviz_log_root, "./",
34 35
              "Graphviz debuger for data flow graphs.");

36
DEFINE_string(IA_output_storage_path, "", "optimized model output path");
Y
Yan Chunwei 已提交
37

38
namespace paddle {
39 40 41
namespace inference {
namespace analysis {

42 43 44 45
class DfgPassManagerImpl final : public DfgPassManager {
 public:
  DfgPassManagerImpl() {
    // TODO(Superjomn) set the key with pass reprs.
46
    if (!FLAGS_IA_enable_ir) {
47
      AddPass("fluid-to-data-flow-graph", new FluidToDataFlowGraphPass);
48 49
    } else {
      AddPass("fluid-to-ir-pass", new FluidToIrPass);
50 51 52 53 54 55 56 57 58 59 60 61
    }
    TryAddTensorRtPass();
    AddPass("data-flow-graph-to-fluid", new DataFlowGraphToFluidPass);
    if (!FLAGS_IA_output_storage_path.empty()) {
      AddPass("model-store-pass", new ModelStorePass);
    }
  }

  std::string repr() const override { return "dfg-pass-manager"; }
  std::string description() const override { return "DFG pass manager."; }

 private:
X
Xin Pan 已提交
62
  void AddPass(const std::string& name, AnalysisPass* pass) {
63
    VLOG(30) << "Adding pass " << name;
64 65 66 67 68 69
    Register(name, pass);
    AddGraphvizDebugerPass(pass);
  }

  void TryAddTensorRtPass() {
    if (FLAGS_IA_enable_tensorrt_subgraph_engine) {
70 71
      auto trt_teller = [&](const Node* node) {
        std::unordered_set<std::string> teller_set(
N
nhzlx 已提交
72
            {"mul", "conv2d", "pool2d", "relu", "softmax", "sigmoid",
N
nhzlx 已提交
73
             "depthwise_conv2d", "batch_norm", "concat", "tanh", "pad",
N
nhzlx 已提交
74
             "elementwise_add", "dropout"});
75
        if (!node->IsFunction()) return false;
76 77

        const auto* func = static_cast<const Function*>(node);
W
Wu Yi 已提交
78
        if (teller_set.count(func->func_type())) {
79
          return true;
W
Wu Yi 已提交
80
        } else {
81 82
          return false;
        }
83
      };
84

85 86 87 88 89 90 91
      AddPass("tensorrt-subgraph-marker",
              new TensorRTSubgraphNodeMarkPass(trt_teller));
      AddPass("tensorrt-subgraph", new TensorRTSubGraphPass(trt_teller));
    }
  }

  // Add the graphviz debuger pass if the parent pass has one.
X
Xin Pan 已提交
92
  void AddGraphvizDebugerPass(AnalysisPass* pass) {
93 94 95 96 97 98 99 100 101 102
    auto* debuger_pass = pass->CreateGraphvizDebugerPass();
    if (debuger_pass) {
      Register(debuger_pass->repr(), debuger_pass);
    }
  }
};

Analyzer::Analyzer() { Register("manager1", new DfgPassManagerImpl); }

void Analyzer::Run(Argument* argument) {
103
  std::vector<std::string> passes;
104
  passes.push_back("graph_viz_pass");  // add graphviz for debug.
105
#ifdef PADDLE_WITH_MKLDNN
W
Wojciech Uss 已提交
106
  if (use_mkldnn_) {
107
    VLOG(30) << "Adding MKL-DNN placement pass";
W
Wojciech Uss 已提交
108 109
    passes.push_back("mkldnn_placement_pass");
  }
110
#endif
111 112 113
  // infer_clean_graph_pass should be the first default pass
  // after mkldnn_placement_pass.
  passes.push_back("infer_clean_graph_pass");
114
  passes.push_back("graph_viz_pass");  // add graphviz for debug.
W
Wojciech Uss 已提交
115
  for (auto& pass : ir_passes_) {
116 117 118 119 120 121
    if (!disabled_ir_passes_.count(pass)) {
      passes.push_back(pass);
      passes.push_back("graph_viz_pass");  // add graphviz for debug.
    }
  }
  argument->Set(kFluidToIrPassesAttr, new std::vector<std::string>(passes));
122

123 124 125 126 127 128 129
  for (auto& x : data_) {
    PADDLE_ENFORCE(x->Initialize(argument));
    x->RunAll();
    PADDLE_ENFORCE(x->Finalize());
  }
}

W
Wojciech Uss 已提交
130 131 132 133 134
Analyzer& Analyzer::IncludeAllIrPasses() {
  ir_passes_ = all_ir_passes_;
  return *this;
}

135 136 137 138 139
Analyzer& Analyzer::DisableIrPasses(const std::vector<std::string>& passes) {
  disabled_ir_passes_.insert(passes.begin(), passes.end());
  return *this;
}

W
Wojciech Uss 已提交
140 141 142 143 144 145 146 147 148 149
Analyzer& Analyzer::IncludeIrPasses(const std::vector<std::string>& passes) {
  ir_passes_ = passes;
  return *this;
}

Analyzer& Analyzer::SetUseMkldnn(bool use_mkldnn) {
  use_mkldnn_ = use_mkldnn;
  return *this;
}

150 151
}  // namespace analysis
}  // namespace inference
G
gongweibao 已提交
152
}  // namespace paddle