Add boundary checks and add tests for SparseTensorProtoToDenseTensorProto by yuslepukhin · Pull Request #27323 · microsoft/onnxruntime

added 2 commits

February 11, 2026 19:30

skottmckay

skottmckay

@yuslepukhin yuslepukhin deleted the yuslepukhin/sparse_proto_conversion branch

February 13, 2026 19:00

tianleiwu pushed a commit that referenced this pull request

Feb 13, 2026
…roto (#27323)

### Motivation and Context
### Description
This pull request improves the robustness and correctness of sparse
tensor conversion to dense tensors in the ONNX Runtime codebase. The
main changes include enhanced validation of tensor shapes and indices,
improved support for different index data types, and better error
handling for out-of-bounds indices and invalid shapes.

**Validation and Error Handling Improvements:**
- Added explicit checks to ensure that sparse tensor indices are either
rank 1 or 2, and that values are 1D, returning clear errors for
unsupported shapes. Also, added checks to ensure all dimension sizes are
positive for both values and dense tensors.
- Improved error messages and validation for out-of-bounds indices
during the sparse-to-dense copy process, including both flattened and
multi-dimensional COO indices.
[[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1827-R1850)
[[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406R1861-R1863)

**Index Data Type Handling:**
- Enhanced support for INT8 and INT16 index types by allowing them to be
provided in the `int32_data` field (not just as raw data), and improved
handling of index data extraction for all supported types.

**API and Internal Consistency:**
- Refactored the `CopySparseData` function to take both `dense_dims` and
`dense_elements` as arguments for clearer logic and consistency.
[[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1747-R1781)
[[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1904-R1947)
[[3]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1928-R1957)
[[4]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1938-R1967)
- Updated function documentation to clarify supported formats and
expected tensor shapes.

**Minor Corrections:**
- Improved error messages for invalid index shapes and ensured correct
usage of ONNX enum values and utility functions.
[[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1863-R1872)
[[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1747-R1781)

These changes collectively make sparse tensor handling more robust,
user-friendly, and standards-compliant.

### Motivation and Context
Make the code more robust

tianleiwu added a commit that referenced this pull request

Feb 14, 2026
This cherry-picks the following commits for the release:
- #27323
- #27339

---------

Co-authored-by: Dmitri Smirnov <yuslepukhin@users.noreply.github.com>
Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>

apwojcik pushed a commit to ROCm/onnxruntime that referenced this pull request

Feb 19, 2026
…roto (microsoft#27323)

### Motivation and Context
### Description
This pull request improves the robustness and correctness of sparse
tensor conversion to dense tensors in the ONNX Runtime codebase. The
main changes include enhanced validation of tensor shapes and indices,
improved support for different index data types, and better error
handling for out-of-bounds indices and invalid shapes.

**Validation and Error Handling Improvements:**
- Added explicit checks to ensure that sparse tensor indices are either
rank 1 or 2, and that values are 1D, returning clear errors for
unsupported shapes. Also, added checks to ensure all dimension sizes are
positive for both values and dense tensors.
- Improved error messages and validation for out-of-bounds indices
during the sparse-to-dense copy process, including both flattened and
multi-dimensional COO indices.
[[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1827-R1850)
[[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406R1861-R1863)

**Index Data Type Handling:**
- Enhanced support for INT8 and INT16 index types by allowing them to be
provided in the `int32_data` field (not just as raw data), and improved
handling of index data extraction for all supported types.

**API and Internal Consistency:**
- Refactored the `CopySparseData` function to take both `dense_dims` and
`dense_elements` as arguments for clearer logic and consistency.
[[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1747-R1781)
[[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1904-R1947)
[[3]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1928-R1957)
[[4]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1938-R1967)
- Updated function documentation to clarify supported formats and
expected tensor shapes.

**Minor Corrections:**
- Improved error messages for invalid index shapes and ensured correct
usage of ONNX enum values and utility functions.
[[1]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1863-R1872)
[[2]](diffhunk://#diff-d31e9fbe0f5334fcd949833e035f2b25d5ae810dcd505c545f6b372b546b1406L1747-R1781)

These changes collectively make sparse tensor handling more robust,
user-friendly, and standards-compliant.

### Motivation and Context
Make the code more robust

This was referenced

Feb 23, 2026