-
Notifications
You must be signed in to change notification settings - Fork 72
Remove the RefAttr class #2328
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
Remove the RefAttr class #2328
Conversation
@@ -422,6 +423,8 @@ | |||
value: Any | |||
doc_string: str | None | |||
|
|||
def is_ref(self) -> Literal[False]: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 month ago
To fix the issue, replace the ...
placeholder in the is_ref
method with a concrete implementation that returns False
. This aligns the method's behavior with its type annotation (Literal[False]
) and ensures that the protocol is clear and unambiguous for implementers.
-
Copy modified lines R426-R427
@@ -425,3 +425,4 @@ | ||
|
||
def is_ref(self) -> Literal[False]: ... | ||
def is_ref(self) -> Literal[False]: | ||
return False | ||
|
@@ -441,6 +444,8 @@ | |||
type: _enums.AttributeType | |||
doc_string: str | None | |||
|
|||
def is_ref(self) -> Literal[True]: ... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 month ago
To fix the issue, the is_ref
method in the ReferenceAttributeProtocol
class should be updated to explicitly return True
as a Literal[True]
. This ensures that the method has a concrete implementation that matches its type annotation and intended behavior. The change should be made directly in the ReferenceAttributeProtocol
class definition.
-
Copy modified lines R447-R448
@@ -446,4 +446,4 @@ | ||
|
||
def is_ref(self) -> Literal[True]: ... | ||
|
||
def is_ref(self) -> Literal[True]: | ||
return True | ||
|
❌ 3 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR removes the separate RefAttr
class by merging its functionality into the existing Attr
class and introduces an is_ref()
method for reference checks.
Key changes:
- Consolidated attribute classes and removed
RefAttr
across the IR. - Updated all type annotations and
isinstance(attr, RefAttr)
checks to useAttr
andattr.is_ref()
. - Adjusted serialization/deserialization and protocol definitions to handle reference attributes via
is_ref()
.
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
onnxscript/version_converter/_version_converter.py | Changed visit_attribute signature to ir.Attr . |
onnxscript/rewriter/llama_rule_sets.py | Replaced isinstance(..., RefAttr) with is_ref() . |
onnxscript/rewriter/_rewrite_rule.py | Simplified check signatures; updated copy_attr_value . |
onnxscript/rewriter/_pattern_ir.py | Narrowed AttrPattern to ir.Attr only. |
onnxscript/optimizer/_constant_folding.py | Changed visit_attribute signature to ir.Attr . |
onnxscript/ir/serde.py | Split serialization paths using is_ref() . |
onnxscript/ir/passes/common/inliner.py | Updated clone_attr and attr_map types. |
onnxscript/ir/external_data.py | Replaced isinstance(..., RefAttr) with is_ref() . |
onnxscript/ir/_tape.py | Updated attribute sequences to ir.Attr . |
onnxscript/ir/_protocols.py | Added Literal import and is_ref() to protocols. |
onnxscript/ir/_core.py | Removed RefAttr class; merged into Attr . |
onnxscript/ir/_convenience/_constructors.py | Updated attribute sequences to ir.Attr . |
onnxscript/ir/_convenience/init.py | Removed RefAttr import and tightened types. |
Comments suppressed due to low confidence (3)
onnxscript/version_converter/_version_converter.py:265
- Reference attributes are no longer skipped here—since all attributes are now
Attr
,isinstance(attr, ir.Attr)
will also match refs. Add a guard to skip ref attrs, e.g.,if not attr.is_ref():
before processing.
if isinstance(attr, ir.Attr):
onnxscript/optimizer/_constant_folding.py:1063
- Same as above, reference attributes will match
ir.Attr
and may causevisit_graph
on a ref. Addnot attr.is_ref()
to skip processing reference attrs.
if isinstance(attr, ir.Attr):
onnxscript/rewriter/_rewrite_rule.py:371
- The exception message still references the removed
RefAttr
class. Update it to something like"Reference attributes are not supported."
for clarity.
raise NotImplementedError("RefAttr not supported.")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
Rational
We defined the class
RefAttr
in the IR to represent reference attributes in ONNX. Node attributes can beAttr
andRefAttr
. However, since most of the time we are working with concrete attributes, the union of types creates a typing situation where we always need to assert the types before taking the values, even if we know aRefAttr
cannot exist (outside of a function definition).This additionally matches the definition of AttributeProto in ONNX.
Change
This change merged the two classes, and instead defines a
is_ref()
method for users to check the reference attribute.The change is BC breaking for usage like
isinstance(attr, ir.RefAttr)
. Fortunately all such usages exist in this code base and not in PyTorch, so we are safe to complete the change.