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

22 23 24 25 26 27 28 29 30 31 32 33
///
/// \file paddle_pass_builder.h
///
/// \brief Class Paddle Passs Builder and its subclasses(pass strategies).
/// \section sec_intro Introduction
/// This class aims to build passes for paddle and define passes' strategies.
///
/// \author paddle-infer@baidu.com
/// \date 2020-3-23
/// \since 1.7

/// \namespace paddle
34
namespace paddle {
35

36 37 38 39 40 41 42 43 44 45 46
/// \class PaddlePassBuilder
/// \brief This class build passes based on vector<string> input. It is part of
/// inference API. Users can build passes, insert new passes, delete passes
/// using this class and its functions.
///
/// Example Usage:
///     Build a new pass.
/// \code{cpp}
/// const vector<string> passes(1, "conv_relu_mkldnn_fuse_pass");
/// PaddlePassBuilder builder(passes);
/// \endcode
47
class PD_INFER_DECL PaddlePassBuilder {
48
 public:
49 50
  /// \brief Constructor of the class. It stores the input passes.
  /// \param[in] passes passes' types.
51 52 53
  explicit PaddlePassBuilder(const std::vector<std::string> &passes)
      : passes_(passes) {}

54 55
  /// \brief Stores the input passes.
  /// \param[in] passes passes' types.
56 57 58 59
  void SetPasses(std::initializer_list<std::string> passes) {
    passes_ = passes;
  }

60 61
  /// \brief Append a pass to the end of the passes.
  /// \param[in] pass_type the type of the new pass.
62 63
  void AppendPass(const std::string &pass_type);

64 65 66
  /// \brief Insert a pass to a specific position.
  /// \param[in] idx the position to insert.
  /// \param[in] pass_type the type of insert pass.
67 68
  void InsertPass(size_t idx, const std::string &pass_type);

69 70
  /// \brief Delete the pass at certain position 'idx'.
  /// \param[in] idx the position to delete.
71 72
  void DeletePass(size_t idx);

73 74
  /// \brief Delete all passes that has a certain type 'pass_type'.
  /// \param[in] pass_type the certain pass type to be deleted.
75 76
  void DeletePass(const std::string &pass_type);

77
  /// \brief Delete all the passes.
78
  void ClearPasses();
79 80 81

  /// \brief Append an analysis pass.
  /// \param[in] pass the type of the new analysis pass.
Y
Yan Chunwei 已提交
82 83
  void AppendAnalysisPass(const std::string &pass);

84 85
  /// \brief Visualize the computation graph after each pass by generating a DOT
  /// language file, one can draw them with the Graphviz toolkit.
86
  void TurnOnDebug();
87
  /// \brief Human-readable information of the passes.
88 89
  std::string DebugString();

90 91
  /// \brief Get information of passes.
  /// \return Return list of the passes.
92
  const std::vector<std::string> &AllPasses() const { return passes_; }
93 94 95

  /// \brief Get information of analysis passes.
  /// \return Return list of analysis passes.
Y
Yan Chunwei 已提交
96 97 98 99 100 101 102
  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;
  }
103 104

 protected:
105
  /// \cond Protected
Y
Yan Chunwei 已提交
106
  std::vector<std::string> analysis_passes_{
107
      {"ir_graph_build_pass", "ir_graph_clean_pass", "ir_analysis_pass",
108 109
       "ir_params_sync_among_devices_pass", "adjust_cudnn_workspace_size_pass",
       "inference_op_replace_pass"}};
110
  std::vector<std::string> passes_;
111
  /// \endcond
112 113
};

114 115 116
/// \class PassStrategy
/// \brief This class defines the pass strategies like whether to use gpu/cuDNN
/// kernel/MKLDNN.
117
class PD_INFER_DECL PassStrategy : public PaddlePassBuilder {
118
 public:
119 120
  /// \brief Constructor of PassStrategy class. It works the same as
  /// PaddlePassBuilder class. \param[in] passes passes' types.
121 122 123
  explicit PassStrategy(const std::vector<std::string> &passes)
      : PaddlePassBuilder(passes) {}

124
  /// \brief Enable the use of cuDNN kernel.
125 126
  virtual void EnableCUDNN() {}

127 128 129
  /// \brief Enable the use of MKLDNN.
  /// The MKLDNN control exists in both CPU and GPU mode, because there can
  /// still be some CPU kernels running in GPU mode.
Y
Yan Chunwei 已提交
130
  virtual void EnableMKLDNN() {}
131

132
  /// \brief Enable MKLDNN quantize optimization.
133
  virtual void EnableMkldnnQuantizer() {}
134

135 136 137
  /// \brief Enable MKLDNN bfloat16.
  virtual void EnableMkldnnBfloat16() {}

138 139
  /// \brief Check if we are using gpu.
  /// \return A bool variable implying whether we are in gpu mode.
140 141
  bool use_gpu() const { return use_gpu_; }

142
  /// \brief Default destructor.
143
  virtual ~PassStrategy() = default;
144 145

