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

Pass match object to conditional function. #2088

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rezaasjd
Copy link

@rezaasjd rezaasjd commented Mar 4, 2025

Match object can be used to single out a replacement to a pattern that was matched if the user intends to.
This would be helpful since node.name is not passed to replacer functions and as a result user cannot have logic based on the node names that were part of the match.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This PR updates the conditional function invocation in the pattern rewriter so that the full match object is passed instead of a placeholder context.

  • Removed the unused assignment of a dummy context.
  • Updated the _condition_function call to receive the match object for improved flexibility in node-based logic.

Reviewed Changes

File Description
onnxscript/rewriter/pattern.py Updated _condition_function call to pass the match object and removed redundant context assignment.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

onnxscript/rewriter/pattern.py:1373

  • Ensure that _condition_function is updated to accept a MatchResult object instead of a context parameter and that its documentation reflects this new interface.
if not self._condition_function(match, **match.bindings):

onnxscript/rewriter/pattern.py:1368

  • Remove the obsolete 'context = None' assignment since it is no longer used after passing the match object.
context = None  # TODO(rama)

Copy link

codecov bot commented Mar 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.99%. Comparing base (3d8f64a) to head (5611803).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2088   +/-   ##
=======================================
  Coverage   72.99%   72.99%           
=======================================
  Files         216      216           
  Lines       28945    28944    -1     
  Branches     3428     3428           
=======================================
  Hits        21128    21128           
  Misses       6665     6665           
+ Partials     1152     1151    -1     

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

@justinchuby justinchuby self-requested a review March 21, 2025 14:53
@justinchuby
Copy link
Collaborator

cc @gramalingam for your intention on what the context object should be

@justinchuby
Copy link
Collaborator

Thanks for creating the PR! Sounds like a good idea to me to include this bit of information; but I guess we also need to make sure users will not be able to alter the original model in a replacement function (?) Also the context class needs to be forward compatible so we may want to wrap the match with something else. I will leave this for @gramalingam to decide

@justinchuby
Copy link
Collaborator

@rezaasjd could you share a concrete use case?

@gramalingam
Copy link
Collaborator

Can you clarify what you mean by "node name"? I think you mean the actual nodes that matched specific pattern-nodes?

If so, one thing you can currently do is to name intermediate-values in the pattern, and get them as part of the bindings. For example, using

   some_value_z = op.Concat(x, y, axis=-2, _outputs=["some_value"])

in the pattern will bind "some_value" to the ir.Value that is the output of the matching Concat node. So, you can use it in the condition function as

   def condition_function(context, ..., some_value, ...):
      concat_node = some_value.producer()
      ...

Does that help?

@rezaasjd
Copy link
Author

@gramalingam this would work too!

Just out of curiosity, is there a plan to pass in more info to conditional function? I am asking this because the infra is there to do conditioned replacement, but the information coming from bindings may be limited.

I can imagine I will need to condition based on the actual value of some initializer in the future and while that is doable right now, it's not very straightforward.

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.

3 participants