Skip to content

Commit f33ac1f

Browse files
authored
cuda.core: update PyTorch example to use native __cuda_stream__ (NVIDIA#2305)
* cuda.core: update PyTorch example to use native __cuda_stream__ Signed-off-by: Ralf Juengling <rjuengling@nvidia.com> * Add torch version check --------- Signed-off-by: Ralf Juengling <rjuengling@nvidia.com>
1 parent 7d72ca5 commit f33ac1f

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

cuda_core/examples/pytorch_example.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,26 @@
3232
"""
3333

3434

35-
# Create a wrapper class that implements __cuda_stream__
36-
class PyTorchStreamWrapper:
37-
def __init__(self, pt_stream):
38-
self.pt_stream = pt_stream
39-
40-
def __cuda_stream__(self):
41-
stream_id = self.pt_stream.cuda_stream
42-
return (0, stream_id) # Return format required by CUDA Python
35+
def torch_version() -> tuple[int, int, int]:
36+
v = torch.__version__
37+
v = v.split("+")[0].split(".")
38+
return tuple(int(x) for x in v)
4339

4440

4541
def main():
4642
dev = Device()
4743
dev.set_current()
4844

45+
print(f"PyTorch version: {torch_version()}", file=sys.stderr)
46+
if torch_version() < (2, 10, 0):
47+
# Need support for the __cuda_stream__ method.
48+
print("PyTorch version 2.10 or later required, skipping example", file=sys.stderr)
49+
return
50+
4951
pt_stream = torch.cuda.current_stream()
5052
print(f"PyTorch stream: {pt_stream}", file=sys.stderr)
5153

52-
stream = dev.create_stream(PyTorchStreamWrapper(pt_stream))
54+
stream = dev.create_stream(pt_stream)
5355

5456
try:
5557
# prepare program

0 commit comments

Comments
 (0)