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? |
| 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 |
|
@abagusetty the issue is that there are no kernels for copying to strided host memory, only contiguous. So in such a case as this, we get a C-contiguous copy back. With cases like F-contiguous memory, A proper solution requires new kernels. At minimum, the limitation should be documented. |
| # apply the requested memory layout; ``"K"`` preserves the strides of the | ||
| # source array as closely as possible and is the default | ||
| return np.asarray(result, order=order) |
There was a problem hiding this comment.
part of the reason is that this will preserve the strides of the source result array, which are not necessarily correct per NumPy's order="K" semantics themselves
|
@abagusetty I will look for other edge cases, but if this works in this case, I don't see a likely issue otherwise |
Fixed
dpnp.asnumpyanddpnp.ndarray.asnumpyignoring theorderargFixes: #2568