2023-04-17 11:53:57 -07:00
|
|
|
# Copyright 2023 The JAX Authors.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2023-12-11 13:59:29 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-21 14:20:39 -04:00
|
|
|
from collections.abc import Sequence
|
2023-04-17 11:53:57 -07:00
|
|
|
|
|
|
|
import jax
|
|
|
|
from jax.experimental import mesh_utils
|
|
|
|
from jax._src.lib import xla_client as xc
|
2024-02-22 16:54:52 -08:00
|
|
|
from jax._src.lib import xla_extension
|
2023-04-19 13:30:44 -07:00
|
|
|
from jax._src import xla_bridge as xb
|
2023-04-17 11:53:57 -07:00
|
|
|
|
|
|
|
Device = xc.Device
|
|
|
|
|
|
|
|
|
2024-01-26 13:52:47 +00:00
|
|
|
class TopologyDescription:
|
2023-06-23 15:11:37 -07:00
|
|
|
def __init__(self, devices: list[Device]):
|
|
|
|
self.devices: list[Device] = devices
|
2023-04-17 11:53:57 -07:00
|
|
|
|
|
|
|
|
2023-04-19 13:30:44 -07:00
|
|
|
def get_attached_topology(platform=None) -> TopologyDescription:
|
|
|
|
return TopologyDescription(jax.devices(backend=platform))
|
|
|
|
|
|
|
|
|
|
|
|
def get_topology_desc(
|
2023-12-11 13:59:29 +00:00
|
|
|
topology_name: str = "", platform: str | None = None, **kwargs
|
2023-04-19 13:30:44 -07:00
|
|
|
) -> TopologyDescription:
|
2023-05-11 10:41:12 -07:00
|
|
|
if platform == "tpu" or platform is None:
|
2023-04-19 13:30:44 -07:00
|
|
|
return TopologyDescription(
|
2023-06-17 12:34:36 -07:00
|
|
|
xb.make_pjrt_tpu_topology(
|
|
|
|
topology_name, **kwargs
|
|
|
|
)._make_compile_only_devices()
|
2023-04-19 13:30:44 -07:00
|
|
|
)
|
2024-02-22 16:54:52 -08:00
|
|
|
try:
|
|
|
|
topology = xb.make_pjrt_topology(platform, topology_name, **kwargs)
|
|
|
|
return TopologyDescription(topology._make_compile_only_devices())
|
|
|
|
except xla_extension.XlaRuntimeError as e:
|
|
|
|
msg, *_ = e.args
|
|
|
|
if msg.startswith("UNIMPLEMENTED"):
|
|
|
|
raise NotImplementedError(msg) from e
|
|
|
|
else:
|
|
|
|
raise
|
2023-04-17 11:53:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
# -- future mesh_utils --
|
|
|
|
|
2023-04-19 13:30:44 -07:00
|
|
|
|
|
|
|
def make_mesh(
|
|
|
|
topo: TopologyDescription,
|
|
|
|
mesh_shape: Sequence[int],
|
2023-06-23 15:11:37 -07:00
|
|
|
axis_names: tuple[str, ...],
|
2023-04-19 13:30:44 -07:00
|
|
|
*,
|
|
|
|
contiguous_submeshes: bool = False
|
|
|
|
) -> jax.sharding.Mesh:
|
2023-04-17 11:53:57 -07:00
|
|
|
devices = mesh_utils.create_device_mesh(
|
|
|
|
mesh_shape, list(topo.devices), contiguous_submeshes=contiguous_submeshes)
|
|
|
|
return jax.sharding.Mesh(devices, axis_names)
|