Skip to content

Conversation

@abrarsheikh
Copy link
Contributor

@abrarsheikh abrarsheikh commented Nov 9, 2025

Summary

This PR extends Ray Serve's replica rank system to track node rank and local rank in addition to the existing global rank, enabling better distributed serving coordination and multi-node deployment awareness.

Changes

Core Implementation (deployment_state.py)

  • Extended DeploymentRankManager to maintain three levels of rank tracking:
    • Global rank: Replica-level rank across all nodes (0 to N-1)
    • Node rank: Index assigned to each node (0 to M-1)
    • Local rank: Replica's rank within its node (0 to K-1 per node)
  • Modified assign_rank() to return ReplicaRank objects containing all three rank types
  • Added node rank manager and per-node local rank managers to track replica placement
  • Updated get_replica_ranks_mapping() to return Dict[str, ReplicaRank] instead of Dict[str, int]

Backward Compatibility

All existing functionality is preserved. The global rank behavior remains unchanged, with node and local ranks added as additional fields in the ReplicaRank object.


Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
@abrarsheikh abrarsheikh added the go add ONLY when ready to merge, run all tests label Nov 9, 2025
Signed-off-by: abrar <abrar@anyscale.com>
@abrarsheikh abrarsheikh changed the title Add Node Rank and Local Rank Support to Ray Serve Replica Ranks [4/n] [Serve] Add Node Rank and Local Rank Support to Ray Serve Replica Ranks Nov 9, 2025
abrarsheikh added a commit that referenced this pull request Nov 19, 2025
**Summary**

Modified replica rank assignment to defer rank allocation until the
replica is actually allocated, rather than assigning it during the
startup call. This is necessary when we want to add node local rank in
future, in order to support node rank and node local rank we need to
know the node_id which is only known after replica is allocated.

**Changes**

- Changed `start()` method signature to accept `assign_rank_callback`
instead of a pre-assigned `rank` parameter
- Rank is now assigned after `_allocated_obj_ref` is resolved, ensuring
the replica is allocated before rank assignment
- Pass rank to `initialize_and_get_metadata()` method on the replica
actor, allowing rank to be set during initialization
- Updated `ReplicaBase.initialize()` to accept rank as a parameter and
set it along with the internal replica context
- Added `PENDING_INITIALIZATION` status check to handle cases where
`_ready_obj_ref` is not yet set

Next PR #58479

---------

Signed-off-by: abrar <abrar@anyscale.com>
Base automatically changed from LLM-2497-abrar-rank-p3 to master November 19, 2025 18:04
Aydin-ab pushed a commit to Aydin-ab/ray-aydin that referenced this pull request Nov 19, 2025
…roject#58477)

**Summary**

Modified replica rank assignment to defer rank allocation until the
replica is actually allocated, rather than assigning it during the
startup call. This is necessary when we want to add node local rank in
future, in order to support node rank and node local rank we need to
know the node_id which is only known after replica is allocated.

**Changes**

- Changed `start()` method signature to accept `assign_rank_callback`
instead of a pre-assigned `rank` parameter
- Rank is now assigned after `_allocated_obj_ref` is resolved, ensuring
the replica is allocated before rank assignment
- Pass rank to `initialize_and_get_metadata()` method on the replica
actor, allowing rank to be set during initialization
- Updated `ReplicaBase.initialize()` to accept rank as a parameter and
set it along with the internal replica context
- Added `PENDING_INITIALIZATION` status check to handle cases where
`_ready_obj_ref` is not yet set

Next PR ray-project#58479

---------

Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: Aydin Abiar <aydin@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: abrar <abrar@anyscale.com>
@abrarsheikh abrarsheikh marked this pull request as ready for review November 19, 2025 19:32
@abrarsheikh abrarsheikh requested a review from a team as a code owner November 19, 2025 19:32
@ray-gardener ray-gardener bot added the serve Ray Serve Related Issue label Nov 20, 2025
Signed-off-by: abrar <abrar@anyscale.com>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Nov 21, 2025
…roject#58477)

**Summary**

Modified replica rank assignment to defer rank allocation until the
replica is actually allocated, rather than assigning it during the
startup call. This is necessary when we want to add node local rank in
future, in order to support node rank and node local rank we need to
know the node_id which is only known after replica is allocated.

**Changes**

