Skip to content

Commit bd82361

Browse files
judyzhaoxinwucamiloCienet
authored andcommitted
Add fix for catching Auto Axis Type with with_sharding_constraint and reshard if needed
1 parent 4741ca9 commit bd82361

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

axlearn/common/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,14 @@ def with_sharding_constraint(x: Tensor, shardings):
637637
mesh = thread_resources.env.physical_mesh
638638
if mesh.empty or mesh.size == 1:
639639
return x
640-
return jax.lax.with_sharding_constraint(x, shardings)
640+
try:
641+
return jax.lax.with_sharding_constraint(x, shardings)
642+
except ValueError as e:
643+
logging.warning("with_sharding_constraint failed: %s. Try to re-shard.", e)
644+
if "reshard" in str(e) or "Explicit" in str(e):
645+
named_sharding = jax.sharding.NamedSharding(mesh, shardings)
646+
return jax.device_put(x, named_sharding)
647+
raise e
641648

642649

643650
def maybe_shard(x: NestedTensor, partition_spec: Optional[PartitionSpec]) -> NestedTensor:

0 commit comments

Comments
 (0)