From 9fcc13fcd7329bcf8ab42aafd1e4baee97476f9d Mon Sep 17 00:00:00 2001 From: Pathways-on-Cloud Team Date: Mon, 23 Nov 2026 17:08:41 -0800 Subject: [PATCH] Fall back to shard assembly for concatenate_by_mesh_axis on JAX < 0.10.0. PiperOrigin-RevId: 1028579513 --- pathwaysutils/elastic/manager.py | 32 +++++++++++++++++++++++------ pathwaysutils/jax/__init__.py | 35 +++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 11 deletions(-) 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)