-
Notifications
You must be signed in to change notification settings - Fork 26
privatize pk.array #406
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
base: main
Are you sure you want to change the base?
privatize pk.array #406
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| from enum import Enum | ||
| import os | ||
| import sys | ||
| import warnings | ||
| from types import ModuleType | ||
| from typing import Dict, Generic, Iterator, List, Optional, Tuple, TypeVar, Union | ||
|
|
||
|
|
@@ -945,7 +946,7 @@ def is_array(array) -> bool: | |
| return True | ||
|
|
||
|
|
||
| def array( | ||
| def _array( | ||
| array, space: Optional[MemorySpace] = None, layout: Optional[Layout] = None | ||
| ) -> ViewType: | ||
| """ | ||
|
|
@@ -958,8 +959,11 @@ def array( | |
| """ | ||
|
|
||
| # if numpy array, use from_numpy() | ||
| if isinstance(array, np.ndarray) or np.isscalar(array): | ||
| if isinstance(array, np.ndarray): | ||
| return from_numpy(array, space, layout) | ||
| # Python scalars do not expose .dtype, so normalize first | ||
| if np.isscalar(array): | ||
| return from_numpy(np.asarray(array), space, layout) | ||
| # test if the input array can duck-type to a numpy-like array | ||
| # and run from_array to preprocess the array to numpy | ||
| if is_array(array): | ||
|
|
@@ -968,6 +972,25 @@ def array( | |
| return from_numpy(np.asarray(array), space, layout) | ||
|
|
||
|
|
||
| def array( | ||
| array, space: Optional[MemorySpace] = None, layout: Optional[Layout] = None | ||
| ) -> ViewType: | ||
| """ | ||
| Deprecated public compatibility shim for internal array conversion. | ||
|
|
||
| Prefer `pk.asarray` for user-level conversions. | ||
| """ | ||
|
|
||
| warnings.warn( | ||
| "pk.array is deprecated and will be removed in a future release. " | ||
| "Use pk.asarray for user conversions; pk._array is internal/private.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
|
|
||
| return _array(array, space, layout) | ||
|
|
||
|
|
||
| # asarray is required for comformance with the array API: | ||
| # https://data-apis.org/array-api/2021.12/API_specification/creation_functions.html#objects-in-api | ||
|
|
||
|
|
@@ -977,7 +1000,17 @@ def asarray(obj, /, *, dtype=None, device=None, copy=None): | |
| # for now, let's cheat and use NumPy asarray() followed | ||
| # by pykokkos from_numpy() | ||
|
|
||
| if not isinstance(obj, list) and obj in {pk.e, pk.pi, pk.inf, pk.nan}: | ||
| is_array_api_constant = False | ||
| if np.isscalar(obj): | ||
| if obj in (pk.e, pk.pi, pk.inf): | ||
| is_array_api_constant = True | ||
| else: | ||
| try: | ||
| is_array_api_constant = bool(np.isnan(obj)) | ||
| except TypeError: | ||
| is_array_api_constant = False | ||
|
|
||
| if is_array_api_constant: | ||
|
Comment on lines
+1003
to
+1013
Collaborator
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. What's the point of it? Why we can't use |
||
| if dtype is None: | ||
| dtype = pk.float64 | ||
| view = pk.View([1], dtype=dtype) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,9 +97,9 @@ def __init__(self, threads: int, i_1: int, i_2: int, i_3: int, i_4: int): | |
| cp_arr = cp.zeros((threads, 2)).astype(np.int32) | ||
| list_arr = [np.array([0, 0], dtype=np.int32)] * threads | ||
|
|
||
| self.np_view: pk.View2D[int] = pk.array(np_arr) | ||
| self.cp_view: pk.View2D[int] = pk.array(cp_arr) | ||
| self.list_view: pk.View2D[int] = pk.array(list_arr) | ||
| self.np_view: pk.View2D[int] = pk.asarray(np_arr) | ||
| self.cp_view: pk.View2D[int] = pk._array(cp_arr) | ||
| self.list_view: pk.View2D[int] = pk.asarray(list_arr) | ||
|
Comment on lines
+100
to
+102
Collaborator
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 end goal is not to use You can treat #ifdef __CUDA_ARCH__
xp = cupy
#else
xp = numpy
#endif |
||
|
|
||
| @pk.workunit | ||
| def v1d(self, tid: int) -> None: | ||
|
|
@@ -283,9 +283,9 @@ def test_arrays(self): | |
| cp_arr = cp.zeros((self.threads, 2)).astype(np.int32) | ||
| list_arr = [np.array([0, 0], dtype=np.int32)] * self.threads | ||
|
|
||
| np_view = pk.array(np_arr) | ||
| cp_view = pk.array(cp_arr) | ||
| list_view = pk.array(list_arr) | ||
| np_view = pk.asarray(np_arr) | ||
| cp_view = pk._array(cp_arr) | ||
| list_view = pk.asarray(list_arr) | ||
|
|
||
| pk.parallel_for( | ||
| pk.RangePolicy(pk.OpenMP, 0, self.threads), addition_np, np_arr=np_view | ||
|
|
@@ -376,6 +376,41 @@ def test_asarray_consts_vs_numpy(const, np_dtype, pk_dtype): | |
| assert not "int" in pk_type_string | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "arr", | ||
| [ | ||
| np.array([1, 2, 3], dtype=np.int32), | ||
| [1, 2, 3], | ||
| 7, | ||
| ], | ||
| ) | ||
| def test_array_deprecated_alias_still_converts(arr): | ||
| with pytest.warns(DeprecationWarning, match="pk.array is deprecated"): | ||
| view = pk.array(arr) | ||
| assert_allclose(view, np.asarray(arr)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "arr", | ||
| [ | ||
| np.array([3, 1, 4], dtype=np.int32), | ||
| [3, 1, 4], | ||
| ], | ||
| ) | ||
| def test_private_array_conversion_matches_asarray(arr): | ||
| from_private = pk._array(arr) | ||
| from_public = pk.asarray(arr) | ||
| assert type(from_private) is type(from_public) | ||
| assert_allclose(from_private, from_public) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not HAS_CUDA, reason="CUDA/cupy not available") | ||
|
Collaborator
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. This one is cool. We should add more of this + more CUDA test. |
||
| def test_private_array_conversion_accepts_cupy_array(): | ||
| cp_arr = cp.array([3, 1, 4], dtype=cp.int32) | ||
| from_private = pk._array(cp_arr) | ||
| assert_allclose(from_private, cp.asnumpy(cp_arr)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "pk_dtype, np_dtype", | ||
| [ | ||
|
|
@@ -416,7 +451,7 @@ def test_result_type_supported(pk_dtype, pk_dtype2, expected_promo): | |
| @pytest.mark.parametrize( | ||
| "pk_dtype, pk_dtype2", | ||
| [ | ||
| (pk.array(np.array([0])), pk.uint16), | ||
| (pk.asarray(np.array([0])), pk.uint16), | ||
| (pk.uint64, pk.int8), | ||
| (pk.float32, pk.int64), | ||
| ], | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove it completely.
No need to give warnings.