File size: 7,413 Bytes
8b0c479
 
 
ed1b13e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
license: llama2
---

## Lazy LoRA

### Benefits

0. using the updated [Meta's LLaMA-2 models](https://huggingface.co/meta-llama/Llama-2-70b-hf).
1. support [4-bit qlora](https://arxiv.org/abs/2305.14314), extreme GPU memory and inference time saving;
2. comparable MMLU evaluation dataset results, llama2-70b's 68.9% to our 68.21% (-0.69%).
3. This lazy-lora adapter is based on [Meta's LLaMA-2-70b-hf](https://huggingface.co/meta-llama/Llama-2-70b-hf), and using the [oasst1 dataset](https://huggingface.co/datasets/OpenAssistant/oasst1), following [Guanaco](https://huggingface.co/timdettmers/guanaco-65b).

### Introduction
Determine the rank of LoRA layers by the singular values of pretrained weight matrices.
Also, combines:
1. LoRA: [LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS](https://arxiv.org/abs/2106.09685)
2. Prefix Tuning: [Prefix-Tuning: Optimizing Continuous Prompts for Generation](https://aclanthology.org/2021.acl-long.3
53/), [P-Tuning v2: Prompt Tuning Can Be Comparable to Fine-tuning Universally Across Scales and Tasks](https://arxiv.or
g/pdf/2110.07602.pdf)
3. Prompt Tuning: [The Power of Scale for Parameter-Efficient Prompt Tuning](https://arxiv.org/abs/2104.08691)
4. LLaMA adapter: [LLaMA-Adapter: Efficient Fine-tuning of Language Models with Zero-init Attention] (https://arxiv.org/abs/2303.16199)
in one model.

This allows you to perform LoRA (additional low rank adapters inserted to each linear layer), and prompt learning (additional virtual tokens attached to the input and to the attention layers acting as `past_key_values`)

## Usage:
```python
import sys
sys.path.insert(1, '/workspace/asr/peft/src')
# TODO set this path to the lazy-lora source code path, 
# or you can install it from source code:
# TODO, please install lazylora for usage:
# git clone [email protected]:Xianchao-Wu/peft.git
# cd peft
# python setup.py install

from transformers import (AutoTokenizer, 
	AutoModelForCausalLM, BitsAndBytesConfig)
from peft import PeftModel, PeftConfig
import os
import torch

#import ipdb; ipdb.set_trace()
cache_dir="/workspace/asr/peft/qlora"
# TODO set this cache_dir to the path where you 
# stored (or, want to store) llama2-70b-hf model

lazylora_dir=os.getcwd() 
# the path that contains 'adapter_config.json' 
#     and 'adapter_model.bin'

config = PeftConfig.from_pretrained(lazylora_dir)

tokenizer = AutoTokenizer.from_pretrained(
    config.base_model_name_or_path,
    cache_dir=cache_dir,
    use_auth_token=True
)

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type='nf4',
    bnb_4bit_compute_dtype=torch.bfloat16
)

model = AutoModelForCausalLM.from_pretrained(
    config.base_model_name_or_path,
    quantization_config=bnb_config,
    device_map="auto",
    cache_dir=cache_dir,
    use_auth_token=True
)
#model.print_trainable_parameters()
print(sum(p.numel() for p in model.parameters()))
# 34,751,127,552 -> half-size of 70B due to 4-bit loading

model = PeftModel.from_pretrained(model, lazylora_dir)
print('after adding lazy lora parameters:')
model.print_trainable_parameters()
# trainable params: 0 || all params: 35,579,442,176 || trainable%: 0.0
```

## MMLU result:

```json
{"mmlu_loss": 2.3140328107200987,
 "mmlu_eval_accuracy_abstract_algebra": 0.36363636363636365,
 "mmlu_eval_accuracy_high_school_chemistry": 0.5,
 "mmlu_eval_accuracy_college_physics": 0.45454545454545453,
 "mmlu_eval_accuracy_international_law": 0.9230769230769231,
 "mmlu_eval_accuracy_nutrition": 0.696969696969697,
 "mmlu_eval_accuracy_world_religions": 0.8947368421052632,
 "mmlu_eval_accuracy_medical_genetics": 1.0,
 "mmlu_eval_accuracy_high_school_computer_science": 0.6666666666666666,
 "mmlu_eval_accuracy_anatomy": 0.5,
 "mmlu_eval_accuracy_sociology": 1.0,
 "mmlu_eval_accuracy_human_sexuality": 0.5833333333333334,
 "mmlu_eval_accuracy_high_school_world_history": 0.7307692307692307,
 "mmlu_eval_accuracy_jurisprudence": 0.7272727272727273,
 "mmlu_eval_accuracy_high_school_mathematics": 0.2413793103448276,
 "mmlu_eval_accuracy_college_biology": 0.8125,
 "mmlu_eval_accuracy_machine_learning": 0.5454545454545454,
 "mmlu_eval_accuracy_us_foreign_policy": 1.0,
 "mmlu_eval_accuracy_high_school_microeconomics": 0.7692307692307693,
 "mmlu_eval_accuracy_high_school_us_history": 1.0,
 "mmlu_eval_accuracy_security_studies": 0.7777777777777778,
 "mmlu_eval_accuracy_college_chemistry": 0.25,
 "mmlu_eval_accuracy_college_computer_science": 0.5454545454545454,
 "mmlu_eval_accuracy_miscellaneous": 0.7790697674418605,
 "mmlu_eval_accuracy_professional_accounting": 0.7419354838709677,
 "mmlu_eval_accuracy_business_ethics": 0.7272727272727273,
 "mmlu_eval_accuracy_electrical_engineering": 0.5625,
 "mmlu_eval_accuracy_elementary_mathematics": 0.4878048780487805,
 "mmlu_eval_accuracy_high_school_biology": 0.71875,
 "mmlu_eval_accuracy_college_mathematics": 0.45454545454545453,
 "mmlu_eval_accuracy_high_school_european_history": 0.7777777777777778,
 "mmlu_eval_accuracy_professional_law": 0.5588235294117647,
 "mmlu_eval_accuracy_prehistory": 0.8,
 "mmlu_eval_accuracy_high_school_macroeconomics": 0.7674418604651163,
 "mmlu_eval_accuracy_formal_logic": 0.42857142857142855,
 "mmlu_eval_accuracy_philosophy": 0.7941176470588235,
 "mmlu_eval_accuracy_astronomy": 0.75,
 "mmlu_eval_accuracy_clinical_knowledge": 0.7586206896551724,
 "mmlu_eval_accuracy_global_facts": 0.5,
 "mmlu_eval_accuracy_high_school_government_and_politics": 0.9523809523809523,
 "mmlu_eval_accuracy_moral_disputes": 0.6842105263157895,
 "mmlu_eval_accuracy_econometrics": 0.5,
 "mmlu_eval_accuracy_management": 0.9090909090909091,
 "mmlu_eval_accuracy_high_school_psychology": 0.9666666666666667,
 "mmlu_eval_accuracy_high_school_geography": 0.9090909090909091,
 "mmlu_eval_accuracy_human_aging": 0.6956521739130435,
 "mmlu_eval_accuracy_logical_fallacies": 0.7222222222222222,
 "mmlu_eval_accuracy_moral_scenarios": 0.49,
 "mmlu_eval_accuracy_conceptual_physics": 0.5384615384615384,
 "mmlu_eval_accuracy_professional_psychology": 0.782608695652174,
 "mmlu_eval_accuracy_college_medicine": 0.7727272727272727,
 "mmlu_eval_accuracy_high_school_physics": 0.11764705882352941,
 "mmlu_eval_accuracy_computer_security": 0.7272727272727273,
 "mmlu_eval_accuracy_virology": 0.5555555555555556,
 "mmlu_eval_accuracy_professional_medicine": 0.7741935483870968,
 "mmlu_eval_accuracy_marketing": 0.96,
 "mmlu_eval_accuracy_public_relations": 0.6666666666666666,
 "mmlu_eval_accuracy_high_school_statistics": 0.5652173913043478,
 "mmlu_eval_accuracy": 0.682100004303323,
 "epoch": 1.7}
```

## License and intended use 

This lazy-lora adapter is based on [Meta's LLaMA-2-70b-hf](https://huggingface.co/meta-llama/Llama-2-70b-hf), and using the [oasst1 dataset](https://huggingface.co/datasets/OpenAssistant/oasst1), following [Guanaco](https://huggingface.co/timdettmers/guanaco-65b).

lazy lora adapter weights are available under LLAMA-2 license. Note the use of the lazy lora adapter weights, requires access to the LLaMA model weighs. Lazy lora is based on LLaMA and therefore should be used according to the LLaMA license.


## Risks and Biases

The model can produce factually incorrect output, and should not be relied on to produce factually accurate information. The model was trained on various public datasets; it is possible that this model could generate lewd, biased, or otherwise offensive outputs.