Ordenador commited on
Commit
4d49c7d
1 Parent(s): 0a9e3cc

feat: first commit with alpaca-lora Low-Rank LLaMA Instruct-Tuning example

Browse files
Files changed (4) hide show
  1. Makefile +24 -0
  2. app.py +79 -0
  3. requirements.in +7 -0
  4. requirements.txt +260 -0
Makefile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SHELL=/bin/sh
2
+ export PATH := ./venv/bin:$(PATH)
3
+ .PHONY: help
4
+ help: ## This help.
5
+ @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
6
+
7
+ .DEFAULT_GOAL := help
8
+
9
+ venv:
10
+ touch requirements.txt ;\
11
+ test -d venv || virtualenv --python=$$PYTHON3 venv
12
+
13
+ pip-compile: venv
14
+ python -m pip install --upgrade pip;\
15
+ pip install pip-tools;\
16
+ touch requirements.in ;\
17
+ pip-compile --output-file requirements.txt requirements.in;\
18
+ pip install -r requirements.txt
19
+
20
+ autopep8:
21
+ autopep8 -i *.py
22
+
23
+ clean:
24
+ rm -fr venv
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import time
4
+ from peft import PeftModel
5
+ from transformers import LLaMATokenizer, LLaMAForCausalLM, GenerationConfig
6
+
7
+ tokenizer = LLaMATokenizer.from_pretrained("decapoda-research/llama-7b-hf")
8
+ model = LLaMAForCausalLM.from_pretrained(
9
+ "decapoda-research/llama-7b-hf",
10
+ load_in_8bit=True,
11
+ device_map="auto",
12
+ )
13
+ model = PeftModel.from_pretrained(model, "tloen/alpaca-lora-7b")
14
+
15
+
16
+ def generate_prompt(instruction, input=None):
17
+ if input:
18
+ 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.
19
+
20
+ ### Instruction:
21
+ {instruction}
22
+
23
+ ### Input:
24
+ {input}
25
+
26
+ ### Response:"""
27
+ else:
28
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
29
+
30
+ ### Instruction:
31
+ {instruction}
32
+
33
+ ### Response:"""
34
+
35
+
36
+
37
+ generation_config = GenerationConfig(
38
+ temperature=0.1,
39
+ top_p=0.75,
40
+ num_beams=4,
41
+ )
42
+
43
+ def evaluate(instruction, input=None):
44
+ prompt = generate_prompt(instruction, input)
45
+ inputs = tokenizer(prompt, return_tensors="pt")
46
+ input_ids = inputs["input_ids"].cuda()
47
+ generation_output = model.generate(
48
+ input_ids=input_ids,
49
+ generation_config=generation_config,
50
+ return_dict_in_generate=True,
51
+ output_scores=True,
52
+ max_new_tokens=256
53
+ )
54
+ for s in generation_output.sequences:
55
+ output = tokenizer.decode(s)
56
+ print("Response:", output.split("### Response:")[1].strip())
57
+
58
+
59
+
60
+ with gr.Blocks() as demo:
61
+ chatbot = gr.Chatbot()
62
+ msg = gr.Textbox()
63
+ clear = gr.Button("Clear")
64
+
65
+ def user(user_message, history):
66
+ return "", history + [[user_message, None]]
67
+
68
+ def bot(history):
69
+ bot_message = evaluate(input("Instruction: {}".format(history)))
70
+ history[-1][1] = bot_message
71
+ time.sleep(1)
72
+ return history
73
+
74
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
75
+ bot, chatbot, chatbot
76
+ )
77
+ clear.click(lambda: None, None, chatbot, queue=False)
78
+
79
+ demo.launch()
requirements.in ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ bitsandbytes
3
+ datasets
4
+ loralib
5
+ sentencepiece
6
+ git+https://github.com/zphang/transformers@c3dc391
7
+ git+https://github.com/huggingface/peft.git
requirements.txt ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # This file is autogenerated by pip-compile with Python 3.10
3
+ # by the following command:
4
+ #
5
+ # pip-compile --output-file=requirements.txt requirements.in
6
+ #
7
+ accelerate==0.17.1
8
+ # via peft
9
+ aiofiles==23.1.0
10
+ # via gradio
11
+ aiohttp==3.8.4
12
+ # via
13
+ # datasets
14
+ # fsspec
15
+ # gradio
16
+ aiosignal==1.3.1
17
+ # via aiohttp
18
+ altair==4.2.2
19
+ # via gradio
20
+ anyio==3.6.2
21
+ # via
22
+ # httpcore
23
+ # starlette
24
+ async-timeout==4.0.2
25
+ # via aiohttp
26
+ attrs==22.2.0
27
+ # via
28
+ # aiohttp
29
+ # jsonschema
30
+ bitsandbytes==0.37.1
31
+ # via -r requirements.in
32
+ certifi==2022.12.7
33
+ # via
34
+ # httpcore
35
+ # httpx
36
+ # requests
37
+ charset-normalizer==3.1.0
38
+ # via
39
+ # aiohttp
40
+ # requests
41
+ click==8.1.3
42
+ # via uvicorn
43
+ contourpy==1.0.7
44
+ # via matplotlib
45
+ cycler==0.11.0
46
+ # via matplotlib
47
+ datasets==2.10.1
48
+ # via -r requirements.in
49
+ dill==0.3.6
50
+ # via
51
+ # datasets
52
+ # multiprocess
53
+ entrypoints==0.4
54
+ # via altair
55
+ fastapi==0.94.1
56
+ # via gradio
57
+ ffmpy==0.3.0
58
+ # via gradio
59
+ filelock==3.10.0
60
+ # via
61
+ # huggingface-hub
62
+ # torch
63
+ # transformers
64
+ fonttools==4.39.2
65
+ # via matplotlib
66
+ frozenlist==1.3.3
67
+ # via
68
+ # aiohttp
69
+ # aiosignal
70
+ fsspec[http]==2023.3.0
71
+ # via
72
+ # datasets
73
+ # gradio
74
+ gradio==3.21.0
75
+ # via -r requirements.in
76
+ h11==0.14.0
77
+ # via
78
+ # httpcore
79
+ # uvicorn
80
+ httpcore==0.16.3
81
+ # via httpx
82
+ httpx==0.23.3
83
+ # via gradio
84
+ huggingface-hub==0.13.2
85
+ # via
86
+ # datasets
87
+ # gradio
88
+ # transformers
89
+ idna==3.4
90
+ # via
91
+ # anyio
92
+ # requests
93
+ # rfc3986
94
+ # yarl
95
+ jinja2==3.1.2
96
+ # via
97
+ # altair
98
+ # gradio
99
+ # torch
100
+ jsonschema==4.17.3
101
+ # via altair
102
+ kiwisolver==1.4.4
103
+ # via matplotlib
104
+ linkify-it-py==2.0.0
105
+ # via markdown-it-py
106
+ loralib==0.1.1
107
+ # via -r requirements.in
108
+ markdown-it-py[linkify]==2.2.0
109
+ # via
110
+ # gradio
111
+ # mdit-py-plugins
112
+ markupsafe==2.1.2
113
+ # via
114
+ # gradio
115
+ # jinja2
116
+ matplotlib==3.7.1
117
+ # via gradio
118
+ mdit-py-plugins==0.3.3
119
+ # via gradio
120
+ mdurl==0.1.2
121
+ # via markdown-it-py
122
+ mpmath==1.3.0
123
+ # via sympy
124
+ multidict==6.0.4
125
+ # via
126
+ # aiohttp
127
+ # yarl
128
+ multiprocess==0.70.14
129
+ # via datasets
130
+ networkx==3.0
131
+ # via torch
132
+ numpy==1.24.2
133
+ # via
134
+ # accelerate
135
+ # altair
136
+ # contourpy
137
+ # datasets
138
+ # gradio
139
+ # matplotlib
140
+ # pandas
141
+ # peft
142
+ # pyarrow
143
+ # transformers
144
+ orjson==3.8.7
145
+ # via gradio
146
+ packaging==23.0
147
+ # via
148
+ # accelerate
149
+ # datasets
150
+ # huggingface-hub
151
+ # matplotlib
152
+ # peft
153
+ # transformers
154
+ pandas==1.5.3
155
+ # via
156
+ # altair
157
+ # datasets
158
+ # gradio
159
+ peft @ git+https://github.com/huggingface/peft.git
160
+ # via -r requirements.in
161
+ pillow==9.4.0
162
+ # via
163
+ # gradio
164
+ # matplotlib
165
+ psutil==5.9.4
166
+ # via
167
+ # accelerate
168
+ # peft
169
+ pyarrow==11.0.0
170
+ # via datasets
171
+ pydantic==1.10.6
172
+ # via
173
+ # fastapi
174
+ # gradio
175
+ pydub==0.25.1
176
+ # via gradio
177
+ pyparsing==3.0.9
178
+ # via matplotlib
179
+ pyrsistent==0.19.3
180
+ # via jsonschema
181
+ python-dateutil==2.8.2
182
+ # via
183
+ # matplotlib
184
+ # pandas
185
+ python-multipart==0.0.6
186
+ # via gradio
187
+ pytz==2022.7.1
188
+ # via pandas
189
+ pyyaml==6.0
190
+ # via
191
+ # accelerate
192
+ # datasets
193
+ # gradio
194
+ # huggingface-hub
195
+ # peft
196
+ # transformers
197
+ regex==2022.10.31
198
+ # via transformers
199
+ requests==2.28.2
200
+ # via
201
+ # datasets
202
+ # fsspec
203
+ # gradio
204
+ # huggingface-hub
205
+ # responses
206
+ # transformers
207
+ responses==0.18.0
208
+ # via datasets
209
+ rfc3986[idna2008]==1.5.0
210
+ # via httpx
211
+ sentencepiece==0.1.97
212
+ # via -r requirements.in
213
+ six==1.16.0
214
+ # via python-dateutil
215
+ sniffio==1.3.0
216
+ # via
217
+ # anyio
218
+ # httpcore
219
+ # httpx
220
+ starlette==0.26.1
221
+ # via fastapi
222
+ sympy==1.11.1
223
+ # via torch
224
+ tokenizers==0.13.2
225
+ # via transformers
226
+ toolz==0.12.0
227
+ # via altair
228
+ torch==2.0.0
229
+ # via
230
+ # accelerate
231
+ # peft
232
+ tqdm==4.65.0
233
+ # via
234
+ # datasets
235
+ # huggingface-hub
236
+ # transformers
237
+ transformers @ git+https://github.com/zphang/transformers@c3dc391
238
+ # via
239
+ # -r requirements.in
240
+ # peft
241
+ typing-extensions==4.5.0
242
+ # via
243
+ # gradio
244
+ # huggingface-hub
245
+ # pydantic
246
+ # torch
247
+ uc-micro-py==1.0.1
248
+ # via linkify-it-py
249
+ urllib3==1.26.15
250
+ # via
251
+ # requests
252
+ # responses
253
+ uvicorn==0.21.1
254
+ # via gradio
255
+ websockets==10.4
256
+ # via gradio
257
+ xxhash==3.2.0
258
+ # via datasets
259
+ yarl==1.8.2
260
+ # via aiohttp