Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
language:
|
4 |
+
- en
|
5 |
+
pipeline_tag: text-generation
|
6 |
+
tags:
|
7 |
+
- quantization
|
8 |
+
- lora
|
9 |
+
- loftq
|
10 |
+
- llama
|
11 |
+
---
|
12 |
+
# LoftQ Initialization
|
13 |
+
|
14 |
+
| [Paper](https://arxiv.org/abs/2310.08659) | [Code](https://github.com/yxli2123/LoftQ) | [PEFT Example](https://github.com/huggingface/peft/tree/main/examples/loftq_finetuning) |
|
15 |
+
|
16 |
+
LoftQ (LoRA-fine-tuning-aware Quantization) provides a quantized backbone Q and LoRA adapters A and B, given a full-precision pre-trained weight W.
|
17 |
+
|
18 |
+
This model, `CodeLlama-7b-hf-4bit-64rank`, is obtained from [CodeLLAMA-7b](https://huggingface.co/meta-llama/CodeLlama-7b-hf).
|
19 |
+
The backbone is under `LoftQ/CodeLlama-7b-hf-4bit-64rank` and LoRA adapters are under the `subfolder='loftq_init'`.
|
20 |
+
|
21 |
+
## Model Info
|
22 |
+
### Backbone
|
23 |
+
- Stored format: `torch.bfloat16`
|
24 |
+
- Size: ~ 14 GiB
|
25 |
+
- Loaded format: bitsandbytes nf4
|
26 |
+
- Size loaded on GPU: ~3.5 GiB
|
27 |
+
|
28 |
+
### LoRA adapters
|
29 |
+
- rank: 64
|
30 |
+
- lora_alpha: 16
|
31 |
+
- target_modules: ["down_proj", "up_proj", "q_proj", "k_proj", "v_proj", "o_proj", "gate_proj"]
|
32 |
+
|
33 |
+
## Usage
|
34 |
+
|
35 |
+
**Training.** Here's an example of loading this model and preparing for the LoRA fine-tuning.
|
36 |
+
|
37 |
+
```python
|
38 |
+
import torch
|
39 |
+
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
|
40 |
+
from peft import PeftModel
|
41 |
+
|
42 |
+
MODEL_ID = "LoftQ/CodeLlama-7b-hf-4bit-64rank"
|
43 |
+
|
44 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
45 |
+
MODEL_ID,
|
46 |
+
torch_dtype=torch.bfloat16, # you may change it with different models
|
47 |
+
quantization_config=BitsAndBytesConfig(
|
48 |
+
load_in_4bit=True,
|
49 |
+
bnb_4bit_compute_dtype=torch.bfloat16, # bfloat16 is recommended
|
50 |
+
bnb_4bit_use_double_quant=False,
|
51 |
+
bnb_4bit_quant_type='nf4',
|
52 |
+
),
|
53 |
+
)
|
54 |
+
peft_model = PeftModel.from_pretrained(
|
55 |
+
base_model,
|
56 |
+
MODEL_ID,
|
57 |
+
subfolder="loftq_init",
|
58 |
+
is_trainable=True,
|
59 |
+
)
|
60 |
+
|
61 |
+
# Do training with peft_model ...
|
62 |
+
```
|
63 |
+
|
64 |
+
**Inference.** Here is an example code for inference after the model has been fine-tuned on [GSM8K](https://huggingface.co/datasets/gsm8k).
|
65 |
+
|
66 |
+
```python
|
67 |
+
import torch
|
68 |
+
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
|
69 |
+
from peft import PeftModel
|
70 |
+
|
71 |
+
MODEL_ID = "LoftQ/CodeLlama-7b-hf-4bit-64rank"
|
72 |
+
|
73 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
74 |
+
MODEL_ID,
|
75 |
+
torch_dtype=torch.bfloat16, # you may change it with different models
|
76 |
+
quantization_config=BitsAndBytesConfig(
|
77 |
+
load_in_4bit=True,
|
78 |
+
bnb_4bit_compute_dtype=torch.bfloat16, # bfloat16 is recommended
|
79 |
+
bnb_4bit_use_double_quant=False,
|
80 |
+
bnb_4bit_quant_type='nf4',
|
81 |
+
),
|
82 |
+
)
|
83 |
+
peft_model = PeftModel.from_pretrained(
|
84 |
+
base_model,
|
85 |
+
MODEL_ID,
|
86 |
+
subfolder="gsm8k",
|
87 |
+
is_trainable=True,
|
88 |
+
)
|
89 |
+
|
90 |
+
# Do inference with peft_model ...
|
91 |
+
```
|
92 |
+
|
93 |
+
See the full code at our [Github Repo]((https://github.com/yxli2123/LoftQ))
|
94 |
+
|
95 |
+
|
96 |
+
## Citation
|
97 |
+
|
98 |
+
```bibtex
|
99 |
+
@article{li2023loftq,
|
100 |
+
title={Loftq: Lora-fine-tuning-aware quantization for large language models},
|
101 |
+
author={Li, Yixiao and Yu, Yifan and Liang, Chen and He, Pengcheng and Karampatziakis, Nikos and Chen, Weizhu and Zhao, Tuo},
|
102 |
+
journal={arXiv preprint arXiv:2310.08659},
|
103 |
+
year={2023}
|
104 |
+
}
|
105 |
+
```
|