Skip to content

privatize pk.array - #406

Open
nayyirahsan wants to merge 2 commits into
kokkos:mainfrom
nayyirahsan:nayyirahsan/pkArrayPrivate
Open

privatize pk.array#406
nayyirahsan wants to merge 2 commits into
kokkos:mainfrom
nayyirahsan:nayyirahsan/pkArrayPrivate

Conversation

@nayyirahsan

Copy link
Copy Markdown
Contributor

Closes #388.

Renames pk.array to pk._array as the internal conversion function. pk.array is kept temporarily as a compatibility wrapper that emits a DeprecationWarning and forwards to pk._array. User-facing call sites are migrated to pk.asarray, while CuPy/GPU paths use pk._array directly to preserve conversion semantics. Deprecation and compatibility tests are added covering NumPy arrays, lists, scalars, and CuPy (CUDA-gated).

Comment on lines +975 to +992
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)

Copy link
Copy Markdown
Collaborator

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.

Comment on lines +1003 to +1013
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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of it? Why we can't use pk.nan?
And it looks invalid since we can test cupy arrays with np.isnan, which should give errors.

Comment thread tests/test_views.py
Comment on lines +100 to +102
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end goal is not to use pykokkos internal data structures. If we will deprecate array but still will use asarray - that's not correct.
We want to have something like self.np_view = np.array(...) and be able to use it like that.
To be 100% flexible, we can have some intermediate data structure (per-file only) like xp that should decide if we want to use cupy or numpy based on current execution space.
But there should not be xp.asarray(numpy_array). It should be xp.array(...).

You can treat xp as a C++ macros like this:

#ifdef __CUDA_ARCH__
    xp = cupy
#else
    xp = numpy
#endif

Comment thread tests/test_views.py
assert_allclose(from_private, from_public)


@pytest.mark.skipif(not HAS_CUDA, reason="CUDA/cupy not available")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set pk.array as private (i.e., pk._array)

2 participants