Commit 727210b
Expose match functionality of rewrite-rule by extracting base classes (#2447)
This PR extracts the pattern matching functionality from rewrite rules
into standalone base classes, allowing users to use pattern matching
without needing the replacement functionality.
## Changes
### New Base Classes
**`PatternImpl`**: Core pattern matching functionality
- Encapsulates `_target_pattern`, `_matcher`, and `_condition_function`
- Provides `match()` method that returns `MatchResult` or `None`
- Can be used standalone for pattern matching without rewriting
**`PatternBase`**: Base class for class-based pattern definition
- Provides abstract `pattern()` method for defining patterns
- Provides optional `check()` method for condition functions
- Includes `create_pattern_impl()` method to generate `PatternImpl`
instances
### Updated Classes
**`RewriteRule`**: Now inherits from `PatternImpl`
- Maintains all existing functionality
- Gains access to standalone pattern matching capabilities
- Uses inherited `match()` method in `try_rewrite()`
**`RewriteRuleClassBase`**: Now inherits from `PatternBase`
- Maintains all existing functionality
- Gains access to pattern-only capabilities
- Still provides `rule()` class method to create `RewriteRule` instances
## Usage Examples
### Standalone Pattern Matching
```python
from onnxscript.rewriter import pattern
# Define a pattern
def identity_pattern(op, x):
return op.Identity(x)
# Create a pattern matcher (no replacement needed)
pattern_matcher = pattern.PatternImpl(identity_pattern, name="IdentityMatcher")
# Use it to check if a node matches the pattern
match_result = pattern_matcher.match(model, graph, node)
if match_result:
print(f"Pattern matched! Found {len(match_result.nodes)} nodes")
```
### Class-Based Pattern Definition
```python
class MyPattern(pattern.PatternBase):
def pattern(self, op, x):
return op.Identity(x)
def check(self, context, x):
# Custom condition logic
return pattern.MatchResult()
# Create a pattern implementation
my_pattern = MyPattern()
pattern_impl = my_pattern.create_pattern_impl()
```
### Existing Functionality Preserved
```python
# RewriteRule still works exactly as before
rule = pattern.RewriteRule(target_pattern, replacement_pattern)
# But now it can also be used for just pattern matching
match_result = rule.match(model, graph, node) # New capability
count = rule.apply_to_model(model) # Existing functionality
```
## Backward Compatibility
All existing functionality is preserved. The changes are purely additive
- existing code using `RewriteRule` and `RewriteRuleClassBase` will
continue to work without modification.
## Testing
- All existing tests pass (34/34 tests successful)
- Added comprehensive test suite for new base classes
- Created example demonstrating standalone pattern matching usage
- Verified inheritance relationships work correctly
Fixes #2446.
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 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](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: gramalingam <10075881+gramalingam@users.noreply.github.com>
Co-authored-by: G. Ramalingam <grama@microsoft.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Ti-Tai Wang <titaiwang@microsoft.com>
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>1 parent 7517f2e commit 727210b
File tree
4 files changed
+627
-84
lines changed- examples
- onnxscript/rewriter
4 files changed
+627
-84
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
0 commit comments