-
Notifications
You must be signed in to change notification settings - Fork 0
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
[DO NOT MERGE] per-token dynamic observer #24
Draft
bfineran
wants to merge
4
commits into
main
Choose a base branch
from
per-token-quant
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.
Draft
Changes from all commits
Commits
Show all changes
4 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 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 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 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 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 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 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,35 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
|
||
from compressed_tensors.quantization.observers.base import Observer | ||
from compressed_tensors.quantization.observers.memoryless import MemorylessObserver | ||
|
||
|
||
__all__ = ["DynamicObserver"] | ||
|
||
|
||
@Observer.register("dynamic") | ||
class DynamicObserver(MemorylessObserver): | ||
""" | ||
Values targted for a dyanmic observer do not require calibration, | ||
this observer will persist in the model through the lifecycle, calculating | ||
the quantization parameters on the fly for each observed Tensor. | ||
|
||
This base dynamic observer uses the `calculate_qparams` from MemorylessObserver | ||
where each scale and zero point is based solely on the currently observed | ||
Tensor. | ||
""" | ||
|
||
DYNAMIC = False |
70 changes: 70 additions & 0 deletions
70
src/compressed_tensors/quantization/observers/per_token.py
This file contains 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,70 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
|
||
from typing import Tuple | ||
|
||
import torch | ||
from compressed_tensors.quantization.observers.base import Observer | ||
from compressed_tensors.quantization.observers.helpers import calculate_qparams | ||
from compressed_tensors.quantization.quant_args import QuantizationArgs | ||
from torch import FloatTensor, IntTensor, Tensor | ||
|
||
|
||
__all__ = ["PerTokenObserver"] | ||
|
||
|
||
@Observer.register("per_token", alias="per_token_dynamic") | ||
class PerTokenObserver(Observer): | ||
""" | ||
Values targted for a dyanmic observer do not require calibration, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: spelling |
||
this observer will persist in the model through the lifecycle, calculating | ||
the quantization parameters on the fly for each observed Tensor. | ||
|
||
This base dynamic observer uses the `calculate_qparams` from MemorylessObserver | ||
where each scale and zero point is based solely on the currently observed | ||
Tensor. | ||
|
||
:param axis: axis that token dimension is expected to be in | ||
""" | ||
|
||
def __init__(self, quantization_args: QuantizationArgs, axis: int = 1): | ||
super().__init__(quantization_args=quantization_args) | ||
|
||
self.axis = 1 | ||
|
||
DYNAMIC = True | ||
|
||
def calculate_qparams(self, observed: Tensor) -> Tuple[FloatTensor, IntTensor]: | ||
""" | ||
:param observed: observed tensor to calculate quantization parameters for | ||
:return: tuple of scale and zero point derived from the observed tensor | ||
""" | ||
# reduce every dimension except token dimension | ||
reduce_dims = [idx for idx in range(observed.dim()) if idx != self.axis] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should not reduce along batch as well |
||
|
||
# return shape will be [1, ..., num_tokens, 1, ...] with same num dims | ||
min_vals = observed.amin(dim=reduce_dims, keepdim=True) | ||
max_vals = observed.amax(dim=reduce_dims, keepdim=True) | ||
|
||
# ensure zero is in the range | ||
min_vals = torch.min(min_vals, torch.zeros_like(min_vals)) | ||
max_vals = torch.max(max_vals, torch.zeros_like(max_vals)) | ||
|
||
# returned shape will match the min/max vals shape | ||
# since keepdim=True, the reduced dims will have their dims set to 1 | ||
# so scales and zero points should broadcast correctly along the | ||
# token axis | ||
# TODO: add test for the broadcast mentioned above | ||
return calculate_qparams(min_vals, max_vals, self.quantization_args) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are per token observers always dynamic?