Skip to content

[API Compatibility] Add cuda_stream to Paddle stream wrapper#79250

Merged
SigureMo merged 1 commit into
PaddlePaddle:developfrom
ShigureNyako:feature/torch-current-stream-cuda-stream
Jun 5, 2026
Merged

[API Compatibility] Add cuda_stream to Paddle stream wrapper#79250
SigureMo merged 1 commit into
PaddlePaddle:developfrom
ShigureNyako:feature/torch-current-stream-cuda-stream

Conversation

@ShigureNyako

@ShigureNyako ShigureNyako commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

PR Category

User Experience

PR Types

Improvements

Description

参考 NVIDIA/cudnn-frontend@develop...PFCCLab:cudnn-frontend:paddle 中 Paddle 适配对当前 CUDA stream 的处理:外部库原本使用 PyTorch 形态的 torch.cuda.current_stream().cuda_stream 获取 cudaStream_t,Paddle 适配需要改成 stream_base.raw_stream

本 PR 在 Paddle 的 Stream 包装层补齐最小兼容接口:

  • paddle.device.Stream 增加只读属性 cuda_stream,CUDA stream 返回底层 stream_base.cuda_stream 的整型 handle,可被外部 CUDA/C++/Python 库继续作为当前 stream 使用。
  • 非 CUDA stream 不返回伪造 CUDA handle,而是抛出清晰的 AttributeError,保持设备语义明确。
  • paddle.use_compat_guard() 下,import torch 后的 torch.cuda.current_stream().cuda_stream 可直接走 Paddle 兼容代理,不引入 PyTorch 依赖。

这样外部项目不需要为了 Paddle 每次把 torch.cuda.current_stream().cuda_stream 改写成 Paddle 专用路径,也不混入 nvtx.range 相关改动。

验证:

  • python3 -m py_compile python/paddle/device/__init__.py test/legacy_test/test_cuda_stream_event.py
  • ruff check python/paddle/device/__init__.py test/legacy_test/test_cuda_stream_event.py
  • ruff format --check python/paddle/device/__init__.py test/legacy_test/test_cuda_stream_event.py
  • prek --files python/paddle/device/__init__.py test/legacy_test/test_cuda_stream_event.py
  • commit 前 prek hook 已通过

本地限制:当前源码 checkout 没有构建/安装 libpaddlePYTHONPATH=python import paddle 会失败,因此无法在本机完成 CUDA 单测运行;新增测试已按 CUDA 可用性 guard。

是否引起精度变化

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

自查完成:本 PR 只补齐 paddle.cuda.current_stream().cuda_stream / torch compat 代理路径,未包含 nvtx.range 相关改动;本地已通过 py_compile、ruff、prek(受限于未构建 libpaddle,CUDA 单测无法在本机执行)。@SigureMo 麻烦有空帮忙看一下,谢谢!

SigureMo
SigureMo previously approved these changes Jun 4, 2026

@SigureMo SigureMo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

Comment thread python/paddle/device/__init__.py Outdated
def cuda_stream(self) -> int:
if isinstance(self.stream_base, core.CUDAStream):
return self.stream_base.cuda_stream
raise AttributeError("cuda_stream is only available for CUDA streams")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这行是不是没测?覆盖率过不了吧?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实,这个非 CUDA 分支之前没覆盖。已补一个 CPU-safe 的最小测试:用未初始化的 paddle.device.Stream 配一个非 CUDA stream_base,断言访问 cuda_stream 会抛出这个 AttributeError,避免 coverage 漏掉这行。

@ShigureNyako ShigureNyako force-pushed the feature/torch-current-stream-cuda-stream branch from 11921d9 to 97a57f9 Compare June 4, 2026 15:28
expected_cuda_stream = stream.stream_base.cuda_stream
expected_raw_stream = stream.stream_base.raw_stream

with paddle.use_compat_guard():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果只是测 use_compat_guard 下,没必要,因为在 API 对齐的情况下,这就是天然对齐的,这个 case 清理掉吧

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,已清理这个 use_compat_guard / import torch 的测试,只保留直接 API 对齐测试。

Comment thread python/paddle/device/__init__.py Outdated
Comment on lines +1502 to +1504
if isinstance(self.stream_base, core.CUDAStream):
return self.stream_base.cuda_stream
raise AttributeError("cuda_stream is only available for CUDA streams")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你改成这样

Suggested change
if isinstance(self.stream_base, core.CUDAStream):
return self.stream_base.cuda_stream
raise AttributeError("cuda_stream is only available for CUDA streams")
assert isinstance(self.stream_base, core.CUDAStream), "cuda_stream is only available for CUDA streams"
return self.stream_base.cuda_stream

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,已按建议改成 assert isinstance(self.stream_base, core.CUDAStream) 后直接返回 self.stream_base.cuda_stream

Comment on lines +189 to +196
def test_cuda_stream_non_cuda_stream(self):
stream = object.__new__(paddle.device.Stream)
stream.stream_base = object()

with self.assertRaisesRegex(
AttributeError, "cuda_stream is only available for CUDA streams"
):
_ = stream.cuda_stream

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种畸形的测试不要加吧,没太大意义

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,已移除这个通过 object.__new__ 构造的非正常测试。

Co-authored-by: Codex <noreply@openai.com>
@ShigureNyako ShigureNyako force-pushed the feature/torch-current-stream-cuda-stream branch from 97a57f9 to 775654c Compare June 4, 2026 15:39
@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

2 similar comments
@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@SigureMo SigureMo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

@paddle-bot paddle-bot Bot added the contributor External developers label Jun 4, 2026
@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@8df3d8a). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #79250   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?         4           
  Branches           ?         0           
===========================================
  Hits               ?         4           
  Misses             ?         0           
  Partials           ?         0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

1 similar comment
@ShigureNyako

Copy link
Copy Markdown
Contributor Author

/re-run all-failed

@SigureMo

SigureMo commented Jun 4, 2026

Copy link
Copy Markdown
Member

停停停,先别 rerun 了,得等 #79240 修好了我给你 rerun

@ShigureNyako

Copy link
Copy Markdown
Contributor Author

收到,我这边先不再触发 rerun,等 #79240 修好后由你来 rerun。谢谢!

@SigureMo SigureMo merged commit 148b086 into PaddlePaddle:develop Jun 5, 2026
150 of 156 checks passed
@SigureMo SigureMo deleted the feature/torch-current-stream-cuda-stream branch June 5, 2026 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants