Skip to content

Validate Rust tensor element types - #32035

Merged
Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/rust-tensor-element-type-conversion
Aug 13, 2026
Merged

Akshay Sonawane (apsonawane) merged 2 commits into
mainfrom
fix/rust-tensor-element-type-conversion

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request improves how tensor element types are handled in the ONNX Runtime Rust bindings. It introduces more robust error handling for unsupported and undefined tensor element types, replacing previous unchecked conversions with a safe and explicit conversion mechanism. The most important changes are:

Error Handling Improvements:

  • Added a new OrtError::UnsupportedTensorElementType(i64) variant to represent unsupported tensor types, and updated the UndefinedTensorElementType error message for clarity.

Type Conversion and Safety:

  • Implemented TryFrom<sys::ONNXTensorElementDataType> for TensorElementDataType, returning descriptive errors for undefined or unsupported types instead of using unsafe transmute.
  • Updated code in dangerous module to use the new try_from conversion for tensor element types, eliminating unsafe casting and ensuring errors are handled explicitly.

Testing:

  • Added unit tests to verify that supported types convert successfully, undefined types return the correct error, and unsupported types are properly rejected.

Code Cleanliness:

  • Added necessary imports for TryFrom in session.rs to support the new conversion logic.

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 hardens tensor element type handling in the ONNX Runtime Rust bindings by replacing an unchecked transmute with an explicit fallible conversion that reports undefined/unsupported types via OrtError.

Changes:

  • Added OrtError::UnsupportedTensorElementType(i64) to represent tensor element types not supported by the Rust binding.
  • Implemented TryFrom<sys::ONNXTensorElementDataType> for TensorElementDataType and updated session IO inspection to use it instead of transmute.
  • Added unit tests for supported, undefined, and unsupported tensor element type conversions.

Reviewed changes

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

File Description
rust/onnxruntime/src/session.rs Switches session IO type extraction to use TensorElementDataType::try_from(...) instead of transmute.
rust/onnxruntime/src/lib.rs Adds TryFrom conversion logic for TensorElementDataType plus unit tests.
rust/onnxruntime/src/error.rs Introduces a new OrtError variant for unsupported tensor element types.

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

Comment thread rust/onnxruntime/src/lib.rs Resolved
@apsonawane
Akshay Sonawane (apsonawane) merged commit 2b801da into main Aug 13, 2026
97 of 109 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the fix/rust-tensor-element-type-conversion branch August 13, 2026 23:49
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