Skip to content

Add implementation of dpnp.lib.stride_tricks.as_strided#2991

Open
antonwolfy wants to merge 7 commits into
masterfrom
impl-lib.as_strided
Open

Add implementation of dpnp.lib.stride_tricks.as_strided#2991
antonwolfy wants to merge 7 commits into
masterfrom
impl-lib.as_strided

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

This PR adds a new dpnp.lib namespace and implements dpnp.lib.stride_tricks.as_strided.
The function creates a view into an array with a caller-specified shape and strides, sharing the base array's memory.

It closes #2969.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@antonwolfy antonwolfy added this to the 0.21.0 release milestone Jul 3, 2026
@antonwolfy antonwolfy self-assigned this Jul 3, 2026
@antonwolfy antonwolfy force-pushed the impl-lib.as_strided branch from 3b47991 to 4b62bf8 Compare July 3, 2026 12:27
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/2991/index.html

@coveralls

coveralls commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.127% (+0.02%) from 78.107% — impl-lib.as_strided into master

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev2=py313h509198e_19 ran successfully.
Passed: 1373
Failed: 2
Skipped: 5

@antonwolfy antonwolfy marked this pull request as ready for review July 6, 2026 12:02
Comment thread dpnp/lib/stride_tricks.py
Comment thread dpnp/lib/stride_tricks.py
Comment thread dpnp/lib/stride_tricks.py
"""

dpnp.check_supported_arrays_type(x)
dpnp.check_limitations(subok=subok)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to raise a warning or an error if the user passes a bool value to check_bounds parameter?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ignoring that keyword, why do we need to validate it?

Comment thread dpnp/__init__.py
from ._version import get_versions
from . import exceptions as exceptions
from . import fft as fft
from . import lib as lib

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add lib to submodules as is done for fft/ linalg on line 1060?

a = generate_random_numpy_array((4,), dtype=dt)
ia = dpnp.array(a)

result = as_strided(ia, shape=(2,), strides=(2 * ia.itemsize,))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we pass strides that are not multiples of itemsize we willll get incorrect strides result

In [21]: a = dpnp.array([1, 2, 3, 4, 5], dtype="int32")

In [22]: a_np = dpnp.asnumpy(a)

In [23]: res = dpnp.lib.stride_tricks.as_strided(a, shape=(3,), strides=(6,))

In [24]: res_np = numpy.lib.stride_tricks.as_strided(a_np, shape=(3,), strides=(6,))

In [25]: res.strides
Out[25]: (4,)

In [26]: res_np.strides
Out[26]: (6,)

In [27]: res
Out[27]: array([1, 2, 3], dtype=int32)

In [28]: res_np
Out[28]: array([     1, 196608,      4], dtype=int32)

Maybe it makes sense to add an error exception to ndarray stride logic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dpnp.lib.stride_tricks.as_strided mentioned in documentation but not implemented

3 participants