HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
graph_viewer.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include "core/graph/graph.h"
7 #include "core/framework/session_options.h"
8 #include <unordered_set>
9 
10 namespace onnxruntime {
11 class Function;
12 struct IndexedSubGraph;
13 } // namespace onnxruntime
14 
15 namespace onnxruntime {
16 
17 // use value-based compare to make sure transformer output order is consistent
18 struct NodeCompare {
19  bool operator()(const Node* n1, const Node* n2) const;
20 };
21 
22 /**
23 @class GraphViewer
24 Class that provides a read-only view of the Graph.
25 @remarks If the underlying Graph is changed, GetNodesInTopologicalOrder and GetRootNodes may become invalid.
26 */
27 class GraphViewer {
28  public:
29  /**
30  Construct a GraphViewer from the provided Graph instance.
31  */
32  explicit GraphViewer(const Graph& graph);
33 
34  /**
35  Construct a GraphViewer from the provided Graph instance, filtering to the nodes specified in the IndexedSubGraph
36  */
37  explicit GraphViewer(const Graph& graph, const IndexedSubGraph& filter_info);
38 
39  /** Gets the Graph name. */
40  const std::string& Name() const noexcept;
41 
42  /** Gets the Graph description. */
43  const std::string& Description() const noexcept;
44 
45  /** Gets the path of the owning model if any **/
46  const Path& ModelPath() const noexcept { return graph_->ModelPath(); }
47 
48  /**
49  Gets a tensor created from an initializer.
50  @param tensor_name The tensor name
51  @param[out] value Sets the pointer to the TensorProto if found, or nullptr if not.
52  @returns True if found. False if not.
53  */
54  bool GetInitializedTensor(const std::string& tensor_name, const ONNX_NAMESPACE::TensorProto*& value) const;
55 
56  /** Returns true if an initializer value can be overridden by a graph input with the same name. */
57  bool CanOverrideInitializer() const noexcept;
58 
59  /**
60  Gets the Graph inputs, excluding initializers.
61  @returns Collection of NodeArg pointers for the graph inputs, excluding inputs that have matching initializers.
62  @remarks No nullptr values in the returned collection. The order will be the same as in the GraphProto.
63  Inputs are for filter_info_ if set.
64  */
65  const std::vector<const NodeArg*>& GetInputs() const noexcept;
66 
67  /**
68  Gets the Graph inputs, including any initializers.
69  @returns Collection of NodeArg pointers for all the graph inputs.
70  @remarks No nullptr values in the returned collection. The order will be the same as in the GraphProto.
71  Inputs are for filter_info_ if set.
72  */
73  const std::vector<const NodeArg*>& GetInputsIncludingInitializers() const noexcept;
74 
75  /**
76  Gets the Graph outputs.
77  @returns Collection of NodeArg pointers for all the graph outputs.
78  @remarks No nullptr values in the returned collection. The order will be the same as in the GraphProto.
79  Outputs are for filter_info_ if set.
80  */
81  const std::vector<const NodeArg*>& GetOutputs() const noexcept;
82 
83  /** Returns true if one or more of the Node outputs are Graph outputs.
84  */
85  bool NodeProducesGraphOutput(const Node& node) const;
86 
87  /** Gets all ValueInfo NodeArg instances in the Graph.
88  @remarks NOT filtered using filter_info_.
89  */
90  const std::unordered_set<const NodeArg*>& GetValueInfo() const noexcept;
91 
92  /**
93  Gets the Node instance at the specified index.
94  @param node_index Index to retrieve Node from.
95  @remarks May return nullptr if index no longer points to a valid node due to the node being freed, or if
96  node is excluded by filter_info_.
97  */
98  const Node* GetNode(NodeIndex node_index) const;
99 
100  /** Gets an iterator over all the valid Nodes in the Graph.
101  @remarks Nodes are filtered using filter_info_ if set.
102  */
103  const ConstGraphNodes& Nodes() const noexcept;
104 
105  /** Gets the number of valid nodes in the Graph.
106  @remarks Returns the number of nodes in filter_info_ if set.
107  */
108  int NumberOfNodes() const noexcept;
109 
110  /** Gets the maximum NodeIndex value used by Nodes in the Graph. */
111  int MaxNodeIndex() const noexcept;
112 
113  /** Gets the NodeIndex values for the Graph nodes, sorted into topological order.
114  @remarks Filtered using filter_info_ if set.
115  */
116  const std::vector<NodeIndex>& GetNodesInTopologicalOrder(ExecutionOrder order = ExecutionOrder::DEFAULT) const;
117 
118  /**
119  Gets the NodeIndex values for the root nodes in the Graph.
120  The root nodes are the topmost nodes in the Graph that receive inputs from the Graph inputs
121  and no other nodes in the Graph.
122  @remarks Not supported if filter_info_ is set.
123  */
124  const std::vector<NodeIndex>& GetRootNodes() const;
125 
126  /** Gets all tensors created from initializers. */
127  const InitializedTensorSet& GetAllInitializedTensors() const noexcept;
128 
129  /**
130  Gets the NodeArg instance for the given name.
131  @returns A NodeArg if found, a nullptr if not.
132  */
133  const NodeArg* GetNodeArg(const std::string& name) const;
134 
135  /** Gets the map of operator domains to their opset versions. */
136  const std::unordered_map<std::string, int>& DomainToVersionMap() const noexcept {
137  return graph_->DomainToVersionMap();
138  }
139 
140  /** Checks if this is a Subgraph */
141  bool IsSubgraph() const;
142 
143  /** Get the internal graph*/
144  const Graph& GetGraph() const { return *graph_; }
145 
146 #if !defined(ORT_MINIMAL_BUILD)
147  const std::unordered_set<std::string>& GetOuterScopeNodeArgNames() const noexcept;
148 #endif
149 
150  /**
151  returns true if 'name' is an initializer, and is constant and cannot be overridden at runtime.
152  @param check_outer_scope If true and the 'graph_' is a subgraph, check parent graph/s for 'name'
153  if the name is not found in 'graph_'.
154  */
155  bool IsConstantInitializer(const std::string& name, bool check_outer_scope) const;
156 
157  /** Check if a given name is an initializer tensor's name in this graph. */
158  bool IsInitializedTensor(const std::string& name) const;
159 
160  /** returns the initializer's TensorProto if 'name' is an initializer, is constant and
161  cannot be overridden at runtime. If the initializer is not found or is not constant, a nullptr is returned.
162  @param check_outer_scope If true and the graph is a subgraph,
163  check ancestor graph/s for 'name' if not found in 'graph'.
164  @remarks This function will return the result from GetConstantInitializer of the underlying Graph,
165  if a const initializer is part of the underlying Graph but not part of this GraphViewer,
166  it will still be returned instead of nullptr
167  */
168  const ONNX_NAMESPACE::TensorProto* GetConstantInitializer(const std::string& name, bool check_outer_scope) const;
169 
170  /** Get the Node containing this Graph if IsSubgraph is true. Returns nullptr otherwise. */
171  const Node* ParentNode() const noexcept { return graph_->ParentNode(); }
172 
173 #if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
174  /** Get the consumer nodes of a node arg */
175  std::vector<const Node*> GetConsumerNodes(const std::string& node_arg_name) const {
176  return graph_->GetConsumerNodes(node_arg_name);
177  }
178 
179  /** Get the producer node of a node arg */
180  const Node* GetProducerNode(const std::string& node_arg_name) const {
181  return graph_->GetProducerNode(node_arg_name);
182  }
183 #endif
184 
185  /** Get the filter info that restricts the graph viewer to a subset of nodes if set.
186  @returns Filter info or nullptr
187  */
188  const IndexedSubGraph* GetFilterInfo() const { return filter_info_; }
189 
190 #if !defined(ORT_MINIMAL_BUILD)
192 #endif
193 
194  private:
195  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(GraphViewer);
196  GraphViewer(const Graph& graph, const IndexedSubGraph* filter_info);
197 
198  const Graph* graph_;
199  ConstGraphNodes graph_nodes_;
200 
201  // The NodeIndex values of the graph nodes sorted in topological order.
202  std::vector<NodeIndex> nodes_in_topological_order_;
203 
204 #if !defined(ORT_MINIMAL_BUILD)
205  // The NodeIndex values of the graph nodes sorted in topological order with priority.
206  std::vector<NodeIndex> nodes_in_topological_order_with_priority_;
207 #endif
208 
209  // Graph root nodes.
210  std::vector<NodeIndex> root_nodes_;
211 
212  // if we're limiting the view to an IndexedSubGraph we need to create a few pieces of infrastructure that would
213  // usually come from the full graph
214  const IndexedSubGraph* filter_info_{nullptr};
215  using FilteredNodeSet = InlinedHashSet<NodeIndex>;
216  FilteredNodeSet filtered_node_indices_;
217  std::vector<const NodeArg*> filtered_node_inputs_;
218  std::vector<const NodeArg*> filtered_node_inputs_including_initializers_;
219  std::vector<const NodeArg*> filtered_node_outputs_;
220  InitializedTensorSet filtered_initializers_;
221 };
222 } // namespace onnxruntime
std::unordered_map< std::string, const ONNX_NAMESPACE::TensorProto * > InitializedTensorSet
Definition: basic_types.h:33
const IndexedSubGraph * GetFilterInfo() const
Definition: graph_viewer.h:188
const Node * ParentNode() const noexcept
Definition: graph_viewer.h:171
IOnnxRuntimeOpSchemaCollectionPtr GetSchemaRegistry() const
bool IsConstantInitializer(const std::string &name, bool check_outer_scope) const
std::shared_ptr< IOnnxRuntimeOpSchemaCollection > IOnnxRuntimeOpSchemaCollectionPtr
Definition: basic_types.h:44
const InitializedTensorSet & GetAllInitializedTensors() const noexcept
bool operator()(const Node *n1, const Node *n2) const
const std::vector< const NodeArg * > & GetInputs() const noexcept
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
int NumberOfNodes() const noexcept
const Node * ParentNode() const
Definition: graph.h:1332
bool NodeProducesGraphOutput(const Node &node) const
const std::unordered_map< std::string, int > & DomainToVersionMap() const noexcept
Definition: graph.h:1092
bool IsInitializedTensor(const std::string &name) const
const Path & ModelPath() const noexcept
Definition: graph_viewer.h:46
int MaxNodeIndex() const noexcept
const ONNX_NAMESPACE::TensorProto * GetConstantInitializer(const std::string &name, bool check_outer_scope) const
const std::string & Description() const noexcept
const std::string & Name() const noexcept
IOnnxRuntimeOpSchemaCollectionPtr GetSchemaRegistry() const
Definition: graph_viewer.h:191
GraphViewer(const Graph &graph)
const Node * GetProducerNode(const std::string &node_arg_name) const
Definition: graph_viewer.h:180
const ConstGraphNodes & Nodes() const noexcept
const std::unordered_set< const NodeArg * > & GetValueInfo() const noexcept
const std::vector< NodeIndex > & GetRootNodes() const
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
GLuint const GLchar * name
Definition: glcorearb.h:786
bool CanOverrideInitializer() const noexcept
const std::vector< const NodeArg * > & GetInputsIncludingInitializers() const noexcept
bool IsSubgraph() const
const Node * GetProducerNode(const std::string &node_arg_name) const
Definition: graph.h:1232
const std::unordered_set< std::string > & GetOuterScopeNodeArgNames() const noexcept
const std::vector< const NodeArg * > & GetOutputs() const noexcept
const Path & ModelPath() const
std::vector< const Node * > GetConsumerNodes(const std::string &node_arg_name) const
Definition: graph.h:1250
const Node * GetNode(NodeIndex node_index) const
Definition: core.h:1131
const Graph & GetGraph() const
Definition: graph_viewer.h:144
const NodeArg * GetNodeArg(const std::string &name) const
std::unordered_map< std::string, int > DomainToVersionMap
const std::vector< NodeIndex > & GetNodesInTopologicalOrder(ExecutionOrder order=ExecutionOrder::DEFAULT) const
std::vector< const Node * > GetConsumerNodes(const std::string &node_arg_name) const
Definition: graph_viewer.h:175
size_t NodeIndex
Definition: basic_types.h:30
bool GetInitializedTensor(const std::string &tensor_name, const ONNX_NAMESPACE::TensorProto *&value) const