Skip to content

Commit

Permalink
[IR] Create a convenience function to create name->value mappings (#1704
Browse files Browse the repository at this point in the history
)

I realized we also cannot maintain the mapping in the
IR, because we don’t require values to always have a name while in IR.

---------

Co-authored-by: G. Ramalingam <[email protected]>
  • Loading branch information
justinchuby and gramalingam authored Jul 3, 2024
1 parent c38d6f8 commit 619f5ed
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions onnxscript/ir/_convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,29 @@ def tensor(
doc_string=name,
)
return tensor_


def create_value_mapping(graph: _core.Graph) -> dict[str, _core.Value]:
"""Return a dictionary mapping names to values in the graph.
The mapping does not include values from subgraphs.
Args:
graph: The graph to extract the mapping from.
Returns:
A dictionary mapping names to values.
"""
values = {}
values.update(graph.initializers)
# The names of the values can be None or "", which we need to exclude
for input in graph.inputs:
if not input.name:
continue
values[input.name] = input
for node in graph:
for value in node.outputs:
if not value.name:
continue
values[value.name] = value
return values

0 comments on commit 619f5ed

Please sign in to comment.