8000 Apply review comments · numpy/numpy@2cda618 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2cda618

Browse files
committed
Apply review comments
1 parent 6c51000 commit 2cda618

File tree

8 files changed

+39
-92
lines changed

8 files changed

+39
-92
lines changed
Lines changed: 3 additions & 1 deletion
  • < 8000 /ul>
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
``ndarray.device`` and ``ndarray.to_device``
22
--------------------------------------------
33

4-
``ndarray.device`` attribute and ``ndarray.to_device`` function were
4+
``ndarray.device`` attribute and ``ndarray.to_device`` method were
55
added to `numpy.ndarray` class for Array API compatibility.
66

77
Additionally, ``device`` keyword-only arguments were added to:
88
`numpy.asarray`, `numpy.arange`, `numpy.empty`, `numpy.empty_like`,
99
`numpy.eye`, `numpy.full`, `numpy.full_like`, `numpy.linspace`,
1010
`numpy.ones`, `numpy.ones_like`, `numpy.zeros`, and `numpy.zeros_like`.
11+
12+
For all these new arguments, only ``device="cpu"`` is supported.

numpy/_core/_add_newdocs.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,7 @@
933933
Defaults to 'K'.
934934
device : str, optional
935935
The device on which to place the created array. Default: None.
936-
937-
.. note::
938-
939-
Only the ``"cpu"`` device is supported by NumPy.
936+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
940937
941938
.. versionadded:: 2.0.0
942939
${ARRAY_FUNCTION_LIKE}
@@ -946,8 +943,8 @@
946943
Returns
947944
-------
948945
out : ndarray
949-
Array interpretation of `a`. No copy is performed if the input
950-
is already an ndarray with matching dtype and order. If `a` is a
946+
Array interpretation of ``a``. No copy is performed if the input
947+
is already an ndarray with matching dtype and order. If ``a`` is a
951948
subclass of ndarray, a base class ndarray is returned.
952949
953950
See Also
@@ -1209,10 +1206,7 @@
12091206
memory.
12101207
device : str, optional
12111208
The device on which to place the created array. Default: None.
1212-
1213-
.. note::
1214-
1215-
Only the ``"cpu"`` device is supported by NumPy.
1209+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
12161210
12171211
.. versionadded:: 2.0.0
12181212
${ARRAY_FUNCTION_LIKE}
@@ -1735,10 +1729,7 @@
17351729
type from the other input arguments.
17361730
device : str, optional
17371731
The device on which to place the created array. Default: None.
1738-
1739-
.. note::
1740-
1741-
Only the ``"cpu"`` device is supported by NumPy.
1732+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
17421733
17431734
.. versionadded:: 2.0.0
17441735
${ARRAY_FUNCTION_LIKE}

