Amod commited on
Commit
bed93a9
1 Parent(s): c659014

Uploaded Jupyter Notebook

Browse files
Files changed (1) hide show
  1. falcon7b-fine-tuned-therapy.ipynb +305 -0
falcon7b-fine-tuned-therapy.ipynb ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "private_outputs": true,
8
+ "gpuType": "T4"
9
+ },
10
+ "kernelspec": {
11
+ "name": "python3",
12
+ "display_name": "Python 3"
13
+ },
14
+ "language_info": {
15
+ "name": "python"
16
+ },
17
+ "accelerator": "GPU"
18
+ },
19
+ "cells": [
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": null,
23
+ "metadata": {
24
+ "id": "wVEwK8exTOmG"
25
+ },
26
+ "outputs": [],
27
+ "source": [
28
+ "!pip install -qU bitsandbytes transformers datasets accelerate loralib einops xformers"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "source": [
34
+ "!pip install -q -U git+https://github.com/huggingface/peft.git"
35
+ ],
36
+ "metadata": {
37
+ "id": "xF0gYxCezHpC"
38
+ },
39
+ "execution_count": null,
40
+ "outputs": []
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "source": [
45
+ "from huggingface_hub import notebook_login\n",
46
+ "\n",
47
+ "notebook_login()"
48
+ ],
49
+ "metadata": {
50
+ "id": "S1Ny2qUfYe6c"
51
+ },
52
+ "execution_count": null,
53
+ "outputs": []
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "source": [
58
+ "!nvidia-smi"
59
+ ],
60
+ "metadata": {
61
+ "id": "eunFqJaXYmoE"
62
+ },
63
+ "execution_count": null,
64
+ "outputs": []
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "source": [
69
+ "import os\n",
70
+ "import bitsandbytes as bnb\n",
71
+ "import pandas as pd\n",
72
+ "import torch\n",
73
+ "import torch.nn as nn\n",
74
+ "import transformers\n",
75
+ "from datasets import load_dataset\n",
76
+ "from peft import (\n",
77
+ " LoraConfig,\n",
78
+ " PeftConfig,\n",
79
+ " get_peft_model,\n",
80
+ " prepare_model_for_kbit_training,\n",
81
+ ")\n",
82
+ "from transformers import (\n",
83
+ " AutoConfig,\n",
84
+ " AutoModelForCausalLM,\n",
85
+ " AutoTokenizer,\n",
86
+ " BitsAndBytesConfig,\n",
87
+ ")\n",
88
+ "\n",
89
+ "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\""
90
+ ],
91
+ "metadata": {
92
+ "id": "tgjXtugbYpmE"
93
+ },
94
+ "execution_count": null,
95
+ "outputs": []
96
+ },
97
+ {
98
+ "cell_type": "code",
99
+ "source": [
100
+ "model_id = \"tiiuae/falcon-7b-instruct\"\n",
101
+ "\n",
102
+ "bnb_config = BitsAndBytesConfig(\n",
103
+ " load_in_4bit=True,\n",
104
+ " load_4bit_use_double_quant=True,\n",
105
+ " bnb_4bit_quant_type=\"nf4\",\n",
106
+ " bnb_4bit_compute_dtype=torch.bfloat16,\n",
107
+ ")\n",
108
+ "\n",
109
+ "model =AutoModelForCausalLM.from_pretrained(\n",
110
+ " model_id,\n",
111
+ " device_map=\"auto\",\n",
112
+ " trust_remote_code=True,\n",
113
+ " quantization_config=bnb_config,\n",
114
+ ")\n",
115
+ "\n",
116
+ "tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
117
+ "tokenizer.pad_token = tokenizer.eos_token"
118
+ ],
119
+ "metadata": {
120
+ "id": "SZzmS9kQZcds"
121
+ },
122
+ "execution_count": null,
123
+ "outputs": []
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "source": [
128
+ "def print_trainable_parameters(model):\n",
129
+ " \"\"\"\n",
130
+ " Prints the number of trainable parameters in the model.\n",
131
+ " \"\"\"\n",
132
+ " trainable_params = 0\n",
133
+ " all_param = 0\n",
134
+ " for _, param in model.named_parameters():\n",
135
+ " all_param += param.numel()\n",
136
+ " if param.requires_grad:\n",
137
+ " trainable_params += param.numel()\n",
138
+ " print(\n",
139
+ " f\"trainable params: {trainable_params} || all params: {all_param} || trainable%: {100 * trainable_params / all_param}\"\n",
140
+ " )"
141
+ ],
142
+ "metadata": {
143
+ "id": "TOD9rLaWaTjG"
144
+ },
145
+ "execution_count": null,
146
+ "outputs": []
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "source": [
151
+ "model.gradient_checkpointing_enable()\n",
152
+ "model = prepare_model_for_kbit_training(model"
153
+ ],
154
+ "metadata": {
155
+ "id": "IETKOBiRfLBM"
156
+ },
157
+ "execution_count": null,
158
+ "outputs": []
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "source": [
163
+ "config = LoraConfig(\n",
164
+ " r=16,\n",
165
+ " lora_alpha=32,\n",
166
+ " target_modules=[\"query_key_value\"],\n",
167
+ " lora_dropout=0.05,\n",
168
+ " bias=\"none\",\n",
169
+ " task_type=\"CAUSAL_LM\"\n",
170
+ ")\n",
171
+ "\n",
172
+ "model = get_peft_model(model, config)\n",
173
+ "print_trainable_parameters(model)"
174
+ ],
175
+ "metadata": {
176
+ "id": "3-fkzAk9fM4c"
177
+ },
178
+ "execution_count": null,
179
+ "outputs": []
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "source": [
184
+ "def generate_prompt(data_point):\n",
185
+ " return f\"\"\"\n",
186
+ "<human>: {data_point[\"Context\"]}\n",
187
+ "<assistance>: {data_point[\"Response\"]}\n",
188
+ " \"\"\".strip()\n",
189
+ "\n",
190
+ "def generate_and_tokenize_prompt(data_point):\n",
191
+ " full_prompt = generate_prompt(data_point)\n",
192
+ " tokenized_full_prompt = tokenizer(full_prompt, padding=True, truncation=True)\n",
193
+ " return tokenized_full_prompt\n"
194
+ ],
195
+ "metadata": {
196
+ "id": "pisCY6iDfX2N"
197
+ },
198
+ "execution_count": null,
199
+ "outputs": []
200
+ },
201
+ {
202
+ "cell_type": "code",
203
+ "source": [
204
+ "from datasets import load_dataset\n",
205
+ "dataset_name = 'alexandreteles/mental-health-conversational-data'\n",
206
+ "dataset = load_dataset(dataset_name, split=\"train\")"
207
+ ],
208
+ "metadata": {
209
+ "id": "9V_J1XY5fiit"
210
+ },
211
+ "execution_count": null,
212
+ "outputs": []
213
+ },
214
+ {
215
+ "cell_type": "code",
216
+ "source": [
217
+ "dataset[320]"
218
+ ],
219
+ "metadata": {
220
+ "id": "K9TZWSXifl7G"
221
+ },
222
+ "execution_count": null,
223
+ "outputs": []
224
+ },
225
+ {
226
+ "cell_type": "code",
227
+ "source": [
228
+ "dataset = dataset.shuffle().map(generate_and_tokenize_prompt)"
229
+ ],
230
+ "metadata": {
231
+ "id": "G_4-L6RtukRM"
232
+ },
233
+ "execution_count": null,
234
+ "outputs": []
235
+ },
236
+ {
237
+ "cell_type": "code",
238
+ "source": [
239
+ "print(dataset.shape)"
240
+ ],
241
+ "metadata": {
242
+ "id": "X_zzdEFuuw7a"
243
+ },
244
+ "execution_count": null,
245
+ "outputs": []
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "source": [
250
+ "OUTPUT_DIR = \"experiments\""
251
+ ],
252
+ "metadata": {
253
+ "id": "XP0hBNrFxzp8"
254
+ },
255
+ "execution_count": null,
256
+ "outputs": []
257
+ },
258
+ {
259
+ "cell_type": "code",
260
+ "source": [
261
+ "%load_ext tensorboard\n",
262
+ "%tensorboard --logdir experiments/runs"
263
+ ],
264
+ "metadata": {
265
+ "id": "N1gfJRpdx15C"
266
+ },
267
+ "execution_count": null,
268
+ "outputs": []
269
+ },
270
+ {
271
+ "cell_type": "code",
272
+ "source": [
273
+ "training_args = transformers.TrainingArguments(\n",
274
+ " per_device_train_batch_size=2,\n",
275
+ " gradient_accumulation_steps=4,\n",
276
+ " num_train_epochs=5,\n",
277
+ " learning_rate=2e-4,\n",
278
+ " fp16=True,\n",
279
+ " save_total_limit=4,\n",
280
+ " logging_steps=10,\n",
281
+ " output_dir=OUTPUT_DIR,\n",
282
+ " max_steps=800,\n",
283
+ " optim=\"paged_adamw_8bit\",\n",
284
+ " lr_scheduler_type = 'cosine',\n",
285
+ " warmup_ratio = 0.05,\n",
286
+ " report_to = 'tensorboard'\n",
287
+ ")\n",
288
+ "\n",
289
+ "trainer = transformers.Trainer(\n",
290
+ " model=model,\n",
291
+ " train_dataset=dataset,\n",
292
+ " args=training_args,\n",
293
+ " data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),\n",
294
+ ")\n",
295
+ "model.config.use_cache = False # silence the warnings. Please re-enable for inference!\n",
296
+ "trainer.train()"
297
+ ],
298
+ "metadata": {
299
+ "id": "796I79rpx5tt"
300
+ },
301
+ "execution_count": null,
302
+ "outputs": []
303
+ }
304
+ ]
305
+ }