Skip to content

[llm] Add get_bundle_indices_sorted_by_node utility - #61715

Merged
kouroshHakha merged 5 commits into
masterfrom
pg-sort-bundles
Mar 16, 2026
Merged

kouroshHakha merged 5 commits into
masterfrom
pg-sort-bundles

Conversation

@jeffreywang88

@jeffreywang88 jeffreywang88 commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

When a placement group is provisioned, adjacent bundle indices don't necessarily map to the same physical node. If users overlook this and don't sort the bundles on the application end, they may end up placing a single TP group across nodes.

We add get_bundle_indices_sorted_by_node() utility to ray/llm/_internal/serve/utils/pg_utils.py that returns bundle indices reordered so that same-node bundles are adjacent, with the driver node's bundles first. Nodes other than the driver node are sorted by node ID.

Also clarified such behavior under https://docs.ray.io/en/latest/ray-core/scheduling/placement-group.html (the new note section):
Screenshot 2026-03-13 at 1 16 30 PM

Why not sort by GPU ID within each node?

The placement group table (bundles_to_node_id) only exposes which node each bundle is assigned to. It does not expose which physical GPU (CUDA_VISIBLE_DEVICES index) a bundle maps to. GPU IDs are only available from inside the task/actor running on that bundle, via ray.get_gpu_ids(). They are not queryable from the placement group metadata.

To enable GPU-aware ordering, we would need to either:

  1. Expose bundles_to_gpu_id in the placement group table (requires changes to the GCS placement group proto and _gen_placement_group_info in ray/_private/state.py). This approach would require the GCS scheduler to also resolve instance-level GPU assignments at PG creation time (now happens at task/actor scheduling time).
  2. Probe GPU IDs at runtime by spawning a lightweight task on each bundle to call ray.get_gpu_ids() and collect the results. This will add latency to the API.

Approach 1 involves intricate changes in GCS scheduler, while approach 2 seems like a hack to me and could be done on the application side if necessary.

Related issues

N/A

@jeffreywang88
jeffreywang88 requested a review from a team as a code owner March 13, 2026 05:02

@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 the get_bundle_indices_sorted_by_node utility in ray.util.placement_group. The function reorders bundle indices from a placement group to group them by node, prioritizing the driver's node. This is intended to help with locality-aware scheduling in distributed applications. The changes include the function implementation and corresponding unit and integration tests. I have reviewed the code and have no comments.

Comment thread python/ray/util/placement_group.py Resolved
@jeffreywang88 jeffreywang88 added the go add ONLY when ready to merge, run all tests label Mar 13, 2026
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Mar 13, 2026
@edoakes

edoakes commented Mar 13, 2026

Copy link
Copy Markdown
Collaborator

Why do we need a separate public API for this? Looks like the public APIs already expose the necessary functionality -- the implementation of get_bundle_indices_sorted_by_node is just wrapping a few lines of existing code calling public APIs.

@jeffreywang88

Copy link
Copy Markdown
Contributor Author

Chatted offline -- will move these utilities to serve llm which is where they are needed. For external libraries, they will handle such sorting on their own. And will improve placement group documentation to explicitly call out that bundles aren't necessarily sorted by physical ranks.

@jeffreywang88
jeffreywang88 requested a review from a team as a code owner March 13, 2026 20:11
@jeffreywang88
jeffreywang88 removed the request for review from MengjinYan March 13, 2026 20:12
@jeffreywang88 jeffreywang88 changed the title [core] Add get_bundle_indices_sorted_by_node public utility [llm] Add get_bundle_indices_sorted_by_node utility Mar 13, 2026

@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

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Signed-off-by: Jeffrey Wang <[email protected]>
Comment thread python/ray/util/placement_group.py Resolved
@kouroshHakha
kouroshHakha merged commit 6713909 into master Mar 16, 2026
3 checks passed
@kouroshHakha
kouroshHakha deleted the pg-sort-bundles branch March 16, 2026 02:31
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants