Pokai Chang commited on
Commit
94ece44
β€’
1 Parent(s): 27c4f46
Files changed (2) hide show
  1. LLaMA_LoRA.ipynb +349 -0
  2. README.md +24 -0
LLaMA_LoRA.ipynb ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "private_outputs": true,
8
+ "toc_visible": true,
9
+ "authorship_tag": "ABX9TyOQclbOikH+aaXWlbaTkEqc",
10
+ "include_colab_link": true
11
+ },
12
+ "kernelspec": {
13
+ "name": "python3",
14
+ "display_name": "Python 3"
15
+ },
16
+ "language_info": {
17
+ "name": "python"
18
+ },
19
+ "gpuClass": "standard",
20
+ "accelerator": "GPU"
21
+ },
22
+ "cells": [
23
+ {
24
+ "cell_type": "markdown",
25
+ "metadata": {
26
+ "id": "view-in-github",
27
+ "colab_type": "text"
28
+ },
29
+ "source": [
30
+ "<a href=\"https://colab.research.google.com/github/zetavg/LLaMA-LoRA/blob/main/LLaMA_LoRA.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "markdown",
35
+ "source": [
36
+ "# πŸ¦™πŸŽ›οΈ LLaMA-LoRA\n",
37
+ "\n", "TL;DR: **Runtime > Run All** (`⌘/Ctrl+F9`). Takes about 5 minutes to start. You will be promped to authorize Google Drive access."
38
+ ],
39
+ "metadata": {
40
+ "id": "bb4nzBvLfZUj"
41
+ }
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "source": [
46
+ "#@markdown To prevent Colab from disconnecting you, here is a music player that will loop infinitely (it's silent):\n",
47
+ "%%html\n",
48
+ "<audio src=\"https://github.com/anars/blank-audio/raw/master/1-hour-of-silence.mp3\" autoplay muted loop controls />"
49
+ ],
50
+ "metadata": {
51
+ "id": "DwarOgXbG77C",
52
+ "cellView": "form"
53
+ },
54
+ "execution_count": null,
55
+ "outputs": []
56
+ },
57
+ {
58
+ "cell_type": "markdown",
59
+ "source": [
60
+ "# Config\n",
61
+ "\n",
62
+ "Some configurations to run this notebook. "
63
+ ],
64
+ "metadata": {
65
+ "id": "5uS5jJ8063f_"
66
+ }
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "source": [
71
+ "# @title Git/Project { display-mode: \"form\", run: \"auto\" }\n",
72
+ "# @markdown Project settings.\n",
73
+ "\n",
74
+ "# @markdown The URL of the LLaMA-LoRA project<br>&nbsp;&nbsp;(default: `https://github.com/zetavg/llama-lora.git`):\n",
75
+ "llama_lora_project_url = \"https://github.com/zetavg/llama-lora.git\" # @param {type:\"string\"}\n",
76
+ "# @markdown The branch to use for LLaMA-LoRA project:\n",
77
+ "llama_lora_project_branch = \"dev-2\" # @param {type:\"string\"}\n",
78
+ "\n",
79
+ "# # @markdown Forces the local directory to be updated by the remote branch:\n",
80
+ "# force_update = True # @param {type:\"boolean\"}\n"
81
+ ],
82
+ "metadata": {
83
+ "id": "v3ZCPW0JBCcH"
84
+ },
85
+ "execution_count": null,
86
+ "outputs": []
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "source": [
91
+ "# @title Google Drive { display-mode: \"form\", run: \"auto\" }\n",
92
+ "# @markdown Google Drive will be used to store data that is used or outputed by this notebook. you will be prompted to authorize access while running this notebook. \n",
93
+ "#\n",
94
+ "# @markdown Currently, it's not possible to access only a specific folder of Google Drive, we have no choice to mount the entire Google Drive, but will only access given folder.\n",
95
+ "#\n",
96
+ "# @markdown You can customize the location of the stored data here.\n",
97
+ "\n",
98
+ "# @markdown The folder in Google Drive where Colab Notebook data are stored<br />&nbsp;&nbsp;**(WARNING: The content of this folder will be modified by this notebook)**:\n",
99
+ "google_drive_folder = \"Colab Data/LLaMA LoRA\" # @param {type:\"string\"}\n",
100
+ "# google_drive_colab_data_folder = \"Colab Notebooks/Notebook Data\"\n",
101
+ "\n",
102
+ "# Where Google Drive will be mounted in the Colab runtime.\n",
103
+ "google_drive_mount_path = \"/content/drive\"\n",
104
+ "\n",
105
+ "from requests import get\n",
106
+ "from socket import gethostname, gethostbyname\n",
107
+ "host_ip = gethostbyname(gethostname())\n",
108
+ "colab_notebook_filename = get(f\"http://{host_ip}:9000/api/sessions\").json()[0][\"name\"]\n",
109
+ "\n",
110
+ "# def remove_ipynb_extension(filename: str) -> str:\n",
111
+ "# extension = \".ipynb\"\n",
112
+ "# if filename.endswith(extension):\n",
113
+ "# return filename[:-len(extension)]\n",
114
+ "# return filename\n",
115
+ "\n",
116
+ "# colab_notebook_name = remove_ipynb_extension(colab_notebook_filename)\n",
117
+ "\n",
118
+ "from google.colab import drive\n",
119
+ "drive.mount(google_drive_mount_path)\n",
120
+ "\n",
121
+ "# google_drive_data_directory_relative_path = f\"{google_drive_colab_data_folder}/{colab_notebook_name}\"\n",
122
+ "google_drive_data_directory_relative_path = google_drive_folder\n",
123
+ "google_drive_data_directory_path = f\"{google_drive_mount_path}/My Drive/{google_drive_data_directory_relative_path}\"\n",
124
+ "!mkdir -p \"{google_drive_data_directory_path}\"\n",
125
+ "!ln -nsf \"{google_drive_data_directory_path}\" ./data\n",
126
+ "!touch \"data/This folder is used by the Colab notebook \\\"{colab_notebook_filename}\\\".txt\"\n",
127
+ "!echo \"Data will be stored in Google Drive folder: \\\"{google_drive_data_directory_relative_path}\\\", which is mounted under \\\"{google_drive_data_directory_path}\\\"\"\n"
128
+ ],
129
+ "metadata": {
130
+ "id": "iZmRtUY68U5f"
131
+ },
132
+ "execution_count": null,
133
+ "outputs": []
134
+ },
135
+ {
136
+ "cell_type": "code",
137
+ "source": [
138
+ "# @title Model/Training Settings { display-mode: \"form\", run: \"auto\" }\n",
139
+ "\n",
140
+ "base_model = \"decapoda-research/llama-7b-hf\" # @param {type:\"string\"}"
141
+ ],
142
+ "metadata": {
143
+ "id": "Ep3Qhwj0Bzwf"
144
+ },
145
+ "execution_count": null,
146
+ "outputs": []
147
+ },
148
+ {
149
+ "cell_type": "markdown",
150
+ "source": [
151
+ "# Runtime Info\n",
152
+ "\n",
153
+ "Print out some information about the Colab runtime. Code from https://colab.research.google.com/notebooks/pro.ipynb."
154
+ ],
155
+ "metadata": {
156
+ "id": "Qg8tzrgb6ya_"
157
+ }
158
+ },
159
+ {
160
+ "cell_type": "code",
161
+ "execution_count": null,
162
+ "metadata": {
163
+ "id": "AHbRt8sK6YWy"
164
+ },
165
+ "outputs": [],
166
+ "source": [
167
+ "# @title GPU Info { display-mode: \"form\" }\n",
168
+ "#\n",
169
+ "# @markdown By running this cell, you can see what GPU you've been assigned.\n",
170
+ "#\n",
171
+ "# @markdown If the execution result of running the code cell below is \"Not connected to a GPU\", you can change the runtime by going to `Runtime / Change runtime type` in the menu to enable a GPU accelerator, and then re-execute the code cell.\n",
172
+ "\n",
173
+ "\n",
174
+ "gpu_info = !nvidia-smi\n",
175
+ "gpu_info = '\\n'.join(gpu_info)\n",
176
+ "if gpu_info.find('failed') >= 0:\n",
177
+ " print('Not connected to a GPU')\n",
178
+ "else:\n",
179
+ " print(gpu_info)"
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "code",
184
+ "source": [
185
+ "# @title RAM Info { display-mode: \"form\" }\n",
186
+ "#\n",
187
+ "# @markdown By running this code cell, You can see how much memory you have available.\n",
188
+ "#\n",
189
+ "# @markdown Normally, a high-RAM runtime is not needed, but if you need more RAM, you can enable a high-RAM runtime via `Runtime / Change runtime type` in the menu. Then select High-RAM in the Runtime shape dropdown. After, re-execute the code cell.\n",
190
+ "\n",
191
+ "from psutil import virtual_memory\n",
192
+ "ram_gb = virtual_memory().total / 1e9\n",
193
+ "print('Your runtime has {:.1f} gigabytes of available RAM\\n'.format(ram_gb))\n",
194
+ "\n",
195
+ "if ram_gb < 20:\n",
196
+ " print('Not using a high-RAM runtime')\n",
197
+ "else:\n",
198
+ " print('You are using a high-RAM runtime!')"
199
+ ],
200
+ "metadata": {
201
+ "id": "rGM5MYjR7yeS"
202
+ },
203
+ "execution_count": null,
204
+ "outputs": []
205
+ },
206
+ {
207
+ "cell_type": "markdown",
208
+ "source": [
209
+ "# Prepare the Project\n",
210
+ "\n",
211
+ "Clone the project and install dependencies (takes about 5m on the first run)."
212
+ ],
213
+ "metadata": {
214
+ "id": "8vSPMNtNAqOo"
215
+ }
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "source": [
220
+ "![ ! -d llama_lora ] && git clone -b {llama_lora_project_branch} --filter=tree:0 {llama_lora_project_url} llama_lora\n",
221
+ "!cd llama_lora && git add --all && git stash && git fetch origin {llama_lora_project_branch} && git checkout {llama_lora_project_branch} && git reset origin/{llama_lora_project_branch} --hard\n",
222
+ "![ ! -f llama-lora-requirements-installed ] && cd llama_lora && pip install -r requirements.txt && touch ../llama-lora-requirements-installed"
223
+ ],
224
+ "metadata": {
225
+ "id": "JGYz2VDoAzC8"
226
+ },
227
+ "execution_count": null,
228
+ "outputs": []
229
+ },
230
+ {
231
+ "cell_type": "markdown",
232
+ "source": [
233
+ "# Launch"
234
+ ],
235
+ "metadata": {
236
+ "id": "o90p1eYQimyr"
237
+ }
238
+ },
239
+ {
240
+ "cell_type": "code",
241
+ "source": [
242
+ "# The following command will launch the app in one shot, but we will not do this here.\n",
243
+ "# Instead, we will import and run Python code from the runtime, so that each part\n",
244
+ "# can be reloaded easily in the Colab notebook and provide readable outputs.\n",
245
+ "# It also resolves the GPU out-of-memory issue on training.\n",
246
+ "# !python llama_lora/app.py --base_model='{base_model}' --data_dir='./data' --share"
247
+ ],
248
+ "metadata": {
249
+ "id": "HYVjcvwXimB6"
250
+ },
251
+ "execution_count": null,
252
+ "outputs": []
253
+ },
254
+ {
255
+ "cell_type": "code",
256
+ "source": [
257
+ "# @title Load the App (set config, prepare data dir, load base bodel)\n",
258
+ "\n",
259
+ "# @markdown For a LLaMA-7B model, it will take about ~5m to load for the first execution,\n",
260
+ "# @markdown including download. Subsequent executions will take about 2m to load.\n",
261
+ "\n",
262
+ "# Set Configs\n",
263
+ "from llama_lora.llama_lora.globals import Global\n",
264
+ "Global.base_model = base_model\n",
265
+ "data_dir_realpath = !realpath ./data\n",
266
+ "Global.data_dir = data_dir_realpath[0]\n",
267
+ "Global.load_8bit = True\n",
268
+ "\n",
269
+ "# Prepare Data Dir\n",
270
+ "import os\n",
271
+ "from llama_lora.llama_lora.utils.data import init_data_dir\n",
272
+ "init_data_dir()\n",
273
+ "\n",
274
+ "# Load the Base Model\n",
275
+ "from llama_lora.llama_lora.models import load_base_model\n",
276
+ "load_base_model()\n",
277
+ "print(f\"Base model loaded: '{Global.base_model}'.\")"
278
+ ],
279
+ "metadata": {
280
+ "id": "Yf6g248ylteP"
281
+ },
282
+ "execution_count": null,
283
+ "outputs": []
284
+ },
285
+ {
286
+ "cell_type": "markdown",
287
+ "source": [
288
+ "## Start Gradio UI πŸš€ (open the app from the URL output-ed here)\n",
289
+ "\n",
290
+ "You will see `Running on public URL: https://...` in the output of the following code cell, click on it to open the Gradio UI."
291
+ ],
292
+ "metadata": {
293
+ "id": "K-hCouBClKAe"
294
+ }
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "source": [
299
+ "import gradio as gr\n",
300
+ "from llama_lora.llama_lora.ui.main_page import main_page, get_page_title, main_page_custom_css\n",
301
+ "\n",
302
+ "with gr.Blocks(title=get_page_title(), css=main_page_custom_css()) as app:\n",
303
+ " main_page()\n",
304
+ "\n",
305
+ "app.queue(concurrency_count=1).launch(share=True, debug=True, server_name=\"127.0.0.1\")"
306
+ ],
307
+ "metadata": {
308
+ "id": "iLygNTcHk0N8"
309
+ },
310
+ "execution_count": null,
311
+ "outputs": []
312
+ },
313
+ {
314
+ "cell_type": "markdown",
315
+ "source": [
316
+ "# Reset"
317
+ ],
318
+ "metadata": {
319
+ "id": "RW09SrCZpqpa"
320
+ }
321
+ },
322
+ {
323
+ "cell_type": "code",
324
+ "source": [
325
+ "# @title Kill Session { display-mode: \"form\" }\n",
326
+ "# @markdown If you ran out of runtime resources, you can **check the following \n",
327
+ "# @markdown checkbox and run this code cell to kill the runtime session** while\n",
328
+ "# @markdown preserving your downloaded data.\n",
329
+ "do_kill_session = False # @param {type:\"boolean\"}\n",
330
+ "# @markdown You will need to re-run this notebook from start after doing this.\n",
331
+ "#\n",
332
+ "# @markdown All data that are saved to disk, including Python dependencies, base\n",
333
+ "# @markdown models will all be preserved, so the second run will be much faster.\n",
334
+ "\n",
335
+ "import os\n",
336
+ "def kill_session():\n",
337
+ " os.kill(os.getpid(), 9)\n",
338
+ "\n",
339
+ "if do_kill_session:\n",
340
+ " kill_session()"
341
+ ],
342
+ "metadata": {
343
+ "id": "bM4sY2tVps8U"
344
+ },
345
+ "execution_count": null,
346
+ "outputs": []
347
+ }
348
+ ]
349
+ }
README.md ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # πŸ¦™πŸŽ›οΈ LLaMA-LoRA
2
+
3
+ <a href="https://colab.research.google.com/github/zetavg/LLaMA-LoRA/blob/main/LLaMA_LoRA.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
4
+
5
+ Making evaluating and fine-tuning LLaMA models with low-rank adaptation (LoRA) easy.
6
+
7
+
8
+ ## Features
9
+
10
+ * [1-click up and running in Google Colab](https://colab.research.google.com/github/zetavg/LLaMA-LoRA/blob/main/LLaMA_LoRA.ipynb).
11
+ * Loads and stores data in Google Drive.
12
+ * Evaluate various LLaMA LoRA models stored in your folder or from Hugging Face.<br /><a href="https://user-images.githubusercontent.com/3784687/230273443-3b1738ca-fbc4-427b-a1e6-9dabca160288.mp4"><img width="640px" src="https://user-images.githubusercontent.com/3784687/230272844-09f7a35b-46bf-4101-b15d-4ddf243b8bef.gif" /></a>
13
+ * Fine-tune LLaMA models with different prompt templates and training dataset format.<br /><a href="https://user-images.githubusercontent.com/3784687/230276439-d729f900-c8c3-47b8-b6f6-93a5cfcecbe8.mp4"><img width="640px" src="https://user-images.githubusercontent.com/3784687/230277315-9a91d983-1690-4594-9d54-912eda8963ee.gif" /></a>
14
+ * Load JSON and JSONL datasets from your folder, or even paste plain text directly into the UI.
15
+ * Supports Stanford Alpaca [seed_tasks](https://github.com/tatsu-lab/stanford_alpaca/blob/main/seed_tasks.jsonl), [alpaca_data](https://github.com/tatsu-lab/stanford_alpaca/blob/main/alpaca_data.json) and [OpenAI "prompt"-"completion"](https://platform.openai.com/docs/guides/fine-tuning/data-formatting) format.
16
+
17
+
18
+ ## Acknowledgements
19
+
20
+ * https://github.com/tloen/alpaca-lora
21
+ * https://github.com/lxe/simple-llama-finetuner
22
+ * ...
23
+
24
+ TBC