Supporting order arg to asnumpy#2980
Conversation
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.
|
Can one of the admins verify this patch? |
| Args: | ||
| usm_ary (usm_ndarray): | ||
| Input array | ||
| order (``"C"``, ``"F"``, ``"A"``, ``"K"``): |
There was a problem hiding this comment.
| order (``"C"``, ``"F"``, ``"A"``, ``"K"``): | |
| order (``"C"``, ``"F"``, ``"A"``, ``"K"``, optional): |
There was a problem hiding this comment.
I believe None value is also support for tensor.asnumpy
| 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Any size == 0 array reports C_CONTIGUOUS = True and F_CONTIGUOUS = True regardless of order.
It'd make sense to leave as original:
| return np.ndarray(ary.shape, dtype=ary.dtype, order=order) | |
| return np.ndarray(ary.shape, dtype=ary.dtype) |
but place a comment
| 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") |
There was a problem hiding this comment.
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"] | ||
|
|
There was a problem hiding this comment.
Messing test with zero-sized ndarray
Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com>
|
@antonwolfy Thanks for taking time. Addressed the above suggestions |
Fixed
dpnp.asnumpyanddpnp.ndarray.asnumpyignoring theorderargFixes: #2568