-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new config items and support smooth quant (#514)
Signed-off-by: wenhuach21 <wenhua.cheng@intel.com> Signed-off-by: Mengni Wang <mengni.wang@intel.com>
- Loading branch information
1 parent
5c00d63
commit cbb5cf5
Showing
15 changed files
with
871 additions
and
209 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,63 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021 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. | ||
|
||
"""Build SmoothQuant algorithm class.""" | ||
|
||
import numpy as np | ||
from .algorithm import Algorithm, algorithm_registry | ||
from ..utils import logger | ||
|
||
@algorithm_registry(algorithm_type='smooth_quant') | ||
class SmoothQuant(Algorithm): | ||
"""SmoothQuant algorithm class.""" | ||
def __init__(self, percentile=99.999, op_types=['MatMul', 'Linear', 'Conv'], | ||
scales_per_op=True): | ||
"""Initialize SmoothQuant class. | ||
Args: | ||
percentile:Percentile of calibration to remove outliers | ||
op_types: The op types whose input tensor will be dumped | ||
scales_per_op: True, each op will have an individual scale, mainly for accuracy | ||
False, ops with the same input will share a scale, mainly for performance | ||
""" | ||
self.percentile = percentile | ||
self.op_types = op_types | ||
self.scales_per_op = scales_per_op | ||
self.alpha = 1.0 | ||
self.tune_cfg = None | ||
|
||
def __call__(self, origin_model, q_model, adaptor, dataloader, iterations): | ||
"""Return the processed model via SmoothQuant algorithm. | ||
Fake input channel quantization, for more details please refer to: | ||
[1] SmoothQuant: Accurate and Efficient Post-Training Quantization for Large Language Models | ||
[2] SPIQ: Data-Free Per-Channel Static Input Quantization | ||
inert Mul op before each conv/matmul with adjusted weights | ||
Args: | ||
origin_model: origin_model | ||
q_model: q_model | ||
adaptor: adaptor | ||
dataloader: dataloader | ||
iterations: iterations | ||
Returns: | ||
model: A modified onnx model | ||
""" | ||
q_model = adaptor.smooth_quant(origin_model, dataloader, iterations, self.tune_cfg, self.alpha, | ||
self.percentile, self.op_types, self.scales_per_op) | ||
return q_model |
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
Oops, something went wrong.