Skip to content

Commit a283adc

Browse files
jpablomchYuan0320ljaljushkin
authored
Release QAT example with NLS (#3480)
### Changes Adds example to use NLS fine-tuning with quantization-aware LoRA on downstream tasks. ### Reason for changes To support fine-tuning for downstream scenarios, and NLS often boost the performance of LoRA fine-tuning on downstream tasks. ### Related tickets https://jira.devtools.intel.com/browse/CVS-166802 ### Tests See the results in NLSDownstreamTasks.md. We have conducted extensive evaluation on 11 language models and 4 downstream tasks. examples job: https://github.com/openvinotoolkit/nncf/actions/runs/14934370942 --------- Signed-off-by: J. Pablo Muñoz <[email protected]> Co-authored-by: Yuan0320 <[email protected]> Co-authored-by: Lyalyushkin Nikolay <[email protected]>
1 parent daa5174 commit a283adc

File tree

14 files changed

+1103
-29
lines changed

14 files changed

+1103
-29
lines changed

.ci/cspell_dict.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ elts
127127
eltwise
128128
eltwises
129129
embeddingbag
130+
emnlp
130131
eprint
131132
eprinttype
132133
errstate
@@ -173,6 +174,7 @@ hardswish
173174
hardtanh
174175
hawq
175176
headport
177+
hellaswag
176178
hiddens
177179
hparam
178180
hparams
@@ -200,6 +202,7 @@ inputless
200202
interp
201203
ioff
202204
iterval
205+
jinjie
203206
junitxml
204207
keepdim
205208
keepdims
@@ -308,6 +311,7 @@ onnxp
308311
onnxqdq
309312
onnxroi
310313
onnxx
314+
openbookqa
311315
openvino
312316
openvinotoolkit
313317
oplambda
@@ -430,6 +434,7 @@ sparsifiers
430434
sparsifies
431435
sparsify
432436
sparsifying
437+
sqft
433438
squeezenet
434439
stabilityai_stablelm
435440
stdv
@@ -483,6 +488,7 @@ vtype
483488
weakrefs
484489
weightable
485490
whowhatbench
491+
winogrande
486492
xlabel
487493
xnli
488494
xticklabels
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Quantization-aware NLS Tuning for improving accuracy on downstream Tasks
2+
3+
This example demonstrates how to improve accuracy of Large Language Models (LLMs) with 4bit weights by
4+
quantization-aware-training with **Neural Low-Rank Adapter Search (NLS)** on downstream tasks.
5+
6+
<p align="center">
7+
<img src="/examples/llm_compression/torch/qat_with_nls_downstream/pics/lora_vs_nls.png" alt="LoRA vs NLS" width="400"/>
8+
</p>
9+
10+
[main.py](main.py) supports fine-tuning and evaluating a language model with quantization-aware training and **Neural Low-Rank Adapter Search (NLS)** proposed by [Shears](https://arxiv.org/abs/2404.10934) and [SQFT](https://arxiv.org/abs/2410.03750) on various downstream tasks. For example, to run the script for the task [openbookqa](https://huggingface.co/datasets/allenai/openbookqa), you can use the following command:
11+
12+
```bash
13+
python main.py --pretrained Qwen/Qwen2.5-3B-Instruct --output_dir output --task openbookqa --lr 1e-4 --epochs 3 --batch_size 16 --eval_batch_size 64 --lora_rank_space 32 24 16
14+
```
15+
16+
- `--pretrained`: The model ID or path of a pretrained Hugging Face model configuration.
17+
- `--output_dir`: Path to the directory for storing logs, tuning checkpoints, compressed models, and evaluation results.
18+
- `--task`: The evaluation task to be performed. Choices: ["gsm8k", "hellaswag", "openbookqa", "winogrande", "arc_challenge", "arc_easy"].
19+
- `--lr`: Learning rate for fine-tuning.
20+
- `--epochs`: Number of epochs for training.
21+
- `--batch_size`: Size of the training batch.
22+
- `--eval_batch_size`: Size of the batch for evaluation.
23+
- `--lora_rank_space`: Specifies the search space for LoRA adapter ranks. For example, [32, 24, 16] indicates the ranks to be considered during NLS training and searching.
24+
- `--eval_only`: Whether to perform evaluation only. If specified, the model will be loaded from the checkpoint for evaluation.
25+
- `--resume`: Whether to resume training from a checkpoint. If specified, the script will load the trained checkpoint and continue training or evaluation.
26+
- `--custom_rank_config`: Specifies the LoRA rank of adapters per layer.
27+
28+
Regarding evaluation, the script will automatically use a heuristic to obtain a good configuration for evaluation. This default strategy takes advantage of some information from the training phase and requires the evaluation of only 7 suggested configurations. This is automatically done in the example script, and only the best configuration from these candidates is returned to the user. More powerful elastic LoRA NLS configurations can be optionally obtained through more advanced search algorithms. We also support testing a custom configuration for evaluation after training. The following command will load the trained checkpoint and test the specified LoRA rank configuration:
29+
30+
```bash
31+
python main.py --pretrained Qwen/Qwen2.5-3B-Instruct --output_dir output --eval_only --resume --task openbookqa --lora_rank_space 32 24 16 --custom_rank_config 32 24 16 24 24 32 24 32 32 16 24 16 24 32 24 16 24 24 32 32 24 32 32 16 32 32 24 32
32+
```
33+
34+
This script also supports running the vanilla LoRA method. We only need to pass a single number for `--lora_rank_space`, such as `--lora_rank_space 32`. In addition, the training time of LoRA and NLS is very similar, and there is almost no overhead in activating different sub-adapters during training. For instance, fine-tuning the compressed Llama-3.2-3B-Instruct model for 3 epochs on [arc-challenge](https://huggingface.co/datasets/allenai/ai2_arc) takes 161.83 seconds with LoRA and 164.89 seconds with NLS.
35+
36+
## Results
37+
38+
The table below illustrates the performance improvements achieved by integrating Quantization-Aware Training (QAT) with absorbable LoRA and NLS on compressed models across various downstream tasks before exporting to OpenVINO. Our evaluation encompasses 11 large language models and 4 downstream tasks: [openbookqa](https://huggingface.co/datasets/allenai/openbookqa), [winogrande](https://huggingface.co/datasets/allenai/winogrande), [arc-challenge](https://huggingface.co/datasets/allenai/ai2_arc), and [arc-easy](https://huggingface.co/datasets/allenai/ai2_arc). The value in the table represents the mean accuracy of these tasks, with "acc_norm" used for all except winogrande, which uses "acc" ([lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness)).
39+
40+
To ensure a fair and comprehensive comparison, we conducted experiments with epochs set to 3, 4, and 5, LoRA rank set to 16 and 32, and the corresponding LoRA rank space of NLS set to `[16,12,8]` and `[32,24,16]`. We present the best results.
41+
INT4 (LoRA + PTWC) results are derived from the best BF16 (LoRA) model using the OpenVINO PTWC (AWQ + Scale Estimation + GPTQ) method. All quantization methods compressed the models to `INT4_ASYM` precision with a group size of 64. For BF16 model + LoRA finetuning, we used [PEFT](https://github.com/huggingface/peft) for inserting the LoRA adapters, ensuring consistent adapter placement with QAT.
42+
43+
**Conclusion:** The results indicate a performance comparison among the methods:
44+
**BF16 < INT4 (LoRA + PTWC) < INT4 (QAT + LoRA) < INT4 (QAT + NLS) ≲ BF16 (LoRA)**. This demonstrates that QAT + NLS generally provides the best performance among the quantized models, closely approaching the performance of BF16 (LoRA).
45+
46+
\* We highlight the best of the INT4 results.
47+
48+
| Model | BF16 | BF16 (LoRA) | INT4 (LoRA + PTWC) | INT4 (QAT + LoRA) | INT4 (QAT + NLS) |
49+
|--------------------------------------|-------|-------------|--------------------|-------------------|------------------|
50+
| meta-llama/Meta-Llama-3-8B | 0.6233| 0.7277 | 0.7167 | 0.7236 | **0.7350** |
51+
| meta-llama/Meta-Llama-3-8B-Instruct | 0.6286| 0.7148 | 0.7098 | 0.7076 | **0.7128** |
52+
| meta-llama/Llama-3.1-8B | 0.6310| 0.7330 | 0.7201 | 0.7243 | **0.7297** |
53+
| meta-llama/Llama-3.1-8B-Instruct | 0.6297| 0.7197 | 0.7160 | 0.7140 | **0.7166** |
54+
| Qwen/Qwen2.5-7B | 0.6207| 0.7344 | 0.7269 | 0.7366 | **0.7408** |
55+
| Qwen/Qwen2.5-7B-Instruct | 0.6401| 0.7305 | 0.7234 | 0.7356 | **0.7382** |
56+
| mistralai/Mistral-7B-v0.3 | 0.6209| 0.7208 | 0.7115 | 0.7164 | **0.7291** |
57+
| Qwen/Qwen2.5-3B-Instruct | 0.5814| 0.7003 | 0.6839 | 0.6916 | **0.6966** |
58+
| meta-llama/Llama-3.2-3B-Instruct | 0.5435| 0.6515 | 0.6503 | 0.6510 | **0.6570** |
59+
| HuggingFaceTB/SmolLM-1.7B-Instruct | 0.4934| 0.5759 | 0.5751 | **0.5765** | 0.5733 |
60+
| google/gemma-2-2b-it | 0.6133| 0.6806 | 0.6658 | 0.6801 | **0.6843** |
61+
62+
## Citation
63+
64+
If you find this code and the NLS technique helpful, please kindly cite:
65+
66+
```bibtex
67+
@inproceedings{munoz2025low,
68+
title=Low-Rank Adapters Meet Neural Architecture Search for LLM Compression,
69+
author="Munoz, J. Pablo and
70+
Yuan, Jinjie and
71+
Jain, Nilesh",,
72+
booktitle={AAAI'25 workshop on CoLoRAI - Connecting Low-Rank Representations in AI},
73+
year={2025},
74+
url={https://arxiv.org/abs/2501.16372}
75+
}
76+
```
77+
78+
```bibtex
79+
@inproceedings{munoz-2024-sqft,
80+
title = "{SQFT}: Low-cost Model Adaptation in Low-precision Sparse Foundation Models",
81+
author = "Munoz, Juan Pablo and
82+
Yuan, Jinjie and
83+
Jain, Nilesh",
84+
booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2024",
85+
month = nov,
86+
year = "2024",
87+
address = "Miami, Florida, USA",
88+
publisher = "Association for Computational Linguistics",
89+
url = "https://aclanthology.org/2024.findings-emnlp.749",
90+
pages = "12817--12832",
91+
}
92+
```

0 commit comments

Comments
 (0)