Skip to content

[jax][tpu] Simplify elastic TPU scaling to use simplier slice counting - #62141

Merged
MengjinYan merged 5 commits into
ray-project:masterfrom
liulehui:tpu-elastic-training
Apr 1, 2026
Merged

MengjinYan merged 5 commits into
ray-project:masterfrom
liulehui:tpu-elastic-training

Conversation

@liulehui

@liulehui liulehui commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

Description

  1. Introduces get_num_tpu_slices in ray.util.tpu, which counts physically intact TPU slices (all hosts alive, correct chip count) without requiring them to be idle. This replaces the use of get_num_ready_tpu_slices during elastic scaling, removing the need to fully shut down the old worker group before querying for available slices.
  2. Simplifies ElasticScalingPolicy by removing the current_num_workers bookkeeping. Previously, the policy had to track running workers and add back "owned" slices to the ready count. With intact-slice counting, this workaround is no longer needed — the formula is simply min(resource_based_slices, intact_slices) * workers_per_slice.
  3. Removes the eager _shutdown_worker_group call from the controller's non-running state handler, since the scaling policy no longer requires workers to be stopped before it can accurately count available slices.

@liulehui
liulehui marked this pull request as ready for review March 27, 2026 17:40
@liulehui
liulehui requested review from a team as code owners March 27, 2026 17:40
@liulehui liulehui added the go add ONLY when ready to merge, run all tests label Mar 27, 2026

@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 introduces a new utility, get_num_intact_tpu_slices, to count physically intact TPU slices regardless of their idle status, which improves the reliability of elastic scaling for TPU workloads. The changes also include refactoring the elastic scaling policy to use this new utility and removing redundant worker group shutdowns in the controller. Feedback includes identifying a duplicate test case in test_tpu.py, suggesting the restoration of a helpful comment regarding impossible TPU configurations, and providing recommendations to improve code clarity and conciseness in the new TPU utility function.

Comment thread python/ray/tests/test_tpu.py Resolved
Comment thread python/ray/util/tpu.py Resolved
Comment thread python/ray/util/tpu.py Resolved
@liulehui
liulehui requested a review from ryanaoleary March 27, 2026 19:08
@ray-gardener ray-gardener Bot added train Ray Train Related Issue core Issues that should be addressed in Ray Core labels Mar 27, 2026

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

Nice this is super clean and thoroughly tested!

Comment thread python/ray/util/tpu.py Resolved
@ryanaoleary

Copy link
Copy Markdown
Contributor

Talked offline about how this change re-introduces the issue of fragmented TPU slices leading to the controller appearing to have the resources to scale when it doesn't. For example if we have 4 slices (each with 4 hosts) allocated to task1, and 2 other slices allocated to task2. If the first 4 slices each have 1 node die, the calculation using this intact logic will be:

num_complete_slices = total_num_workers // workers_per_slice = 4*2/4=2
num_intact_slices  will be the 2 healthy slices of task2.

Thus we'd get num_complete_slices = min(num_complete_slices, num_intact_slices )=2 - which means we'll try to re-size to fit 2 new slices but it should be 0 since the existing, intact slices are scheduled to task 2. We discussed offline that this is worth re-introducing so that we can avoid pro-actively shutting down the worker group - which was required when checking for availability since in the case where we have a failed group (i.e. scaling from 4 -> 2 workers) scaling down, all 4 TPU nodes would be held by the PG when we're trying to re-schedule onto the 2 available, alive nodes.

I think this behavior trade-off is fine if it enables torchft and is on par with how GPUs are handled. Additionally, we expect TPU slices to be scaled atomically so the case where we have 3/4 alive Ray nodes on a slice should be rare - since KubeRay will scale-down/re-start all Pods in multi-host workers groups.

@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, maybe we can create a follow-up issue for how to handle idleness for GPU/TPU workers that are in contention

@liulehui
liulehui force-pushed the tpu-elastic-training branch from a98ecde to e776e58 Compare March 29, 2026 18:30
Comment thread python/ray/util/tpu.py Resolved
@liulehui
liulehui requested a review from MengjinYan March 30, 2026 19:08
Comment thread python/ray/util/tpu.py Resolved

@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

Comment thread python/ray/util/tpu.py Resolved
Comment thread python/ray/util/tpu.py Resolved
@liulehui
liulehui force-pushed the tpu-elastic-training branch from 161c482 to 5d65c70 Compare April 1, 2026 00:28

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

🙌

@liulehui liulehui changed the title [jax][tpu] Simplify elastic TPU scaling to use intact slice counting [jax][tpu] Simplify elastic TPU scaling to use simplier slice counting Apr 1, 2026

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

Looks good from Core's perspective as well

@MengjinYan
MengjinYan merged commit 9cbcf12 into ray-project:master Apr 1, 2026
3 checks passed
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
ray-project#62141)

## Description
1. Introduces `get_num_tpu_slices` in ray.util.tpu, which counts
physically intact TPU slices (all hosts alive, correct chip count)
without requiring them to be idle. This replaces the use of
`get_num_ready_tpu_slices` during elastic scaling, removing the need to
fully shut down the old worker group before querying for available
slices.
2. Simplifies ElasticScalingPolicy by removing the current_num_workers
bookkeeping. Previously, the policy had to track running workers and add
back "owned" slices to the ready count. With intact-slice counting, this
workaround is no longer needed — the formula is simply
min(resource_based_slices, intact_slices) * workers_per_slice.
3. Removes the eager `_shutdown_worker_group` call from the controller's
non-running state handler, since the scaling policy no longer requires
workers to be stopped before it can accurately count available slices.

---------

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

Labels

core Issues that should be addressed in Ray Core 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.

5 participants