Repository navigation
Fix StdScaler broadcasting with keepdim disabled - #3370
Draft
linjiaye929 wants to merge 1 commit into
Draft
linjiaye929 wants to merge 1 commit into
linjiaye929 wants to merge 1 commit into
Conversation
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.
Issue #, if available: No existing issue found for this broadcasting defect.
Description of changes:
StdScalerdefaults tokeepdim=False, but its reduced loc and scale are subtracted/divided directly into unreduced data. For a (2, 3) input this raises a size-mismatch error; square inputs silently broadcast along the wrong axis and compute an incorrect variance.Keep the reduction dimension internally for weighted centering, variance and normalization, then squeeze only returned loc and scale when requested. The keepdim=True behavior used by built-in model callers is preserved, as are float64 variance accumulation, output dtype and minimum scale.
Regression tests cover rectangular and square inputs, partial observations, float32/float64, positive/negative reduction axes and multivariate inputs. The original implementation fails the rectangular/default and silent square cases.
Validation on Python 3.12.14, PyTorch 2.10.0+cpu:
CUDA, the full dependency/Python matrix, non-torch tests and upstream CI have not been verified. This is a draft for review, not a claim of CI approval.
AI disclosure: this fix, tests and explanation were prepared and checked with an OpenAI AI coding assistant.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Requested release label: bug fix