Skip to content

Commit 4eee527

Browse files
authored
Merge pull request #389 from NaderAlAwar/replace-stream-attribute-with-method
Change `__cuda_stream__` from attribute to method
2 parents ac5b4f0 + 777b106 commit 4eee527

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

cuda_core/cuda/core/experimental/_stream.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
import os
8+
import warnings
89
import weakref
910
from dataclasses import dataclass
1011
from typing import TYPE_CHECKING, Optional, Tuple, Union
@@ -87,9 +88,19 @@ def _init(obj=None, *, options: Optional[StreamOptions] = None):
8788
if obj is not None and options is not None:
8889
raise ValueError("obj and options cannot be both specified")
8990
if obj is not None:
90-
if not hasattr(obj, "__cuda_stream__"):
91-
raise ValueError
92-
info = obj.__cuda_stream__
91+
try:
92+
info = obj.__cuda_stream__()
93+
except AttributeError as e:
94+
raise TypeError(f"{type(obj)} object does not have a '__cuda_stream__' method") from e
95+
except TypeError:
96+
info = obj.__cuda_stream__
97+
warnings.simplefilter("once", DeprecationWarning)
98+
warnings.warn(
99+
"Implementing __cuda_stream__ as an attribute is deprecated; it must be implemented as a method",
100+
stacklevel=3,
101+
category=DeprecationWarning,
102+
)
103+
93104
assert info[0] == 0
94105
self._mnff.handle = cuda.CUstream(info[1])
95106
# TODO: check if obj is created under the current context/device
@@ -132,7 +143,6 @@ def close(self):
132143
"""
133144
self._mnff.close()
134145

135-
@property
136146
def __cuda_stream__(self) -> Tuple[int, int]:
137147
"""Return an instance of a __cuda_stream__ protocol."""
138148
return (0, self.handle)
@@ -279,7 +289,6 @@ def from_handle(handle: int) -> Stream:
279289
"""
280290

281291
class _stream_holder:
282-
@property
283292
def __cuda_stream__(self):
284293
return (0, handle)
285294

cuda_core/docs/source/interoperability.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ exposing their own stream types.
3434

3535
To address this issue, we propose the ``__cuda_stream__`` protocol (currently version
3636
0) as follows: For any Python objects that are meant to be interpreted as a stream, they
37-
should add a ``__cuda_stream__`` attribute that returns a 2-tuple: The version number
37+
should add a ``__cuda_stream__`` method that returns a 2-tuple: The version number
3838
(``0``) and the address of ``cudaStream_t`` (both as Python `int`):
3939

4040
.. code-block:: python
4141
4242
class MyStream:
4343
44-
@property
4544
def __cuda_stream__(self):
4645
return (0, self.ptr)
4746

cuda_core/docs/source/release.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
maxdepth: 3
66
---
77
8+
0.2.0 <release/0.2.0-notes>
89
0.1.1 <release/0.1.1-notes>
910
0.1.0 <release/0.1.0-notes>
1011
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``cuda.core`` v0.2.0 Release notes
2+
==================================
3+
4+
Breaking changes
5+
----------------
6+
7+
- Change ``__cuda_stream__`` from attribute to method

cuda_core/examples/simple_multi_gpu_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class StreamAdaptor:
8181
def __init__(self, obj):
8282
self.obj = obj
8383

84-
@property
8584
def __cuda_stream__(self):
8685
# Note: CuPy streams have a .ptr attribute
8786
return (0, self.obj.ptr)

0 commit comments

Comments
 (0)