Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions transformer_engine/jax/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
W_JOINED_AXES = "nvte_w_joined"


def _get_mesh():
# Handle Mesh's set via `with mesh:`
mesh = _PXLA_THREAD_RESOURCES.env.physical_mesh
if mesh is not None and not mesh.empty:
return mesh
# Handle Mesh's set via `jax.set_mesh(mesh)`
return jax.sharding.get_abstract_mesh()


def _get_mesh_info(resource: str, mesh: jax.sharding.Mesh):
assert resource in mesh.axis_names, f"{resource} is not in the axis_names of Mesh {mesh}."
return mesh.shape[resource], resource
Expand All @@ -63,15 +72,15 @@ def is_mesh_available() -> bool:
"""
Check if a physical mesh is available.
"""
mesh = _PXLA_THREAD_RESOURCES.env.physical_mesh
mesh = _get_mesh()
return mesh is not None and not mesh.empty


def get_sharding_map_logic_axis_to_mesh_axis():
"""
Generate a dict to map logical axes to mesh axes.
"""
mesh = _PXLA_THREAD_RESOURCES.env.physical_mesh
mesh = _get_mesh()
if mesh is None or mesh.empty:
# If no mesh is defined, return an empty dict and do not require a MeshResource context to be present
return {}
Expand Down Expand Up @@ -130,7 +139,7 @@ def with_sharding_constraint(x: jnp.array, pspec: PartitionSpec):
if pspec is None:
return x

mesh = _PXLA_THREAD_RESOURCES.env.physical_mesh
mesh = _get_mesh()
if mesh.empty:
return x

Expand Down Expand Up @@ -211,7 +220,7 @@ def get_all_mesh_axes():
"""
Get all name of mesh axes
"""
mesh = _PXLA_THREAD_RESOURCES.env.physical_mesh
mesh = _get_mesh()
return mesh.axis_names


Expand Down Expand Up @@ -251,7 +260,7 @@ def get_num_devices_in_mesh(mesh=None):
by the global mesh.
"""
if mesh is None:
mesh = _PXLA_THREAD_RESOURCES.env.physical_mesh
mesh = _get_mesh()
if mesh.empty:
return 1
return np.prod(list(mesh.shape.values()))
Expand All @@ -264,7 +273,7 @@ def get_mesh_axis_size(axis, mesh=None):
by the global mesh.
"""
if mesh is None:
mesh = _PXLA_THREAD_RESOURCES.env.physical_mesh
mesh = _get_mesh()

if axis is None:
return 1
Expand Down
Loading