|
| 1 | +"""Utilities for dealing with sharding in JAX.""" |
| 2 | + |
| 3 | +import jax |
| 4 | +from jax.sharding import NamedSharding |
| 5 | +from jax.sharding import PartitionSpec |
| 6 | + |
| 7 | + |
| 8 | +def get_mesh() -> jax.sharding.Mesh: |
| 9 | + """Creates a mesh from all available GPUs. |
| 10 | + Here, we simply create a one-dimensional mesh.""" |
| 11 | + return jax.sharding.Mesh(jax.devices(), ("batch",)) |
| 12 | + |
| 13 | + |
| 14 | +def get_replicated_sharding(mesh=None): |
| 15 | + """Returns a sharding spec that replicates data across all devices.""" |
| 16 | + if mesh is None: |
| 17 | + mesh = get_mesh() |
| 18 | + return NamedSharding(mesh, PartitionSpec()) |
| 19 | + |
| 20 | + |
| 21 | +def shard_replicated(x, mesh=None): |
| 22 | + """Shards a tensor across all devices.""" |
| 23 | + if mesh is None: |
| 24 | + mesh = get_mesh() |
| 25 | + return jax.tree.map( |
| 26 | + lambda x: jax.device_put(x, get_replicated_sharding(mesh)), x) |
| 27 | + |
| 28 | + |
| 29 | +def get_naive_sharding_spec(mesh=None): |
| 30 | + """Returns a sharding spec that shards data along the first axis.""" |
| 31 | + if mesh is None: |
| 32 | + mesh = get_mesh() |
| 33 | + return NamedSharding(mesh, PartitionSpec("batch")) |
| 34 | + |
| 35 | + |
| 36 | +def get_naive_sharding(x, mesh=None): |
| 37 | + """Given a 1D mesh and a tensor, try to shard along the appropriate axis.""" |
| 38 | + if mesh is None: |
| 39 | + mesh = get_mesh() |
| 40 | + grid_size = mesh.shape["batch"] |
| 41 | + if len(x.shape) > 0 and x.shape[0] % grid_size == 0: |
| 42 | + return NamedSharding(mesh, PartitionSpec("batch")) |
| 43 | + else: |
| 44 | + return NamedSharding(mesh, PartitionSpec()) |
| 45 | + |
| 46 | + |
| 47 | +def shard_params(params, mesh=None): |
| 48 | + """Shards a parameter tree across all devices |
| 49 | + with naive sharding (see get_naive_sharding).""" |
| 50 | + if mesh is None: |
| 51 | + mesh = get_mesh() |
| 52 | + return jax.tree.map(lambda x: jax.device_put(x, get_naive_sharding(x)), |
| 53 | + params) |
| 54 | + |
| 55 | + |
| 56 | +def shard_naive(x, mesh=None): |
| 57 | + return shard_params(x, mesh) |
| 58 | + |
| 59 | + |
| 60 | +def get_naive_sharding_tree(input_tree, mesh=None): |
| 61 | + if mesh is None: |
| 62 | + mesh = get_mesh() |
| 63 | + return jax.tree.map(lambda x: get_naive_sharding(x, mesh), input_tree) |
| 64 | + |
| 65 | + |
| 66 | +def get_sharding_tree(params, mesh=None): |
| 67 | + """Returns a sharding tree for a parameter tree.""" |
| 68 | + return jax.tree.map(lambda x: get_naive_sharding(x, mesh), params) |
| 69 | + |
| 70 | + |
| 71 | +def get_empty_sharding(mesh=None): |
| 72 | + """Returns a sharding spec that replicates data across all devices.""" |
| 73 | + if mesh is None: |
| 74 | + mesh = get_mesh() |
| 75 | + return NamedSharding(mesh, PartitionSpec()) |
| 76 | + |
| 77 | + |
| 78 | +def disp_shard_info(x: jax.Array): |
| 79 | + """Displays shard info of a jax array.""" |
| 80 | + for shard in x.addressable_shards: |
| 81 | + print(f"shard.device: {shard.device}, index: {shard.index}, replica_id:" |
| 82 | + f" {shard.replica_id}.\n") |
0 commit comments