Skip to content
Open
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: 13 additions & 8 deletions haiku/_src/batch_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,19 @@ def __call__(
mean = jnp.mean(inputs, axis, keepdims=True)
mean_of_squares = jnp.mean(jnp.square(inputs), axis, keepdims=True)
if self.cross_replica_axis:
mean = jax.lax.pmean(
mean,
axis_name=self.cross_replica_axis,
axis_index_groups=self.cross_replica_axis_index_groups)
mean_of_squares = jax.lax.pmean(
mean_of_squares,
axis_name=self.cross_replica_axis,
axis_index_groups=self.cross_replica_axis_index_groups)
try:
mean = jax.lax.pmean(
mean,
axis_name=self.cross_replica_axis,
axis_index_groups=self.cross_replica_axis_index_groups)
mean_of_squares = jax.lax.pmean(
mean_of_squares,
axis_name=self.cross_replica_axis,
axis_index_groups=self.cross_replica_axis_index_groups)
except NameError:
# If the axis is not bound (e.g. during init or non-mapped execution),
# we skip the sync and fall back to local statistics.
pass
var = mean_of_squares - jnp.square(mean)
else:
mean = self.mean_ema.average.astype(inputs.dtype)
Expand Down