-
Notifications
You must be signed in to change notification settings - Fork 29
Fix for dpnp buffer-arg ndarray ignoring offset #3068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abagusetty
wants to merge
3
commits into
IntelPython:master
Choose a base branch
from
abagusetty:fix-dpnp-bufferarg-offset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−10
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -529,6 +529,84 @@ def test_nonzero_offset_buffer_ctor(self): | |
| assert_array_equal(ia.view(), expected) | ||
| assert_array_equal(ia.view(dpnp.uint32), expected.view(numpy.uint32)) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "src_dt, new_dt", | ||
| [ | ||
| (dpnp.complex64, dpnp.uint16), | ||
| (dpnp.complex128, dpnp.float64), | ||
| (dpnp.float64, dpnp.float32), | ||
| (dpnp.int64, dpnp.int8), | ||
| (dpnp.int32, dpnp.int16), | ||
| (dpnp.int16, dpnp.int64), | ||
| ], | ||
| ) | ||
| def test_nonzero_offset_buffer_ctor_dtype_mismatch(self, src_dt, new_dt): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test hard-fails on devices without native fp64/complex128 support.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guarded it |
||
| if not has_support_aspect64() and ( | ||
| dpnp.dtype(src_dt) in [dpnp.float64, dpnp.complex128] | ||
| or dpnp.dtype(new_dt) in [dpnp.float64, dpnp.complex128] | ||
| ): | ||
| pytest.skip("requires fp64 support") | ||
|
|
||
| # the element offset of the `buffer=` array is expressed in units of | ||
| # the buffer's own dtype and has to be rescaled when the requested | ||
| # dtype has a different itemsize | ||
| base = dpnp.arange(32, dtype=src_dt) | ||
| sl = base[8:] | ||
|
|
||
| byte_offset = 8 * dpnp.dtype(src_dt).itemsize | ||
| size = (base.nbytes - byte_offset) // dpnp.dtype(new_dt).itemsize | ||
|
|
||
| ia = dpnp.ndarray((size,), dtype=new_dt, buffer=sl) | ||
| assert ia.data.ptr == sl.data.ptr | ||
| assert_array_equal(ia, dpnp.asnumpy(sl).view(new_dt)) | ||
|
|
||
| # an explicit `offset` is expressed in units of the requested dtype | ||
| # and adds up with the rescaled offset of the buffer | ||
| ia = dpnp.ndarray((size - 1,), dtype=new_dt, buffer=sl, offset=1) | ||
| assert ia.data.ptr == sl.data.ptr + dpnp.dtype(new_dt).itemsize | ||
| assert_array_equal(ia, dpnp.asnumpy(sl).view(new_dt)[1:]) | ||
|
|
||
| def test_nonzero_offset_buffer_ctor_usm_ndarray(self): | ||
| # the same rescaling applies when `buffer=` is a bare usm_ndarray | ||
| # rather than a dpnp.ndarray | ||
| base = dpnp.arange(16, dtype=dpnp.complex64) | ||
| sl = base[4:] | ||
| usm_sl = sl.get_array() | ||
|
|
||
| for dt in [dpnp.complex64, dpnp.uint16, dpnp.float32]: | ||
| size = usm_sl.nbytes // dpnp.dtype(dt).itemsize | ||
| ia = dpnp.ndarray((size,), dtype=dt, buffer=usm_sl) | ||
| assert ia.data.ptr == sl.data.ptr | ||
|
|
||
| # and the dtype still defaults to the buffer's one | ||
| ia = dpnp.ndarray((12,), buffer=usm_sl) | ||
| assert ia.dtype == base.dtype | ||
| assert ia.data.ptr == sl.data.ptr | ||
|
|
||
| def test_nonzero_offset_buffer_ctor_write_through(self): | ||
| # a write through the dtype-mismatched view must land in the parent | ||
| # allocation at the offset the buffer points at | ||
| base = dpnp.zeros(16, dtype=dpnp.complex64) | ||
| sl = base[8:] | ||
|
|
||
| ia = dpnp.ndarray((16,), dtype=dpnp.float32, buffer=sl) | ||
| ia[:] = 1 | ||
|
|
||
| expected = numpy.zeros(16, dtype=numpy.complex64) | ||
| expected[8:] = 1 + 1j | ||
| assert_array_equal(base, expected) | ||
|
|
||
| def test_misaligned_offset_buffer_ctor_error(self): | ||
| base = dpnp.arange(16, dtype=dpnp.int16) | ||
| # the buffer starts at a byte offset of 6, which is not addressable | ||
| # with an itemsize of 8 | ||
| with pytest.raises(ValueError, match="not a multiple"): | ||
| dpnp.ndarray((3,), dtype=dpnp.int64, buffer=base[3:]) | ||
|
|
||
| # and the same holds for a bare usm_ndarray buffer | ||
| with pytest.raises(ValueError, match="not a multiple"): | ||
| dpnp.ndarray((3,), dtype=dpnp.int64, buffer=base[3:].get_array()) | ||
|
|
||
| def test_misaligned_offset_error(self): | ||
| ia = dpnp.arange(10, dtype=dpnp.int16) | ||
| # numpy supports such a view, but usm_ndarray cannot address memory | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two small gaps worth filling while we're here (non-blocking):
No test exercises a non-zero incoming
offset=arg together with a dtype-mismatched buffer — theoffset=1case uses a same-itemsize dtype, so theoffset += add_offsetaddition is never checked with a non-trivialadd_offset. A case likedpnp.ndarray((size,), dtype=new_dt, buffer=sl, offset=1)would cover it.The misalignment
ValueErroris only asserted for adpnp_arraybuffer (test_misaligned_offset_buffer_ctor_error). Since the fix routes bareusm_ndarraybuffers through the same branch, a matching case withbuffer=base[3:].get_array()would guard that path too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed both aspects