paddle_pass_builder.h 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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.

#pragma once

#include <sstream>
#include <string>
#include <vector>

21 22 23
/*! \file */

/*! \namespace paddle */
24
namespace paddle {
25 26

/** This is a pass builder based on string. It is part of inference API.
27 28 29 30 31 32
 */
class PaddlePassBuilder {
 public:
  explicit PaddlePassBuilder(const std::vector<std::string> &passes)
      : passes_(passes) {}

33 34 35 36
  void SetPasses(std::initializer_list<std::string> passes) {
    passes_ = passes;
  }

37
  /** Append a pass to the end of the passes. */
38 39
  void AppendPass(const std::string &pass_type);

40 41 42 43
  /** Insert a pass to a specific position.
   * @param idx the position to insert.
   * @param pass_type the pass key.
   */
44 45
  void InsertPass(size_t idx, const std::string &pass_type);

46
  /** Delete the `idx`-th pass. */
47 48
  void DeletePass(size_t idx);

49
  /** Delete all the passes that has type `pass_type`. */
50 51
  void DeletePass(const std::string &pass_type);

52
  void ClearPasses();
Y
Yan Chunwei 已提交
53 54 55
  /** Append an analysis pass. */
  void AppendAnalysisPass(const std::string &pass);

56 57 58
  /** Visualize the computation graph after each pass by generating a DOT
   * language file, one can draw them with the Graphviz toolkit.
   */
59 60
  void TurnOnDebug();

61
  /** Human-readible information. */
62 63 64
  std::string DebugString();

  const std::vector<std::string> &AllPasses() const { return passes_; }
Y
Yan Chunwei 已提交
65 66 67 68 69 70 71
  std::vector<std::string> AnalysisPasses() const {
    auto passes = analysis_passes_;
    // To make sure the ir_graph_to_program should be the last pass so any
    // modication of IR will persist to the program.
    passes.push_back("ir_graph_to_program_pass");
    return passes;
  }
72 73

 protected:
Y
Yan Chunwei 已提交
74
  std::vector<std::string> analysis_passes_{
75
      {"ir_graph_build_pass", "ir_graph_clean_pass", "ir_analysis_pass",
76 77
       "ir_params_sync_among_devices_pass", "adjust_cudnn_workspace_size_pass",
       "inference_op_replace_pass"}};
78 79 80
  std::vector<std::string> passes_;
};

81
/**Pass strategy to help control the IR passes.
82 83 84 85 86 87
 */
class PassStrategy : public PaddlePassBuilder {
 public:
  explicit PassStrategy(const std::vector<std::string> &passes)
      : PaddlePassBuilder(passes) {}

88 89 90 91
  /** Enable the use of cuDNN kernel
   */
  virtual void EnableCUDNN() {}

92 93 94
  /** The MKLDNN control exists in both CPU and GPU mode, because there can be
   * still some CPU kernels running in CPU mode.
   */
Y
Yan Chunwei 已提交
95
  virtual void EnableMKLDNN() {}
96

M
mozga-intel 已提交
97 98 99 100
  /** Enable NGRAPH optimization
   */
  virtual void EnableNgraph() {}

101
  /** Enable MKLDNN quantize optimization
102
   */
103
  virtual void EnableMkldnnQuantizer() {}
104

105 106
  bool use_gpu() const { return use_gpu_; }

107
  virtual ~PassStrategy() = default;
108 109

 protected:
M
mozga-intel 已提交
110
  bool use_ngraph_{false};
111
  bool use_gpu_{false};
Y
Yan Chunwei 已提交
112
  bool use_mkldnn_{false};
113 114
};

115
/** The CPU passes controller, it is used in AnalysisPredictor with CPU mode.
116 117 118
 */
class CpuPassStrategy : public PassStrategy {
 public:
119
  CpuPassStrategy();
120

Y
Yan Chunwei 已提交
121
  explicit CpuPassStrategy(const CpuPassStrategy &other)
W
Wojciech Uss 已提交
122 123
      : PassStrategy(other.AllPasses()) {
    use_gpu_ = other.use_gpu_;
M
mozga-intel 已提交
124
    use_ngraph_ = other.use_ngraph_;
W
Wojciech Uss 已提交
125 126 127
    use_mkldnn_ = other.use_mkldnn_;
    use_mkldnn_quantizer_ = other.use_mkldnn_quantizer_;
  }
Y
Yan Chunwei 已提交
128

129 130
  virtual ~CpuPassStrategy() = default;

131
  void EnableCUDNN() override;
M
mozga-intel 已提交
132
  void EnableNgraph() override;
W
Wojciech Uss 已提交
133 134
  void EnableMKLDNN() override;
  void EnableMkldnnQuantizer() override;
135 136

 protected:
M
mozga-intel 已提交
137
  bool use_ngraph_{false};
138
  bool use_mkldnn_quantizer_{false};
139 140
};

141
/** The GPU passes strategy, it is used in AnalysisPredictor with GPU mode.
142 143 144
 */
class GpuPassStrategy : public PassStrategy {
 public:
145
  GpuPassStrategy();
146

Y
Yan Chunwei 已提交
147
  explicit GpuPassStrategy(const GpuPassStrategy &other)
148 149
      : PassStrategy(other.AllPasses()) {
    use_gpu_ = true;
150
    use_cudnn_ = other.use_cudnn_;
151
  }
152

153
  void EnableCUDNN() override;
M
mozga-intel 已提交
154
  void EnableNgraph() override;
155
  void EnableMKLDNN() override;
156
  void EnableMkldnnQuantizer() override;
157 158

  virtual ~GpuPassStrategy() = default;
159 160 161

 protected:
  bool use_cudnn_{false};
162 163
};

164
extern const std::vector<std::string> kTRTSubgraphPasses;
165 166
extern const std::vector<std::string> kAnakinSubgraphPasses;

167
}  // namespace paddle