-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_all_notebooks.py
430 lines (360 loc) · 15.6 KB
/
update_all_notebooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import argparse
import json
import os
import re
import shutil
from datetime import datetime
from glob import glob
general_announcement_content = """To run this, press "*Runtime*" and press "*Run all*" on a **free** Tesla T4 Google Colab instance!
<div class="align-center">
<a href="https://github.com/unslothai/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/unsloth%20new%20logo.png" width="115"></a>
<a href="https://discord.gg/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/Discord button.png" width="145"></a>
<a href="https://docs.unsloth.ai/"><img src="https://github.com/unslothai/unsloth/blob/main/images/documentation%20green%20button.png?raw=true" width="125"></a></a> Join Discord if you need help + ⭐ <i>Star us on <a href="https://github.com/unslothai/unsloth">Github</a> </i> ⭐
</div>
To install Unsloth on your own computer, follow the installation instructions on our Github page [here](https://github.com/unslothai/unsloth?tab=readme-ov-file#-installation-instructions).
**[NEW] As of Novemeber 2024, Unsloth now supports vision finetuning!**
You will learn how to do [data prep](#Data), how to [train](#Train), how to [run the model](#Inference), & [how to save it](#Save)"""
installation_content = """%%capture
!pip install unsloth
# Also get the latest nightly Unsloth!
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git
"""
installation_kaggle_content = """%%capture
# Kaggle is slow - you'll have to wait 5 minutes for it to install.
!pip install pip3-autoremove
!pip-autoremove torch torchvision torchaudio -y
!pip install torch torchvision torchaudio xformers --index-url https://download.pytorch.org/whl/cu121
!pip install unsloth"""
new_announcement_content_non_vlm = """* We support Llama 3.2 Vision 11B, 90B; Pixtral; Qwen2VL 2B, 7B, 72B; and any Llava variant like Llava NeXT!
* We support 16bit LoRA via `load_in_4bit=False` or 4bit QLoRA. Both are accelerated and use much less memory!
"""
new_announcement_content_vlm = """**We also support finetuning ONLY the vision part of the model, or ONLY the language part. Or you can select both! You can also select to finetune the attention or the MLP layers!**"""
naming_mapping = {"mistral": ["pixtral"]}
def copy_folder(source_path, new_name, destination_path=None, replace=False):
if destination_path is None:
destination_path = os.path.dirname(source_path)
new_path = os.path.join(destination_path, new_name)
try:
if replace and os.path.exists(new_path):
shutil.rmtree(new_path)
print(f"Removed existing folder: '{new_path}'")
shutil.copytree(source_path, new_path)
print(f"Successfully copied '{source_path}' to '{new_path}'")
except FileNotFoundError:
print(f"Error: Source folder '{source_path}' not found")
except Exception as e:
print(f"An error occurred: {str(e)}")
def is_path_contains_any(file_path, words):
return any(re.search(word, file_path, re.IGNORECASE) for word in words)
def update_notebook_sections(
notebook_path,
general_announcement,
installation_steps,
installation_steps_kaggle,
new_announcement_non_vlm,
new_announcement_vlm,
):
try:
with open(notebook_path, "r", encoding="utf-8") as f:
notebook_content = json.load(f)
updated = False
i = 0
while i < len(notebook_content["cells"]):
cell = notebook_content["cells"][i]
if cell["cell_type"] == "markdown":
source_str = "".join(cell["source"]).strip()
if source_str == "# General":
if (
i + 1 < len(notebook_content["cells"])
and notebook_content["cells"][i + 1]["cell_type"] == "markdown"
):
notebook_content["cells"][i + 1]["source"] = [
f"{line}\n" for line in general_announcement.splitlines()
]
updated = True
i += 1
elif source_str == "# News":
if (
i + 1 < len(notebook_content["cells"])
and notebook_content["cells"][i + 1]["cell_type"] == "markdown"
):
if is_path_contains_any(notebook_path, ["Vision"]):
announcement = new_announcement_vlm
else:
announcement = new_announcement_non_vlm
notebook_content["cells"][i + 1]["source"] = [
f"{line}\n" for line in announcement.splitlines()
]
updated = True
i += 1
elif source_str == "# Installation":
if (
i + 1 < len(notebook_content["cells"])
and notebook_content["cells"][i + 1]["cell_type"] == "code"
):
if is_path_contains_any(notebook_path, ["kaggle"]):
installation = installation_steps_kaggle
else:
installation = installation_steps
notebook_content["cells"][i + 1]["source"] = [
f"{line}\n" for line in installation.splitlines()
]
updated = True
i += 1
i += 1
# Ensure GPU metadata is set for Colab
if "metadata" not in notebook_content:
notebook_content["metadata"] = {}
if "accelerator" not in notebook_content["metadata"]:
notebook_content["metadata"]["accelerator"] = "GPU"
updated = True
if "colab" not in notebook_content["metadata"]:
notebook_content["metadata"]["colab"] = {"provenance": []}
updated = True
if "kernelspec" not in notebook_content["metadata"]:
notebook_content["metadata"]["kernelspec"] = {
"display_name": "Python 3",
"name": "python3",
}
updated = True
if updated:
with open(notebook_path, "w", encoding="utf-8") as f:
json.dump(notebook_content, f, indent=1)
print(f"Updated: {notebook_path}")
else:
print(f"No sections found to update in: {notebook_path}")
except FileNotFoundError:
print(f"Error: Notebook not found at {notebook_path}")
except json.JSONDecodeError:
print(f"Error: Invalid JSON in notebook at {notebook_path}")
except Exception as e:
print(f"An unexpected error occurred while processing {notebook_path}: {e}")
def main():
notebook_directory = "nb"
notebook_pattern = "*.ipynb"
notebook_files = glob(os.path.join(notebook_directory, notebook_pattern))
if not notebook_files:
print(
f"No notebooks found in the directory: {notebook_directory} with pattern: {notebook_pattern}"
)
return
for notebook_file in notebook_files:
update_notebook_sections(
notebook_file,
general_announcement_content,
installation_content,
installation_kaggle_content,
new_announcement_content_non_vlm,
new_announcement_content_vlm,
)
def update_readme(
args,
readme_path,
notebooks_dir,
type_order=None,
kaggle_accelerator="nvidiaTeslaT4",
):
if args.to_main_repo:
base_url_colab = (
"https://colab.research.google.com/github/unslothai/unsloth/blob/main/nb/"
)
base_url_kaggle = "https://www.kaggle.com/notebooks/welcome?src=https://github.com/unslothai/unsloth/blob/main/nb/"
else:
base_url_colab = (
"https://colab.research.google.com/github/unslothai/notebooks/blob/main/"
)
base_url_kaggle = "https://www.kaggle.com/notebooks/welcome?src=https://github.com/unslothai/notebooks/blob/main/"
paths = glob(os.path.join(notebooks_dir, "*.ipynb"))
list_models = ["Llama", "Phi", "Mistral", "Qwen", "Gemma", "Other notebooks"]
sections = {}
for section in list_models:
sections[section] = {
"Colab": {
"header": f"### {section} Notebooks\n",
"rows": [],
},
"Kaggle": {"header": f"### {section} Notebooks\n", "rows": []},
}
colab_table_header = "| Model | Type | Colab Link | \n| --- | --- | --- | \n"
kaggle_table_header = "| Model | Type | Kaggle Link | \n| --- | --- | --- | \n"
notebook_data = []
for path in paths:
notebook_name = os.path.basename(path)
is_kaggle = is_path_contains_any(path.lower(), ["kaggle"])
section_name = "Other notebooks"
if is_kaggle:
link = f"[Open in Kaggle]({base_url_kaggle}{path}"
# Force to use GPU on start for Kaggle
if kaggle_accelerator:
link += f"&accelerator={kaggle_accelerator})"
else:
link += ")"
else:
link = f"[Open in Colab]({base_url_colab}{path})"
parts = notebook_name.replace(".ipynb", "").split("-")
if is_kaggle:
model = parts[1].replace("_", " ")
else:
model = parts[0].replace("_", " ")
for sect in sections:
check = [sect.lower()]
check.extend(naming_mapping.get(sect.lower(), []))
if is_path_contains_any(path.lower(), check):
section_name = sect
break
type_ = parts[-1].replace("_", " ")
if is_path_contains_any(path.lower(), ["vision"]):
type_ = f"**{type_}**"
notebook_data.append(
{
"model": model,
"type": type_,
"link": link,
"section": section_name,
"path": path,
}
)
if type_order:
notebook_data.sort(
key=lambda x: (
list_models.index(x["section"]),
type_order.index(x["type"])
if x["type"] in type_order
else float("inf"),
)
)
else:
notebook_data.sort(key=lambda x: (list_models.index(x["section"]), x["type"]))
for data in notebook_data:
if is_path_contains_any(data["path"].lower(), ["kaggle"]):
sections[data["section"]]["Kaggle"]["rows"].append(
f"| {data['model']} | {data['type']} | {data['link']}\n"
)
else:
sections[data["section"]]["Colab"]["rows"].append(
f"| {data['model']} | {data['type']} | {data['link']}\n"
)
try:
with open(readme_path, "r", encoding="utf-8") as f:
readme_content = f.read()
start_marker = "# 📒 Fine-tuning Notebooks"
start_index = readme_content.find(start_marker)
if start_index == -1:
raise ValueError(f"Start marker '{start_marker}' not found in README.")
start_index += len(start_marker)
end_marker = "<!-- End of Notebook Links -->"
end_index = readme_content.find(end_marker)
if end_index == -1:
raise ValueError(f"End marker '{end_marker}' not found in README.")
content_before = readme_content[:start_index]
content_after = readme_content[end_index:]
temp = (
"(https://github.com/unslothai/unsloth/nb/#Kaggle-Notebooks).\n\n"
if args.to_main_repo
else "(https://github.com/unslothai/notebooks/#Kaggle-Notebooks).\n\n"
)
colab_updated_notebooks_links = (
"Below are our notebooks for Google Colab categorized by model.\n"
"You can also view our [Kaggle notebooks here]"
f"{temp}"
)
kaggle_updated_notebooks_links = (
"# 📒 Kaggle Notebooks\n"
"<details>\n <summary> \n"
"Click for all our Kaggle notebooks categorized by model:\n "
"</summary>\n\n"
)
for section in list_models:
colab_updated_notebooks_links += (
sections[section]["Colab"]["header"] + colab_table_header
)
colab_updated_notebooks_links += (
"".join(sections[section]["Colab"]["rows"]) + "\n"
)
kaggle_updated_notebooks_links += (
sections[section]["Kaggle"]["header"] + kaggle_table_header
)
kaggle_updated_notebooks_links += (
"".join(sections[section]["Kaggle"]["rows"]) + "\n"
)
kaggle_updated_notebooks_links += "</details>\n\n"
timestamp = f"<!-- Last updated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} -->\n"
updated_readme_content = (
content_before
+ "\n"
+ colab_updated_notebooks_links
+ kaggle_updated_notebooks_links
+ timestamp
+ content_after
)
with open(readme_path, "w", encoding="utf-8") as f:
f.write(updated_readme_content)
print(f"Successfully updated {readme_path}")
except FileNotFoundError:
print(f"Error: {readme_path} not found.")
except Exception as e:
print(f"An error occurred while updating {readme_path}: {e}")
def copy_and_update_notebooks(
template_dir,
destination_dir,
general_announcement,
installation,
installation_kaggle,
new_announcement_non_vlm,
new_announcement_vlm,
):
"""Copies notebooks from template_dir to destination_dir, updates them, and renames them."""
template_notebooks = glob(os.path.join(template_dir, "*.ipynb"))
if os.path.exists(destination_dir):
shutil.rmtree(destination_dir)
os.makedirs(destination_dir, exist_ok=True)
for template_notebook_path in template_notebooks:
notebook_name = os.path.basename(template_notebook_path)
colab_notebook_name = notebook_name
destination_notebook_path = os.path.join(destination_dir, colab_notebook_name)
shutil.copy2(template_notebook_path, destination_notebook_path)
print(f"Copied '{colab_notebook_name}' to '{destination_dir}'")
kaggle_notebook_name = "Kaggle-" + notebook_name
destination_notebook_path = os.path.join(destination_dir, kaggle_notebook_name)
shutil.copy2(template_notebook_path, destination_notebook_path)
print(f"Copied '{kaggle_notebook_name}' to '{destination_dir}'")
update_notebook_sections(
destination_notebook_path,
general_announcement,
installation_kaggle,
installation_kaggle,
new_announcement_non_vlm,
new_announcement_vlm,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--to_main_repo",
action="store_true",
help="Whether update notebooks and README.md for Unsloth main repository or not. Default is False.",
)
args = parser.parse_args()
copy_and_update_notebooks(
"original_template",
"nb",
general_announcement_content,
installation_content,
installation_kaggle_content,
new_announcement_content_non_vlm,
new_announcement_content_vlm,
)
main()
notebook_directory = "nb"
readme_path = "README.md"
type_order = [
"Alpaca",
"Conversational",
"CPT",
"DPO",
"ORPO",
"Text_Completion",
"CSV",
"Inference",
"Unsloth_Studio",
] # Define your desired order here
update_readme(args, readme_path, notebook_directory, type_order)