-
Notifications
You must be signed in to change notification settings - Fork 281
Add layer wise quantization doc and ONNXRT example #1434
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
25 commits
Select commit
Hold shift + click to select a range
abb6312
update onnxrt woq example
yuwenzho 0655119
add layer-wise quantization example
yuwenzho d4e39e8
Merge branch 'master' into yuwenzho/woq_example_doc
yuwenzho ca67739
fix docstring
yuwenzho 54179d0
update README.md
yuwenzho ea2078e
add layer-wise quantization doc
yuwenzho 1acdc9c
update onnxrt lwq figure
yuwenzho a6a656b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7035187
update quantization_layer_wise.md
yuwenzho 66be652
update ox_utils
yuwenzho 785f6a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ae13425
Merge branch 'master' into yuwenzho/lwq_example_doc
yuwenzho de59987
fix import bug
yuwenzho 595007b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 966aa9b
Update run_quant.sh
yuwenzho 1f10a92
fix import bug
yuwenzho 472ebbe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 23ce23b
update requirement
yuwenzho da05ba4
update README and doc
yuwenzho e3116d9
update onnx_model.py
yuwenzho cf126ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0ccb151
Merge branch 'master' into yuwenzho/lwq_example_doc
yuwenzho 449114f
update onnx_model.py
yuwenzho ed3f844
update main.py
yuwenzho 95ed5fd
update main.py
yuwenzho 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,98 @@ | ||
Layer Wise Quantization (LWQ) | ||
===== | ||
|
||
1. [Introduction](#introduction) | ||
|
||
2. [Supported Framework Model Matrix](#supported-framework-model-matrix) | ||
|
||
3. [Examples](#examples) | ||
|
||
## Introduction | ||
|
||
Large language models (LLMs) have shown exceptional performance across various tasks, meanwhile, the substantial parameter size poses significant challenges for deployment. Layer-wise quantization(LWQ) can greatly reduce the memory footprint of LLMs, usually 80-90% reduction, which means that users can quantize LLMs even on single node using GPU or CPU. We can quantize the model under memory-constrained devices, therefore making the huge-sized LLM quantization possible. | ||
|
||
<img src="./imgs/lwq.png" width=780 height=429> | ||
|
||
*Figure 1: The process of layer-wise quantization for PyTorch model. The color grey means empty parameters and the color blue represents parameters need to be quantized. Every rectangle inside model represents one layer.* | ||
|
||
<img src="./imgs/lwq_ort.png" width=900 height=400> | ||
|
||
*Figure 2: The process of layer-wise quantization for ONNX model. The graph of LLM is split into several parts, and each subgraph is quantized in turn.* | ||
|
||
## Supported Framework Model Matrix | ||
|
||
|
||
<table class="tg"> | ||
<thead> | ||
<tr> | ||
<th colspan="2" style="text-align:center;vertical-align:middle">Types/Framework</th> | ||
<th style="text-align:center;vertical-align:middle">PyTorch</th> | ||
<th style="text-align:center;vertical-align:middle">ONNX Runtime</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td style="text-align:center;vertical-align:middle" colspan="2">W8A8 Post Training Static Quantization</td> | ||
<td style="text-align:center;vertical-align:middle">✔</td> | ||
<td style="text-align:center;vertical-align:middle">✔</td> | ||
</tr> | ||
<tr> | ||
<td style="text-align:center;vertical-align:middle" rowspan="4">Weight-only Quantization</td> | ||
<td style="text-align:center;vertical-align:middle">RTN</td> | ||
<td style="text-align:center;vertical-align:middle">✔</td> | ||
<td style="text-align:center;vertical-align:middle" rowspan="4">✕</td> | ||
</tr> | ||
<tr> | ||
<td style="text-align:center;vertical-align:middle">AWQ</td> | ||
<td style="text-align:center;vertical-align:middle">✕</td> | ||
</tr> | ||
<tr> | ||
<td style="text-align:center;vertical-align:middle">GPTQ</td> | ||
<td style="text-align:center;vertical-align:middle">✔</td> | ||
</tr> | ||
<tr> | ||
<td style="text-align:center;vertical-align:middle">TEQ</td> | ||
<td style="text-align:center;vertical-align:middle">✕</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
## Examples | ||
|
||
#### PyTorch framework example | ||
|
||
```python | ||
from neural_compressor import PostTrainingQuantConfig, quantization | ||
from neural_compressor.adaptor.torch_utils.layer_wise_quant import load_empty_model | ||
|
||
fp32_model = load_empty_model(model_name_or_path, torchscript=True) | ||
conf = PostTrainingQuantConfig( | ||
approach="weight_only", | ||
recipes={ | ||
"layer_wise_quant": True, | ||
"rtn_args": {"enable_full_range": True}, | ||
}, | ||
) | ||
|
||
q_model = quantization.fit( | ||
fp32_model, | ||
conf, | ||
calib_dataloader=eval_dataloader, | ||
eval_func=lambda x: 0.1, | ||
) | ||
ouput_dir = "./saved_model" | ||
q_model.save(ouput_dir) | ||
q_model = load(ouput_dir, fp32_model, weight_only=True, layer_wise=True) | ||
``` | ||
|
||
#### ONNX Runtime framework example | ||
|
||
```python | ||
from neural_compressor import quantization, PostTrainingQuantConfig | ||
|
||
conf = PostTrainingQuantConfig(recipes={"layer_wise_quant": True}) | ||
q_model = quantization.fit(fp32_model_path, conf, calib_dataloader=dataloader) | ||
q_model.save(int8_model_path) | ||
``` | ||
|
||
Refer to [ONNX Runtime llama-2 LWQ example](../../examples/onnxrt/nlp/huggingface_model/text_generation/llama/quantization/weight_only) |
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
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
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.