Upload flacuna.py
Browse files- flacuna.py +52 -0
flacuna.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from peft import LoraConfig, get_peft_model
|
| 4 |
+
from transformers import LlamaForCausalLM, LlamaTokenizer
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class LoraArguments:
|
| 9 |
+
lora_r: int = 8
|
| 10 |
+
lora_alpha: int = 16
|
| 11 |
+
lora_dropout: float = 0.05
|
| 12 |
+
lora_target_modules = ["q_proj", "v_proj"]
|
| 13 |
+
lora_weight_path: str = ""
|
| 14 |
+
bias: str = "none"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
if __name__ == "__main__":
|
| 18 |
+
device = 0
|
| 19 |
+
lora_args = LoraArguments
|
| 20 |
+
base_model = "TheBloke/vicuna-13B-1.1-HF"
|
| 21 |
+
|
| 22 |
+
tokenizer = LlamaTokenizer.from_pretrained(base_model)
|
| 23 |
+
model = LlamaForCausalLM.from_pretrained(
|
| 24 |
+
base_model, load_in_8bit=True,
|
| 25 |
+
torch_dtype=torch.float16, device_map={"": device}
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
lora_config = LoraConfig(
|
| 29 |
+
r=lora_args.lora_r, lora_alpha=lora_args.lora_alpha, lora_dropout=lora_args.lora_dropout,
|
| 30 |
+
target_modules=lora_args.lora_target_modules, bias=lora_args.bias, task_type="CAUSAL_LM",
|
| 31 |
+
)
|
| 32 |
+
model = get_peft_model(model, lora_config)
|
| 33 |
+
|
| 34 |
+
weight = torch.load("flacuna-13b-v1.0/pytorch_model.bin", map_location="cpu")
|
| 35 |
+
model.load_state_dict(weight)
|
| 36 |
+
|
| 37 |
+
prompt = (
|
| 38 |
+
"A chat between a curious user and an artificial intelligence assistant. "
|
| 39 |
+
"The assistant gives helpful, detailed, and polite answers to the user's questions. "
|
| 40 |
+
"USER: You are tasked to demonstrate your writing skills in professional or work settings for the following question.\n"
|
| 41 |
+
"Can you help me write a speech for a graduation ceremony, inspiring and motivating the graduates to pursue their dreams and make a positive impact on the world?\n"
|
| 42 |
+
"Output: ASSISTANT: "
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
inputs = tokenizer([prompt], return_tensors="pt")
|
| 46 |
+
inputs = {k: v.to("cuda:{}".format(device)) for k, v in inputs.items()}
|
| 47 |
+
|
| 48 |
+
out = model.generate(
|
| 49 |
+
**inputs, max_new_tokens=500, min_new_tokens=100, early_stopping=True, do_sample=True, top_k=8, temperature=0.75
|
| 50 |
+
)
|
| 51 |
+
decoded = tokenizer.decode(out[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
| 52 |
+
print (decoded)
|