Skip to content

Commit e44bffc

Browse files
committed
Implement the all_values convenience function
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
1 parent c96b72b commit e44bffc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/onnx_ir/_convenience/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,41 @@ def get_const_tensor(
556556
)
557557
value.type = new_value_type
558558
return tensor
559+
560+
561+
def all_values(
562+
graph_like: _core.Graph | _core.Function | _core.GraphView,
563+
) -> Iterable[_core.Value]:
564+
"""Yield all values in a graph / function / graph view.
565+
566+
Args:
567+
graph_like: The graph, function, or graph view to yield values from.
568+
569+
Returns:
570+
An iterable of all values in the graph / function / graph view.
571+
"""
572+
# Yield all values in the model
573+
seen = set()
574+
for value in graph_like.inputs:
575+
if value not in seen:
576+
seen.add(value)
577+
yield value
578+
if not isinstance(graph_like, _core.Function):
579+
for value in graph_like.initializers.values():
580+
if value not in seen:
581+
seen.add(value)
582+
yield value
583+
for node in traversal.RecursiveGraphIterator(graph_like):
584+
# NOTE: If an input or initializer of a subgraph is unused, it will not be yielded by this function since it is not technically part of the graph. This is consistent with ONNX's handling of unused inputs and initializers.
585+
for value in node.inputs:
586+
if value not in seen:
587+
seen.add(value)
588+
yield value
589+
for value in node.outputs:
590+
if value not in seen:
591+
seen.add(value)
592+
yield value
593+
for value in graph_like.outputs:
594+
if value not in seen:
595+
seen.add(value)
596+
yield value

0 commit comments

Comments
 (0)