1 /* Copyright 2017 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 #ifndef TENSORFLOW_LITE_GRAPH_INFO_H_ 16 #define TENSORFLOW_LITE_GRAPH_INFO_H_ 17 18 #include <stddef.h> 19 20 #include <cstdint> 21 #include <utility> 22 #include <vector> 23 24 #include "tensorflow/lite/c/common.h" 25 26 namespace tflite { 27 28 // Basic information about an inference graph, where execution nodes 29 // are connected via tensors. 30 class GraphInfo { 31 public: ~GraphInfo()32 virtual ~GraphInfo() {} 33 34 // Total number of tensors in the graph. 35 virtual size_t num_tensors() const = 0; 36 37 // Returns a tensor given its index which is expected to be between 0 and 38 // num_tensors(). 39 virtual TfLiteTensor* tensor(size_t index) = 0; 40 41 // Number of nodes in the current execution plan. 42 virtual size_t num_execution_nodes() const = 0; 43 44 // Total number of known nodes, which may include nodes that are no longer in 45 // the execution plan. This happens in case of applying multiple delegates. 46 // Should be >= num_execution_nodes() 47 virtual size_t num_total_nodes() const = 0; 48 49 // Returns a node given its index in the execution plan, which is expected to 50 // be between 0 and num_execution_nodes(). 51 virtual const TfLiteNode& node(size_t index) const = 0; 52 53 // Returns an implementation-specific node index which may be different from 54 // execution-plan index. 55 // Expected to be between 0 and num_total_nodes(). 56 virtual size_t node_index(size_t index) const = 0; 57 58 // Returns the indices of the input tensors. 59 virtual const std::vector<int>& inputs() const = 0; 60 61 // Returns the indices of the output tensors. 62 virtual const std::vector<int>& outputs() const = 0; 63 64 // Returns the indices of the variable tensors. 65 virtual const std::vector<int>& variables() const = 0; 66 }; 67 68 // Represents a subset of nodes in a TensorFlow Lite graph. 69 struct NodeSubset { 70 enum Type { 71 kTfUnexplored = 0, // temporarily used during creation 72 kTfPartition, 73 kTfNonPartition 74 }; 75 Type type = kTfUnexplored; 76 // Nodes within the node sub set 77 std::vector<int> nodes; 78 // Tensors that stride output from another node sub set that this depends on, 79 // or global inputs to the TensorFlow Lite full graph. 80 std::vector<int> input_tensors; 81 // Outputs that are consumed by other node sub sets or are global output 82 // tensors. All output tensors of the nodes in the node sub set that do not 83 // appear in this list are intermediate results that can be potentially 84 // elided. 85 std::vector<int> output_tensors; 86 }; 87 88 // Node edge.second depends on node edge.first. 89 using ControlEdge = std::pair<int32_t, int32_t>; 90 using ControlEdges = std::vector<ControlEdge>; 91 92 // Partitions a list of node indices `nodes_to_partition` into node subsets. 93 // Each node subset is in dependency order (i.e. all members of the node subsets 94 // can be executed in the order they occur). Maintains the relative ordering of 95 // nodes that have their `might_have_side_effects` attribute set. `node_subsets` 96 // is assumed to be empty. 97 TfLiteStatus PartitionGraphIntoIndependentNodeSubsets( 98 const GraphInfo* info, const TfLiteIntArray* nodes_to_partition, 99 std::vector<NodeSubset>* node_subsets); 100 101 // Partitions a list of node indices `nodes_to_partition` into node subsets. 102 // Each node subset is in dependency order (i.e. all members of the node subset 103 // can be executed in the order they occur). `control_edges` specified a control 104 // dependency DAG on the nodes contained in `info`. The resulting partitioning 105 // will respect these control dependencies. This way, restrictions (in addition 106 // to the nodes' data dependencies) can be imposed on the ultimate execution 107 // order of the graph. 108 // 109 // (Example: with `control_edges.empty()` and `nodes_to_partition == {2, 3}`, 110 // the graph 111 // /------------\ 112 // | v 113 // 0 --> 1 --> 2* --> 3* 4 --> 5 114 // | ^ 115 // \-------------------/ 116 // 117 // will be partitioned as {{0, 1, 4}, {2, 3}, {5}}, since data dependencies 118 // (notated '-->') allow for execution of 4 immediately after 1. 119 // 120 // With an additional control dependency `control_edges == {{3, 4}}` (notated 121 // '==>'), execution of node 4 requires prior execution of node 3: 122 // 123 // /------------\ 124 // | v 125 // 0 --> 1 --> 2* --> 3* ==> 4 --> 5 126 // | ^ 127 // \-------------------/ 128 // 129 // and the partitioning will be {{0, 1}, {2, 3}, {4, 5}}.) 130 // 131 // `node_subsets` is assumed to be empty. 132 TfLiteStatus PartitionGraphIntoIndependentNodeSubsets( 133 const GraphInfo* info, const TfLiteIntArray* nodes_to_partition, 134 const ControlEdges& control_edges, std::vector<NodeSubset>* node_subsets); 135 136 } // namespace tflite 137 138 #endif // TENSORFLOW_LITE_GRAPH_INFO_H_ 139