-
Notifications
You must be signed in to change notification settings - Fork 324
[JAX] Add AbsMaxObserver #2585
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
Open
wpietka
wants to merge
5
commits into
main
Choose a base branch
from
dev/wpietkax/absmax-observer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−2
Open
[JAX] Add AbsMaxObserver #2585
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #!/usr/bin/env python | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) 2026 Intel Corporation | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Tests for activation observers used during static quantization calibration. | ||
|
|
||
| Verifies that AbsMaxObserver tracks the maximum absolute value, and that eligible | ||
| layers select the correct observer: MinMaxObserver only for asymmetric integer | ||
| quantization, and AbsMaxObserver otherwise (fp8 and symmetric int8). | ||
| """ | ||
|
|
||
| import pytest | ||
| from jax import numpy as jnp | ||
|
|
||
| from neural_compressor.jax.quantization.layers_static import ( | ||
| AbsMaxObserver, | ||
| MinMaxObserver, | ||
| StaticQDQLayer, | ||
| get_activation_observer, | ||
| ) | ||
|
|
||
| # Mark all tests in this file as smoke tests | ||
| pytestmark = pytest.mark.smoke_test | ||
|
|
||
|
|
||
| def test_abs_max_observer_tracks_max_abs(): | ||
| """AbsMaxObserver records the running maximum absolute value across calls.""" | ||
| observer = AbsMaxObserver(dtype="float32") | ||
|
|
||
| assert not observer.is_calibrated() | ||
|
|
||
| observer(jnp.array([1.0, -2.0, 0.5], dtype=jnp.float32)) | ||
| observer(jnp.array([-5.0, 3.0], dtype=jnp.float32)) | ||
| observer(jnp.array([4.0, -1.0], dtype=jnp.float32)) | ||
|
|
||
| assert observer.is_calibrated() | ||
|
|
||
| calibrated_range = observer.get_calibrated_range() | ||
| # Only the maximum absolute value is returned | ||
| assert float(calibrated_range) == pytest.approx(5.0) | ||
|
|
||
|
|
||
| def test_abs_max_observer_passthrough(): | ||
| """AbsMaxObserver returns its inputs unchanged.""" | ||
| observer = AbsMaxObserver(dtype="float32") | ||
| inputs = jnp.array([1.0, -2.0, 3.0], dtype=jnp.float32) | ||
| outputs = observer(inputs) | ||
| assert jnp.array_equal(inputs, outputs) | ||
|
|
||
|
|
||
| def test_abs_max_observer_respects_mask(): | ||
| """AbsMaxObserver ignores masked-out positions.""" | ||
| observer = AbsMaxObserver(dtype="float32") | ||
| inputs = jnp.array([[1.0, -9.0, 2.0]], dtype=jnp.float32) | ||
| mask = jnp.array([[True, False, True]]) | ||
| observer(inputs, mask=mask) | ||
| calibrated_range = observer.get_calibrated_range() | ||
| # The masked -9.0 must be ignored, so max abs is 2.0 | ||
| assert float(calibrated_range) == pytest.approx(2.0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "activation_dtype,asymmetric,expected", | ||
| [ | ||
| (jnp.dtype("float8_e4m3fn"), False, AbsMaxObserver), | ||
| (jnp.dtype("float8_e5m2"), False, AbsMaxObserver), | ||
| (jnp.dtype("float8_e4m3fn"), True, AbsMaxObserver), # fp8 is always symmetric | ||
| (jnp.dtype("int8"), False, AbsMaxObserver), # symmetric int8 | ||
| (jnp.dtype("int8"), True, MinMaxObserver), # asymmetric int8 | ||
| ], | ||
| ) | ||
| def test_get_activation_observer_selection(activation_dtype, asymmetric, expected): | ||
| """The helper selects MinMaxObserver only for asymmetric integer quantization.""" | ||
| observer = get_activation_observer(activation_dtype, asymmetric, dtype_policy="float32") | ||
| assert isinstance(observer, expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "activation_dtype,asymmetric,expected", | ||
| [ | ||
| (jnp.dtype("float8_e4m3fn"), False, AbsMaxObserver), | ||
| (jnp.dtype("int8"), False, AbsMaxObserver), | ||
| (jnp.dtype("int8"), True, MinMaxObserver), | ||
| ], | ||
| ) | ||
| def test_static_qdq_layer_uses_expected_observer(activation_dtype, asymmetric, expected): | ||
| """StaticQDQLayer attaches the observer that matches its quantization scheme.""" | ||
| layer = StaticQDQLayer( | ||
| name="static_qdq", | ||
| activation_dtype=activation_dtype, | ||
| dtype="float32", | ||
| asymmetric=asymmetric, | ||
| ) | ||
| layer.add_observers() | ||
| assert isinstance(layer.input_observer, expected) | ||
|
|
||
|
|
||
| def test_static_qdq_layer_no_observer_with_fixed_range(): | ||
| """No observer is attached when a fixed range is provided.""" | ||
| layer = StaticQDQLayer( | ||
| name="static_qdq_fixed", | ||
| activation_dtype=jnp.dtype("float8_e4m3fn"), | ||
| dtype="float32", | ||
| fixed_range=(-3.0, 3.0), | ||
| ) | ||
| layer.add_observers() | ||
| assert not hasattr(layer, "input_observer") |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.