| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + ``ndarray.device`` and ``ndarray.to_device`` | ||
| 2 | + -------------------------------------------- | ||
| 3 | + | ||
| 4 | + ``ndarray.device`` attribute and ``ndarray.to_device`` function were | ||
| 5 | + added to `numpy.ndarray` class for Array API compatibility. | ||
| 6 | + | ||
| 7 | + Additionally, ``device`` keyword-only arguments were added to: | ||
| 8 | + `numpy.asarray`, `numpy.arange`, `numpy.empty`, `numpy.empty_like`, | ||
| 9 | + `numpy.eye`, `numpy.full`, `numpy.full_like`, `numpy.linspace`, | ||
| 10 | + `numpy.ones`, `numpy.ones_like`, `numpy.zeros`, and `numpy.zeros_like`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -588,12 +588,6 @@ Creation functions differences | |||
| 588 | 588 | * - ``copy`` keyword argument to ``asarray`` | |
| 589 | 589 | - **Compatible** | |
| 590 | 590 | - | |
| 591 | - * - New ``device`` keyword argument to all array creation functions | ||
| 592 | - (``asarray``, ``arange``, ``empty``, ``empty_like``, ``eye``, ``full``, | ||
| 593 | - ``full_like``, ``linspace``, ``ones``, ``ones_like``, ``zeros``, and | ||
| 594 | - ``zeros_like``). | ||
| 595 | - - **Compatible** | ||
| 596 | - - ``device`` would effectively do nothing, since NumPy is CPU only. | ||
| 597 | 591 | ||
| 598 | 592 | Elementwise functions differences | |
| 599 | 593 | --------------------------------- | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2465,6 +2465,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): | |||
| 2465 | 2465 | ||
| 2466 | 2466 | def __array_namespace__(self, *, api_version: str = ...) -> Any: ... | |
| 2467 | 2467 | ||
| 2468 | + def to_device(self, device: L["cpu"], /, *, stream: None | int | Any = ...) -> NDArray[Any]: ... | ||
| 2469 | + | ||
| 2470 | + @property | ||
| 2471 | + def device(self) -> L["cpu"]: ... | ||
| 2472 | + | ||
| 2468 | 2473 | def bitwise_count( | |
| 2469 | 2474 | self, | |
| 2470 | 2475 | out: None | NDArray[Any] = ..., | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -912,7 +912,7 @@ | |||
| 912 | 912 | ||
| 913 | 913 | add_newdoc('numpy._core.multiarray', 'asarray', | |
| 914 | 914 | """ | |
| 915 | - asarray(a, dtype=None, order=None, *, like=None) | ||
| 915 | + asarray(a, dtype=None, order=None, *, device=None, like=None) | ||
| 916 | 916 | ||
| 917 | 917 | Convert the input to an array. | |
| 918 | 918 | ||
@@ -931,6 +931,10 @@ | |||
| 931 | 931 | 'A' (any) means 'F' if `a` is Fortran contiguous, 'C' otherwise | |
| 932 | 932 | 'K' (keep) preserve input order | |
| 933 | 933 | Defaults to 'K'. | |
| 934 | + device : str, optional | ||
| 935 | + The device on which to place the created array. Default: None. | ||
| 936 | + | ||
| 937 | + .. versionadded:: 2.0.0 | ||
| 934 | 938 | ${ARRAY_FUNCTION_LIKE} | |
| 935 | 939 | ||
| 936 | 940 | .. versionadded:: 1.20.0 | |
@@ -1184,7 +1188,7 @@ | |||
| 1184 | 1188 | ||
| 1185 | 1189 | add_newdoc('numpy._core.multiarray', 'empty', | |
| 1186 | 1190 | """ | |
| 1187 | - empty(shape, dtype=float, order='C', *, like=None) | ||
| 1191 | + empty(shape, dtype=float, order='C', *, device=None, like=None) | ||
| 1188 | 1192 | ||
| 1189 | 1193 | Return a new array of given shape and type, without initializing entries. | |
| 1190 | 1194 | ||
@@ -1199,6 +1203,10 @@ | |||
| 1199 | 1203 | Whether to store multi-dimensional data in row-major | |
| 1200 | 1204 | (C-style) or column-major (Fortran-style) order in | |
| 1201 | 1205 | memory. | |
| 1206 | + device : str, optional | ||
| 1207 | + The device on which to place the created array. Default: None. | ||
| 1208 | + | ||
| 1209 | + .. versionadded:: 2.0.0 | ||
| 1202 | 1210 | ${ARRAY_FUNCTION_LIKE} | |
| 1203 | 1211 | ||
| 1204 | 1212 | .. versionadded:: 1.20.0 | |
@@ -1676,7 +1684,7 @@ | |||
| 1676 | 1684 | ||
| 1677 | 1685 | add_newdoc('numpy._core.multiarray', 'arange', | |
| 1678 | 1686 | """ | |
| 1679 | - arange([start,] stop[, step,], dtype=None, *, like=None) | ||
| 1687 | + arange([start,] stop[, step,], dtype=None, *, device=None, like=None) | ||
| 1680 | 1688 | ||
| 1681 | 1689 | Return evenly spaced values within a given interval. | |
| 1682 | 1690 | ||
@@ -1717,6 +1725,10 @@ | |||
| 1717 | 1725 | dtype : dtype, optional | |
| 1718 | 1726 | The type of the output array. If `dtype` is not given, infer the data | |
| 1719 | 1727 | type from the other input arguments. | |
| 1728 | + device : str, optional | ||
| 1729 | + The device on which to place the created array. Default: None. | ||
| 1730 | + | ||
| 1731 | + .. versionadded:: 2.0.0 | ||
| 1720 | 1732 | ${ARRAY_FUNCTION_LIKE} | |
| 1721 | 1733 | ||
| 1722 | 1734 | .. versionadded:: 1.20.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,13 +17,13 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | 19 | def _linspace_dispatcher(start, stop, num=None, endpoint=None, retstep=None, | |
| 20 | - dtype=None, axis=None): | ||
| 20 | + dtype=None, axis=None, *, device=None): | ||
| 21 | 21 | return (start, stop) | |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | 24 | @array_function_dispatch(_linspace_dispatcher) | |
| 25 | 25 | def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, | |
| 26 | - axis=0): | ||
| 26 | + axis=0, *, device=None): | ||
| 27 | 27 | """ | |
| 28 | 28 | Return evenly spaced numbers over a specified interval. | |
| 29 | 29 | ||
@@ -64,13 +64,16 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, | |||
| 64 | 64 | array of integers. | |
| 65 | 65 | ||
| 66 | 66 | .. versionadded:: 1.9.0 | |
| 67 | - | ||
| 68 | 67 | axis : int, optional | |
| 69 | 68 | The axis in the result to store the samples. Relevant only if start | |
| 70 | 69 | or stop are array-like. By default (0), the samples will be along a | |
| 71 | 70 | new axis inserted at the beginning. Use -1 to get an axis at the end. | |
| 72 | 71 | ||
| 73 | 72 | .. versionadded:: 1.16.0 | |
| 73 | + device : str, optional | ||
| 74 | + The device on which to place the created array. Default: None. | ||
| 75 | + | ||
| 76 | + .. versionadded:: 2.0.0 | ||
| 74 | 77 | ||
| 75 | 78 | Returns | |
| 76 | 79 | ------- | |
@@ -119,6 +122,11 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, | |||
| 119 | 122 | >>> plt.show() | |
| 120 | 123 | ||
| 121 | 124 | """ | |
| 125 | + if device not in ["cpu", None]: | ||
| 126 | + raise ValueError( | ||
| 127 | + f"Unsupported device: {device}. Only \"cpu\" is allowed." | ||
| 128 | + ) | ||
| 129 | + | ||
| 122 | 130 | num = operator.index(num) | |
| 123 | 131 | if num < 0: | |
| 124 | 132 | raise ValueError( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,8 @@ def linspace( | |||
| 28 | 28 | retstep: L[False] = ..., | |
| 29 | 29 | dtype: None = ..., | |
| 30 | 30 | axis: SupportsIndex = ..., | |
| 31 | + *, | ||
| 32 | + device: None | L["cpu"] = ..., | ||
| 31 | 33 | ) -> NDArray[floating[Any]]: ... | |
| 32 | 34 | @overload | |
| 33 | 35 | def linspace( | |
@@ -38,6 +40,8 @@ def linspace( | |||
| 38 | 40 | retstep: L[False] = ..., | |
| 39 | 41 | dtype: None = ..., | |
| 40 | 42 | axis: SupportsIndex = ..., | |
| 43 | + *, | ||
| 44 | + device: None | L["cpu"] = ..., | ||
| 41 | 45 | ) -> NDArray[complexfloating[Any, Any]]: ... | |
| 42 | 46 | @overload | |
| 43 | 47 | def linspace( | |
@@ -48,6 +52,8 @@ def linspace( | |||
| 48 | 52 | retstep: L[False] = ..., | |
| 49 | 53 | dtype: _DTypeLike[_SCT] = ..., | |
| 50 | 54 | axis: SupportsIndex = ..., | |
| 55 | + *, | ||
| 56 | + device: None | L["cpu"] = ..., | ||
| 51 | 57 | ) -> NDArray[_SCT]: ... | |
| 52 | 58 | @overload | |
| 53 | 59 | def linspace( | |
@@ -58,6 +64,8 @@ def linspace( | |||
| 58 | 64 | retstep: L[False] = ..., | |
| 59 | 65 | dtype: DTypeLike = ..., | |
| 60 | 66 | axis: SupportsIndex = ..., | |
| 67 | + *, | ||
| 68 | + device: None | L["cpu"] = ..., | ||
| 61 | 69 | ) -> NDArray[Any]: ... | |
| 62 | 70 | @overload | |
| 63 | 71 | def linspace( | |
@@ -68,6 +76,8 @@ def linspace( | |||
| 68 | 76 | retstep: L[True] = ..., | |
| 69 | 77 | dtype: None = ..., | |
| 70 | 78 | axis: SupportsIndex = ..., | |
| 79 | + *, | ||
| 80 | + device: None | L["cpu"] = ..., | ||
| 71 | 81 | ) -> tuple[NDArray[floating[Any]], floating[Any]]: ... | |
| 72 | 82 | @overload | |
| 73 | 83 | def linspace( | |
@@ -78,6 +88,8 @@ def linspace( | |||
| 78 | 88 | retstep: L[True] = ..., | |
| 79 | 89 | dtype: None = ..., | |
| 80 | 90 | axis: SupportsIndex = ..., | |
| 91 | + *, | ||
| 92 | + device: None | L["cpu"] = ..., | ||
| 81 | 93 | ) -> tuple[NDArray[complexfloating[Any, Any]], complexfloating[Any, Any]]: ... | |
| 82 | 94 | @overload | |
| 83 | 95 | def linspace( | |
@@ -88,6 +100,8 @@ def linspace( | |||
| 88 | 100 | retstep: L[True] = ..., | |
| 89 | 101 | dtype: _DTypeLike[_SCT] = ..., | |
| 90 | 102 | axis: SupportsIndex = ..., | |
| 103 | + *, | ||
| 104 | + device: None | L["cpu"] = ..., | ||
| 91 | 105 | ) -> tuple[NDArray[_SCT], _SCT]: ... | |
| 92 | 106 | @overload | |
| 93 | 107 | def linspace( | |
@@ -98,6 +112,8 @@ def linspace( | |||
| 98 | 112 | retstep: L[True] = ..., | |
| 99 | 113 | dtype: DTypeLike = ..., | |
| 100 | 114 | axis: SupportsIndex = ..., | |
| 115 | + *, | ||
| 116 | + device: None | L["cpu"] = ..., | ||
| 101 | 117 | ) -> tuple[NDArray[Any], Any]: ... | |
| 102 | 118 | ||
| 103 | 119 | @overload | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -300,6 +300,11 @@ typedef enum { | |||
| 300 | 300 | NPY_BUSDAY_RAISE | |
| 301 | 301 | } NPY_BUSDAY_ROLL; | |
| 302 | 302 | ||
| 303 | + /* Device enum for Array API compatibility */ | ||
| 304 | + typedef enum { | ||
| 305 | + NPY_DEVICE_CPU = 0, | ||
| 306 | + } NPY_DEVICE; | ||
| 307 | + | ||
| 303 | 308 | /************************************************************ | |
| 304 | 309 | * NumPy Auxiliary Data for inner loops, sort functions, etc. | |
| 305 | 310 | ************************************************************/ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,9 +81,12 @@ | |||
| 81 | 81 | ||
| 82 | 82 | ||
| 83 | 83 | @array_function_from_c_func_and_dispatcher(_multiarray_umath.empty_like) | |
| 84 | - def empty_like(prototype, dtype=None, order=None, subok=None, shape=None): | ||
| 84 | + def empty_like( | ||
| 85 | + prototype, dtype=None, order=None, subok=None, shape=None, *, device=None | ||
| 86 | + ): | ||
| 85 | 87 | """ | |
| 86 | - empty_like(prototype, dtype=None, order='K', subok=True, shape=None) | ||
| 88 | + empty_like(prototype, dtype=None, order='K', subok=True, shape=None, | ||
| 89 | + shape=None, *, device=None) | ||
| 87 | 90 | ||
| 88 | 91 | Return a new array with the same shape and type as a given array. | |
| 89 | 92 | ||
@@ -113,6 +116,10 @@ def empty_like(prototype, dtype=None, order=None, subok=None, shape=None): | |||
| 113 | 116 | order='C' is implied. | |
| 114 | 117 | ||
| 115 | 118 | .. versionadded:: 1.17.0 | |
| 119 | + device : str, optional | ||
| 120 | + The device on which to place the created array. Default: None. | ||
| 121 | + | ||
| 122 | + .. versionadded:: 2.0.0 | ||
| 116 | 123 | ||
| 117 | 124 | Returns | |
| 118 | 125 | ------- | |
| Back | FazBrowse Home | New Git URL |
0 commit comments