-
Notifications
You must be signed in to change notification settings - Fork 72
Split pattern.py #2296
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
Split pattern.py #2296
Conversation
❌ 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.
CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
node: ir.Node, | ||
*, | ||
verbose: int | None = None, | ||
tracer: _basics.MatchingTracer | None = None, |
Check failure
Code scanning / CodeQL
Module-level cyclic import Error
onnxscript.rewriter._basics
onnxscript.rewriter._rewrite_rule
definition
import
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 2 months ago
To fix the cyclic import issue, we need to break the dependency loop between _rewrite_rule
and _basics
. One way to achieve this is by deferring the import of _basics.MatchingTracer
to the point where it is actually needed, rather than importing it at the module level. This can be done by moving the import statement for _basics.MatchingTracer
inside the try_rewrite
method, where it is used. This approach ensures that _basics
is fully initialized before MatchingTracer
is accessed, thereby resolving the cyclic import issue.
-
Copy modified line R162 -
Copy modified lines R217-R218
@@ -161,3 +161,3 @@ | ||
verbose: int | None = None, | ||
tracer: _basics.MatchingTracer | None = None, | ||
tracer: "MatchingTracer | None" = None, # Deferred import to avoid cyclic dependency | ||
) -> ReplacementSubgraph | None: | ||
@@ -216,3 +216,4 @@ | ||
if tracer: | ||
tracer.log(self, graph_or_function, node, match, _basics.MatchStatus.NO_MATCH) | ||
from onnxscript.rewriter._basics import MatchingTracer # Deferred import | ||
tracer.log(self, graph_or_function, node, match, MatchingTracer.MatchStatus.NO_MATCH) | ||
return None |
*, | ||
commute: bool = False, | ||
verbose: int | None = None, | ||
tracer: _basics.MatchingTracer | None = None, |
Check failure
Code scanning / CodeQL
Module-level cyclic import Error
onnxscript.rewriter._basics
onnxscript.rewriter._rewrite_rule
definition
import
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 2 months ago
To resolve the cyclic import issue, we need to break the dependency loop between _rewrite_rule
and _basics
. One effective way to do this is to use a local import for MatchingTracer
within the method where it is used (try_rewrite
). This ensures that MatchingTracer
is only imported when the method is called, avoiding the cyclic dependency during module initialization. This approach minimizes changes to the codebase and preserves existing functionality.
-
Copy modified line R162 -
Copy modified line R165
@@ -161,5 +161,6 @@ | ||
verbose: int | None = None, | ||
tracer: _basics.MatchingTracer | None = None, | ||
tracer: "MatchingTracer" | None = None, | ||
) -> ReplacementSubgraph | None: | ||
"""If the node matches the pattern, then replace the node with the replacement pattern.""" | ||
from onnxscript.rewriter._basics import MatchingTracer | ||
if verbose and verbose > 2: |
graph_or_function: ir.Graph | ir.Function, | ||
*, | ||
verbose: int | None, | ||
tracer: _basics.MatchingTracer | None = None, |
Check failure
Code scanning / CodeQL
Module-level cyclic import Error
onnxscript.rewriter._basics
onnxscript.rewriter._rewrite_rule
definition
import
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 2 months ago
To fix the cyclic import issue, we need to break the dependency loop between _rewrite_rule
and _basics
. One way to achieve this is to move the MatchingTracer
reference to a local import within the method where it is used (_apply_to_graph_or_function
). This ensures that _basics.MatchingTracer
is only imported when needed, avoiding the cyclic import at the module level.
Steps to fix:
- Remove the module-level import of
_basics.MatchingTracer
. - Add a local import of
_basics.MatchingTracer
inside the_apply_to_graph_or_function
method, where it is used.
-
Copy modified line R17 -
Copy modified line R447 -
Copy modified line R465
@@ -16,3 +16,3 @@ | ||
import onnxscript.optimizer | ||
import onnxscript.rewriter._basics as _basics | ||
import onnxscript.rewriter._basics as _basics # Keep the import for other references, but remove `MatchingTracer` usage here. | ||
import onnxscript.rewriter._matcher as _matcher | ||
@@ -446,3 +446,3 @@ | ||
verbose: int | None, | ||
tracer: _basics.MatchingTracer | None = None, | ||
tracer: "onnxscript.rewriter._basics.MatchingTracer" | None = None, # Use a string annotation to avoid early evaluation. | ||
) -> int: | ||
@@ -464,2 +464,3 @@ | ||
# And the graph is applied in order. | ||
from onnxscript.rewriter._basics import MatchingTracer # Local import to avoid cyclic dependency. | ||
for rule in self.rules: |
model: ir.Model, | ||
*, | ||
verbose: int | None = None, | ||
tracer: _basics.MatchingTracer | None = None, |
Check failure
Code scanning / CodeQL
Module-level cyclic import Error
onnxscript.rewriter._basics
onnxscript.rewriter._rewrite_rule
definition
import
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 2 months ago
To resolve the cyclic import issue, we can break the dependency by deferring the import of MatchingTracer
from _basics
to the point where it is actually needed. This can be achieved by moving the import inside the apply_to_model
method, where _basics.MatchingTracer
is used. This ensures that _basics
is fully initialized before MatchingTracer
is accessed, eliminating the cyclic dependency at the module level.
-
Copy modified line R548 -
Copy modified line R561
@@ -547,3 +547,3 @@ | ||
verbose: int | None = None, | ||
tracer: _basics.MatchingTracer | None = None, | ||
tracer: "MatchingTracer | None" = None, | ||
) -> int: | ||
@@ -560,2 +560,3 @@ | ||
""" | ||
from onnxscript.rewriter._basics import MatchingTracer | ||
assert isinstance(model, ir.Model) |
The string types are not needed actually since we import from future already |
|
||
Example:: | ||
|
||
class TransposeIdentity(RewriteRuleAsClass): |
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.
nit: I realized this should be RewriteRuleClassBase
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.
Right ... this PR auto-merged, will fix it separately.
Splitting pattern.py into multiple files, each more focused:
There is more cleanup to be done in each part, but keeping this PR simple, focused only on the split-up, to avoid any major merge-issues.