Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions onnxscript/rewriter/_ir_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def visit(node: ir.Node, depth):


def get_const_value(value: ir.Value) -> ir.TensorProtocol | None:
"""Return a runtime constant, excluding overridable graph-input defaults."""
if value.is_graph_input():
return None
node = value.producer()
if node is not None:
optimizer.basic_constant_propagation([node])
Expand All @@ -61,11 +64,8 @@ def get_const_value(value: ir.Value) -> ir.TensorProtocol | None:
def get_numpy_value(val: ir.Value | None) -> np.ndarray | None:
"""Convenience wrapper to get (optional) numpy value from an optional IR Value.

This is intended for use in optimizations/rewriting. Note that this does not
yet handle the distinction between inputs with default values (values that are
both graph inputs and graph initializers), which should not be treated as a
constant, and true constant values. The caller should make the distinction, as
a value does not contain enough information to determine this. (TODO)
This is intended for use in optimizations/rewriting. Graph inputs with
initializer defaults are not constants, since callers can override them.
"""
if val is None:
return None
Expand Down
3 changes: 2 additions & 1 deletion onnxscript/rewriter/_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import onnx_ir as ir

import onnxscript.rewriter._basics as _basics
import onnxscript.rewriter._ir_utils as _ir_utils
import onnxscript.rewriter._pattern_ir as _pattern_ir


Expand Down Expand Up @@ -81,7 +82,7 @@ def _match_constant(self, pattern_constant: _pattern_ir.Constant, value: ir.Valu
if subgraph replacement happens. But subsequent DCE will remove the constant
node if it is not used elsewhere.
"""
constant_value = value.const_value
constant_value = _ir_utils.get_const_value(value)
if constant_value is None:
return self.fail(
f"Value {value.name} is not a constant, expecting {pattern_constant.value}.",
Expand Down
Loading
Loading