forked from vllm-project/llm-compressor
-
Notifications
You must be signed in to change notification settings - Fork 1
Refine ar doc local #16
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
Closed
Closed
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
fe69212
move w4a16 example
yiliu30 833bf0d
add overall readme
yiliu30 68ba0b0
add tips(wip)
yiliu30 7c26466
update docs
yiliu30 56a97cf
update
yiliu30 847c45c
fix
yiliu30 6487a03
correct typo
yiliu30 5e858ca
fix
yiliu30 c349382
correct
yiliu30 bba90c1
clean
yiliu30 6f6924b
refine
yiliu30 d1a6278
update
yiliu30 637af92
fix
yiliu30 cb09b40
update
yiliu30 caec29c
update
yiliu30 99f99ca
update
yiliu30 e2dd02b
update
yiliu30 095c1db
fix
yiliu30 0f2424e
fix typo
yiliu30 155a2c0
Merge branch 'main' into refine-ar-doc
yiliu30 2d8a6c1
merge main
yiliu30 3393466
update
yiliu30 985ee41
fix
yiliu30 3ecd09a
update
yiliu30 538ede8
move qwen3 to w4a16
yiliu30 ee8b4b8
merge main
yiliu30 253186d
update
yiliu30 6e86d4b
update
yiliu30 fc4a5ef
update
yiliu30 06a5efb
fix
yiliu30 d43c61b
fix typo
yiliu30 ac6ab30
fix
yiliu30 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # `AutoRound` Quantization | ||
|
|
||
| `llm-compressor` supports [AutoRound](https://aclanthology.org/2024.findings-emnlp.662.pdf), an advanced quantization technique that delivers **high-accuracy**, **low-bit quantization**. The quantized results are fully compatible with `compressed-tensors` and can be served directly with vLLM. | ||
|
|
||
| AutoRound introduces three trainable parameters (V, α, and β) to optimize rounding values and clipping ranges during quantization. The method processes each decoder layer sequentially, using block-wise output reconstruction error as the training objective to fine-tune these parameters. This approach combines the efficiency of post-training quantization with the adaptability of parameter tuning, delivering robust compression for large language models while maintaining strong performance. | ||
|
|
||
| ## Installation | ||
|
|
||
| To get started, install: | ||
|
|
||
| ```bash | ||
| git clone https://github.com/vllm-project/llm-compressor.git | ||
| cd llm-compressor | ||
| pip install -e . | ||
| ``` | ||
|
|
||
| ## Quickstart | ||
|
|
||
| The example includes an end-to-end script for applying the AutoRound quantization algorithm. | ||
|
|
||
| ```bash | ||
| python3 llama3_example.py | ||
| ``` | ||
|
|
||
| The resulting model `Meta-Llama-3-8B-Instruct-W4A16-G128-AutoRound` is ready to be loaded into vLLM. | ||
|
|
||
| ## Code Walkthrough | ||
|
|
||
| Now, we will step through the code in the example. There are four steps: | ||
| 1) Load model | ||
| 2) Prepare calibration data | ||
| 3) Apply quantization | ||
| 4) Evaluate accuracy in vLLM | ||
|
|
||
| ### 1) Load Model | ||
|
|
||
| Load the model using `AutoModelForCausalLM` for handling quantized saving and loading. | ||
|
|
||
| ```python | ||
| from transformers import AutoTokenizer, AutoModelForCausalLM | ||
|
|
||
| MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct" | ||
| model = AutoModelForCausalLM.from_pretrained(MODEL_ID, dtype="auto") | ||
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
| ``` | ||
|
|
||
| ### 2) Prepare Calibration Data | ||
|
|
||
| When quantizing model weights with AutoRound, you’ll need a small set of sample data to run the algorithm. By default, we are using [NeelNanda/pile-10k](https://huggingface.co/datasets/NeelNanda/pile-10k) as our calibration dataset. | ||
| Recommended starting points: | ||
| - 128 samples — typically sufficient for stable calibration (increase if accuracy degrades). | ||
| - 2048 sequence length — a good baseline for most LLMs. | ||
| - 200 tuning steps — usually enough to converge (increase if accuracy drops). | ||
|
|
||
| ```python | ||
| # Select calibration dataset. | ||
| from auto_round.calib_dataset import get_dataset | ||
|
|
||
| NUM_CALIBRATION_SAMPLES = 128 | ||
| MAX_SEQUENCE_LENGTH = 2048 | ||
|
|
||
| # Get aligned calibration dataset. | ||
| ds = get_dataset( | ||
| tokenizer=tokenizer, | ||
| seqlen=MAX_SEQUENCE_LENGTH, | ||
| nsamples=NUM_CALIBRATION_SAMPLES, | ||
| ) | ||
| ``` | ||
|
|
||
| ### 3) Apply Quantization | ||
|
|
||
| With the dataset ready, we will now apply AutoRound quantization to the model. | ||
|
|
||
| ```python | ||
| from llmcompressor import oneshot | ||
| from llmcompressor.modifiers.autoround import AutoRoundModifier | ||
|
|
||
| # Configure the quantization algorithm to run. | ||
| recipe = AutoRoundModifier( | ||
| targets="Linear", scheme="W4A16", ignore=["lm_head"], iters=200 | ||
| ) | ||
|
|
||
| # Apply quantization. | ||
| oneshot( | ||
| model=model, | ||
| dataset=ds, | ||
| recipe=recipe, | ||
| max_seq_length=MAX_SEQUENCE_LENGTH, | ||
| num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||
| # disable shuffling to get slightly better mmlu score | ||
| shuffle_calibration_samples=False, | ||
| ) | ||
|
|
||
|
|
||
| # Save to disk compressed. | ||
| SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-W4A16-G128-AutoRound" | ||
| model.save_pretrained(SAVE_DIR, save_compressed=True) | ||
| tokenizer.save_pretrained(SAVE_DIR) | ||
| ``` | ||
|
|
||
| We have successfully created an `int4` model! | ||
|
|
||
| ### 4) Evaluate Accuracy | ||
|
|
||
| With the model created, we can now load and run in vLLM (after installing). | ||
|
|
||
| ```python | ||
| from vllm import LLM | ||
| model = LLM("./Meta-Llama-3-8B-Instruct-W4A16-G128-AutoRound") | ||
| ``` | ||
|
|
||
| We can evaluate accuracy with `lm_eval` (`pip install lm-eval==0.4.9.1`): | ||
| > Note: quantized models can be sensitive to the presence of the `bos` token. `lm_eval` does not add a `bos` token by default, so make sure to include the `add_bos_token=True` argument when running your evaluations. | ||
|
|
||
| Run the following to test accuracy on GSM-8K: | ||
|
|
||
| ```bash | ||
| lm_eval --model vllm \ | ||
| --model_args pretrained="./Meta-Llama-3-8B-Instruct-W4A16-G128-AutoRound",add_bos_token=true \ | ||
| --tasks gsm8k \ | ||
| --num_fewshot 5 \ | ||
| --limit 1000 \ | ||
| --batch_size 'auto' | ||
| ``` | ||
|
|
||
| We can see the resulting scores look good! | ||
|
|
||
| ```bash | ||
| | Tasks | Version | Filter | n-shot | Metric | | Value | | Stderr | | ||
| | ----- | ------: | ---------------- | -----: | ----------- | --- | ----: | --- | -----: | | ||
| | gsm8k | 3 | flexible-extract | 5 | exact_match | ↑ | 0.737 | ± | 0.0139 | | ||
| | | | strict-match | 5 | exact_match | ↑ | 0.736 | ± | 0.0139 | | ||
| ``` | ||
| > Note: quantized model accuracy may vary slightly due to nondeterminism. | ||
|
|
||
|
|
||
| ### Questions or Feature Request? | ||
yiliu30 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Please open up an issue on [vllm-project/llm-compressor](https://github.com/vllm-project/llm-compressor) or [intel/auto-round](https://github.com/intel/auto-round). | ||
File renamed without changes.
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.