-
Notifications
You must be signed in to change notification settings - Fork 27
Add implementation of dpnp.lib.stride_tricks.as_strided
#2991
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1476580
Add dpnp.lib submodule and implementation of stride_tricks
antonwolfy 5241f7e
Enable affected third party tests
antonwolfy 8cb5eb6
Add more tests coverage
antonwolfy 4f50ff6
Add documentation page for new submodule
antonwolfy e4091e3
Update changelog
antonwolfy 4b62bf8
Add more tests to cover special cases
antonwolfy 1673d95
Limit tests by NumPy version where check_bounds keyword was implemented
antonwolfy 1a05259
Add lib to the submodules
antonwolfy c3a0069
Merge branch 'master' into impl-lib.as_strided
antonwolfy 0560eea
Merge branch 'master' into impl-lib.as_strided
antonwolfy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| .. _routines.lib: | ||
| .. module:: dpnp.lib | ||
|
|
||
| Lib module (:mod:`dpnp.lib`) | ||
| ============================ | ||
|
|
||
| .. hint:: `NumPy API Reference: Lib module (numpy.lib) <https://numpy.org/doc/stable/reference/routines.lib.html>`_ | ||
|
|
||
| Submodules | ||
| ---------- | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
|
|
||
| stride_tricks |
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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ These functions cover a subset of | |
| functional | ||
| io | ||
| indexing | ||
| lib | ||
| linalg | ||
| logic | ||
| math | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # ***************************************************************************** | ||
| # Copyright (c) 2026, Intel Corporation | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # - Redistributions of source code must retain the above copyright notice, | ||
| # this list of conditions and the following disclaimer. | ||
| # - Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # - Neither the name of the copyright holder nor the names of its contributors | ||
| # may be used to endorse or promote products derived from this software | ||
| # without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
| # THE POSSIBILITY OF SUCH DAMAGE. | ||
| # ***************************************************************************** | ||
|
|
||
| """ | ||
| ``dpnp.lib`` | ||
| ============ | ||
|
|
||
| The library of stride-manipulation utilities of DPNP. | ||
|
|
||
| This namespace mimics parts of ``numpy.lib`` on top of DPNP functionality. | ||
| """ | ||
|
|
||
| from . import stride_tricks | ||
|
|
||
| __all__ = ["stride_tricks"] |
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 |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| # ***************************************************************************** | ||
| # Copyright (c) 2026, Intel Corporation | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # - Redistributions of source code must retain the above copyright notice, | ||
| # this list of conditions and the following disclaimer. | ||
| # - Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # - Neither the name of the copyright holder nor the names of its contributors | ||
| # may be used to endorse or promote products derived from this software | ||
| # without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
| # THE POSSIBILITY OF SUCH DAMAGE. | ||
| # ***************************************************************************** | ||
|
|
||
| """Utilities that manipulate strides to achieve desirable effects.""" | ||
|
|
||
| import dpnp | ||
|
|
||
| __all__ = ["as_strided"] | ||
|
|
||
|
|
||
| def as_strided( | ||
| x, | ||
| shape=None, | ||
| strides=None, | ||
| subok=False, | ||
| writeable=True, | ||
| *, | ||
| check_bounds=None, | ||
| ): | ||
| """ | ||
| Create a view into the array with the given shape and strides. | ||
|
|
||
| For full documentation refer to :obj:`numpy.lib.stride_tricks.as_strided`. | ||
|
|
||
| Warnings | ||
| -------- | ||
| This function has to be used with extreme care, see notes. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x : {dpnp.ndarray, usm_ndarray} | ||
| Array to create a new view from. | ||
| shape : {None, sequence of ints}, optional | ||
| The shape of the new array. | ||
|
|
||
| Default: ``x.shape``. | ||
| strides : {None, sequence of ints}, optional | ||
|
vlad-perevezentsev marked this conversation as resolved.
|
||
| The strides of the new array, expressed in bytes. | ||
|
|
||
| Default: ``x.strides``. | ||
| writeable : bool, optional | ||
| If set to ``False``, the returned array will always be read-only. | ||
| Otherwise it will be writable if the original array was. | ||
|
|
||
| Default: ``True``. | ||
| check_bounds : {None, bool}, optional | ||
| Ignored as no effect, the underlying USM array cannot be constructed | ||
| over out-of-bounds memory. | ||
|
|
||
| Default: ``None``. | ||
|
|
||
| Returns | ||
| ------- | ||
| view : dpnp.ndarray | ||
| A view into the memory of `x` with the requested `shape` and `strides`, | ||
| sharing the same data. | ||
|
|
||
| Limitations | ||
| ----------- | ||
| Parameter `subok` is supported with default value. | ||
| Otherwise ``NotImplementedError`` exception will be raised. | ||
|
|
||
| See Also | ||
| -------- | ||
| :obj:`dpnp.broadcast_to` : Broadcast an array to a given shape. | ||
| :obj:`dpnp.reshape` : Give a new shape to an array without changing its | ||
| data. | ||
|
|
||
| Notes | ||
| ----- | ||
| :obj:`dpnp.lib.stride_tricks.as_strided` creates a view into the array | ||
| given the exact strides and shape. This means it manipulates the internal | ||
| data structure of the array and, if done incorrectly, the array elements | ||
| can point to the wrong data and silently produce incorrect results. It is | ||
| advisable to always use the original ``x.strides`` when calculating new | ||
| strides to avoid reliance on a contiguous memory layout. | ||
|
|
||
| Furthermore, arrays created with this function often contain self | ||
| overlapping memory, so that two elements are identical. Writing to a shared | ||
| element then changes every position that references it, so element-wise | ||
| write operations on such arrays are typically unpredictable. A bulk write | ||
| over an overlapping view is rejected, because it would address more memory | ||
| than the base allocation holds. | ||
|
|
||
| Since writing to these arrays has to be tested and done with great care, | ||
| you may want to use ``writeable=False`` to avoid accidental write | ||
| operations. | ||
|
|
||
| For these reasons it is advisable to avoid | ||
| :obj:`dpnp.lib.stride_tricks.as_strided` when possible. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> import dpnp as np | ||
| >>> x = np.array([1, 2, 3, 4], dtype=np.int32) | ||
|
|
||
| Downsample the array by taking every second element: | ||
|
|
||
| >>> np.lib.stride_tricks.as_strided(x, shape=(2,), | ||
| ... strides=(2 * x.itemsize,)) | ||
| array([1, 3], dtype=int32) | ||
|
|
||
| Broadcast the array along a new leading axis using a zero stride: | ||
|
|
||
| >>> np.lib.stride_tricks.as_strided(x, shape=(3, 4), | ||
| ... strides=(0, x.itemsize)) | ||
| array([[1, 2, 3, 4], | ||
| [1, 2, 3, 4], | ||
| [1, 2, 3, 4]], dtype=int32) | ||
|
|
||
| Build a self-overlapping sliding-window view, where a single element maps | ||
| onto several positions. Here a length-5 array yields a ``3x3`` window in | ||
| which each value repeats along the anti-diagonals: | ||
|
|
||
| >>> y = np.arange(5, dtype=np.int32) | ||
| >>> np.lib.stride_tricks.as_strided(y, shape=(3, 3), | ||
| ... strides=(y.itemsize, y.itemsize)) | ||
| array([[0, 1, 2], | ||
| [1, 2, 3], | ||
| [2, 3, 4]], dtype=int32) | ||
|
|
||
| Attempting to create an out-of-bounds view: | ||
|
|
||
| >>> np.lib.stride_tricks.as_strided(y, shape=(10,), | ||
| ... strides=(y.itemsize,)) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: buffer='[0 1 2 3 4]' can not accommodate the requested array. | ||
|
|
||
| """ | ||
|
|
||
| dpnp.check_supported_arrays_type(x) | ||
| dpnp.check_limitations(subok=subok) | ||
|
vlad-perevezentsev marked this conversation as resolved.
|
||
|
|
||
| shape = x.shape if shape is None else tuple(shape) | ||
| strides = x.strides if strides is None else tuple(strides) | ||
|
|
||
| view = dpnp.ndarray( | ||
|
vlad-perevezentsev marked this conversation as resolved.
|
||
| shape, | ||
| dtype=x.dtype, | ||
| buffer=x, | ||
| strides=strides, | ||
| ) | ||
|
|
||
| if view.flags.writable and not writeable: | ||
| view.flags.writable = False | ||
| return view | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.