- Changed `start()` method signature to accept `assign_rank_callback`
instead of a pre-assigned `rank` parameter
- Rank is now assigned after `_allocated_obj_ref` is resolved, ensuring
the replica is allocated before rank assignment
- Pass rank to `initialize_and_get_metadata()` method on the replica
actor, allowing rank to be set during initialization
- Updated `ReplicaBase.initialize()` to accept rank as a parameter and
set it along with the internal replica context
- Added `PENDING_INITIALIZATION` status check to handle cases where
`_ready_obj_ref` is not yet set

Next PR ray-project#58479

---------

Signed-off-by: abrar <abrar@anyscale.com>
ykdojo pushed a commit to ykdojo/ray that referenced this pull request Nov 27, 2025
…roject#58477)

**Summary**

Modified replica rank assignment to defer rank allocation until the
replica is actually allocated, rather than assigning it during the
startup call. This is necessary when we want to add node local rank in
future, in order to support node rank and node local rank we need to
know the node_id which is only known after replica is allocated.

**Changes**

- Changed `start()` method signature to accept `assign_rank_callback`
instead of a pre-assigned `rank` parameter
- Rank is now assigned after `_allocated_obj_ref` is resolved, ensuring
the replica is allocated before rank assignment
- Pass rank to `initialize_and_get_metadata()` method on the replica
actor, allowing rank to be set during initialization
- Updated `ReplicaBase.initialize()` to accept rank as a parameter and
set it along with the internal replica context
- Added `PENDING_INITIALIZATION` status check to handle cases where
`_ready_obj_ref` is not yet set

Next PR ray-project#58479

---------

Signed-off-by: abrar <abrar@anyscale.com>
Signed-off-by: YK <1811651+ykdojo@users.noreply.github.com>
SheldonTsen pushed a commit to SheldonTsen/ray that referenced this pull request Dec 1, 2025
…roject#58477)

**Summary**

Modified replica rank assignment to defer rank allocation until the
replica is actually allocated, rather than assigning it during the
startup call. This is necessary when we want to add node local rank in
future, in order to support node rank and node local rank we need to
know the node_id which is only known after replica is allocated.

**Changes**

- Changed `start()` method signature to accept `assign_rank_callback`
instead of a pre-assigned `rank` parameter
- Rank is now assigned after `_allocated_obj_ref` is resolved, ensuring
the replica is allocated before rank assignment
- Pass rank to `initialize_and_get_metadata()` method on the replica
actor, allowing rank to be set during initialization
- Updated `ReplicaBase.initialize()` to accept rank as a parameter and
set it along with the internal replica context
- Added `PENDING_INITIALIZATION` status check to handle cases where
`_ready_obj_ref` is not yet set

Next PR ray-project#58479

---------

Signed-off-by: abrar <abrar@anyscale.com>
Copy link
Contributor

@eicherseiji eicherseiji left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @abrarsheikh!


return self._execute_with_error_handling(
_get_replica_rank_impl, ReplicaRank(rank=0, node_rank=-1, local_rank=-1)
_get_replica_rank_impl, ReplicaRank(rank=0, node_rank=0, local_rank=0)
Copy link
Contributor

Choose a reason for hiding this comment

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

For my information, why change the default from -1 -> 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

-1 was a placeholder, for a previous stacked diff.

@abrarsheikh abrarsheikh enabled auto-merge (squash) December 3, 2025 00:36
self._local_rank_managers[node_id].recover_rank(replica_id, rank.local_rank)

# Track the replica-to-node mapping
self._replica_to_node[replica_id] = node_id
Copy link

Choose a reason for hiding this comment

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

Bug: Inconsistent state on partial failure in recover_rank

The order of operations in _recover_rank_impl is inconsistent with _assign_rank_impl. In _assign_rank_impl, _replica_to_node[replica_id] = node_id is set first (before any rank assignments), but in _recover_rank_impl, it's set last (after all rank recoveries). When _fail_on_rank_error=False and an error occurs after recovering the global rank but before setting _replica_to_node, the system ends up in an inconsistent state where _replica_rank_manager has the replica's global rank but _replica_to_node doesn't have the mapping. This causes has_replica_rank() to return False even though ranks are partially assigned, potentially leading to duplicate assignment errors on retry.

Additional Locations (1)

Fix in Cursor Fix in Web

@abrarsheikh abrarsheikh merged commit f232a80 into master Dec 3, 2025
7 checks passed
@abrarsheikh abrarsheikh deleted the LLM-2497-abrar-rank-p4 branch December 3, 2025 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants