Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 15, 2025

This PR introduces the PatternMatchContext class to provide context information during pattern matching in the ONNX rewriter system.

Changes Made

Core Implementation

  • Added PatternMatchContext class in onnxscript/rewriter/_basics.py with read-only properties:

    • model: The model being matched
    • graph_or_function: The graph or function being matched
    • main_root_node: The main root node of the matching subgraph
    • output_values: The output values of the matching subgraph
    • nodes: All nodes of the matching subgraph
  • Updated pattern matching logic in onnxscript/rewriter/_rewrite_rule.py at line 134 to create and pass PatternMatchContext instances to condition functions

  • Exported the new class in the rewriter module's __all__ list for external use

Usage Example

def condition_with_context(context, x, y):
    # Access match context information
    model = context.model
    main_node = context.main_root_node
    matched_nodes = context.nodes
    outputs = context.output_values
    
    # Use context for advanced pattern validation
    if main_node.op_type == "Mul" and len(matched_nodes) > 1:
        return True
    return False

rule = pattern.RewriteRule(
    target_pattern, 
    replacement_pattern,
    condition_function=condition_with_context
)

Testing

  • Comprehensive test suite in onnxscript/rewriter/pattern_match_context_test.py covering:
    • Property access and type validation
    • Read-only behavior enforcement
    • Backward compatibility with existing condition functions
    • Practical usage scenarios in real pattern matching

Backward Compatibility

  • All existing condition functions continue to work unchanged
  • The context parameter is passed as the first argument, maintaining the existing **match.bindings pattern
  • No breaking changes to the existing API

Validation

  • All existing rewriter tests pass (39/39 tests in pattern-related modules)
  • New functionality validated with 4 comprehensive test cases
  • Integration testing confirms proper context creation and usage

Fixes #2454.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Pattern match context Implement PatternMatchContext class for rewriter pattern matching Jul 15, 2025
@Copilot Copilot AI requested a review from gramalingam July 15, 2025 05:02
Copilot finished work on behalf of gramalingam July 15, 2025 05:02
Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
@Copilot Copilot AI requested a review from gramalingam July 15, 2025 05:24
Copilot finished work on behalf of gramalingam July 15, 2025 05:24
…omments

Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
@Copilot Copilot AI requested a review from gramalingam July 15, 2025 05:29
Copilot finished work on behalf of gramalingam July 15, 2025 05:29
Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
Copilot finished work on behalf of gramalingam July 15, 2025 05:33
@gramalingam
Copy link
Collaborator

@copilot can you add a brief section in the rewriter tutorial explaining this context object?

…Context

Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
Copilot finished work on behalf of gramalingam July 15, 2025 15:37
@Copilot Copilot AI requested a review from gramalingam July 15, 2025 15:37
Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
Copilot finished work on behalf of gramalingam July 15, 2025 15:45
Copy link
Collaborator

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

Run lintrunner f

@justinchuby justinchuby changed the title Implement PatternMatchContext class for rewriter pattern matching Implement MatchContext class for rewriter pattern matching Jul 15, 2025
@gramalingam
Copy link
Collaborator

@copilot can you fix the lint errors by running "lintrunner f" or "lintrunner -a"?

Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 15, 2025

Run lintrunner f

Fixed lint errors by running lintrunner f and ruff format/check --fix --unsafe-fixes. All formatting issues have been resolved. (552e58a)

@copilot can you fix the lint errors by running "lintrunner f" or "lintrunner -a"?

Fixed all lint errors by running lintrunner f and ruff formatting tools. Issues resolved include import sorting, whitespace cleanup, and code formatting. (552e58a)

Copilot finished work on behalf of gramalingam July 15, 2025 19:15
@gramalingam gramalingam enabled auto-merge (squash) July 15, 2025 19:32
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@gramalingam gramalingam merged commit 2f147eb into main Jul 15, 2025
24 of 32 checks passed
@gramalingam gramalingam deleted the copilot/fix-2454 branch July 15, 2025 20:50
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.

Pattern match context
4 participants