 protected:
146
  /// \cond Protected
147
  bool use_gpu_{false};
Y
Yan Chunwei 已提交
148
  bool use_mkldnn_{false};
149
  /// \endcond
150 151
};

152 153 154
/// \class CpuPassStrategy
/// \brief The CPU passes controller, it is used in AnalysisPredictor with CPU
/// mode.
155
class PD_INFER_DECL CpuPassStrategy : public PassStrategy {
156
 public:
157
  /// \brief Default constructor of CpuPassStrategy.
158
  CpuPassStrategy();
159

160 161
  /// \brief Construct by copying another CpuPassStrategy object.
  /// \param[in] other The CpuPassStrategy object we want to copy.
Y
Yan Chunwei 已提交
162
  explicit CpuPassStrategy(const CpuPassStrategy &other)
W
Wojciech Uss 已提交
163 164 165 166
      : PassStrategy(other.AllPasses()) {
    use_gpu_ = other.use_gpu_;
    use_mkldnn_ = other.use_mkldnn_;
    use_mkldnn_quantizer_ = other.use_mkldnn_quantizer_;
167
    use_mkldnn_bfloat16_ = other.use_mkldnn_bfloat16_;
W
Wojciech Uss 已提交
168
  }
169
  /// \brief Default destructor.
170 171
  virtual ~CpuPassStrategy() = default;

172
  /// \brief Enable the use of cuDNN kernel.
173
  void EnableCUDNN() override;
174 175

  /// \brief Enable the use of MKLDNN.
W
Wojciech Uss 已提交
176
  void EnableMKLDNN() override;
177 178

  /// \brief Enable MKLDNN quantize optimization.
W
Wojciech Uss 已提交
179
  void EnableMkldnnQuantizer() override;
180

181 182 183
  /// \brief Enable MKLDNN bfloat16.
  void EnableMkldnnBfloat16() override;

184
 protected:
185
  /// \cond Protected
186
  bool use_mkldnn_quantizer_{false};
187
  bool use_mkldnn_bfloat16_{false};
188
  /// \endcond
189 190
};

191 192 193
/// \class GpuPassStrategy
/// \brief The GPU passes controller, it is used in AnalysisPredictor with GPU
/// mode.
194
class PD_INFER_DECL GpuPassStrategy : public PassStrategy {
195
 public:
196
  /// \brief Default constructor of GpuPassStrategy.
197
  GpuPassStrategy();
198

199 200
  /// \brief Construct by copying another GpuPassStrategy object.
  /// \param[in] other The GpuPassStrategy object we want to copy.
Y
Yan Chunwei 已提交
201
  explicit GpuPassStrategy(const GpuPassStrategy &other)
202 203
      : PassStrategy(other.AllPasses()) {
    use_gpu_ = true;
204
    use_cudnn_ = other.use_cudnn_;
205
  }
206

207
  /// \brief Enable the use of cuDNN kernel.
208
  void EnableCUDNN() override;
209 210

  /// \brief Not supported in GPU mode yet.
211
  void EnableMKLDNN() override;
212 213

  /// \brief Not supported in GPU mode yet.
214
  void EnableMkldnnQuantizer() override;
215

216 217 218
  /// \brief Not supported in GPU mode yet.
  void EnableMkldnnBfloat16() override;

219
  /// \brief Default destructor.
220
  virtual ~GpuPassStrategy() = default;
221 222

 protected:
223
  /// \cond Protected
224
  bool use_cudnn_{false};
225
  /// \endcond
226
};
227

228
/// \brief List of tensorRT subgraph passes.
229
PD_INFER_DECL extern const std::vector<std::string> kTRTSubgraphPasses;
230 231

/// \brief List of lite subgraph passes.
232
PD_INFER_DECL extern const std::vector<std::string> kLiteSubgraphPasses;
233

234
}  // namespace paddle