Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple of ir utilities #1972

Merged
merged 6 commits into from
Dec 9, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions onnxscript/rewriter/_ir_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,70 @@
# Licensed under the MIT License.
from __future__ import annotations

import numpy as np

import onnxscript.ir as ir
from onnxscript.optimizer import basic_constant_propagation


def display_slice(x: ir.Value | ir.Node, backward: bool = True, depth_limit: int = 5):
shubhambhokare1 marked this conversation as resolved.
Show resolved Hide resolved
"""Display the subgraph computing a given value or node upto a certain depth."""
slice = []

def visit(node: ir.Node, depth):
if node in slice:
return
slice.append(node)
if depth < depth_limit:
if backward:
for inp in node.inputs:
if inp is not None and inp.producer() is not None:
visit(inp.producer(), depth + 1) # type: ignore[arg-type]
else:
for out in node.outputs:
for consumer, _ in out.uses():
visit(consumer, depth + 1)

if isinstance(x, ir.Node):
visit(x, 0)
elif isinstance(x, ir.Value) and x.producer() is not None:
visit(x.producer(), 0) # type: ignore[arg-type]
if slice:
graph = slice[0].graph
if graph:
# Display nodes in same order as in graph:
# Currently doesn't handle (control-flow) subgraphs
for node in graph:
if node in slice:
node.display()
else:
for node in reversed(slice):
node.display()


def get_const_value(value: ir.Value) -> ir.TensorProtocol | None:
node = value.producer()
if node is not None:
basic_constant_propagation([node])
return value.const_value


def get_numpy_value(val: ir.Value | None) -> np.ndarray | None:
gramalingam marked this conversation as resolved.
Show resolved Hide resolved
if val is None:
return None
const_value = val.const_value
if const_value is not None:
try:
return const_value.numpy()
except FileNotFoundError:
# External data is not available.
return None
return None


def get_singleton_value(val: ir.Value | None):
shubhambhokare1 marked this conversation as resolved.
Show resolved Hide resolved
"""Returns element of a single element tensor constant value, and None otherwise."""
np_val = get_numpy_value(val)
if np_val is not None and np_val.size == 1:
return np_val.item()
return None
Loading