• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SEGMENT_SEGMENT_H_
17 #define TENSORFLOW_COMPILER_TF2TENSORRT_SEGMENT_SEGMENT_H_
18 
19 #include <set>
20 #include <vector>
21 
22 #include "absl/types/optional.h"
23 #include "tensorflow/compiler/tf2tensorrt/segment/union_find.h"
24 #include "tensorflow/core/framework/graph.pb.h"
25 #include "tensorflow/core/graph/graph.h"
26 #include "tensorflow/core/grappler/costs/graph_properties.h"
27 #include "tensorflow/core/lib/core/status.h"
28 #include "tensorflow/core/platform/types.h"
29 
30 #if GOOGLE_CUDA && GOOGLE_TENSORRT
31 
32 namespace tensorflow {
33 namespace tensorrt {
34 namespace segment {
35 
36 constexpr char kTftrtOpMaxBatchSizeAttr[] = "_tftrt_op_max_batch_size";
37 
38 struct SegmentOptions {
39   // This struct holds per graph segmenting parameters.
40   // Segment must contain at least this many nodes.
41   int minimum_segment_size = 2;
42   bool use_implicit_batch = true;
43   // The maximum batch size used to build the engines in the graph, when
44   // use_implicit_batch is true.
45   absl::optional<int> maximum_batch_size = absl::nullopt;
46   // When use_implicit_batch is false or when we are building dynamic engines,
47   // we allow dynamic non-batch dimensions.
48   bool allow_dynamic_non_batch_dim = false;
49   // The name of the device to put the segment on.
50   std::set<string> exclude_node_list;
51 };
52 
53 struct NodePtrCompare {
operatorNodePtrCompare54   bool operator()(const Node* lhs, const Node* rhs) const {
55     return lhs->name() < rhs->name();
56   }
57 };
58 
59 struct Segment {
SegmentSegment60   Segment() {}
SegmentSegment61   Segment(const ClusterProperty& property,
62           const std::set<const Node*, NodePtrCompare>& nodes)
63       : property(property), nodes(nodes) {}
64   ClusterProperty property;
65   std::set<const Node*, NodePtrCompare> nodes;
66 };
67 
68 // Vector of segments, each entry contains a set of node pointers.
69 using SegmentVector = std::vector<Segment>;
70 
71 // Get the subgraphs of a graph that can be handled by TensorRT.
72 //
73 // @param tf_graph Graph of the network.
74 // @graph_properties is the static graph properties.
75 // @param candidate_fn A function that returns OK for a Node* if
76 // that node can be handled by TensorRT.
77 // @param segments Returns the TensorRT segments/subgraphs. Each entry
78 // in the vector describes a subgraph by giving a set of the names of
79 // all the NodeDefs in that subgraph.
80 // @return the status.
81 Status SegmentGraph(const Graph* tf_graph,
82                     const grappler::GraphProperties* graph_properties,
83                     const std::function<Status(const Node*)>& candidate_fn,
84                     const std::function<bool(const Edge*)>& input_candidate_fn,
85                     const std::function<bool(const Edge*)>& output_candidate_fn,
86                     const SegmentOptions& options, SegmentVector* segments);
87 
88 }  // namespace segment
89 }  // namespace tensorrt
90 }  // namespace tensorflow
91 
92 #endif  // GOOGLE_CUDA && GOOGLE_TENSORRT
93 
94 #endif  // TENSORFLOW_COMPILER_TF2TENSORRT_SEGMENT_SEGMENT_H_
95