Fix ShapeInferencePass crash on initializers without const_value#380
Open
Fix ShapeInferencePass crash on initializers without const_value#380
Conversation
Replace the assertion `assert initializer.const_value is not None` in `_c_api_utils.call_onnx_api` with a conditional that handles initializers without tensor data. When `const_value` is `None`, the initializer is removed from the initializers dict (to avoid serialization issues) but kept as an input so shape inference can use its declared type/shape. The restore step also handles the `const_value=None` case by using `model.graph.initializers.add()` instead of `register_initializer()` which requires `const_value` to be set. Fixes #233 Agent-Logs-Url: https://github.com/onnx/ir-py/sessions/d2e003ba-091e-434b-a510-bbf5c12dfec4 Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix AssertionError on unloaded initializers in ShapeInferencePass
Fix ShapeInferencePass crash on initializers without const_value
Mar 31, 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 #380 +/- ##
==========================================
+ Coverage 80.02% 80.05% +0.03%
==========================================
Files 52 52
Lines 6394 6399 +5
Branches 1294 1297 +3
==========================================
+ Hits 5117 5123 +6
Misses 912 912
+ Partials 365 364 -1 ☔ View full report in Codecov by Sentry. |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a crash in ShapeInferencePass when a graph contains “unloaded” initializers (const_value=None), which is common for models constructed before weights are loaded. The update ensures ONNX shape inference can still run using the initializer’s declared type/shape.
Changes:
- Update
_c_api_utils.call_onnx_api()to tolerate initializers withconst_value=Noneby skipping const-derived shape/dtype inference and temporarily treating them as graph inputs for the ONNX C API call. - Adjust restoration logic to re-add unloaded initializers without using
register_initializer()(which requiresconst_value). - Add a regression test covering MatMul shape inference with an unloaded initializer.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/onnx_ir/passes/common/_c_api_utils.py |
Avoids asserting on const_value and restores unloaded initializers safely after ONNX API calls. |
src/onnx_ir/passes/common/shape_inference_test.py |
Adds a test case for shape inference with an initializer that has no tensor data loaded. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Justin Chu <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.
ShapeInferencePassfails withAssertionErrorwhen any graph initializer hasconst_value=None— the standard state for models built without loaded weights. This blocks shape inference on any pre-weight-loading model.Changes
_c_api_utils.py: Replaceassert initializer.const_value is not Nonewith a conditional — skip shape/dtype inference fromconst_valuewhen absent, remove the initializer from the dict (avoiding serialization failure), but keep it as a graph input so ONNX shape inference can still use its declared type/shape._c_api_utils.py(restore path): Useinitializers.add()instead ofregister_initializer()for unloaded initializers, sinceregister_initializer()enforcesconst_value is not None.shape_inference_test.py: Add test with aMatMulnode where one input is an initializer with noconst_value, verifying inference succeeds and model state is properly restored.