fix: prevent initializer name collision in rewrite rule registration#2946
Open
OYCN wants to merge 1 commit into
Open
fix: prevent initializer name collision in rewrite rule registration#2946OYCN wants to merge 1 commit into
OYCN wants to merge 1 commit into
Conversation
When multiple rewrite matches produce initializers with the same name, the second registration would silently overwrite the first in the graph.initializers dict. The original Value became an unregistered dangling reference, causing toposort validation failure on serialization. The root cause was two independent for-loops: the first checked for duplicates but its `continue` only affected itself (dead code), while the second unconditionally overwrote all entries. Fix: merge into a single loop that detects name conflicts and auto-generates a unique suffix (_1, _2, ...) before registering. Signed-off-by: opluss <opluss@qq.com>
Author
|
@microsoft-github-policy-service agree |
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.
Summary
Fix a bug where multiple rewrite rule matches producing initializers with the same name would silently clobber each other, resulting in a dangling
ir.Valuereference and a toposort validation failure on serialization.Problem
When two
Reshape→Reshapechains share the same shape initializer (e.g. containing-1), theReshapeReshaperule fires twice, each time callingop.initializer(ir.Tensor(..., name=shape.name)). Both produce an initializer with the same name but different resolved values.The registration logic in
_rewrite_rule.pyhad two independent for-loops:The second loop always overwrote the dict entry, so the first registered Value lost its dict reference.
NameFixPasswould then rename it to ensure uniqueness, but never re-registered it as an initializer — leaving it as a dangling reference that failsonnx.checker.Fix
Merge into a single loop. On name conflict with a different Value object, auto-generate a unique suffix (
_1,_2, ...) and register under the new name. Since node inputs hold object references (not name strings), the rename propagates correctly on serialization.Test
Added
test_reshape_reshape_shared_shape_no_clobberingcovering the shared-shape scenario. Asserts that all shape Values are registered initializers and the serialized model passesonnx.checker.check_model(full_check=True).Related
Also filed a separate issue/PR in onnx-ir for
NameFixPassnot registering renamed Values as initializers — that's an independent bug exposed by the same scenario.