numpy/_core/function_base.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,
7272
.. versionadded:: 1.16.0
7373
device : str, optional
7474
The device on which to place the created array. Default: None.
75-
76-
.. note::
77-
78-
Only the ``"cpu"`` device is supported by NumPy.
75+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
7976
8077
.. versionadded:: 2.0.0
8178
@@ -126,11 +123,6 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,
126123
>>> plt.show()
127124
128125
"""
129-
if device not in ["cpu", None]:
130-
raise ValueError(
131-
f"Unsupported device: {device}. Only \"cpu\" is allowed."
132-
)
133-
134126
num = operator.index(num)
135127
if num < 0:
136128
raise ValueError(
@@ -152,7 +144,9 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,
152144
integer_dtype = _nx.issubdtype(dtype, _nx.integer)
153145

154146
delta = stop - start
155-
y = _nx.arange(0, num, dtype=dt).reshape((-1,) + (1,) * ndim(delta))
147+
y = _nx.arange(
148+
0, num, dtype=dt, device=device
149+
).reshape((-1,) + (1,) * ndim(delta))
156150
# In-place multiplication y *= delta/div is faster, but prevents
157151
# the multiplicant from overriding what class is produced, and thus
158152
# prevents, e.g. use of Quantities, see gh-7142. Hence, we multiply

numpy/_core/multiarray.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ def empty_like(
118118
.. versionadded:: 1.17.0
119119
device : str, optional
120120
The device on which to place the created array. Default: None.
121-
122-
.. note::
123-
124-
Only the ``"cpu"`` device is supported by NumPy.
121+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
125122
126123
.. versionadded:: 2.0.0
127124

numpy/_core/numeric.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ def zeros_like(
9696
.. versionadded:: 1.17.0
9797
device : str, optional
9898
The device on which to place the created array. Default: None.
99-
100-
.. note::
101-
102-
Only the ``"cpu"`` device is supported by NumPy.
99+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
103100
104101
.. versionadded:: 2.0.0
105102
@@ -161,10 +158,7 @@ def ones(shape, dtype=None, order='C', *, device=None, like=None):
161158
memory.
162159
device : str, optional
163160
The device on which to place the created array. Default: None.
164-
165-
.. note::
166-
167-
Only the ``"cpu"`` device is supported by NumPy.
161+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
168162
169163
.. versionadded:: 2.0.0
170164
${ARRAY_FUNCTION_LIKE}
@@ -201,15 +195,12 @@ def ones(shape, dtype=None, order='C', *, device=None, like=None):
201195
[1., 1.]])
202196
203197
"""
204-
if device not in ["cpu", None]:
205-
raise ValueError(
206-
f"Unsupported device: {device}. Only \"cpu\" is allowed."
207-
)
208-
209198
if like is not None:
210-
return _ones_with_like(like, shape, dtype=dtype, order=order)
199+
return _ones_with_like(
200+
like, shape, dtype=dtype, order=order, device=device
201+
)
211202

212-
a = empty(shape, dtype, order)
203+
a = empty(shape, dtype, order, device=device)
213204
multiarray.copyto(a, 1, casting='unsafe')
214205
return a
215206

