Skip to content

Add option to treat initializers as non-constants - #2

Open
take-cheeze wants to merge 1 commit into
mainfrom
claude/initializers-as-constants-option
Open

Add option to treat initializers as non-constants#2
take-cheeze wants to merge 1 commit into
mainfrom
claude/initializers-as-constants-option

Conversation

@take-cheeze

Copy link
Copy Markdown
Member

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 to false, initializers are treated as non-constant so value-baking passes leave initializer-backed weights untouched, while Constant nodes are still treated as constants.

This is the onnxoptimizer-side change for the onnxsim initializers_as_constants / --initializers-as-non-constants feature (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 — IsConstantTensor and FetchConstantTensor — 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 public SetInitializersAsConstants(bool) / InitializersAsConstants() API.
  • cpp2py_export.cc: expose set_initializers_as_constants / initializers_as_constants to Python.
  • __init__.py: add an initializers_as_constants: bool = True keyword to optimize(); it sets the switch around the run and restores it afterwards so the thread-local state never leaks to other callers.
  • Tests: cover that fuse-bn folds by default, that it does not when initializers are treated as non-constant, and that the switch is restored after optimize().

Notes

  • The switch is thread-local and restored by the Python wrapper; C++ callers that flip it directly should restore it themselves.
  • Constant nodes are always treated as constants regardless of the setting.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MyzBiNk5LvTaVPgURqD5UH


Generated by Claude Code

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants