Skip to content

Commit

Permalink
manager: expose participating_rank (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
d4l3k authored Feb 3, 2025
1 parent 6e5dcbd commit 87290f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 20 additions & 0 deletions torchft/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,35 @@ def batches_committed(self) -> int:
"""
return self._batches_committed

def participating_rank(self) -> Optional[int]:
"""
Get the replica group rank of the current quorum. This will be the same on all
ranks within the replica group.
If this replica group is not participating in the current quorum, this will be None.
This will block on the async quorum if it is not yet ready.
Returns:
the rank of the current quorum
"""
self.wait_quorum()

return self._participating_rank

def num_participants(self) -> int:
"""
Get the number of participants in the current quorum.
This is the number of replicas participating in the current step.
This will block on the async quorum if it is not yet ready.
Returns:
the number of participants in the current quorum
"""
self.wait_quorum()

assert self._participating_world_size >= 0, "internal error"
return self._participating_world_size

Expand Down
9 changes: 8 additions & 1 deletion torchft/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import concurrent
from datetime import timedelta
from typing import Optional
from unittest import TestCase
Expand Down Expand Up @@ -521,10 +522,16 @@ def test_manager_wrap_future_timeout(self, client_mock: MagicMock) -> None:
def test_manager_numerics(self, client_mock: MagicMock) -> None:
manager = self._create_manager()

manager._quorum_future = MagicMock()
manager._quorum_future = quorum_future = MagicMock(
spec=concurrent.futures.Future
)
manager._participating_rank = 1
manager._participating_world_size = 5
self.assertEqual(manager.num_participants(), 5)
self.assertEqual(quorum_future.result.call_count, 1)
self.assertEqual(manager.participating_rank(), 1)
self.assertEqual(quorum_future.result.call_count, 2)

# pyre-ignore[16]: _pg is mocked
manager._pg.allreduce.return_value = _DummyWork(None)

Expand Down

0 comments on commit 87290f5

Please sign in to comment.