fix(dnn): BatchNorm running-var bias, pooling channel_axis, LayerNorm error#843
Merged
Conversation
…erNorm error - BatchNorm stored the biased batch variance into running_var; apply Bessel's N/(N-1) correction for the running buffer (PyTorch-consistent), keeping the biased variance for in-batch normalization (Medium) - Pooling rejected the leftmost negative channel_axis (== -x_dim) due to an abs() bound; widen to -x_dim <= axis < x_dim (Pool, _MaxPoolNd, AdaptivePool) (Medium) - LayerNorm wrong-shape error did ", ".join(<ints>) -> TypeError masking the intended ValueError; map(str, ...) (Medium) Findings recorded in docs/issues-found-20260619-dnn.md
Reviewer's GuideAdjusts BatchNorm to store an unbiased (Bessel-corrected) running variance while keeping biased in-batch normalization, fixes pooling channel_axis validation to accept the leftmost negative axis, and corrects LayerNorm’s shape-mismatch error message to raise the intended ValueError; adds focused regression tests and an audit doc describing the issues and their status. Flow diagram for BatchNorm.update unbiased running_var computationflowchart TD
X["Input x"] --> A["Compute mean over axis"]
A --> B["Compute mean_of_square over axis"]
B --> C["var = max(0, mean_of_square - mean^2)<br/>(biased, divisor N)"]
C --> D["num_reduced = product of reduced x.shape[axis]"]
D --> E{"num_reduced > 1?"}
E -- Yes --> F["unbiased_var = var * (num_reduced / (num_reduced - 1))"]
E -- No --> G["unbiased_var = var"]
F --> H["running_var = momentum * running_var + (1 - momentum) * unbiased_var"]
G --> H
C --> I["Use biased var for in-batch normalization"]
Flow diagram for pooling channel_axis validation and normalizationflowchart TD
X["Given x_dim and channel_axis"] --> A{"channel_axis is not None and != 0?"}
A -- No --> Z["Use default channel_axis"]
A -- Yes --> B{"-x_dim <= channel_axis < x_dim?"}
B -- No --> C["Raise ValueError: Invalid channel axis"]
B -- Yes --> D{"channel_axis < 0?"}
D -- Yes --> E["channel_axis = x_dim + channel_axis<br/>(convert negative to positive index)"]
D -- No --> F["Keep channel_axis as is"]
E --> G["Proceed with pooling using resolved channel_axis"]
F --> G
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fresh review of
brainpy/dnn. Three Medium fixes:running_var; now applies Bessel'sN/(N-1)correction for the running buffer (PyTorch-consistent), keeping biased variance for in-batch normalization.channel_axis(== -x_dim) due to anabs()bound; widened to-x_dim <= axis < x_dim(Pool, _MaxPoolNd, AdaptivePool).', '.join(<ints>)->TypeErrormasking the intendedValueError.Prior-audit GroupNorm/InstanceNorm + affine-norm findings verified already-fixed. Regression tests added (589 passed in-scope). Findings:
docs/issues-found-20260619-dnn.md.Summary by Sourcery
Fix dnn normalization and pooling edge-case bugs and document the audit findings for these modules.
Bug Fixes:
Documentation:
Tests: