Skip to content

Commit 05ab762

Browse files
authored
Use cudaStream_t instead of cuda_stream_view in pylibcudf Cython (#22368)
Contributes to rapidsai/rmm#2359 Authors: - Vyas Ramasubramani (https://github.com/vyasr) Approvers: - Bradley Dice (https://github.com/bdice) URL: #22368
1 parent 41be396 commit 05ab762

357 files changed

Lines changed: 3470 additions & 2967 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

python/cudf_polars/cudf_polars/utils/cuda_stream.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
if TYPE_CHECKING:
1414
from collections.abc import Callable, Sequence
1515

16+
from pylibcudf.utils import CudaStreamLike
1617
from rmm.pylibrmm.stream import Stream
1718

1819

@@ -27,7 +28,7 @@ def get_cuda_stream() -> Stream:
2728

2829

2930
def join_cuda_streams(
30-
*, downstreams: Sequence[Stream], upstreams: Sequence[Stream]
31+
*, downstreams: Sequence[CudaStreamLike], upstreams: Sequence[CudaStreamLike]
3132
) -> None:
3233
"""
3334
Join multiple CUDA streams.
@@ -46,7 +47,7 @@ def join_cuda_streams(
4647

4748

4849
def get_joined_cuda_stream(
49-
get_cuda_stream: Callable[[], Stream], *, upstreams: Sequence[Stream]
50+
get_cuda_stream: Callable[[], Stream], *, upstreams: Sequence[CudaStreamLike]
5051
) -> Stream:
5152
"""
5253
Return a CUDA stream that is joined to the given streams.

python/pylibcudf/pylibcudf/binaryop.pxd

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
22
# SPDX-License-Identifier: Apache-2.0
33

44
from libcpp cimport bool
55
from pylibcudf.libcudf.binaryop cimport binary_operator
66
from rmm.pylibrmm.memory_resource cimport DeviceMemoryResource
7-
from rmm.pylibrmm.stream cimport Stream
87

98
from .column cimport Column
109
from .scalar cimport Scalar
@@ -25,7 +24,7 @@ cpdef Column binary_operation(
2524
RightBinaryOperand rhs,
2625
binary_operator op,
2726
DataType output_type,
28-
Stream stream=*,
27+
object stream = *,
2928
DeviceMemoryResource mr=*,
3029
)
3130

python/pylibcudf/pylibcudf/binaryop.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
22
# SPDX-License-Identifier: Apache-2.0
33

44
from enum import IntEnum
55

66
from rmm.pylibrmm.memory_resource import DeviceMemoryResource
7-
from rmm.pylibrmm.stream import Stream
87

98
from pylibcudf.column import Column
109
from pylibcudf.scalar import Scalar
1110
from pylibcudf.types import DataType
11+
from pylibcudf.utils import CudaStreamLike
1212

1313
class BinaryOperator(IntEnum):
1414
ADD = ...
@@ -52,7 +52,7 @@ def binary_operation(
5252
rhs: Column | Scalar,
5353
op: BinaryOperator,
5454
output_type: DataType,
55-
stream: Stream | None = None,
55+
stream: CudaStreamLike | None = None,
5656
mr: DeviceMemoryResource | None = None,
5757
) -> Column: ...
5858
def is_supported_operation(

python/pylibcudf/pylibcudf/binaryop.pyx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
22
# SPDX-License-Identifier: Apache-2.0
33

44
from cython.operator import dereference
@@ -20,6 +20,7 @@ from .column cimport Column
2020
from .scalar cimport Scalar
2121
from .types cimport DataType
2222
from .utils cimport _get_stream, _get_memory_resource
23+
from cuda.bindings.cyruntime cimport cudaStream_t
2324

2425
__all__ = ["BinaryOperator", "binary_operation", "is_supported_operation"]
2526

@@ -28,7 +29,7 @@ cpdef Column binary_operation(
2829
RightBinaryOperand rhs,
2930
binary_operator op,
3031
DataType output_type,
31-
Stream stream=None,
32+
object stream=None,
3233
DeviceMemoryResource mr=None,
3334
):
3435
"""Perform a binary operation between a column and another column or scalar.
@@ -61,7 +62,8 @@ cpdef Column binary_operation(
6162
The result of the binary operation
6263
"""
6364
cdef unique_ptr[column] result
64-
stream = _get_stream(stream)
65+
cdef Stream _stream = _get_stream(stream)
66+
cdef cudaStream_t _cs = _stream.view().value()
6567
mr = _get_memory_resource(mr)
6668

6769
if LeftBinaryOperand is Column and RightBinaryOperand is Column:
@@ -71,7 +73,7 @@ cpdef Column binary_operation(
7173
rhs.view(),
7274
op,
7375
output_type.c_obj,
74-
stream.view(),
76+
_cs,
7577
mr.get_mr()
7678
)
7779
elif LeftBinaryOperand is Column and RightBinaryOperand is Scalar:
@@ -81,7 +83,7 @@ cpdef Column binary_operation(
8183
dereference(rhs.c_obj),
8284
op,
8385
output_type.c_obj,
84-
stream.view(),
86+
_cs,
8587
mr.get_mr()
8688
)
8789
elif LeftBinaryOperand is Scalar and RightBinaryOperand is Column:
@@ -91,13 +93,13 @@ cpdef Column binary_operation(
9193
rhs.view(),
9294
op,
9395
output_type.c_obj,
94-
stream.view(),
96+
_cs,
9597
mr.get_mr()
9698
)
9799
else:
98100
raise ValueError(f"Invalid arguments {lhs} and {rhs}")
99101

100-
return Column.from_libcudf(move(result), stream, mr)
102+
return Column.from_libcudf(move(result), _stream, mr)
101103

102104

103105
cpdef bool is_supported_operation(

python/pylibcudf/pylibcudf/column.pxd

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ from libcpp.vector cimport vector
66
from libc.stdint cimport uint64_t
77

88
from rmm.librmm.device_buffer cimport device_buffer
9-
from rmm.pylibrmm.stream cimport Stream
109
from rmm.pylibrmm.memory_resource cimport DeviceMemoryResource
1110
from pylibcudf.libcudf.column.column cimport column
1211
from pylibcudf.libcudf.column.column_view cimport (
@@ -27,7 +26,7 @@ cdef class OwnerWithCAI:
2726
cdef dict cai
2827

2928
@staticmethod
30-
cdef create(column_view cv, object owner, Stream stream)
29+
cdef create(column_view cv, object owner, object stream)
3130

3231

3332
cdef class OwnerMaskWithCAI:
@@ -38,7 +37,7 @@ cdef class OwnerMaskWithCAI:
3837
cdef create(column_view cv, object owner)
3938

4039

41-
cdef gpumemoryview _copy_array_to_device(object buf, Stream stream=*)
40+
cdef gpumemoryview _copy_array_to_device(object buf, object stream=*)
4241

4342

4443
cdef class Column:
@@ -61,7 +60,7 @@ cdef class Column:
6160
@staticmethod
6261
cdef Column from_libcudf(
6362
unique_ptr[column] libcudf_col,
64-
Stream stream,
63+
object stream,
6564
DeviceMemoryResource mr
6665
)
6766

@@ -72,7 +71,7 @@ cdef class Column:
7271
cdef Column from_column_view_of_arbitrary(
7372
const column_view& cv,
7473
object owner,
75-
Stream stream,
74+
object stream,
7675
)
7776

7877
@staticmethod
@@ -81,10 +80,10 @@ cdef class Column:
8180
tuple shape,
8281
DataType dtype,
8382
Column base=*,
84-
Stream stream=*,
83+
object stream=*,
8584
)
8685

87-
cpdef Scalar to_scalar(self, Stream stream=*, DeviceMemoryResource mr=*)
86+
cpdef Scalar to_scalar(self, object stream=*, DeviceMemoryResource mr=*)
8887
cpdef DataType type(self)
8988
cpdef Column child(self, size_type index)
9089
cpdef size_type num_children(self)
@@ -95,7 +94,7 @@ cdef class Column:
9594
cpdef object data(self)
9695
cpdef object null_mask(self)
9796
cpdef list children(self)
98-
cpdef Column copy(self, Stream stream=*, DeviceMemoryResource mr=*)
97+
cpdef Column copy(self, object stream=*, DeviceMemoryResource mr=*)
9998
cpdef uint64_t device_buffer_size(self)
10099
cpdef Column with_mask(self, object, size_type, bint validate=*)
101100

@@ -108,10 +107,10 @@ cdef class ListsColumnView:
108107
cpdef child(self)
109108
cpdef offsets(self)
110109
cdef lists_column_view view(self) nogil
111-
cpdef Column get_sliced_child(self, Stream stream=*)
110+
cpdef Column get_sliced_child(self, object stream=*)
112111

113112

114113
cdef class StructsColumnView:
115114
cdef Column _column
116115
cdef structs_column_view view(self) nogil
117-
cpdef Column get_sliced_child(self, int index, Stream stream=*)
116+
cpdef Column get_sliced_child(self, int index, object stream=*)

python/pylibcudf/pylibcudf/column.pyi

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ from typing import Any, Protocol, TypedDict
66

77
from rmm.pylibrmm.device_buffer import DeviceBuffer
88
from rmm.pylibrmm.memory_resource import DeviceMemoryResource
9-
from rmm.pylibrmm.stream import Stream
109

1110
from pylibcudf._interop_helpers import ArrowLike, ColumnMetadata
1211
from pylibcudf.scalar import Scalar
1312
from pylibcudf.span import Span
1413
from pylibcudf.types import DataType
14+
from pylibcudf.utils import CudaStreamLike
1515

1616
class ArrayInterfaceBase(TypedDict):
1717
shape: tuple[int, ...]
@@ -64,7 +64,7 @@ class Column:
6464
def num_children(self) -> int: ...
6565
def copy(
6666
self,
67-
stream: Stream | None = None,
67+
stream: CudaStreamLike | None = None,
6868
mr: DeviceMemoryResource | None = None,
6969
) -> Column: ...
7070
def device_buffer_size(self) -> int: ...
@@ -77,19 +77,19 @@ class Column:
7777
def from_scalar(
7878
scalar: Scalar,
7979
size: int,
80-
stream: Stream | None = None,
80+
stream: CudaStreamLike | None = None,
8181
mr: DeviceMemoryResource | None = None,
8282
) -> Column: ...
8383
def to_scalar(
8484
self,
85-
stream: Stream | None = None,
85+
stream: CudaStreamLike | None = None,
8686
mr: DeviceMemoryResource | None = None,
8787
) -> Scalar: ...
8888
@staticmethod
8989
def all_null_like(
9090
like: Column,
9191
size: int,
92-
stream: Stream | None = None,
92+
stream: CudaStreamLike | None = None,
9393
mr: DeviceMemoryResource | None = None,
9494
) -> Column: ...
9595
@staticmethod
@@ -99,54 +99,58 @@ class Column:
9999
def to_arrow(
100100
self,
101101
metadata: ColumnMetadata | str | None = None,
102-
stream: Stream | None = None,
102+
stream: CudaStreamLike | None = None,
103103
) -> ArrowLike: ...
104104
# Private methods below are included because polars is currently using them,
105105
# but we want to remove stubs for these private methods eventually
106106
def _to_schema(self, metadata: Any = None) -> Any: ...
107-
def _to_host_array(self, stream: Stream) -> Any: ...
107+
def _to_host_array(self, stream: CudaStreamLike) -> Any: ...
108108
@staticmethod
109109
def from_arrow(
110110
obj: ArrowLike,
111111
dtype: DataType | None = None,
112-
stream: Stream | None = None,
112+
stream: CudaStreamLike | None = None,
113113
mr: DeviceMemoryResource | None = None,
114114
) -> Column: ...
115115
@classmethod
116116
def from_cuda_array_interface(
117-
cls, obj: SupportsCudaArrayInterface, stream: Stream | None = None
117+
cls,
118+
obj: SupportsCudaArrayInterface,
119+
stream: CudaStreamLike | None = None,
118120
) -> Column: ...
119121
@classmethod
120122
def from_array_interface(
121-
cls, obj: SupportsArrayInterface, stream: Stream | None = None
123+
cls, obj: SupportsArrayInterface, stream: CudaStreamLike | None = None
122124
) -> Column: ...
123125
@classmethod
124126
def from_array(
125127
cls,
126128
obj: SupportsCudaArrayInterface | SupportsArrayInterface,
127-
stream: Stream | None = None,
129+
stream: CudaStreamLike | None = None,
128130
) -> Column: ...
129131
@staticmethod
130132
def struct_from_children(children: Sequence[Column]) -> Column: ...
131133
@staticmethod
132134
def from_iterable_of_py(
133135
obj: Iterable,
134136
dtype: DataType | None = None,
135-
stream: Stream | None = None,
137+
stream: CudaStreamLike | None = None,
136138
) -> Column: ...
137139

138140
class ListsColumnView:
139141
def __init__(self, column: Column): ...
140142
def child(self) -> Column: ...
141143
def offsets(self) -> Column: ...
142-
def get_sliced_child(self, stream: Stream | None = None) -> Column: ...
144+
def get_sliced_child(
145+
self, stream: CudaStreamLike | None = None
146+
) -> Column: ...
143147

144148
class StructsColumnView:
145149
def __init__(self, column: Column): ...
146150
def child(self) -> Column: ...
147151
def offsets(self) -> Column: ...
148152
def get_sliced_child(
149-
self, index: int, stream: Stream | None = None
153+
self, index: int, stream: CudaStreamLike | None = None
150154
) -> Column: ...
151155

152156
def is_c_contiguous(

0 commit comments

Comments
 (0)