Skip to content

Validate ORT format edge slots - #32200

Open
Akshay Sonawane (apsonawane) wants to merge 1 commit into
mainfrom
msrc/ort-edge-slot-bounds-fix
Open

Validate ORT format edge slots#32200
Akshay Sonawane (apsonawane) wants to merge 1 commit into
mainfrom
msrc/ort-edge-slot-bounds-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request enhances the robustness and correctness of ONNX Runtime's ORT format model loading, particularly around edge validation in computational graphs. It introduces stricter checks for edge consistency, improves error handling, and significantly expands test coverage to prevent invalid graph structures from being loaded.

Edge validation and canonicalization improvements:

  • The Node::LoadEdgesFromOrtFormat method was refactored to enforce stricter validation of edge indices, ensure control edges are handled correctly, and guarantee that edges only connect matching NodeArgs. This includes checks for out-of-range indices, mismatched arguments, and correct handling of control edges. (onnxruntime/core/graph/graph.cc, include/onnxruntime/core/graph/graph.h) [1] [2]
  • After edge loading, the graph now checks that each destination argument slot has at most one producer, preventing invalid graphs with multiple producers for the same input. (onnxruntime/core/graph/graph.cc)

Test coverage expansion:

  • New helper functions and tests were added to rigorously verify edge validation logic. These tests cover scenarios such as out-of-range source/destination indices, mismatched node arguments, canonicalization of reciprocal edges, control edge handling, and detection of multiple producers for a single destination slot. (onnxruntime/test/framework/ort_model_only_test.cc) [1] [2]

These changes collectively ensure that only valid and well-formed graphs are accepted from ORT format models, reducing the risk of subtle bugs or crashes due to malformed edge definitions.

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

Strengthens ORT-format graph loading by validating and canonicalizing serialized edge relationships.

Changes:

  • Validates edge slots, NodeArgs, control edges, and producer conflicts.
  • Canonicalizes one-sided and reciprocal edge records.
  • Adds focused malformed-model tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/graph/graph.cc Implements edge validation and canonicalization.
include/onnxruntime/core/graph/graph.h Allows edge loading to update graph relationships.
onnxruntime/test/framework/ort_model_only_test.cc Adds edge-loading validation tests.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +915 to +918
const bool is_control_edge = src_arg_index == INT_MAX && dst_arg_index == INT_MAX;
ORT_RETURN_IF((src_arg_index == INT_MAX) != (dst_arg_index == INT_MAX),
"Node::LoadEdgesFromOrtFormat, ", edge_description,
" has an invalid control-edge slot pair. Invalid ORT format model.");
auto add_edges = [&graph](const flatbuffers::Vector<const onnxruntime::fbs::EdgeEnd*>* fbs_edges,
EdgeSet& edge_set, const std::string& dst_name) -> Status {
auto add_edges = [this, &graph](const flatbuffers::Vector<const onnxruntime::fbs::EdgeEnd*>* fbs_edges,
const std::string& edge_description, bool input_edges) -> Status {

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.

nit: the edge description can be inferred from the new bool parameter. also, the edge description value is plural (e.g., "input edges") which doesn't read well with many of the error messages.

Comment on lines +920 to +922
src_node.relationships_.output_edges.emplace(dst_node);
dst_node.relationships_.input_edges.emplace(src_node);
dst_node.relationships_.control_inputs.insert(src_node.Name());

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.

this is a copy of the logic in Graph::AddControlEdge(). maybe refactor into a shared helper function like void AddControlEdgeBetweenNodes(Node& src_node, Node& dst_node)?

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