LoftQ commited on
Commit
a325970
·
1 Parent(s): 3302fd4

Create README.md

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