Repository navigation
[HIP] [Bugfix] Accept generic torch.Stream in torch_to_c_types - #7101
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the change adding generic torch.Stream support to HIP argument conversion.
No open issues 🎉
What changed in this PR
This PR updates the HIP argument-conversion helper to accept generic torch.Stream objects, including those returned by torch.accelerator.current_stream(). It isolates the stream fix from the unrelated build and JIT changes in #6260.
Changes:
- Accepts generic streams while preserving existing
torch.cuda.Streamhandling. - Preserves the default stream’s zero handle.
- Adds four regression tests covering CUDA-specific and generic streams, including default streams.
| File | Description |
|---|---|
tests/test_torch_to_c_types.py |
Adds stream-handle conversion tests, skipped when no GPU is available. |
csrc/cpp_itfs/torch_utils.py |
Converts generic stream metadata into a raw HIP stream handle. |
Review effort: Balanced
e50fc20 to
6120e1b
Compare
|
@aiter-bot review |
[ROCm/aiter] PR #7101 — [HIP] [Bugfix] Accept generic torch.Stream in torch_to_c_typestorch_to_c_types, the ctypes argument marshaller used by every csrc/cpp_itfs HIP kernel launcher, now accepts a device-agnostic torch.Stream (e.g. from torch.accelerator.current_stream()) by rebuilding it as a torch.cuda.Stream to recover the raw HIP stream handle, where it previously raised ValueError. Review (advisory): |
|
@yuychang pls remove tests. |
- torch.accelerator.current_stream() returns a torch.Stream, which has no .cuda_stream, so it raised ValueError. - Any torch.Stream is now accepted; a torch.cuda.Stream still passes its .cuda_stream handle. - A torch.Stream without .cuda_stream gets its handle through torch.cuda.Stream(stream_id, device_index, device_type). - The default stream's handle 0 is passed as 0 and is not mistaken for a missing handle. - Split out of ROCm#6260 so that PR changes one kernel backend.
6120e1b to
9bfdb80
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Validating the shared HIP converter’s reconstructed PyTorch stream handles across default streams, side streams, and devices requires a PyTorch/ROCm runtime unavailable in this review environment.
No open issues 🎉
Review effort: Balanced
|
@amd-ruitang3 Done in 9bfdb80: removed |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
|
LGTM |
- torch.accelerator.current_stream() returns a torch.Stream, which has no .cuda_stream, so it raised ValueError. - Any torch.Stream is now accepted; a torch.cuda.Stream still passes its .cuda_stream handle. - A torch.Stream without .cuda_stream gets its handle through torch.cuda.Stream(stream_id, device_index, device_type). - The default stream's handle 0 is passed as 0 and is not mistaken for a missing handle. - Split out of #6260 so that PR changes one kernel backend.
- torch.accelerator.current_stream() returns a torch.Stream, which has no .cuda_stream, so it raised ValueError. - Any torch.Stream is now accepted; a torch.cuda.Stream still passes its .cuda_stream handle. - A torch.Stream without .cuda_stream gets its handle through torch.cuda.Stream(stream_id, device_index, device_type). - The default stream's handle 0 is passed as 0 and is not mistaken for a missing handle. - Split out of ROCm#6260 so that PR changes one kernel backend.
Summary
torch_to_c_typesnow accepts anytorch.Stream, including the generic onetorch.accelerator.current_stream()returns; before, onlytorch.cuda.Streamwas accepted.Motivation
Passing
torch.accelerator.current_stream()to thecsrc/cpp_itfslaunchers raisesValueError: Unsupported type: <class 'torch.Stream'>. Moved here from #6260 because CONTRIBUTE.md asks for one kernel backend per PR; the two changes are independent.Changes
csrc/cpp_itfs/torch_utils.py(only changed file): atorch.cuda.Streamstill passes itscuda_stream; a generictorch.Streamgets its handle viatorch.cuda.Stream(stream_id=..., ...).tests/test_torch_to_c_types.pywas removed in 9bfdb80 at a reviewer's request.Testing
Checked by hand on MI355X (gfx950); see "Validation and unverified scope".
Documentation
Internal argument marshaller; its documented inputs only gain the generic
torch.Stream.Dependencies
Breaking Changes
None;
torch.cuda.Streampasses the same handle as before and other types take the same path.PR Self-Review
Reviewed revision
9bfdb803159ab473d5d1e62bcf005e4431c5e998Behavior and intent
Before, a
torch.Streamthat is not atorch.cuda.Streamfell through toctypes_mapand raised, so device-agnostic code could not pass its current stream to thecsrc/cpp_itfslaunchers. Now a generic stream is rebuilt as atorch.cuda.Streamto get the same raw HIP handle, and ahandle is Nonecheck keeps the default stream's 0.Validation and unverified scope
MI355X (gfx950), ROCm 7.2, torch 2.11.0+rocm7.2; a script compares the marshalled handle with
torch.cuda.current_stream().cuda_streamat 9bfdb80 and onmain:torch_to_c_types(torch.accelerator.current_stream()), default and side stream → same handle (main:ValueError).torch_to_c_types(torch.cuda.current_stream())→ unchanged; a CPUtorch.Stream→RuntimeError;object()→ValueError.gh pr checks 7101→ all passed, including C++ Interface Tests on MI35X and MI300X.PR-specific question
Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
isinstance(arg, torch.Stream)selects the path, after thetorch.Tensorbranch.torch.cuda.Stream(...)accepts only a CUDA/HIP device type, so a non-GPU stream raises instead of yielding a bogus handle; non-stream types still raiseValueError.Changed op tests
None. No file under
op_tests/changes; the only changed file iscsrc/cpp_itfs/torch_utils.py.🤖 Generated with Claude Code