Skip to content

Supporting order arg to asnumpy#2980

Open
abagusetty wants to merge 7 commits into
IntelPython:masterfrom
abagusetty:fix-asnumpy-order-2884
Open

Supporting order arg to asnumpy#2980
abagusetty wants to merge 7 commits into
IntelPython:masterfrom
abagusetty:fix-asnumpy-order-2884

Conversation

@abagusetty

Copy link
Copy Markdown
Contributor

Fixed dpnp.asnumpy and dpnp.ndarray.asnumpy ignoring the order arg
Fixes: #2568

  • 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?

abagusetty and others added 2 commits June 26, 2026 17:30
dpnp.asnumpy and dpnp.ndarray.asnumpy accepted an order argument but
dropped it for dpnp_array and usm_ndarray inputs. The underlying
_copy_to_numpy rebuilt the NumPy array from the source strides, so a
non-contiguous source was returned with a non-contiguous layout even
when order='C' was requested, diverging from NumPy/CuPy.

Thread order through the full conversion chain:
- _copy_to_numpy(ary, order='K'): apply layout via np.asarray
- dpt.asnumpy(usm_ary, order='K'): forward order
- dpnp_array.asnumpy(order='C'): forward order
- dpnp.asnumpy(a, order='C'): pass order to both array branches

Public APIs default to 'C' to match NumPy/CuPy; internal helpers default
to 'K' to preserve the existing stride-keeping behavior of their direct
callers (to_numpy, _ctors, _print).

Add TestAsNumpy covering iface/method/usm_ndarray paths and default
semantics.
@intel-python-devops

Copy link
Copy Markdown

Can one of the admins verify this patch?

@antonwolfy antonwolfy added this to the 0.21.0 release milestone Jun 30, 2026
@coveralls

coveralls commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.071% (-0.002%) from 78.073% — abagusetty:fix-asnumpy-order-2884 into IntelPython:master

Comment thread dpnp/tensor/_copy_utils.py Outdated
Args:
usm_ary (usm_ndarray):
Input array
order (``"C"``, ``"F"``, ``"A"``, ``"K"``):

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.

Suggested change
order (``"C"``, ``"F"``, ``"A"``, ``"K"``):
order (``"C"``, ``"F"``, ``"A"``, ``"K"``, optional):

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.

I believe None value is also support for tensor.asnumpy

Comment thread dpnp/tensor/_copy_utils.py Outdated
if ary.size == 0:
# no data needs to be copied for zero sized array
return np.ndarray(ary.shape, dtype=ary.dtype)
return np.ndarray(ary.shape, dtype=ary.dtype, order=order)

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.

I wonder if it is allowed to pass K and A values here, because based on the documentation it's limited to C and F only:

{‘C’, ‘F’}, optional

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.

Any size == 0 array reports C_CONTIGUOUS = True and F_CONTIGUOUS = True regardless of order.
It'd make sense to leave as original:

Suggested change
return np.ndarray(ary.shape, dtype=ary.dtype, order=order)
return np.ndarray(ary.shape, dtype=ary.dtype)

but place a comment

Comment thread CHANGELOG.md Outdated
def test_method_order_k_keeps_strides(self):
# explicit "K" keeps the strides of the source as closely as possible
a = self._non_c_contiguous_array()
result = a.asnumpy(order="K")

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.

missing test for order="A" and order=None

a = self._non_c_contiguous_array()
result = a.asnumpy(order="K")
assert not result.flags["C_CONTIGUOUS"]

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.

Messing test with zero-sized ndarray

@abagusetty

Copy link
Copy Markdown
Contributor Author

@antonwolfy Thanks for taking time. Addressed the above suggestions

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.asnumpy does not honor order arg

4 participants