sorokinvld tloen commited on
Commit
a17ee88
0 Parent(s):

Duplicate from tloen/alpaca-lora

Browse files

Co-authored-by: Eric J. Wang <tloen@users.noreply.huggingface.co>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +12 -0
  3. app.py +165 -0
  4. requirements.txt +8 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Alpaca-LoRA
3
+ emoji: 🦙
4
+ colorFrom: red
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.21.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: tloen/alpaca-lora
12
+ ---
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel
3
+ import transformers
4
+ import gradio as gr
5
+
6
+ assert (
7
+ "LlamaTokenizer" in transformers._import_structure["models.llama"]
8
+ ), "LLaMA is now in HuggingFace's main branch.\nPlease reinstall it: pip uninstall transformers && pip install git+https://github.com/huggingface/transformers.git"
9
+ from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
10
+
11
+ tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
12
+
13
+ BASE_MODEL = "decapoda-research/llama-7b-hf"
14
+ LORA_WEIGHTS = "tloen/alpaca-lora-7b"
15
+
16
+ if torch.cuda.is_available():
17
+ device = "cuda"
18
+ else:
19
+ device = "cpu"
20
+
21
+ try:
22
+ if torch.backends.mps.is_available():
23
+ device = "mps"
24
+ except:
25
+ pass
26
+
27
+ if device == "cuda":
28
+ model = LlamaForCausalLM.from_pretrained(
29
+ BASE_MODEL,
30
+ load_in_8bit=False,
31
+ torch_dtype=torch.float16,
32
+ device_map="auto",
33
+ )
34
+ model = PeftModel.from_pretrained(
35
+ model, LORA_WEIGHTS, torch_dtype=torch.float16, force_download=True
36
+ )
37
+ elif device == "mps":
38
+ model = LlamaForCausalLM.from_pretrained(
39
+ BASE_MODEL,
40
+ device_map={"": device},
41
+ torch_dtype=torch.float16,
42
+ )
43
+ model = PeftModel.from_pretrained(
44
+ model,
45
+ LORA_WEIGHTS,
46
+ device_map={"": device},
47
+ torch_dtype=torch.float16,
48
+ )
49
+ else:
50
+ model = LlamaForCausalLM.from_pretrained(
51
+ BASE_MODEL, device_map={"": device}, low_cpu_mem_usage=True
52
+ )
53
+ model = PeftModel.from_pretrained(
54
+ model,
55
+ LORA_WEIGHTS,
56
+ device_map={"": device},
57
+ )
58
+
59
+
60
+ def generate_prompt(instruction, input=None):
61
+ if input:
62
+ 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.
63
+
64
+ ### Instruction:
65
+ {instruction}
66
+
67
+ ### Input:
68
+ {input}
69
+
70
+ ### Response:"""
71
+ else:
72
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
73
+
74
+ ### Instruction:
75
+ {instruction}
76
+
77
+ ### Response:"""
78
+
79
+ if device != "cpu":
80
+ model.half()
81
+ model.eval()
82
+ if torch.__version__ >= "2":
83
+ model = torch.compile(model)
84
+
85
+
86
+ def evaluate(
87
+ instruction,
88
+ input=None,
89
+ temperature=0.1,
90
+ top_p=0.75,
91
+ top_k=40,
92
+ num_beams=4,
93
+ max_new_tokens=128,
94
+ **kwargs,
95
+ ):
96
+ prompt = generate_prompt(instruction, input)
97
+ inputs = tokenizer(prompt, return_tensors="pt")
98
+ input_ids = inputs["input_ids"].to(device)
99
+ generation_config = GenerationConfig(
100
+ temperature=temperature,
101
+ top_p=top_p,
102
+ top_k=top_k,
103
+ num_beams=num_beams,
104
+ **kwargs,
105
+ )
106
+ with torch.no_grad():
107
+ generation_output = model.generate(
108
+ input_ids=input_ids,
109
+ generation_config=generation_config,
110
+ return_dict_in_generate=True,
111
+ output_scores=True,
112
+ max_new_tokens=max_new_tokens,
113
+ )
114
+ s = generation_output.sequences[0]
115
+ output = tokenizer.decode(s)
116
+ return output.split("### Response:")[1].strip()
117
+
118
+
119
+ g = gr.Interface(
120
+ fn=evaluate,
121
+ inputs=[
122
+ gr.components.Textbox(
123
+ lines=2, label="Instruction", placeholder="Tell me about alpacas."
124
+ ),
125
+ gr.components.Textbox(lines=2, label="Input", placeholder="none"),
126
+ gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Temperature"),
127
+ gr.components.Slider(minimum=0, maximum=1, value=0.75, label="Top p"),
128
+ gr.components.Slider(minimum=0, maximum=100, step=1, value=40, label="Top k"),
129
+ gr.components.Slider(minimum=1, maximum=4, step=1, value=4, label="Beams"),
130
+ gr.components.Slider(
131
+ minimum=1, maximum=512, step=1, value=128, label="Max tokens"
132
+ ),
133
+ ],
134
+ outputs=[
135
+ gr.inputs.Textbox(
136
+ lines=5,
137
+ label="Output",
138
+ )
139
+ ],
140
+ title="🦙🌲 Alpaca-LoRA",
141
+ description="Alpaca-LoRA is a 7B-parameter LLaMA model finetuned to follow instructions. It is trained on the [Stanford Alpaca](https://github.com/tatsu-lab/stanford_alpaca) dataset and makes use of the Huggingface LLaMA implementation. For more information, please visit [the project's website](https://github.com/tloen/alpaca-lora).",
142
+ )
143
+ g.queue(concurrency_count=1)
144
+ g.launch()
145
+
146
+ # Old testing code follows.
147
+
148
+ """
149
+ if __name__ == "__main__":
150
+ # testing code for readme
151
+ for instruction in [
152
+ "Tell me about alpacas.",
153
+ "Tell me about the president of Mexico in 2019.",
154
+ "Tell me about the king of France in 2019.",
155
+ "List all Canadian provinces in alphabetical order.",
156
+ "Write a Python program that prints the first 10 Fibonacci numbers.",
157
+ "Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.",
158
+ "Tell me five words that rhyme with 'shock'.",
159
+ "Translate the sentence 'I have no mouth but I must scream' into Spanish.",
160
+ "Count up from 1 to 500.",
161
+ ]:
162
+ print("Instruction:", instruction)
163
+ print("Response:", evaluate(instruction))
164
+ print()
165
+ """
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ datasets
2
+ loralib
3
+ sentencepiece
4
+ git+https://github.com/huggingface/transformers.git
5
+ accelerate
6
+ bitsandbytes
7
+ git+https://github.com/huggingface/peft.git
8
+ gradio