Skip to content

Use new GraphInitializers tensor APIs to simplify codebase - #423

Draft
justinchuby wants to merge 1 commit into
mainfrom
copilot/fix-issue-420
Draft

justinchuby wants to merge 1 commit into
mainfrom
copilot/fix-issue-420

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Accesses to initializer tensors across the test suite still used the old three-layer pattern (initializers["name"].const_value, for value in .values(): value.const_value) introduced before the get_tensor() / tensors() / tensor_items() APIs existed.

Changes

  • initializers["name"].const_valueinitializers.get_tensor("name") — applied across external_data_test.py, serde_test.py, _safetensors_test.py, _io_test.py, shape_inference_test.py, initializer_deduplication_test.py, constant_manipulation_test.py
  • for value in .values(): value.const_valuefor tensor in .tensors() — where only the tensor is needed
  • for name, value in .items(): value.const_valuefor name, tensor in .tensor_items() — where name + tensor are both needed
# Before
external_tensor = model.graph.initializers["tensor1"].const_value
for value in model.graph.initializers.values():
    self.assertIsInstance(value.const_value, ir.Tensor)

# After
external_tensor = model.graph.initializers.get_tensor("tensor1")
for tensor in model.graph.initializers.tensors():
    self.assertIsInstance(tensor, ir.Tensor)

Production code is left unchanged: those loops either require the Value object for operations unrelated to the tensor (naming, replace_all_uses_with, etc.) or deliberately skip None const_values — behaviour that differs from tensors()'s strict-raise semantics.

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
@justinchuby
justinchuby requested review from a team and titaiwangms as code owners May 22, 2026 17:05
@justinchuby
justinchuby requested a review from Copilot May 22, 2026 17:05
@justinchuby
justinchuby marked this pull request as draft May 22, 2026 17:06
@codecov

codecov Bot commented May 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.57%. Comparing base (4ee65a2) to head (a14f6b7).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #423   +/-   ##
=======================================
  Coverage   84.57%   84.57%           
=======================================
  Files          52       52           
  Lines        6501     6501           
  Branches     1329     1329           
=======================================
  Hits         5498     5498           
  Misses        643      643           
  Partials      360      360           

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates the test suite to use the newer GraphInitializers tensor-centric APIs (get_tensor(), tensors(), tensor_items()) instead of repeatedly dereferencing initializer Value.const_value, improving readability and aligning tests with the current public API surface.

Changes:

  • Replaced initializers["name"].const_value with initializers.get_tensor("name") in multiple tests.
  • Replaced loops over .values()/.items() + .const_value with .tensors() / .tensor_items() where appropriate.
  • Kept behavior-sensitive cases using get_tensor() (which returns default when const_value is None) to preserve existing test intent.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/onnx_ir/serde_test.py Uses initializers.get_tensor() for initializer tensor assertions in serde tests.
src/onnx_ir/passes/common/shape_inference_test.py Updates initializer access patterns and preserves None-const-value coverage via get_tensor().
src/onnx_ir/passes/common/initializer_deduplication_test.py Switches initializer tensor reads to get_tensor() in deduplication tests.
src/onnx_ir/passes/common/constant_manipulation_test.py Updates lifted-initializer verification to compare directly against get_tensor() results.
src/onnx_ir/external_data_test.py Converts external-data tests to use get_tensor() for initializer tensor access.
src/onnx_ir/_safetensors/_safetensors_test.py Replaces initializer iteration/access with tensors() and tensor_items() for safetensors tests.
src/onnx_ir/_io_test.py Uses get_tensor() for initializer tensor assertions in IO tests.

@justinchuby

Copy link
Copy Markdown
Member Author

These are all tests. So I don't know how useful the API is, especially when the setter is not mirroring it. @gramalingam do you have thoughts?

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.

3 participants