paddle_pass_builder.h 11.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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>
19
#include <unordered_set>
20
#include <vector>
W
wanghuancoder 已提交
21

22
#include "paddle_infer_declare.h"  // NOLINT
23

24 25 26 27 28 29 30 31 32 33 34 35
///
/// \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
36
namespace paddle {
37

38 39 40 41 42 43 44 45 46 47 48
/// \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
49
class PD_INFER_DECL PaddlePassBuilder {
50
 public:
51 52
  /// \brief Constructor of the class. It stores the input passes.
  /// \param[in] passes passes' types.
53 54 55
  explicit PaddlePassBuilder(const std::vector<std::string> &passes)
      : passes_(passes) {}

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

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

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

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

75 76 77 78
  /// \brief Get the certain position of a pass.
  /// \param[in] pass_type the type of insert pass.
  size_t GetPassIndex(const std::string &pass_type);

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

83
  /// \brief Delete all the passes.
84
  void ClearPasses();
85 86 87

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

90 91
  /// \brief Visualize the computation graph after each pass by generating a DOT
  /// language file, one can draw them with the Graphviz toolkit.
92
  void TurnOnDebug();
93
  /// \brief Human-readable information of the passes.
94 95
  std::string DebugString();

96 97
  /// \brief Get information of passes.
  /// \return Return list of the passes.
98
  const std::vector<std::string> &AllPasses() const { return passes_; }
99 100 101

  /// \brief Get information of analysis passes.
  /// \return Return list of analysis passes.
Y
Yan Chunwei 已提交
102 103 104 105 106 107 108
  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;
  }
109

110 111 112 113
  const std::unordered_set<std::string> &GetAllDeletedPasses() const {
    return deleted_passes_;
  }

114
 protected:
115
  /// \cond Protected
Y
Yan Chunwei 已提交
116
  std::vector<std::string> analysis_passes_{
W
Wilber 已提交
117 118
      {"ir_graph_build_pass",
       "ir_analysis_pass",
119
       "save_optimized_model_pass",
W
Wilber 已提交
120 121
       "ir_params_sync_among_devices_pass",
       "adjust_cudnn_workspace_size_pass",
122
       "inference_op_replace_pass"}};
123
  std::vector<std::string> passes_;
124
  std::unordered_set<std::string> deleted_passes_;
125
  /// \endcond
126 127
};

128 129 130
/// \class PassStrategy
/// \brief This class defines the pass strategies like whether to use gpu/cuDNN
/// kernel/MKLDNN.
131
class PD_INFER_DECL PassStrategy : public PaddlePassBuilder {
132
 public:
133 134
  /// \brief Constructor of PassStrategy class. It works the same as
  /// PaddlePassBuilder class. \param[in] passes passes' types.
135 136 137
  explicit PassStrategy(const std::vector<std::string> &passes)
      : PaddlePassBuilder(passes) {}

138
  /// \brief Enable the use of cuDNN kernel.
139 140
  virtual void EnableCUDNN() {}

141 142 143
  /// \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 已提交
144
  virtual void EnableMKLDNN() {}
145

146
  /// \brief Enable MKLDNN quantize optimization.
147
  virtual void EnableMkldnnQuantizer() {}
148

149 150 151
  /// \brief Enable MKLDNN bfloat16.
  virtual void EnableMkldnnBfloat16() {}

B
baoachun 已提交
152 153 154
  /// \brief Enable MKLDNN int8.
  virtual void EnableMkldnnInt8() {}

P
Paulina Gacek 已提交
155 156 157
  /// \brief Disable MKLDNN fc passes.
  virtual void DisableMkldnnFcPasses() {}

158 159
  /// \brief Check if we are using gpu.
  /// \return A bool variable implying whether we are in gpu mode.
160 161
  bool use_gpu() const { return use_gpu_; }

162 163 164 165
  /// \brief Check if we are using xpu.
  /// \return A bool variable implying whether we are in xpu mode.
  bool use_xpu() const { return use_xpu_; }

J
jianghaicheng 已提交
166 167 168 169
  /// \brief Check if we are using ipu.
  /// \return A bool variable implying whether we are in ipu mode.
  bool use_ipu() const { return use_ipu_; }

170 171 172 173
  /// \brief Check if we are using CustomDevice.
  /// \return A bool variable implying whether we are in CustomDevice mode.
  bool use_custom_device() const { return use_custom_device_; }

174
  /// \brief Default destructor.
175
  virtual ~PassStrategy() = default;
176 177

 protected:
178
  /// \cond Protected
179
  bool use_xpu_{false};
180
  bool use_gpu_{false};
J
jianghaicheng 已提交
181
  bool use_ipu_{false};
Y
Yan Chunwei 已提交
182
  bool use_mkldnn_{false};
183
  bool use_custom_device_{false};
184 185

  bool use_gpu_low_precision_{false};
186
  /// \endcond
187 188
};

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

197 198
  /// \brief Construct by copying another CpuPassStrategy object.
  /// \param[in] other The CpuPassStrategy object we want to copy.
Y
Yan Chunwei 已提交
199
  explicit CpuPassStrategy(const CpuPassStrategy &other)
W
Wojciech Uss 已提交
200 201 202 203
      : PassStrategy(other.AllPasses()) {
    use_gpu_ = other.use_gpu_;
    use_mkldnn_ = other.use_mkldnn_;
    use_mkldnn_quantizer_ = other.use_mkldnn_quantizer_;
204
    use_mkldnn_bfloat16_ = other.use_mkldnn_bfloat16_;
B
baoachun 已提交
205
    use_mkldnn_int8_ = other.use_mkldnn_int8_;
P
Paulina Gacek 已提交
206
    disable_mkldnn_fc_passes_ = other.disable_mkldnn_fc_passes_;
W
Wojciech Uss 已提交
207
  }
