Skip to content

Commit aec7b2e

Browse files
committed
Improve handling of attr ref
Signed-off-by: Ganesan Ramalingam <[email protected]>
1 parent 843afce commit aec7b2e

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

onnxscript/_internal/converter.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -534,32 +534,44 @@ def _translate_attr(
534534

535535
if isinstance(expr, ast.Name):
536536
val = self._lookup(expr.id, self._source_of(expr))
537-
if isinstance(val, values.AttrRef):
538-
attr_type = ir.AttributeType(ta.pytype_to_attrtype(val.typeinfo))
539-
attr_ref = ir.Attr(attr_name, attr_type, None, ref_attr_name=val.value.name)
540-
if attr_meta is not None and (attr_ref.type != attr_meta.type):
541-
self.fail(
542-
expr,
543-
f"Attribute type '{attr_ref.type}' does not match expected type '{attr_meta.type}'",
537+
if isinstance(val, values.SymbolValue):
538+
val = val.value
539+
if isinstance(val, ir.Attr):
540+
# A reference to an attribute parameter:
541+
attr = val
542+
attr_ref = ir.Attr(
543+
attr_name, attr.type, value=None, ref_attr_name=attr.name
544544
)
545-
return attr_ref
546-
if isinstance(val, values.SymbolValue) and isinstance(
547-
val.value, irbuilder.IRFunction
548-
):
549-
irfunction = val.value
550-
# Check that outer-scope variables referenced by function have same value
551-
# at function-definition site and use-as-attribute site, to avoid errors.
552-
for pyvar, previous in irfunction.outer_scope_variables:
553-
current = self._lookup(pyvar, self._source_of(expr))
554-
if current.value != previous.value:
545+
if attr_meta is not None and (attr.type != attr_meta.type):
555546
self.fail(
556547
expr,
557-
f"Outer scope variable '{pyvar}' referenced by function "
558-
f"'{expr.id!r}' modified.",
548+
f"Attribute type '{attr_ref.type}' does not match expected type '{attr_meta.type}'",
559549
)
560-
561-
# Create GraphProto attribute
562-
val = irfunction.to_graph_proto()
550+
return attr_ref
551+
if isinstance(val, irbuilder.IRFunction):
552+
# A reference to a nested-function: convert to GraphProto and use it.
553+
irfunction = val
554+
# Check that outer-scope variables referenced by function have same value
555+
# at function-definition site and use-as-attribute site, to avoid errors.
556+
for pyvar, previous in irfunction.outer_scope_variables:
557+
current = self._lookup(pyvar, self._source_of(expr))
558+
if current.value != previous.value:
559+
self.fail(
560+
expr,
561+
f"Outer scope variable '{pyvar}' referenced by function "
562+
f"'{expr.id!r}' modified.",
563+
)
564+
# Create GraphProto attribute
565+
val = irfunction.to_graph_proto()
566+
if isinstance(val, ir.Value):
567+
self.fail(expr, f"Cannot use ir.Value '{expr.id}' as an attribute.")
568+
else:
569+
# Treat as a constant python-value, to be converted below.
570+
pass
571+
else:
572+
# This must be a reference to an outer-scope python-value, typically a constant.
573+
# The value will be converted to an ONNX attribute value below.
574+
pass
563575
else:
564576
val = self._eval_constant_expr(expr)
565577

0 commit comments

Comments
 (0)