Skip to content
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

Make ColumnBase.__cuda_array_interface__ opt out instead of opt in #15622

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,11 +1101,27 @@ def __arrow_array__(self, type=None):
)

@property
def __cuda_array_interface__(self):
raise NotImplementedError(
f"dtype {self.dtype} is not yet supported via "
"`__cuda_array_interface__`"
)
def __cuda_array_interface__(self) -> abc.Mapping[str, Any]:
output = {
"shape": (len(self),),
"strides": (self.dtype.itemsize,),
"typestr": self.dtype.str,
"data": (self.data_ptr, False),
"version": 1,
}

if self.nullable and self.has_nulls():
# Create a simple Python object that exposes the
# `__cuda_array_interface__` attribute here since we need to modify
# some of the attributes from the numba device array
output["mask"] = cuda_array_interface_wrapper(
ptr=self.mask_ptr,
size=len(self),
owner=self.mask,
readonly=True,
typestr="<t1",
)
return output

def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
return _array_ufunc(self, ufunc, method, inputs, kwargs)
Expand Down
27 changes: 2 additions & 25 deletions python/cudf/cudf/core/column/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import locale
import re
from locale import nl_langinfo
from typing import Any, Mapping, Optional, Sequence, cast
from typing import Any, Optional, Sequence, cast

import numpy as np
import pandas as pd
Expand All @@ -25,7 +25,7 @@
)
from cudf.api.types import is_datetime64_dtype, is_scalar, is_timedelta64_dtype
from cudf.core._compat import PANDAS_GE_220
from cudf.core.buffer import Buffer, cuda_array_interface_wrapper
from cudf.core.buffer import Buffer
from cudf.core.column import ColumnBase, as_column, column, string
from cudf.core.column.timedelta import _unit_to_nanoseconds_conversion
from cudf.utils.dtypes import _get_base_dtype
Expand Down Expand Up @@ -399,29 +399,6 @@ def normalize_binop_value(self, other: DatetimeLikeScalar) -> ScalarLike:

return NotImplemented

@property
def __cuda_array_interface__(self) -> Mapping[str, Any]:
output = {
"shape": (len(self),),
"strides": (self.dtype.itemsize,),
"typestr": self.dtype.str,
"data": (self.data_ptr, False),
"version": 1,
}

if self.nullable and self.has_nulls():
# Create a simple Python object that exposes the
# `__cuda_array_interface__` attribute here since we need to modify
# some of the attributes from the numba device array
output["mask"] = cuda_array_interface_wrapper(
ptr=self.mask_ptr,
size=len(self),
owner=self.mask,
readonly=True,
typestr="<t1",
)
return output

def as_datetime_column(
self, dtype: Dtype, format: str | None = None
) -> DatetimeColumn:
Expand Down
37 changes: 2 additions & 35 deletions python/cudf/cudf/core/column/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@
from __future__ import annotations

import functools
from typing import (
Any,
Callable,
Mapping,
Optional,
Sequence,
Tuple,
Union,
cast,
)
from typing import Any, Callable, Optional, Sequence, Tuple, Union, cast

import cupy as cp
import numpy as np
Expand All @@ -37,7 +28,7 @@
is_integer_dtype,
is_scalar,
)
from cudf.core.buffer import Buffer, cuda_array_interface_wrapper
from cudf.core.buffer import Buffer
from cudf.core.column import (
ColumnBase,
as_column,
Expand Down Expand Up @@ -194,30 +185,6 @@ def __setitem__(self, key: Any, value: Any):
if out:
self._mimic_inplace(out, inplace=True)

@property
def __cuda_array_interface__(self) -> Mapping[str, Any]:
output = {
"shape": (len(self),),
"strides": (self.dtype.itemsize,),
"typestr": self.dtype.str,
"data": (self.data_ptr, False),
"version": 1,
}

if self.nullable and self.has_nulls():
# Create a simple Python object that exposes the
# `__cuda_array_interface__` attribute here since we need to modify
# some of the attributes from the numba device array
output["mask"] = cuda_array_interface_wrapper(
ptr=self.mask_ptr,
size=len(self),
owner=self.mask,
readonly=True,
typestr="<t1",
)

return output

def unary_operator(self, unaryop: Union[str, Callable]) -> ColumnBase:
if callable(unaryop):
return libcudf.transform.transform(self, unaryop)
Expand Down
7 changes: 7 additions & 0 deletions python/cudf/cudf/core/column/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -5600,6 +5600,13 @@ def data_array_view(
) -> cuda.devicearray.DeviceNDArray:
raise ValueError("Cannot get an array view of a StringColumn")

@property
def __cuda_array_interface__(self):
raise NotImplementedError(
f"dtype {self.dtype} is not yet supported via "
"`__cuda_array_interface__`"
)

def to_arrow(self) -> pa.Array:
"""Convert to PyArrow Array
Expand Down
19 changes: 15 additions & 4 deletions python/cudf/cudf/tests/test_cuda_array_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

import cudf
from cudf.core.buffer.spill_manager import get_global_manager
from cudf.testing._utils import DATETIME_TYPES, NUMERIC_TYPES, assert_eq
from cudf.testing._utils import (
DATETIME_TYPES,
NUMERIC_TYPES,
TIMEDELTA_TYPES,
assert_eq,
)


@pytest.mark.parametrize("dtype", NUMERIC_TYPES + DATETIME_TYPES)
Expand Down Expand Up @@ -42,7 +47,9 @@ def test_cuda_array_interface_interop_in(dtype, module):
assert_eq(pd_data, gdf["test"])


@pytest.mark.parametrize("dtype", NUMERIC_TYPES + DATETIME_TYPES + ["str"])
@pytest.mark.parametrize(
"dtype", NUMERIC_TYPES + DATETIME_TYPES + TIMEDELTA_TYPES + ["str"]
)
@pytest.mark.parametrize("module", ["cupy", "numba"])
def test_cuda_array_interface_interop_out(dtype, module):
expectation = does_not_raise()
Expand Down Expand Up @@ -73,7 +80,9 @@ def to_host_function(x):
assert_eq(expect, got)


@pytest.mark.parametrize("dtype", NUMERIC_TYPES + DATETIME_TYPES)
@pytest.mark.parametrize(
"dtype", NUMERIC_TYPES + DATETIME_TYPES + TIMEDELTA_TYPES
)
@pytest.mark.parametrize("module", ["cupy", "numba"])
def test_cuda_array_interface_interop_out_masked(dtype, module):
expectation = does_not_raise()
Expand Down Expand Up @@ -104,7 +113,9 @@ def to_host_function(x):
module_data = module_constructor(cudf_data) # noqa: F841


@pytest.mark.parametrize("dtype", NUMERIC_TYPES + DATETIME_TYPES)
@pytest.mark.parametrize(
"dtype", NUMERIC_TYPES + DATETIME_TYPES + TIMEDELTA_TYPES
)
@pytest.mark.parametrize("nulls", ["all", "some", "bools", "none"])
@pytest.mark.parametrize("mask_type", ["bits", "bools"])
def test_cuda_array_interface_as_column(dtype, nulls, mask_type):
Expand Down
Loading