Skip to content

Fix BatchNorm cleanup after failed precise statistics updates - #161

Open
Linxiushen wants to merge 1 commit into
facebookresearch:mainfrom
Linxiushen:codex/precise-bn-error-cleanup
Open

Linxiushen wants to merge 1 commit into
facebookresearch:mainfrom
Linxiushen:codex/precise-bn-error-cleanup

Conversation

@Linxiushen

Copy link
Copy Markdown

update_bn_stats restores BatchNorm momentum and removes its temporary forward pre-hooks only on success. If the loader ends early, iteration raises, or forward raises after BatchNorm, the caller's layers retain momentum=1.0 and stale hooks. A later normal forward can then raise the reused-BN assertion.

Move restoration and hook removal into finally, retaining the original exception and the existing statistics calculation. The regressions cover insufficient input, a forward exception, and a loader exception; they also check distinct layer momenta (including None), preservation of caller hooks, and subsequent normal forward/recalibration. Running statistics and other model state are not rolled back on failure.

Validation on Windows, Python 3.12.14, CPU PyTorch 2.9.1:

  • On the unchanged production base, the expanded regression suite has three failing assertions across two methods (including the two exception subtests); the existing successful-statistics test passes.
  • python -m unittest discover -s tests -v: 118 passed, 2 CUDA tests skipped, with tabulate==0.9.0.
  • python -m unittest discover -s tests/param_scheduler -v: 20 passed.
  • Python 3.9.13 with the CI versions Black 24.2.0, isort 4.3.21 and Flake8 3.8.1: changed-file Black/Flake8 checks and repository isort check pass; git diff --check passes.

Retained environment/base limitations: with tabulate 0.10.0, test_model_stats_table fails identically on the pristine base and this patch. Repository-wide Black has existing formatting differences, and repository-wide Flake8 reports an unchanged B950 in tests/test_checkpoint.py:291; both were checked against the pristine base. This patch formats the two existing assertions in the affected function as required by Black, and does not change those unrelated files. No GPU, distributed execution, pretrained model, or training-quality result is claimed.

AI assistance: the patch and regression tests were prepared with Codex.

@meta-cla

meta-cla Bot commented Oct 20, 2026

Copy link
Copy Markdown

Hi @Linxiushen!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

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.

1 participant