208
  /// \brief Default destructor.
209 210
  virtual ~CpuPassStrategy() = default;

211
  /// \brief Enable the use of cuDNN kernel.
212
  void EnableCUDNN() override;
213 214

  /// \brief Enable the use of MKLDNN.
W
Wojciech Uss 已提交
215
  void EnableMKLDNN() override;
216 217

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

220 221 222
  /// \brief Enable MKLDNN bfloat16.
  void EnableMkldnnBfloat16() override;

B
baoachun 已提交
223 224 225
  /// \brief Enable MKLDNN int8.
  void EnableMkldnnInt8() override;

P
Paulina Gacek 已提交
226 227 228
  /// \brief Disable MKLDNN fc passes.
  void DisableMkldnnFcPasses() override;

229
 protected:
P
Paulina Gacek 已提交
230 231 232
  /// \brief Erase MKLDNN fc passes.
  void EraseFcMkldnnPasses();

233
  /// \cond Protected
234
  bool use_mkldnn_quantizer_{false};
235
  bool use_mkldnn_bfloat16_{false};
B
baoachun 已提交
236
  bool use_mkldnn_int8_{false};
P
Paulina Gacek 已提交
237
  bool disable_mkldnn_fc_passes_{false};
238
  /// \endcond
239 240
};

241 242 243
/// \class GpuPassStrategy
/// \brief The GPU passes controller, it is used in AnalysisPredictor with GPU
/// mode.
244
class PD_INFER_DECL GpuPassStrategy : public PassStrategy {
245
 public:
246
  /// \brief Default constructor of GpuPassStrategy.
247
  GpuPassStrategy();
248

249 250
  /// \brief Construct by copying another GpuPassStrategy object.
  /// \param[in] other The GpuPassStrategy object we want to copy.
Y
Yan Chunwei 已提交
251
  explicit GpuPassStrategy(const GpuPassStrategy &other)
252 253
      : PassStrategy(other.AllPasses()) {
    use_gpu_ = true;
254
    use_cudnn_ = other.use_cudnn_;
255
  }
256

257
  /// \brief Enable the use of cuDNN kernel.
258
  void EnableCUDNN() override;
259 260

  /// \brief Not supported in GPU mode yet.
261
  void EnableMKLDNN() override;
262 263

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

266 267 268
  /// \brief Not supported in GPU mode yet.
  void EnableMkldnnBfloat16() override;

B
baoachun 已提交
269 270 271
  /// \brief Not supported in GPU mode yet.
  void EnableMkldnnInt8() override;

P
Paulina Gacek 已提交
272 273 274
  /// \brief Disable MKLDNN fc passes.
  void DisableMkldnnFcPasses() override;

275
  /// \brief Default destructor.
276
  virtual ~GpuPassStrategy() = default;
277 278

 protected:
279
  /// \cond Protected
280
  bool use_cudnn_{false};
281
  /// \endcond
282
};
283

284 285 286 287 288
/// \class XpuPassStrategy
/// \brief The XPU passes controller, it is used in AnalysisPredictor with XPU
/// mode.
class PD_INFER_DECL XpuPassStrategy final : public PassStrategy {
 public:
289
  XpuPassStrategy();
W
Wilber 已提交
290 291
};

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/// \class CustomDevicePassStrategy
/// \brief The CustomDevice passes controller, it is used in AnalysisPredictor
/// with CustomDevice
/// mode.
class PD_INFER_DECL CustomDevicePassStrategy final : public PassStrategy {
 public:
  CustomDevicePassStrategy() : PassStrategy({}) { use_custom_device_ = true; }

  /// \brief Construct by copying another CustomDevicePassStrategy object.
  /// \param[in] other The CustomDevicePassStrategy object we want to copy.
  explicit CustomDevicePassStrategy(const CustomDevicePassStrategy &other)
      : PassStrategy(other.AllPasses()) {
    use_custom_device_ = true;
  }
};

J
jianghaicheng 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/// \class IpuPassStrategy
/// \brief The IPU passes controller, it is used in AnalysisPredictor with IPU
/// mode.
class PD_INFER_DECL IpuPassStrategy final : public PassStrategy {
 public:
  /// \brief Default constructor of IpuPassStrategy.
  IpuPassStrategy();

  /// \brief Construct by copying another IpuPassStrategy object.
  /// \param[in] other The IpuPassStrategy object we want to copy.
  explicit IpuPassStrategy(const IpuPassStrategy &other)
      : PassStrategy(other.AllPasses()) {
    use_ipu_ = true;
  }
};

324
/// \brief List of tensorRT subgraph passes.
325
PD_INFER_DECL extern const std::vector<std::string> kTRTSubgraphPasses;
326

D
denglin-github 已提交
327 328 329
/// \brief List of dlnne subgraph passes.
PD_INFER_DECL extern const std::vector<std::string> kDlnneSubgraphPasses;

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

333 334 335
/// \brief List of cinn compiler passes.
PD_INFER_DECL extern const std::vector<std::string> kCINNCompilerPasses;

336 337 338 339 340 341
/// \brief TODO(inference): Most of the existing pass fusion operators do not
/// support fp16/bf16 precision, temporarily use low precision pass to prevent
/// running errors. After fusion operator supports low precision, delete this.
PD_INFER_DECL extern const std::vector<std::string> kGpuLowerPrecisionPasses;
PD_INFER_DECL extern const std::vector<std::string> kTrtLowerPrecisionPasses;

342
}  // namespace paddle