Skip to content

Validate in-memory initializer references - #32042

Merged
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/in-memory-external-data-validation
Aug 14, 2026
Merged

Validate in-memory initializer references#32042
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/in-memory-external-data-validation

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request introduces stricter validation and error handling for initializers with in-memory external data references in ONNX Runtime's graph handling. The main goal is to ensure that all such references are properly registered and that their data matches expectations, preventing invalid model states and improving robustness. Additionally, new tests are added to verify these behaviors.

Validation and Error Handling Improvements:

  • Added a new ValidateInMemoryInitializers method to the Graph class, which checks that all in-memory external data initializers have corresponding OrtValue objects with matching data, and integrated this validation into the graph transformation process. [1] [2] [3]
  • Introduced a helper function GetValidatedInMemoryInitializer in graph_utils.cc to enforce that in-memory external data initializers are registered and their data matches, replacing ad-hoc checks in various code paths.
  • Updated MakeInitializerCopyIfNotExist and ConvertInMemoryDataToInline to use the new validation helper, ensuring consistent and early detection of invalid initializer states. [1] [2]

Testing Enhancements:

  • Added the RejectsUnregisteredInMemoryInitializerCopy test to verify that the system correctly rejects initializers with arbitrary or unregistered in-memory references, both during validation and when attempting to copy such initializers.

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

This PR strengthens ONNX Runtime graph robustness by adding centralized validation for initializers that use in-memory external data references, ensuring those references are properly backed by cached OrtValues (and match the expected in-memory pointer) before graph transformations that may copy initializer data.

Changes:

  • Adds Graph::ValidateInMemoryInitializers() and calls it early in InferenceSession::TransformGraph() to reject invalid/malicious in-memory initializer references before AOT inlining/partitioning.
  • Introduces a local GetValidatedInMemoryInitializer helper in graph_utils.cc and routes relevant initializer-copy/inline-conversion paths through it.
  • Adds a regression test to ensure unregistered/arbitrary in-memory initializer references are rejected.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
onnxruntime/test/ir/graph_test.cc Adds coverage to reject copying/validating unregistered in-memory initializer references.
onnxruntime/core/session/inference_session.cc Invokes the new in-memory initializer validation early in graph transformation.
onnxruntime/core/graph/graph.cc Implements Graph::ValidateInMemoryInitializers() over graph + subgraphs.
onnxruntime/core/graph/graph_utils.cc Adds a shared validation helper and uses it in initializer copy/inline-conversion paths.
include/onnxruntime/core/graph/graph.h Declares Graph::ValidateInMemoryInitializers() in the public Graph interface (non-minimal builds).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/core/session/inference_session.cc
Comment thread onnxruntime/core/graph/graph.cc
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