Skip to content

cuda.core: account for DLTensor.byte_offset in from_dlpack - #2594

Open
fedonman wants to merge 1 commit into
NVIDIA:mainfrom
fedonman:fix-smv-dlpack-byte-offset
Open

cuda.core: account for DLTensor.byte_offset in from_dlpack#2594
fedonman wants to merge 1 commit into
NVIDIA:mainfrom
fedonman:fix-smv-dlpack-byte-offset

Conversation

@fedonman

Copy link
Copy Markdown

Description

closes #2592

DLPack places a tensor's first element at data + byte_offset. view_as_dlpack, which backs StridedMemoryView.from_dlpack and from_any_interface, set ptr from data alone, so a producer that reports the allocation base in data and expresses a slice as byte_offset produced a view pointing byte_offset bytes before the tensor, with nothing raised. The capsule-consuming importer in the same module, _smv_from_dlpack_capsule, already folds byte_offset in, so the two import paths disagreed about the same capsule. This makes view_as_dlpack match it, and that one line is the whole production change.

Correcting ptr also fixes the round-trip. The __dlpack__ re-export writes ptr back out as data with byte_offset = 0, so before this change a non-zero offset was dropped permanently on the way back out.

The new test exercises both the versioned and the unversioned capsule branches. It views the first 8 elements of a 9-element array, then re-describes the same tensor as src[1:9] by writing byte_offset alone, so a regression fails an assertion rather than reading out of bounds. shape and strides share a single producer-owned allocation, so the test does not touch them.

A release note is added under Fixes and enhancements in cuda_core/docs/source/release/1.2.0-notes.rst; the affected behavior has been present since 0.1.0.

Verification

The new test, both capsule flavours:

tests/test_utils_dlpack.py::test_from_dlpack_honours_byte_offset[unversioned] PASSED [ 50%]
tests/test_utils_dlpack.py::test_from_dlpack_honours_byte_offset[versioned] PASSED [100%]
======================= 2 passed, 30 deselected in 0.11s =======================

The same test with the one-line change reverted:

=========================== short test summary info ============================
FAILED tests/test_utils_dlpack.py::test_from_dlpack_honours_byte_offset[unversioned]
FAILED tests/test_utils_dlpack.py::test_from_dlpack_honours_byte_offset[versioned]
======================= 2 failed, 30 deselected in 0.14s =======================

The full cuda_core suite:

3569 passed, 412 skipped, 1 xfailed in 159.90s (0:02:39)

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

DLPack places a tensor's first element at data + byte_offset, but
view_as_dlpack set StridedMemoryView.ptr from data alone. A producer
that reports the allocation base in data and expresses a slice as
byte_offset therefore produced a view pointing byte_offset bytes before
the tensor, with nothing raised. The offset was also lost permanently on
a round-trip, because the __dlpack__ re-export writes ptr back out as
data with byte_offset = 0.

The capsule-consuming importer in the same module already folds
byte_offset in, so the two import paths disagreed about the same
capsule. This makes view_as_dlpack match it.

Closes NVIDIA#2592

Signed-off-by: Vyron Vasileiadis <hi@fedonman.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: StridedMemoryView.from_dlpack ignores DLTensor.byte_offset

1 participant