Add option to treat initializers as non-constants - #2
Open
take-cheeze wants to merge 1 commit into
Open
Conversation
Introduce a thread-local switch, SetInitializersAsConstants / InitializersAsConstants, that controls whether the fusion/elimination passes treat graph initializers as constant tensors. The single choke points IsConstantTensor and FetchConstantTensor consult it, so when the switch is off every value-baking pass (fuse_bn_into_conv, nop-reshape on a constant shape, ...) leaves initializer-backed weights untouched while Constant nodes stay constant. Expose it through the Python binding and as an initializers_as_constants keyword on onnxoptimizer.optimize(), which sets the switch around the run and restores it afterwards. Add tests covering the fuse-bn case and that the switch is restored. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MyzBiNk5LvTaVPgURqD5UH
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
Adds a switch that controls whether the fusion/elimination passes treat graph initializers as constant tensors. The default (
true) preserves onnxoptimizer's historical behaviour; when set tofalse, initializers are treated as non-constant so value-baking passes leave initializer-backed weights untouched, whileConstantnodes are still treated as constants.This is the onnxoptimizer-side change for the onnxsim
initializers_as_constants/--initializers-as-non-constantsfeature (onnxsim/onnxsim#517), split out here so its diff is self-contained.Changes
passes/pass_util.cc/pass_util.h: introduce a thread-local switch and gate the two constant choke points —IsConstantTensorandFetchConstantTensor— on it. Because these are the single points every value-baking pass (e.g.fuse_bn_into_conv, nop-reshape/expand on a constant shape) goes through to decide "is this value a known constant", one change makes them all respect the switch. Structural initializer passes (unused/duplicate-initializer elimination, renaming) access initializers directly and are intentionally unaffected.optimize.h: declare the publicSetInitializersAsConstants(bool)/InitializersAsConstants()API.cpp2py_export.cc: exposeset_initializers_as_constants/initializers_as_constantsto Python.__init__.py: add aninitializers_as_constants: bool = Truekeyword tooptimize(); it sets the switch around the run and restores it afterwards so the thread-local state never leaks to other callers.optimize().Notes
Constantnodes are always treated as constants regardless of the setting.🤖 Generated with Claude Code
https://claude.ai/code/session_01MyzBiNk5LvTaVPgURqD5UH
Generated by Claude Code