-
Notifications
You must be signed in to change notification settings - Fork 257
Release QAT example with NLS #3480
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
Merged
ljaljushkin
merged 24 commits into
openvinotoolkit:develop
from
jpablomch:qat_with_nls_release
May 21, 2025
Merged
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
bd9ee7b
Release qat with nls
jpablomch 69b29e9
Fix pre-commit, cspell, markdownlint, bandit issues
jpablomch c1ac20c
Refactor for NLS subclasses
jpablomch 511bc70
Add estimated test duration
jpablomch c9b6e77
Fix NLS quantizers
jpablomch e061af9
Fix NLS instructions
jpablomch 97cb4d6
Fix test
jpablomch 2ced88b
Fix acc. tolerance
jpablomch 5526857
Update tests/cross_fw/examples/.test_durations
jpablomch 8f4a9dc
Use model.dummy_inputs instead of get_model_input
jpablomch ba88aa0
Remove unnecessary import
jpablomch 8a5b7db
Dummy inputs to device
jpablomch 407fbb3
Refactor NLS code in separate folder
jpablomch 28e8f71
Add BF16 results
jpablomch b375e2d
Merge develop
jpablomch b73d9ce
Merge branch 'develop' into qat_with_nls_release
jpablomch fbb6e86
Add export to OV
jpablomch 92fb699
Fix main NLS export to OV
jpablomch 936233e
Merge branch 'develop' into qat_with_nls_release
jpablomch db007b3
Add default lora rank space
jpablomch 4793f27
Fix defaults for training
jpablomch 1fba986
Fix example with do_train as default
jpablomch 6281dfe
Add eval_only
jpablomch bd61bf3
Fix pre-commit error
jpablomch 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
104 changes: 104 additions & 0 deletions
104
examples/llm_compression/torch/qat_with_lora/NLSDownstreamTasks.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,104 @@ | ||
# NLS Tuning with Downstream Tasks | ||
|
||
<p align="center"> | ||
<img src="/examples/llm_compression/torch/qat_with_lora/pics/lora_vs_nls.png" alt="alt text" width="400"/> | ||
</p> | ||
|
||
[main_nls.py](./main_nls.py) script 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: | ||
|
||
```bash | ||
python main_nls.py --pretrained Qwen/Qwen2.5-3B-Instruct --output_dir output --do_train --task openbookqa --lr 1e-4 --epochs 3 --batch_size 16 --eval_batch_size 64 --lora_rank_space 32 24 16 | ||
``` | ||
|
||
- `--pretrained`: The model ID or path of a pretrained Hugging Face model configuration. | ||
- `--output_dir`: Path to the directory for storing logs, tuning checkpoints, compressed models, and evaluation results. | ||
- `--do_train`: Whether to perform training. If not specified, the script will only evaluate the compressed model. | ||
- `--task`: The evaluation task to be performed. Choices: ["gsm8k", "hellaswag", "openbookqa", "winogrande", "arc_challenge", "arc_easy"]. | ||
- `--lr`: Learning rate for fine-tuning. | ||
- `--epochs`: Number of epochs for training. | ||
- `--batch_size`: Size of the training batch. | ||
- `--eval_batch_size`: Size of the batch for evaluation. | ||
- `--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. | ||
|
||
This script will automatically use a heuristic to obtain a good configuration for evaluation, but more powerful LoRA rank configurations can be optionally obtained through more advanced search algorithms. Here, we also support testing an arbitrary configuration for evaluation after training. The following command will load the trained checkpoint and test the specified LoRA rank configuration: | ||
|
||
```bash | ||
python main_nls.py --pretrained Qwen/Qwen2.5-3B-Instruct --output_dir output --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 | ||
``` | ||
|
||
- `--resume`: Whether to resume training from a checkpoint. If specified, the script will load the trained checkpoint and continue training or evaluation. | ||
- `--custom_rank_config`: Specifies the LoRA rank of adapters per layer. | ||
|
||
## Results | ||
|
||
The table illustrates that Quantization-Aware Training integrated with absorbable QAT + LoRA / QAT + NLS substantially improves the performance of compressed models on downstream tasks, and QAT + NLS performs better than QAT + LoRA overall. | ||
|
||
The average score in the table represent the average accuracy of the four 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) (all are "acc_norm" except winogrande which is "acc" of [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness)). For QAT + LoRA and QAT + NLS, we conducted experiments with epochs set to 3, 4, and 5, LoRA rank set to 16 and 32, the corresponding LoRA rank space of NLS set to `[16,12,8]` and `[32,24,16]`. We present the best results for each method. All quantization methods compressed the models to `INT4_ASYM` precision with a group size of `64`. | ||
|
||
| Model | Precision | Average score | | ||
|--------------------------------------|--------------------|---------------| | ||
| google/gemma-2-2b-it | BF16 | 0.6133 | | ||
| | INT4 (QAT + LoRA) | 0.6801 | | ||
| | INT4 (QAT + NLS) | **0.6843** | | ||
| Qwen/Qwen2.5-3B-Instruct | BF16 | 0.5814 | | ||
| | INT4 (QAT + LoRA) | 0.6916 | | ||
| | INT4 (QAT + NLS) | **0.6966** | | ||
| mistralai/Mistral-7B-v0.3 | BF16 | 0.6209 | | ||
| | INT4 (QAT + LoRA) | 0.7164 | | ||
| | INT4 (QAT + NLS) | **0.7291** | | ||
| meta-llama/Llama-3.2-3B-Instruct | BF16 | 0.5435 | | ||
| | INT4 (QAT + LoRA) | 0.6510 | | ||
| | INT4 (QAT + NLS) | **0.6570** | | ||
| HuggingFaceTB/SmolLM-1.7B-Instruct | BF16 | 0.4934 | | ||
| | INT4 (QAT + LoRA) | **0.5765** | | ||
| | INT4 (QAT + NLS) | 0.5733 | | ||
| meta-llama/Meta-Llama-3-8B | BF16 | 0.6233 | | ||
| | INT4 (QAT + LoRA) | 0.7236 | | ||
| | INT4 (QAT + NLS) | **0.7350** | | ||
| meta-llama/Meta-Llama-3-8B-Instruct | BF16 | 0.6286 | | ||
| | INT4 (QAT + LoRA) | 0.7076 | | ||
| | INT4 (QAT + NLS) | **0.7128** | | ||
| meta-llama/Llama-3.1-8B | BF16 | 0.6310 | | ||
| | INT4 (QAT + LoRA) | 0.7243 | | ||
| | INT4 (QAT + NLS) | **0.7297** | | ||
| meta-llama/Llama-3.1-8B-Instruct | BF16 | 0.6297 | | ||
| | INT4 (QAT + LoRA) | 0.7140 | | ||
| | INT4 (QAT + NLS) | **0.7166** | | ||
| Qwen/Qwen2.5-7B | BF16 | 0.6207 | | ||
| | INT4 (QAT + LoRA) | 0.7366 | | ||
| | INT4 (QAT + NLS) | **0.7408** | | ||
| Qwen/Qwen2.5-7B-Instruct | BF16 | 0.6401 | | ||
| | INT4 (QAT + LoRA) | 0.7356 | | ||
| | INT4 (QAT + NLS) | **0.7382** | | ||
|
||
jpablomch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## Citation | ||
|
||
If you find this code and the NLS technique helpful, please kindly cite: | ||
|
||
```bibtex | ||
@inproceedings{munoz2025low, | ||
title=Low-Rank Adapters Meet Neural Architecture Search for LLM Compression, | ||
author="Munoz, J. Pablo and | ||
Yuan, Jinjie and | ||
Jain, Nilesh",, | ||
booktitle={AAAI'25 workshop on CoLoRAI - Connecting Low-Rank Representations in AI}, | ||
year={2025}, | ||
url={https://arxiv.org/abs/2501.16372} | ||
} | ||
``` | ||
|
||
```bibtex | ||
@inproceedings{munoz-2024-sqft, | ||
title = "{SQFT}: Low-cost Model Adaptation in Low-precision Sparse Foundation Models", | ||
author = "Munoz, Juan Pablo and | ||
Yuan, Jinjie and | ||
Jain, Nilesh", | ||
booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2024", | ||
month = nov, | ||
year = "2024", | ||
address = "Miami, Florida, USA", | ||
publisher = "Association for Computational Linguistics", | ||
url = "https://aclanthology.org/2024.findings-emnlp.749", | ||
pages = "12817--12832", | ||
} | ||
``` |
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,10 @@ | ||
# Copyright (c) 2025 Intel Corporation | ||
alexsu52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
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.
Uh oh!
There was an error while loading. Please reload this page.