Repository navigation
[Core][TPU] Support physical chip mask translation for multi-device TPU chips - #65723
ryanaoleary merged 17 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for multi-core TPUs in Ray by mapping physical TPU chips to logical device IDs. It adds helper functions to determine TPU resources per chip and updates the process-visible accelerator ID configuration to handle logical-to-physical mapping. The review feedback highlights a potential TypeError when sorting mixed-type physical chip IDs, dead code and a normalization bug in TPU type resolution, and suggests adding test coverage for mixed-type sorting to prevent regressions.
95191a5 to
5037376
Compare
…ual-chiplet TPUs Signed-off-by: Ryan O'Leary <[email protected]>
Signed-off-by: Ryan O'Leary <[email protected]>
5037376 to
6cb1920
Compare
|
cc: @andrewsykim for review if you get time, this fixes the privileged container issue with TPU |
…ore TPUs Signed-off-by: Ryan O'Leary <[email protected]>
| # 3) Dual-Device (v7x): 2 discrete chiplets, enumerated as 2 logical XLA devices | ||
| # per chip (4D topology, e.g. "2,2,1,2"). Listed in DUAL_DEVICE_TPU_TYPES. | ||
| SINGLE_CORE_TPU_TYPES = ("v5litepod", "v6e") | ||
| DUAL_DEVICE_TPU_TYPES = ("v7x",) |
There was a problem hiding this comment.
v5p is dual-core per chip but they're fused since v5p follows megacore architecture (https://docs.jax.dev/en/latest/pallas/tpu/pipelining.html#tpus-in-megacore-configuration) like v4, when I list the devices on the TPU node I only see 1 per chip
There was a problem hiding this comment.
I removed DUAL_DEVICE_TPU_TYPES from the PR actually since we shouldn't be auto-detecting dual device types - the behavior needs to be opt/in or it'll break legacy JAX/SPMD behavior. Users specify RAY_TPU_RESOURCE_PER_CHIP and the total TPU resource on the node, and then Slice PG and the accelerator manager ensure the env vars are mapped correctly, etc.
…multi-device TPUs Signed-off-by: Ryan O'Leary <[email protected]>
Signed-off-by: Ryan O'Leary <[email protected]>
Signed-off-by: Ryan O'Leary <[email protected]>
|
|
||
| # Map logical device IDs to physical chip IDs | ||
| physical_chips = { | ||
| TPUAcceleratorManager._map_device_to_chip_id(device_id, resource_per_chip) |
There was a problem hiding this comment.
From AI review:
Problem: In set_current_process_visible_accelerator_ids, logical devices are mapped to physical chips using dev_idx // devices_per_chip:
If Actor A requests TPU: 1 and is assigned logical device 0, 0 // 2 = 0
→
→ sets TPU_VISIBLE_CHIPS="0", TPU_CHIPS_PER_HOST_BOUNDS="1,1,1".
If Actor B requests TPU: 1 and is assigned logical device 1, 1 // 2 = 0
→
→ also sets TPU_VISIBLE_CHIPS="0", TPU_CHIPS_PER_HOST_BOUNDS="1,1,1".
WDYT?
There was a problem hiding this comment.
I don't think this was a valid issue - TPU_VISIBLE_CHIPS is chip granular and it's actually intended for actors/logical devices to share a chip in the tpu7x case where each chip has 2 separate PJRT devices and frameworks like TorchTPU will run a process per device.
7d8ccab
Signed-off-by: Ryan O'Leary <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 1a14ce7. Configure here.
Signed-off-by: Ryan O'Leary <[email protected]>
…ting them Signed-off-by: Ryan O'Leary <[email protected]>
…cess Signed-off-by: Ryan O'Leary <[email protected]>
…PU chips (ray-project#65723) ## Description On GKE, the TPU device plugin explicitly injects `TPU_VISIBLE_CHIPS="0,1,2,3"` for privileged containers (`securityContext.privileged: true`), while leaving it unset for non-privileged containers. On dual-device TPUs (TPU7x), this injected physical chip mask causes Ray to clamp the node to 4 logical resources instead of the 8 detected from hardware (or explicitly set with`--resources='{"TPU": 8}', causing startup validation failures. Previously, `TPUAcceleratorManager` handled `TPU_VISIBLE_CHIPS` without translation: 1. `get_current_process_visible_accelerator_ids()` returned physical chip counts resource accounting to fail when auto-detecting or explicitly requesting all logical devices. 2. `set_current_process_visible_accelerator_ids()` passed logical device IDs directly into `TPU_VISIBLE_CHIPS`, setting out-of-bounds physical chip indices for worker processes. This PR changes: - `get_current_process_visible_accelerator_ids()`: Expands physical chip indices to logical device IDs (e.g. `["0", "1", "2", "3"]` -> `["0", ..., "7"]`). - `set_current_process_visible_accelerator_ids()`: Maps logical device IDs back to physical chip indices (`device_id // resource_per_chip`) before exporting `TPU_VISIBLE_CHIPS` - `get_tpu_resource_per_chip()`: A single validated accessor for `RAY_TPU_RESOURCE_PER_CHIP`, replacing five copies of `int(os.environ.get(...))` in `ray/util/tpu.py` where a non-positive value silently produced zero TPU resources per slice. - `normalize_tpu_accelerator_type()`: A single helper for the canonical `v{gen}` spelling, replacing three inlined variants. This also fixes `is_valid_tpu_accelerator_topology()`, which split `TPU-V6E` on `-` before parsing the generation and so rejected every `TPU-`-prefixed version. Both masking directions are gated on `RAY_TPU_RESOURCE_PER_CHIP` (default `1`), so per-device accounting is opt-in and the default host-level behavior is unchanged. The above change is required to get `TPU_VISIBLE_CHIPS` set correctly for both legacy SPMD behavior (workloads request all TPU chips on a host) and with frameworks that run a process per TPU device (i.e. PyTorch). ## Related issues Contributes to: ray-project#64055 ## Additional information There is some overlap in this PR with: ray-project#65221 in terms of the `RAY_TPU_RESOURCE_PER_CHIP_ENV_VAR` change and getter. --------- Signed-off-by: Ryan O'Leary <[email protected]>
…er-device MPMD (#67029) ## Description `TPUAcceleratorManager.set_current_process_visible_accelerator_ids` currently collapses assigned TPU IDs by `RAY_TPU_RESOURCE_PER_CHIP` and exports only `TPU_VISIBLE_CHIPS`. While `TPU_VISIBLE_CHIPS` is the physical chip mask used by JAX/PJRT (libtpu), PyTorch/TorchTPU and downstream RL frameworks such as veRL read `TPU_VISIBLE_DEVICES` for per-device MPMD worker binding and platform detection: - On dual-device TPU chips (Ironwood TPU with `RAY_TPU_RESOURCE_PER_CHIP=2`), Ray schedules one `TPU` resource per logical device, but two 1-device actors scheduled on the same physical chip (`device_ids` `"0"` and `"1"`) both receive `TPU_VISIBLE_CHIPS="0"` and no `TPU_VISIBLE_DEVICES`. - When multiple MPMD worker groups share a host, TorchTPU falls back to `LOCAL_RANK` when `TPU_VISIBLE_DEVICES` is unset, causing workers in different groups to contend for the same `/dev/vfio/<rank>` devices. - Downstream frameworks such as veRL check `"TPU_NAME" in os.environ or "TPU_VISIBLE_DEVICES" in os.environ` to detect TPU workers. This PR updates `TPUAcceleratorManager.set_current_process_visible_accelerator_ids` and the worker task save/restore path: - Export `TPU_VISIBLE_DEVICES` with the uncollapsed logical device IDs assigned to the worker, while keeping `TPU_VISIBLE_CHIPS` set to the collapsed physical chip indices. - Clear `TPU_VISIBLE_DEVICES` alongside `TPU_VISIBLE_CHIPS` on full-node allocations. - Save and restore all environment variables modified by `set_current_process_visible_accelerator_ids` in `ray._private.utils.set_visible_accelerator_ids` so reused workers restore `TPU_VISIBLE_DEVICES` and pre-set container `TPU_CHIPS_PER_HOST_BOUNDS` / `TPU_HOST_BOUNDS` after normal tasks finish. - Respect `RAY_EXPERIMENTAL_NOSET_TPU_VISIBLE_CHIPS=1` (suppressing both `TPU_VISIBLE_CHIPS` and `TPU_VISIBLE_DEVICES`) so framework-managed TPU environment variables remain untouched. Because `libtpu` prioritizes non-empty `TPU_VISIBLE_CHIPS` over `TPU_VISIBLE_DEVICES`, existing JAX sub-host and SPMD behavior is unchanged. ## Related issues - Contributes to #64055 - Related PRs: - #65723 - #66400 - verl-project/verl-hardware-plugin#18 - verl-project/verl#7849 ## Additional information - Ran unit and integration tests in `python/ray/tests/accelerators/test_tpu.py` (113 passed), `python/ray/tests/test_tpu.py` (186 passed), and `python/ray/train/tests/test_torch_device_manager.py`. - Manually verified on GKE TPU v6e (`2x4`) and TPU v7x (`2x2x2`) clusters across `RAY_TPU_RESOURCE_PER_CHIP=1` and `2`, colocated MPMD worker groups with overlapping `LOCAL_RANK`s, multi-host `torch.distributed` collectives, `RAY_EXPERIMENTAL_NOSET_TPU_VISIBLE_CHIPS=1`, and JAX sub-host/multi-host SPMD workloads. Manual testing comment below. --------- Signed-off-by: Ryan O'Leary <[email protected]>

Description
On GKE, the TPU device plugin explicitly injects
TPU_VISIBLE_CHIPS="0,1,2,3"for privileged containers (securityContext.privileged: true), while leaving it unset for non-privileged containers. On dual-device TPUs (TPU7x), this injected physical chip mask causes Ray to clamp the node to 4 logical resources instead of the 8 detected from hardware (or explicitly set with`--resources='{"TPU": 8}', causing startup validation failures.Previously,
TPUAcceleratorManagerhandledTPU_VISIBLE_CHIPSwithout translation:get_current_process_visible_accelerator_ids()returned physical chip counts resource accounting to fail when auto-detecting or explicitly requesting all logical devices.set_current_process_visible_accelerator_ids()passed logical device IDs directly intoTPU_VISIBLE_CHIPS, setting out-of-bounds physical chip indices for worker processes.This PR changes:
get_current_process_visible_accelerator_ids(): Expands physical chip indices to logicaldevice IDs (e.g.
["0", "1", "2", "3"]->["0", ..., "7"]).set_current_process_visible_accelerator_ids(): Maps logical device IDs back to physicalchip indices (
device_id // resource_per_chip) before exportingTPU_VISIBLE_CHIPSget_tpu_resource_per_chip(): A single validated accessor forRAY_TPU_RESOURCE_PER_CHIP,replacing five copies of
int(os.environ.get(...))inray/util/tpu.pywhere anon-positive value silently produced zero TPU resources per slice.
normalize_tpu_accelerator_type(): A single helper for the canonicalv{gen}spelling,replacing three inlined variants. This also fixes
is_valid_tpu_accelerator_topology(),which split
TPU-V6Eon-before parsing the generation and so rejected everyTPU--prefixed version.Both masking directions are gated on
RAY_TPU_RESOURCE_PER_CHIP(default1), soper-device accounting is opt-in and the default host-level behavior is unchanged.
The above change is required to get
TPU_VISIBLE_CHIPSset correctly for both legacy SPMD behavior (workloads request all TPU chips on a host) and with frameworks that run a process per TPU device (i.e. PyTorch).Related issues
Contributes to: #64055
Additional information
There is some overlap in this PR with: #65221 in terms of the
RAY_TPU_RESOURCE_PER_CHIP_ENV_VARchange and getter.