Skip to content

[Train][TPU] Enable stream support in TPUTorchDeviceManager - #66822

Merged
ryanaoleary merged 1 commit into
ray-project:masterfrom
chiayi:tpu-device-manager-streams
Oct 30, 2026
Merged

ryanaoleary merged 1 commit into
ray-project:masterfrom
chiayi:tpu-device-manager-streams

Conversation

@chiayi

@chiayi chiayi commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Description

Enable TPU stream support in TPUTorchDeviceManager using torch.tpu so prepare_data_loader(..., auto_transfer=True) can prefetch batches asynchronously onto TPU devices. Also handle record_stream errors on torch_tpu, where aten::record_stream is not supported.

Key Changes

  • Implement supports_stream(), create_stream(), get_stream_context(), and get_current_stream() in TPUTorchDeviceManager using torch.tpu (returning nullcontext() when stream is None).
  • Handle unsupported record_stream errors (NotImplementedError, and RuntimeError on tpu) in _WrappedDataLoader._wait_for_batch().
  • Add unit and live TPU hardware tests in test_tpu_torch_backend.py and register the target in BUILD.bazel.

Related issues

N/A

Test commands run and results

  • Unit & live TPU tests:
    • pytest -v python/ray/train/tests/test_tpu_torch_backend.py -> 4 passed
  • Live GKE TPU cluster verification:
    • Verified on a GKE TPU v5e RayCluster via RayJob with auto_transfer=True and auto_transfer=False.

@chiayi
chiayi force-pushed the tpu-device-manager-streams branch 3 times, most recently from dc4ff55 to 4e1a634 Compare October 7, 2026 21:18
@chiayi
chiayi marked this pull request as ready for review October 7, 2026 21:20
@chiayi
chiayi requested a review from a team as a code owner October 7, 2026 21:20

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds stream support for TPU devices in TPUTorchDeviceManager and updates _WrappedDataLoader to handle record_stream errors on TPU and other custom backends. It also introduces corresponding unit tests. The review feedback suggests two improvements: first, checking if torch has the tpu attribute in supports_stream to prevent potential AttributeErrors when torch.tpu is unavailable; second, logging ignored RuntimeErrors during record_stream on TPU at a debug level to prevent masking other genuine runtime issues.

@chiayi
chiayi force-pushed the tpu-device-manager-streams branch from 4e1a634 to e699b79 Compare October 7, 2026 21:25

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@chiayi
chiayi force-pushed the tpu-device-manager-streams branch 3 times, most recently from 78b7b44 to ce952f3 Compare October 7, 2026 22:57

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@ray-gardener ray-gardener Bot added train Ray Train Related Issue community-contribution Contributed by the community labels Oct 8, 2026
@chiayi
chiayi force-pushed the tpu-device-manager-streams branch from ce952f3 to b2a160e Compare October 8, 2026 05:54

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit b2a160e. Configure here.

@chiayi
chiayi force-pushed the tpu-device-manager-streams branch 2 times, most recently from 1c64660 to f831e6f Compare October 8, 2026 22:51
@chiayi
chiayi force-pushed the tpu-device-manager-streams branch from f831e6f to b3b46df Compare October 19, 2026 23:00

@ryanaoleary ryanaoleary 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.

LGTM

@chiayi
chiayi force-pushed the tpu-device-manager-streams branch from b3b46df to d497a3b Compare October 29, 2026 20:17
@ryanaoleary
ryanaoleary enabled auto-merge (squash) October 30, 2026 02:23
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Oct 30, 2026
@ryanaoleary
ryanaoleary merged commit bccc465 into ray-project:master Oct 30, 2026
8 checks passed
ryanaoleary pushed a commit that referenced this pull request Nov 10, 2026
…or guide (#67779)

## Description
Adds a `PyTorch: Prefetch batches with device streams` section to
`doc/source/train/user-guides/using-accelerators.md`.

It documents how `ray.train.torch.prepare_data_loader` prefetches
batches with `torch.tpu.Stream` on TPUs and `torch.cuda.Stream` on GPUs
when `auto_transfer=True`.

## Related issues
Doc related to #66822

## Additional information

Signed-off-by: Aaron Liang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community go add ONLY when ready to merge, run all tests train Ray Train Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants