-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for `len()` and `nbytes` in `pylibcudf.gpumemoryview`. Having those methods is helpful to ensure proper serialization in Dask/Distributed, as utility methods that serialize objects, in this case used by cudf-polars, may use the appropriate method or property to determine the size of the object being transferred. Authors: - Peter Andreas Entschev (https://github.com/pentschev) Approvers: - Matthew Murray (https://github.com/Matt711) - Richard (Rick) Zamora (https://github.com/rjzamora) URL: #18133
- Loading branch information
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright (c) 2025, NVIDIA CORPORATION. | ||
|
||
import itertools | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import rmm | ||
|
||
import pylibcudf as plc | ||
|
||
DTYPES = [ | ||
"u1", | ||
"i2", | ||
"f4", | ||
"f8", | ||
"f16", | ||
] | ||
SIZES = [ | ||
0, | ||
1, | ||
1000, | ||
1024, | ||
10000, | ||
] | ||
|
||
|
||
@pytest.fixture(params=tuple(itertools.product(SIZES, DTYPES)), ids=repr) | ||
def np_array(request): | ||
size, dtype = request.param | ||
return np.empty((size,), dtype=dtype) | ||
|
||
|
||
def test_cuda_array_interface(np_array): | ||
buf = rmm.DeviceBuffer( | ||
ptr=np_array.__array_interface__["data"][0], size=np_array.nbytes | ||
) | ||
gpumemview = plc.gpumemoryview(buf) | ||
|
||
np_array_view = np_array.view("u1") | ||
|
||
ai = np_array_view.__array_interface__ | ||
cai = gpumemview.__cuda_array_interface__ | ||
assert cai["shape"] == ai["shape"] | ||
assert cai["strides"] == ai["strides"] | ||
assert cai["typestr"] == ai["typestr"] | ||
|
||
|
||
def test_len(np_array): | ||
buf = rmm.DeviceBuffer( | ||
ptr=np_array.__array_interface__["data"][0], size=np_array.nbytes | ||
) | ||
gpumemview = plc.gpumemoryview(buf) | ||
|
||
np_array_view = np_array.view("u1") | ||
|
||
assert len(gpumemview) == len(np_array_view) | ||
assert gpumemview.nbytes == np_array.nbytes |