xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/tf2tensorrt/convert/trt_optimization_pass.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_TF2TENSORRT_CONVERT_TRT_OPTIMIZATION_PASS_H_
17 #define TENSORFLOW_COMPILER_TF2TENSORRT_CONVERT_TRT_OPTIMIZATION_PASS_H_
18 
19 #include <memory>
20 #include <string>
21 
22 #include "tensorflow/compiler/tf2tensorrt/convert/trt_parameters.h"
23 #include "tensorflow/compiler/tf2tensorrt/convert/utils.h"
24 #include "tensorflow/core/framework/graph.pb.h"
25 #include "tensorflow/core/grappler/costs/graph_properties.h"
26 #include "tensorflow/core/grappler/optimizers/custom_graph_optimizer.h"
27 #include "tensorflow/core/grappler/utils.h"
28 #include "tensorflow/core/platform/logging.h"
29 
30 #if GOOGLE_CUDA && GOOGLE_TENSORRT
31 
32 #if !IS_TRT_VERSION_GE(7, 0, 0, 0)
33 #error From version 2.6, we only support NVIDIA TensorRT version 7 or newer.
34 #error Please update your environment and relaunch the compilation.
35 #endif
36 
37 namespace tensorflow {
38 namespace tensorrt {
39 namespace convert {
40 
41 class TRTOptimizationPass : public grappler::CustomGraphOptimizer {
42  public:
43   struct ConversionParams {
44     string trt_logger_name = "DefaultLogger";
45     size_t max_batch_size = -1;
46     size_t max_workspace_size_bytes = 1 << 30;
47     TrtPrecisionMode precision_mode = TrtPrecisionMode::FP32;
48     int minimum_segment_size = 3;
49     // Whether to create engine on conversion or execution time
50     bool is_dynamic_op = false;
51     // maximum number of cached engines
52     int max_cached_engines = 1;
53     bool use_calibration = true;
54     bool use_implicit_batch = true;
55     ProfileStrategy profile_strategy = ProfileStrategy::kRange;
56     bool allow_build_at_runtime = true;
57     bool use_explicit_precision = false;
58   };
59 
60   TRTOptimizationPass(const string& name = "TRTOptimizationPass")
name_(name)61       : name_(name) {}
62 
name()63   string name() const override { return name_; };
64 
UsesFunctionLibrary()65   bool UsesFunctionLibrary() const override { return true; }
66 
67   Status Init(
68       const RewriterConfig_CustomGraphOptimizer* config = nullptr) override;
69 
70   Status Optimize(grappler::Cluster* cluster,
71                   const grappler::GrapplerItem& item,
72                   GraphDef* optimized_graph) override;
73 
74  private:
75   const string name_;
76 
77   ConversionParams params_;
78 
79   std::vector<int> batches_;
80 };
81 
82 }  // namespace convert
83 }  // namespace tensorrt
84 }  // namespace tensorflow
85 
86 #endif  // GOOGLE_CUDA && GOOGLE_TENSORRT
87 #endif  // TENSORFLOW_COMPILER_TF2TENSORRT_CONVERT_TRT_OPTIMIZATION_PASS_H_
88