Skip to content

fix(batch_norm): allow string cross_replica_axis for vmap support#859

Open
aditya161205 wants to merge 2 commits into
google-deepmind:mainfrom
aditya161205:fix/batchnorm-vmap-support
Open

fix(batch_norm): allow string cross_replica_axis for vmap support#859
aditya161205 wants to merge 2 commits into
google-deepmind:mainfrom
aditya161205:fix/batchnorm-vmap-support

Conversation

@aditya161205
Copy link
Copy Markdown

Fixes NameError when using hk.BatchNorm with a named jax.vmap axis.

Problem

hk.BatchNorm previously assumed cross_replica_axis was always available (like in pmap). When used with vmap, this caused a NameError: unbound axis name during initialization because the named axis doesn't exist in the init context.

Fix

Added a try-except NameError block around the jax.lax.pmean calls.

  • If the axis is unbound (e.g. during init), it now skips the sync and uses local stats.
  • Inside vmap, it correctly syncs statistics across the batch dimension.

Verification

  • Reproduced the crash locally using the script from #812.
  • Verified fix: init no longer crashes, and vmap statistics match expected values (std ~1.0).
  • Passed existing tests: haiku/_src/batch_norm_test.py

Closes #812
Closes #822

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Jan 8, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Average Pooling (avg_pool) returns the wrong shape BatchNorm vmap support for cross_replica_axis fails tests

1 participant