dispatch#
- ray.util.tpu.dispatch(fn: Any, *args: Any, topology: str | None = None, accelerator_version: str | None = None, tpu_slice: SlicePlacementGroup | None = None, num_slices: int = 1, chips_per_vm: int | None = None, head_reservation_timeout_s: float | None = 100.0, pg_ready_timeout_s: float | None = None, **kwargs: Any) List[ray._raylet.ObjectRef][source]#
Run a remote function on every host in a TPU slice.
Dispatches one task per host in the slice, pinning each task to its corresponding placement-group bundle via
PlacementGroupSchedulingStrategy. The function blocks until the underlying placement group is scheduled, then returns a list of object references — one per host — that can be passed directly toray.get.Resource options (
num_cpus=0,resources={"TPU": N}, andscheduling_strategy) are applied automatically via.options()and override any values set in the@ray.remotedecorator.- Parameters:
fn – A
@ray.remote-decorated function to run on every host.*args – Positional arguments broadcast to every task invocation.
topology – The TPU topology string (e.g.
"4x4","2x2x2"). Required whentpu_sliceisNone; ignored otherwise.accelerator_version – The TPU accelerator generation (e.g.
"v4","v6e"). Required whentpu_sliceisNone; ignored otherwise.tpu_slice – An existing
SlicePlacementGroupto schedule onto. When provided, the slice is used directly anddispatchdoes not create, modify, or tear down any placement groups. WhenNone(default), a new slice is reserved internally and its head placement groups are released once the worker placement group becomes ready.num_slices – Number of TPU slices to reserve. Ignored when
tpu_sliceis provided. Defaults to1.chips_per_vm – Optional override for the number of chips per VM. Ignored when
tpu_sliceis provided.head_reservation_timeout_s – Seconds to wait for each head placement group during slice reservation. Ignored when
tpu_sliceis provided. Defaults toDEFAULT_TPU_HEAD_RESERVATION_TIMEOUT_S.pg_ready_timeout_s – Seconds to wait for the worker placement group to become ready after reservation. Pass
Noneto wait indefinitely (default).**kwargs – Keyword arguments broadcast to every task invocation.
- Returns:
- One object reference per host in the slice.
Pass the list to
ray.getto retrieve results.
- Return type:
List[ray.ObjectRef]
- Raises:
TypeError – If
fnis not a@ray.remote-decorated function (i.e. it has no.options()method).ValueError – If
tpu_sliceisNoneand eithertopologyoraccelerator_versionis not provided.TimeoutError – If the placement group does not become ready within
pg_ready_timeout_sseconds. When the slice was created internally, it is shut down before the error is raised to avoid leaking resources.
Examples:
import ray from ray.util.tpu import dispatch, slice_placement_group @ray.remote def my_tpu_task(): import jax return jax.device_count() # One-shot: reserve a v6e 4x4 slice, run on every host, then # release automatically when the driver exits. results = ray.get( dispatch(my_tpu_task, topology="4x4", accelerator_version="v6e") ) # Reuse an existing slice across multiple calls. slice_handle = slice_placement_group(topology="4x4", accelerator_version="v6e") ray.get(slice_handle.placement_group.ready()) results1 = ray.get(dispatch(my_tpu_task, tpu_slice=slice_handle)) results2 = ray.get(dispatch(my_tpu_task, tpu_slice=slice_handle)) slice_handle.shutdown()
PublicAPI (alpha): This API is in alpha and may change before becoming stable.