Repository navigation
Added support of 2-3 bit symmetric mode to LoRA-QAT #4158
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
Open
andreyanufr
wants to merge
27
commits into
openvinotoolkit:develop
Choose a base branch
from
andreyanufr:aanuf/2_3_bit_training
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
32e2e5d
Added rapcking of weights to support torch compression for 2-3 bit in…
andreyanufr 029d4cf
1) Removed debug code.
andreyanufr 03ceee7
Fixed style bugs.
andreyanufr db5a21a
Added test for weight repacking.
andreyanufr 707c35f
Added test for weight repacking.
andreyanufr eeb5572
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr 2c45ac3
Train 2-3 bit quantizers.
andreyanufr b0f0240
Revert experimental changes.
andreyanufr ab544bb
Fixed style and removed unused code.
andreyanufr fc71994
1)Added optional equalization for MLP layer.
andreyanufr 8f8cc35
Avoid awq in the case of weight equalization.
andreyanufr 6ad2ffe
Added pile dataset for train and PTQ.
andreyanufr 6d94fee
Add ultracaht dataset.
andreyanufr 9fdee25
Added lr scheduler.
andreyanufr f418fbb
1) Removed lm_eval validation in the end of training.
andreyanufr 4bb71c7
1) Added new words to spell check dict.
andreyanufr 13a9cd4
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr 547582c
1) Fixed logging of repack_weights.
andreyanufr 8ec441a
Rename QAT to QAD.
andreyanufr 0369755
Fixed README.
andreyanufr df4691d
Applied suggestions.
andreyanufr 8c85da3
Removed unused comment.
andreyanufr f6e995b
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr 2a0130d
Update optimum-intel version.
andreyanufr b414e89
1) Added args check.
andreyanufr ebbeb18
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr ecc6f5a
Moved repack_weights to other place.
andreyanufr 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
123 changes: 123 additions & 0 deletions
123
examples/llm_compression/torch/distillation_qad_with_lora_2_3_bit/README.md
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,123 @@ | ||
| # Quantization Aware Distillation with absorbable LoRA for 2/3-bit LLM compression | ||
|
|
||
| This example trains a Hugging Face causal LM with NNCF weight compression in `FQ_LORA` mode. The script does not just run a static example; it performs an end-to-end compression + fine-tuning pipeline for 2-bit or 3-bit quantized weights. | ||
|
|
||
| ## What the script actually does | ||
|
|
||
| Running `main.py` executes the following sequence: | ||
|
|
||
| 1. Loads a pretrained model and tokenizer from Hugging Face. | ||
| 2. Builds a training dataset from one of: | ||
| - `pile-10k` | ||
| - `wikitext-2` | ||
| - `ultrachat_200k` | ||
| 3. Optionally equalizes MLP weights before compression to improve low-bit robustness. | ||
| 4. Applies `compress_weights(...)` with: | ||
| - `INT3_SYM` for `--bits 3` | ||
| - `INT2_SYM` for `--bits 2` | ||
| - `compression_format=CompressionFormat.FQ_LORA` | ||
| 5. Enables trainable absorbable LoRA adapters and quantizer scales. | ||
| 6. Runs KL-based distillation training against the original model hidden states / logits. | ||
| 7. Optionally performs a second scales-only fine-tuning phase. | ||
| 8. Saves the NNCF checkpoint. | ||
| 9. Exports the tuned model to OpenVINO IR. | ||
| 10. Optionally exports a dequantized PyTorch model as well. | ||
|
|
||
| This is a real training pipeline for low-bit LLM optimization, not a simple inference example. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before running this example, ensure you have Python 3.10+ installed and set up your environment: | ||
|
|
||
| ### 1. Create and activate a virtual environment | ||
|
|
||
| ```bash | ||
| python3 -m venv nncf_env | ||
| source nncf_env/bin/activate # On Windows: nncf_env\Scripts\activate.bat | ||
| ``` | ||
|
|
||
| ### 2. Install NNCF and other dependencies | ||
|
|
||
| ```bash | ||
| python3 -m pip install ../../../../ -r requirements.txt | ||
| ``` | ||
|
|
||
| ## Run Example | ||
|
|
||
| The example is fully automated. Just run the following command in the prepared Python environment: | ||
|
|
||
| ```bash | ||
| python main.py | ||
| ``` | ||
|
|
||
| ## Important CLI options | ||
|
|
||
| ### Model and compression | ||
|
|
||
| - `--pretrained`: HF model ID or local path. | ||
| - `--bits`: `2` or `3` bits per weight. | ||
| - `--lora_rank`: rank of absorbable LoRA adapters. | ||
| - `--basic_init`: use a simpler initialization without AWQ / scale estimation. | ||
| - `--equalize_mlp`: run MLP equalization before compression. | ||
| - `--resume`: continue from a saved checkpoint instead of reinitializing. | ||
|
|
||
| ### Data | ||
|
|
||
| - `--dataset`: one of `pile-10k`, `wikitext-2`, or `ultrachat_200k` | ||
| - `--num_train_samples`: number of training samples | ||
| - `--train_seqlen`: context length for training samples | ||
|
|
||
| ### Training | ||
|
|
||
| - `--lr`: base learning rate | ||
| - `--epochs`: number of main training epochs | ||
| - `--scale_epochs`: extra epochs where only quantizer scales are trained | ||
| - `--linear_lr_scheduler`: use a linear learning-rate decay schedule | ||
| - `--batch_size`: accumulation target | ||
| - `--microbatch_size`: per-step microbatch size | ||
| - `--full_determinism`: to enable deterministic QAD | ||
|
|
||
| ### Output/export | ||
|
|
||
| - `--output_dir`: root directory for logs, checkpoints, and exported models | ||
| - `--save_pt`: export a dequantized PyTorch model after training | ||
|
|
||
| ## Output artifacts | ||
|
|
||
| The script writes artifacts under the chosen output directory. For example, with `--output_dir output` and `--bits 3`, it creates: | ||
|
|
||
| ```text | ||
| output/ | ||
| tb/ | ||
| YYYY-MM-DD__HH-MM-SS/ | ||
| last_bits_3/ | ||
| nncf_checkpoint.pth | ||
| last_bits_3/ | ||
| model.xml | ||
| model.bin | ||
| ... | ||
| ``` | ||
|
|
||
| The checkpoint contains the NNCF state and, when appropriate, the model state. A TensorBoard directory is created for loss and LR tracking. When `--save_pt` is enabled, the script also exports a dequantized PyTorch model under: | ||
|
|
||
| ```text | ||
| output/last_bits_3/dequantized/ | ||
| ``` | ||
|
|
||
| ## Training behavior | ||
|
|
||
| The optimization loop is distillation-based: | ||
|
|
||
| - teacher hidden states are precomputed with the original model | ||
| - the compressed model receives the same tokens | ||
| - the script minimizes a KL divergence between the student and teacher outputs / hidden states | ||
| - optimizer updates are accumulated across microbatch steps and applied when `batch_size // microbatch_size` is reached | ||
|
|
||
| This makes the example a low-bit QAD + LoRA distillation workflow rather than a plain quantization pass. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The script asserts `torch.cuda.is_available()`, so it expects a CUDA-capable environment. | ||
| - Default model is `meta-llama/Llama-3.2-1B-Instruct`. | ||
| - OpenVINO export is always performed at the end of the run, after checkpoint restoration and stripping. | ||
| - `--resume` reuses the checkpoint if present; otherwise the script initializes from scratch. | ||
Oops, something went wrong.
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.
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.
Done.