-
Notifications
You must be signed in to change notification settings - Fork 283
add export funcs for autoround and fix incorrect hype-parameters in example #1536
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
de3fe48
add export funcs for autoround
WeiweiZhang1 1454c37
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fbfd8c2
fixtypo
WeiweiZhang1 261032b
Merge branch 'autoround/enable_optimum_format_export' of https://gith…
WeiweiZhang1 10c80b7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b716464
fixed incorrect default hyperparameters
wenhuach21 8a248a6
fixed mixstral7b*8 issue
wenhuach21 d0f645c
port export func to file
WeiweiZhang1 ab12cf1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0794fe0
add export
WeiweiZhang1 880669f
Merge branch 'autoround/enable_optimum_format_export' of https://gith…
WeiweiZhang1 bbe009a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7c48226
fixtypo
WeiweiZhang1 3276e5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 97773ee
change device type
WeiweiZhang1 722f2fc
Merge branch 'autoround/enable_optimum_format_export' of https://gith…
WeiweiZhang1 c5e848e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
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,99 @@ | ||
| # 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. | ||
|
|
||
| import copy | ||
| import json | ||
| from typing import Union | ||
|
|
||
| try: | ||
| from neural_compressor.utils.utility import LazyImport | ||
|
|
||
| torch = LazyImport("torch") | ||
| from neural_compressor.utils import logger | ||
| except: # pragma: no cover | ||
| import logging | ||
|
|
||
| import torch | ||
|
|
||
| logger = logging.getLogger() | ||
|
|
||
|
|
||
| def export_compressed_model( | ||
| model, | ||
| weight_config: Union[str, dict], | ||
| enable_full_range=False, | ||
| compression_dtype=torch.int32, | ||
| compression_dim=1, | ||
| scale_dtype=torch.float32, | ||
| device="cpu", | ||
| use_optimum_format=True, | ||
| ): | ||
| """Convert Linear to WeightOnlyLinear for low memory inference. | ||
| Args: | ||
| weight_config (str|dict): qconfig dict or Path of qconfig.json. | ||
| enable_full_range (bool, optional): Whether to leverage the full compression range | ||
| under symmetric quantization. Defaults to False. | ||
| compression_dtype (torch.Tensor, optional): The target dtype after comoression. | ||
| Defaults to torch.int32. | ||
| compression_dim (int, optional): Select from [0, 1], 0 is output channel, | ||
| 1 is input channel. Defaults to 1. | ||
| scale_dtype (torch.Tensor, optional): Use float32 or float16. | ||
| Defaults to torch.float32. | ||
| device (str, optional): choose device for compression. Defaults to cpu. | ||
| use_optimum_format (bool, optional): use the popular huggingface compression format. | ||
| 1: compression_dim: weight = 1, zeros = 0 and both are transposed. | ||
| 2: zeros -= 1 before compression. Why we need it? | ||
| 3: g_idx: use same number for one group instead of recording the channel order. | ||
| 4. parameter name changed, such as 'packed_weight' -> 'qweight'. | ||
| 5. zeros is always needed even for sym. | ||
| """ | ||
| from .autoround import get_module, quant_weight_w_scale, set_module | ||
| from .model_wrapper import WeightOnlyLinear | ||
|
|
||
| compressed_model = copy.deepcopy(model) | ||
WeiweiZhang1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if isinstance(weight_config, str): | ||
| with open(weight_config, "r") as f: | ||
| q_config = json.load(f) | ||
| else: | ||
| q_config = weight_config | ||
| for k, v in q_config.items(): | ||
| logger.info(f"Compressing {k} on device {device}") | ||
| if v["data_type"] == "float": | ||
| continue | ||
| else: | ||
| dtype = v["data_type"] | ||
| num_bits = v["bits"] | ||
| group_size = v["group_size"] | ||
| scheme = v["scheme"] | ||
| m = get_module(compressed_model, k) | ||
| fp_weight = m.weight.data | ||
| scale = torch.tensor(v["scale"], dtype=torch.float32) # may exist dtype dismatch problem | ||
| zp = None if scheme == "sym" else torch.tensor(v["zp"], dtype=torch.int32) | ||
| int_weight = quant_weight_w_scale(fp_weight, scale, zp, group_size) | ||
| int_weight = int_weight.type(torch.int32) | ||
| new_module = WeightOnlyLinear( | ||
| m.in_features, | ||
| m.out_features, | ||
| num_bits, | ||
| group_size, | ||
| dtype=dtype, | ||
| zp=zp is not None, | ||
| bias=m.bias is not None, | ||
| device=device, | ||
| use_optimum_format=True, | ||
| ) | ||
| new_module.pack(int_weight, scale, zp, m.bias) | ||
| set_module(compressed_model, k, new_module) | ||
| return compressed_model | ||
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.