Skip to content

fix: normalize cache key dtype to prevent initializer name collisions#2888

Merged
gramalingam merged 2 commits intomainfrom
rama/init-bug
Apr 14, 2026
Merged

fix: normalize cache key dtype to prevent initializer name collisions#2888
gramalingam merged 2 commits intomainfrom
rama/init-bug

Conversation

@gramalingam
Copy link
Copy Markdown
Collaborator

When a Python int literal (e.g. 1) is used in both untyped positions (like a Gather index, where the ONNX schema type variable is unbound) and typed positions (like Add with an INT64 tensor), the constant cache created two entries: (1, None) and (1, INT64). Both generated the same initializer name 'const_1_i64' but as different ir.Value objects, causing register_initializer to raise ValueError.

Fix: before cache lookup, normalize dtype=None to the default ONNX dtype for the Python type (_PYTHON_TYPE_TO_DTYPE: int->INT64, float->FLOAT). This merges both entries into a single cache key and reuses the same ir.Value. Applied to both scalar and sequence (list/tuple) branches.

When a Python int literal (e.g. `1`) is used in both untyped positions
(like a Gather index, where the ONNX schema type variable is unbound)
and typed positions (like Add with an INT64 tensor), the constant cache
created two entries: (1, None) and (1, INT64). Both generated the same
initializer name 'const_1_i64' but as different ir.Value objects,
causing register_initializer to raise ValueError.

Fix: before cache lookup, normalize dtype=None to the default ONNX dtype
for the Python type (_PYTHON_TYPE_TO_DTYPE: int->INT64, float->FLOAT).
This merges both entries into a single cache key and reuses the same
ir.Value. Applied to both scalar and sequence (list/tuple) branches.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: G Ramalingam <grama@microsoft.com>
Comment thread onnxscript/_internal/builder_test.py Fixed
Comment thread onnxscript/_internal/builder_test.py Fixed
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.30%. Comparing base (13f265c) to head (e957e18).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2888      +/-   ##
==========================================
+ Coverage   72.24%   72.30%   +0.05%     
==========================================
  Files         241      241              
  Lines       29650    29678      +28     
  Branches     2914     2916       +2     
==========================================
+ Hits        21421    21459      +38     
+ Misses       7235     7227       -8     
+ Partials      994      992       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: G Ramalingam <grama@microsoft.com>
@gramalingam gramalingam enabled auto-merge (squash) April 14, 2026 20:42
@gramalingam gramalingam merged commit 63ffecf into main Apr 14, 2026
29 of 33 checks passed
@gramalingam gramalingam deleted the rama/init-bug branch April 14, 2026 21:21
justinchuby pushed a commit that referenced this pull request Apr 17, 2026
…#2888)

When a Python int literal (e.g. `1`) is used in both untyped positions
(like a Gather index, where the ONNX schema type variable is unbound)
and typed positions (like Add with an INT64 tensor), the constant cache
created two entries: (1, None) and (1, INT64). Both generated the same
initializer name 'const_1_i64' but as different ir.Value objects,
causing register_initializer to raise ValueError.

Fix: before cache lookup, normalize dtype=None to the default ONNX dtype
for the Python type (_PYTHON_TYPE_TO_DTYPE: int->INT64, float->FLOAT).
This merges both entries into a single cache key and reuses the same
ir.Value. Applied to both scalar and sequence (list/tuple) branches.

---------

Signed-off-by: G Ramalingam <grama@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants