cuda.core: account for DLTensor.byte_offset in from_dlpack - #2594
cuda.core: account for DLTensor.byte_offset in from_dlpack#2594fedonman wants to merge 1 commit into
Conversation
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>
|
@fedonman I was assigned #2592 today, but this PR already has the fix, so I would rather see it merged than open another one. I closed my own #2595 as a duplicate of this back on Aug 10. The only thing blocking it is that the branch is 30 commits behind Are you able to merge For what it is worth, I audited whether the one line is sufficient. |
|
I reviewed this properly rather than just eyeballing the diff. No blocking findings. Recording what I checked so it is not re-done. The fix line is the idiom already shipping in this file. After patching, the right-hand side of L1105 is byte-identical to L866 in So this is not a new expression, it makes the two import paths agree. One line is sufficient. Cython accepts it. I cannot build The spec claim holds against an independent implementation. I built the issue's producer and consumed it with numpy 2.3.5, which has its own DLPack reader: So The test drives the path it claims to. Three things you may or may not want to act on, none blocking:
The merge into |
Description
closes #2592
DLPack places a tensor's first element at
data + byte_offset.view_as_dlpack, which backsStridedMemoryView.from_dlpackandfrom_any_interface, setptrfromdataalone, so a producer that reports the allocation base indataand expresses a slice asbyte_offsetproduced a view pointingbyte_offsetbytes before the tensor, with nothing raised. The capsule-consuming importer in the same module,_smv_from_dlpack_capsule, already foldsbyte_offsetin, so the two import paths disagreed about the same capsule. This makesview_as_dlpackmatch it, and that one line is the whole production change.Correcting
ptralso fixes the round-trip. The__dlpack__re-export writesptrback out asdatawithbyte_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 writingbyte_offsetalone, so a regression fails an assertion rather than reading out of bounds.shapeandstridesshare a single producer-owned allocation, so the test does not touch them.A release note is added under
Fixes and enhancementsincuda_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:
The same test with the one-line change reverted:
The full
cuda_coresuite:Checklist