Skip to content

Commit 7a24226

Browse files
Cristian GarciaFlax Authors
authored andcommitted
_apply_sharding disallow mixed Explicit/Auto mesh
PiperOrigin-RevId: 859299941
1 parent d1e9f2e commit 7a24226

2 files changed

Lines changed: 39 additions & 49 deletions

File tree

flax/core/spmd.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,34 @@ def get_pspec(sharding_names, sharding_rules = None) -> PartitionSpec:
3232
return PartitionSpec(*from_sharding_rules(sharding_names, rules))
3333
return PartitionSpec(*sharding_names)
3434

35-
def _apply_sharding(value, sharding):
36-
with jax.disable_jit(False):
37-
return jax.jit(lambda x: x, out_shardings=sharding)(value)
35+
def _apply_sharding(value, sharding, mesh):
36+
if mesh.are_all_axes_explicit:
37+
return jax.sharding.reshard(value, sharding)
38+
elif mesh.are_all_axes_auto:
39+
return jax.lax.with_sharding_constraint(value, sharding)
40+
else:
41+
raise ValueError(
42+
'Mesh must have all axes as Explicit or all axes as Auto. '
43+
f'Got mixed axis types: {mesh.axis_types}')
44+
3845

39-
def shard_value(value, sharding_names, sharding_rules, mesh):
46+
def shard_value(
47+
value, sharding_names, sharding_rules,
48+
mesh: jax.sharding.AbstractMesh | jax.sharding.Mesh | None
49+
):
4050
if not sharding_names:
4151
return value
42-
if not mesh and not meta.global_mesh_defined():
52+
53+
if mesh is None:
54+
mesh = meta.get_global_mesh()
55+
56+
if mesh is None:
4357
raise ValueError(
4458
'An auto mesh context or metadata is required if creating a variable'
4559
f' with annotation {sharding_names=}. '
4660
'For more guidance, see https://flax.readthedocs.io/en/latest/flip/4844-var-eager-sharding.html.')
4761
pspec = get_pspec(sharding_names, sharding_rules)
48-
if mesh is not None:
49-
return _apply_sharding(value, NamedSharding(mesh, pspec))
50-
return _apply_sharding(value, pspec)
62+
return _apply_sharding(value, NamedSharding(mesh, pspec), mesh)
5163

5264

5365

tests/nnx/spmd_test.py

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -301,57 +301,35 @@ def __init__(self, rngs):
301301
assert jax.tree.leaves(abs_state)[0].sharding.is_equivalent_to(
302302
NamedSharding(mesh, P(None, 'model')), ndim=2)
303303

304-
def test_explicit_sharding(self):
305-
mesh = jax.make_mesh(
306-
(2, 2),
307-
('row', 'col'),
308-
axis_types=(jax.sharding.AxisType.Auto, jax.sharding.AxisType.Explicit),
309-
)
310-
v = nnx.Variable(
311-
jnp.ones((4, 4)),
312-
sharding_names=('row', 'col'),
313-
mesh=mesh,
314-
)
315-
self.assertEqual(v.sharding.mesh, mesh)
316-
self.assertEqual(
317-
v.sharding.spec,
318-
P('row', 'col'),
319-
)
304+
@parameterized.parameters('auto', 'explicit', 'mixed')
305+
def test_sharding_axis_types(self, mode):
306+
if mode == 'auto':
307+
axis_types = (jax.sharding.AxisType.Auto, jax.sharding.AxisType.Auto)
308+
elif mode == 'explicit':
309+
axis_types = (jax.sharding.AxisType.Explicit, jax.sharding.AxisType.Explicit)
310+
else:
311+
axis_types = (jax.sharding.AxisType.Auto, jax.sharding.AxisType.Explicit)
320312

321-
def test_explicit_sharding_disable_jit(self):
322313
mesh = jax.make_mesh(
323314
(2, 2),
324315
('row', 'col'),
325-
axis_types=(jax.sharding.AxisType.Auto, jax.sharding.AxisType.Explicit),
316+
axis_types=axis_types,
326317
)
327-
with jax.disable_jit(True):
318+
if mode == 'mixed':
319+
with self.assertRaises(ValueError):
320+
nnx.Variable(
321+
jnp.ones((4, 4)),
322+
sharding_names=('row', 'col'),
323+
mesh=mesh,
324+
)
325+
else:
328326
v = nnx.Variable(
329327
jnp.ones((4, 4)),
330328
sharding_names=('row', 'col'),
331329
mesh=mesh,
332330
)
333-
self.assertEqual(v.sharding.mesh, mesh)
334-
self.assertEqual(
335-
v.sharding.spec,
336-
P('row', 'col'),
337-
)
338-
339-
def test_explicit_sharding_mesh_context(self):
340-
mesh = jax.make_mesh(
341-
(2, 2),
342-
('row', 'col'),
343-
axis_types=(jax.sharding.AxisType.Auto, jax.sharding.AxisType.Explicit),
344-
)
345-
with jax.set_mesh(mesh):
346-
v = nnx.Variable(
347-
jnp.ones((4, 4)),
348-
sharding_names=('row', 'col'),
349-
)
350-
self.assertEqual(v.sharding.mesh, mesh)
351-
self.assertEqual(
352-
v.sharding.spec,
353-
P('row', 'col'),
354-
)
331+
self.assertEqual(v.sharding.mesh, mesh)
332+
self.assertEqual(v.sharding.spec, P('row', 'col'))
355333

356334
def has_sharding_spec(array):
357335
sharding = array.sharding

0 commit comments

Comments
 (0)