Add keepdim support for AffineQuantizedMinMaxObserver#3748
Merged
jerryzh168 merged 1 commit intomainfrom Jan 29, 2026
Merged
Conversation
Summary: This PR adds a keepdim parameter to AffineQuantizedMinMaxObserver and related functions, allowing users to preserve dimensions in quantization parameters (scale/zero_point) that match the shape of min/max statistics. Motivation When computing quantization parameters, it's useful to maintain dimension alignment between min/max statistics and the resulting scale/zero_point tensors. This simplifies broadcasting operations and makes the tensors easier to work with in downstream quantization workflows. This for support per tensor float8 quantization, with 3D inputs Changes 1. Core Quantization Primitives - torchao/quantization/quant_primitives.py: - Added keepdim: bool = False parameter to choose_qparams_affine_with_min_max() - When keepdim=True, scale/zero_point retain the same shape as min_val/max_val 2. Observer Base Class - torchao/quantization/observer.py: - Added keepdim: bool = False parameter to AffineQuantizedObserverBase.__init__() - Stored as self.keepdim for use in derived classes 3. MinMax Observer - torchao/quantization/observer.py: - Updated AffineQuantizedMinMaxObserver.forward() to use keepdim=self.keepdim in torch.amin/amax calls - Updated AffineQuantizedMinMaxObserver.calculate_qparams() to pass keepdim to choose_qparams_affine_with_min_max() 4. Tests - test/quantization/test_observer.py: - Added test_keepdim_per_tensor(): Verifies keepdim behavior for per-tensor quantization - Added test_keepdim_per_axis(): Verifies keepdim behavior for per-axis quantization - Tests confirm that with keepdim=True, scale/zero_point shapes match min_val/max_val shapes Behavior With keepdim=False (default, backward compatible): min_val.shape = [] # scalar scale.shape = [] # scalar min_val.shape = [10] scale.shape = [10] With keepdim=True: min_val.shape = [1, 1] scale.shape = [1, 1] min_val.shape = [10, 1] scale.shape = [10, 1] Backward Compatibility ✅ Fully backward compatible - keepdim defaults to False, preserving existing behavior. Testing - Unit tests added for both PerTensor (both 2D and 3D inputs) and PerAxis granularities - Tests verify correct shapes and equivalent values between keepdim=True/False Test Plan: pytest test/quantization/test_observer.py -k keepdim Reviewers: Subscribers: Tasks: Tags: [ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/3748
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 3 PendingAs of commit 7b673a8 with merge base a003c32 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
jerryzh168
added a commit
that referenced
this pull request
Jan 28, 2026
Summary: This PR adds a keepdim parameter to AffineQuantizedMinMaxObserver and related functions, allowing users to preserve dimensions in quantization parameters (scale/zero_point) that match the shape of min/max statistics. Motivation When computing quantization parameters, it's useful to maintain dimension alignment between min/max statistics and the resulting scale/zero_point tensors. This simplifies broadcasting operations and makes the tensors easier to work with in downstream quantization workflows. This for support per tensor float8 quantization, with 3D inputs Changes 1. Core Quantization Primitives - torchao/quantization/quant_primitives.py: - Added keepdim: bool = False parameter to choose_qparams_affine_with_min_max() - When keepdim=True, scale/zero_point retain the same shape as min_val/max_val 2. Observer Base Class - torchao/quantization/observer.py: - Added keepdim: bool = False parameter to AffineQuantizedObserverBase.__init__() - Stored as self.keepdim for use in derived classes 3. MinMax Observer - torchao/quantization/observer.py: - Updated AffineQuantizedMinMaxObserver.forward() to use keepdim=self.keepdim in torch.amin/amax calls - Updated AffineQuantizedMinMaxObserver.calculate_qparams() to pass keepdim to choose_qparams_affine_with_min_max() 4. Tests - test/quantization/test_observer.py: - Added test_keepdim_per_tensor(): Verifies keepdim behavior for per-tensor quantization - Added test_keepdim_per_axis(): Verifies keepdim behavior for per-axis quantization - Tests confirm that with keepdim=True, scale/zero_point shapes match min_val/max_val shapes Behavior With keepdim=False (default, backward compatible): min_val.shape = [] # scalar scale.shape = [] # scalar min_val.shape = [10] scale.shape = [10] With keepdim=True: min_val.shape = [1, 1] scale.shape = [1, 1] min_val.shape = [10, 1] scale.shape = [10, 1] Backward Compatibility ✅ Fully backward compatible - keepdim defaults to False, preserving existing behavior. Testing - Unit tests added for both PerTensor (both 2D and 3D inputs) and PerAxis granularities - Tests verify correct shapes and equivalent values between keepdim=True/False Test Plan: pytest test/quantization/test_observer.py -k keepdim Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 04cb012 Pull Request resolved: #3748
howardzhang-cv
approved these changes
Jan 28, 2026
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.
Stack from ghstack (oldest at bottom):
Summary:
This PR adds a keepdim parameter to AffineQuantizedMinMaxObserver and related functions, allowing users to preserve dimensions in quantization parameters (scale/zero_point) that match the shape of min/max
statistics.
Motivation
When computing quantization parameters, it's useful to maintain dimension alignment between min/max statistics and the resulting scale/zero_point tensors. This simplifies broadcasting operations and makes the
tensors easier to work with in downstream quantization workflows.
This for support per tensor float8 quantization, with 3D inputs
Changes
Behavior
With keepdim=False (default, backward compatible):
min_val.shape = [] # scalar
scale.shape = [] # scalar
min_val.shape = [10]
scale.shape = [10]
With keepdim=True:
min_val.shape = [1, 1]
scale.shape = [1, 1]
min_val.shape = [10, 1]
scale.shape = [10, 1]
Backward Compatibility
✅ Fully backward compatible - keepdim defaults to False, preserving existing behavior.
Testing
Test Plan:
pytest test/quantization/test_observer.py -k keepdim
Reviewers:
Subscribers:
Tasks:
Tags: