-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
API: Add device
and to_device
to numpy.ndarray
[Array API]
#25233
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
``ndarray.device`` and ``ndarray.to_device`` | ||
-------------------------------------------- | ||
|
||
``ndarray.device`` attribute and ``ndarray.to_device`` method were | ||
added to `numpy.ndarray` class for Array API compatibility. | ||
|
||
Additionally, ``device`` keyword-only arguments were added to: | ||
`numpy.asarray`, `numpy.arange`, `numpy.empty`, `numpy.empty_like`, | ||
`numpy.eye`, `numpy.full`, `numpy.full_like`, `numpy.linspace`, | ||
`numpy.ones`, `numpy.ones_like`, `numpy.zeros`, and `numpy.zeros_like`. | ||
|
||
For all these new arguments, only ``device="cpu"`` is supported. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -912,7 +912,7 @@ | |||||||||||||||||
|
||||||||||||||||||
add_newdoc('numpy._core.multiarray', 'asarray', | ||||||||||||||||||
""" | ||||||||||||||||||
asarray(a, dtype=None, order=None, *, like=None) | ||||||||||||||||||
asarray(a, dtype=None, order=None, *, device=None, like=None) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and below, why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why not just set the default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now the function dispatch requires numpy/numpy/_core/overrides.py Lines 149 to 156 in 448e4da
Can I keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's true that We're adding these keyword arguments for Array API compatibility only, so I would prefer to fully conform with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it matters from the Array API perspective, it doesn't make NumPy non-compliant, it just makes it explicit about what that default is. But, I'll give a very slight vote to just keep it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order doesn't matter much in principle, since it's keyword-only and >>> import numpy as np
>>> import dask.array as da
>>> d = da.ones((1, 2))
>>> np.asarray([1, 2], device='cpu')
array([1, 2])
>>> np.asarray([1, 2], device='cpu', like=d)
Traceback (most recent call last):
Cell In[24], line 1
np.asarray([1, 2], device='cpu', like=d)
File ~/code/tmp/dask/dask/array/core.py:1760 in __array_function__
return da_func(*args, **kwargs)
File ~/code/tmp/dask/dask/array/core.py:4581 in asarray
return from_array(a, getitem=getter_inline, **kwargs)
TypeError: from_array() got an unexpected keyword argument 'device' The In this case, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah wait, it's maybe not that extreme - >>> np.asarray([1, 2], device='cpu')
array([1, 2])
>>> np.asarray([1, 2], like=d)
dask.array<array, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>
>>> np.asarray([1, 2], like=d, device='cpu')
...
TypeError: from_array() got an unexpected keyword argument 'device' This would have been much more problematic if libraries like SciPy and scikit-learn supported non-numpy array inputs via And it's fairly easy to add support for new keywords in Dask & co. They just need a new 2.0-compatible release that adds such support, and then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rgommers - I've gotten quite used to updating astropy's On the order: if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to clarify: the check is only there to test habits, and probably mainly to make sure it is kwarg only. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @mhvk, that is useful context. A test that flag mismatches sounds like a great idea. 🤞🏼 most implementers have that. |
||||||||||||||||||
|
||||||||||||||||||
Convert the input to an array. | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -931,15 +931,20 @@ | |||||||||||||||||
'A' (any) means 'F' if `a` is Fortran contiguous, 'C' otherwise | ||||||||||||||||||
'K' (keep) preserve input order | ||||||||||||||||||
Defaults to 'K'. | ||||||||||||||||||
device : str, optional | ||||||||||||||||||
The device on which to place the created array. Default: None. | ||||||||||||||||||
For Array-API interoperability only, so must be ``"cpu"`` if passed. | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 2.0.0 | ||||||||||||||||||
${ARRAY_FUNCTION_LIKE} | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 1.20.0 | ||||||||||||||||||
|
||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
out : ndarray | ||||||||||||||||||
Array interpretation of `a`. No copy is performed if the input | ||||||||||||||||||
is already an ndarray with matching dtype and order. If `a` is a | ||||||||||||||||||
Array interpretation of ``a``. No copy is performed if the input | ||||||||||||||||||
is already an ndarray with matching dtype and order. If ``a`` is a | ||||||||||||||||||
subclass of ndarray, a base class ndarray is returned. | ||||||||||||||||||
|
||||||||||||||||||
See Also | ||||||||||||||||||
|
@@ -1184,7 +1189,7 @@ | |||||||||||||||||
|
||||||||||||||||||
add_newdoc('numpy._core.multiarray', 'empty', | ||||||||||||||||||
""" | ||||||||||||||||||
empty(shape, dtype=float, order='C', *, like=None) | ||||||||||||||||||
empty(shape, dtype=float, order='C', *, device=None, like=None) | ||||||||||||||||||
|
||||||||||||||||||
Return a new array of given shape and type, without initializing entries. | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -1199,6 +1204,11 @@ | |||||||||||||||||
Whether to store multi-dimensional data in row-major | ||||||||||||||||||
(C-style) or column-major (Fortran-style) order in | ||||||||||||||||||
memory. | ||||||||||||||||||
device : str, optional | ||||||||||||||||||
The device on which to place the created array. Default: None. | ||||||||||||||||||
For Array-API interoperability only, so must be ``"cpu"`` if passed. | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 2.0.0 | ||||||||||||||||||
${ARRAY_FUNCTION_LIKE} | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 1.20.0 | ||||||||||||||||||
|
@@ -1676,7 +1686,7 @@ | |||||||||||||||||
|
||||||||||||||||||
add_newdoc('numpy._core.multiarray', 'arange', | ||||||||||||||||||
""" | ||||||||||||||||||
arange([start,] stop[, step,], dtype=None, *, like=None) | ||||||||||||||||||
arange([start,] stop[, step,], dtype=None, *, device=None, like=None) | ||||||||||||||||||
|
||||||||||||||||||
Return evenly spaced values within a given interval. | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -1717,6 +1727,11 @@ | |||||||||||||||||
dtype : dtype, optional | ||||||||||||||||||
The type of the output array. If `dtype` is not given, infer the data | ||||||||||||||||||
type from the other input arguments. | ||||||||||||||||||
device : str, optional | ||||||||||||||||||
The device on which to place the created array. Default: None. | ||||||||||||||||||
For Array-API interoperability only, so must be ``"cpu"`` if passed. | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 2.0.0 | ||||||||||||||||||
${ARRAY_FUNCTION_LIKE} | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 1.20.0 | ||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.