Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(layers): Fix incorrect masked mean/variance in BatchNormalization layer #20815

Merged

Conversation

harshaljanjani
Copy link
Contributor

… layer

Update masked moments calculation to properly account for broadcast dimensions when summing mask weights.

Added test to verify broadcast mask handling produces zero-centered outputs.
@codecov-commenter
Copy link

codecov-commenter commented Jan 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.01%. Comparing base (734cd03) to head (1db8cc0).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #20815      +/-   ##
==========================================
- Coverage   82.01%   82.01%   -0.01%     
==========================================
  Files         558      559       +1     
  Lines       52182    52291     +109     
  Branches     8067     8084      +17     
==========================================
+ Hits        42795    42884      +89     
- Misses       7421     7431      +10     
- Partials     1966     1976      +10     
Flag Coverage Δ
keras 81.82% <100.00%> (-0.01%) ⬇️
keras-jax 64.27% <100.00%> (+0.02%) ⬆️
keras-numpy 59.00% <100.00%> (+0.02%) ⬆️
keras-openvino 29.83% <100.00%> (-0.02%) ⬇️
keras-tensorflow 64.80% <100.00%> (+0.01%) ⬆️
keras-torch 64.19% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@harshaljanjani
Copy link
Contributor Author

harshaljanjani commented Jan 27, 2025

The error is in the line in ops.mean() for the OpenVINO backend:
axis_const = ov_opset.constant(axis, dtype=Type.i32).output(0)
Until further information is available, I will mark the test case as skipped for the OpenVINO backend.
Edit: Fixed.

- Convert tuple reduction axes to list format for compatibility with OpenVINO's constant op

- Remove OpenVINO skip decorator after fixing axis format
Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

keras/src/layers/normalization/batch_normalization.py Outdated Show resolved Hide resolved
Avoid repeated type checks and conversions during forward pass.
@harshaljanjani
Copy link
Contributor Author

harshaljanjani commented Jan 28, 2025

@fchollet I know it's been a long PR with the RandomChoice and RandomApply layers, but it's my first month contributing, and I've learned a lot about src/layers/preprocessing during the migration process. It's alright if you don't want those layers in the codebase, but I'll fix the reviews and re-request a review anyway, thanks!
Just let me know if there's any project you're looking for contributors on, I’d love to learn and contribute!

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

LGTM, thank you

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jan 28, 2025
@fchollet fchollet merged commit 871ad7a into keras-team:master Jan 28, 2025
7 checks passed
@google-ml-butler google-ml-butler bot removed awaiting review ready to pull Ready to be merged into the codebase kokoro:force-run labels Jan 28, 2025
@harshaljanjani harshaljanjani deleted the batchnorm-mean-variance-fix branch January 28, 2025 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BatchNormalization gives incorrect output with masked inputs > 3 dimensions
4 participants