-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add save&load API to SmoothQuant ipex model (#1673)
Signed-off-by: Cheng, Zixuan <[email protected]>
- Loading branch information
1 parent
e81a2dd
commit 9c6102b
Showing
7 changed files
with
207 additions
and
7 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
74 changes: 74 additions & 0 deletions
74
neural_compressor/torch/algorithms/smooth_quant/save_load.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,74 @@ | ||
# Copyright (c) 2024 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. | ||
|
||
# pylint:disable=import-error | ||
import torch | ||
|
||
try: | ||
import intel_extension_for_pytorch as ipex | ||
except: | ||
assert False, "Please install IPEX for smooth quantization." | ||
|
||
from neural_compressor.torch.algorithms.static_quant import load, save | ||
|
||
|
||
def recover_model_from_json(model, json_file_path, example_inputs): # pragma: no cover | ||
"""Recover ipex model from JSON file. | ||
Args: | ||
model (object): fp32 model need to do quantization. | ||
json_file_path (json): configuration JSON file for ipex. | ||
example_inputs (tuple or torch.Tensor or dict): example inputs that will be passed to the ipex function. | ||
Returns: | ||
(object): quantized model | ||
""" | ||
from torch.ao.quantization.observer import MinMaxObserver | ||
|
||
if ipex.__version__ >= "2.1.100": | ||
qconfig = ipex.quantization.get_smooth_quant_qconfig_mapping(alpha=0.5, act_observer=MinMaxObserver) | ||
else: | ||
qconfig = ipex.quantization.get_smooth_quant_qconfig_mapping(alpha=0.5, act_observer=MinMaxObserver()) | ||
if isinstance(example_inputs, dict): | ||
model = ipex.quantization.prepare(model, qconfig, example_kwarg_inputs=example_inputs, inplace=True) | ||
else: | ||
model = ipex.quantization.prepare(model, qconfig, example_inputs=example_inputs, inplace=True) | ||
|
||
model.load_qconf_summary(qconf_summary=json_file_path) | ||
model = ipex.quantization.convert(model, inplace=True) | ||
with torch.no_grad(): | ||
try: | ||
if isinstance(example_inputs, dict): | ||
# pylint: disable=E1120,E1123 | ||
model = torch.jit.trace(model, example_kwarg_inputs=example_inputs) | ||
else: | ||
model = torch.jit.trace(model, example_inputs) | ||
model = torch.jit.freeze(model.eval()) | ||
except: | ||
if isinstance(example_inputs, dict): | ||
# pylint: disable=E1120,E1123 | ||
model = torch.jit.trace(model, example_kwarg_inputs=example_inputs, strict=False, check_trace=False) | ||
else: | ||
model = torch.jit.trace(model, example_inputs, strict=False) | ||
model = torch.jit.freeze(model.eval()) | ||
if isinstance(example_inputs, dict): | ||
model(**example_inputs) | ||
model(**example_inputs) | ||
elif isinstance(example_inputs, tuple) or isinstance(example_inputs, list): | ||
model(*example_inputs) | ||
model(*example_inputs) | ||
else: | ||
model(example_inputs) | ||
model(example_inputs) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
|
||
from .utility import * | ||
from .static_quant import static_quantize | ||
from .save_load import save, load |
48 changes: 48 additions & 0 deletions
48
neural_compressor/torch/algorithms/static_quant/save_load.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,48 @@ | ||
# Copyright (c) 2024 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. | ||
|
||
# pylint:disable=import-error | ||
import json | ||
import os | ||
|
||
import torch | ||
|
||
try: | ||
import intel_extension_for_pytorch as ipex | ||
except: | ||
assert False, "Please install IPEX for static quantization." | ||
|
||
from neural_compressor.torch.utils import QCONFIG_NAME, WEIGHT_NAME, logger | ||
|
||
|
||
def save(model, output_dir="./saved_results"): | ||
if not os.path.exists(output_dir): | ||
os.mkdir(output_dir) | ||
|
||
qmodel_file_path = os.path.join(os.path.abspath(os.path.expanduser(output_dir)), WEIGHT_NAME) | ||
qconfig_file_path = os.path.join(os.path.abspath(os.path.expanduser(output_dir)), QCONFIG_NAME) | ||
model.ori_save(qmodel_file_path) | ||
with open(qconfig_file_path, "w") as f: | ||
json.dump(model.tune_cfg, f, indent=4) | ||
|
||
logger.info("Save quantized model to {}.".format(qmodel_file_path)) | ||
logger.info("Save configuration of quantized model to {}.".format(qconfig_file_path)) | ||
|
||
|
||
def load(output_dir="./saved_results"): | ||
qmodel_file_path = os.path.join(os.path.abspath(os.path.expanduser(output_dir)), WEIGHT_NAME) | ||
model = torch.jit.load(qmodel_file_path) | ||
model = torch.jit.freeze(model.eval()) | ||
logger.info("Quantized model loading successful.") | ||
return 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
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