-
Notifications
You must be signed in to change notification settings - Fork 463
[do-not-merge] examples][awq] Update AWQ examples to stacked recipe pattern #2460
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,39 @@ | ||
| # AWQ Quantization # | ||
| # MAI 2026 Efficient LLMs Challenge — Optimized On-Device Inference | ||
|
|
||
| Activation Aware Quantization (AWQ) is a state-of-the-art technique to quantize the weights of large language models which involves using a small calibration dataset to calibrate the model. The AWQ algorithm utilizes calibration data to derive scaling factors which reduce the dynamic range of weights while minimizing accuracy loss to the most salient weight values. | ||
| ## Architecture | ||
|
|
||
| The AWQ implementation found in LLM Compressor is derived from the pioneering work of [AutoAWQ](https://github.com/casper-hansen/AutoAWQ) and with assistance from its original maintainer, [@casper-hansen](https://github.com/casper-hansen). | ||
|
|
||
| ## AWQ Recipe ## | ||
|
|
||
| The AWQ recipe has been inferfaced as follows, where the `AWQModifier` adjusts model scales ahead of efficient weight quantization by the `QuantizationModifier` | ||
|
|
||
| ```python | ||
| recipe = [ | ||
| AWQModifier(ignore=["lm_head"], scheme="W4A16_ASYM", targets=["Linear"]), | ||
| ] | ||
| ``` | ||
|
|
||
| ## Compressing Your Own Model ## | ||
| To use your own model, start with an existing example change the `model_id` to match your own model stub. | ||
| ```python | ||
| model_id = "path/to/your/model" | ||
| model = AutoModelForCausalLM.from_pretrained(model_id, dtype="auto") | ||
| mai2026_efficient_llm/ | ||
| ├── kernels/ # C/NEON optimized compute kernels | ||
| │ ├── quantize.h # Quantization data structures | ||
| │ ├── gemv_neon.c # ARM NEON GEMV kernels (W4A8, W2A8, mixed) | ||
| │ ├── gemv_reference.c # Reference C kernels (Colab/x86 fallback) | ||
| │ └── Makefile # Cross-compile for ARM64 or native | ||
| ├── engine/ # Python inference engine | ||
| │ ├── __init__.py | ||
| │ ├── model_loader.py # Load & quantize HF models | ||
| │ ├── quantizer.py # Mixed-precision quantization with layer importance | ||
| │ ├── inference.py # Token-by-token generation with custom kernels | ||
| │ └── benchmark.py # Benchmarking utilities | ||
| ├── configs/ # Model & optimization configs | ||
| │ └── qwen2.5_0.5b.yaml | ||
| ├── scripts/ | ||
| │ ├── export_gguf.py # Export to GGUF for llama.cpp comparison | ||
| │ └── deploy_pi.sh # Pi 5 deployment script | ||
| ├── colab_demo.ipynb # Google Colab notebook (auto-generated) | ||
| ├── run_colab.py # Colab-compatible entry point | ||
| ├── run_pi5.py # Pi 5 optimized entry point | ||
| └── requirements.txt | ||
| ``` | ||
|
|
||
| ## Adding Mappings ## | ||
| In order to target weight and activation scaling locations within the model, the `AWQModifier` must be provided an AWQ mapping. For example, the AWQ mapping for the Llama family of models looks like this: | ||
|
|
||
| ```python | ||
| [ | ||
| AWQMapping( | ||
| "re:.*input_layernorm", | ||
| ["re:.*q_proj", "re:.*k_proj", "re:.*v_proj"], | ||
| ), | ||
| AWQMapping("re:.*v_proj", ["re:.*o_proj"]), | ||
| AWQMapping( | ||
| "re:.*post_attention_layernorm", | ||
| ["re:.*gate_proj", "re:.*up_proj"], | ||
| ), | ||
| AWQMapping( | ||
| "re:.*up_proj", | ||
| ["re:.*down_proj"], | ||
| ), | ||
| ] | ||
| ## Quick Start (Colab) | ||
| ```bash | ||
| pip install -r requirements.txt | ||
| python run_colab.py --model Qwen/Qwen2.5-0.5B-Instruct --bits 4 | ||
| ``` | ||
|
|
||
| Note: the mappings define which layers get smoothed whereas targets and ignore define which layers get quantized. So if you include a layer in the ignore list that is going to get matched due to the included mappings, it will get smoothed but not quantized. | ||
|
|
||
| To support other model families, you can supply your own mappings via the `mappings` argument with instantiating the `AWQModifier`, or you can add them to the registry [here](/src/llmcompressor/modifiers/awq/mappings.py) (contributions are welcome!) | ||
| ## Quick Start (Pi 5) | ||
| ```bash | ||
| cd kernels && make arm64 | ||
| cd .. && python run_pi5.py --model Qwen/Qwen2.5-0.5B-Instruct --bits 4 --threads 4 | ||
| ``` | ||
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,92 @@ | ||
| """ | ||
| AWQ + GPTQModifier: Stacked Recipe Example | ||
| ========================================== | ||
| Stacking AWQModifier with GPTQModifier combines AWQ's activation-aware | ||
| smoothing with GPTQ's second-order weight quantization for higher accuracy | ||
| at W4A16. | ||
|
|
||
| recipe = [ | ||
| AWQModifier(...), | ||
| GPTQModifier(...), | ||
| ] | ||
|
|
||
| AWQModifier runs first and re-scales weights so that quantization-sensitive | ||
| channels become easier for GPTQ to handle. | ||
| """ | ||
|
|
||
| from datasets import load_dataset | ||
| from transformers import AutoModelForCausalLM, AutoTokenizer | ||
|
|
||
| from llmcompressor import oneshot | ||
| from llmcompressor.modifiers.awq import AWQModifier | ||
| from llmcompressor.modifiers.quantization import GPTQModifier | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 1. Model | ||
| # --------------------------------------------------------------------------- | ||
| MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct" | ||
|
|
||
| model = AutoModelForCausalLM.from_pretrained(MODEL_ID, dtype="auto") | ||
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 2. Calibration dataset | ||
| # --------------------------------------------------------------------------- | ||
| DATASET_ID = "neuralmagic/calibration" | ||
| NUM_CALIBRATION_SAMPLES = 512 | ||
| MAX_SEQUENCE_LENGTH = 2048 | ||
|
|
||
| ds = load_dataset(DATASET_ID, name="LLM", split=f"train[:{NUM_CALIBRATION_SAMPLES}]") | ||
| ds = ds.shuffle(seed=42) | ||
|
|
||
|
|
||
| def preprocess(example): | ||
| return { | ||
| "text": tokenizer.apply_chat_template( | ||
| example["messages"], | ||
| tokenize=False, | ||
| add_generation_prompt=False, | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| ds = ds.map(preprocess, remove_columns=ds.column_names) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 3. Recipe: AWQ smoothing pass → GPTQ weight quantization | ||
| # | ||
| # AWQModifier : activation-aware smoothing (scale search uses scheme args). | ||
| # GPTQModifier : Hessian-based weight quantization on the smoothed model. | ||
| # | ||
| # Both modifiers must agree on scheme / targets / ignore. | ||
| # --------------------------------------------------------------------------- | ||
| recipe = [ | ||
| AWQModifier( | ||
| ignore=["lm_head"], | ||
| scheme="W4A16_ASYM", | ||
| targets=["Linear"], | ||
| ), | ||
| GPTQModifier( | ||
| scheme="W4A16_ASYM", | ||
| targets=["Linear"], | ||
| ignore=["lm_head"], | ||
| dampening_frac=0.01, | ||
| ), | ||
| ] | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # 4. Apply | ||
| # --------------------------------------------------------------------------- | ||
| OUTPUT_DIR = MODEL_ID.split("/")[-1] + "-AWQ-GPTQ-W4A16" | ||
|
|
||
| oneshot( | ||
| model=model, | ||
| tokenizer=tokenizer, | ||
| dataset=ds, | ||
| recipe=recipe, | ||
| output_dir=OUTPUT_DIR, | ||
| max_seq_length=MAX_SEQUENCE_LENGTH, | ||
| num_calibration_samples=NUM_CALIBRATION_SAMPLES, | ||
| ) | ||
|
|
||
| print(f"\nSaved to: {OUTPUT_DIR}") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The content of this README appears to have been replaced with information from an unrelated project ("MAI 2026 Efficient LLMs Challenge"). The documentation should be updated to reflect the new stacked recipe pattern for AWQ as demonstrated in the examples, rather than being replaced with this content.