Repository navigation
Conversation
…ffers in tensor extensions Compose outer row slicing with the child values slice in whole-array fixed-shape tensor conversion so child offsets and exact byte lengths are respected with overflow checks, and return a static immutable zero-byte buffer when zero-element tensor arrays or scalars omit the values buffer. Signed-off-by: mora <m4ru1@qq.com>
…et in SliceTensorBuffer Return an immutable zero-byte buffer when a zero-element tensor slice starts past a non-null empty or shorter backing buffer, matching ValidateLayout's zero-length contract while keeping zero-copy view offsets for in-range slices. Signed-off-by: mora <m4ru1@qq.com>
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.
Rationale for this change
Calling
FixedShapeTensorArray::ToTensor()(or Pythonto_tensor(),to_numpy_ndarray(), and DLPack 1.0+ export) when the underlying childvaluesarray is sliced ignores the child offset and silently returns preceding elements from the backing buffer, and retains trailing buffer bytes that cause valid zero-sized tensor shapes (such as(20, 0, 2)over a shorter sliced buffer) to fail validation with"strides must not involve buffer over run". In addition, converting valid zero-element tensor arrays or scalars viaToTensor()orMakeTensor()crashes withSIGSEGVwhen the zero-length values buffer is omitted.What changes are included in this PR?
Whole-array tensor conversion now slices the child values array by the outer row range—combining outer and child element offsets with overflow checks—before delegating to the shared tensor-buffer extractor so both array and scalar conversions clamp the view to the exact tensor byte length in one place. When the underlying values buffer is omitted or shorter than a zero-element slice offset, the shared extractor returns a static immutable zero-byte buffer for zero-element tensors and
Status::Invalidwhen elements are expected.Non-null backing buffers keep zero-copy read-only view sharing, shapes, permutations, strides, dimension names, and existing bounds checks on non-empty slices. Using a non-null immutable zero-byte buffer for omitted-buffer empty tensors preserves read-only ownership and tensor strides across downstream consumers such as
Tensor.to_numpy(), DLPack 1.0+, and IPC.Are these changes tested?
Yes. Added unit tests in
cpp/src/arrow/extension/tensor_extension_array_test.cccovering child-only and combined outer + child slicing (including non-trivial permutation, dimension names, and zero-copy pointer/size checks), zero-sized tensor shapes over sliced non-empty buffers, and omitted or empty values buffers on zero-element arrays and scalars (plusStatus::Invalidrejection when elements are expected).Verified locally on macOS arm64 (
Debug,-DARROW_BUILD_TESTS=ON): all 5 new tests fail or crash as expected against unpatchedmain, and the fullarrow-canonical-extensions-testsuite passes with this patch (45/45).clang-format,cpplint, andgit diff --checkare clean.Are there any user-facing changes?
No public API changes. Valid
FixedShapeTensorArrayinstances with sliced child arrays and valid zero-element tensor arrays and scalars now convert cleanly toarrow::Tensor(and PythonTensor/ NumPy / DLPack 1.0+) instead of returning wrong elements, raising spurious stride-overrun errors, or crashing.This PR contains a "Critical Fix". It fixes (b) silent wrong-offset data produced by
FixedShapeTensorArray::ToTensor()when the childvaluesarray is sliced, and (c) aSIGSEGVcrash onValidateFull()-valid zero-element tensor arrays and scalars whose values buffer is omitted.Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
AI coding agents assisted with source analysis, drafting the C++ fix and unit tests, and preparing this PR description. The author verified the root-cause analysis against the checked-out sources, reviewed the complete diff line by line, and ran the local C++ build and test suite reported above.
PR code and description written by:
Reviewed before submission by: