forked from vllm-project/llm-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quatization lifecycle - disable forward pass override + helper for we…
…ight quant param updates (vllm-project#111)
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
from .initialize import * | ||
from .compressed import * | ||
from .apply import * | ||
from .helpers import * |
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,53 @@ | ||
# 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. | ||
|
||
""" | ||
Miscelaneous helpers for the quantization lifecycle | ||
""" | ||
|
||
|
||
from torch.nn import Module | ||
|
||
|
||
__all__ = [ | ||
"update_layer_weight_quant_params", | ||
"enable_quantization", | ||
"disable_quantization", | ||
] | ||
|
||
|
||
def update_layer_weight_quant_params(layer: Module): | ||
weight = getattr(layer, "weight", None) | ||
scale = getattr(layer, "weight_scale", None) | ||
zero_point = getattr(layer, "weight_zero_point", None) | ||
observer = getattr(layer, "weight_observer", None) | ||
|
||
if weight is None or observer is None or scale is None or zero_point is None: | ||
# scale, zp, or observer not calibratable or weight not available | ||
return | ||
|
||
updated_scale, updated_zero_point = observer(weight) | ||
|
||
# update scale and zero point | ||
device = next(layer.parameters()).device | ||
scale.data = updated_scale.to(device) | ||
zero_point.data = updated_zero_point.to(device) | ||
|
||
|
||
def enable_quantization(module: Module): | ||
module.quantization_enabled = True | ||
|
||
|
||
def disable_quantization(module: Module): | ||
module.quantization_enabled = False |
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,57 @@ | ||
# 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 copy import deepcopy | ||
|
||
import torch | ||
from compressed_tensors.quantization import ( | ||
QuantizationConfig, | ||
apply_quantization_config, | ||
disable_quantization, | ||
enable_quantization, | ||
) | ||
from torch.nn import Linear | ||
|
||
|
||
def test_quantization_enabled_disabled(): | ||
inp = torch.randn(16) | ||
model = Linear(16, 16) | ||
quantized_model = deepcopy(model) | ||
apply_quantization_config( | ||
model=quantized_model, | ||
config=QuantizationConfig( | ||
config_groups=dict(W4A16=["Linear"]), | ||
quantization_status="calibration", | ||
), | ||
) | ||
|
||
# run one calibration pass | ||
quantized_model(inp) | ||
|
||
model_output = model(inp) | ||
quantized_model_output = quantized_model(inp) | ||
|
||
# quantized and non quantized outputs should be different | ||
assert not torch.all(model_output == quantized_model_output) | ||
|
||
# disable quantization | ||
quantized_model.apply(disable_quantization) | ||
# check that quantized model now matches model output | ||
assert torch.all(model_output == quantized_model(inp)) | ||
|
||
# re-enable quantization | ||
quantized_model.apply(enable_quantization) | ||
# check that quantized model matches original quantized output | ||
assert torch.all(quantized_model_output == quantized_model(inp)) |