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

Extend basic matcher to handle multiple-output-nodes #1734

Merged
merged 14 commits into from
Jul 25, 2024

Conversation

gramalingam
Copy link
Collaborator

This PR extends the basic matcher to handle multiple output nodes. This provides an alternative to the generic-matcher algorithm, which is incomplete and fails in some circumstances. This can also be useful in debugging match-failures (when it is unclear if the failure is valid or due to limitations of the matching algorithm). The drawback is that this algorithm can, in some cases, be expensive, especially when the number of output-nodes is large and the graph size is large. (So far, however, we haven't encountered patterns with more than 2 output-nodes.)

Copy link

codecov bot commented Jul 17, 2024

Codecov Report

Attention: Patch coverage is 73.33333% with 28 lines in your changes missing coverage. Please review.

Project coverage is 75.01%. Comparing base (937558f) to head (27e29b8).

Files Patch % Lines
onnxscript/rewriter/pattern.py 73.33% 11 Missing and 13 partials ⚠️
onnxscript/rewriter/generic_pattern.py 20.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1734      +/-   ##
==========================================
+ Coverage   74.96%   75.01%   +0.04%     
==========================================
  Files         245      245              
  Lines       26383    26451      +68     
  Branches     4802     4826      +24     
==========================================
+ Hits        19779    19843      +64     
+ Misses       5682     5676       -6     
- Partials      922      932      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

onnxscript/rewriter/pattern.py Fixed Show fixed Hide fixed
onnxscript/rewriter/pattern.py Fixed Show fixed Hide fixed
onnxscript/rewriter/pattern.py Fixed Show fixed Hide fixed
onnxscript/rewriter/pattern.py Fixed Show fixed Hide fixed
onnxscript/rewriter/pattern.py Fixed Show fixed Hide fixed
onnxscript/rewriter/pattern.py Fixed Show fixed Hide fixed
Copy link

github-actions bot commented Jul 17, 2024

Test Results

     24 files  ±     0      24 suites  ±0   3h 18m 22s ⏱️ - 3m 36s
 13 386 tests  -  2 179  11 818 ✅  - 1 744    1 538 💤  -    434   30 ❌  - 1 
477 208 runs  +17 845  99 962 ✅ +2 627  376 978 💤 +15 219  268 ❌  - 1 

For more details on these failures, see this check.

Results for commit 27e29b8. ± Comparison against base commit 937558f.

This pull request removes 2179 tests.
onnxscript._internal.analysis_test.TestAssignedVarAnalysis ‑ test_basic_defs
onnxscript._internal.analysis_test.TestAssignedVarAnalysis ‑ test_doc_string
onnxscript._internal.analysis_test.TestAssignedVarAnalysis ‑ test_if_defs
onnxscript._internal.analysis_test.TestAssignedVarAnalysis ‑ test_if_loop_defs
onnxscript._internal.analysis_test.TestAssignedVarAnalysis ‑ test_loop_defs
onnxscript._internal.analysis_test.TestExposedUses ‑ test_basic
onnxscript._internal.analysis_test.TestExposedUses ‑ test_called_function
onnxscript._internal.analysis_test.TestExposedUses ‑ test_doc_string
onnxscript._internal.analysis_test.TestExposedUses ‑ test_for_loop
onnxscript._internal.analysis_test.TestExposedUses ‑ test_if
…

♻️ This comment has been updated with latest results.

@justinchuby justinchuby self-assigned this Jul 18, 2024
@justinchuby
Copy link
Collaborator

when the number of output-nodes is large

Clarifying to understand better: is it referring to that of the non-match subgraphs or the target pattern? For example, if we have a simple pattern with small outputs and a graph with many outputs, will it still be costly?

@gramalingam
Copy link
Collaborator Author

when the number of output-nodes is large

Clarifying to understand better: is it referring to that of the non-match subgraphs or the target pattern? For example, if we have a simple pattern with small outputs and a graph with many outputs, will it still be costly?

The complexity is sort of (Number of graph nodes) ^ (Number of pattern-output-nodes) ... here a pattern-output-node is a pattern-node that produces an output-value of the pattern (again, loosely speaking). In reality, Number of graph nodes is a worst-case estimate, it really is number of nodes with same optype as corresponding pattern-output-node.

So, if we are looking for a pattern Z1 = Sin(X), Z2 = Cos(X), return Z1, Z2 both statements are pattern-output-nodes. If NS is the number of Sin nodes in graph, and NC is the number of Cos nodes in graph, the complexity is NSxNC.

Comment on lines +682 to +683
@property
def output_node(self) -> NodePattern:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a method instead? Properties can usually be assigned

Suggested change
@property
def output_node(self) -> NodePattern:
def output_node(self) -> NodePattern:

@@ -706,18 +710,18 @@ def __reversed__(self) -> Iterator[NodePattern]:

@property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@property

nit

if output_values is None:
return match
if not _valid_to_replace(match.nodes, output_values):
return match.fail("Matched nodes have other uses preventing replacement.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print all uses for info?

@gramalingam gramalingam merged commit 19f1126 into main Jul 25, 2024
30 of 43 checks passed
@gramalingam gramalingam deleted the rama/simple-multi-output branch July 25, 2024 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants