Skip to content

Commit a8d98a2

Browse files
danielsuojsspencer
authored andcommitted
[pmap] Fix mesh sharding mismatch for ferminet with jax_pmap_shmap_merge.
When jax_pmap_shmap_merge is enabled, pmap creates an internal mesh with the specified axis_name. Input arrays must have matching NamedSharding with the same axis_name, otherwise JAX raises a sharding mismatch error. This change: - Inlines device_put_replicated logic in replicate_all_local_devices so we can pass an axis_name, allowing replicated args to have matching mesh sharding. - Updates broadcast_all_local_devices to use axis_name-aware pmap when jax_pmap_shmap_merge is enabled. - Updates ferminet calls to pass the correct axis_name (PMAP_AXIS_NAME). PiperOrigin-RevId: 866453666 Change-Id: I8bfffeb48bd2db6f0bb91860ccba1aadf9e78a19
1 parent b65b124 commit a8d98a2

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

ferminet/observables.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,15 @@ def make_density_matrix(
326326
# r' positions
327327
# In the case of excited states, keep the array flat
328328
rprime_pos = pos[idx].reshape(*data_shape, -1)
329+
rprime_pos = kfac_jax.utils.broadcast_all_local_devices(
330+
rprime_pos, axis_name=constants.PMAP_AXIS_NAME)
329331
rprime_prob = jnp.ones(rprime_pos.shape[:-1])
332+
rprime_prob = kfac_jax.utils.broadcast_all_local_devices(
333+
rprime_prob, axis_name=constants.PMAP_AXIS_NAME)
330334
# MCMC move width for r' Monte Carlo sampling
331-
move_width = kfac_jax.utils.replicate_all_local_devices(jnp.asarray([0.1]))
335+
move_width = kfac_jax.utils.replicate_all_local_devices(
336+
jnp.asarray([0.1]), axis_name=constants.PMAP_AXIS_NAME
337+
)
332338
pmove = np.zeros(cfg.mcmc.adapt_frequency)
333339

334340
density_state = DensityState(t=t,

ferminet/train.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,12 @@ def make_kfac_training_step(
340340
update. See the Step protocol for details.
341341
"""
342342
mcmc_step = constants.pmap(mcmc_step, donate_argnums=1)
343-
shared_mom = kfac_jax.utils.replicate_all_local_devices(jnp.zeros([]))
343+
shared_mom = kfac_jax.utils.replicate_all_local_devices(
344+
jnp.zeros([]), axis_name=constants.PMAP_AXIS_NAME
345+
)
344346
shared_damping = kfac_jax.utils.replicate_all_local_devices(
345-
jnp.asarray(damping))
347+
jnp.asarray(damping), axis_name=constants.PMAP_AXIS_NAME
348+
)
346349
# Due to some KFAC cleverness related to donated buffers, need to do this
347350
# to make state resettable
348351
copy_tree = constants.pmap(
@@ -425,9 +428,13 @@ def train(cfg: ml_collections.ConfigDict, writer_manager=None):
425428

426429
# Generate atomic configurations for each walker
427430
batch_atoms = jnp.tile(atoms[None, ...], [device_batch_size, 1, 1])
428-
batch_atoms = kfac_jax.utils.replicate_all_local_devices(batch_atoms)
431+
batch_atoms = kfac_jax.utils.replicate_all_local_devices(
432+
batch_atoms, axis_name=constants.PMAP_AXIS_NAME
433+
)
429434
batch_charges = jnp.tile(charges[None, ...], [device_batch_size, 1])
430-
batch_charges = kfac_jax.utils.replicate_all_local_devices(batch_charges)
435+
batch_charges = kfac_jax.utils.replicate_all_local_devices(
436+
batch_charges, axis_name=constants.PMAP_AXIS_NAME
437+
)
431438

432439
if cfg.debug.deterministic:
433440
seed = 23
@@ -530,7 +537,9 @@ def train(cfg: ml_collections.ConfigDict, writer_manager=None):
530537
)
531538
key, subkey = jax.random.split(key)
532539
params = network.init(subkey)
533-
params = kfac_jax.utils.replicate_all_local_devices(params)
540+
params = kfac_jax.utils.replicate_all_local_devices(
541+
params, axis_name=constants.PMAP_AXIS_NAME
542+
)
534543
signed_network = network.apply
535544
# Often just need log|psi(x)|.
536545
if cfg.system.get('states', 0):
@@ -619,9 +628,13 @@ def log_network(*args, **kwargs):
619628
# is nstates * nelectrons. The vmap over nstates is handled in the function
620629
# created in make_total_ansatz
621630
pos = jnp.reshape(pos, data_shape + (-1,))
622-
pos = kfac_jax.utils.broadcast_all_local_devices(pos)
631+
pos = kfac_jax.utils.broadcast_all_local_devices(
632+
pos, axis_name=constants.PMAP_AXIS_NAME
633+
)
623634
spins = jnp.reshape(spins, data_shape + (-1,))
624-
spins = kfac_jax.utils.broadcast_all_local_devices(spins)
635+
spins = kfac_jax.utils.broadcast_all_local_devices(
636+
spins, axis_name=constants.PMAP_AXIS_NAME
637+
)
625638
data = networks.FermiNetData(
626639
positions=pos, spins=spins, atoms=batch_atoms, charges=batch_charges
627640
)

0 commit comments

Comments
 (0)