Upload 3 files
Browse files- falcon-app.py +94 -0
- falcon-finetune-personachat.py +100 -0
- requirements.txt +10 -0
falcon-app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import (
|
| 3 |
+
AutoModelForCausalLM,
|
| 4 |
+
AutoTokenizer
|
| 5 |
+
)
|
| 6 |
+
from peft import PeftModel
|
| 7 |
+
|
| 8 |
+
model_name = "tiiuae/falcon-7b"
|
| 9 |
+
model_id = "personachat-finetuned-3000-steps"
|
| 10 |
+
template = open("template.txt", "r").read()
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
+
model_name,
|
| 13 |
+
trust_remote_code = True
|
| 14 |
+
)
|
| 15 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
model_name,
|
| 17 |
+
device_map = "auto",
|
| 18 |
+
load_in_8bit = True,
|
| 19 |
+
trust_remote_code = True,
|
| 20 |
+
low_cpu_mem_usage = True
|
| 21 |
+
)
|
| 22 |
+
tuned_model = PeftModel.from_pretrained(
|
| 23 |
+
base_model,
|
| 24 |
+
model_id
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def parse_response(encoded_output, user_input):
|
| 28 |
+
decoded_output = tokenizer.batch_decode(encoded_output)[0]
|
| 29 |
+
decoded_output = decoded_output.replace(user_input, "")
|
| 30 |
+
decoded_output = decoded_output.split("<|endoftext|>",1)[0].strip()
|
| 31 |
+
return decoded_output
|
| 32 |
+
|
| 33 |
+
def generate(personality, user_input, state = {"base_state":[], "tune_state":[]}):
|
| 34 |
+
try:
|
| 35 |
+
personality = "\n".join(personality.split("."))
|
| 36 |
+
except: pass
|
| 37 |
+
state["base_state"].append(user_input)
|
| 38 |
+
state["tune_state"].append(user_input)
|
| 39 |
+
base_prompt = template.format(
|
| 40 |
+
personality = personality,
|
| 41 |
+
history = "\n".join(state["base_state"])
|
| 42 |
+
)
|
| 43 |
+
tune_prompt = template.format(
|
| 44 |
+
personality = personality,
|
| 45 |
+
history = "\n".join(state["tune_state"])
|
| 46 |
+
)
|
| 47 |
+
print("****************************")
|
| 48 |
+
print(base_prompt)
|
| 49 |
+
print("****************************")
|
| 50 |
+
print(tune_prompt)
|
| 51 |
+
print("****************************")
|
| 52 |
+
base_input_ids = tokenizer(base_prompt, return_tensors="pt").to("cuda")
|
| 53 |
+
tune_input_ids = tokenizer(tune_prompt, return_tensors="pt").to("cuda")
|
| 54 |
+
kwargs = dict({
|
| 55 |
+
"top_k": 0,
|
| 56 |
+
"top_p": 0.9,
|
| 57 |
+
"do_sample": True,
|
| 58 |
+
"temperature": 0.5,
|
| 59 |
+
"max_new_tokens": 50,
|
| 60 |
+
"repetition_penalty": 1.1,
|
| 61 |
+
"num_return_sequences": 1
|
| 62 |
+
})
|
| 63 |
+
base_model_response = parse_response(
|
| 64 |
+
base_model.generate(
|
| 65 |
+
input_ids = base_input_ids["input_ids"],
|
| 66 |
+
**kwargs
|
| 67 |
+
),
|
| 68 |
+
base_prompt
|
| 69 |
+
)
|
| 70 |
+
tune_model_response = parse_response(
|
| 71 |
+
tuned_model.generate(
|
| 72 |
+
input_ids = tune_input_ids["input_ids"],
|
| 73 |
+
**kwargs
|
| 74 |
+
),
|
| 75 |
+
tune_prompt
|
| 76 |
+
)
|
| 77 |
+
state["base_state"].append(base_model_response)
|
| 78 |
+
state["tune_state"].append(tune_model_response)
|
| 79 |
+
return base_model_response, tune_model_response, state
|
| 80 |
+
|
| 81 |
+
gr.Interface(
|
| 82 |
+
fn = generate,
|
| 83 |
+
inputs = [
|
| 84 |
+
gr.Textbox(label = "user personality", place_holder = "Enter your personality"),
|
| 85 |
+
gr.Textbox(label = "user chat", place_holder = "Enter your message"),
|
| 86 |
+
"state"
|
| 87 |
+
],
|
| 88 |
+
outputs = [
|
| 89 |
+
gr.Textbox(label = "base model response"),
|
| 90 |
+
gr.Textbox(label = "fine tuned model response"),
|
| 91 |
+
"state"
|
| 92 |
+
],
|
| 93 |
+
theme = "gradio/seafoam"
|
| 94 |
+
).launch(share = True)
|
falcon-finetune-personachat.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch, einops
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from peft import LoraConfig
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoModelForCausalLM,
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
BitsAndBytesConfig,
|
| 8 |
+
AutoTokenizer,
|
| 9 |
+
TrainingArguments
|
| 10 |
+
)
|
| 11 |
+
from peft.tuners.lora import LoraLayer
|
| 12 |
+
|
| 13 |
+
from trl import SFTTrainer
|
| 14 |
+
|
| 15 |
+
template = """### Personality:
|
| 16 |
+
{personality}
|
| 17 |
+
|
| 18 |
+
### History:
|
| 19 |
+
{history}
|
| 20 |
+
|
| 21 |
+
### Response:
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
model_name = "tiiuae/falcon-7b"
|
| 25 |
+
dataset_name = "bavard/personachat_truecased"
|
| 26 |
+
|
| 27 |
+
def create_and_prepare_model():
|
| 28 |
+
compute_dtype = getattr(torch, "float16")
|
| 29 |
+
|
| 30 |
+
bnb_config = BitsAndBytesConfig(
|
| 31 |
+
load_in_4bit=True,
|
| 32 |
+
bnb_4bit_quant_type="nf4",
|
| 33 |
+
bnb_4bit_compute_dtype=compute_dtype,
|
| 34 |
+
bnb_4bit_use_double_quant=True,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# device_map={"": 0}
|
| 38 |
+
device_map="auto"
|
| 39 |
+
|
| 40 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 41 |
+
model_name, quantization_config=bnb_config, device_map=device_map, trust_remote_code=True
|
| 42 |
+
)
|
| 43 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map=device_map, trust_remote_code=True)
|
| 44 |
+
|
| 45 |
+
peft_config = LoraConfig(
|
| 46 |
+
lora_alpha=16,
|
| 47 |
+
lora_dropout=0.1,
|
| 48 |
+
r=64,
|
| 49 |
+
bias="none",
|
| 50 |
+
task_type="CAUSAL_LM",
|
| 51 |
+
target_modules=[
|
| 52 |
+
"query_key_value"
|
| 53 |
+
],
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 57 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 58 |
+
|
| 59 |
+
return model, peft_config, tokenizer
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
training_arguments = TrainingArguments(
|
| 63 |
+
output_dir="./results",
|
| 64 |
+
per_device_train_batch_size=1,
|
| 65 |
+
gradient_accumulation_steps=4,
|
| 66 |
+
optim="paged_adamw_32bit",
|
| 67 |
+
save_steps=1000,
|
| 68 |
+
logging_steps=10,
|
| 69 |
+
learning_rate=2e-4,
|
| 70 |
+
fp16=True,
|
| 71 |
+
max_grad_norm=0.3,
|
| 72 |
+
max_steps=10000,
|
| 73 |
+
warmup_ratio=0.03,
|
| 74 |
+
group_by_length=False,
|
| 75 |
+
lr_scheduler_type="constant",
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
dataset = load_dataset(dataset_name, split="train")
|
| 79 |
+
model, peft_config, tokenizer = create_and_prepare_model()
|
| 80 |
+
model.config.use_cache = False
|
| 81 |
+
|
| 82 |
+
def formatting_func(example):
|
| 83 |
+
return template.format(
|
| 84 |
+
personality = "\n".join(example["personality"]),
|
| 85 |
+
history = "\n".join(example["history"]),
|
| 86 |
+
response = example["candidates"][-1]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
trainer = SFTTrainer(
|
| 90 |
+
model=model,
|
| 91 |
+
train_dataset=dataset,
|
| 92 |
+
peft_config=peft_config,
|
| 93 |
+
max_seq_length=512,
|
| 94 |
+
tokenizer=tokenizer,
|
| 95 |
+
args=training_arguments,
|
| 96 |
+
packing=True,
|
| 97 |
+
formatting_func=formatting_func
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
trainer.train()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
bitsandbytes
|
| 2 |
+
git+https://github.com/huggingface/transformers.git
|
| 3 |
+
git+https://github.com/huggingface/peft.git
|
| 4 |
+
git+https://github.com/huggingface/accelerate.git
|
| 5 |
+
datasets
|
| 6 |
+
trl
|
| 7 |
+
einops
|
| 8 |
+
scipy
|
| 9 |
+
nvitop
|
| 10 |
+
gradio
|