get_torchtpu_env_vars#

ray.util.tpu.get_torchtpu_env_vars(topology: str, worker_hostnames: str | List[str] | None = None, worker_id: int | None = None, slicebuilder_addresses: str | List[str] | None = None, slicebuilder_port: int = 8471, master_addr: str | None = None, tpu_resource_per_chip: int = 1, accelerator_type: str | None = None) → Dict[str, str][source]#

Returns environment variables required for PyTorch/TPU (torch_tpu) execution.

Derived TORCH_TPU_SLICEBUILDER_ADDRESSES entries are ordered host-major across worker_hostnames with devices_per_host sequential ports per host starting at slicebuilder_port (torch_tpu indexes addresses[RANK % len(addresses)]). Standard torch.distributed rank variables (RANK, LOCAL_RANK, WORLD_SIZE, MASTER_PORT) are set by the caller or launcher (such as torchrun), and multi-slice workloads pair this with get_tpu_coordinator_env_vars().

Parameters:
  • topology (str) – The target TPU topology string (e.g. “4x4”, “2x4”, or “4,4,1”).

  • worker_hostnames (str | List[str] | None) – Optional comma-separated string or list of host IP addresses or DNS hostnames. When both worker_hostnames and slicebuilder_addresses are omitted, only TORCH_TPU_TOPOLOGY (plus MASTER_ADDR or TPU_WORKER_ID if explicitly passed) is returned for pairing with ambient container environment variables.

  • worker_id (int | None) – Optional integer ID of the worker host (0-indexed, for passing to torchrun-style launchers as --node_rank / NODE_RANK). Defaults to 0 for a single host; when omitted on multiple hosts, TPU_WORKER_ID is not set.

  • slicebuilder_addresses (str | List[str] | None) – Optional comma-separated string or list of address:port strings. If omitted, derived from worker_hostnames and slicebuilder_port.

  • slicebuilder_port (int) – Base port number for slicebuilder addresses (defaults to 8471).

  • master_addr (str | None) – Optional IP address or hostname of the rank-0 master node. If omitted, defaults to the first worker hostname.

  • tpu_resource_per_chip (int) – Logical TPU resources per physical chip (defaults to 1).

  • accelerator_type (str | None) – Optional TPU accelerator version or type (e.g. “v6e”, “v7x”).

Returns:

A dictionary mapping PyTorch/TPU environment variables to their values.

Raises:
  • TypeError – If worker_id, slicebuilder_port, or tpu_resource_per_chip is not an integer.

  • ValueError – If topology, worker_hostnames, slicebuilder_addresses, slicebuilder_port, master_addr, worker_id, or tpu_resource_per_chip is invalid.

Return type:

Dict[str, str]

PublicAPI (alpha): This API is in alpha and may change before becoming stable.