Skip to content

[HIP] [Bugfix] Accept generic torch.Stream in torch_to_c_types - #7101

Merged
amd-ruitang3 merged 1 commit into
ROCm:mainfrom
yuychang:k3/torch-stream-c-types
Oct 30, 2026
Merged

amd-ruitang3 merged 1 commit into
ROCm:mainfrom
yuychang:k3/torch-stream-c-types

Conversation

@yuychang

@yuychang yuychang commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

torch_to_c_types now accepts any torch.Stream, including the generic one torch.accelerator.current_stream() returns; before, only torch.cuda.Stream was accepted.

Motivation

Passing torch.accelerator.current_stream() to the csrc/cpp_itfs launchers raises ValueError: 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): a torch.cuda.Stream still passes its cuda_stream; a generic torch.Stream gets its handle via torch.cuda.Stream(stream_id=..., ...).
  • The default stream's handle 0 is passed as 0, not treated as missing.
  • The regression test tests/test_torch_to_c_types.py was removed in 9bfdb80 at a reviewer's request.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Checked by hand on MI355X (gfx950); see "Validation and unverified scope".

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Internal argument marshaller; its documented inputs only gain the generic torch.Stream.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None; torch.cuda.Stream passes the same handle as before and other types take the same path.

PR Self-Review

Reviewed revision

9bfdb803159ab473d5d1e62bcf005e4431c5e998

Behavior and intent

Before, a torch.Stream that is not a torch.cuda.Stream fell through to ctypes_map and raised, so device-agnostic code could not pass its current stream to the csrc/cpp_itfs launchers. Now a generic stream is rebuilt as a torch.cuda.Stream to get the same raw HIP handle, and a handle is None check 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_stream at 9bfdb80 and on main:

  • 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 CPU torch.Stream → RuntimeError; object() → ValueError.
  • gh pr checks 7101 → all passed, including C++ Interface Tests on MI35X and MI300X.
  • Unverified: no automated test covers the new branch; no kernel launched with a generic stream; device index other than 0; other torch versions; MI300X/MI250X by hand.

PR-specific question

Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?

  • No kernel change and no arch, dtype or shape boundary: only isinstance(arg, torch.Stream) selects the path, after the torch.Tensor branch.
  • 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 raise ValueError.

Changed op tests

None. No file under op_tests/ changes; the only changed file is csrc/cpp_itfs/torch_utils.py.

🤖 Generated with Claude Code

@yuychang
yuychang requested review from a team and a balanced review from Copilot October 28, 2026 16:07
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7101 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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.Stream handling.
  • 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

@amd-ruitang3

Copy link
Copy Markdown
Contributor

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

[ROCm/aiter] PR #7101 — [HIP] [Bugfix] Accept generic torch.Stream in torch_to_c_types

torch_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): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — triage: the PR changes runtime code but ships no validator-runnable test target (its regression test sits in tests/, outside the validator's op_tests target set; the auto-run was also disabled, REVIEW_AUTO_VALIDATE=0). The changed path is executed at run time, but the PR's own tests/test_torch_to_c_types.py (4 tests, 10 assertions, which CI's "Focused regression tests" step runs as python3 -m pytest -q tests on MI35X shard 0) covers it, so the missing validator target is not reported as a defect.
Perf (advisory): NOT RUN — triage: no perf target exists for the changed path (a Python arg-marshalling helper, not a kernel; the PR ships no benchmark entry point), and this box has no torch installation to hand-measure with.

@amd-ruitang3

Copy link
Copy Markdown
Contributor

@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.
@yuychang
yuychang force-pushed the k3/torch-stream-c-types branch from 6120e1b to 9bfdb80 Compare October 29, 2026 07:34
Copilot AI balanced review requested due to automatic review settings October 29, 2026 07:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 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

@yuychang

Copy link
Copy Markdown
Contributor Author

@amd-ruitang3 Done in 9bfdb80: removed tests/test_torch_to_c_types.py, so the PR now only changes csrc/cpp_itfs/torch_utils.py. I re-checked the fix by hand on MI355X (torch 2.11.0+rocm7.2): torch_to_c_types(torch.accelerator.current_stream()) returns the same handle as torch.cuda.current_stream() on the default stream (0) and on a side stream, and torch.cuda.Stream arguments behave as before. The description is updated to match.

@aiter-bot

aiter-bot commented Oct 29, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 9bfdb803159a · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

Before, a `torch.Stream` that is not a `torch.cuda.Stream` fell through to `ctypes_map` and raised, so device-agnostic code could not pass its current stream to the `csrc/cpp_itfs` launchers. Now a generic stream is rebuilt as a `torch.cuda.Stream` to get the same raw HIP handle, and a `handle is None` check 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_stream` at 9bfdb8031 and on `main`:
- `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 CPU `torch.Stream` → `RuntimeError`; `object()` → `ValueError`.
- `gh pr checks 7101` → all passed, including C++ Interface Tests on MI35X and MI300X.
- Unverified: no automated test covers the new branch; no kernel launched with a generic stream; device index other than 0; other torch versions; MI300X/MI250X by hand.

PR-specific question

*Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?*

- No kernel change and no arch, dtype or shape boundary: only `isinstance(arg, torch.Stream)` selects the path, after the `torch.Tensor` branch.
- `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 raise `ValueError`.

Changed op tests

None. No file under `op_tests/` changes; the only changed file is `csrc/cpp_itfs/torch_utils.py`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@amd-ruitang3

Copy link
Copy Markdown
Contributor

LGTM

@amd-ruitang3
amd-ruitang3 merged commit 89a28cd into ROCm:main Oct 30, 2026
61 checks passed
solinzby1 pushed a commit that referenced this pull request Nov 2, 2026
- 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.
shantipriya-amd pushed a commit to shantipriya-amd/aiter that referenced this pull request Nov 18, 2026
- 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants