From 775654c85ba3c4c82b477745c26e46d282896b87 Mon Sep 17 00:00:00 2001 From: Nyako Shigure Date: Thu, 4 Jun 2026 22:44:18 +0800 Subject: [PATCH] [API Compatibility] Add CUDA stream handle to Stream wrapper Co-authored-by: Codex --- python/paddle/device/__init__.py | 7 +++++++ test/legacy_test/test_cuda_stream_event.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/python/paddle/device/__init__.py b/python/paddle/device/__init__.py index 9fc1bcb5cb747..4347c99078ef6 100644 --- a/python/paddle/device/__init__.py +++ b/python/paddle/device/__init__.py @@ -1497,6 +1497,13 @@ def _as_parameter_(self): else: return ctypes.c_void_p(self.stream_base.raw_stream) + @property + def cuda_stream(self) -> int: + assert isinstance(self.stream_base, core.CUDAStream), ( + "cuda_stream is only available for CUDA streams" + ) + return self.stream_base.cuda_stream + def __cuda_stream__(self): """ CUDA Stream protocol described at diff --git a/test/legacy_test/test_cuda_stream_event.py b/test/legacy_test/test_cuda_stream_event.py index d57965cf5ab85..21a375bba3217 100644 --- a/test/legacy_test/test_cuda_stream_event.py +++ b/test/legacy_test/test_cuda_stream_event.py @@ -193,6 +193,17 @@ def test_cuda_stream(self): self.assertTrue(type(cuda_stream) is int) ptr = ctypes.c_void_p(cuda_stream) + def test_paddle_cuda_current_stream_cuda_stream(self): + if paddle.is_compiled_with_cuda() and paddle.cuda.is_available(): + stream = paddle.cuda.current_stream() + cuda_stream = stream.cuda_stream + ptr = ctypes.c_void_p(cuda_stream) + + self.assertTrue(type(cuda_stream) is int) + self.assertTrue(cuda_stream >= 0 or ptr.value is not None) + self.assertEqual(cuda_stream, stream.stream_base.cuda_stream) + self.assertEqual(cuda_stream, stream.stream_base.raw_stream) + if __name__ == "__main__": unittest.main()