-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalpaca_350m_+_fine_tuning.py
More file actions
230 lines (191 loc) · 6.39 KB
/
alpaca_350m_+_fine_tuning.py
File metadata and controls
230 lines (191 loc) · 6.39 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
"""Alpaca-350m + Fine-Tuning.ipynb
Automatically generated by Colaboratory.
**Alpaca-350M Fine-Tuned**
1. Stanford Alpaca's Training Recipe
2. 350M Parameters (Smaller Model)
3. LoRA fine-tuning to run with fewer computational resources and training parameters
4. PEFT (Parameter-Efficient-Fine-Tuning) library from HuggingFace used for fine-tuning
"""
# Commented out IPython magic to ensure Python compatibility.
## Building Colaboratory around Eric Wang's recreation of Alpaca using LoRA.
!git clone https://github.com/tloen/alpaca-lora.git
# %cd alpaca-lora/
## Installing dependencies
!pip install bitsandbytes
!pip install GPUtil
!pip install -q datasets loralib sentencepiece
!pip install -q git+https://github.com/zphang/transformers@c3dc391
!pip install -q git+https://github.com/huggingface/peft.git
!pip install torch
## Checking Dataset
from datasets import load_dataset
from transformers import AutoTokenizer, pipeline
tokenizer = AutoTokenizer.from_pretrained("RootYuan/opt-350m-alpaca", add_eos_token=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.pad_token_id = tokenizer.eos_token_id
data = load_dataset("json", data_files="alpaca_data.json")
def generate_prompt(instruction, input=None):
if input:
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Input:
{input}
### Response: """
else:
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:"""
data = data.map(lambda data_point: {"prompt": tokenizer(generate_prompt(data_point))})
## Fine-tuning process
import os
import torch
import torch.nn as nn
from datasets import load_dataset
import bitsandbytes as bnb
import transformers
from transformers import LLaMAForCausalLM, LLaMATokenizer, AutoTokenizer, AutoConfig, AutoModelForCausalLM
from peft import get_peft_model, prepare_model_for_int8_training, LoraConfig
MICRO_BATCH_SIZE = 4 # 4 works with a smaller GPU
BATCH_SIZE = 32
GRADIENT_ACCUMULATION_STEPS = BATCH_SIZE // MICRO_BATCH_SIZE
EPOCHS = 2 # Stanford's Alpaca uses 3
LEARNING_RATE = 2e-5 # Stanford's Alpaca uses 2e-5
CUTOFF_LEN = 256 # Stanford's Alpaca uses 512, but 256 accounts for 96% of the data and runs far quicker
LORA_R = 4
LORA_ALPHA = 16
LORA_DROPOUT = 0.05
model = AutoModelForCausalLM.from_pretrained (
"RootYuan/opt-350m-alpaca",
load_in_8bit=True,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained (
"RootYuan/opt-350m-alpaca", add_eos_token=True
)
model = prepare_model_for_int8_training(model)
config = LoraConfig (
r=LORA_R,
lora_alpha=LORA_ALPHA,
target_modules=["q_proj", "v_proj"],
lora_dropout=LORA_DROPOUT,
bias="none",
task_type="CAUSAL_LM",
)
model = get_peft_model(model, config)
tokenizer.pad_token_id = 0
data = load_dataset("json", data_files="alpaca_data.json")
def generate_prompt(instruction, input=None):
if input:
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Input:
{input}
### Response: """
else:
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:"""
data = data.shuffle().map(
lambda data_point: tokenizer(
generate_prompt(data_point),
truncation=True,
max_length=CUTOFF_LEN,
padding="max_length",
)
)
trainer = transformers.Trainer(
model=model,
train_dataset=data["train"],
args=transformers.TrainingArguments(
per_device_train_batch_size=MICRO_BATCH_SIZE,
gradient_accumulation_steps=GRADIENT_ACCUMULATION_STEPS,
warmup_steps=50,
num_train_epochs=EPOCHS,
learning_rate=LEARNING_RATE,
fp16=True,
logging_steps=1,
output_dir="lora-alpaca",
save_total_limit=3,
),
data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),
)
model.config.use_cache = False
trainer.train(resume_from_checkpoint=False)
model.save_pretrained("lora-alpaca")
## Push Model to HuggingFace
from huggingface_hub import notebook_login
notebook_login()
#You can edit the code to push the model to your HuggingFace Account
model.push_to_hub("RyanAir/Alpaca-350M-Fine-Tuned", use_auth_token=True)
## Generation Process
from peft import PeftModel
from transformers import LLaMATokenizer, LLaMAForCausalLM, GenerationConfig
tokenizer = LLaMATokenizer.from_pretrained("RootYuan/opt-350m-alpaca")
model = LLaMAForCausalLM.from_pretrained(
"RootYuan/opt-350m-alpaca",
load_in_8bit=True,
device_map="auto",
)
model = PeftModel.from_pretrained(model, "RyanAir/Alpaca-350M-Fine-Tuned")
# Prompt can be edited as per requirement
PROMPT = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Write a poem as an Alpaca.
### Response:"""
inputs = tokenizer(
PROMPT,
return_tensors="pt",
)
input_ids = inputs["input_ids"].cuda()
generation_config = GenerationConfig(
temperature=0.6,
top_p=0.95,
repetition_penalty=1.15,
)
print("Generating...")
generation_output = model.generate(
input_ids=input_ids,
generation_config=generation_config,
return_dict_in_generate=True,
output_scores=True,
max_new_tokens=128,
)
for s in generation_output.sequences:
print(tokenizer.decode(s))
# Commented out IPython magic to ensure Python compatibility.
# PROMPT ='''Below is an instruction that describes a task. Write a response that appropriately completes the request.
#
# ### Instruction:
# Write on the purpose of an Alpaca
#
# ### Response:
# '''
#
# %%time
#
# inputs = tokenizer(
# PROMPT,
# return_tensors="pt",
# )
# input_ids = inputs["input_ids"].cuda()
#
# generation_config = GenerationConfig(
# temperature=0.6,
# top_p=0.95,
# repetition_penalty=1.15,
# )
# print("Generating...")
# generation_output = model.generate(
# input_ids=input_ids,
# generation_config=generation_config,
# return_dict_in_generate=True,
# output_scores=True,
# max_new_tokens=128,
# )
# for s in generation_output.sequences:
# print(tokenizer.decode(s))