fix: OrtValue.from_dlpack rejects zero-size tensors as non-contiguous#27451
Merged
justinchuby merged 2 commits intomainfrom Feb 25, 2026
Merged
fix: OrtValue.from_dlpack rejects zero-size tensors as non-contiguous#27451justinchuby merged 2 commits intomainfrom
justinchuby merged 2 commits intomainfrom
Conversation
…sion NumPy 2.x sets all strides to 0 for zero-size tensors, which caused the right-to-left stride check to fail on the first non-zero dimension (e.g., dimension 128 with stride 0 != running_size 1) before even reaching the zero dimension. Fix by pre-scanning all dimensions for any zero size before performing per-dimension stride validation. Add regression tests in test_ort_value_dlpack_zero_size covering the KV-cache use case shape (1, 8, 0, 128) and other zero-size shapes. Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix OrtValue.from_dlpack to support zero-size tensors
fix: OrtValue.from_dlpack rejects zero-size tensors as non-contiguous
Feb 25, 2026
justinchuby
approved these changes
Feb 25, 2026
tianleiwu
approved these changes
Feb 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes
IsContiguousTensorindlpack_converter.ccto correctly accept zero-size tensors, which are vacuously contiguous regardless of their strides.Root cause:
IsContiguousTensoriterates dimensions right-to-left. NumPy 2.x sets all strides to 0 for zero-size tensors. For shape(1, 8, 0, 128), the check hits dim 3 (size=128, stride=0) and fails (128 != 1 && stride[3]=0 != running_size=1) before ever reaching the zero dimension — so the existing in-loop early-return never fires.Fix: Replace the in-loop zero-dimension check with a pre-scan that short-circuits before any stride validation:
Tests: Adds
test_ort_value_dlpack_zero_sizecovering:(1, 8, 0, 128)— the KV-cache use case from the issue(0,),(0, 4),(4, 0)— other zero-size shapesBoth the
arr.__dlpack__()path and the OrtValue round-trip (to_dlpack→from_dlpack) are exercised.Motivation and Context
OrtValue.from_dlpackraisedRuntimeError: ORT only supports contiguous tensor for nowfor any zero-size NumPy array (e.g. shape(1, 8, 0, 128)), making it unusable for KV-cache models whose initial cache has a zero-length sequence dimension.OrtValue.ortvalue_from_numpyhandled the same arrays correctly — only the DLPack path was broken.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.