Skip to content

Fix ShapeInferencePass crash on initializers without const_value#380

Open
Copilot wants to merge 5 commits intomainfrom
copilot/fix-assertion-error-initializers
Open

Fix ShapeInferencePass crash on initializers without const_value#380
Copilot wants to merge 5 commits intomainfrom
copilot/fix-assertion-error-initializers

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

ShapeInferencePass fails with AssertionError when any graph initializer has const_value=None — the standard state for models built without loaded weights. This blocks shape inference on any pre-weight-loading model.

W = ir.Value(name="W", shape=ir.Shape([3, 4]), type=float_type)  # no const_value
graph = ir.Graph(..., initializers=[W], ...)
common_passes.ShapeInferencePass()(model)  # AssertionError in _c_api_utils.py:46

Changes

  • _c_api_utils.py: Replace assert initializer.const_value is not None with a conditional — skip shape/dtype inference from const_value when 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): Use initializers.add() instead of register_initializer() for unloaded initializers, since register_initializer() enforces const_value is not None.
  • shape_inference_test.py: Add test with a MatMul node where one input is an initializer with no const_value, verifying inference succeeds and model state is properly restored.

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
Copilot AI requested a review from justinchuby March 31, 2026 23:42
@justinchuby justinchuby marked this pull request as ready for review April 2, 2026 14:44
@justinchuby justinchuby requested review from a team and titaiwangms as code owners April 2, 2026 14:44
@justinchuby justinchuby added this to the 0.2.1 milestone Apr 2, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.05%. Comparing base (5fd5e94) to head (3de5306).

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.
📢 Have feedback on the report? Share it here.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with const_value=None by 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 requires const_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.

@justinchuby justinchuby disabled auto-merge April 3, 2026 14:06
justinchuby and others added 2 commits April 3, 2026 07:07
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
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.

ShapeInferencePass: AssertionError on unloaded initializers + function body shapes not updated

3 participants