Skip to content

Commit db355eb

Browse files
committed
Cleanup
Signed-off-by: G Ramalingam <grama@microsoft.com>
1 parent 678349d commit db355eb

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

docs/tutorial/rewriter/node_value_checkers.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,16 @@ This means you should be careful when designing patterns with multiple alternati
179179

180180
## Error Handling
181181

182-
Checkers can return either:
183-
- `True`: Check passed, continue matching
184-
- `False`: Check failed, pattern does not match
185-
- `MatchResult`: More detailed result with potential failure reasons
182+
Both check functions (including condition functions and node/value-level checkers) and
183+
rewrite functions support the same conventions for indicating failure:
186184

187-
If a checker raises an exception, it will be caught and treated as a match failure, allowing patterns to fail gracefully when encountering unexpected conditions.
185+
- **`MatchResult` with `.fail()`** *(recommended)*: Return `MatchResult().fail("reason", source)` to indicate failure with a descriptive reason and optional source node/value. This provides the most useful diagnostic information for debugging.
186+
- **Raise `MatchFailureError`** *(recommended)*: Raise `MatchFailureError("reason", source_node_or_value)` to indicate failure. This is especially convenient in utility functions called from a check or rewrite, since it avoids having to explicitly propagate failure status through the call chain.
187+
- **Return `None` or `False`**: These indicate failure without providing a reason. They are supported but not recommended, since a failure reason is valuable for debugging why a rule did not apply.
188+
189+
Including a descriptive failure reason is strongly encouraged. The rewriter's tracing infrastructure
190+
uses these reasons to report why rules failed to match, which is essential for diagnosing
191+
issues when developing or debugging rewrite rules.
192+
193+
For **check functions**, success is indicated by returning `True` or a truthy `MatchResult`.
194+
For **rewrite functions**, success is indicated by returning one or more `ir.Value` results.

onnxscript/rewriter/_rewrite_rule.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ def get_replacement(self, match: _basics.MatchResult) -> ReplacementSubgraph | N
223223
match.fail(e.reason, list(e.failure_sources))
224224
return None
225225
# Support the same failure conventions as check functions for uniformity:
226-
# - None or False: simple failure indicator (deprecated for False, but supported)
227-
# - Falsy MatchResult: failure with reason/source info
226+
# - None or False: indicates failure without a reason (not recommended)
227+
# - Falsy MatchResult: failure with reason/source info (recommended)
228+
# - MatchFailureError exception: failure with reason/source info (recommended)
228229
if new_outputs is None or new_outputs is False:
229230
return None
230231
if isinstance(new_outputs, _basics.MatchResult):

onnxscript/rewriter/pattern_base_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ def test_rewrite_returning_ir_value_succeeds(self):
322322

323323
# Use a non-self-referential pattern to avoid infinite rewrite loops
324324
def add_zero_pattern(op, x):
325-
zero = pattern.Constant(0.0)
326-
return op.Add(x, zero)
325+
return op.Add(x, 0.0)
327326

328327
def identity_replacement(op, x):
329328
return op.Identity(x)

0 commit comments

Comments
 (0)