Skip to content

FIX get_output_shape updating BatchNorm running stats - #1420

Merged
bruAristimunha merged 3 commits into
masterfrom
fix/get-output-shape-eval
Oct 16, 2026
Merged

bruAristimunha merged 3 commits into
masterfrom
fix/get-output-shape-eval

Conversation

@adammounir

Copy link
Copy Markdown
Collaborator

EEGModuleMixin.get_output_shape() runs a forward pass on a zero input under torch.inference_mode(), but does not switch the model to eval mode. In train mode (the default for a freshly built model), BatchNorm still updates its running statistics with that all-zeros batch, and dropout draws masks (consuming RNG).

  • Calling it on a trained model in train mode corrupts its BN statistics. On a Deep4Net with running_mean=5, running_var=4, one call moves them to 4.5 / 3.6.
  • EEGNet, EEGITNet, EEGConformer, MSVTNet and TSception call it in __init__, so every new instance already has num_batches_tracked == 1 and running stats pulled toward the zero input.

Fix

Record module.training for every module, call self.eval(), run the probe, then restore each module's own flag in finally. Restoring each flag, rather than calling self.train(was_training), keeps submodules that the user had put in eval on purpose (e.g. a frozen encoder) as they were.

Tests

New test_get_output_shape_leaves_batchnorm_stats_and_modes_unchanged. It checks that the BN stats and num_batches_tracked are unchanged, that both the model's and a frozen submodule's modes are restored, and that EEGNet starts with num_batches_tracked == 0. It fails on master. test_base.py: 50 passed. test_models.py: 694 passed, 6 skipped.

Copilot AI balanced review requested due to automatic review settings October 15, 2026 07:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The functional fix is sound; the remaining changelog example correction is nonblocking.

1 open finding
What changed in this PR

Prevents output-shape inference from changing BatchNorm statistics or drawing dropout masks in EEG models.

Changes:

  • Runs the shape probe in eval mode and restores individual module modes afterward.
  • Adds regression coverage for statistics and mode preservation.
  • Documents the behavior change.
File Description
test/​unit_tests/​models/​test_base.py Tests statistics preservation, mode restoration, and EEGNet initialization.
docs/​whats_new.rst Adds a release note for the fix.
braindecode/​models/​base.py Temporarily switches the probe to eval mode with restoration in finally.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread docs/whats_new.rst
Bug fixes
==========

- :meth:`braindecode.models.base.EEGModuleMixin.get_output_shape` runs its probe forward pass in eval mode and restores each module's mode afterwards, so it no longer updates BatchNorm running statistics or draws dropout masks; models that call it in ``__init__`` (e.g. :class:`braindecode.models.EEGNet`, :class:`braindecode.models.EEGConformer`) start with untouched BatchNorm statistics (:gh:`1420` by `Adam Mounir`_).
@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.14%. Comparing base (baec1ae) to head (72fc78b).
⚠️ Report is 22 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1420   +/-   ##
=======================================
  Coverage   90.13%   90.14%           
=======================================
  Files         167      167           
  Lines       21815    21848   +33     
=======================================
+ Hits        19663    19694   +31     
- Misses       2152     2154    +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bruAristimunha
bruAristimunha merged commit c633dac into master Oct 16, 2026
7 checks passed
@bruAristimunha
bruAristimunha deleted the fix/get-output-shape-eval branch November 22, 2026 23:02
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.

3 participants