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 <vector> 19 20 #include "tensorflow/lite/c/c_api_internal.h" 21 22 namespace tflite { 23 24 // Basic information about an inference graph, where execution nodes 25 // are connected via tensors. 26 class GraphInfo { 27 public: ~GraphInfo()28 virtual ~GraphInfo() {} 29 30 // Total number of tensors in the graph. 31 virtual size_t num_tensors() const = 0; 32 33 // Returns a tensor given its index which is expected to be between 0 and 34 // num_tensors(). 35 virtual TfLiteTensor* tensor(size_t index) = 0; 36 37 // Total number of nodes in the graph. 38 virtual size_t num_nodes() const = 0; 39 40 // Returns a node given its index which is expected to be between 0 and 41 // num_nodes(). 42 virtual const TfLiteNode& node(size_t index) const = 0; 43 44 // Returns the indices of the input tensors. 45 virtual const std::vector<int>& inputs() const = 0; 46 47 // Returns the indices of the output tensors. 48 virtual const std::vector<int>& outputs() const = 0; 49 50 // Returns the indices of the variable tensors. 51 virtual const std::vector<int>& variables() const = 0; 52 }; 53 54 // Represents a subset of nodes in a TensorFlow Lite graph. 55 struct NodeSubset { 56 enum Type { 57 kTfUnexplored = 0, // temporarily used during creation 58 kTfPartition, 59 kTfNonPartition 60 }; 61 Type type = kTfUnexplored; 62 // Nodes within the node sub set 63 std::vector<int> nodes; 64 // Tensors that stride output from another node sub set that this depends on, 65 // or global inputs to the TensorFlow Lite full graph. 66 std::vector<int> input_tensors; 67 // Outputs that are consumed by other node sub sets or are global output 68 // tensors. All output tensors of the nodes in the node sub set that do not 69 // appear in this list are intermediate results that can be potentially 70 // elided. 71 std::vector<int> output_tensors; 72 }; 73 74 // Partitions a list of node indices `nodes_to_partition` into node sub sets. 75 // Each node sub set is in dependency order (i.e. all members of the node sub 76 // sets). `node_subsets` is assumed to be empty. 77 TfLiteStatus PartitionGraphIntoIndependentNodeSubsets( 78 const GraphInfo* info, const TfLiteIntArray* nodes_to_partition, 79 std::vector<NodeSubset>* node_subsets); 80 81 } // namespace tflite 82 83 #endif // TENSORFLOW_LITE_GRAPH_INFO_H_ 84