From 5d283e0d76a7be02b44c703b95e4a0181681f4fe Mon Sep 17 00:00:00 2001 From: Pathways-on-Cloud Team Date: Sun, 25 Oct 2026 01:55:41 -0700 Subject: [PATCH] Make DefaultSliceHealthChecker._plus_one a static method. PiperOrigin-RevId: 1006641630 --- pathwaysutils/elastic/elastic.py | 4 ++-- pathwaysutils/elastic/manager.py | 32 +++++++++++++++++++++++------ pathwaysutils/jax/__init__.py | 35 +++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/pathwaysutils/elastic/elastic.py b/pathwaysutils/elastic/elastic.py index f20aecd..223aec2 100644 --- a/pathwaysutils/elastic/elastic.py +++ b/pathwaysutils/elastic/elastic.py @@ -78,7 +78,8 @@ class DefaultSliceHealthChecker(SliceHealthChecker): that the devices are active and can perform computations. """ - def _plus_one(self, x: jax.Array) -> jax.Array: + @staticmethod + def _plus_one(x: jax.Array) -> jax.Array: return x + 1 def _simple_execution(self, devices: Sequence[jax.Device]) -> jax.Array: @@ -124,7 +125,6 @@ def validate(self) -> Set[int]: return frozenset(active_slice_indices) - def get_slice_to_devices( devices: Sequence[jax.Device], ) -> Mapping[int, Sequence[jax.Device]]: diff --git a/pathwaysutils/elastic/manager.py b/pathwaysutils/elastic/manager.py index 78f3639..7868ff5 100644 --- a/pathwaysutils/elastic/manager.py +++ b/pathwaysutils/elastic/manager.py @@ -100,11 +100,22 @@ class Manager: available_inactive_slices: Set[int] _stop_event: threading.Event | None _monitor_thread: threading.Thread | None - def __init__(self, devices: Sequence[jax.Device] | None = None) -> None: + def __init__( + self, + devices: Sequence[jax.Device] | None = None, + *, + minimum_slice_count: int | None = None, + poll_interval: float | int = 10, + timeout: float | int | None = None, + ) -> None: """Initializes the manager. Args: devices: The devices to use. If None, jax.devices() is used. + minimum_slice_count: Minimum active slices to wait for (< 1 = all). If + None, active slices are sampled once without waiting. + poll_interval: Seconds between slice health checks when waiting. + timeout: Maximum seconds to wait when waiting, or None for no timeout. """ if devices is None: devices = jax.devices() @@ -115,9 +126,18 @@ def __init__(self, devices: Sequence[jax.Device] | None = None) -> None: self._active_slice_indices = frozenset() self.available_inactive_slices = frozenset() - self.active_slice_indices = elastic.get_active_slice_indices( - slice_to_devices=self.slice_to_devices - ) + if minimum_slice_count is not None: + slice_count = None if minimum_slice_count < 1 else minimum_slice_count + self.active_slice_indices = elastic.wait_for_slices( + slice_count=slice_count, + poll_interval=poll_interval, + timeout=timeout, + slice_to_devices=self.slice_to_devices, + ) + else: + self.active_slice_indices = elastic.get_active_slice_indices( + slice_to_devices=self.slice_to_devices + ) self._stop_event = None self._monitor_thread = None @@ -324,7 +344,7 @@ def elastic_retry( Deprecated: Use `retry_policy` instead. TODO: b/523384843 - Remove max_retries parameter. minimum_slice_count: The minimum number of slices required to run the - function. If None, defaults to the total number of slices. + function. If None or < 1, defaults to the total number of slices. poll_interval: The number of seconds to wait between activity checks. Defaults to 10 seconds. timeout: The maximum number of seconds to wait for slices to become active @@ -348,7 +368,7 @@ def elastic_retry( """ target_slice_count = ( self.total_slice_count - if minimum_slice_count is None + if minimum_slice_count is None or minimum_slice_count < 1 else minimum_slice_count ) diff --git a/pathwaysutils/jax/__init__.py b/pathwaysutils/jax/__init__.py index 7ae663d..8839d0d 100644 --- a/pathwaysutils/jax/__init__.py +++ b/pathwaysutils/jax/__init__.py @@ -18,10 +18,38 @@ """ -import functools +from collections.abc import Sequence import jax +def _concatenate_from_shards( + arrays: Sequence[Sequence[jax.Array]], + sharded_dim_idxs: Sequence[int], + out_shardings: Sequence[jax.sharding.NamedSharding], + **unused_kwargs, +) -> list[jax.Array]: + """Concatenates submesh arrays via single-device shard assembly.""" + results = [] + for replicas, dim, sharding in zip( + arrays, sharded_dim_idxs, out_shardings, strict=True + ): + shape = list(replicas[0].shape) + if dim >= 0: + shape[dim] = sum(replica.shape[dim] for replica in replicas) + shards = [] + for replica in replicas: + shards.extend(shard.data for shard in replica.addressable_shards) + results.append( + jax.make_array_from_single_device_arrays( + tuple(shape), sharding, shards, dtype=replicas[0].dtype + ) + ) + return results + + +import functools + + class _FakeJaxFunction: """An object that raises an ImportError for __getattr__ and __call__. @@ -90,10 +118,7 @@ def __call__(self, *args, **kwargs): except (ImportError, AttributeError): # jax<0.10.0 - concatenate_by_mesh_axis = _FakeJaxFunction( - "jax.jaxlib._pathways._concatenate_by_mesh_axis", - "0.10.0", - ) + concatenate_by_mesh_axis = _concatenate_from_shards @functools.lru_cache(maxsize=1)