@@ -258,10 +249,7 @@ def ones_like(
258249
.. versionadded:: 1.17.0
259250
device : str, optional
260251
The device on which to place the created array. Default: None.
261-
262-
.. note::
263-
264-
Only the ``"cpu"`` device is supported by NumPy.
252+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
265253
266254
.. versionadded:: 2.0.0
267255
@@ -328,10 +316,7 @@ def full(shape, fill_value, dtype=None, order='C', *, device=None, like=None):
328316
(row- or column-wise) order in memory.
329317
device : str, optional
330318
The device on which to place the created array. Default: None.
331-
332-
.. note::
333-
334-
Only the ``"cpu"`` device is supported by NumPy.
319+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
335320
336321
.. versionadded:: 2.0.0
337322
${ARRAY_FUNCTION_LIKE}
@@ -364,19 +349,15 @@ def full(shape, fill_value, dtype=None, order='C', *, device=None, like=None):
364349
[1, 2]])
365350
366351
"""
367-
if device not in ["cpu", None]:
368-
raise ValueError(
369-
f"Unsupported device: {device}. Only \"cpu\" is allowed."
370-
)
371-
372352
if like is not None:
373353
return _full_with_like(
374-
like, shape, fill_value, dtype=dtype, order=order)
354+
like, shape, fill_value, dtype=dtype, order=order, device=device
355+
)
375356

376357
if dtype is None:
377358
fill_value = asarray(fill_value)
378359
dtype = fill_value.dtype
379-
a = empty(shape, dtype, order)
360+
a = empty(shape, dtype, order, device=device)
380361
multiarray.copyto(a, fill_value, casting='unsafe')
381362
return a
382363

@@ -425,10 +406,7 @@ def full_like(
425406
.. versionadded:: 1.17.0
426407
device : str, optional
427408
The device on which to place the created array. Default: None.
428-
429-
.. note::
430-
431-
Only the ``"cpu"`` device is supported by NumPy.
409+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
432410
433411
.. versionadded:: 2.0.0
434412
@@ -467,12 +445,9 @@ def full_like(
467445
[[ 0, 0, 255],
468446
[ 0, 0, 255]]])
469447
"""
470-
if device not in ["cpu", None]:
471-
raise ValueError(
472-
f"Unsupported device: {device}. Only \"cpu\" is allowed."
473-
)
474-
475-
res = empty_like(a, dtype=dtype, order=order, subok=subok, shape=shape)
448+
res = empty_like(
449+
a, dtype=dtype, order=order, subok=subok, shape=shape, device=device
450+
)
476451
multiarray.copyto(res, fill_value, casting='unsafe')
477452
return res
478453

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,13 +2181,15 @@ array_zeros(PyObject *NPY_UNUSED(ignored),
21812181
NPY_ORDER order = NPY_CORDER;
21822182
npy_bool is_f_order = NPY_FALSE;
21832183
PyArrayObject *ret = NULL;
2184+
NPY_DEVICE device = NPY_DEVICE_CPU;
21842185
PyObject *like = Py_None;
21852186
NPY_PREPARE_ARGPARSER;
21862187

21872188
if (npy_parse_arguments("zeros", args, len_args, kwnames,
21882189
"shape", &PyArray_IntpConverter, &shape,
21892190
"|dtype", &PyArray_DTypeOrDescrConverterOptional, &dt_info,
21902191
"|order", &PyArray_OrderConverter, &order,
2192+
"$device", &PyArray_DeviceConverterOptional, &device,
21912193
"$like", NULL, &like,
21922194
NULL, NULL, NULL) < 0) {
21932195
goto finish;

numpy/fft/_helper.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,14 @@ def fftfreq(n, d=1.0, device=None):
164164
array([ 0. , 1.25, 2.5 , ..., -3.75, -2.5 , -1.25])
165165
166166
"""
167-
if device not in ["cpu", None]:
168-
raise ValueError(
169-
f"Unsupported device: {device}. Only \"cpu\" is allowed."
170-
)
171167
if not isinstance(n, integer_types):
172168
raise ValueError("n should be an integer")
173169
val = 1.0 / (n * d)
174-
results = empty(n, int)
170+
results = empty(n, int, device=device)
175171
N = (n-1)//2 + 1
176-
p1 = arange(0, N, dtype=int)
172+
p1 = arange(0, N, dtype=int, device=device)
177173
results[:N] = p1
178-
p2 = arange(-(n//2), 0, dtype=int)
174+
p2 = arange(-(n//2), 0, dtype=int, device=device)
179175
results[N:] = p2
180176
return results * val
181177

@@ -231,13 +227,9 @@ def rfftfreq(n, d=1.0, device=None):
231227
array([ 0., 10., 20., 30., 40., 50.])
232228
233229
"""
234-
if device not in ["cpu", None]:
235-
raise ValueError(
236-
f"Unsupported device: {device}. Only \"cpu\" is allowed."
237-
)
238230
if not isinstance(n, integer_types):
239231
raise ValueError("n should be an integer")
240232
val = 1.0/(n*d)
241233
N = n//2 + 1
242-
results = arange(0, N, dtype=int)
234+
results = arange(0, N, dtype=int, device=device)
243235
return results * val

numpy/lib/_twodim_base_impl.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,7 @@ def eye(N, M=None, k=0, dtype=float, order='C', *, device=None, like=None):
180180
.. versionadded:: 1.14.0
181181
device : str, optional
182182
The device on which to place the created array. Default: None.
183-
184-
.. note::
185-
186-
Only the ``"cpu"`` device is supported by NumPy.
183+
For Array-API interoperability only, so must be ``"cpu"`` if passed.
187184
188185
.. versionadded:: 2.0.0
189186
${ARRAY_FUNCTION_LIKE}
@@ -212,16 +209,13 @@ def eye(N, M=None, k=0, dtype=float, order='C', *, device=None, like=None):
212209
[0., 0., 0.]])
213210
214211
"""
215-
if device not in ["cpu", None]:
216-
raise ValueError(
217-
f"Unsupported device: {device}. Only \"cpu\" is allowed."
218-
)
219-
220212
if like is not None:
221-
return _eye_with_like(like, N, M=M, k=k, dtype=dtype, order=order)
213+
return _eye_with_like(
214+
like, N, M=M, k=k, dtype=dtype, order=order, device=device
215+
)
222216
if M is None:
223217
M = N
224-
m = zeros((N, M), dtype=dtype, order=order)
218+
m = zeros((N, M), dtype=dtype, order=order, device=device)
225219
if k >= M:
226220
return m
227221
# Ensure M and k are integers, so we don't get any surprise casting

0 commit comments

Comments
 (0)
0