Skip to content
Open
Show file tree
Hide file tree
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 Jul 27, 2026
029d4cf
1) Removed debug code.
andreyanufr Jul 30, 2026
03ceee7
Fixed style bugs.
andreyanufr Jul 30, 2026
db5a21a
Added test for weight repacking.
andreyanufr Jul 30, 2026
707c35f
Added test for weight repacking.
andreyanufr Jul 30, 2026
eeb5572
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr Sep 14, 2026
2c45ac3
Train 2-3 bit quantizers.
andreyanufr Sep 15, 2026
b0f0240
Revert experimental changes.
andreyanufr Sep 16, 2026
ab544bb
Fixed style and removed unused code.
andreyanufr Sep 16, 2026
fc71994
1)Added optional equalization for MLP layer.
andreyanufr Sep 17, 2026
8f8cc35
Avoid awq in the case of weight equalization.
andreyanufr Sep 17, 2026
6ad2ffe
Added pile dataset for train and PTQ.
andreyanufr Sep 17, 2026
6d94fee
Add ultracaht dataset.
andreyanufr Sep 18, 2026
9fdee25
Added lr scheduler.
andreyanufr Sep 18, 2026
f418fbb
1) Removed lm_eval validation in the end of training.
andreyanufr Sep 21, 2026
4bb71c7
1) Added new words to spell check dict.
andreyanufr Sep 21, 2026
13a9cd4
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr Sep 24, 2026
547582c
1) Fixed logging of repack_weights.
andreyanufr Sep 25, 2026
8ec441a
Rename QAT to QAD.
andreyanufr Sep 30, 2026
0369755
Fixed README.
andreyanufr Sep 30, 2026
df4691d
Applied suggestions.
andreyanufr Oct 1, 2026
8c85da3
Removed unused comment.
andreyanufr Oct 1, 2026
f6e995b
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr Oct 1, 2026
2a0130d
Update optimum-intel version.
andreyanufr Oct 1, 2026
b414e89
1) Added args check.
andreyanufr Oct 2, 2026
ebbeb18
Merge remote-tracking branch 'upstream/develop' into aanuf/2_3_bit_tr…
andreyanufr Oct 5, 2026
ecc6f5a
Moved repack_weights to other place.
andreyanufr Oct 6, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .ci/cspell_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ logicalnot
logicalor
logicalxor
logit
logits
loglikelihoods
lspec
lstmsequence
Expand All @@ -253,6 +254,7 @@ metatype
metatypes
metzen
mhsa
microbatch
microscaling
minfo
minfos
Expand Down Expand Up @@ -425,6 +427,7 @@ segm
selu
senina
sess
seqlen
sgraph
shamporov
shapeof
Expand Down Expand Up @@ -479,6 +482,7 @@ togethercomputer
torchdynamo
truncnorm
tupled
ultrachat
unet
unflatten
unpatch
Expand Down
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `--microbatch_size`: per-step microbatch size
- `--microbatch_size`: per-step microbatch size
- `--full_determinism`: to enable deterministic QAT

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- `--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.
Loading
Loading