Skip to content

Add eliminate_initializer_from_input pass - #325

Open
take-cheeze wants to merge 1 commit into
onnx:mainfrom
onnxsim:claude/onnxopt-initializer-from-input
Open

Add eliminate_initializer_from_input pass#325
take-cheeze wants to merge 1 commit into
onnx:mainfrom
onnxsim:claude/onnxopt-initializer-from-input

Conversation

@take-cheeze

Copy link
Copy Markdown
Member

IR<4 exports (e.g. the onnx-caffe2 models in the ONNX Model Zoo) list every initializer in graph.input as well. ONNX treats an initializer that is also a graph input as an overridable default rather than a constant, so Graph::is_constant_initializer -- and hence IsConstantTensor -- reports the weights as non-constant. That silently disables every constant-dependent pass (fuse_bn_into_conv, fuse_add_bias_into_conv,
fuse_matmul_add_bias_into_gemm, constant folding, ...), so these models leave the optimizer completely unchanged.

Add a pass that removes an initializer's redundant graph.input entry (keeping the initializer), i.e. normalises the legacy graph to the IR>=4 form, and register it so it runs before the constant-dependent fusions.

Verified on onnxmodelzoo/resnet101-v1-7 (IR version 3, 588 initializers all listed as inputs): with the inputs stripped, fuse_bn_into_conv folds all 104 BatchNormalization nodes it previously left untouched, matching onnxslim's output.

Claude-Session: https://claude.ai/code/session_01CzPBPhoPYRKPVcjZcVFy5h

IR<4 exports (e.g. the onnx-caffe2 models in the ONNX Model Zoo) list
every initializer in graph.input as well. ONNX treats an initializer that
is also a graph input as an overridable default rather than a constant, so
Graph::is_constant_initializer -- and hence IsConstantTensor -- reports the
weights as non-constant. That silently disables every constant-dependent
pass (fuse_bn_into_conv, fuse_add_bias_into_conv,
fuse_matmul_add_bias_into_gemm, constant folding, ...), so these models
leave the optimizer completely unchanged.

Add a pass that removes an initializer's redundant graph.input entry
(keeping the initializer), i.e. normalises the legacy graph to the IR>=4
form, and register it so it runs before the constant-dependent fusions.

Verified on onnxmodelzoo/resnet101-v1-7 (IR version 3, 588 initializers all
listed as inputs): with the inputs stripped, fuse_bn_into_conv folds all
104 BatchNormalization nodes it previously left untouched, matching
onnxslim's output.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CzPBPhoPYRKPVcjZcVFy5h
Signed-off-by: take-cheeze <takechi101010@gmail.com>
@take-cheeze
take-cheeze requested review from a team as code owners July 29, 2026 15:57
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