Fix non-deterministic rewriter behavior in multi-output pattern matching#2880
Merged
justinchuby merged 2 commits intomainfrom Apr 2, 2026
Merged
Conversation
… of set for output_nodes Replace set[NodePattern] with dict[NodePattern, None] to preserve deterministic insertion order from the outputs sequence. Python sets have non-deterministic iteration order due to hash randomization, which caused the multi-output pattern matching to behave differently across runs. Fixes #2234 Agent-Logs-Url: https://github.com/microsoft/onnxscript/sessions/50f46d7a-beee-47c4-9369-3c28417380d1 Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix non-deterministic rewriter behavior in onnxscript
Fix non-deterministic rewriter behavior in multi-output pattern matching
Apr 2, 2026
justinchuby
approved these changes
Apr 2, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2880 +/- ##
==========================================
+ Coverage 71.96% 72.04% +0.07%
==========================================
Files 239 239
Lines 29224 29305 +81
Branches 2878 2880 +2
==========================================
+ Hits 21031 21112 +81
Misses 7216 7216
Partials 977 977 ☔ View full report in Codecov by Sentry. |
xadupre
approved these changes
Apr 2, 2026
justinchuby
added a commit
that referenced
this pull request
Apr 17, 2026
…ing (#2880) `GraphPattern.__init__` collects output nodes into a `set[NodePattern]`, then converts to a list via `list(output_nodes)`. Python's hash randomization means this list has non-deterministic order across process invocations. For multi-output patterns (e.g. `SlicesSplit`), the matcher fixes `output_nodes[0]` to the current node and searches for `output_nodes[1:]` — so different orderings cause the same rule to match or not match non-deterministically. - Replace `set[NodePattern]` with `dict[NodePattern, None]` (ordered set idiom) to preserve deterministic insertion order from the `outputs` sequence - Add regression test verifying `output_nodes` ordering is stable across repeated `GraphPattern` constructions ```python # Before: non-deterministic iteration order output_nodes: set[NodePattern] = set() ... output_nodes.add(candidate) ... self.output_nodes: list[NodePattern] = list(output_nodes) # After: preserves insertion order from outputs sequence output_nodes: dict[NodePattern, None] = {} ... output_nodes[candidate] = None ... self.output_nodes: list[NodePattern] = list(output_nodes) ``` Fix #2878 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GraphPattern.__init__collects output nodes into aset[NodePattern], then converts to a list vialist(output_nodes). Python's hash randomization means this list has non-deterministic order across process invocations. For multi-output patterns (e.g.SlicesSplit), the matcher fixesoutput_nodes[0]to the current node and searches foroutput_nodes[1:]— so different orderings cause the same rule to match or not match non-deterministically.set[NodePattern]withdict[NodePattern, None](ordered set idiom) to preserve deterministic insertion order from theoutputssequenceoutput_nodesordering is stable across repeatedGraphPatternconstructionsFix #2878