stevhliu HF staff commited on
Commit
d5aaa83
1 Parent(s): 95aec9b

Upload 7 files

Browse files
multitask_prompt_tuning.ipynb ADDED
@@ -0,0 +1,408 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "58ff91ca-ce92-43d0-ae8b-4e9e89e193f6",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "from datasets import load_dataset\n",
13
+ "from transformers import set_seed, AutoModelForSeq2SeqLM, AutoTokenizer\n",
14
+ "from peft import get_peft_model, MultitaskPromptTuningConfig, TaskType, MultitaskPromptTuningInit\n",
15
+ "\n",
16
+ "set_seed(42)\n",
17
+ "\n",
18
+ "model_name = \"google/flan-t5-base\"\n",
19
+ "\n",
20
+ "peft_config = MultitaskPromptTuningConfig(\n",
21
+ " tokenizer_name_or_path=model_name,\n",
22
+ " num_tasks=2,\n",
23
+ " task_type=TaskType.SEQ_2_SEQ_LM,\n",
24
+ " prompt_tuning_init=MultitaskPromptTuningInit.TEXT,\n",
25
+ " num_virtual_tokens=50,\n",
26
+ " num_transformer_submodules=1,\n",
27
+ " prompt_tuning_init_text=\"classify the following into either positive or negative, or entailment, neutral or contradiction:\",\n",
28
+ ")\n",
29
+ "\n",
30
+ "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
31
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n",
32
+ "model = get_peft_model(model, peft_config)\n",
33
+ "\n",
34
+ "model = model.cuda()\n",
35
+ "\n",
36
+ "\n",
37
+ "def send_to_device(batch):\n",
38
+ " for i in batch:\n",
39
+ " batch[i] = batch[i].cuda()\n",
40
+ " return batch"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": null,
46
+ "id": "eb112bc1-ffaf-49fa-a216-0d601ec304ee",
47
+ "metadata": {
48
+ "tags": []
49
+ },
50
+ "outputs": [],
51
+ "source": [
52
+ "def get_sst2(split: str):\n",
53
+ " examples = load_dataset(\"sst2\")[split]\n",
54
+ " result_examples = []\n",
55
+ " for example in examples:\n",
56
+ " result_examples.append({})\n",
57
+ "\n",
58
+ " result_examples[-1][\"input\"] = example[\"sentence\"].strip() + \"</s>\"\n",
59
+ " result_examples[-1][\"output\"] = (\n",
60
+ " f\"positive{tokenizer.eos_token}\" if example[\"label\"] == 1 else f\"negative{tokenizer.eos_token}\"\n",
61
+ " )\n",
62
+ " result_examples[-1][\"task_id\"] = 0\n",
63
+ "\n",
64
+ " return result_examples\n",
65
+ "\n",
66
+ "\n",
67
+ "def get_mnli(split: str):\n",
68
+ " examples = load_dataset(\"multi_nli\")[split]\n",
69
+ " result_examples = []\n",
70
+ " for example in examples:\n",
71
+ " result_examples.append({})\n",
72
+ "\n",
73
+ " result_examples[-1][\"input\"] = example[\"premise\"].strip() + \" \" + example[\"hypothesis\"].strip() + \"</s>\"\n",
74
+ "\n",
75
+ " if example[\"label\"] == 0:\n",
76
+ " result_examples[-1][\"output\"] = f\"entailment{tokenizer.eos_token}\"\n",
77
+ " elif example[\"label\"] == 1:\n",
78
+ " result_examples[-1][\"output\"] = f\"neutral{tokenizer.eos_token}\"\n",
79
+ " else:\n",
80
+ " result_examples[-1][\"output\"] = f\"contradiction{tokenizer.eos_token}\"\n",
81
+ "\n",
82
+ " result_examples[-1][\"task_id\"] = 1\n",
83
+ "\n",
84
+ " return result_examples"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": null,
90
+ "id": "e5a16ec4-8fef-4ba9-95b6-a661eb51e50c",
91
+ "metadata": {
92
+ "tags": []
93
+ },
94
+ "outputs": [],
95
+ "source": [
96
+ "from typing import Tuple\n",
97
+ "from torch.utils.data import Dataset, DataLoader\n",
98
+ "import torch\n",
99
+ "\n",
100
+ "\n",
101
+ "class MyDataset(Dataset):\n",
102
+ " def __init__(self, split: str, mode: str = \"source\") -> None:\n",
103
+ " super().__init__()\n",
104
+ "\n",
105
+ " if split == \"train\":\n",
106
+ " if mode == \"source\":\n",
107
+ " self.examples = get_sst2(split) + get_mnli(split)\n",
108
+ " elif mode == \"target\":\n",
109
+ " self.examples = get_sst2(split)\n",
110
+ " if split == \"val\":\n",
111
+ " self.examples = get_sst2(\"validation\")\n",
112
+ " if split == \"test\":\n",
113
+ " self.examples = get_sst2(\"validation\")\n",
114
+ "\n",
115
+ " def __getitem__(self, index) -> dict:\n",
116
+ " return self.examples[index]\n",
117
+ "\n",
118
+ " def __len__(self) -> int:\n",
119
+ " return len(self.examples)\n",
120
+ "\n",
121
+ " def __getitem__(self, index) -> dict:\n",
122
+ " return self.examples[index]\n",
123
+ "\n",
124
+ " def __len__(self) -> int:\n",
125
+ " return len(self.examples)\n",
126
+ "\n",
127
+ "\n",
128
+ "def collate_fn(batch: dict) -> Tuple[torch.Tensor, torch.Tensor]:\n",
129
+ " input = [i[\"input\"] for i in batch]\n",
130
+ " input = tokenizer(input, add_special_tokens=False, return_tensors=\"pt\", padding=True)\n",
131
+ "\n",
132
+ " output = [i[\"output\"] for i in batch]\n",
133
+ " output = tokenizer(output, add_special_tokens=False, return_tensors=\"pt\", padding=True).input_ids\n",
134
+ " output[output == tokenizer.pad_token_id] = -100\n",
135
+ "\n",
136
+ " task_ids = [i[\"task_id\"] for i in batch]\n",
137
+ " task_ids = torch.tensor(task_ids)\n",
138
+ "\n",
139
+ " return {\n",
140
+ " \"input_ids\": input.input_ids,\n",
141
+ " \"attention_mask\": input.attention_mask,\n",
142
+ " \"labels\": output,\n",
143
+ " \"task_ids\": task_ids,\n",
144
+ " }\n",
145
+ "\n",
146
+ "\n",
147
+ "train = DataLoader(MyDataset(\"train\"), shuffle=True, batch_size=8, collate_fn=collate_fn)\n",
148
+ "val = DataLoader(MyDataset(\"val\"), shuffle=False, batch_size=8, collate_fn=collate_fn)\n",
149
+ "test = DataLoader(MyDataset(\"test\"), shuffle=False, batch_size=8, collate_fn=collate_fn)"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "markdown",
154
+ "id": "fe0aec7b-f61e-4b00-a90e-c1201dc1f84c",
155
+ "metadata": {},
156
+ "source": [
157
+ "## source training"
158
+ ]
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": null,
163
+ "id": "cceecc94-f43a-4f62-8d45-926f2f02f36d",
164
+ "metadata": {
165
+ "tags": []
166
+ },
167
+ "outputs": [],
168
+ "source": [
169
+ "from torch.optim.adamw import AdamW\n",
170
+ "from transformers import get_cosine_schedule_with_warmup\n",
171
+ "from tqdm import tqdm\n",
172
+ "from sklearn.metrics import f1_score"
173
+ ]
174
+ },
175
+ {
176
+ "cell_type": "code",
177
+ "execution_count": null,
178
+ "id": "eae5516b-73ab-44a8-a083-4e8de6127f30",
179
+ "metadata": {
180
+ "tags": []
181
+ },
182
+ "outputs": [],
183
+ "source": [
184
+ "POSITIVE_TOKEN_ID = tokenizer(\" positive\", add_special_tokens=False)[\"input_ids\"][0]\n",
185
+ "NEGATIVE_TOKEN_ID = tokenizer(\" negative\", add_special_tokens=False)[\"input_ids\"][0]\n",
186
+ "\n",
187
+ "\n",
188
+ "def classify(batch):\n",
189
+ " batch = send_to_device(batch)\n",
190
+ " # we pass labels here since we need to generate and peft doesn't support generation yet.\n",
191
+ " # No clue how to get around this\n",
192
+ " scores = model(**batch).logits\n",
193
+ " preds = []\n",
194
+ " for i in range(scores.shape[0]):\n",
195
+ " if scores[i, 0, POSITIVE_TOKEN_ID] > scores[i, 0, NEGATIVE_TOKEN_ID]:\n",
196
+ " preds.append(POSITIVE_TOKEN_ID)\n",
197
+ " else:\n",
198
+ " preds.append(NEGATIVE_TOKEN_ID)\n",
199
+ " return preds\n",
200
+ "\n",
201
+ "\n",
202
+ "@torch.inference_mode()\n",
203
+ "def evaluate(model, data):\n",
204
+ " loss = 0\n",
205
+ " preds = []\n",
206
+ " golds = []\n",
207
+ "\n",
208
+ " for batch in tqdm(data):\n",
209
+ " batch = send_to_device(batch)\n",
210
+ " loss += model(**batch).loss\n",
211
+ " golds.extend(batch[\"labels\"][:, 0].tolist())\n",
212
+ " preds.extend(classify(batch))\n",
213
+ "\n",
214
+ " return loss / len(val), f1_score(golds, preds, pos_label=POSITIVE_TOKEN_ID)\n",
215
+ "\n",
216
+ "\n",
217
+ "optimizer = AdamW(model.parameters(), lr=1e-4)\n",
218
+ "scheduler = get_cosine_schedule_with_warmup(optimizer, 200, len(train))\n",
219
+ "\n",
220
+ "n = 1000\n",
221
+ "step = 0\n",
222
+ "train_ = tqdm(train)\n",
223
+ "\n",
224
+ "val_loss, f1 = evaluate(model, val)\n",
225
+ "print(\n",
226
+ " f\"\"\"\n",
227
+ "before source training\n",
228
+ "val loss = {val_loss}\n",
229
+ "f1 = {f1}\"\"\"\n",
230
+ ")\n",
231
+ "\n",
232
+ "for batch in train_:\n",
233
+ " if step % n == 0:\n",
234
+ " val_loss, f1 = evaluate(model, val)\n",
235
+ " print(\n",
236
+ " f\"\"\"\n",
237
+ "step = {step}\n",
238
+ "val loss = {val_loss}\n",
239
+ "f1 = {f1}\"\"\"\n",
240
+ " )\n",
241
+ " model.save_pretrained(f\"checkpoints_source/{step}\")\n",
242
+ "\n",
243
+ " step += 1\n",
244
+ " batch = send_to_device(batch)\n",
245
+ " loss = model(**batch).loss\n",
246
+ " loss.backward()\n",
247
+ " optimizer.step()\n",
248
+ " scheduler.step()\n",
249
+ " train_.set_postfix(train_loss=loss)"
250
+ ]
251
+ },
252
+ {
253
+ "cell_type": "markdown",
254
+ "id": "74168ef3-66f3-41a7-a40b-7840b103fbf9",
255
+ "metadata": {},
256
+ "source": [
257
+ "## target training"
258
+ ]
259
+ },
260
+ {
261
+ "cell_type": "code",
262
+ "execution_count": null,
263
+ "id": "b09fd456-163e-4dc1-b24d-f2d0d349036c",
264
+ "metadata": {
265
+ "tags": []
266
+ },
267
+ "outputs": [],
268
+ "source": [
269
+ "train = DataLoader(MyDataset(\"train\", \"target\"), shuffle=True, batch_size=8, collate_fn=collate_fn)\n",
270
+ "val = DataLoader(MyDataset(\"val\", \"target\"), shuffle=False, batch_size=8, collate_fn=collate_fn)\n",
271
+ "test = DataLoader(MyDataset(\"test\", \"target\"), shuffle=False, batch_size=8, collate_fn=collate_fn)"
272
+ ]
273
+ },
274
+ {
275
+ "cell_type": "markdown",
276
+ "id": "4a539944-f16c-4c3f-bb4a-7b5d9a6042e2",
277
+ "metadata": {},
278
+ "source": [
279
+ "#### create a fresh model"
280
+ ]
281
+ },
282
+ {
283
+ "cell_type": "code",
284
+ "execution_count": null,
285
+ "id": "5520d904-aa6c-4654-9335-ed4e7d76cba2",
286
+ "metadata": {
287
+ "tags": []
288
+ },
289
+ "outputs": [],
290
+ "source": [
291
+ "peft_config = MultitaskPromptTuningConfig(\n",
292
+ " tokenizer_name_or_path=model_name,\n",
293
+ " num_tasks=1,\n",
294
+ " task_type=TaskType.SEQ_2_SEQ_LM,\n",
295
+ " prompt_tuning_init=MultitaskPromptTuningInit.EXACT_SOURCE_TASK,\n",
296
+ " prompt_tuning_init_state_dict_path=\"checkpoints_source/50000/adapter_model.bin\",\n",
297
+ " num_virtual_tokens=50,\n",
298
+ " num_transformer_submodules=1,\n",
299
+ ")\n",
300
+ "\n",
301
+ "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
302
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n",
303
+ "model = get_peft_model(model, peft_config)\n",
304
+ "\n",
305
+ "model = model.cuda()"
306
+ ]
307
+ },
308
+ {
309
+ "cell_type": "code",
310
+ "execution_count": null,
311
+ "id": "dfa39c2d-d1c5-4ed4-90f8-26e8e324371c",
312
+ "metadata": {
313
+ "tags": []
314
+ },
315
+ "outputs": [],
316
+ "source": [
317
+ "optimizer = AdamW(model.parameters(), lr=1e-4)\n",
318
+ "scheduler = get_cosine_schedule_with_warmup(optimizer, 200, len(train))\n",
319
+ "\n",
320
+ "n = 1000\n",
321
+ "step = 0\n",
322
+ "train_ = tqdm(train)\n",
323
+ "\n",
324
+ "val_loss, f1 = evaluate(model, val)\n",
325
+ "print(\n",
326
+ " f\"\"\"\n",
327
+ "before target training\n",
328
+ "val loss = {val_loss}\n",
329
+ "f1 = {f1}\"\"\"\n",
330
+ ")\n",
331
+ "\n",
332
+ "for batch in train_:\n",
333
+ " if step % n == 0:\n",
334
+ " val_loss, f1 = evaluate(model, val)\n",
335
+ " print(\n",
336
+ " f\"\"\"\n",
337
+ "step = {step}\n",
338
+ "val loss = {val_loss}\n",
339
+ "f1 = {f1}\"\"\"\n",
340
+ " )\n",
341
+ " model.save_pretrained(f\"checkpoints_target/{step}\")\n",
342
+ "\n",
343
+ " step += 1\n",
344
+ " batch = send_to_device(batch)\n",
345
+ " loss = model(**batch).loss\n",
346
+ " loss.backward()\n",
347
+ " optimizer.step()\n",
348
+ " scheduler.step()\n",
349
+ " train_.set_postfix(train_loss=loss)"
350
+ ]
351
+ },
352
+ {
353
+ "cell_type": "code",
354
+ "execution_count": null,
355
+ "id": "b6a6eeda-1e09-49a6-8845-cd96c8573145",
356
+ "metadata": {
357
+ "tags": []
358
+ },
359
+ "outputs": [],
360
+ "source": [
361
+ "# load last checkpoint for now\n",
362
+ "from peft import set_peft_model_state_dict\n",
363
+ "\n",
364
+ "sd_6000 = torch.load(\"checkpoints_target/6000/adapter_model.bin\")\n",
365
+ "set_peft_model_state_dict(model, sd_6000)\n",
366
+ "\n",
367
+ "# evaluate val\n",
368
+ "val_loss, f1 = evaluate(model, val)\n",
369
+ "print(\n",
370
+ " f\"\"\"\n",
371
+ "final\n",
372
+ "val loss = {val_loss}\n",
373
+ "f1 = {f1}\"\"\"\n",
374
+ ")\n",
375
+ "\n",
376
+ "# evaluate test\n",
377
+ "test_loss, f1 = evaluate(model, test)\n",
378
+ "print(\n",
379
+ " f\"\"\"\n",
380
+ "final\n",
381
+ "test loss = {test_loss}\n",
382
+ "f1 = {f1}\"\"\"\n",
383
+ ")"
384
+ ]
385
+ }
386
+ ],
387
+ "metadata": {
388
+ "kernelspec": {
389
+ "display_name": "Python 3 (ipykernel)",
390
+ "language": "python",
391
+ "name": "python3"
392
+ },
393
+ "language_info": {
394
+ "codemirror_mode": {
395
+ "name": "ipython",
396
+ "version": 3
397
+ },
398
+ "file_extension": ".py",
399
+ "mimetype": "text/x-python",
400
+ "name": "python",
401
+ "nbconvert_exporter": "python",
402
+ "pygments_lexer": "ipython3",
403
+ "version": "3.9.13"
404
+ }
405
+ },
406
+ "nbformat": 4,
407
+ "nbformat_minor": 5
408
+ }
peft_ia3_seq2seq.ipynb ADDED
@@ -0,0 +1,2711 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 12,
6
+ "metadata": {
7
+ "id": "5f93b7d1"
8
+ },
9
+ "outputs": [],
10
+ "source": [
11
+ "from transformers import AutoModelForSeq2SeqLM\n",
12
+ "import peft\n",
13
+ "from peft import get_peft_config, get_peft_model, get_peft_model_state_dict, IA3Config, TaskType\n",
14
+ "import torch\n",
15
+ "from datasets import load_dataset\n",
16
+ "import os\n",
17
+ "\n",
18
+ "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
19
+ "from transformers import AutoTokenizer\n",
20
+ "from torch.utils.data import DataLoader\n",
21
+ "from transformers import default_data_collator, get_linear_schedule_with_warmup\n",
22
+ "from tqdm import tqdm\n",
23
+ "from datasets import load_dataset\n",
24
+ "\n",
25
+ "device = \"cuda\"\n",
26
+ "model_name_or_path = \"bigscience/mt0-large\"\n",
27
+ "tokenizer_name_or_path = \"bigscience/mt0-large\"\n",
28
+ "\n",
29
+ "checkpoint_name = \"financial_sentiment_analysis_ia3_v1.pt\"\n",
30
+ "text_column = \"sentence\"\n",
31
+ "label_column = \"text_label\"\n",
32
+ "max_length = 128\n",
33
+ "lr = 8e-3\n",
34
+ "num_epochs = 3\n",
35
+ "batch_size = 8"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 13,
41
+ "metadata": {
42
+ "colab": {
43
+ "base_uri": "https://localhost:8080/"
44
+ },
45
+ "id": "b9e6368c",
46
+ "outputId": "fc2888a8-4fe9-4d61-dd2d-753e751e1416"
47
+ },
48
+ "outputs": [
49
+ {
50
+ "data": {
51
+ "text/plain": [
52
+ "<module 'peft' from '/usr/local/lib/python3.10/dist-packages/peft/__init__.py'>"
53
+ ]
54
+ },
55
+ "execution_count": 13,
56
+ "metadata": {},
57
+ "output_type": "execute_result"
58
+ }
59
+ ],
60
+ "source": [
61
+ "import importlib\n",
62
+ "\n",
63
+ "importlib.reload(peft)"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 14,
69
+ "metadata": {
70
+ "id": "8d0850ac"
71
+ },
72
+ "outputs": [],
73
+ "source": [
74
+ "# creating model\n",
75
+ "peft_config = IA3Config(task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, feedforward_modules=[])\n",
76
+ "\n",
77
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": 15,
83
+ "metadata": {
84
+ "colab": {
85
+ "base_uri": "https://localhost:8080/"
86
+ },
87
+ "id": "e10c3831",
88
+ "outputId": "e69c5e07-ae58-446c-8301-e99ac6b85d62"
89
+ },
90
+ "outputs": [
91
+ {
92
+ "data": {
93
+ "text/plain": [
94
+ "MT5ForConditionalGeneration(\n",
95
+ " (shared): Embedding(250112, 1024)\n",
96
+ " (encoder): MT5Stack(\n",
97
+ " (embed_tokens): Embedding(250112, 1024)\n",
98
+ " (block): ModuleList(\n",
99
+ " (0): MT5Block(\n",
100
+ " (layer): ModuleList(\n",
101
+ " (0): MT5LayerSelfAttention(\n",
102
+ " (SelfAttention): MT5Attention(\n",
103
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
104
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
105
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
106
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
107
+ " (relative_attention_bias): Embedding(32, 16)\n",
108
+ " )\n",
109
+ " (layer_norm): MT5LayerNorm()\n",
110
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
111
+ " )\n",
112
+ " (1): MT5LayerFF(\n",
113
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
114
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
115
+ " (wi_1): Linear(in_features=1024, out_features=2816, bias=False)\n",
116
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
117
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
118
+ " (act): NewGELUActivation()\n",
119
+ " )\n",
120
+ " (layer_norm): MT5LayerNorm()\n",
121
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
122
+ " )\n",
123
+ " )\n",
124
+ " )\n",
125
+ " (1-23): 23 x MT5Block(\n",
126
+ " (layer): ModuleList(\n",
127
+ " (0): MT5LayerSelfAttention(\n",
128
+ " (SelfAttention): MT5Attention(\n",
129
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
130
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
131
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
132
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
133
+ " )\n",
134
+ " (layer_norm): MT5LayerNorm()\n",
135
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
136
+ " )\n",
137
+ " (1): MT5LayerFF(\n",
138
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
139
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
140
+ " (wi_1): Linear(in_features=1024, out_features=2816, bias=False)\n",
141
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
142
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
143
+ " (act): NewGELUActivation()\n",
144
+ " )\n",
145
+ " (layer_norm): MT5LayerNorm()\n",
146
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
147
+ " )\n",
148
+ " )\n",
149
+ " )\n",
150
+ " )\n",
151
+ " (final_layer_norm): MT5LayerNorm()\n",
152
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
153
+ " )\n",
154
+ " (decoder): MT5Stack(\n",
155
+ " (embed_tokens): Embedding(250112, 1024)\n",
156
+ " (block): ModuleList(\n",
157
+ " (0): MT5Block(\n",
158
+ " (layer): ModuleList(\n",
159
+ " (0): MT5LayerSelfAttention(\n",
160
+ " (SelfAttention): MT5Attention(\n",
161
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
162
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
163
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
164
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
165
+ " (relative_attention_bias): Embedding(32, 16)\n",
166
+ " )\n",
167
+ " (layer_norm): MT5LayerNorm()\n",
168
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
169
+ " )\n",
170
+ " (1): MT5LayerCrossAttention(\n",
171
+ " (EncDecAttention): MT5Attention(\n",
172
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
173
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
174
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
175
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
176
+ " )\n",
177
+ " (layer_norm): MT5LayerNorm()\n",
178
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
179
+ " )\n",
180
+ " (2): MT5LayerFF(\n",
181
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
182
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
183
+ " (wi_1): Linear(in_features=1024, out_features=2816, bias=False)\n",
184
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
185
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
186
+ " (act): NewGELUActivation()\n",
187
+ " )\n",
188
+ " (layer_norm): MT5LayerNorm()\n",
189
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
190
+ " )\n",
191
+ " )\n",
192
+ " )\n",
193
+ " (1-23): 23 x MT5Block(\n",
194
+ " (layer): ModuleList(\n",
195
+ " (0): MT5LayerSelfAttention(\n",
196
+ " (SelfAttention): MT5Attention(\n",
197
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
198
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
199
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
200
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
201
+ " )\n",
202
+ " (layer_norm): MT5LayerNorm()\n",
203
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
204
+ " )\n",
205
+ " (1): MT5LayerCrossAttention(\n",
206
+ " (EncDecAttention): MT5Attention(\n",
207
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
208
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
209
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
210
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
211
+ " )\n",
212
+ " (layer_norm): MT5LayerNorm()\n",
213
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
214
+ " )\n",
215
+ " (2): MT5LayerFF(\n",
216
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
217
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
218
+ " (wi_1): Linear(in_features=1024, out_features=2816, bias=False)\n",
219
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
220
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
221
+ " (act): NewGELUActivation()\n",
222
+ " )\n",
223
+ " (layer_norm): MT5LayerNorm()\n",
224
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
225
+ " )\n",
226
+ " )\n",
227
+ " )\n",
228
+ " )\n",
229
+ " (final_layer_norm): MT5LayerNorm()\n",
230
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
231
+ " )\n",
232
+ " (lm_head): Linear(in_features=1024, out_features=250112, bias=False)\n",
233
+ ")"
234
+ ]
235
+ },
236
+ "execution_count": 15,
237
+ "metadata": {},
238
+ "output_type": "execute_result"
239
+ }
240
+ ],
241
+ "source": [
242
+ "model"
243
+ ]
244
+ },
245
+ {
246
+ "cell_type": "code",
247
+ "execution_count": 16,
248
+ "metadata": {
249
+ "colab": {
250
+ "base_uri": "https://localhost:8080/"
251
+ },
252
+ "id": "05978e96",
253
+ "outputId": "ea9b7d40-010f-4df0-ec64-a7146a5f8b08"
254
+ },
255
+ "outputs": [
256
+ {
257
+ "name": "stdout",
258
+ "output_type": "stream",
259
+ "text": [
260
+ "trainable params: 282,624 || all params: 1,229,863,936 || trainable%: 0.022980103060766553\n"
261
+ ]
262
+ },
263
+ {
264
+ "data": {
265
+ "text/plain": [
266
+ "PeftModelForSeq2SeqLM(\n",
267
+ " (base_model): IA3Model(\n",
268
+ " (model): MT5ForConditionalGeneration(\n",
269
+ " (shared): Embedding(250112, 1024)\n",
270
+ " (encoder): MT5Stack(\n",
271
+ " (embed_tokens): Embedding(250112, 1024)\n",
272
+ " (block): ModuleList(\n",
273
+ " (0): MT5Block(\n",
274
+ " (layer): ModuleList(\n",
275
+ " (0): MT5LayerSelfAttention(\n",
276
+ " (SelfAttention): MT5Attention(\n",
277
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
278
+ " (k): Linear(\n",
279
+ " in_features=1024, out_features=1024, bias=False\n",
280
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
281
+ " )\n",
282
+ " (v): Linear(\n",
283
+ " in_features=1024, out_features=1024, bias=False\n",
284
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
285
+ " )\n",
286
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
287
+ " (relative_attention_bias): Embedding(32, 16)\n",
288
+ " )\n",
289
+ " (layer_norm): MT5LayerNorm()\n",
290
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
291
+ " )\n",
292
+ " (1): MT5LayerFF(\n",
293
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
294
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
295
+ " (wi_1): Linear(\n",
296
+ " in_features=1024, out_features=2816, bias=False\n",
297
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 2816x1])\n",
298
+ " )\n",
299
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
300
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
301
+ " (act): NewGELUActivation()\n",
302
+ " )\n",
303
+ " (layer_norm): MT5LayerNorm()\n",
304
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
305
+ " )\n",
306
+ " )\n",
307
+ " )\n",
308
+ " (1-23): 23 x MT5Block(\n",
309
+ " (layer): ModuleList(\n",
310
+ " (0): MT5LayerSelfAttention(\n",
311
+ " (SelfAttention): MT5Attention(\n",
312
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
313
+ " (k): Linear(\n",
314
+ " in_features=1024, out_features=1024, bias=False\n",
315
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
316
+ " )\n",
317
+ " (v): Linear(\n",
318
+ " in_features=1024, out_features=1024, bias=False\n",
319
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
320
+ " )\n",
321
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
322
+ " )\n",
323
+ " (layer_norm): MT5LayerNorm()\n",
324
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
325
+ " )\n",
326
+ " (1): MT5LayerFF(\n",
327
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
328
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
329
+ " (wi_1): Linear(\n",
330
+ " in_features=1024, out_features=2816, bias=False\n",
331
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 2816x1])\n",
332
+ " )\n",
333
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
334
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
335
+ " (act): NewGELUActivation()\n",
336
+ " )\n",
337
+ " (layer_norm): MT5LayerNorm()\n",
338
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
339
+ " )\n",
340
+ " )\n",
341
+ " )\n",
342
+ " )\n",
343
+ " (final_layer_norm): MT5LayerNorm()\n",
344
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
345
+ " )\n",
346
+ " (decoder): MT5Stack(\n",
347
+ " (embed_tokens): Embedding(250112, 1024)\n",
348
+ " (block): ModuleList(\n",
349
+ " (0): MT5Block(\n",
350
+ " (layer): ModuleList(\n",
351
+ " (0): MT5LayerSelfAttention(\n",
352
+ " (SelfAttention): MT5Attention(\n",
353
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
354
+ " (k): Linear(\n",
355
+ " in_features=1024, out_features=1024, bias=False\n",
356
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
357
+ " )\n",
358
+ " (v): Linear(\n",
359
+ " in_features=1024, out_features=1024, bias=False\n",
360
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
361
+ " )\n",
362
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
363
+ " (relative_attention_bias): Embedding(32, 16)\n",
364
+ " )\n",
365
+ " (layer_norm): MT5LayerNorm()\n",
366
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
367
+ " )\n",
368
+ " (1): MT5LayerCrossAttention(\n",
369
+ " (EncDecAttention): MT5Attention(\n",
370
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
371
+ " (k): Linear(\n",
372
+ " in_features=1024, out_features=1024, bias=False\n",
373
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
374
+ " )\n",
375
+ " (v): Linear(\n",
376
+ " in_features=1024, out_features=1024, bias=False\n",
377
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
378
+ " )\n",
379
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
380
+ " )\n",
381
+ " (layer_norm): MT5LayerNorm()\n",
382
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
383
+ " )\n",
384
+ " (2): MT5LayerFF(\n",
385
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
386
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
387
+ " (wi_1): Linear(\n",
388
+ " in_features=1024, out_features=2816, bias=False\n",
389
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 2816x1])\n",
390
+ " )\n",
391
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
392
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
393
+ " (act): NewGELUActivation()\n",
394
+ " )\n",
395
+ " (layer_norm): MT5LayerNorm()\n",
396
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
397
+ " )\n",
398
+ " )\n",
399
+ " )\n",
400
+ " (1-23): 23 x MT5Block(\n",
401
+ " (layer): ModuleList(\n",
402
+ " (0): MT5LayerSelfAttention(\n",
403
+ " (SelfAttention): MT5Attention(\n",
404
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
405
+ " (k): Linear(\n",
406
+ " in_features=1024, out_features=1024, bias=False\n",
407
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
408
+ " )\n",
409
+ " (v): Linear(\n",
410
+ " in_features=1024, out_features=1024, bias=False\n",
411
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
412
+ " )\n",
413
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
414
+ " )\n",
415
+ " (layer_norm): MT5LayerNorm()\n",
416
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
417
+ " )\n",
418
+ " (1): MT5LayerCrossAttention(\n",
419
+ " (EncDecAttention): MT5Attention(\n",
420
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
421
+ " (k): Linear(\n",
422
+ " in_features=1024, out_features=1024, bias=False\n",
423
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
424
+ " )\n",
425
+ " (v): Linear(\n",
426
+ " in_features=1024, out_features=1024, bias=False\n",
427
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 1024x1])\n",
428
+ " )\n",
429
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
430
+ " )\n",
431
+ " (layer_norm): MT5LayerNorm()\n",
432
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
433
+ " )\n",
434
+ " (2): MT5LayerFF(\n",
435
+ " (DenseReluDense): MT5DenseGatedActDense(\n",
436
+ " (wi_0): Linear(in_features=1024, out_features=2816, bias=False)\n",
437
+ " (wi_1): Linear(\n",
438
+ " in_features=1024, out_features=2816, bias=False\n",
439
+ " (ia3_l): ParameterDict( (default): Parameter containing: [torch.FloatTensor of size 2816x1])\n",
440
+ " )\n",
441
+ " (wo): Linear(in_features=2816, out_features=1024, bias=False)\n",
442
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
443
+ " (act): NewGELUActivation()\n",
444
+ " )\n",
445
+ " (layer_norm): MT5LayerNorm()\n",
446
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
447
+ " )\n",
448
+ " )\n",
449
+ " )\n",
450
+ " )\n",
451
+ " (final_layer_norm): MT5LayerNorm()\n",
452
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
453
+ " )\n",
454
+ " (lm_head): Linear(in_features=1024, out_features=250112, bias=False)\n",
455
+ " )\n",
456
+ " )\n",
457
+ ")"
458
+ ]
459
+ },
460
+ "execution_count": 16,
461
+ "metadata": {},
462
+ "output_type": "execute_result"
463
+ }
464
+ ],
465
+ "source": [
466
+ "model = get_peft_model(model, peft_config)\n",
467
+ "model.print_trainable_parameters()\n",
468
+ "model"
469
+ ]
470
+ },
471
+ {
472
+ "cell_type": "code",
473
+ "execution_count": 17,
474
+ "metadata": {
475
+ "colab": {
476
+ "base_uri": "https://localhost:8080/",
477
+ "height": 140,
478
+ "referenced_widgets": [
479
+ "bbfb7533b5ca459194e171df56b79566",
480
+ "c894e8237aa34c56bb250acab1466005",
481
+ "a5a126b229064812bf3dcb228118be50",
482
+ "661e1b29c59a4295b594edfa4f50ff87",
483
+ "1bcba805972b484d8b6aa6542c81841c",
484
+ "e71f5c7f1d5d4f83b58c68d2fa310d9c",
485
+ "6a567e0a1a5447519c5df10e777520cf",
486
+ "7aeca19b84904906a04c12659f84ff9e",
487
+ "dd4b895874ce46ceb1ad0d9bc973f98f",
488
+ "b138f91be7f94008806eaf0a6988bc3f",
489
+ "da14180f51ab44b48470cb9ea74d3864",
490
+ "9e12d97af6124a5a8c6627708b300c1e",
491
+ "faa18df899c14e9cac6721253e6c9128",
492
+ "79d0ede7a5b24756aa6d34fda8c29159",
493
+ "3b175b452f4347558aa3c4501cc90030",
494
+ "fc4637a1b37e4e90874c71aa4271ac74",
495
+ "1b8aada826a0451bb60c418b19178c8c",
496
+ "a91916e02e9c424e881e45b3aa978574",
497
+ "ca509bd409624c998e555c9a779b8aae",
498
+ "9c890fc422954347b86d3bde7a421caf",
499
+ "6f9453484ea94587a64d70f1b3a1f6e4",
500
+ "48770ef159f44c01be2a75c75aecd80f",
501
+ "0c561dab67914ea9b6e1aab803600551",
502
+ "1e021a1954b44d69a90101a96c360661",
503
+ "013e3343285f437a893bdd673fb90e22",
504
+ "28802da68fb04d70b1c6bc511a04676f",
505
+ "94174da0d6554be087d4527bea5b511a",
506
+ "dc8ab16a1e6c4e6893c95ccd16568f9a",
507
+ "72383136663448d89cf3b82b87cbb392",
508
+ "5b1bdaf16cbc473081e4237f839167b9",
509
+ "51f8fb45485540bb985b606d43ae04ea",
510
+ "f760cd4758334ca9a43fd15612fd808b",
511
+ "f60e9915d2a74ca7bc010d7684f5acf6"
512
+ ]
513
+ },
514
+ "id": "4ee2babf",
515
+ "outputId": "3c413083-247d-47da-f25c-032764be0beb"
516
+ },
517
+ "outputs": [
518
+ {
519
+ "name": "stderr",
520
+ "output_type": "stream",
521
+ "text": [
522
+ "WARNING:datasets.builder:Found cached dataset financial_phrasebank (/root/.cache/huggingface/datasets/financial_phrasebank/sentences_allagree/1.0.0/550bde12e6c30e2674da973a55f57edde5181d53f5a5a34c1531c53f93b7e141)\n"
523
+ ]
524
+ },
525
+ {
526
+ "data": {
527
+ "application/vnd.jupyter.widget-view+json": {
528
+ "model_id": "bbfb7533b5ca459194e171df56b79566",
529
+ "version_major": 2,
530
+ "version_minor": 0
531
+ },
532
+ "text/plain": [
533
+ " 0%| | 0/1 [00:00<?, ?it/s]"
534
+ ]
535
+ },
536
+ "metadata": {},
537
+ "output_type": "display_data"
538
+ },
539
+ {
540
+ "data": {
541
+ "application/vnd.jupyter.widget-view+json": {
542
+ "model_id": "9e12d97af6124a5a8c6627708b300c1e",
543
+ "version_major": 2,
544
+ "version_minor": 0
545
+ },
546
+ "text/plain": [
547
+ "Map: 0%| | 0/2037 [00:00<?, ? examples/s]"
548
+ ]
549
+ },
550
+ "metadata": {},
551
+ "output_type": "display_data"
552
+ },
553
+ {
554
+ "data": {
555
+ "application/vnd.jupyter.widget-view+json": {
556
+ "model_id": "0c561dab67914ea9b6e1aab803600551",
557
+ "version_major": 2,
558
+ "version_minor": 0
559
+ },
560
+ "text/plain": [
561
+ "Map: 0%| | 0/227 [00:00<?, ? examples/s]"
562
+ ]
563
+ },
564
+ "metadata": {},
565
+ "output_type": "display_data"
566
+ },
567
+ {
568
+ "data": {
569
+ "text/plain": [
570
+ "{'sentence': 'It will be operated by Nokia , and supported by its Nokia NetAct network and service management system .',\n",
571
+ " 'label': 1,\n",
572
+ " 'text_label': 'neutral'}"
573
+ ]
574
+ },
575
+ "execution_count": 17,
576
+ "metadata": {},
577
+ "output_type": "execute_result"
578
+ }
579
+ ],
580
+ "source": [
581
+ "# loading dataset\n",
582
+ "dataset = load_dataset(\"financial_phrasebank\", \"sentences_allagree\")\n",
583
+ "dataset = dataset[\"train\"].train_test_split(test_size=0.1)\n",
584
+ "dataset[\"validation\"] = dataset[\"test\"]\n",
585
+ "del dataset[\"test\"]\n",
586
+ "\n",
587
+ "classes = dataset[\"train\"].features[\"label\"].names\n",
588
+ "dataset = dataset.map(\n",
589
+ " lambda x: {\"text_label\": [classes[label] for label in x[\"label\"]]},\n",
590
+ " batched=True,\n",
591
+ " num_proc=1,\n",
592
+ ")\n",
593
+ "\n",
594
+ "dataset[\"train\"][0]"
595
+ ]
596
+ },
597
+ {
598
+ "cell_type": "code",
599
+ "execution_count": 18,
600
+ "metadata": {
601
+ "colab": {
602
+ "base_uri": "https://localhost:8080/",
603
+ "height": 17,
604
+ "referenced_widgets": [
605
+ "e1e80a68a9e7429397cafc96c3c11f80",
606
+ "5307864c2b1143f4b44f3f172611113e",
607
+ "2e2b6c3f48974ea4aca9b7710a03379e",
608
+ "aae78f9bd53348bda45967a38736cb78",
609
+ "34db17e0f28d40d6abafb8acd5dda379",
610
+ "8361dc2e0a834da6a0ad87f7b0cb4e1b",
611
+ "56f1d9d56dd44c8aa923d09a59cb0ebc",
612
+ "d93bfb366db14c2fa77b038752f69b38",
613
+ "749aaa39135841f98b344ffb840df3d4",
614
+ "5e5aa58adb0f48579871df33845e30b1",
615
+ "c25b49b7adaa48a0a3a306aa1e0661b4",
616
+ "21f582e1208a4a38ae3c0cdce87e5c14",
617
+ "d9d37b8b79f24dbf837327a250a5a346",
618
+ "8ba99043c350456d8623ce1d8c98f7a0",
619
+ "8bf37c12d5f74f7d8dbba423a9ee3ac3",
620
+ "f9d86ad7fa734f3a857505a542256a3c",
621
+ "86bf02b06ed740a88015c2b944205c1e",
622
+ "aef6a6be67f749908060d8038b6d3804",
623
+ "664c02903cb248fb9339805bccfd6c1d",
624
+ "82195b807b664a9585a76e0e50fe7609",
625
+ "8621932be14f42858d841e2ac1b173e7",
626
+ "71bcdb1e02144c9587879d8d815b91d4"
627
+ ]
628
+ },
629
+ "id": "adf9608c",
630
+ "outputId": "3e4bc95f-1dc4-4d34-c212-6d2374359673"
631
+ },
632
+ "outputs": [
633
+ {
634
+ "data": {
635
+ "application/vnd.jupyter.widget-view+json": {
636
+ "model_id": "e1e80a68a9e7429397cafc96c3c11f80",
637
+ "version_major": 2,
638
+ "version_minor": 0
639
+ },
640
+ "text/plain": [
641
+ "Running tokenizer on dataset: 0%| | 0/2037 [00:00<?, ? examples/s]"
642
+ ]
643
+ },
644
+ "metadata": {},
645
+ "output_type": "display_data"
646
+ },
647
+ {
648
+ "data": {
649
+ "application/vnd.jupyter.widget-view+json": {
650
+ "model_id": "21f582e1208a4a38ae3c0cdce87e5c14",
651
+ "version_major": 2,
652
+ "version_minor": 0
653
+ },
654
+ "text/plain": [
655
+ "Running tokenizer on dataset: 0%| | 0/227 [00:00<?, ? examples/s]"
656
+ ]
657
+ },
658
+ "metadata": {},
659
+ "output_type": "display_data"
660
+ }
661
+ ],
662
+ "source": [
663
+ "# data preprocessing\n",
664
+ "tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)\n",
665
+ "\n",
666
+ "\n",
667
+ "def preprocess_function(examples):\n",
668
+ " inputs = examples[text_column]\n",
669
+ " targets = examples[label_column]\n",
670
+ " model_inputs = tokenizer(inputs, max_length=max_length, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
671
+ " labels = tokenizer(targets, max_length=3, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
672
+ " labels = labels[\"input_ids\"]\n",
673
+ " labels[labels == tokenizer.pad_token_id] = -100\n",
674
+ " model_inputs[\"labels\"] = labels\n",
675
+ " return model_inputs\n",
676
+ "\n",
677
+ "\n",
678
+ "processed_datasets = dataset.map(\n",
679
+ " preprocess_function,\n",
680
+ " batched=True,\n",
681
+ " num_proc=1,\n",
682
+ " remove_columns=dataset[\"train\"].column_names,\n",
683
+ " load_from_cache_file=False,\n",
684
+ " desc=\"Running tokenizer on dataset\",\n",
685
+ ")\n",
686
+ "\n",
687
+ "train_dataset = processed_datasets[\"train\"]\n",
688
+ "eval_dataset = processed_datasets[\"validation\"]\n",
689
+ "\n",
690
+ "train_dataloader = DataLoader(\n",
691
+ " train_dataset, shuffle=True, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True\n",
692
+ ")\n",
693
+ "eval_dataloader = DataLoader(eval_dataset, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True)"
694
+ ]
695
+ },
696
+ {
697
+ "cell_type": "code",
698
+ "execution_count": 19,
699
+ "metadata": {
700
+ "id": "f733a3c6"
701
+ },
702
+ "outputs": [],
703
+ "source": [
704
+ "# optimizer and lr scheduler\n",
705
+ "optimizer = torch.optim.AdamW(model.parameters(), lr=lr)\n",
706
+ "lr_scheduler = get_linear_schedule_with_warmup(\n",
707
+ " optimizer=optimizer,\n",
708
+ " num_warmup_steps=0,\n",
709
+ " num_training_steps=(len(train_dataloader) * num_epochs),\n",
710
+ ")"
711
+ ]
712
+ },
713
+ {
714
+ "cell_type": "code",
715
+ "execution_count": 20,
716
+ "metadata": {
717
+ "colab": {
718
+ "base_uri": "https://localhost:8080/"
719
+ },
720
+ "id": "6b3a4090",
721
+ "outputId": "369cfce9-90f2-47a1-8653-ea1168943949"
722
+ },
723
+ "outputs": [
724
+ {
725
+ "name": "stderr",
726
+ "output_type": "stream",
727
+ "text": [
728
+ "100%|██████████| 255/255 [02:33<00:00, 1.67it/s]\n",
729
+ "100%|██████████| 29/29 [00:08<00:00, 3.48it/s]\n"
730
+ ]
731
+ },
732
+ {
733
+ "name": "stdout",
734
+ "output_type": "stream",
735
+ "text": [
736
+ "epoch=0: train_ppl=tensor(1.4939, device='cuda:0') train_epoch_loss=tensor(0.4014, device='cuda:0') eval_ppl=tensor(1.0514, device='cuda:0') eval_epoch_loss=tensor(0.0501, device='cuda:0')\n"
737
+ ]
738
+ },
739
+ {
740
+ "name": "stderr",
741
+ "output_type": "stream",
742
+ "text": [
743
+ "100%|██████████| 255/255 [02:32<00:00, 1.67it/s]\n",
744
+ "100%|██████████| 29/29 [00:08<00:00, 3.43it/s]\n"
745
+ ]
746
+ },
747
+ {
748
+ "name": "stdout",
749
+ "output_type": "stream",
750
+ "text": [
751
+ "epoch=1: train_ppl=tensor(1.0523, device='cuda:0') train_epoch_loss=tensor(0.0510, device='cuda:0') eval_ppl=tensor(1.0383, device='cuda:0') eval_epoch_loss=tensor(0.0376, device='cuda:0')\n"
752
+ ]
753
+ },
754
+ {
755
+ "name": "stderr",
756
+ "output_type": "stream",
757
+ "text": [
758
+ "100%|██████████| 255/255 [02:32<00:00, 1.68it/s]\n",
759
+ "100%|██████████| 29/29 [00:08<00:00, 3.44it/s]"
760
+ ]
761
+ },
762
+ {
763
+ "name": "stdout",
764
+ "output_type": "stream",
765
+ "text": [
766
+ "epoch=2: train_ppl=tensor(1.0397, device='cuda:0') train_epoch_loss=tensor(0.0389, device='cuda:0') eval_ppl=tensor(1.0392, device='cuda:0') eval_epoch_loss=tensor(0.0385, device='cuda:0')\n"
767
+ ]
768
+ },
769
+ {
770
+ "name": "stderr",
771
+ "output_type": "stream",
772
+ "text": [
773
+ "\n"
774
+ ]
775
+ }
776
+ ],
777
+ "source": [
778
+ "# training and evaluation\n",
779
+ "model = model.to(device)\n",
780
+ "\n",
781
+ "for epoch in range(num_epochs):\n",
782
+ " model.train()\n",
783
+ " total_loss = 0\n",
784
+ " for step, batch in enumerate(tqdm(train_dataloader)):\n",
785
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
786
+ " outputs = model(**batch)\n",
787
+ " loss = outputs.loss\n",
788
+ " total_loss += loss.detach().float()\n",
789
+ " loss.backward()\n",
790
+ " optimizer.step()\n",
791
+ " lr_scheduler.step()\n",
792
+ " optimizer.zero_grad()\n",
793
+ "\n",
794
+ " model.eval()\n",
795
+ " eval_loss = 0\n",
796
+ " eval_preds = []\n",
797
+ " for step, batch in enumerate(tqdm(eval_dataloader)):\n",
798
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
799
+ " with torch.no_grad():\n",
800
+ " outputs = model(**batch)\n",
801
+ " loss = outputs.loss\n",
802
+ " eval_loss += loss.detach().float()\n",
803
+ " eval_preds.extend(\n",
804
+ " tokenizer.batch_decode(torch.argmax(outputs.logits, -1).detach().cpu().numpy(), skip_special_tokens=True)\n",
805
+ " )\n",
806
+ "\n",
807
+ " eval_epoch_loss = eval_loss / len(eval_dataloader)\n",
808
+ " eval_ppl = torch.exp(eval_epoch_loss)\n",
809
+ " train_epoch_loss = total_loss / len(train_dataloader)\n",
810
+ " train_ppl = torch.exp(train_epoch_loss)\n",
811
+ " print(f\"{epoch=}: {train_ppl=} {train_epoch_loss=} {eval_ppl=} {eval_epoch_loss=}\")"
812
+ ]
813
+ },
814
+ {
815
+ "cell_type": "code",
816
+ "execution_count": 21,
817
+ "metadata": {
818
+ "colab": {
819
+ "base_uri": "https://localhost:8080/"
820
+ },
821
+ "id": "6cafa67b",
822
+ "outputId": "0db923d2-522c-4cb7-b694-6e2e20beae98"
823
+ },
824
+ "outputs": [
825
+ {
826
+ "name": "stdout",
827
+ "output_type": "stream",
828
+ "text": [
829
+ "accuracy=96.91629955947137 % on the evaluation dataset\n",
830
+ "eval_preds[:10]=['neutral', 'neutral', 'neutral', 'neutral', 'positive', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral']\n",
831
+ "dataset['validation']['text_label'][:10]=['neutral', 'neutral', 'neutral', 'neutral', 'positive', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral']\n"
832
+ ]
833
+ }
834
+ ],
835
+ "source": [
836
+ "# print accuracy\n",
837
+ "correct = 0\n",
838
+ "total = 0\n",
839
+ "for pred, true in zip(eval_preds, dataset[\"validation\"][\"text_label\"]):\n",
840
+ " if pred.strip() == true.strip():\n",
841
+ " correct += 1\n",
842
+ " total += 1\n",
843
+ "accuracy = correct / total * 100\n",
844
+ "print(f\"{accuracy=} % on the evaluation dataset\")\n",
845
+ "print(f\"{eval_preds[:10]=}\")\n",
846
+ "print(f\"{dataset['validation']['text_label'][:10]=}\")"
847
+ ]
848
+ },
849
+ {
850
+ "cell_type": "code",
851
+ "execution_count": 22,
852
+ "metadata": {
853
+ "id": "a8de6005"
854
+ },
855
+ "outputs": [],
856
+ "source": [
857
+ "# saving model\n",
858
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
859
+ "model.save_pretrained(peft_model_id)"
860
+ ]
861
+ },
862
+ {
863
+ "cell_type": "code",
864
+ "execution_count": 23,
865
+ "metadata": {
866
+ "colab": {
867
+ "base_uri": "https://localhost:8080/"
868
+ },
869
+ "id": "bd20cd4c",
870
+ "outputId": "0f25d837-80b1-476f-c897-92c3fef04fb2"
871
+ },
872
+ "outputs": [
873
+ {
874
+ "name": "stdout",
875
+ "output_type": "stream",
876
+ "text": [
877
+ "1.2M\tbigscience/mt0-large_IA3_SEQ_2_SEQ_LM/adapter_model.bin\n"
878
+ ]
879
+ }
880
+ ],
881
+ "source": [
882
+ "ckpt = f\"{peft_model_id}/adapter_model.bin\"\n",
883
+ "!du -h $ckpt"
884
+ ]
885
+ },
886
+ {
887
+ "cell_type": "code",
888
+ "execution_count": 24,
889
+ "metadata": {
890
+ "id": "76c2fc29"
891
+ },
892
+ "outputs": [],
893
+ "source": [
894
+ "from peft import PeftModel, PeftConfig\n",
895
+ "\n",
896
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
897
+ "\n",
898
+ "config = PeftConfig.from_pretrained(peft_model_id)\n",
899
+ "model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)\n",
900
+ "model = PeftModel.from_pretrained(model, peft_model_id)"
901
+ ]
902
+ },
903
+ {
904
+ "cell_type": "code",
905
+ "execution_count": 25,
906
+ "metadata": {
907
+ "colab": {
908
+ "base_uri": "https://localhost:8080/"
909
+ },
910
+ "id": "37d712ce",
911
+ "outputId": "4828819a-b640-4f6c-91e3-878b648e9a75"
912
+ },
913
+ "outputs": [
914
+ {
915
+ "name": "stdout",
916
+ "output_type": "stream",
917
+ "text": [
918
+ "25 November 2010 - Finnish paints and coatings company Tikkurila Oyj ( HEL : TIK1V ) said today that Finnish state-owned investment company Solidium Oy sold its 14.7 % stake in the company for a total of EUR98m .\n",
919
+ "{'input_ids': tensor([[ 877, 3277, 1068, 259, 264, 515, 143136, 42068, 263,\n",
920
+ " 305, 259, 101264, 263, 5835, 22538, 4496, 2697, 20860,\n",
921
+ " 385, 274, 76347, 259, 267, 259, 93686, 353, 561,\n",
922
+ " 259, 271, 2426, 7883, 533, 515, 143136, 6509, 264,\n",
923
+ " 45815, 37624, 5835, 35133, 16558, 20860, 22026, 2476, 5006,\n",
924
+ " 487, 1448, 259, 96189, 281, 287, 5835, 332, 259,\n",
925
+ " 262, 2725, 304, 2687, 5577, 282, 259, 260, 1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
926
+ " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
927
+ " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}\n",
928
+ "tensor([[ 0, 59006, 1]])\n",
929
+ "['neutral']\n"
930
+ ]
931
+ }
932
+ ],
933
+ "source": [
934
+ "model.eval()\n",
935
+ "i = 13\n",
936
+ "inputs = tokenizer(dataset[\"validation\"][text_column][i], return_tensors=\"pt\")\n",
937
+ "print(dataset[\"validation\"][text_column][i])\n",
938
+ "print(inputs)\n",
939
+ "\n",
940
+ "with torch.no_grad():\n",
941
+ " outputs = model.generate(input_ids=inputs[\"input_ids\"], max_new_tokens=10)\n",
942
+ " print(outputs)\n",
943
+ " print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True))"
944
+ ]
945
+ },
946
+ {
947
+ "cell_type": "code",
948
+ "execution_count": null,
949
+ "metadata": {
950
+ "id": "66c65ea4"
951
+ },
952
+ "outputs": [],
953
+ "source": []
954
+ },
955
+ {
956
+ "cell_type": "code",
957
+ "execution_count": null,
958
+ "metadata": {
959
+ "id": "65e71f78"
960
+ },
961
+ "outputs": [],
962
+ "source": []
963
+ }
964
+ ],
965
+ "metadata": {
966
+ "accelerator": "GPU",
967
+ "colab": {
968
+ "gpuType": "T4",
969
+ "machine_shape": "hm",
970
+ "provenance": []
971
+ },
972
+ "kernelspec": {
973
+ "display_name": "Python 3",
974
+ "language": "python",
975
+ "name": "python3"
976
+ },
977
+ "language_info": {
978
+ "codemirror_mode": {
979
+ "name": "ipython",
980
+ "version": 3
981
+ },
982
+ "file_extension": ".py",
983
+ "mimetype": "text/x-python",
984
+ "name": "python",
985
+ "nbconvert_exporter": "python",
986
+ "pygments_lexer": "ipython3",
987
+ "version": "3.8.3"
988
+ },
989
+ "vscode": {
990
+ "interpreter": {
991
+ "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
992
+ }
993
+ },
994
+ "widgets": {
995
+ "application/vnd.jupyter.widget-state+json": {
996
+ "013e3343285f437a893bdd673fb90e22": {
997
+ "model_module": "@jupyter-widgets/controls",
998
+ "model_module_version": "1.5.0",
999
+ "model_name": "FloatProgressModel",
1000
+ "state": {
1001
+ "_dom_classes": [],
1002
+ "_model_module": "@jupyter-widgets/controls",
1003
+ "_model_module_version": "1.5.0",
1004
+ "_model_name": "FloatProgressModel",
1005
+ "_view_count": null,
1006
+ "_view_module": "@jupyter-widgets/controls",
1007
+ "_view_module_version": "1.5.0",
1008
+ "_view_name": "ProgressView",
1009
+ "bar_style": "",
1010
+ "description": "",
1011
+ "description_tooltip": null,
1012
+ "layout": "IPY_MODEL_5b1bdaf16cbc473081e4237f839167b9",
1013
+ "max": 227,
1014
+ "min": 0,
1015
+ "orientation": "horizontal",
1016
+ "style": "IPY_MODEL_51f8fb45485540bb985b606d43ae04ea",
1017
+ "value": 227
1018
+ }
1019
+ },
1020
+ "0c561dab67914ea9b6e1aab803600551": {
1021
+ "model_module": "@jupyter-widgets/controls",
1022
+ "model_module_version": "1.5.0",
1023
+ "model_name": "HBoxModel",
1024
+ "state": {
1025
+ "_dom_classes": [],
1026
+ "_model_module": "@jupyter-widgets/controls",
1027
+ "_model_module_version": "1.5.0",
1028
+ "_model_name": "HBoxModel",
1029
+ "_view_count": null,
1030
+ "_view_module": "@jupyter-widgets/controls",
1031
+ "_view_module_version": "1.5.0",
1032
+ "_view_name": "HBoxView",
1033
+ "box_style": "",
1034
+ "children": [
1035
+ "IPY_MODEL_1e021a1954b44d69a90101a96c360661",
1036
+ "IPY_MODEL_013e3343285f437a893bdd673fb90e22",
1037
+ "IPY_MODEL_28802da68fb04d70b1c6bc511a04676f"
1038
+ ],
1039
+ "layout": "IPY_MODEL_94174da0d6554be087d4527bea5b511a"
1040
+ }
1041
+ },
1042
+ "1b8aada826a0451bb60c418b19178c8c": {
1043
+ "model_module": "@jupyter-widgets/base",
1044
+ "model_module_version": "1.2.0",
1045
+ "model_name": "LayoutModel",
1046
+ "state": {
1047
+ "_model_module": "@jupyter-widgets/base",
1048
+ "_model_module_version": "1.2.0",
1049
+ "_model_name": "LayoutModel",
1050
+ "_view_count": null,
1051
+ "_view_module": "@jupyter-widgets/base",
1052
+ "_view_module_version": "1.2.0",
1053
+ "_view_name": "LayoutView",
1054
+ "align_content": null,
1055
+ "align_items": null,
1056
+ "align_self": null,
1057
+ "border": null,
1058
+ "bottom": null,
1059
+ "display": null,
1060
+ "flex": null,
1061
+ "flex_flow": null,
1062
+ "grid_area": null,
1063
+ "grid_auto_columns": null,
1064
+ "grid_auto_flow": null,
1065
+ "grid_auto_rows": null,
1066
+ "grid_column": null,
1067
+ "grid_gap": null,
1068
+ "grid_row": null,
1069
+ "grid_template_areas": null,
1070
+ "grid_template_columns": null,
1071
+ "grid_template_rows": null,
1072
+ "height": null,
1073
+ "justify_content": null,
1074
+ "justify_items": null,
1075
+ "left": null,
1076
+ "margin": null,
1077
+ "max_height": null,
1078
+ "max_width": null,
1079
+ "min_height": null,
1080
+ "min_width": null,
1081
+ "object_fit": null,
1082
+ "object_position": null,
1083
+ "order": null,
1084
+ "overflow": null,
1085
+ "overflow_x": null,
1086
+ "overflow_y": null,
1087
+ "padding": null,
1088
+ "right": null,
1089
+ "top": null,
1090
+ "visibility": null,
1091
+ "width": null
1092
+ }
1093
+ },
1094
+ "1bcba805972b484d8b6aa6542c81841c": {
1095
+ "model_module": "@jupyter-widgets/base",
1096
+ "model_module_version": "1.2.0",
1097
+ "model_name": "LayoutModel",
1098
+ "state": {
1099
+ "_model_module": "@jupyter-widgets/base",
1100
+ "_model_module_version": "1.2.0",
1101
+ "_model_name": "LayoutModel",
1102
+ "_view_count": null,
1103
+ "_view_module": "@jupyter-widgets/base",
1104
+ "_view_module_version": "1.2.0",
1105
+ "_view_name": "LayoutView",
1106
+ "align_content": null,
1107
+ "align_items": null,
1108
+ "align_self": null,
1109
+ "border": null,
1110
+ "bottom": null,
1111
+ "display": null,
1112
+ "flex": null,
1113
+ "flex_flow": null,
1114
+ "grid_area": null,
1115
+ "grid_auto_columns": null,
1116
+ "grid_auto_flow": null,
1117
+ "grid_auto_rows": null,
1118
+ "grid_column": null,
1119
+ "grid_gap": null,
1120
+ "grid_row": null,
1121
+ "grid_template_areas": null,
1122
+ "grid_template_columns": null,
1123
+ "grid_template_rows": null,
1124
+ "height": null,
1125
+ "justify_content": null,
1126
+ "justify_items": null,
1127
+ "left": null,
1128
+ "margin": null,
1129
+ "max_height": null,
1130
+ "max_width": null,
1131
+ "min_height": null,
1132
+ "min_width": null,
1133
+ "object_fit": null,
1134
+ "object_position": null,
1135
+ "order": null,
1136
+ "overflow": null,
1137
+ "overflow_x": null,
1138
+ "overflow_y": null,
1139
+ "padding": null,
1140
+ "right": null,
1141
+ "top": null,
1142
+ "visibility": null,
1143
+ "width": null
1144
+ }
1145
+ },
1146
+ "1e021a1954b44d69a90101a96c360661": {
1147
+ "model_module": "@jupyter-widgets/controls",
1148
+ "model_module_version": "1.5.0",
1149
+ "model_name": "HTMLModel",
1150
+ "state": {
1151
+ "_dom_classes": [],
1152
+ "_model_module": "@jupyter-widgets/controls",
1153
+ "_model_module_version": "1.5.0",
1154
+ "_model_name": "HTMLModel",
1155
+ "_view_count": null,
1156
+ "_view_module": "@jupyter-widgets/controls",
1157
+ "_view_module_version": "1.5.0",
1158
+ "_view_name": "HTMLView",
1159
+ "description": "",
1160
+ "description_tooltip": null,
1161
+ "layout": "IPY_MODEL_dc8ab16a1e6c4e6893c95ccd16568f9a",
1162
+ "placeholder": "​",
1163
+ "style": "IPY_MODEL_72383136663448d89cf3b82b87cbb392",
1164
+ "value": "Map: 0%"
1165
+ }
1166
+ },
1167
+ "21f582e1208a4a38ae3c0cdce87e5c14": {
1168
+ "model_module": "@jupyter-widgets/controls",
1169
+ "model_module_version": "1.5.0",
1170
+ "model_name": "HBoxModel",
1171
+ "state": {
1172
+ "_dom_classes": [],
1173
+ "_model_module": "@jupyter-widgets/controls",
1174
+ "_model_module_version": "1.5.0",
1175
+ "_model_name": "HBoxModel",
1176
+ "_view_count": null,
1177
+ "_view_module": "@jupyter-widgets/controls",
1178
+ "_view_module_version": "1.5.0",
1179
+ "_view_name": "HBoxView",
1180
+ "box_style": "",
1181
+ "children": [
1182
+ "IPY_MODEL_d9d37b8b79f24dbf837327a250a5a346",
1183
+ "IPY_MODEL_8ba99043c350456d8623ce1d8c98f7a0",
1184
+ "IPY_MODEL_8bf37c12d5f74f7d8dbba423a9ee3ac3"
1185
+ ],
1186
+ "layout": "IPY_MODEL_f9d86ad7fa734f3a857505a542256a3c"
1187
+ }
1188
+ },
1189
+ "28802da68fb04d70b1c6bc511a04676f": {
1190
+ "model_module": "@jupyter-widgets/controls",
1191
+ "model_module_version": "1.5.0",
1192
+ "model_name": "HTMLModel",
1193
+ "state": {
1194
+ "_dom_classes": [],
1195
+ "_model_module": "@jupyter-widgets/controls",
1196
+ "_model_module_version": "1.5.0",
1197
+ "_model_name": "HTMLModel",
1198
+ "_view_count": null,
1199
+ "_view_module": "@jupyter-widgets/controls",
1200
+ "_view_module_version": "1.5.0",
1201
+ "_view_name": "HTMLView",
1202
+ "description": "",
1203
+ "description_tooltip": null,
1204
+ "layout": "IPY_MODEL_f760cd4758334ca9a43fd15612fd808b",
1205
+ "placeholder": "​",
1206
+ "style": "IPY_MODEL_f60e9915d2a74ca7bc010d7684f5acf6",
1207
+ "value": " 0/227 [00:00&lt;?, ? examples/s]"
1208
+ }
1209
+ },
1210
+ "2e2b6c3f48974ea4aca9b7710a03379e": {
1211
+ "model_module": "@jupyter-widgets/controls",
1212
+ "model_module_version": "1.5.0",
1213
+ "model_name": "FloatProgressModel",
1214
+ "state": {
1215
+ "_dom_classes": [],
1216
+ "_model_module": "@jupyter-widgets/controls",
1217
+ "_model_module_version": "1.5.0",
1218
+ "_model_name": "FloatProgressModel",
1219
+ "_view_count": null,
1220
+ "_view_module": "@jupyter-widgets/controls",
1221
+ "_view_module_version": "1.5.0",
1222
+ "_view_name": "ProgressView",
1223
+ "bar_style": "",
1224
+ "description": "",
1225
+ "description_tooltip": null,
1226
+ "layout": "IPY_MODEL_d93bfb366db14c2fa77b038752f69b38",
1227
+ "max": 2037,
1228
+ "min": 0,
1229
+ "orientation": "horizontal",
1230
+ "style": "IPY_MODEL_749aaa39135841f98b344ffb840df3d4",
1231
+ "value": 2037
1232
+ }
1233
+ },
1234
+ "34db17e0f28d40d6abafb8acd5dda379": {
1235
+ "model_module": "@jupyter-widgets/base",
1236
+ "model_module_version": "1.2.0",
1237
+ "model_name": "LayoutModel",
1238
+ "state": {
1239
+ "_model_module": "@jupyter-widgets/base",
1240
+ "_model_module_version": "1.2.0",
1241
+ "_model_name": "LayoutModel",
1242
+ "_view_count": null,
1243
+ "_view_module": "@jupyter-widgets/base",
1244
+ "_view_module_version": "1.2.0",
1245
+ "_view_name": "LayoutView",
1246
+ "align_content": null,
1247
+ "align_items": null,
1248
+ "align_self": null,
1249
+ "border": null,
1250
+ "bottom": null,
1251
+ "display": null,
1252
+ "flex": null,
1253
+ "flex_flow": null,
1254
+ "grid_area": null,
1255
+ "grid_auto_columns": null,
1256
+ "grid_auto_flow": null,
1257
+ "grid_auto_rows": null,
1258
+ "grid_column": null,
1259
+ "grid_gap": null,
1260
+ "grid_row": null,
1261
+ "grid_template_areas": null,
1262
+ "grid_template_columns": null,
1263
+ "grid_template_rows": null,
1264
+ "height": null,
1265
+ "justify_content": null,
1266
+ "justify_items": null,
1267
+ "left": null,
1268
+ "margin": null,
1269
+ "max_height": null,
1270
+ "max_width": null,
1271
+ "min_height": null,
1272
+ "min_width": null,
1273
+ "object_fit": null,
1274
+ "object_position": null,
1275
+ "order": null,
1276
+ "overflow": null,
1277
+ "overflow_x": null,
1278
+ "overflow_y": null,
1279
+ "padding": null,
1280
+ "right": null,
1281
+ "top": null,
1282
+ "visibility": "hidden",
1283
+ "width": null
1284
+ }
1285
+ },
1286
+ "3b175b452f4347558aa3c4501cc90030": {
1287
+ "model_module": "@jupyter-widgets/controls",
1288
+ "model_module_version": "1.5.0",
1289
+ "model_name": "HTMLModel",
1290
+ "state": {
1291
+ "_dom_classes": [],
1292
+ "_model_module": "@jupyter-widgets/controls",
1293
+ "_model_module_version": "1.5.0",
1294
+ "_model_name": "HTMLModel",
1295
+ "_view_count": null,
1296
+ "_view_module": "@jupyter-widgets/controls",
1297
+ "_view_module_version": "1.5.0",
1298
+ "_view_name": "HTMLView",
1299
+ "description": "",
1300
+ "description_tooltip": null,
1301
+ "layout": "IPY_MODEL_6f9453484ea94587a64d70f1b3a1f6e4",
1302
+ "placeholder": "​",
1303
+ "style": "IPY_MODEL_48770ef159f44c01be2a75c75aecd80f",
1304
+ "value": " 0/2037 [00:00&lt;?, ? examples/s]"
1305
+ }
1306
+ },
1307
+ "48770ef159f44c01be2a75c75aecd80f": {
1308
+ "model_module": "@jupyter-widgets/controls",
1309
+ "model_module_version": "1.5.0",
1310
+ "model_name": "DescriptionStyleModel",
1311
+ "state": {
1312
+ "_model_module": "@jupyter-widgets/controls",
1313
+ "_model_module_version": "1.5.0",
1314
+ "_model_name": "DescriptionStyleModel",
1315
+ "_view_count": null,
1316
+ "_view_module": "@jupyter-widgets/base",
1317
+ "_view_module_version": "1.2.0",
1318
+ "_view_name": "StyleView",
1319
+ "description_width": ""
1320
+ }
1321
+ },
1322
+ "51f8fb45485540bb985b606d43ae04ea": {
1323
+ "model_module": "@jupyter-widgets/controls",
1324
+ "model_module_version": "1.5.0",
1325
+ "model_name": "ProgressStyleModel",
1326
+ "state": {
1327
+ "_model_module": "@jupyter-widgets/controls",
1328
+ "_model_module_version": "1.5.0",
1329
+ "_model_name": "ProgressStyleModel",
1330
+ "_view_count": null,
1331
+ "_view_module": "@jupyter-widgets/base",
1332
+ "_view_module_version": "1.2.0",
1333
+ "_view_name": "StyleView",
1334
+ "bar_color": null,
1335
+ "description_width": ""
1336
+ }
1337
+ },
1338
+ "5307864c2b1143f4b44f3f172611113e": {
1339
+ "model_module": "@jupyter-widgets/controls",
1340
+ "model_module_version": "1.5.0",
1341
+ "model_name": "HTMLModel",
1342
+ "state": {
1343
+ "_dom_classes": [],
1344
+ "_model_module": "@jupyter-widgets/controls",
1345
+ "_model_module_version": "1.5.0",
1346
+ "_model_name": "HTMLModel",
1347
+ "_view_count": null,
1348
+ "_view_module": "@jupyter-widgets/controls",
1349
+ "_view_module_version": "1.5.0",
1350
+ "_view_name": "HTMLView",
1351
+ "description": "",
1352
+ "description_tooltip": null,
1353
+ "layout": "IPY_MODEL_8361dc2e0a834da6a0ad87f7b0cb4e1b",
1354
+ "placeholder": "​",
1355
+ "style": "IPY_MODEL_56f1d9d56dd44c8aa923d09a59cb0ebc",
1356
+ "value": "Running tokenizer on dataset: 98%"
1357
+ }
1358
+ },
1359
+ "56f1d9d56dd44c8aa923d09a59cb0ebc": {
1360
+ "model_module": "@jupyter-widgets/controls",
1361
+ "model_module_version": "1.5.0",
1362
+ "model_name": "DescriptionStyleModel",
1363
+ "state": {
1364
+ "_model_module": "@jupyter-widgets/controls",
1365
+ "_model_module_version": "1.5.0",
1366
+ "_model_name": "DescriptionStyleModel",
1367
+ "_view_count": null,
1368
+ "_view_module": "@jupyter-widgets/base",
1369
+ "_view_module_version": "1.2.0",
1370
+ "_view_name": "StyleView",
1371
+ "description_width": ""
1372
+ }
1373
+ },
1374
+ "5b1bdaf16cbc473081e4237f839167b9": {
1375
+ "model_module": "@jupyter-widgets/base",
1376
+ "model_module_version": "1.2.0",
1377
+ "model_name": "LayoutModel",
1378
+ "state": {
1379
+ "_model_module": "@jupyter-widgets/base",
1380
+ "_model_module_version": "1.2.0",
1381
+ "_model_name": "LayoutModel",
1382
+ "_view_count": null,
1383
+ "_view_module": "@jupyter-widgets/base",
1384
+ "_view_module_version": "1.2.0",
1385
+ "_view_name": "LayoutView",
1386
+ "align_content": null,
1387
+ "align_items": null,
1388
+ "align_self": null,
1389
+ "border": null,
1390
+ "bottom": null,
1391
+ "display": null,
1392
+ "flex": null,
1393
+ "flex_flow": null,
1394
+ "grid_area": null,
1395
+ "grid_auto_columns": null,
1396
+ "grid_auto_flow": null,
1397
+ "grid_auto_rows": null,
1398
+ "grid_column": null,
1399
+ "grid_gap": null,
1400
+ "grid_row": null,
1401
+ "grid_template_areas": null,
1402
+ "grid_template_columns": null,
1403
+ "grid_template_rows": null,
1404
+ "height": null,
1405
+ "justify_content": null,
1406
+ "justify_items": null,
1407
+ "left": null,
1408
+ "margin": null,
1409
+ "max_height": null,
1410
+ "max_width": null,
1411
+ "min_height": null,
1412
+ "min_width": null,
1413
+ "object_fit": null,
1414
+ "object_position": null,
1415
+ "order": null,
1416
+ "overflow": null,
1417
+ "overflow_x": null,
1418
+ "overflow_y": null,
1419
+ "padding": null,
1420
+ "right": null,
1421
+ "top": null,
1422
+ "visibility": null,
1423
+ "width": null
1424
+ }
1425
+ },
1426
+ "5e5aa58adb0f48579871df33845e30b1": {
1427
+ "model_module": "@jupyter-widgets/base",
1428
+ "model_module_version": "1.2.0",
1429
+ "model_name": "LayoutModel",
1430
+ "state": {
1431
+ "_model_module": "@jupyter-widgets/base",
1432
+ "_model_module_version": "1.2.0",
1433
+ "_model_name": "LayoutModel",
1434
+ "_view_count": null,
1435
+ "_view_module": "@jupyter-widgets/base",
1436
+ "_view_module_version": "1.2.0",
1437
+ "_view_name": "LayoutView",
1438
+ "align_content": null,
1439
+ "align_items": null,
1440
+ "align_self": null,
1441
+ "border": null,
1442
+ "bottom": null,
1443
+ "display": null,
1444
+ "flex": null,
1445
+ "flex_flow": null,
1446
+ "grid_area": null,
1447
+ "grid_auto_columns": null,
1448
+ "grid_auto_flow": null,
1449
+ "grid_auto_rows": null,
1450
+ "grid_column": null,
1451
+ "grid_gap": null,
1452
+ "grid_row": null,
1453
+ "grid_template_areas": null,
1454
+ "grid_template_columns": null,
1455
+ "grid_template_rows": null,
1456
+ "height": null,
1457
+ "justify_content": null,
1458
+ "justify_items": null,
1459
+ "left": null,
1460
+ "margin": null,
1461
+ "max_height": null,
1462
+ "max_width": null,
1463
+ "min_height": null,
1464
+ "min_width": null,
1465
+ "object_fit": null,
1466
+ "object_position": null,
1467
+ "order": null,
1468
+ "overflow": null,
1469
+ "overflow_x": null,
1470
+ "overflow_y": null,
1471
+ "padding": null,
1472
+ "right": null,
1473
+ "top": null,
1474
+ "visibility": null,
1475
+ "width": null
1476
+ }
1477
+ },
1478
+ "661e1b29c59a4295b594edfa4f50ff87": {
1479
+ "model_module": "@jupyter-widgets/controls",
1480
+ "model_module_version": "1.5.0",
1481
+ "model_name": "HTMLModel",
1482
+ "state": {
1483
+ "_dom_classes": [],
1484
+ "_model_module": "@jupyter-widgets/controls",
1485
+ "_model_module_version": "1.5.0",
1486
+ "_model_name": "HTMLModel",
1487
+ "_view_count": null,
1488
+ "_view_module": "@jupyter-widgets/controls",
1489
+ "_view_module_version": "1.5.0",
1490
+ "_view_name": "HTMLView",
1491
+ "description": "",
1492
+ "description_tooltip": null,
1493
+ "layout": "IPY_MODEL_b138f91be7f94008806eaf0a6988bc3f",
1494
+ "placeholder": "​",
1495
+ "style": "IPY_MODEL_da14180f51ab44b48470cb9ea74d3864",
1496
+ "value": " 1/1 [00:00&lt;00:00, 67.12it/s]"
1497
+ }
1498
+ },
1499
+ "664c02903cb248fb9339805bccfd6c1d": {
1500
+ "model_module": "@jupyter-widgets/base",
1501
+ "model_module_version": "1.2.0",
1502
+ "model_name": "LayoutModel",
1503
+ "state": {
1504
+ "_model_module": "@jupyter-widgets/base",
1505
+ "_model_module_version": "1.2.0",
1506
+ "_model_name": "LayoutModel",
1507
+ "_view_count": null,
1508
+ "_view_module": "@jupyter-widgets/base",
1509
+ "_view_module_version": "1.2.0",
1510
+ "_view_name": "LayoutView",
1511
+ "align_content": null,
1512
+ "align_items": null,
1513
+ "align_self": null,
1514
+ "border": null,
1515
+ "bottom": null,
1516
+ "display": null,
1517
+ "flex": null,
1518
+ "flex_flow": null,
1519
+ "grid_area": null,
1520
+ "grid_auto_columns": null,
1521
+ "grid_auto_flow": null,
1522
+ "grid_auto_rows": null,
1523
+ "grid_column": null,
1524
+ "grid_gap": null,
1525
+ "grid_row": null,
1526
+ "grid_template_areas": null,
1527
+ "grid_template_columns": null,
1528
+ "grid_template_rows": null,
1529
+ "height": null,
1530
+ "justify_content": null,
1531
+ "justify_items": null,
1532
+ "left": null,
1533
+ "margin": null,
1534
+ "max_height": null,
1535
+ "max_width": null,
1536
+ "min_height": null,
1537
+ "min_width": null,
1538
+ "object_fit": null,
1539
+ "object_position": null,
1540
+ "order": null,
1541
+ "overflow": null,
1542
+ "overflow_x": null,
1543
+ "overflow_y": null,
1544
+ "padding": null,
1545
+ "right": null,
1546
+ "top": null,
1547
+ "visibility": null,
1548
+ "width": null
1549
+ }
1550
+ },
1551
+ "6a567e0a1a5447519c5df10e777520cf": {
1552
+ "model_module": "@jupyter-widgets/controls",
1553
+ "model_module_version": "1.5.0",
1554
+ "model_name": "DescriptionStyleModel",
1555
+ "state": {
1556
+ "_model_module": "@jupyter-widgets/controls",
1557
+ "_model_module_version": "1.5.0",
1558
+ "_model_name": "DescriptionStyleModel",
1559
+ "_view_count": null,
1560
+ "_view_module": "@jupyter-widgets/base",
1561
+ "_view_module_version": "1.2.0",
1562
+ "_view_name": "StyleView",
1563
+ "description_width": ""
1564
+ }
1565
+ },
1566
+ "6f9453484ea94587a64d70f1b3a1f6e4": {
1567
+ "model_module": "@jupyter-widgets/base",
1568
+ "model_module_version": "1.2.0",
1569
+ "model_name": "LayoutModel",
1570
+ "state": {
1571
+ "_model_module": "@jupyter-widgets/base",
1572
+ "_model_module_version": "1.2.0",
1573
+ "_model_name": "LayoutModel",
1574
+ "_view_count": null,
1575
+ "_view_module": "@jupyter-widgets/base",
1576
+ "_view_module_version": "1.2.0",
1577
+ "_view_name": "LayoutView",
1578
+ "align_content": null,
1579
+ "align_items": null,
1580
+ "align_self": null,
1581
+ "border": null,
1582
+ "bottom": null,
1583
+ "display": null,
1584
+ "flex": null,
1585
+ "flex_flow": null,
1586
+ "grid_area": null,
1587
+ "grid_auto_columns": null,
1588
+ "grid_auto_flow": null,
1589
+ "grid_auto_rows": null,
1590
+ "grid_column": null,
1591
+ "grid_gap": null,
1592
+ "grid_row": null,
1593
+ "grid_template_areas": null,
1594
+ "grid_template_columns": null,
1595
+ "grid_template_rows": null,
1596
+ "height": null,
1597
+ "justify_content": null,
1598
+ "justify_items": null,
1599
+ "left": null,
1600
+ "margin": null,
1601
+ "max_height": null,
1602
+ "max_width": null,
1603
+ "min_height": null,
1604
+ "min_width": null,
1605
+ "object_fit": null,
1606
+ "object_position": null,
1607
+ "order": null,
1608
+ "overflow": null,
1609
+ "overflow_x": null,
1610
+ "overflow_y": null,
1611
+ "padding": null,
1612
+ "right": null,
1613
+ "top": null,
1614
+ "visibility": null,
1615
+ "width": null
1616
+ }
1617
+ },
1618
+ "71bcdb1e02144c9587879d8d815b91d4": {
1619
+ "model_module": "@jupyter-widgets/controls",
1620
+ "model_module_version": "1.5.0",
1621
+ "model_name": "DescriptionStyleModel",
1622
+ "state": {
1623
+ "_model_module": "@jupyter-widgets/controls",
1624
+ "_model_module_version": "1.5.0",
1625
+ "_model_name": "DescriptionStyleModel",
1626
+ "_view_count": null,
1627
+ "_view_module": "@jupyter-widgets/base",
1628
+ "_view_module_version": "1.2.0",
1629
+ "_view_name": "StyleView",
1630
+ "description_width": ""
1631
+ }
1632
+ },
1633
+ "72383136663448d89cf3b82b87cbb392": {
1634
+ "model_module": "@jupyter-widgets/controls",
1635
+ "model_module_version": "1.5.0",
1636
+ "model_name": "DescriptionStyleModel",
1637
+ "state": {
1638
+ "_model_module": "@jupyter-widgets/controls",
1639
+ "_model_module_version": "1.5.0",
1640
+ "_model_name": "DescriptionStyleModel",
1641
+ "_view_count": null,
1642
+ "_view_module": "@jupyter-widgets/base",
1643
+ "_view_module_version": "1.2.0",
1644
+ "_view_name": "StyleView",
1645
+ "description_width": ""
1646
+ }
1647
+ },
1648
+ "749aaa39135841f98b344ffb840df3d4": {
1649
+ "model_module": "@jupyter-widgets/controls",
1650
+ "model_module_version": "1.5.0",
1651
+ "model_name": "ProgressStyleModel",
1652
+ "state": {
1653
+ "_model_module": "@jupyter-widgets/controls",
1654
+ "_model_module_version": "1.5.0",
1655
+ "_model_name": "ProgressStyleModel",
1656
+ "_view_count": null,
1657
+ "_view_module": "@jupyter-widgets/base",
1658
+ "_view_module_version": "1.2.0",
1659
+ "_view_name": "StyleView",
1660
+ "bar_color": null,
1661
+ "description_width": ""
1662
+ }
1663
+ },
1664
+ "79d0ede7a5b24756aa6d34fda8c29159": {
1665
+ "model_module": "@jupyter-widgets/controls",
1666
+ "model_module_version": "1.5.0",
1667
+ "model_name": "FloatProgressModel",
1668
+ "state": {
1669
+ "_dom_classes": [],
1670
+ "_model_module": "@jupyter-widgets/controls",
1671
+ "_model_module_version": "1.5.0",
1672
+ "_model_name": "FloatProgressModel",
1673
+ "_view_count": null,
1674
+ "_view_module": "@jupyter-widgets/controls",
1675
+ "_view_module_version": "1.5.0",
1676
+ "_view_name": "ProgressView",
1677
+ "bar_style": "",
1678
+ "description": "",
1679
+ "description_tooltip": null,
1680
+ "layout": "IPY_MODEL_ca509bd409624c998e555c9a779b8aae",
1681
+ "max": 2037,
1682
+ "min": 0,
1683
+ "orientation": "horizontal",
1684
+ "style": "IPY_MODEL_9c890fc422954347b86d3bde7a421caf",
1685
+ "value": 2037
1686
+ }
1687
+ },
1688
+ "7aeca19b84904906a04c12659f84ff9e": {
1689
+ "model_module": "@jupyter-widgets/base",
1690
+ "model_module_version": "1.2.0",
1691
+ "model_name": "LayoutModel",
1692
+ "state": {
1693
+ "_model_module": "@jupyter-widgets/base",
1694
+ "_model_module_version": "1.2.0",
1695
+ "_model_name": "LayoutModel",
1696
+ "_view_count": null,
1697
+ "_view_module": "@jupyter-widgets/base",
1698
+ "_view_module_version": "1.2.0",
1699
+ "_view_name": "LayoutView",
1700
+ "align_content": null,
1701
+ "align_items": null,
1702
+ "align_self": null,
1703
+ "border": null,
1704
+ "bottom": null,
1705
+ "display": null,
1706
+ "flex": null,
1707
+ "flex_flow": null,
1708
+ "grid_area": null,
1709
+ "grid_auto_columns": null,
1710
+ "grid_auto_flow": null,
1711
+ "grid_auto_rows": null,
1712
+ "grid_column": null,
1713
+ "grid_gap": null,
1714
+ "grid_row": null,
1715
+ "grid_template_areas": null,
1716
+ "grid_template_columns": null,
1717
+ "grid_template_rows": null,
1718
+ "height": null,
1719
+ "justify_content": null,
1720
+ "justify_items": null,
1721
+ "left": null,
1722
+ "margin": null,
1723
+ "max_height": null,
1724
+ "max_width": null,
1725
+ "min_height": null,
1726
+ "min_width": null,
1727
+ "object_fit": null,
1728
+ "object_position": null,
1729
+ "order": null,
1730
+ "overflow": null,
1731
+ "overflow_x": null,
1732
+ "overflow_y": null,
1733
+ "padding": null,
1734
+ "right": null,
1735
+ "top": null,
1736
+ "visibility": null,
1737
+ "width": null
1738
+ }
1739
+ },
1740
+ "82195b807b664a9585a76e0e50fe7609": {
1741
+ "model_module": "@jupyter-widgets/controls",
1742
+ "model_module_version": "1.5.0",
1743
+ "model_name": "ProgressStyleModel",
1744
+ "state": {
1745
+ "_model_module": "@jupyter-widgets/controls",
1746
+ "_model_module_version": "1.5.0",
1747
+ "_model_name": "ProgressStyleModel",
1748
+ "_view_count": null,
1749
+ "_view_module": "@jupyter-widgets/base",
1750
+ "_view_module_version": "1.2.0",
1751
+ "_view_name": "StyleView",
1752
+ "bar_color": null,
1753
+ "description_width": ""
1754
+ }
1755
+ },
1756
+ "8361dc2e0a834da6a0ad87f7b0cb4e1b": {
1757
+ "model_module": "@jupyter-widgets/base",
1758
+ "model_module_version": "1.2.0",
1759
+ "model_name": "LayoutModel",
1760
+ "state": {
1761
+ "_model_module": "@jupyter-widgets/base",
1762
+ "_model_module_version": "1.2.0",
1763
+ "_model_name": "LayoutModel",
1764
+ "_view_count": null,
1765
+ "_view_module": "@jupyter-widgets/base",
1766
+ "_view_module_version": "1.2.0",
1767
+ "_view_name": "LayoutView",
1768
+ "align_content": null,
1769
+ "align_items": null,
1770
+ "align_self": null,
1771
+ "border": null,
1772
+ "bottom": null,
1773
+ "display": null,
1774
+ "flex": null,
1775
+ "flex_flow": null,
1776
+ "grid_area": null,
1777
+ "grid_auto_columns": null,
1778
+ "grid_auto_flow": null,
1779
+ "grid_auto_rows": null,
1780
+ "grid_column": null,
1781
+ "grid_gap": null,
1782
+ "grid_row": null,
1783
+ "grid_template_areas": null,
1784
+ "grid_template_columns": null,
1785
+ "grid_template_rows": null,
1786
+ "height": null,
1787
+ "justify_content": null,
1788
+ "justify_items": null,
1789
+ "left": null,
1790
+ "margin": null,
1791
+ "max_height": null,
1792
+ "max_width": null,
1793
+ "min_height": null,
1794
+ "min_width": null,
1795
+ "object_fit": null,
1796
+ "object_position": null,
1797
+ "order": null,
1798
+ "overflow": null,
1799
+ "overflow_x": null,
1800
+ "overflow_y": null,
1801
+ "padding": null,
1802
+ "right": null,
1803
+ "top": null,
1804
+ "visibility": null,
1805
+ "width": null
1806
+ }
1807
+ },
1808
+ "8621932be14f42858d841e2ac1b173e7": {
1809
+ "model_module": "@jupyter-widgets/base",
1810
+ "model_module_version": "1.2.0",
1811
+ "model_name": "LayoutModel",
1812
+ "state": {
1813
+ "_model_module": "@jupyter-widgets/base",
1814
+ "_model_module_version": "1.2.0",
1815
+ "_model_name": "LayoutModel",
1816
+ "_view_count": null,
1817
+ "_view_module": "@jupyter-widgets/base",
1818
+ "_view_module_version": "1.2.0",
1819
+ "_view_name": "LayoutView",
1820
+ "align_content": null,
1821
+ "align_items": null,
1822
+ "align_self": null,
1823
+ "border": null,
1824
+ "bottom": null,
1825
+ "display": null,
1826
+ "flex": null,
1827
+ "flex_flow": null,
1828
+ "grid_area": null,
1829
+ "grid_auto_columns": null,
1830
+ "grid_auto_flow": null,
1831
+ "grid_auto_rows": null,
1832
+ "grid_column": null,
1833
+ "grid_gap": null,
1834
+ "grid_row": null,
1835
+ "grid_template_areas": null,
1836
+ "grid_template_columns": null,
1837
+ "grid_template_rows": null,
1838
+ "height": null,
1839
+ "justify_content": null,
1840
+ "justify_items": null,
1841
+ "left": null,
1842
+ "margin": null,
1843
+ "max_height": null,
1844
+ "max_width": null,
1845
+ "min_height": null,
1846
+ "min_width": null,
1847
+ "object_fit": null,
1848
+ "object_position": null,
1849
+ "order": null,
1850
+ "overflow": null,
1851
+ "overflow_x": null,
1852
+ "overflow_y": null,
1853
+ "padding": null,
1854
+ "right": null,
1855
+ "top": null,
1856
+ "visibility": null,
1857
+ "width": null
1858
+ }
1859
+ },
1860
+ "86bf02b06ed740a88015c2b944205c1e": {
1861
+ "model_module": "@jupyter-widgets/base",
1862
+ "model_module_version": "1.2.0",
1863
+ "model_name": "LayoutModel",
1864
+ "state": {
1865
+ "_model_module": "@jupyter-widgets/base",
1866
+ "_model_module_version": "1.2.0",
1867
+ "_model_name": "LayoutModel",
1868
+ "_view_count": null,
1869
+ "_view_module": "@jupyter-widgets/base",
1870
+ "_view_module_version": "1.2.0",
1871
+ "_view_name": "LayoutView",
1872
+ "align_content": null,
1873
+ "align_items": null,
1874
+ "align_self": null,
1875
+ "border": null,
1876
+ "bottom": null,
1877
+ "display": null,
1878
+ "flex": null,
1879
+ "flex_flow": null,
1880
+ "grid_area": null,
1881
+ "grid_auto_columns": null,
1882
+ "grid_auto_flow": null,
1883
+ "grid_auto_rows": null,
1884
+ "grid_column": null,
1885
+ "grid_gap": null,
1886
+ "grid_row": null,
1887
+ "grid_template_areas": null,
1888
+ "grid_template_columns": null,
1889
+ "grid_template_rows": null,
1890
+ "height": null,
1891
+ "justify_content": null,
1892
+ "justify_items": null,
1893
+ "left": null,
1894
+ "margin": null,
1895
+ "max_height": null,
1896
+ "max_width": null,
1897
+ "min_height": null,
1898
+ "min_width": null,
1899
+ "object_fit": null,
1900
+ "object_position": null,
1901
+ "order": null,
1902
+ "overflow": null,
1903
+ "overflow_x": null,
1904
+ "overflow_y": null,
1905
+ "padding": null,
1906
+ "right": null,
1907
+ "top": null,
1908
+ "visibility": null,
1909
+ "width": null
1910
+ }
1911
+ },
1912
+ "8ba99043c350456d8623ce1d8c98f7a0": {
1913
+ "model_module": "@jupyter-widgets/controls",
1914
+ "model_module_version": "1.5.0",
1915
+ "model_name": "FloatProgressModel",
1916
+ "state": {
1917
+ "_dom_classes": [],
1918
+ "_model_module": "@jupyter-widgets/controls",
1919
+ "_model_module_version": "1.5.0",
1920
+ "_model_name": "FloatProgressModel",
1921
+ "_view_count": null,
1922
+ "_view_module": "@jupyter-widgets/controls",
1923
+ "_view_module_version": "1.5.0",
1924
+ "_view_name": "ProgressView",
1925
+ "bar_style": "",
1926
+ "description": "",
1927
+ "description_tooltip": null,
1928
+ "layout": "IPY_MODEL_664c02903cb248fb9339805bccfd6c1d",
1929
+ "max": 227,
1930
+ "min": 0,
1931
+ "orientation": "horizontal",
1932
+ "style": "IPY_MODEL_82195b807b664a9585a76e0e50fe7609",
1933
+ "value": 227
1934
+ }
1935
+ },
1936
+ "8bf37c12d5f74f7d8dbba423a9ee3ac3": {
1937
+ "model_module": "@jupyter-widgets/controls",
1938
+ "model_module_version": "1.5.0",
1939
+ "model_name": "HTMLModel",
1940
+ "state": {
1941
+ "_dom_classes": [],
1942
+ "_model_module": "@jupyter-widgets/controls",
1943
+ "_model_module_version": "1.5.0",
1944
+ "_model_name": "HTMLModel",
1945
+ "_view_count": null,
1946
+ "_view_module": "@jupyter-widgets/controls",
1947
+ "_view_module_version": "1.5.0",
1948
+ "_view_name": "HTMLView",
1949
+ "description": "",
1950
+ "description_tooltip": null,
1951
+ "layout": "IPY_MODEL_8621932be14f42858d841e2ac1b173e7",
1952
+ "placeholder": "​",
1953
+ "style": "IPY_MODEL_71bcdb1e02144c9587879d8d815b91d4",
1954
+ "value": " 0/227 [00:00&lt;?, ? examples/s]"
1955
+ }
1956
+ },
1957
+ "94174da0d6554be087d4527bea5b511a": {
1958
+ "model_module": "@jupyter-widgets/base",
1959
+ "model_module_version": "1.2.0",
1960
+ "model_name": "LayoutModel",
1961
+ "state": {
1962
+ "_model_module": "@jupyter-widgets/base",
1963
+ "_model_module_version": "1.2.0",
1964
+ "_model_name": "LayoutModel",
1965
+ "_view_count": null,
1966
+ "_view_module": "@jupyter-widgets/base",
1967
+ "_view_module_version": "1.2.0",
1968
+ "_view_name": "LayoutView",
1969
+ "align_content": null,
1970
+ "align_items": null,
1971
+ "align_self": null,
1972
+ "border": null,
1973
+ "bottom": null,
1974
+ "display": null,
1975
+ "flex": null,
1976
+ "flex_flow": null,
1977
+ "grid_area": null,
1978
+ "grid_auto_columns": null,
1979
+ "grid_auto_flow": null,
1980
+ "grid_auto_rows": null,
1981
+ "grid_column": null,
1982
+ "grid_gap": null,
1983
+ "grid_row": null,
1984
+ "grid_template_areas": null,
1985
+ "grid_template_columns": null,
1986
+ "grid_template_rows": null,
1987
+ "height": null,
1988
+ "justify_content": null,
1989
+ "justify_items": null,
1990
+ "left": null,
1991
+ "margin": null,
1992
+ "max_height": null,
1993
+ "max_width": null,
1994
+ "min_height": null,
1995
+ "min_width": null,
1996
+ "object_fit": null,
1997
+ "object_position": null,
1998
+ "order": null,
1999
+ "overflow": null,
2000
+ "overflow_x": null,
2001
+ "overflow_y": null,
2002
+ "padding": null,
2003
+ "right": null,
2004
+ "top": null,
2005
+ "visibility": "hidden",
2006
+ "width": null
2007
+ }
2008
+ },
2009
+ "9c890fc422954347b86d3bde7a421caf": {
2010
+ "model_module": "@jupyter-widgets/controls",
2011
+ "model_module_version": "1.5.0",
2012
+ "model_name": "ProgressStyleModel",
2013
+ "state": {
2014
+ "_model_module": "@jupyter-widgets/controls",
2015
+ "_model_module_version": "1.5.0",
2016
+ "_model_name": "ProgressStyleModel",
2017
+ "_view_count": null,
2018
+ "_view_module": "@jupyter-widgets/base",
2019
+ "_view_module_version": "1.2.0",
2020
+ "_view_name": "StyleView",
2021
+ "bar_color": null,
2022
+ "description_width": ""
2023
+ }
2024
+ },
2025
+ "9e12d97af6124a5a8c6627708b300c1e": {
2026
+ "model_module": "@jupyter-widgets/controls",
2027
+ "model_module_version": "1.5.0",
2028
+ "model_name": "HBoxModel",
2029
+ "state": {
2030
+ "_dom_classes": [],
2031
+ "_model_module": "@jupyter-widgets/controls",
2032
+ "_model_module_version": "1.5.0",
2033
+ "_model_name": "HBoxModel",
2034
+ "_view_count": null,
2035
+ "_view_module": "@jupyter-widgets/controls",
2036
+ "_view_module_version": "1.5.0",
2037
+ "_view_name": "HBoxView",
2038
+ "box_style": "",
2039
+ "children": [
2040
+ "IPY_MODEL_faa18df899c14e9cac6721253e6c9128",
2041
+ "IPY_MODEL_79d0ede7a5b24756aa6d34fda8c29159",
2042
+ "IPY_MODEL_3b175b452f4347558aa3c4501cc90030"
2043
+ ],
2044
+ "layout": "IPY_MODEL_fc4637a1b37e4e90874c71aa4271ac74"
2045
+ }
2046
+ },
2047
+ "a5a126b229064812bf3dcb228118be50": {
2048
+ "model_module": "@jupyter-widgets/controls",
2049
+ "model_module_version": "1.5.0",
2050
+ "model_name": "FloatProgressModel",
2051
+ "state": {
2052
+ "_dom_classes": [],
2053
+ "_model_module": "@jupyter-widgets/controls",
2054
+ "_model_module_version": "1.5.0",
2055
+ "_model_name": "FloatProgressModel",
2056
+ "_view_count": null,
2057
+ "_view_module": "@jupyter-widgets/controls",
2058
+ "_view_module_version": "1.5.0",
2059
+ "_view_name": "ProgressView",
2060
+ "bar_style": "success",
2061
+ "description": "",
2062
+ "description_tooltip": null,
2063
+ "layout": "IPY_MODEL_7aeca19b84904906a04c12659f84ff9e",
2064
+ "max": 1,
2065
+ "min": 0,
2066
+ "orientation": "horizontal",
2067
+ "style": "IPY_MODEL_dd4b895874ce46ceb1ad0d9bc973f98f",
2068
+ "value": 1
2069
+ }
2070
+ },
2071
+ "a91916e02e9c424e881e45b3aa978574": {
2072
+ "model_module": "@jupyter-widgets/controls",
2073
+ "model_module_version": "1.5.0",
2074
+ "model_name": "DescriptionStyleModel",
2075
+ "state": {
2076
+ "_model_module": "@jupyter-widgets/controls",
2077
+ "_model_module_version": "1.5.0",
2078
+ "_model_name": "DescriptionStyleModel",
2079
+ "_view_count": null,
2080
+ "_view_module": "@jupyter-widgets/base",
2081
+ "_view_module_version": "1.2.0",
2082
+ "_view_name": "StyleView",
2083
+ "description_width": ""
2084
+ }
2085
+ },
2086
+ "aae78f9bd53348bda45967a38736cb78": {
2087
+ "model_module": "@jupyter-widgets/controls",
2088
+ "model_module_version": "1.5.0",
2089
+ "model_name": "HTMLModel",
2090
+ "state": {
2091
+ "_dom_classes": [],
2092
+ "_model_module": "@jupyter-widgets/controls",
2093
+ "_model_module_version": "1.5.0",
2094
+ "_model_name": "HTMLModel",
2095
+ "_view_count": null,
2096
+ "_view_module": "@jupyter-widgets/controls",
2097
+ "_view_module_version": "1.5.0",
2098
+ "_view_name": "HTMLView",
2099
+ "description": "",
2100
+ "description_tooltip": null,
2101
+ "layout": "IPY_MODEL_5e5aa58adb0f48579871df33845e30b1",
2102
+ "placeholder": "​",
2103
+ "style": "IPY_MODEL_c25b49b7adaa48a0a3a306aa1e0661b4",
2104
+ "value": " 2000/2037 [00:00&lt;00:00, 3864.28 examples/s]"
2105
+ }
2106
+ },
2107
+ "aef6a6be67f749908060d8038b6d3804": {
2108
+ "model_module": "@jupyter-widgets/controls",
2109
+ "model_module_version": "1.5.0",
2110
+ "model_name": "DescriptionStyleModel",
2111
+ "state": {
2112
+ "_model_module": "@jupyter-widgets/controls",
2113
+ "_model_module_version": "1.5.0",
2114
+ "_model_name": "DescriptionStyleModel",
2115
+ "_view_count": null,
2116
+ "_view_module": "@jupyter-widgets/base",
2117
+ "_view_module_version": "1.2.0",
2118
+ "_view_name": "StyleView",
2119
+ "description_width": ""
2120
+ }
2121
+ },
2122
+ "b138f91be7f94008806eaf0a6988bc3f": {
2123
+ "model_module": "@jupyter-widgets/base",
2124
+ "model_module_version": "1.2.0",
2125
+ "model_name": "LayoutModel",
2126
+ "state": {
2127
+ "_model_module": "@jupyter-widgets/base",
2128
+ "_model_module_version": "1.2.0",
2129
+ "_model_name": "LayoutModel",
2130
+ "_view_count": null,
2131
+ "_view_module": "@jupyter-widgets/base",
2132
+ "_view_module_version": "1.2.0",
2133
+ "_view_name": "LayoutView",
2134
+ "align_content": null,
2135
+ "align_items": null,
2136
+ "align_self": null,
2137
+ "border": null,
2138
+ "bottom": null,
2139
+ "display": null,
2140
+ "flex": null,
2141
+ "flex_flow": null,
2142
+ "grid_area": null,
2143
+ "grid_auto_columns": null,
2144
+ "grid_auto_flow": null,
2145
+ "grid_auto_rows": null,
2146
+ "grid_column": null,
2147
+ "grid_gap": null,
2148
+ "grid_row": null,
2149
+ "grid_template_areas": null,
2150
+ "grid_template_columns": null,
2151
+ "grid_template_rows": null,
2152
+ "height": null,
2153
+ "justify_content": null,
2154
+ "justify_items": null,
2155
+ "left": null,
2156
+ "margin": null,
2157
+ "max_height": null,
2158
+ "max_width": null,
2159
+ "min_height": null,
2160
+ "min_width": null,
2161
+ "object_fit": null,
2162
+ "object_position": null,
2163
+ "order": null,
2164
+ "overflow": null,
2165
+ "overflow_x": null,
2166
+ "overflow_y": null,
2167
+ "padding": null,
2168
+ "right": null,
2169
+ "top": null,
2170
+ "visibility": null,
2171
+ "width": null
2172
+ }
2173
+ },
2174
+ "bbfb7533b5ca459194e171df56b79566": {
2175
+ "model_module": "@jupyter-widgets/controls",
2176
+ "model_module_version": "1.5.0",
2177
+ "model_name": "HBoxModel",
2178
+ "state": {
2179
+ "_dom_classes": [],
2180
+ "_model_module": "@jupyter-widgets/controls",
2181
+ "_model_module_version": "1.5.0",
2182
+ "_model_name": "HBoxModel",
2183
+ "_view_count": null,
2184
+ "_view_module": "@jupyter-widgets/controls",
2185
+ "_view_module_version": "1.5.0",
2186
+ "_view_name": "HBoxView",
2187
+ "box_style": "",
2188
+ "children": [
2189
+ "IPY_MODEL_c894e8237aa34c56bb250acab1466005",
2190
+ "IPY_MODEL_a5a126b229064812bf3dcb228118be50",
2191
+ "IPY_MODEL_661e1b29c59a4295b594edfa4f50ff87"
2192
+ ],
2193
+ "layout": "IPY_MODEL_1bcba805972b484d8b6aa6542c81841c"
2194
+ }
2195
+ },
2196
+ "c25b49b7adaa48a0a3a306aa1e0661b4": {
2197
+ "model_module": "@jupyter-widgets/controls",
2198
+ "model_module_version": "1.5.0",
2199
+ "model_name": "DescriptionStyleModel",
2200
+ "state": {
2201
+ "_model_module": "@jupyter-widgets/controls",
2202
+ "_model_module_version": "1.5.0",
2203
+ "_model_name": "DescriptionStyleModel",
2204
+ "_view_count": null,
2205
+ "_view_module": "@jupyter-widgets/base",
2206
+ "_view_module_version": "1.2.0",
2207
+ "_view_name": "StyleView",
2208
+ "description_width": ""
2209
+ }
2210
+ },
2211
+ "c894e8237aa34c56bb250acab1466005": {
2212
+ "model_module": "@jupyter-widgets/controls",
2213
+ "model_module_version": "1.5.0",
2214
+ "model_name": "HTMLModel",
2215
+ "state": {
2216
+ "_dom_classes": [],
2217
+ "_model_module": "@jupyter-widgets/controls",
2218
+ "_model_module_version": "1.5.0",
2219
+ "_model_name": "HTMLModel",
2220
+ "_view_count": null,
2221
+ "_view_module": "@jupyter-widgets/controls",
2222
+ "_view_module_version": "1.5.0",
2223
+ "_view_name": "HTMLView",
2224
+ "description": "",
2225
+ "description_tooltip": null,
2226
+ "layout": "IPY_MODEL_e71f5c7f1d5d4f83b58c68d2fa310d9c",
2227
+ "placeholder": "​",
2228
+ "style": "IPY_MODEL_6a567e0a1a5447519c5df10e777520cf",
2229
+ "value": "100%"
2230
+ }
2231
+ },
2232
+ "ca509bd409624c998e555c9a779b8aae": {
2233
+ "model_module": "@jupyter-widgets/base",
2234
+ "model_module_version": "1.2.0",
2235
+ "model_name": "LayoutModel",
2236
+ "state": {
2237
+ "_model_module": "@jupyter-widgets/base",
2238
+ "_model_module_version": "1.2.0",
2239
+ "_model_name": "LayoutModel",
2240
+ "_view_count": null,
2241
+ "_view_module": "@jupyter-widgets/base",
2242
+ "_view_module_version": "1.2.0",
2243
+ "_view_name": "LayoutView",
2244
+ "align_content": null,
2245
+ "align_items": null,
2246
+ "align_self": null,
2247
+ "border": null,
2248
+ "bottom": null,
2249
+ "display": null,
2250
+ "flex": null,
2251
+ "flex_flow": null,
2252
+ "grid_area": null,
2253
+ "grid_auto_columns": null,
2254
+ "grid_auto_flow": null,
2255
+ "grid_auto_rows": null,
2256
+ "grid_column": null,
2257
+ "grid_gap": null,
2258
+ "grid_row": null,
2259
+ "grid_template_areas": null,
2260
+ "grid_template_columns": null,
2261
+ "grid_template_rows": null,
2262
+ "height": null,
2263
+ "justify_content": null,
2264
+ "justify_items": null,
2265
+ "left": null,
2266
+ "margin": null,
2267
+ "max_height": null,
2268
+ "max_width": null,
2269
+ "min_height": null,
2270
+ "min_width": null,
2271
+ "object_fit": null,
2272
+ "object_position": null,
2273
+ "order": null,
2274
+ "overflow": null,
2275
+ "overflow_x": null,
2276
+ "overflow_y": null,
2277
+ "padding": null,
2278
+ "right": null,
2279
+ "top": null,
2280
+ "visibility": null,
2281
+ "width": null
2282
+ }
2283
+ },
2284
+ "d93bfb366db14c2fa77b038752f69b38": {
2285
+ "model_module": "@jupyter-widgets/base",
2286
+ "model_module_version": "1.2.0",
2287
+ "model_name": "LayoutModel",
2288
+ "state": {
2289
+ "_model_module": "@jupyter-widgets/base",
2290
+ "_model_module_version": "1.2.0",
2291
+ "_model_name": "LayoutModel",
2292
+ "_view_count": null,
2293
+ "_view_module": "@jupyter-widgets/base",
2294
+ "_view_module_version": "1.2.0",
2295
+ "_view_name": "LayoutView",
2296
+ "align_content": null,
2297
+ "align_items": null,
2298
+ "align_self": null,
2299
+ "border": null,
2300
+ "bottom": null,
2301
+ "display": null,
2302
+ "flex": null,
2303
+ "flex_flow": null,
2304
+ "grid_area": null,
2305
+ "grid_auto_columns": null,
2306
+ "grid_auto_flow": null,
2307
+ "grid_auto_rows": null,
2308
+ "grid_column": null,
2309
+ "grid_gap": null,
2310
+ "grid_row": null,
2311
+ "grid_template_areas": null,
2312
+ "grid_template_columns": null,
2313
+ "grid_template_rows": null,
2314
+ "height": null,
2315
+ "justify_content": null,
2316
+ "justify_items": null,
2317
+ "left": null,
2318
+ "margin": null,
2319
+ "max_height": null,
2320
+ "max_width": null,
2321
+ "min_height": null,
2322
+ "min_width": null,
2323
+ "object_fit": null,
2324
+ "object_position": null,
2325
+ "order": null,
2326
+ "overflow": null,
2327
+ "overflow_x": null,
2328
+ "overflow_y": null,
2329
+ "padding": null,
2330
+ "right": null,
2331
+ "top": null,
2332
+ "visibility": null,
2333
+ "width": null
2334
+ }
2335
+ },
2336
+ "d9d37b8b79f24dbf837327a250a5a346": {
2337
+ "model_module": "@jupyter-widgets/controls",
2338
+ "model_module_version": "1.5.0",
2339
+ "model_name": "HTMLModel",
2340
+ "state": {
2341
+ "_dom_classes": [],
2342
+ "_model_module": "@jupyter-widgets/controls",
2343
+ "_model_module_version": "1.5.0",
2344
+ "_model_name": "HTMLModel",
2345
+ "_view_count": null,
2346
+ "_view_module": "@jupyter-widgets/controls",
2347
+ "_view_module_version": "1.5.0",
2348
+ "_view_name": "HTMLView",
2349
+ "description": "",
2350
+ "description_tooltip": null,
2351
+ "layout": "IPY_MODEL_86bf02b06ed740a88015c2b944205c1e",
2352
+ "placeholder": "​",
2353
+ "style": "IPY_MODEL_aef6a6be67f749908060d8038b6d3804",
2354
+ "value": "Running tokenizer on dataset: 0%"
2355
+ }
2356
+ },
2357
+ "da14180f51ab44b48470cb9ea74d3864": {
2358
+ "model_module": "@jupyter-widgets/controls",
2359
+ "model_module_version": "1.5.0",
2360
+ "model_name": "DescriptionStyleModel",
2361
+ "state": {
2362
+ "_model_module": "@jupyter-widgets/controls",
2363
+ "_model_module_version": "1.5.0",
2364
+ "_model_name": "DescriptionStyleModel",
2365
+ "_view_count": null,
2366
+ "_view_module": "@jupyter-widgets/base",
2367
+ "_view_module_version": "1.2.0",
2368
+ "_view_name": "StyleView",
2369
+ "description_width": ""
2370
+ }
2371
+ },
2372
+ "dc8ab16a1e6c4e6893c95ccd16568f9a": {
2373
+ "model_module": "@jupyter-widgets/base",
2374
+ "model_module_version": "1.2.0",
2375
+ "model_name": "LayoutModel",
2376
+ "state": {
2377
+ "_model_module": "@jupyter-widgets/base",
2378
+ "_model_module_version": "1.2.0",
2379
+ "_model_name": "LayoutModel",
2380
+ "_view_count": null,
2381
+ "_view_module": "@jupyter-widgets/base",
2382
+ "_view_module_version": "1.2.0",
2383
+ "_view_name": "LayoutView",
2384
+ "align_content": null,
2385
+ "align_items": null,
2386
+ "align_self": null,
2387
+ "border": null,
2388
+ "bottom": null,
2389
+ "display": null,
2390
+ "flex": null,
2391
+ "flex_flow": null,
2392
+ "grid_area": null,
2393
+ "grid_auto_columns": null,
2394
+ "grid_auto_flow": null,
2395
+ "grid_auto_rows": null,
2396
+ "grid_column": null,
2397
+ "grid_gap": null,
2398
+ "grid_row": null,
2399
+ "grid_template_areas": null,
2400
+ "grid_template_columns": null,
2401
+ "grid_template_rows": null,
2402
+ "height": null,
2403
+ "justify_content": null,
2404
+ "justify_items": null,
2405
+ "left": null,
2406
+ "margin": null,
2407
+ "max_height": null,
2408
+ "max_width": null,
2409
+ "min_height": null,
2410
+ "min_width": null,
2411
+ "object_fit": null,
2412
+ "object_position": null,
2413
+ "order": null,
2414
+ "overflow": null,
2415
+ "overflow_x": null,
2416
+ "overflow_y": null,
2417
+ "padding": null,
2418
+ "right": null,
2419
+ "top": null,
2420
+ "visibility": null,
2421
+ "width": null
2422
+ }
2423
+ },
2424
+ "dd4b895874ce46ceb1ad0d9bc973f98f": {
2425
+ "model_module": "@jupyter-widgets/controls",
2426
+ "model_module_version": "1.5.0",
2427
+ "model_name": "ProgressStyleModel",
2428
+ "state": {
2429
+ "_model_module": "@jupyter-widgets/controls",
2430
+ "_model_module_version": "1.5.0",
2431
+ "_model_name": "ProgressStyleModel",
2432
+ "_view_count": null,
2433
+ "_view_module": "@jupyter-widgets/base",
2434
+ "_view_module_version": "1.2.0",
2435
+ "_view_name": "StyleView",
2436
+ "bar_color": null,
2437
+ "description_width": ""
2438
+ }
2439
+ },
2440
+ "e1e80a68a9e7429397cafc96c3c11f80": {
2441
+ "model_module": "@jupyter-widgets/controls",
2442
+ "model_module_version": "1.5.0",
2443
+ "model_name": "HBoxModel",
2444
+ "state": {
2445
+ "_dom_classes": [],
2446
+ "_model_module": "@jupyter-widgets/controls",
2447
+ "_model_module_version": "1.5.0",
2448
+ "_model_name": "HBoxModel",
2449
+ "_view_count": null,
2450
+ "_view_module": "@jupyter-widgets/controls",
2451
+ "_view_module_version": "1.5.0",
2452
+ "_view_name": "HBoxView",
2453
+ "box_style": "",
2454
+ "children": [
2455
+ "IPY_MODEL_5307864c2b1143f4b44f3f172611113e",
2456
+ "IPY_MODEL_2e2b6c3f48974ea4aca9b7710a03379e",
2457
+ "IPY_MODEL_aae78f9bd53348bda45967a38736cb78"
2458
+ ],
2459
+ "layout": "IPY_MODEL_34db17e0f28d40d6abafb8acd5dda379"
2460
+ }
2461
+ },
2462
+ "e71f5c7f1d5d4f83b58c68d2fa310d9c": {
2463
+ "model_module": "@jupyter-widgets/base",
2464
+ "model_module_version": "1.2.0",
2465
+ "model_name": "LayoutModel",
2466
+ "state": {
2467
+ "_model_module": "@jupyter-widgets/base",
2468
+ "_model_module_version": "1.2.0",
2469
+ "_model_name": "LayoutModel",
2470
+ "_view_count": null,
2471
+ "_view_module": "@jupyter-widgets/base",
2472
+ "_view_module_version": "1.2.0",
2473
+ "_view_name": "LayoutView",
2474
+ "align_content": null,
2475
+ "align_items": null,
2476
+ "align_self": null,
2477
+ "border": null,
2478
+ "bottom": null,
2479
+ "display": null,
2480
+ "flex": null,
2481
+ "flex_flow": null,
2482
+ "grid_area": null,
2483
+ "grid_auto_columns": null,
2484
+ "grid_auto_flow": null,
2485
+ "grid_auto_rows": null,
2486
+ "grid_column": null,
2487
+ "grid_gap": null,
2488
+ "grid_row": null,
2489
+ "grid_template_areas": null,
2490
+ "grid_template_columns": null,
2491
+ "grid_template_rows": null,
2492
+ "height": null,
2493
+ "justify_content": null,
2494
+ "justify_items": null,
2495
+ "left": null,
2496
+ "margin": null,
2497
+ "max_height": null,
2498
+ "max_width": null,
2499
+ "min_height": null,
2500
+ "min_width": null,
2501
+ "object_fit": null,
2502
+ "object_position": null,
2503
+ "order": null,
2504
+ "overflow": null,
2505
+ "overflow_x": null,
2506
+ "overflow_y": null,
2507
+ "padding": null,
2508
+ "right": null,
2509
+ "top": null,
2510
+ "visibility": null,
2511
+ "width": null
2512
+ }
2513
+ },
2514
+ "f60e9915d2a74ca7bc010d7684f5acf6": {
2515
+ "model_module": "@jupyter-widgets/controls",
2516
+ "model_module_version": "1.5.0",
2517
+ "model_name": "DescriptionStyleModel",
2518
+ "state": {
2519
+ "_model_module": "@jupyter-widgets/controls",
2520
+ "_model_module_version": "1.5.0",
2521
+ "_model_name": "DescriptionStyleModel",
2522
+ "_view_count": null,
2523
+ "_view_module": "@jupyter-widgets/base",
2524
+ "_view_module_version": "1.2.0",
2525
+ "_view_name": "StyleView",
2526
+ "description_width": ""
2527
+ }
2528
+ },
2529
+ "f760cd4758334ca9a43fd15612fd808b": {
2530
+ "model_module": "@jupyter-widgets/base",
2531
+ "model_module_version": "1.2.0",
2532
+ "model_name": "LayoutModel",
2533
+ "state": {
2534
+ "_model_module": "@jupyter-widgets/base",
2535
+ "_model_module_version": "1.2.0",
2536
+ "_model_name": "LayoutModel",
2537
+ "_view_count": null,
2538
+ "_view_module": "@jupyter-widgets/base",
2539
+ "_view_module_version": "1.2.0",
2540
+ "_view_name": "LayoutView",
2541
+ "align_content": null,
2542
+ "align_items": null,
2543
+ "align_self": null,
2544
+ "border": null,
2545
+ "bottom": null,
2546
+ "display": null,
2547
+ "flex": null,
2548
+ "flex_flow": null,
2549
+ "grid_area": null,
2550
+ "grid_auto_columns": null,
2551
+ "grid_auto_flow": null,
2552
+ "grid_auto_rows": null,
2553
+ "grid_column": null,
2554
+ "grid_gap": null,
2555
+ "grid_row": null,
2556
+ "grid_template_areas": null,
2557
+ "grid_template_columns": null,
2558
+ "grid_template_rows": null,
2559
+ "height": null,
2560
+ "justify_content": null,
2561
+ "justify_items": null,
2562
+ "left": null,
2563
+ "margin": null,
2564
+ "max_height": null,
2565
+ "max_width": null,
2566
+ "min_height": null,
2567
+ "min_width": null,
2568
+ "object_fit": null,
2569
+ "object_position": null,
2570
+ "order": null,
2571
+ "overflow": null,
2572
+ "overflow_x": null,
2573
+ "overflow_y": null,
2574
+ "padding": null,
2575
+ "right": null,
2576
+ "top": null,
2577
+ "visibility": null,
2578
+ "width": null
2579
+ }
2580
+ },
2581
+ "f9d86ad7fa734f3a857505a542256a3c": {
2582
+ "model_module": "@jupyter-widgets/base",
2583
+ "model_module_version": "1.2.0",
2584
+ "model_name": "LayoutModel",
2585
+ "state": {
2586
+ "_model_module": "@jupyter-widgets/base",
2587
+ "_model_module_version": "1.2.0",
2588
+ "_model_name": "LayoutModel",
2589
+ "_view_count": null,
2590
+ "_view_module": "@jupyter-widgets/base",
2591
+ "_view_module_version": "1.2.0",
2592
+ "_view_name": "LayoutView",
2593
+ "align_content": null,
2594
+ "align_items": null,
2595
+ "align_self": null,
2596
+ "border": null,
2597
+ "bottom": null,
2598
+ "display": null,
2599
+ "flex": null,
2600
+ "flex_flow": null,
2601
+ "grid_area": null,
2602
+ "grid_auto_columns": null,
2603
+ "grid_auto_flow": null,
2604
+ "grid_auto_rows": null,
2605
+ "grid_column": null,
2606
+ "grid_gap": null,
2607
+ "grid_row": null,
2608
+ "grid_template_areas": null,
2609
+ "grid_template_columns": null,
2610
+ "grid_template_rows": null,
2611
+ "height": null,
2612
+ "justify_content": null,
2613
+ "justify_items": null,
2614
+ "left": null,
2615
+ "margin": null,
2616
+ "max_height": null,
2617
+ "max_width": null,
2618
+ "min_height": null,
2619
+ "min_width": null,
2620
+ "object_fit": null,
2621
+ "object_position": null,
2622
+ "order": null,
2623
+ "overflow": null,
2624
+ "overflow_x": null,
2625
+ "overflow_y": null,
2626
+ "padding": null,
2627
+ "right": null,
2628
+ "top": null,
2629
+ "visibility": "hidden",
2630
+ "width": null
2631
+ }
2632
+ },
2633
+ "faa18df899c14e9cac6721253e6c9128": {
2634
+ "model_module": "@jupyter-widgets/controls",
2635
+ "model_module_version": "1.5.0",
2636
+ "model_name": "HTMLModel",
2637
+ "state": {
2638
+ "_dom_classes": [],
2639
+ "_model_module": "@jupyter-widgets/controls",
2640
+ "_model_module_version": "1.5.0",
2641
+ "_model_name": "HTMLModel",
2642
+ "_view_count": null,
2643
+ "_view_module": "@jupyter-widgets/controls",
2644
+ "_view_module_version": "1.5.0",
2645
+ "_view_name": "HTMLView",
2646
+ "description": "",
2647
+ "description_tooltip": null,
2648
+ "layout": "IPY_MODEL_1b8aada826a0451bb60c418b19178c8c",
2649
+ "placeholder": "​",
2650
+ "style": "IPY_MODEL_a91916e02e9c424e881e45b3aa978574",
2651
+ "value": "Map: 0%"
2652
+ }
2653
+ },
2654
+ "fc4637a1b37e4e90874c71aa4271ac74": {
2655
+ "model_module": "@jupyter-widgets/base",
2656
+ "model_module_version": "1.2.0",
2657
+ "model_name": "LayoutModel",
2658
+ "state": {
2659
+ "_model_module": "@jupyter-widgets/base",
2660
+ "_model_module_version": "1.2.0",
2661
+ "_model_name": "LayoutModel",
2662
+ "_view_count": null,
2663
+ "_view_module": "@jupyter-widgets/base",
2664
+ "_view_module_version": "1.2.0",
2665
+ "_view_name": "LayoutView",
2666
+ "align_content": null,
2667
+ "align_items": null,
2668
+ "align_self": null,
2669
+ "border": null,
2670
+ "bottom": null,
2671
+ "display": null,
2672
+ "flex": null,
2673
+ "flex_flow": null,
2674
+ "grid_area": null,
2675
+ "grid_auto_columns": null,
2676
+ "grid_auto_flow": null,
2677
+ "grid_auto_rows": null,
2678
+ "grid_column": null,
2679
+ "grid_gap": null,
2680
+ "grid_row": null,
2681
+ "grid_template_areas": null,
2682
+ "grid_template_columns": null,
2683
+ "grid_template_rows": null,
2684
+ "height": null,
2685
+ "justify_content": null,
2686
+ "justify_items": null,
2687
+ "left": null,
2688
+ "margin": null,
2689
+ "max_height": null,
2690
+ "max_width": null,
2691
+ "min_height": null,
2692
+ "min_width": null,
2693
+ "object_fit": null,
2694
+ "object_position": null,
2695
+ "order": null,
2696
+ "overflow": null,
2697
+ "overflow_x": null,
2698
+ "overflow_y": null,
2699
+ "padding": null,
2700
+ "right": null,
2701
+ "top": null,
2702
+ "visibility": "hidden",
2703
+ "width": null
2704
+ }
2705
+ }
2706
+ }
2707
+ }
2708
+ },
2709
+ "nbformat": 4,
2710
+ "nbformat_minor": 5
2711
+ }
peft_lora_seq2seq.ipynb ADDED
@@ -0,0 +1,486 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "5f93b7d1",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stdout",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "\n",
14
+ "===================================BUG REPORT===================================\n",
15
+ "Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n",
16
+ "For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link\n",
17
+ "================================================================================\n",
18
+ "CUDA SETUP: CUDA runtime path found: /home/sourab/miniconda3/envs/ml/lib/libcudart.so\n",
19
+ "CUDA SETUP: Highest compute capability among GPUs detected: 7.5\n",
20
+ "CUDA SETUP: Detected CUDA version 117\n",
21
+ "CUDA SETUP: Loading binary /home/sourab/miniconda3/envs/ml/lib/python3.10/site-packages/bitsandbytes/libbitsandbytes_cuda117.so...\n"
22
+ ]
23
+ }
24
+ ],
25
+ "source": [
26
+ "from transformers import AutoModelForSeq2SeqLM\n",
27
+ "from peft import get_peft_config, get_peft_model, get_peft_model_state_dict, LoraConfig, TaskType\n",
28
+ "import torch\n",
29
+ "from datasets import load_dataset\n",
30
+ "import os\n",
31
+ "\n",
32
+ "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
33
+ "from transformers import AutoTokenizer\n",
34
+ "from torch.utils.data import DataLoader\n",
35
+ "from transformers import default_data_collator, get_linear_schedule_with_warmup\n",
36
+ "from tqdm import tqdm\n",
37
+ "from datasets import load_dataset\n",
38
+ "\n",
39
+ "device = \"cuda\"\n",
40
+ "model_name_or_path = \"bigscience/mt0-large\"\n",
41
+ "tokenizer_name_or_path = \"bigscience/mt0-large\"\n",
42
+ "\n",
43
+ "checkpoint_name = \"financial_sentiment_analysis_lora_v1.pt\"\n",
44
+ "text_column = \"sentence\"\n",
45
+ "label_column = \"text_label\"\n",
46
+ "max_length = 128\n",
47
+ "lr = 1e-3\n",
48
+ "num_epochs = 3\n",
49
+ "batch_size = 8"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": null,
55
+ "id": "8d0850ac",
56
+ "metadata": {},
57
+ "outputs": [],
58
+ "source": [
59
+ "# creating model\n",
60
+ "peft_config = LoraConfig(task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1)\n",
61
+ "\n",
62
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)\n",
63
+ "model = get_peft_model(model, peft_config)\n",
64
+ "model.print_trainable_parameters()\n",
65
+ "model"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 3,
71
+ "id": "4ee2babf",
72
+ "metadata": {},
73
+ "outputs": [
74
+ {
75
+ "name": "stderr",
76
+ "output_type": "stream",
77
+ "text": [
78
+ "Found cached dataset financial_phrasebank (/home/sourab/.cache/huggingface/datasets/financial_phrasebank/sentences_allagree/1.0.0/550bde12e6c30e2674da973a55f57edde5181d53f5a5a34c1531c53f93b7e141)\n"
79
+ ]
80
+ },
81
+ {
82
+ "data": {
83
+ "application/vnd.jupyter.widget-view+json": {
84
+ "model_id": "3403bf3d718042018b0531848cc30209",
85
+ "version_major": 2,
86
+ "version_minor": 0
87
+ },
88
+ "text/plain": [
89
+ " 0%| | 0/1 [00:00<?, ?it/s]"
90
+ ]
91
+ },
92
+ "metadata": {},
93
+ "output_type": "display_data"
94
+ },
95
+ {
96
+ "data": {
97
+ "application/vnd.jupyter.widget-view+json": {
98
+ "model_id": "d3d5c45e3776469f9560b6eaa9346f8f",
99
+ "version_major": 2,
100
+ "version_minor": 0
101
+ },
102
+ "text/plain": [
103
+ " 0%| | 0/3 [00:00<?, ?ba/s]"
104
+ ]
105
+ },
106
+ "metadata": {},
107
+ "output_type": "display_data"
108
+ },
109
+ {
110
+ "data": {
111
+ "application/vnd.jupyter.widget-view+json": {
112
+ "model_id": "e9736f26e9aa450b8d65f95c0b9c81cc",
113
+ "version_major": 2,
114
+ "version_minor": 0
115
+ },
116
+ "text/plain": [
117
+ " 0%| | 0/1 [00:00<?, ?ba/s]"
118
+ ]
119
+ },
120
+ "metadata": {},
121
+ "output_type": "display_data"
122
+ },
123
+ {
124
+ "data": {
125
+ "text/plain": [
126
+ "{'sentence': \"The 10,000-odd square metre plot that Stockmann has bought for the Nevsky Center shopping center is located on Nevsky Prospect , St Petersburg 's high street , next to the Vosstaniya Square underground station , in the immediate vicinity of Moscow Station .\",\n",
127
+ " 'label': 1,\n",
128
+ " 'text_label': 'neutral'}"
129
+ ]
130
+ },
131
+ "execution_count": 3,
132
+ "metadata": {},
133
+ "output_type": "execute_result"
134
+ }
135
+ ],
136
+ "source": [
137
+ "# loading dataset\n",
138
+ "dataset = load_dataset(\"financial_phrasebank\", \"sentences_allagree\")\n",
139
+ "dataset = dataset[\"train\"].train_test_split(test_size=0.1)\n",
140
+ "dataset[\"validation\"] = dataset[\"test\"]\n",
141
+ "del dataset[\"test\"]\n",
142
+ "\n",
143
+ "classes = dataset[\"train\"].features[\"label\"].names\n",
144
+ "dataset = dataset.map(\n",
145
+ " lambda x: {\"text_label\": [classes[label] for label in x[\"label\"]]},\n",
146
+ " batched=True,\n",
147
+ " num_proc=1,\n",
148
+ ")\n",
149
+ "\n",
150
+ "dataset[\"train\"][0]"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 4,
156
+ "id": "adf9608c",
157
+ "metadata": {},
158
+ "outputs": [
159
+ {
160
+ "data": {
161
+ "application/vnd.jupyter.widget-view+json": {
162
+ "model_id": "c460989d4ab24e3f97d81ef040b1d1b4",
163
+ "version_major": 2,
164
+ "version_minor": 0
165
+ },
166
+ "text/plain": [
167
+ "Running tokenizer on dataset: 0%| | 0/3 [00:00<?, ?ba/s]"
168
+ ]
169
+ },
170
+ "metadata": {},
171
+ "output_type": "display_data"
172
+ },
173
+ {
174
+ "data": {
175
+ "application/vnd.jupyter.widget-view+json": {
176
+ "model_id": "1acc389b08b94f8a87900b9fbdbccce4",
177
+ "version_major": 2,
178
+ "version_minor": 0
179
+ },
180
+ "text/plain": [
181
+ "Running tokenizer on dataset: 0%| | 0/1 [00:00<?, ?ba/s]"
182
+ ]
183
+ },
184
+ "metadata": {},
185
+ "output_type": "display_data"
186
+ }
187
+ ],
188
+ "source": [
189
+ "# data preprocessing\n",
190
+ "tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)\n",
191
+ "\n",
192
+ "\n",
193
+ "def preprocess_function(examples):\n",
194
+ " inputs = examples[text_column]\n",
195
+ " targets = examples[label_column]\n",
196
+ " model_inputs = tokenizer(inputs, max_length=max_length, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
197
+ " labels = tokenizer(targets, max_length=3, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
198
+ " labels = labels[\"input_ids\"]\n",
199
+ " labels[labels == tokenizer.pad_token_id] = -100\n",
200
+ " model_inputs[\"labels\"] = labels\n",
201
+ " return model_inputs\n",
202
+ "\n",
203
+ "\n",
204
+ "processed_datasets = dataset.map(\n",
205
+ " preprocess_function,\n",
206
+ " batched=True,\n",
207
+ " num_proc=1,\n",
208
+ " remove_columns=dataset[\"train\"].column_names,\n",
209
+ " load_from_cache_file=False,\n",
210
+ " desc=\"Running tokenizer on dataset\",\n",
211
+ ")\n",
212
+ "\n",
213
+ "train_dataset = processed_datasets[\"train\"]\n",
214
+ "eval_dataset = processed_datasets[\"validation\"]\n",
215
+ "\n",
216
+ "train_dataloader = DataLoader(\n",
217
+ " train_dataset, shuffle=True, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True\n",
218
+ ")\n",
219
+ "eval_dataloader = DataLoader(eval_dataset, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True)"
220
+ ]
221
+ },
222
+ {
223
+ "cell_type": "code",
224
+ "execution_count": 5,
225
+ "id": "f733a3c6",
226
+ "metadata": {},
227
+ "outputs": [],
228
+ "source": [
229
+ "# optimizer and lr scheduler\n",
230
+ "optimizer = torch.optim.AdamW(model.parameters(), lr=lr)\n",
231
+ "lr_scheduler = get_linear_schedule_with_warmup(\n",
232
+ " optimizer=optimizer,\n",
233
+ " num_warmup_steps=0,\n",
234
+ " num_training_steps=(len(train_dataloader) * num_epochs),\n",
235
+ ")"
236
+ ]
237
+ },
238
+ {
239
+ "cell_type": "code",
240
+ "execution_count": 6,
241
+ "id": "6b3a4090",
242
+ "metadata": {},
243
+ "outputs": [
244
+ {
245
+ "name": "stderr",
246
+ "output_type": "stream",
247
+ "text": [
248
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [02:21<00:00, 1.81it/s]\n",
249
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:07<00:00, 4.13it/s]\n"
250
+ ]
251
+ },
252
+ {
253
+ "name": "stdout",
254
+ "output_type": "stream",
255
+ "text": [
256
+ "epoch=0: train_ppl=tensor(14.6341, device='cuda:0') train_epoch_loss=tensor(2.6834, device='cuda:0') eval_ppl=tensor(1.0057, device='cuda:0') eval_epoch_loss=tensor(0.0057, device='cuda:0')\n"
257
+ ]
258
+ },
259
+ {
260
+ "name": "stderr",
261
+ "output_type": "stream",
262
+ "text": [
263
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [02:00<00:00, 2.11it/s]\n",
264
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:05<00:00, 5.66it/s]\n"
265
+ ]
266
+ },
267
+ {
268
+ "name": "stdout",
269
+ "output_type": "stream",
270
+ "text": [
271
+ "epoch=1: train_ppl=tensor(1.7576, device='cuda:0') train_epoch_loss=tensor(0.5640, device='cuda:0') eval_ppl=tensor(1.0052, device='cuda:0') eval_epoch_loss=tensor(0.0052, device='cuda:0')\n"
272
+ ]
273
+ },
274
+ {
275
+ "name": "stderr",
276
+ "output_type": "stream",
277
+ "text": [
278
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [01:33<00:00, 2.74it/s]\n",
279
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:04<00:00, 6.23it/s]"
280
+ ]
281
+ },
282
+ {
283
+ "name": "stdout",
284
+ "output_type": "stream",
285
+ "text": [
286
+ "epoch=2: train_ppl=tensor(1.3830, device='cuda:0') train_epoch_loss=tensor(0.3243, device='cuda:0') eval_ppl=tensor(1.0035, device='cuda:0') eval_epoch_loss=tensor(0.0035, device='cuda:0')\n"
287
+ ]
288
+ },
289
+ {
290
+ "name": "stderr",
291
+ "output_type": "stream",
292
+ "text": [
293
+ "\n"
294
+ ]
295
+ }
296
+ ],
297
+ "source": [
298
+ "# training and evaluation\n",
299
+ "model = model.to(device)\n",
300
+ "\n",
301
+ "for epoch in range(num_epochs):\n",
302
+ " model.train()\n",
303
+ " total_loss = 0\n",
304
+ " for step, batch in enumerate(tqdm(train_dataloader)):\n",
305
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
306
+ " outputs = model(**batch)\n",
307
+ " loss = outputs.loss\n",
308
+ " total_loss += loss.detach().float()\n",
309
+ " loss.backward()\n",
310
+ " optimizer.step()\n",
311
+ " lr_scheduler.step()\n",
312
+ " optimizer.zero_grad()\n",
313
+ "\n",
314
+ " model.eval()\n",
315
+ " eval_loss = 0\n",
316
+ " eval_preds = []\n",
317
+ " for step, batch in enumerate(tqdm(eval_dataloader)):\n",
318
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
319
+ " with torch.no_grad():\n",
320
+ " outputs = model(**batch)\n",
321
+ " loss = outputs.loss\n",
322
+ " eval_loss += loss.detach().float()\n",
323
+ " eval_preds.extend(\n",
324
+ " tokenizer.batch_decode(torch.argmax(outputs.logits, -1).detach().cpu().numpy(), skip_special_tokens=True)\n",
325
+ " )\n",
326
+ "\n",
327
+ " eval_epoch_loss = eval_loss / len(eval_dataloader)\n",
328
+ " eval_ppl = torch.exp(eval_epoch_loss)\n",
329
+ " train_epoch_loss = total_loss / len(train_dataloader)\n",
330
+ " train_ppl = torch.exp(train_epoch_loss)\n",
331
+ " print(f\"{epoch=}: {train_ppl=} {train_epoch_loss=} {eval_ppl=} {eval_epoch_loss=}\")"
332
+ ]
333
+ },
334
+ {
335
+ "cell_type": "code",
336
+ "execution_count": 7,
337
+ "id": "6cafa67b",
338
+ "metadata": {},
339
+ "outputs": [
340
+ {
341
+ "name": "stdout",
342
+ "output_type": "stream",
343
+ "text": [
344
+ "accuracy=97.3568281938326 % on the evaluation dataset\n",
345
+ "eval_preds[:10]=['neutral', 'neutral', 'neutral', 'positive', 'neutral', 'positive', 'positive', 'neutral', 'neutral', 'neutral']\n",
346
+ "dataset['validation']['text_label'][:10]=['neutral', 'neutral', 'neutral', 'positive', 'neutral', 'positive', 'positive', 'neutral', 'neutral', 'neutral']\n"
347
+ ]
348
+ }
349
+ ],
350
+ "source": [
351
+ "# print accuracy\n",
352
+ "correct = 0\n",
353
+ "total = 0\n",
354
+ "for pred, true in zip(eval_preds, dataset[\"validation\"][\"text_label\"]):\n",
355
+ " if pred.strip() == true.strip():\n",
356
+ " correct += 1\n",
357
+ " total += 1\n",
358
+ "accuracy = correct / total * 100\n",
359
+ "print(f\"{accuracy=} % on the evaluation dataset\")\n",
360
+ "print(f\"{eval_preds[:10]=}\")\n",
361
+ "print(f\"{dataset['validation']['text_label'][:10]=}\")"
362
+ ]
363
+ },
364
+ {
365
+ "cell_type": "code",
366
+ "execution_count": 8,
367
+ "id": "a8de6005",
368
+ "metadata": {},
369
+ "outputs": [],
370
+ "source": [
371
+ "# saving model\n",
372
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
373
+ "model.save_pretrained(peft_model_id)"
374
+ ]
375
+ },
376
+ {
377
+ "cell_type": "code",
378
+ "execution_count": 9,
379
+ "id": "bd20cd4c",
380
+ "metadata": {},
381
+ "outputs": [
382
+ {
383
+ "name": "stdout",
384
+ "output_type": "stream",
385
+ "text": [
386
+ "9,2M\tbigscience/mt0-large_LORA_SEQ_2_SEQ_LM/adapter_model.bin\r\n"
387
+ ]
388
+ }
389
+ ],
390
+ "source": [
391
+ "ckpt = f\"{peft_model_id}/adapter_model.bin\"\n",
392
+ "!du -h $ckpt"
393
+ ]
394
+ },
395
+ {
396
+ "cell_type": "code",
397
+ "execution_count": 11,
398
+ "id": "76c2fc29",
399
+ "metadata": {},
400
+ "outputs": [],
401
+ "source": [
402
+ "from peft import PeftModel, PeftConfig\n",
403
+ "\n",
404
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
405
+ "\n",
406
+ "config = PeftConfig.from_pretrained(peft_model_id)\n",
407
+ "model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)\n",
408
+ "model = PeftModel.from_pretrained(model, peft_model_id)"
409
+ ]
410
+ },
411
+ {
412
+ "cell_type": "code",
413
+ "execution_count": 15,
414
+ "id": "37d712ce",
415
+ "metadata": {},
416
+ "outputs": [
417
+ {
418
+ "name": "stdout",
419
+ "output_type": "stream",
420
+ "text": [
421
+ "- Demand for fireplace products was lower than expected , especially in Germany .\n",
422
+ "{'input_ids': tensor([[ 259, 264, 259, 82903, 332, 1090, 10040, 10371, 639, 259,\n",
423
+ " 19540, 2421, 259, 25505, 259, 261, 259, 21230, 281, 17052,\n",
424
+ " 259, 260, 1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}\n",
425
+ "tensor([[ 0, 259, 32588, 1]])\n",
426
+ "['negative']\n"
427
+ ]
428
+ }
429
+ ],
430
+ "source": [
431
+ "model.eval()\n",
432
+ "i = 13\n",
433
+ "inputs = tokenizer(dataset[\"validation\"][text_column][i], return_tensors=\"pt\")\n",
434
+ "print(dataset[\"validation\"][text_column][i])\n",
435
+ "print(inputs)\n",
436
+ "\n",
437
+ "with torch.no_grad():\n",
438
+ " outputs = model.generate(input_ids=inputs[\"input_ids\"], max_new_tokens=10)\n",
439
+ " print(outputs)\n",
440
+ " print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True))"
441
+ ]
442
+ },
443
+ {
444
+ "cell_type": "code",
445
+ "execution_count": null,
446
+ "id": "66c65ea4",
447
+ "metadata": {},
448
+ "outputs": [],
449
+ "source": []
450
+ },
451
+ {
452
+ "cell_type": "code",
453
+ "execution_count": null,
454
+ "id": "65e71f78",
455
+ "metadata": {},
456
+ "outputs": [],
457
+ "source": []
458
+ }
459
+ ],
460
+ "metadata": {
461
+ "kernelspec": {
462
+ "display_name": "Python 3 (ipykernel)",
463
+ "language": "python",
464
+ "name": "python3"
465
+ },
466
+ "language_info": {
467
+ "codemirror_mode": {
468
+ "name": "ipython",
469
+ "version": 3
470
+ },
471
+ "file_extension": ".py",
472
+ "mimetype": "text/x-python",
473
+ "name": "python",
474
+ "nbconvert_exporter": "python",
475
+ "pygments_lexer": "ipython3",
476
+ "version": "3.10.5"
477
+ },
478
+ "vscode": {
479
+ "interpreter": {
480
+ "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
481
+ }
482
+ }
483
+ },
484
+ "nbformat": 4,
485
+ "nbformat_minor": 5
486
+ }
peft_lora_seq2seq_accelerate_big_model_inference.ipynb ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "71fbfca2",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "from transformers import AutoModelForSeq2SeqLM\n",
11
+ "from peft import PeftModel, PeftConfig\n",
12
+ "import torch\n",
13
+ "from datasets import load_dataset\n",
14
+ "import os\n",
15
+ "from transformers import AutoTokenizer\n",
16
+ "from torch.utils.data import DataLoader\n",
17
+ "from transformers import default_data_collator, get_linear_schedule_with_warmup\n",
18
+ "from tqdm import tqdm\n",
19
+ "from datasets import load_dataset\n",
20
+ "\n",
21
+ "dataset_name = \"twitter_complaints\"\n",
22
+ "text_column = \"Tweet text\"\n",
23
+ "label_column = \"text_label\"\n",
24
+ "batch_size = 8\n",
25
+ "\n",
26
+ "peft_model_id = \"smangrul/twitter_complaints_bigscience_T0_3B_LORA_SEQ_2_SEQ_LM\"\n",
27
+ "config = PeftConfig.from_pretrained(peft_model_id)"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "code",
32
+ "execution_count": 2,
33
+ "id": "cc55820a",
34
+ "metadata": {},
35
+ "outputs": [],
36
+ "source": [
37
+ "peft_model_id = \"smangrul/twitter_complaints_bigscience_T0_3B_LORA_SEQ_2_SEQ_LM\"\n",
38
+ "max_memory = {0: \"6GIB\", 1: \"0GIB\", 2: \"0GIB\", 3: \"0GIB\", 4: \"0GIB\", \"cpu\": \"30GB\"}\n",
39
+ "config = PeftConfig.from_pretrained(peft_model_id)\n",
40
+ "model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, device_map=\"auto\", max_memory=max_memory)\n",
41
+ "model = PeftModel.from_pretrained(model, peft_model_id, device_map=\"auto\", max_memory=max_memory)"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": null,
47
+ "id": "e1a3648b",
48
+ "metadata": {},
49
+ "outputs": [],
50
+ "source": [
51
+ "from datasets import load_dataset\n",
52
+ "\n",
53
+ "dataset = load_dataset(\"ought/raft\", dataset_name)\n",
54
+ "\n",
55
+ "classes = [k.replace(\"_\", \" \") for k in dataset[\"train\"].features[\"Label\"].names]\n",
56
+ "print(classes)\n",
57
+ "dataset = dataset.map(\n",
58
+ " lambda x: {\"text_label\": [classes[label] for label in x[\"Label\"]]},\n",
59
+ " batched=True,\n",
60
+ " num_proc=1,\n",
61
+ ")\n",
62
+ "print(dataset)\n",
63
+ "dataset[\"train\"][0]"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": null,
69
+ "id": "fe12d4d3",
70
+ "metadata": {},
71
+ "outputs": [],
72
+ "source": [
73
+ "tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)\n",
74
+ "target_max_length = max([len(tokenizer(class_label)[\"input_ids\"]) for class_label in classes])\n",
75
+ "\n",
76
+ "\n",
77
+ "def preprocess_function(examples):\n",
78
+ " inputs = examples[text_column]\n",
79
+ " targets = examples[label_column]\n",
80
+ " model_inputs = tokenizer(inputs, truncation=True)\n",
81
+ " labels = tokenizer(\n",
82
+ " targets, max_length=target_max_length, padding=\"max_length\", truncation=True, return_tensors=\"pt\"\n",
83
+ " )\n",
84
+ " labels = labels[\"input_ids\"]\n",
85
+ " labels[labels == tokenizer.pad_token_id] = -100\n",
86
+ " model_inputs[\"labels\"] = labels\n",
87
+ " return model_inputs\n",
88
+ "\n",
89
+ "\n",
90
+ "processed_datasets = dataset.map(\n",
91
+ " preprocess_function,\n",
92
+ " batched=True,\n",
93
+ " num_proc=1,\n",
94
+ " remove_columns=dataset[\"train\"].column_names,\n",
95
+ " load_from_cache_file=True,\n",
96
+ " desc=\"Running tokenizer on dataset\",\n",
97
+ ")\n",
98
+ "\n",
99
+ "train_dataset = processed_datasets[\"train\"]\n",
100
+ "eval_dataset = processed_datasets[\"train\"]\n",
101
+ "test_dataset = processed_datasets[\"test\"]\n",
102
+ "\n",
103
+ "\n",
104
+ "def collate_fn(examples):\n",
105
+ " return tokenizer.pad(examples, padding=\"longest\", return_tensors=\"pt\")\n",
106
+ "\n",
107
+ "\n",
108
+ "train_dataloader = DataLoader(\n",
109
+ " train_dataset, shuffle=True, collate_fn=collate_fn, batch_size=batch_size, pin_memory=True\n",
110
+ ")\n",
111
+ "eval_dataloader = DataLoader(eval_dataset, collate_fn=collate_fn, batch_size=batch_size, pin_memory=True)\n",
112
+ "test_dataloader = DataLoader(test_dataset, collate_fn=collate_fn, batch_size=batch_size, pin_memory=True)"
113
+ ]
114
+ },
115
+ {
116
+ "cell_type": "code",
117
+ "execution_count": 5,
118
+ "id": "b33be5e6",
119
+ "metadata": {},
120
+ "outputs": [
121
+ {
122
+ "name": "stdout",
123
+ "output_type": "stream",
124
+ "text": [
125
+ "@NYTsupport i have complained a dozen times &amp; yet my papers are still thrown FAR from my door. Why is this so hard to resolve?\n",
126
+ "{'input_ids': tensor([[25335, 1499, 3, 10, 3320, 12056, 382, 20390, 3, 23,\n",
127
+ " 43, 25932, 3, 9, 9611, 648, 3, 184, 4624, 117,\n",
128
+ " 780, 82, 5778, 33, 341, 3, 12618, 377, 4280, 45,\n",
129
+ " 82, 1365, 5, 1615, 19, 48, 78, 614, 12, 7785,\n",
130
+ " 58, 16229, 3, 10, 3, 1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
131
+ " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}\n",
132
+ "tensor([[ 0, 10394, 1]], device='cuda:0')\n",
133
+ "['complaint']\n"
134
+ ]
135
+ }
136
+ ],
137
+ "source": [
138
+ "model.eval()\n",
139
+ "i = 15\n",
140
+ "inputs = tokenizer(f'{text_column} : {dataset[\"test\"][i][\"Tweet text\"]} Label : ', return_tensors=\"pt\")\n",
141
+ "print(dataset[\"test\"][i][\"Tweet text\"])\n",
142
+ "print(inputs)\n",
143
+ "\n",
144
+ "with torch.no_grad():\n",
145
+ " outputs = model.generate(input_ids=inputs[\"input_ids\"].to(\"cuda\"), max_new_tokens=10)\n",
146
+ " print(outputs)\n",
147
+ " print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True))"
148
+ ]
149
+ },
150
+ {
151
+ "cell_type": "code",
152
+ "execution_count": 6,
153
+ "id": "b6d6cd5b",
154
+ "metadata": {},
155
+ "outputs": [
156
+ {
157
+ "name": "stderr",
158
+ "output_type": "stream",
159
+ "text": [
160
+ " 0%| | 0/7 [00:00<?, ?it/s]You're using a T5TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n",
161
+ "100%|████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:10<00:00, 1.48s/it]\n"
162
+ ]
163
+ }
164
+ ],
165
+ "source": [
166
+ "model.eval()\n",
167
+ "eval_preds = []\n",
168
+ "for _, batch in enumerate(tqdm(eval_dataloader)):\n",
169
+ " batch = {k: v.to(\"cuda\") for k, v in batch.items() if k != \"labels\"}\n",
170
+ " with torch.no_grad():\n",
171
+ " outputs = model.generate(**batch, max_new_tokens=10)\n",
172
+ " preds = outputs.detach().cpu().numpy()\n",
173
+ " eval_preds.extend(tokenizer.batch_decode(preds, skip_special_tokens=True))"
174
+ ]
175
+ },
176
+ {
177
+ "cell_type": "code",
178
+ "execution_count": 7,
179
+ "id": "61264abe",
180
+ "metadata": {},
181
+ "outputs": [
182
+ {
183
+ "name": "stdout",
184
+ "output_type": "stream",
185
+ "text": [
186
+ "accuracy=100.0\n",
187
+ "eval_preds[:10]=['no complaint', 'no complaint', 'complaint', 'complaint', 'no complaint', 'no complaint', 'no complaint', 'complaint', 'complaint', 'no complaint']\n",
188
+ "dataset['train'][label_column][:10]=['no complaint', 'no complaint', 'complaint', 'complaint', 'no complaint', 'no complaint', 'no complaint', 'complaint', 'complaint', 'no complaint']\n"
189
+ ]
190
+ }
191
+ ],
192
+ "source": [
193
+ "correct = 0\n",
194
+ "total = 0\n",
195
+ "for pred, true in zip(eval_preds, dataset[\"train\"][label_column]):\n",
196
+ " if pred.strip() == true.strip():\n",
197
+ " correct += 1\n",
198
+ " total += 1\n",
199
+ "accuracy = correct / total * 100\n",
200
+ "print(f\"{accuracy=}\")\n",
201
+ "print(f\"{eval_preds[:10]=}\")\n",
202
+ "print(f\"{dataset['train'][label_column][:10]=}\")"
203
+ ]
204
+ },
205
+ {
206
+ "cell_type": "code",
207
+ "execution_count": null,
208
+ "id": "a70802a3",
209
+ "metadata": {},
210
+ "outputs": [],
211
+ "source": [
212
+ "model.eval()\n",
213
+ "test_preds = []\n",
214
+ "\n",
215
+ "for _, batch in enumerate(tqdm(test_dataloader)):\n",
216
+ " batch = {k: v for k, v in batch.items() if k != \"labels\"}\n",
217
+ " with torch.no_grad():\n",
218
+ " outputs = model.generate(**batch, max_new_tokens=10)\n",
219
+ " preds = outputs.detach().cpu().numpy()\n",
220
+ " test_preds.extend(tokenizer.batch_decode(preds, skip_special_tokens=True))\n",
221
+ " if len(test_preds) > 100:\n",
222
+ " break\n",
223
+ "test_preds"
224
+ ]
225
+ }
226
+ ],
227
+ "metadata": {
228
+ "kernelspec": {
229
+ "display_name": "Python 3 (ipykernel)",
230
+ "language": "python",
231
+ "name": "python3"
232
+ },
233
+ "language_info": {
234
+ "codemirror_mode": {
235
+ "name": "ipython",
236
+ "version": 3
237
+ },
238
+ "file_extension": ".py",
239
+ "mimetype": "text/x-python",
240
+ "name": "python",
241
+ "nbconvert_exporter": "python",
242
+ "pygments_lexer": "ipython3",
243
+ "version": "3.10.5 (v3.10.5:f377153967, Jun 6 2022, 12:36:10) [Clang 13.0.0 (clang-1300.0.29.30)]"
244
+ },
245
+ "vscode": {
246
+ "interpreter": {
247
+ "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
248
+ }
249
+ }
250
+ },
251
+ "nbformat": 4,
252
+ "nbformat_minor": 5
253
+ }
peft_prefix_tuning_seq2seq.ipynb ADDED
@@ -0,0 +1,516 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "5f93b7d1",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stdout",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "\n",
14
+ "===================================BUG REPORT===================================\n",
15
+ "Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n",
16
+ "For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link\n",
17
+ "================================================================================\n",
18
+ "CUDA SETUP: CUDA runtime path found: /home/sourab/miniconda3/envs/ml/lib/libcudart.so\n",
19
+ "CUDA SETUP: Highest compute capability among GPUs detected: 7.5\n",
20
+ "CUDA SETUP: Detected CUDA version 117\n",
21
+ "CUDA SETUP: Loading binary /home/sourab/miniconda3/envs/ml/lib/python3.10/site-packages/bitsandbytes/libbitsandbytes_cuda117.so...\n"
22
+ ]
23
+ }
24
+ ],
25
+ "source": [
26
+ "from transformers import AutoModelForSeq2SeqLM\n",
27
+ "from peft import get_peft_config, get_peft_model, get_peft_model_state_dict, PrefixTuningConfig, TaskType\n",
28
+ "import torch\n",
29
+ "from datasets import load_dataset\n",
30
+ "import os\n",
31
+ "\n",
32
+ "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
33
+ "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"3\"\n",
34
+ "from transformers import AutoTokenizer\n",
35
+ "from torch.utils.data import DataLoader\n",
36
+ "from transformers import default_data_collator, get_linear_schedule_with_warmup\n",
37
+ "from tqdm import tqdm\n",
38
+ "from datasets import load_dataset\n",
39
+ "\n",
40
+ "device = \"cuda\"\n",
41
+ "model_name_or_path = \"t5-large\"\n",
42
+ "tokenizer_name_or_path = \"t5-large\"\n",
43
+ "\n",
44
+ "checkpoint_name = \"financial_sentiment_analysis_prefix_tuning_v1.pt\"\n",
45
+ "text_column = \"sentence\"\n",
46
+ "label_column = \"text_label\"\n",
47
+ "max_length = 128\n",
48
+ "lr = 1e-2\n",
49
+ "num_epochs = 5\n",
50
+ "batch_size = 8"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": null,
56
+ "id": "8d0850ac",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "# creating model\n",
61
+ "peft_config = PrefixTuningConfig(task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, num_virtual_tokens=20)\n",
62
+ "\n",
63
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)\n",
64
+ "model = get_peft_model(model, peft_config)\n",
65
+ "model.print_trainable_parameters()\n",
66
+ "model"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": 3,
72
+ "id": "4ee2babf",
73
+ "metadata": {},
74
+ "outputs": [
75
+ {
76
+ "name": "stderr",
77
+ "output_type": "stream",
78
+ "text": [
79
+ "Found cached dataset financial_phrasebank (/home/sourab/.cache/huggingface/datasets/financial_phrasebank/sentences_allagree/1.0.0/550bde12e6c30e2674da973a55f57edde5181d53f5a5a34c1531c53f93b7e141)\n"
80
+ ]
81
+ },
82
+ {
83
+ "data": {
84
+ "application/vnd.jupyter.widget-view+json": {
85
+ "model_id": "ec4be98991b84181bfa75f8846422b8b",
86
+ "version_major": 2,
87
+ "version_minor": 0
88
+ },
89
+ "text/plain": [
90
+ " 0%| | 0/1 [00:00<?, ?it/s]"
91
+ ]
92
+ },
93
+ "metadata": {},
94
+ "output_type": "display_data"
95
+ },
96
+ {
97
+ "data": {
98
+ "application/vnd.jupyter.widget-view+json": {
99
+ "model_id": "82a6bd694c4f4751a23c370ab51f01a4",
100
+ "version_major": 2,
101
+ "version_minor": 0
102
+ },
103
+ "text/plain": [
104
+ " 0%| | 0/3 [00:00<?, ?ba/s]"
105
+ ]
106
+ },
107
+ "metadata": {},
108
+ "output_type": "display_data"
109
+ },
110
+ {
111
+ "data": {
112
+ "application/vnd.jupyter.widget-view+json": {
113
+ "model_id": "3844878631534468a1495e435563e4b0",
114
+ "version_major": 2,
115
+ "version_minor": 0
116
+ },
117
+ "text/plain": [
118
+ " 0%| | 0/1 [00:00<?, ?ba/s]"
119
+ ]
120
+ },
121
+ "metadata": {},
122
+ "output_type": "display_data"
123
+ },
124
+ {
125
+ "data": {
126
+ "text/plain": [
127
+ "{'sentence': 'Finnish elevators and escalators maker KONE Corporation said on Tuesday ( 18 March ) that it has received a major order from Sir Robert McAlpine to supply all elevators and escalators for the Watermark Place project in the City of London .',\n",
128
+ " 'label': 2,\n",
129
+ " 'text_label': 'positive'}"
130
+ ]
131
+ },
132
+ "execution_count": 3,
133
+ "metadata": {},
134
+ "output_type": "execute_result"
135
+ }
136
+ ],
137
+ "source": [
138
+ "# loading dataset\n",
139
+ "dataset = load_dataset(\"financial_phrasebank\", \"sentences_allagree\")\n",
140
+ "dataset = dataset[\"train\"].train_test_split(test_size=0.1)\n",
141
+ "dataset[\"validation\"] = dataset[\"test\"]\n",
142
+ "del dataset[\"test\"]\n",
143
+ "\n",
144
+ "classes = dataset[\"train\"].features[\"label\"].names\n",
145
+ "dataset = dataset.map(\n",
146
+ " lambda x: {\"text_label\": [classes[label] for label in x[\"label\"]]},\n",
147
+ " batched=True,\n",
148
+ " num_proc=1,\n",
149
+ ")\n",
150
+ "\n",
151
+ "dataset[\"train\"][0]"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": 4,
157
+ "id": "adf9608c",
158
+ "metadata": {},
159
+ "outputs": [
160
+ {
161
+ "name": "stderr",
162
+ "output_type": "stream",
163
+ "text": [
164
+ "/home/sourab/transformers/src/transformers/models/t5/tokenization_t5_fast.py:155: FutureWarning: This tokenizer was incorrectly instantiated with a model max length of 512 which will be corrected in Transformers v5.\n",
165
+ "For now, this behavior is kept to avoid breaking backwards compatibility when padding/encoding with `truncation is True`.\n",
166
+ "- Be aware that you SHOULD NOT rely on t5-large automatically truncating your input to 512 when padding/encoding.\n",
167
+ "- If you want to encode/pad to sequences longer than 512 you can either instantiate this tokenizer with `model_max_length` or pass `max_length` when encoding/padding.\n",
168
+ "- To avoid this warning, please instantiate this tokenizer with `model_max_length` set to your preferred value.\n",
169
+ " warnings.warn(\n"
170
+ ]
171
+ },
172
+ {
173
+ "data": {
174
+ "application/vnd.jupyter.widget-view+json": {
175
+ "model_id": "4af8c12efb5643659573347509079f3a",
176
+ "version_major": 2,
177
+ "version_minor": 0
178
+ },
179
+ "text/plain": [
180
+ "Running tokenizer on dataset: 0%| | 0/3 [00:00<?, ?ba/s]"
181
+ ]
182
+ },
183
+ "metadata": {},
184
+ "output_type": "display_data"
185
+ },
186
+ {
187
+ "data": {
188
+ "application/vnd.jupyter.widget-view+json": {
189
+ "model_id": "86033b6257384584afd034075af808cb",
190
+ "version_major": 2,
191
+ "version_minor": 0
192
+ },
193
+ "text/plain": [
194
+ "Running tokenizer on dataset: 0%| | 0/1 [00:00<?, ?ba/s]"
195
+ ]
196
+ },
197
+ "metadata": {},
198
+ "output_type": "display_data"
199
+ }
200
+ ],
201
+ "source": [
202
+ "# data preprocessing\n",
203
+ "tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)\n",
204
+ "\n",
205
+ "\n",
206
+ "def preprocess_function(examples):\n",
207
+ " inputs = examples[text_column]\n",
208
+ " targets = examples[label_column]\n",
209
+ " model_inputs = tokenizer(inputs, max_length=max_length, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
210
+ " labels = tokenizer(targets, max_length=2, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
211
+ " labels = labels[\"input_ids\"]\n",
212
+ " labels[labels == tokenizer.pad_token_id] = -100\n",
213
+ " model_inputs[\"labels\"] = labels\n",
214
+ " return model_inputs\n",
215
+ "\n",
216
+ "\n",
217
+ "processed_datasets = dataset.map(\n",
218
+ " preprocess_function,\n",
219
+ " batched=True,\n",
220
+ " num_proc=1,\n",
221
+ " remove_columns=dataset[\"train\"].column_names,\n",
222
+ " load_from_cache_file=False,\n",
223
+ " desc=\"Running tokenizer on dataset\",\n",
224
+ ")\n",
225
+ "\n",
226
+ "train_dataset = processed_datasets[\"train\"]\n",
227
+ "eval_dataset = processed_datasets[\"validation\"]\n",
228
+ "\n",
229
+ "train_dataloader = DataLoader(\n",
230
+ " train_dataset, shuffle=True, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True\n",
231
+ ")\n",
232
+ "eval_dataloader = DataLoader(eval_dataset, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True)"
233
+ ]
234
+ },
235
+ {
236
+ "cell_type": "code",
237
+ "execution_count": 5,
238
+ "id": "f733a3c6",
239
+ "metadata": {},
240
+ "outputs": [],
241
+ "source": [
242
+ "# optimizer and lr scheduler\n",
243
+ "optimizer = torch.optim.AdamW(model.parameters(), lr=lr)\n",
244
+ "lr_scheduler = get_linear_schedule_with_warmup(\n",
245
+ " optimizer=optimizer,\n",
246
+ " num_warmup_steps=0,\n",
247
+ " num_training_steps=(len(train_dataloader) * num_epochs),\n",
248
+ ")"
249
+ ]
250
+ },
251
+ {
252
+ "cell_type": "code",
253
+ "execution_count": 6,
254
+ "id": "6b3a4090",
255
+ "metadata": {},
256
+ "outputs": [
257
+ {
258
+ "name": "stderr",
259
+ "output_type": "stream",
260
+ "text": [
261
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:49<00:00, 5.15it/s]\n",
262
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:03<00:00, 7.56it/s]\n"
263
+ ]
264
+ },
265
+ {
266
+ "name": "stdout",
267
+ "output_type": "stream",
268
+ "text": [
269
+ "epoch=0: train_ppl=tensor(2760654.5000, device='cuda:0') train_epoch_loss=tensor(14.8310, device='cuda:0') eval_ppl=tensor(1.0124, device='cuda:0') eval_epoch_loss=tensor(0.0124, device='cuda:0')\n"
270
+ ]
271
+ },
272
+ {
273
+ "name": "stderr",
274
+ "output_type": "stream",
275
+ "text": [
276
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:40<00:00, 6.22it/s]\n",
277
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:05<00:00, 5.05it/s]\n"
278
+ ]
279
+ },
280
+ {
281
+ "name": "stdout",
282
+ "output_type": "stream",
283
+ "text": [
284
+ "epoch=1: train_ppl=tensor(2.7329, device='cuda:0') train_epoch_loss=tensor(1.0054, device='cuda:0') eval_ppl=tensor(1.0081, device='cuda:0') eval_epoch_loss=tensor(0.0080, device='cuda:0')\n"
285
+ ]
286
+ },
287
+ {
288
+ "name": "stderr",
289
+ "output_type": "stream",
290
+ "text": [
291
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:58<00:00, 4.36it/s]\n",
292
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:05<00:00, 5.05it/s]\n"
293
+ ]
294
+ },
295
+ {
296
+ "name": "stdout",
297
+ "output_type": "stream",
298
+ "text": [
299
+ "epoch=2: train_ppl=tensor(2.1698, device='cuda:0') train_epoch_loss=tensor(0.7747, device='cuda:0') eval_ppl=tensor(1.0057, device='cuda:0') eval_epoch_loss=tensor(0.0057, device='cuda:0')\n"
300
+ ]
301
+ },
302
+ {
303
+ "name": "stderr",
304
+ "output_type": "stream",
305
+ "text": [
306
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:58<00:00, 4.35it/s]\n",
307
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:05<00:00, 5.06it/s]\n"
308
+ ]
309
+ },
310
+ {
311
+ "name": "stdout",
312
+ "output_type": "stream",
313
+ "text": [
314
+ "epoch=3: train_ppl=tensor(2.0724, device='cuda:0') train_epoch_loss=tensor(0.7287, device='cuda:0') eval_ppl=tensor(1.0051, device='cuda:0') eval_epoch_loss=tensor(0.0051, device='cuda:0')\n"
315
+ ]
316
+ },
317
+ {
318
+ "name": "stderr",
319
+ "output_type": "stream",
320
+ "text": [
321
+ "100%|████████████████████████████████████████████████████████████████████████████████████████| 255/255 [01:02<00:00, 4.10it/s]\n",
322
+ "100%|██████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:06<00:00, 4.74it/s]\n"
323
+ ]
324
+ },
325
+ {
326
+ "name": "stdout",
327
+ "output_type": "stream",
328
+ "text": [
329
+ "epoch=4: train_ppl=tensor(1.7598, device='cuda:0') train_epoch_loss=tensor(0.5652, device='cuda:0') eval_ppl=tensor(1.0047, device='cuda:0') eval_epoch_loss=tensor(0.0047, device='cuda:0')\n"
330
+ ]
331
+ }
332
+ ],
333
+ "source": [
334
+ "# training and evaluation\n",
335
+ "model = model.to(device)\n",
336
+ "\n",
337
+ "for epoch in range(num_epochs):\n",
338
+ " model.train()\n",
339
+ " total_loss = 0\n",
340
+ " for step, batch in enumerate(tqdm(train_dataloader)):\n",
341
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
342
+ " outputs = model(**batch)\n",
343
+ " loss = outputs.loss\n",
344
+ " total_loss += loss.detach().float()\n",
345
+ " loss.backward()\n",
346
+ " optimizer.step()\n",
347
+ " lr_scheduler.step()\n",
348
+ " optimizer.zero_grad()\n",
349
+ "\n",
350
+ " model.eval()\n",
351
+ " eval_loss = 0\n",
352
+ " eval_preds = []\n",
353
+ " for step, batch in enumerate(tqdm(eval_dataloader)):\n",
354
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
355
+ " with torch.no_grad():\n",
356
+ " outputs = model(**batch)\n",
357
+ " loss = outputs.loss\n",
358
+ " eval_loss += loss.detach().float()\n",
359
+ " eval_preds.extend(\n",
360
+ " tokenizer.batch_decode(torch.argmax(outputs.logits, -1).detach().cpu().numpy(), skip_special_tokens=True)\n",
361
+ " )\n",
362
+ "\n",
363
+ " eval_epoch_loss = eval_loss / len(eval_dataloader)\n",
364
+ " eval_ppl = torch.exp(eval_epoch_loss)\n",
365
+ " train_epoch_loss = total_loss / len(train_dataloader)\n",
366
+ " train_ppl = torch.exp(train_epoch_loss)\n",
367
+ " print(f\"{epoch=}: {train_ppl=} {train_epoch_loss=} {eval_ppl=} {eval_epoch_loss=}\")"
368
+ ]
369
+ },
370
+ {
371
+ "cell_type": "code",
372
+ "execution_count": 7,
373
+ "id": "6cafa67b",
374
+ "metadata": {},
375
+ "outputs": [
376
+ {
377
+ "name": "stdout",
378
+ "output_type": "stream",
379
+ "text": [
380
+ "accuracy=96.91629955947137 % on the evaluation dataset\n",
381
+ "eval_preds[:10]=['negative', 'positive', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral']\n",
382
+ "dataset['validation']['text_label'][:10]=['negative', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'neutral']\n"
383
+ ]
384
+ }
385
+ ],
386
+ "source": [
387
+ "# print accuracy\n",
388
+ "correct = 0\n",
389
+ "total = 0\n",
390
+ "for pred, true in zip(eval_preds, dataset[\"validation\"][\"text_label\"]):\n",
391
+ " if pred.strip() == true.strip():\n",
392
+ " correct += 1\n",
393
+ " total += 1\n",
394
+ "accuracy = correct / total * 100\n",
395
+ "print(f\"{accuracy=} % on the evaluation dataset\")\n",
396
+ "print(f\"{eval_preds[:10]=}\")\n",
397
+ "print(f\"{dataset['validation']['text_label'][:10]=}\")"
398
+ ]
399
+ },
400
+ {
401
+ "cell_type": "code",
402
+ "execution_count": 8,
403
+ "id": "a8de6005",
404
+ "metadata": {},
405
+ "outputs": [],
406
+ "source": [
407
+ "# saving model\n",
408
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
409
+ "model.save_pretrained(peft_model_id)"
410
+ ]
411
+ },
412
+ {
413
+ "cell_type": "code",
414
+ "execution_count": 9,
415
+ "id": "bd20cd4c",
416
+ "metadata": {},
417
+ "outputs": [
418
+ {
419
+ "name": "stdout",
420
+ "output_type": "stream",
421
+ "text": [
422
+ "3,8M\tt5-large_PREFIX_TUNING_SEQ_2_SEQ_LM/adapter_model.bin\r\n"
423
+ ]
424
+ }
425
+ ],
426
+ "source": [
427
+ "ckpt = f\"{peft_model_id}/adapter_model.bin\"\n",
428
+ "!du -h $ckpt"
429
+ ]
430
+ },
431
+ {
432
+ "cell_type": "code",
433
+ "execution_count": 11,
434
+ "id": "76c2fc29",
435
+ "metadata": {},
436
+ "outputs": [],
437
+ "source": [
438
+ "from peft import PeftModel, PeftConfig\n",
439
+ "\n",
440
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
441
+ "\n",
442
+ "config = PeftConfig.from_pretrained(peft_model_id)\n",
443
+ "model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)\n",
444
+ "model = PeftModel.from_pretrained(model, peft_model_id)"
445
+ ]
446
+ },
447
+ {
448
+ "cell_type": "code",
449
+ "execution_count": 27,
450
+ "id": "d997f1cc",
451
+ "metadata": {},
452
+ "outputs": [
453
+ {
454
+ "name": "stdout",
455
+ "output_type": "stream",
456
+ "text": [
457
+ "Acando AB ( ACANB SS ) fell 8.9 percent to 13.35 kronor , the lowest close since Dec. 11 .\n",
458
+ "{'input_ids': tensor([[ 4292, 232, 32, 3, 5359, 41, 3, 22029, 14972, 3,\n",
459
+ " 4256, 3, 61, 4728, 4848, 1298, 1093, 12, 8808, 2469,\n",
460
+ " 3, 22318, 29, 127, 3, 6, 8, 7402, 885, 437,\n",
461
+ " 4451, 5, 850, 3, 5, 1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
462
+ " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}\n",
463
+ "tensor([[ 0, 2841, 1]])\n",
464
+ "['negative']\n"
465
+ ]
466
+ }
467
+ ],
468
+ "source": [
469
+ "model.eval()\n",
470
+ "i = 107\n",
471
+ "inputs = tokenizer(dataset[\"validation\"][text_column][i], return_tensors=\"pt\")\n",
472
+ "print(dataset[\"validation\"][text_column][i])\n",
473
+ "print(inputs)\n",
474
+ "\n",
475
+ "with torch.no_grad():\n",
476
+ " outputs = model.generate(input_ids=inputs[\"input_ids\"], max_new_tokens=10)\n",
477
+ " print(outputs)\n",
478
+ " print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True))"
479
+ ]
480
+ },
481
+ {
482
+ "cell_type": "code",
483
+ "execution_count": null,
484
+ "id": "fb746c1e",
485
+ "metadata": {},
486
+ "outputs": [],
487
+ "source": []
488
+ }
489
+ ],
490
+ "metadata": {
491
+ "kernelspec": {
492
+ "display_name": "Python 3 (ipykernel)",
493
+ "language": "python",
494
+ "name": "python3"
495
+ },
496
+ "language_info": {
497
+ "codemirror_mode": {
498
+ "name": "ipython",
499
+ "version": 3
500
+ },
501
+ "file_extension": ".py",
502
+ "mimetype": "text/x-python",
503
+ "name": "python",
504
+ "nbconvert_exporter": "python",
505
+ "pygments_lexer": "ipython3",
506
+ "version": "3.10.5"
507
+ },
508
+ "vscode": {
509
+ "interpreter": {
510
+ "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
511
+ }
512
+ }
513
+ },
514
+ "nbformat": 4,
515
+ "nbformat_minor": 5
516
+ }
peft_prompt_tuning_seq2seq.ipynb ADDED
@@ -0,0 +1,804 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "5f93b7d1",
7
+ "metadata": {
8
+ "ExecuteTime": {
9
+ "end_time": "2023-05-30T08:37:58.711225Z",
10
+ "start_time": "2023-05-30T08:37:56.881307Z"
11
+ }
12
+ },
13
+ "outputs": [
14
+ {
15
+ "name": "stdout",
16
+ "output_type": "stream",
17
+ "text": [
18
+ "\n",
19
+ "===================================BUG REPORT===================================\n",
20
+ "Welcome to bitsandbytes. For bug reports, please run\n",
21
+ "\n",
22
+ "python -m bitsandbytes\n",
23
+ "\n",
24
+ " and submit this information together with your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n",
25
+ "================================================================================\n",
26
+ "bin /udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/libbitsandbytes_cuda117.so\n",
27
+ "CUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching in backup paths...\n",
28
+ "CUDA SETUP: CUDA runtime path found: /usr/local/cuda/lib64/libcudart.so.11.0\n",
29
+ "CUDA SETUP: Highest compute capability among GPUs detected: 8.0\n",
30
+ "CUDA SETUP: Detected CUDA version 117\n",
31
+ "CUDA SETUP: Loading binary /udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/libbitsandbytes_cuda117.so...\n"
32
+ ]
33
+ },
34
+ {
35
+ "name": "stderr",
36
+ "output_type": "stream",
37
+ "text": [
38
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: /udir/tschilla/anaconda3 did not contain ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] as expected! Searching further paths...\n",
39
+ " warn(msg)\n",
40
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('Europe/Paris')}\n",
41
+ " warn(msg)\n",
42
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/udir/tschilla/.cache/dotnet_bundle_extract')}\n",
43
+ " warn(msg)\n",
44
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('5002'), PosixPath('http'), PosixPath('//127.0.0.1')}\n",
45
+ " warn(msg)\n",
46
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('() { ( alias;\\n eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@\\n}')}\n",
47
+ " warn(msg)\n",
48
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('module'), PosixPath('//matplotlib_inline.backend_inline')}\n",
49
+ " warn(msg)\n",
50
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: Found duplicate ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] files: {PosixPath('/usr/local/cuda/lib64/libcudart.so.11.0'), PosixPath('/usr/local/cuda/lib64/libcudart.so')}.. We'll flip a coin and try one of these, in order to fail forward.\n",
51
+ "Either way, this might cause trouble in the future:\n",
52
+ "If you get `CUDA error: invalid device function` errors, the above might be the cause and the solution is to make sure only one ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] in the paths that we search based on your env.\n",
53
+ " warn(msg)\n"
54
+ ]
55
+ }
56
+ ],
57
+ "source": [
58
+ "import os\n",
59
+ "\n",
60
+ "import torch\n",
61
+ "from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, default_data_collator, get_linear_schedule_with_warmup\n",
62
+ "from peft import get_peft_model, PromptTuningConfig, TaskType, PromptTuningInit\n",
63
+ "from torch.utils.data import DataLoader\n",
64
+ "from tqdm import tqdm\n",
65
+ "from datasets import load_dataset\n",
66
+ "\n",
67
+ "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
68
+ "\n",
69
+ "device = \"cuda\"\n",
70
+ "model_name_or_path = \"t5-large\"\n",
71
+ "tokenizer_name_or_path = \"t5-large\"\n",
72
+ "\n",
73
+ "checkpoint_name = \"financial_sentiment_analysis_prompt_tuning_v1.pt\"\n",
74
+ "text_column = \"sentence\"\n",
75
+ "label_column = \"text_label\"\n",
76
+ "max_length = 128\n",
77
+ "lr = 1\n",
78
+ "num_epochs = 5\n",
79
+ "batch_size = 8"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 2,
85
+ "id": "8d0850ac",
86
+ "metadata": {
87
+ "ExecuteTime": {
88
+ "end_time": "2023-05-30T08:38:12.413984Z",
89
+ "start_time": "2023-05-30T08:38:04.601042Z"
90
+ }
91
+ },
92
+ "outputs": [
93
+ {
94
+ "name": "stdout",
95
+ "output_type": "stream",
96
+ "text": [
97
+ "trainable params: 40960 || all params: 737709056 || trainable%: 0.005552324411210698\n"
98
+ ]
99
+ },
100
+ {
101
+ "name": "stderr",
102
+ "output_type": "stream",
103
+ "text": [
104
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/models/t5/tokenization_t5_fast.py:155: FutureWarning: This tokenizer was incorrectly instantiated with a model max length of 512 which will be corrected in Transformers v5.\n",
105
+ "For now, this behavior is kept to avoid breaking backwards compatibility when padding/encoding with `truncation is True`.\n",
106
+ "- Be aware that you SHOULD NOT rely on t5-large automatically truncating your input to 512 when padding/encoding.\n",
107
+ "- If you want to encode/pad to sequences longer than 512 you can either instantiate this tokenizer with `model_max_length` or pass `max_length` when encoding/padding.\n",
108
+ "- To avoid this warning, please instantiate this tokenizer with `model_max_length` set to your preferred value.\n",
109
+ " warnings.warn(\n"
110
+ ]
111
+ },
112
+ {
113
+ "data": {
114
+ "text/plain": [
115
+ "PeftModelForSeq2SeqLM(\n",
116
+ " (base_model): T5ForConditionalGeneration(\n",
117
+ " (shared): Embedding(32128, 1024)\n",
118
+ " (encoder): T5Stack(\n",
119
+ " (embed_tokens): Embedding(32128, 1024)\n",
120
+ " (block): ModuleList(\n",
121
+ " (0): T5Block(\n",
122
+ " (layer): ModuleList(\n",
123
+ " (0): T5LayerSelfAttention(\n",
124
+ " (SelfAttention): T5Attention(\n",
125
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
126
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
127
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
128
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
129
+ " (relative_attention_bias): Embedding(32, 16)\n",
130
+ " )\n",
131
+ " (layer_norm): T5LayerNorm()\n",
132
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
133
+ " )\n",
134
+ " (1): T5LayerFF(\n",
135
+ " (DenseReluDense): T5DenseActDense(\n",
136
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
137
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
138
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
139
+ " (act): ReLU()\n",
140
+ " )\n",
141
+ " (layer_norm): T5LayerNorm()\n",
142
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
143
+ " )\n",
144
+ " )\n",
145
+ " )\n",
146
+ " (1-23): 23 x T5Block(\n",
147
+ " (layer): ModuleList(\n",
148
+ " (0): T5LayerSelfAttention(\n",
149
+ " (SelfAttention): T5Attention(\n",
150
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
151
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
152
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
153
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
154
+ " )\n",
155
+ " (layer_norm): T5LayerNorm()\n",
156
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
157
+ " )\n",
158
+ " (1): T5LayerFF(\n",
159
+ " (DenseReluDense): T5DenseActDense(\n",
160
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
161
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
162
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
163
+ " (act): ReLU()\n",
164
+ " )\n",
165
+ " (layer_norm): T5LayerNorm()\n",
166
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
167
+ " )\n",
168
+ " )\n",
169
+ " )\n",
170
+ " )\n",
171
+ " (final_layer_norm): T5LayerNorm()\n",
172
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
173
+ " )\n",
174
+ " (decoder): T5Stack(\n",
175
+ " (embed_tokens): Embedding(32128, 1024)\n",
176
+ " (block): ModuleList(\n",
177
+ " (0): T5Block(\n",
178
+ " (layer): ModuleList(\n",
179
+ " (0): T5LayerSelfAttention(\n",
180
+ " (SelfAttention): T5Attention(\n",
181
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
182
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
183
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
184
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
185
+ " (relative_attention_bias): Embedding(32, 16)\n",
186
+ " )\n",
187
+ " (layer_norm): T5LayerNorm()\n",
188
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
189
+ " )\n",
190
+ " (1): T5LayerCrossAttention(\n",
191
+ " (EncDecAttention): T5Attention(\n",
192
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
193
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
194
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
195
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
196
+ " )\n",
197
+ " (layer_norm): T5LayerNorm()\n",
198
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
199
+ " )\n",
200
+ " (2): T5LayerFF(\n",
201
+ " (DenseReluDense): T5DenseActDense(\n",
202
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
203
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
204
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
205
+ " (act): ReLU()\n",
206
+ " )\n",
207
+ " (layer_norm): T5LayerNorm()\n",
208
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
209
+ " )\n",
210
+ " )\n",
211
+ " )\n",
212
+ " (1-23): 23 x T5Block(\n",
213
+ " (layer): ModuleList(\n",
214
+ " (0): T5LayerSelfAttention(\n",
215
+ " (SelfAttention): T5Attention(\n",
216
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
217
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
218
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
219
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
220
+ " )\n",
221
+ " (layer_norm): T5LayerNorm()\n",
222
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
223
+ " )\n",
224
+ " (1): T5LayerCrossAttention(\n",
225
+ " (EncDecAttention): T5Attention(\n",
226
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
227
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
228
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
229
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
230
+ " )\n",
231
+ " (layer_norm): T5LayerNorm()\n",
232
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
233
+ " )\n",
234
+ " (2): T5LayerFF(\n",
235
+ " (DenseReluDense): T5DenseActDense(\n",
236
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
237
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
238
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
239
+ " (act): ReLU()\n",
240
+ " )\n",
241
+ " (layer_norm): T5LayerNorm()\n",
242
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
243
+ " )\n",
244
+ " )\n",
245
+ " )\n",
246
+ " )\n",
247
+ " (final_layer_norm): T5LayerNorm()\n",
248
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
249
+ " )\n",
250
+ " (lm_head): Linear(in_features=1024, out_features=32128, bias=False)\n",
251
+ " )\n",
252
+ " (prompt_encoder): ModuleDict(\n",
253
+ " (default): PromptEmbedding(\n",
254
+ " (embedding): Embedding(40, 1024)\n",
255
+ " )\n",
256
+ " )\n",
257
+ " (word_embeddings): Embedding(32128, 1024)\n",
258
+ ")"
259
+ ]
260
+ },
261
+ "execution_count": 2,
262
+ "metadata": {},
263
+ "output_type": "execute_result"
264
+ }
265
+ ],
266
+ "source": [
267
+ "# creating model\n",
268
+ "peft_config = PromptTuningConfig(\n",
269
+ " task_type=TaskType.SEQ_2_SEQ_LM,\n",
270
+ " prompt_tuning_init=PromptTuningInit.TEXT,\n",
271
+ " num_virtual_tokens=20,\n",
272
+ " prompt_tuning_init_text=\"What is the sentiment of this article?\\n\",\n",
273
+ " inference_mode=False,\n",
274
+ " tokenizer_name_or_path=model_name_or_path,\n",
275
+ ")\n",
276
+ "\n",
277
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)\n",
278
+ "model = get_peft_model(model, peft_config)\n",
279
+ "model.print_trainable_parameters()\n",
280
+ "model"
281
+ ]
282
+ },
283
+ {
284
+ "cell_type": "code",
285
+ "execution_count": 3,
286
+ "id": "4ee2babf",
287
+ "metadata": {
288
+ "ExecuteTime": {
289
+ "end_time": "2023-05-30T08:38:18.759143Z",
290
+ "start_time": "2023-05-30T08:38:17.881621Z"
291
+ }
292
+ },
293
+ "outputs": [
294
+ {
295
+ "name": "stderr",
296
+ "output_type": "stream",
297
+ "text": [
298
+ "Found cached dataset financial_phrasebank (/data/proxem/huggingface/datasets/financial_phrasebank/sentences_allagree/1.0.0/550bde12e6c30e2674da973a55f57edde5181d53f5a5a34c1531c53f93b7e141)\n"
299
+ ]
300
+ },
301
+ {
302
+ "data": {
303
+ "application/vnd.jupyter.widget-view+json": {
304
+ "model_id": "fb63f50cb7cb4f5aae10648ba74d6c4e",
305
+ "version_major": 2,
306
+ "version_minor": 0
307
+ },
308
+ "text/plain": [
309
+ " 0%| | 0/1 [00:00<?, ?it/s]"
310
+ ]
311
+ },
312
+ "metadata": {},
313
+ "output_type": "display_data"
314
+ },
315
+ {
316
+ "data": {
317
+ "application/vnd.jupyter.widget-view+json": {
318
+ "model_id": "",
319
+ "version_major": 2,
320
+ "version_minor": 0
321
+ },
322
+ "text/plain": [
323
+ "Map: 0%| | 0/2037 [00:00<?, ? examples/s]"
324
+ ]
325
+ },
326
+ "metadata": {},
327
+ "output_type": "display_data"
328
+ },
329
+ {
330
+ "data": {
331
+ "application/vnd.jupyter.widget-view+json": {
332
+ "model_id": "",
333
+ "version_major": 2,
334
+ "version_minor": 0
335
+ },
336
+ "text/plain": [
337
+ "Map: 0%| | 0/227 [00:00<?, ? examples/s]"
338
+ ]
339
+ },
340
+ "metadata": {},
341
+ "output_type": "display_data"
342
+ },
343
+ {
344
+ "data": {
345
+ "text/plain": [
346
+ "{'sentence': '`` Lining stone sales were also good in the early autumn , and order books are strong to the end of the year .',\n",
347
+ " 'label': 2,\n",
348
+ " 'text_label': 'positive'}"
349
+ ]
350
+ },
351
+ "execution_count": 3,
352
+ "metadata": {},
353
+ "output_type": "execute_result"
354
+ }
355
+ ],
356
+ "source": [
357
+ "# loading dataset\n",
358
+ "dataset = load_dataset(\"financial_phrasebank\", \"sentences_allagree\")\n",
359
+ "dataset = dataset[\"train\"].train_test_split(test_size=0.1)\n",
360
+ "dataset[\"validation\"] = dataset[\"test\"]\n",
361
+ "del dataset[\"test\"]\n",
362
+ "\n",
363
+ "classes = dataset[\"train\"].features[\"label\"].names\n",
364
+ "dataset = dataset.map(\n",
365
+ " lambda x: {\"text_label\": [classes[label] for label in x[\"label\"]]},\n",
366
+ " batched=True,\n",
367
+ " num_proc=1,\n",
368
+ ")\n",
369
+ "\n",
370
+ "dataset[\"train\"][0]"
371
+ ]
372
+ },
373
+ {
374
+ "cell_type": "code",
375
+ "execution_count": 4,
376
+ "id": "adf9608c",
377
+ "metadata": {
378
+ "ExecuteTime": {
379
+ "end_time": "2023-05-30T08:38:21.132266Z",
380
+ "start_time": "2023-05-30T08:38:20.340722Z"
381
+ }
382
+ },
383
+ "outputs": [
384
+ {
385
+ "data": {
386
+ "application/vnd.jupyter.widget-view+json": {
387
+ "model_id": "",
388
+ "version_major": 2,
389
+ "version_minor": 0
390
+ },
391
+ "text/plain": [
392
+ "Running tokenizer on dataset: 0%| | 0/2037 [00:00<?, ? examples/s]"
393
+ ]
394
+ },
395
+ "metadata": {},
396
+ "output_type": "display_data"
397
+ },
398
+ {
399
+ "data": {
400
+ "application/vnd.jupyter.widget-view+json": {
401
+ "model_id": "",
402
+ "version_major": 2,
403
+ "version_minor": 0
404
+ },
405
+ "text/plain": [
406
+ "Running tokenizer on dataset: 0%| | 0/227 [00:00<?, ? examples/s]"
407
+ ]
408
+ },
409
+ "metadata": {},
410
+ "output_type": "display_data"
411
+ }
412
+ ],
413
+ "source": [
414
+ "# data preprocessing\n",
415
+ "tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)\n",
416
+ "target_max_length = max([len(tokenizer(class_label)[\"input_ids\"]) for class_label in classes])\n",
417
+ "\n",
418
+ "\n",
419
+ "def preprocess_function(examples):\n",
420
+ " inputs = examples[text_column]\n",
421
+ " targets = examples[label_column]\n",
422
+ " model_inputs = tokenizer(inputs, max_length=max_length, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
423
+ " labels = tokenizer(\n",
424
+ " targets, max_length=target_max_length, padding=\"max_length\", truncation=True, return_tensors=\"pt\"\n",
425
+ " )\n",
426
+ " labels = labels[\"input_ids\"]\n",
427
+ " labels[labels == tokenizer.pad_token_id] = -100\n",
428
+ " model_inputs[\"labels\"] = labels\n",
429
+ " return model_inputs\n",
430
+ "\n",
431
+ "\n",
432
+ "processed_datasets = dataset.map(\n",
433
+ " preprocess_function,\n",
434
+ " batched=True,\n",
435
+ " num_proc=1,\n",
436
+ " remove_columns=dataset[\"train\"].column_names,\n",
437
+ " load_from_cache_file=False,\n",
438
+ " desc=\"Running tokenizer on dataset\",\n",
439
+ ")\n",
440
+ "\n",
441
+ "train_dataset = processed_datasets[\"train\"]\n",
442
+ "eval_dataset = processed_datasets[\"validation\"]\n",
443
+ "\n",
444
+ "train_dataloader = DataLoader(\n",
445
+ " train_dataset, shuffle=True, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True\n",
446
+ ")\n",
447
+ "eval_dataloader = DataLoader(eval_dataset, collate_fn=default_data_collator, batch_size=batch_size, pin_memory=True)"
448
+ ]
449
+ },
450
+ {
451
+ "cell_type": "code",
452
+ "execution_count": 5,
453
+ "id": "f733a3c6",
454
+ "metadata": {
455
+ "ExecuteTime": {
456
+ "end_time": "2023-05-30T08:38:22.907922Z",
457
+ "start_time": "2023-05-30T08:38:22.901057Z"
458
+ }
459
+ },
460
+ "outputs": [],
461
+ "source": [
462
+ "# optimizer and lr scheduler\n",
463
+ "optimizer = torch.optim.AdamW(model.parameters(), lr=lr)\n",
464
+ "lr_scheduler = get_linear_schedule_with_warmup(\n",
465
+ " optimizer=optimizer,\n",
466
+ " num_warmup_steps=0,\n",
467
+ " num_training_steps=(len(train_dataloader) * num_epochs),\n",
468
+ ")"
469
+ ]
470
+ },
471
+ {
472
+ "cell_type": "code",
473
+ "execution_count": 7,
474
+ "id": "6b3a4090",
475
+ "metadata": {
476
+ "ExecuteTime": {
477
+ "end_time": "2023-05-30T08:42:29.409070Z",
478
+ "start_time": "2023-05-30T08:38:50.102263Z"
479
+ }
480
+ },
481
+ "outputs": [
482
+ {
483
+ "name": "stderr",
484
+ "output_type": "stream",
485
+ "text": [
486
+ "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:42<00:00, 6.05it/s]\n",
487
+ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:02<00:00, 14.40it/s]\n"
488
+ ]
489
+ },
490
+ {
491
+ "name": "stdout",
492
+ "output_type": "stream",
493
+ "text": [
494
+ "epoch=0: train_ppl=tensor(8.0846, device='cuda:0') train_epoch_loss=tensor(2.0900, device='cuda:0') eval_ppl=tensor(1.3542, device='cuda:0') eval_epoch_loss=tensor(0.3032, device='cuda:0')\n"
495
+ ]
496
+ },
497
+ {
498
+ "name": "stderr",
499
+ "output_type": "stream",
500
+ "text": [
501
+ "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:41<00:00, 6.15it/s]\n",
502
+ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:02<00:00, 14.42it/s]\n"
503
+ ]
504
+ },
505
+ {
506
+ "name": "stdout",
507
+ "output_type": "stream",
508
+ "text": [
509
+ "epoch=1: train_ppl=tensor(1.5088, device='cuda:0') train_epoch_loss=tensor(0.4113, device='cuda:0') eval_ppl=tensor(1.2692, device='cuda:0') eval_epoch_loss=tensor(0.2384, device='cuda:0')\n"
510
+ ]
511
+ },
512
+ {
513
+ "name": "stderr",
514
+ "output_type": "stream",
515
+ "text": [
516
+ "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:41<00:00, 6.18it/s]\n",
517
+ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:02<00:00, 14.45it/s]\n"
518
+ ]
519
+ },
520
+ {
521
+ "name": "stdout",
522
+ "output_type": "stream",
523
+ "text": [
524
+ "epoch=2: train_ppl=tensor(1.5322, device='cuda:0') train_epoch_loss=tensor(0.4267, device='cuda:0') eval_ppl=tensor(1.2065, device='cuda:0') eval_epoch_loss=tensor(0.1877, device='cuda:0')\n"
525
+ ]
526
+ },
527
+ {
528
+ "name": "stderr",
529
+ "output_type": "stream",
530
+ "text": [
531
+ "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:41<00:00, 6.17it/s]\n",
532
+ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:02<00:00, 14.38it/s]\n"
533
+ ]
534
+ },
535
+ {
536
+ "name": "stdout",
537
+ "output_type": "stream",
538
+ "text": [
539
+ "epoch=3: train_ppl=tensor(1.4475, device='cuda:0') train_epoch_loss=tensor(0.3699, device='cuda:0') eval_ppl=tensor(1.2346, device='cuda:0') eval_epoch_loss=tensor(0.2107, device='cuda:0')\n"
540
+ ]
541
+ },
542
+ {
543
+ "name": "stderr",
544
+ "output_type": "stream",
545
+ "text": [
546
+ "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 255/255 [00:42<00:00, 5.94it/s]\n",
547
+ "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29/29 [00:02<00:00, 14.42it/s]"
548
+ ]
549
+ },
550
+ {
551
+ "name": "stdout",
552
+ "output_type": "stream",
553
+ "text": [
554
+ "epoch=4: train_ppl=tensor(1.3428, device='cuda:0') train_epoch_loss=tensor(0.2948, device='cuda:0') eval_ppl=tensor(1.2041, device='cuda:0') eval_epoch_loss=tensor(0.1857, device='cuda:0')\n"
555
+ ]
556
+ },
557
+ {
558
+ "name": "stderr",
559
+ "output_type": "stream",
560
+ "text": [
561
+ "\n"
562
+ ]
563
+ }
564
+ ],
565
+ "source": [
566
+ "# training and evaluation\n",
567
+ "model = model.to(device)\n",
568
+ "\n",
569
+ "for epoch in range(num_epochs):\n",
570
+ " model.train()\n",
571
+ " total_loss = 0\n",
572
+ " for step, batch in enumerate(tqdm(train_dataloader)):\n",
573
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
574
+ " outputs = model(**batch)\n",
575
+ " loss = outputs.loss\n",
576
+ " total_loss += loss.detach().float()\n",
577
+ " loss.backward()\n",
578
+ " optimizer.step()\n",
579
+ " lr_scheduler.step()\n",
580
+ " optimizer.zero_grad()\n",
581
+ "\n",
582
+ " model.eval()\n",
583
+ " eval_loss = 0\n",
584
+ " eval_preds = []\n",
585
+ " for step, batch in enumerate(tqdm(eval_dataloader)):\n",
586
+ " batch = {k: v.to(device) for k, v in batch.items()}\n",
587
+ " with torch.no_grad():\n",
588
+ " outputs = model(**batch)\n",
589
+ " loss = outputs.loss\n",
590
+ " eval_loss += loss.detach().float()\n",
591
+ " eval_preds.extend(\n",
592
+ " tokenizer.batch_decode(torch.argmax(outputs.logits, -1).detach().cpu().numpy(), skip_special_tokens=True)\n",
593
+ " )\n",
594
+ "\n",
595
+ " eval_epoch_loss = eval_loss / len(eval_dataloader)\n",
596
+ " eval_ppl = torch.exp(eval_epoch_loss)\n",
597
+ " train_epoch_loss = total_loss / len(train_dataloader)\n",
598
+ " train_ppl = torch.exp(train_epoch_loss)\n",
599
+ " print(f\"{epoch=}: {train_ppl=} {train_epoch_loss=} {eval_ppl=} {eval_epoch_loss=}\")"
600
+ ]
601
+ },
602
+ {
603
+ "cell_type": "code",
604
+ "execution_count": 8,
605
+ "id": "6cafa67b",
606
+ "metadata": {
607
+ "ExecuteTime": {
608
+ "end_time": "2023-05-30T08:42:42.844671Z",
609
+ "start_time": "2023-05-30T08:42:42.840447Z"
610
+ }
611
+ },
612
+ "outputs": [
613
+ {
614
+ "name": "stdout",
615
+ "output_type": "stream",
616
+ "text": [
617
+ "accuracy=85.46255506607929 % on the evaluation dataset\n",
618
+ "eval_preds[:10]=['neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'positive', 'neutral', 'negative', 'neutral', 'positive']\n",
619
+ "dataset['validation']['text_label'][:10]=['neutral', 'neutral', 'neutral', 'neutral', 'neutral', 'positive', 'neutral', 'negative', 'positive', 'neutral']\n"
620
+ ]
621
+ }
622
+ ],
623
+ "source": [
624
+ "# print accuracy\n",
625
+ "correct = 0\n",
626
+ "total = 0\n",
627
+ "for pred, true in zip(eval_preds, dataset[\"validation\"][\"text_label\"]):\n",
628
+ " if pred.strip() == true.strip():\n",
629
+ " correct += 1\n",
630
+ " total += 1\n",
631
+ "accuracy = correct / total * 100\n",
632
+ "print(f\"{accuracy=} % on the evaluation dataset\")\n",
633
+ "print(f\"{eval_preds[:10]=}\")\n",
634
+ "print(f\"{dataset['validation']['text_label'][:10]=}\")"
635
+ ]
636
+ },
637
+ {
638
+ "cell_type": "code",
639
+ "execution_count": 9,
640
+ "id": "a8de6005",
641
+ "metadata": {
642
+ "ExecuteTime": {
643
+ "end_time": "2023-05-30T08:42:45.752765Z",
644
+ "start_time": "2023-05-30T08:42:45.742397Z"
645
+ }
646
+ },
647
+ "outputs": [],
648
+ "source": [
649
+ "# saving model\n",
650
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
651
+ "model.save_pretrained(peft_model_id)"
652
+ ]
653
+ },
654
+ {
655
+ "cell_type": "code",
656
+ "execution_count": 10,
657
+ "id": "bd20cd4c",
658
+ "metadata": {
659
+ "ExecuteTime": {
660
+ "end_time": "2023-05-30T08:42:47.660873Z",
661
+ "start_time": "2023-05-30T08:42:47.488293Z"
662
+ }
663
+ },
664
+ "outputs": [
665
+ {
666
+ "name": "stdout",
667
+ "output_type": "stream",
668
+ "text": [
669
+ "164K\tt5-large_PROMPT_TUNING_SEQ_2_SEQ_LM/adapter_model.bin\r\n"
670
+ ]
671
+ }
672
+ ],
673
+ "source": [
674
+ "ckpt = f\"{peft_model_id}/adapter_model.bin\"\n",
675
+ "!du -h $ckpt"
676
+ ]
677
+ },
678
+ {
679
+ "cell_type": "code",
680
+ "execution_count": 11,
681
+ "id": "76c2fc29",
682
+ "metadata": {
683
+ "ExecuteTime": {
684
+ "end_time": "2023-05-30T08:42:56.721990Z",
685
+ "start_time": "2023-05-30T08:42:49.060700Z"
686
+ }
687
+ },
688
+ "outputs": [],
689
+ "source": [
690
+ "from peft import PeftModel, PeftConfig\n",
691
+ "\n",
692
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
693
+ "\n",
694
+ "config = PeftConfig.from_pretrained(peft_model_id)\n",
695
+ "model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)\n",
696
+ "model = PeftModel.from_pretrained(model, peft_model_id)"
697
+ ]
698
+ },
699
+ {
700
+ "cell_type": "code",
701
+ "execution_count": 12,
702
+ "id": "d997f1cc",
703
+ "metadata": {
704
+ "ExecuteTime": {
705
+ "end_time": "2023-05-30T08:42:59.600916Z",
706
+ "start_time": "2023-05-30T08:42:58.961468Z"
707
+ }
708
+ },
709
+ "outputs": [
710
+ {
711
+ "name": "stdout",
712
+ "output_type": "stream",
713
+ "text": [
714
+ "Danske Bank is Denmark 's largest bank with 3.5 million customers .\n",
715
+ "tensor([[ 3039, 1050, 1925, 19, 18001, 3, 31, 7, 2015, 2137,\n",
716
+ " 28, 3, 9285, 770, 722, 3, 5, 1]])\n",
717
+ "tensor([[ 0, 7163, 1]])\n",
718
+ "['neutral']\n"
719
+ ]
720
+ }
721
+ ],
722
+ "source": [
723
+ "model.eval()\n",
724
+ "i = 107\n",
725
+ "input_ids = tokenizer(dataset[\"validation\"][text_column][i], return_tensors=\"pt\").input_ids\n",
726
+ "print(dataset[\"validation\"][text_column][i])\n",
727
+ "print(input_ids)\n",
728
+ "\n",
729
+ "with torch.no_grad():\n",
730
+ " outputs = model.generate(input_ids=input_ids, max_new_tokens=10)\n",
731
+ " print(outputs)\n",
732
+ " print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True))"
733
+ ]
734
+ }
735
+ ],
736
+ "metadata": {
737
+ "kernelspec": {
738
+ "display_name": "peft",
739
+ "language": "python",
740
+ "name": "peft"
741
+ },
742
+ "language_info": {
743
+ "codemirror_mode": {
744
+ "name": "ipython",
745
+ "version": 3
746
+ },
747
+ "file_extension": ".py",
748
+ "mimetype": "text/x-python",
749
+ "name": "python",
750
+ "nbconvert_exporter": "python",
751
+ "pygments_lexer": "ipython3",
752
+ "version": "3.9.16"
753
+ },
754
+ "toc": {
755
+ "base_numbering": 1,
756
+ "nav_menu": {},
757
+ "number_sections": true,
758
+ "sideBar": true,
759
+ "skip_h1_title": false,
760
+ "title_cell": "Table of Contents",
761
+ "title_sidebar": "Contents",
762
+ "toc_cell": false,
763
+ "toc_position": {},
764
+ "toc_section_display": true,
765
+ "toc_window_display": false
766
+ },
767
+ "varInspector": {
768
+ "cols": {
769
+ "lenName": 16,
770
+ "lenType": 16,
771
+ "lenVar": 40
772
+ },
773
+ "kernels_config": {
774
+ "python": {
775
+ "delete_cmd_postfix": "",
776
+ "delete_cmd_prefix": "del ",
777
+ "library": "var_list.py",
778
+ "varRefreshCmd": "print(var_dic_list())"
779
+ },
780
+ "r": {
781
+ "delete_cmd_postfix": ") ",
782
+ "delete_cmd_prefix": "rm(",
783
+ "library": "var_list.r",
784
+ "varRefreshCmd": "cat(var_dic_list()) "
785
+ }
786
+ },
787
+ "types_to_exclude": [
788
+ "module",
789
+ "function",
790
+ "builtin_function_or_method",
791
+ "instance",
792
+ "_Feature"
793
+ ],
794
+ "window_display": false
795
+ },
796
+ "vscode": {
797
+ "interpreter": {
798
+ "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
799
+ }
800
+ }
801
+ },
802
+ "nbformat": 4,
803
+ "nbformat_minor": 5
804
+ }
peft_prompt_tuning_seq2seq_with_generate.ipynb ADDED
@@ -0,0 +1,757 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "5f93b7d1",
7
+ "metadata": {
8
+ "ExecuteTime": {
9
+ "end_time": "2023-05-30T09:49:56.334329Z",
10
+ "start_time": "2023-05-30T09:49:54.494916Z"
11
+ }
12
+ },
13
+ "outputs": [
14
+ {
15
+ "ename": "KeyboardInterrupt",
16
+ "evalue": "",
17
+ "output_type": "error",
18
+ "traceback": [
19
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
20
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
21
+ "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtransformers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer, GenerationConfig\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpeft\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m get_peft_model, PromptTuningInit, PromptTuningConfig, TaskType\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n",
22
+ "File \u001b[0;32m<frozen importlib._bootstrap>:1055\u001b[0m, in \u001b[0;36m_handle_fromlist\u001b[0;34m(module, fromlist, import_, recursive)\u001b[0m\n",
23
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/utils/import_utils.py:1076\u001b[0m, in \u001b[0;36m_LazyModule.__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 1074\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_module(name)\n\u001b[1;32m 1075\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m name \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_class_to_module\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[0;32m-> 1076\u001b[0m module \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_module\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_class_to_module\u001b[49m\u001b[43m[\u001b[49m\u001b[43mname\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1077\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(module, name)\n\u001b[1;32m 1078\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
24
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/utils/import_utils.py:1086\u001b[0m, in \u001b[0;36m_LazyModule._get_module\u001b[0;34m(self, module_name)\u001b[0m\n\u001b[1;32m 1084\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get_module\u001b[39m(\u001b[38;5;28mself\u001b[39m, module_name: \u001b[38;5;28mstr\u001b[39m):\n\u001b[1;32m 1085\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1086\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mimportlib\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimport_module\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mmodule_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;18;43m__name__\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1087\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 1088\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[1;32m 1089\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFailed to import \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmodule_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m because of the following error (look up to see its\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1090\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m traceback):\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1091\u001b[0m ) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n",
25
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/importlib/__init__.py:127\u001b[0m, in \u001b[0;36mimport_module\u001b[0;34m(name, package)\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m 126\u001b[0m level \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m--> 127\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_bootstrap\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_gcd_import\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m[\u001b[49m\u001b[43mlevel\u001b[49m\u001b[43m:\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpackage\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlevel\u001b[49m\u001b[43m)\u001b[49m\n",
26
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/training_args_seq2seq.py:21\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtyping\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Optional, Union\n\u001b[1;32m 20\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mgeneration\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mconfiguration_utils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m GenerationConfig\n\u001b[0;32m---> 21\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtraining_args\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TrainingArguments\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m add_start_docstrings\n\u001b[1;32m 25\u001b[0m logger \u001b[38;5;241m=\u001b[39m logging\u001b[38;5;241m.\u001b[39mgetLogger(\u001b[38;5;18m__name__\u001b[39m)\n",
27
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/training_args.py:29\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtyping\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Any, Dict, List, Optional, Union\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpackaging\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m version\n\u001b[0;32m---> 29\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdebug_utils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DebugOption\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtrainer_utils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 31\u001b[0m EvaluationStrategy,\n\u001b[1;32m 32\u001b[0m FSDPOption,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 36\u001b[0m ShardedDDPOption,\n\u001b[1;32m 37\u001b[0m )\n\u001b[1;32m 38\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 39\u001b[0m ExplicitEnum,\n\u001b[1;32m 40\u001b[0m cached_property,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 53\u001b[0m requires_backends,\n\u001b[1;32m 54\u001b[0m )\n",
28
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/debug_utils.py:21\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ExplicitEnum, is_torch_available, logging\n\u001b[1;32m 20\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_torch_available():\n\u001b[0;32m---> 21\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[1;32m 24\u001b[0m logger \u001b[38;5;241m=\u001b[39m logging\u001b[38;5;241m.\u001b[39mget_logger(\u001b[38;5;18m__name__\u001b[39m)\n\u001b[1;32m 27\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mDebugUnderflowOverflow\u001b[39;00m:\n",
29
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/__init__.py:1465\u001b[0m\n\u001b[1;32m 1463\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m library\n\u001b[1;32m 1464\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m TYPE_CHECKING:\n\u001b[0;32m-> 1465\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m _meta_registrations\n\u001b[1;32m 1467\u001b[0m \u001b[38;5;66;03m# Enable CUDA Sanitizer\u001b[39;00m\n\u001b[1;32m 1468\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTORCH_CUDA_SANITIZER\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m os\u001b[38;5;241m.\u001b[39menviron:\n",
30
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/_meta_registrations.py:7\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_prims_common\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mutils\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Tensor\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_decomp\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m _add_op_to_registry, global_decomposition_table, meta_table\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_ops\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m OpOverload\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_prims\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m _elementwise_meta, ELEMENTWISE_PRIM_TYPE_PROMOTION_KIND\n",
31
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/_decomp/__init__.py:169\u001b[0m\n\u001b[1;32m 165\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m decompositions\n\u001b[1;32m 168\u001b[0m \u001b[38;5;66;03m# populate the table\u001b[39;00m\n\u001b[0;32m--> 169\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_decomp\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdecompositions\u001b[39;00m\n\u001b[1;32m 170\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_refs\u001b[39;00m\n\u001b[1;32m 172\u001b[0m \u001b[38;5;66;03m# This list was copied from torch/_inductor/decomposition.py\u001b[39;00m\n\u001b[1;32m 173\u001b[0m \u001b[38;5;66;03m# excluding decompositions that results in prim ops\u001b[39;00m\n\u001b[1;32m 174\u001b[0m \u001b[38;5;66;03m# Resulting opset of decomposition is core aten ops\u001b[39;00m\n",
32
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/_decomp/decompositions.py:10\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtyping\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Callable, cast, Iterable, List, Optional, Tuple, Union\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m---> 10\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_prims\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mprims\u001b[39;00m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_prims_common\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mutils\u001b[39;00m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mnn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctional\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mF\u001b[39;00m\n",
33
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/_prims/__init__.py:33\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_prims_common\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 18\u001b[0m check,\n\u001b[1;32m 19\u001b[0m Dim,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 30\u001b[0m type_to_dtype,\n\u001b[1;32m 31\u001b[0m )\n\u001b[1;32m 32\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_prims_common\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mwrappers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m backwards_not_supported\n\u001b[0;32m---> 33\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_subclasses\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfake_tensor\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m FakeTensor, FakeTensorMode\n\u001b[1;32m 34\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01moverrides\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m handle_torch_function, has_torch_function\n\u001b[1;32m 35\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_pytree\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m tree_flatten, tree_map, tree_unflatten\n",
34
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/_subclasses/__init__.py:3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_subclasses\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfake_tensor\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 4\u001b[0m DynamicOutputShapeException,\n\u001b[1;32m 5\u001b[0m FakeTensor,\n\u001b[1;32m 6\u001b[0m FakeTensorMode,\n\u001b[1;32m 7\u001b[0m UnsupportedFakeTensorException,\n\u001b[1;32m 8\u001b[0m )\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_subclasses\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfake_utils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m CrossRefFakeMode\n\u001b[1;32m 12\u001b[0m __all__ \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m 13\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFakeTensor\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 14\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFakeTensorMode\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCrossRefFakeMode\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 18\u001b[0m ]\n",
35
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/_subclasses/fake_tensor.py:13\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mweakref\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ReferenceType\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m---> 13\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_guards\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Source\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_ops\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m OpOverload\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_prims_common\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 16\u001b[0m elementwise_dtypes,\n\u001b[1;32m 17\u001b[0m ELEMENTWISE_TYPE_PROMOTION_KIND,\n\u001b[1;32m 18\u001b[0m is_float_dtype,\n\u001b[1;32m 19\u001b[0m is_integer_dtype,\n\u001b[1;32m 20\u001b[0m )\n",
36
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/torch/_guards.py:14\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# TODO(voz): Stolen pattern, not sure why this is the case,\u001b[39;00m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;66;03m# but mypy complains.\u001b[39;00m\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 14\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m \u001b[38;5;66;03m# type: ignore[import]\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[1;32m 16\u001b[0m log\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo sympy found\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
37
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/__init__.py:74\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlogic\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (to_cnf, to_dnf, to_nnf, And, Or, Not, Xor, Nand, Nor,\n\u001b[1;32m 68\u001b[0m Implies, Equivalent, ITE, POSform, SOPform, simplify_logic, bool_map,\n\u001b[1;32m 69\u001b[0m true, false, satisfiable)\n\u001b[1;32m 71\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01massumptions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (AppliedPredicate, Predicate, AssumptionsContext,\n\u001b[1;32m 72\u001b[0m assuming, Q, ask, register_handler, remove_handler, refine)\n\u001b[0;32m---> 74\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (Poly, PurePoly, poly_from_expr, parallel_poly_from_expr,\n\u001b[1;32m 75\u001b[0m degree, total_degree, degree_list, LC, LM, LT, pdiv, prem, pquo,\n\u001b[1;32m 76\u001b[0m pexquo, div, rem, quo, exquo, half_gcdex, gcdex, invert,\n\u001b[1;32m 77\u001b[0m subresultants, resultant, discriminant, cofactors, gcd_list, gcd,\n\u001b[1;32m 78\u001b[0m lcm_list, lcm, terms_gcd, trunc, monic, content, primitive, compose,\n\u001b[1;32m 79\u001b[0m decompose, sturm, gff_list, gff, sqf_norm, sqf_part, sqf_list, sqf,\n\u001b[1;32m 80\u001b[0m factor_list, factor, intervals, refine_root, count_roots, real_roots,\n\u001b[1;32m 81\u001b[0m nroots, ground_roots, nth_power_roots_poly, cancel, reduced, groebner,\n\u001b[1;32m 82\u001b[0m is_zero_dimensional, GroebnerBasis, poly, symmetrize, horner,\n\u001b[1;32m 83\u001b[0m interpolate, rational_interpolate, viete, together,\n\u001b[1;32m 84\u001b[0m BasePolynomialError, ExactQuotientFailed, PolynomialDivisionFailed,\n\u001b[1;32m 85\u001b[0m OperationNotSupported, HeuristicGCDFailed, HomomorphismFailed,\n\u001b[1;32m 86\u001b[0m IsomorphismFailed, ExtraneousFactors, EvaluationFailed,\n\u001b[1;32m 87\u001b[0m RefinementFailed, CoercionFailed, NotInvertible, NotReversible,\n\u001b[1;32m 88\u001b[0m NotAlgebraic, DomainError, PolynomialError, UnificationFailed,\n\u001b[1;32m 89\u001b[0m GeneratorsError, GeneratorsNeeded, ComputationFailed,\n\u001b[1;32m 90\u001b[0m UnivariatePolynomialError, MultivariatePolynomialError,\n\u001b[1;32m 91\u001b[0m PolificationFailed, OptionError, FlagError, minpoly,\n\u001b[1;32m 92\u001b[0m minimal_polynomial, primitive_element, field_isomorphism,\n\u001b[1;32m 93\u001b[0m to_number_field, isolate, round_two, prime_decomp, prime_valuation,\n\u001b[1;32m 94\u001b[0m galois_group, itermonomials, Monomial, lex, grlex,\n\u001b[1;32m 95\u001b[0m grevlex, ilex, igrlex, igrevlex, CRootOf, rootof, RootOf,\n\u001b[1;32m 96\u001b[0m ComplexRootOf, RootSum, roots, Domain, FiniteField, IntegerRing,\n\u001b[1;32m 97\u001b[0m RationalField, RealField, ComplexField, PythonFiniteField,\n\u001b[1;32m 98\u001b[0m GMPYFiniteField, PythonIntegerRing, GMPYIntegerRing, PythonRational,\n\u001b[1;32m 99\u001b[0m GMPYRationalField, AlgebraicField, PolynomialRing, FractionField,\n\u001b[1;32m 100\u001b[0m ExpressionDomain, FF_python, FF_gmpy, ZZ_python, ZZ_gmpy, QQ_python,\n\u001b[1;32m 101\u001b[0m QQ_gmpy, GF, FF, ZZ, QQ, ZZ_I, QQ_I, RR, CC, EX, EXRAW,\n\u001b[1;32m 102\u001b[0m construct_domain, swinnerton_dyer_poly, cyclotomic_poly,\n\u001b[1;32m 103\u001b[0m symmetric_poly, random_poly, interpolating_poly, jacobi_poly,\n\u001b[1;32m 104\u001b[0m chebyshevt_poly, chebyshevu_poly, hermite_poly, hermite_prob_poly,\n\u001b[1;32m 105\u001b[0m legendre_poly, laguerre_poly, apart, apart_list, assemble_partfrac_list,\n\u001b[1;32m 106\u001b[0m Options, ring, xring, vring, sring, field, xfield, vfield, sfield)\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mseries\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (Order, O, limit, Limit, gruntz, series, approximants,\n\u001b[1;32m 109\u001b[0m residue, EmptySequence, SeqPer, SeqFormula, sequence, SeqAdd, SeqMul,\n\u001b[1;32m 110\u001b[0m fourier_series, fps, difference_delta, limit_seq)\n\u001b[1;32m 112\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (factorial, factorial2, rf, ff, binomial,\n\u001b[1;32m 113\u001b[0m RisingFactorial, FallingFactorial, subfactorial, carmichael,\n\u001b[1;32m 114\u001b[0m fibonacci, lucas, motzkin, tribonacci, harmonic, bernoulli, bell, euler,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 133\u001b[0m Znm, elliptic_k, elliptic_f, elliptic_e, elliptic_pi, beta, mathieus,\n\u001b[1;32m 134\u001b[0m mathieuc, mathieusprime, mathieucprime, riemann_xi, betainc, betainc_regularized)\n",
38
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/polys/__init__.py:78\u001b[0m\n\u001b[1;32m 3\u001b[0m __all__ \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m 4\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPoly\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPurePoly\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpoly_from_expr\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mparallel_poly_from_expr\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdegree\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 5\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtotal_degree\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdegree_list\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mLC\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mLM\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mLT\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpdiv\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mprem\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpquo\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfield\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mxfield\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvfield\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msfield\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 66\u001b[0m ]\n\u001b[1;32m 68\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolytools\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (Poly, PurePoly, poly_from_expr,\n\u001b[1;32m 69\u001b[0m parallel_poly_from_expr, degree, total_degree, degree_list, LC, LM,\n\u001b[1;32m 70\u001b[0m LT, pdiv, prem, pquo, pexquo, div, rem, quo, exquo, half_gcdex, gcdex,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 75\u001b[0m count_roots, real_roots, nroots, ground_roots, nth_power_roots_poly,\n\u001b[1;32m 76\u001b[0m cancel, reduced, groebner, is_zero_dimensional, GroebnerBasis, poly)\n\u001b[0;32m---> 78\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolyfuncs\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (symmetrize, horner, interpolate,\n\u001b[1;32m 79\u001b[0m rational_interpolate, viete)\n\u001b[1;32m 81\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mrationaltools\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m together\n\u001b[1;32m 83\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolyerrors\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (BasePolynomialError, ExactQuotientFailed,\n\u001b[1;32m 84\u001b[0m PolynomialDivisionFailed, OperationNotSupported, HeuristicGCDFailed,\n\u001b[1;32m 85\u001b[0m HomomorphismFailed, IsomorphismFailed, ExtraneousFactors,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 90\u001b[0m MultivariatePolynomialError, PolificationFailed, OptionError,\n\u001b[1;32m 91\u001b[0m FlagError)\n",
39
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/polys/polyfuncs.py:10\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolyoptions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m allowed_flags, build_options\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolytools\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m poly_from_expr, Poly\n\u001b[0;32m---> 10\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspecialpolys\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m 11\u001b[0m symmetric_poly, interpolating_poly)\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mrings\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m sring\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m numbered_symbols, take, public\n",
40
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/polys/specialpolys.py:298\u001b[0m\n\u001b[1;32m 294\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m dmp_mul(f, h, n, K), dmp_mul(g, h, n, K), h\n\u001b[1;32m 296\u001b[0m \u001b[38;5;66;03m# A few useful polynomials from Wang's paper ('78).\u001b[39;00m\n\u001b[0;32m--> 298\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mrings\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ring\n\u001b[1;32m 300\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_f_0\u001b[39m():\n\u001b[1;32m 301\u001b[0m R, x, y, z \u001b[38;5;241m=\u001b[39m ring(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx,y,z\u001b[39m\u001b[38;5;124m\"\u001b[39m, ZZ)\n",
41
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/polys/rings.py:30\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolyoptions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (Domain \u001b[38;5;28;01mas\u001b[39;00m DomainOpt,\n\u001b[1;32m 27\u001b[0m Order \u001b[38;5;28;01mas\u001b[39;00m OrderOpt, build_options)\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolys\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpolyutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (expr_from_dict, _dict_reorder,\n\u001b[1;32m 29\u001b[0m _parallel_dict_from_expr)\n\u001b[0;32m---> 30\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mprinting\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdefaults\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DefaultPrinting\n\u001b[1;32m 31\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m public, subsets\n\u001b[1;32m 32\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01miterables\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m is_sequence\n",
42
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/printing/__init__.py:5\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;124;03m\"\"\"Printing subsystem\"\"\"\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpretty\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m pager_print, pretty, pretty_print, pprint, pprint_use_unicode, pprint_try_use_unicode\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlatex\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m latex, print_latex, multiline_latex\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmathml\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m mathml, print_mathml\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpython\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m python, print_python\n",
43
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/printing/latex.py:18\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msympify\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m SympifyError\n\u001b[1;32m 17\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlogic\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mboolalg\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m true, BooleanTrue, BooleanFalse\n\u001b[0;32m---> 18\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtensor\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01marray\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m NDimArray\n\u001b[1;32m 20\u001b[0m \u001b[38;5;66;03m# sympy.printing imports\u001b[39;00m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mprinting\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mprecedence\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m precedence_traditional\n",
44
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/tensor/__init__.py:4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;124;03m\"\"\"A module to manipulate symbolic objects with indices including tensors\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \n\u001b[1;32m 3\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mindexed\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m IndexedBase, Idx, Indexed\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mindex_methods\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m get_contraction_structure, get_indices\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m shape\n",
45
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/tensor/indexed.py:114\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlogic\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m fuzzy_bool, fuzzy_not\n\u001b[1;32m 113\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msympify\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m _sympify\n\u001b[0;32m--> 114\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspecial\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtensor_functions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m KroneckerDelta\n\u001b[1;32m 115\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmultipledispatch\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m dispatch\n\u001b[1;32m 116\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01miterables\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m is_sequence, NotIterable\n",
46
+ "File \u001b[0;32m~/anaconda3/envs/peft/lib/python3.9/site-packages/sympy/functions/__init__.py:21\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01melementary\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtrigonometric\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (sin, cos, tan,\n\u001b[1;32m 18\u001b[0m sec, csc, cot, sinc, asin, acos, atan, asec, acsc, acot, atan2)\n\u001b[1;32m 19\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01melementary\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexponential\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (exp_polar, exp, log,\n\u001b[1;32m 20\u001b[0m LambertW)\n\u001b[0;32m---> 21\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01melementary\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mhyperbolic\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (sinh, cosh, tanh, coth,\n\u001b[1;32m 22\u001b[0m sech, csch, asinh, acosh, atanh, acoth, asech, acsch)\n\u001b[1;32m 23\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01melementary\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mintegers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m floor, ceiling, frac\n\u001b[1;32m 24\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfunctions\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01melementary\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpiecewise\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (Piecewise, piecewise_fold,\n\u001b[1;32m 25\u001b[0m piecewise_exclusive)\n",
47
+ "File \u001b[0;32m<frozen importlib._bootstrap>:1007\u001b[0m, in \u001b[0;36m_find_and_load\u001b[0;34m(name, import_)\u001b[0m\n",
48
+ "File \u001b[0;32m<frozen importlib._bootstrap>:986\u001b[0m, in \u001b[0;36m_find_and_load_unlocked\u001b[0;34m(name, import_)\u001b[0m\n",
49
+ "File \u001b[0;32m<frozen importlib._bootstrap>:680\u001b[0m, in \u001b[0;36m_load_unlocked\u001b[0;34m(spec)\u001b[0m\n",
50
+ "File \u001b[0;32m<frozen importlib._bootstrap_external>:846\u001b[0m, in \u001b[0;36mexec_module\u001b[0;34m(self, module)\u001b[0m\n",
51
+ "File \u001b[0;32m<frozen importlib._bootstrap_external>:978\u001b[0m, in \u001b[0;36mget_code\u001b[0;34m(self, fullname)\u001b[0m\n",
52
+ "File \u001b[0;32m<frozen importlib._bootstrap_external>:647\u001b[0m, in \u001b[0;36m_compile_bytecode\u001b[0;34m(data, name, bytecode_path, source_path)\u001b[0m\n",
53
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
54
+ ]
55
+ }
56
+ ],
57
+ "source": [
58
+ "import os\n",
59
+ "\n",
60
+ "import torch\n",
61
+ "from transformers import (\n",
62
+ " AutoTokenizer,\n",
63
+ " default_data_collator,\n",
64
+ " AutoModelForSeq2SeqLM,\n",
65
+ " Seq2SeqTrainingArguments,\n",
66
+ " Seq2SeqTrainer,\n",
67
+ " GenerationConfig,\n",
68
+ ")\n",
69
+ "from peft import get_peft_model, PromptTuningInit, PromptTuningConfig, TaskType\n",
70
+ "from datasets import load_dataset\n",
71
+ "\n",
72
+ "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
73
+ "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
74
+ "\n",
75
+ "device = \"cuda\"\n",
76
+ "model_name_or_path = \"t5-large\"\n",
77
+ "tokenizer_name_or_path = \"t5-large\"\n",
78
+ "\n",
79
+ "checkpoint_name = \"financial_sentiment_analysis_prefix_tuning_v1.pt\"\n",
80
+ "text_column = \"sentence\"\n",
81
+ "label_column = \"text_label\"\n",
82
+ "max_length = 8\n",
83
+ "lr = 1e0\n",
84
+ "num_epochs = 5\n",
85
+ "batch_size = 8"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": 2,
91
+ "id": "8d0850ac",
92
+ "metadata": {
93
+ "ExecuteTime": {
94
+ "end_time": "2023-05-30T09:50:04.808527Z",
95
+ "start_time": "2023-05-30T09:49:56.953075Z"
96
+ }
97
+ },
98
+ "outputs": [
99
+ {
100
+ "name": "stdout",
101
+ "output_type": "stream",
102
+ "text": [
103
+ "trainable params: 40960 || all params: 737709056 || trainable%: 0.005552324411210698\n"
104
+ ]
105
+ },
106
+ {
107
+ "data": {
108
+ "text/plain": [
109
+ "PeftModelForSeq2SeqLM(\n",
110
+ " (base_model): T5ForConditionalGeneration(\n",
111
+ " (shared): Embedding(32128, 1024)\n",
112
+ " (encoder): T5Stack(\n",
113
+ " (embed_tokens): Embedding(32128, 1024)\n",
114
+ " (block): ModuleList(\n",
115
+ " (0): T5Block(\n",
116
+ " (layer): ModuleList(\n",
117
+ " (0): T5LayerSelfAttention(\n",
118
+ " (SelfAttention): T5Attention(\n",
119
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
120
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
121
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
122
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
123
+ " (relative_attention_bias): Embedding(32, 16)\n",
124
+ " )\n",
125
+ " (layer_norm): T5LayerNorm()\n",
126
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
127
+ " )\n",
128
+ " (1): T5LayerFF(\n",
129
+ " (DenseReluDense): T5DenseActDense(\n",
130
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
131
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
132
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
133
+ " (act): ReLU()\n",
134
+ " )\n",
135
+ " (layer_norm): T5LayerNorm()\n",
136
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
137
+ " )\n",
138
+ " )\n",
139
+ " )\n",
140
+ " (1-23): 23 x T5Block(\n",
141
+ " (layer): ModuleList(\n",
142
+ " (0): T5LayerSelfAttention(\n",
143
+ " (SelfAttention): T5Attention(\n",
144
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
145
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
146
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
147
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
148
+ " )\n",
149
+ " (layer_norm): T5LayerNorm()\n",
150
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
151
+ " )\n",
152
+ " (1): T5LayerFF(\n",
153
+ " (DenseReluDense): T5DenseActDense(\n",
154
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
155
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
156
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
157
+ " (act): ReLU()\n",
158
+ " )\n",
159
+ " (layer_norm): T5LayerNorm()\n",
160
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
161
+ " )\n",
162
+ " )\n",
163
+ " )\n",
164
+ " )\n",
165
+ " (final_layer_norm): T5LayerNorm()\n",
166
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
167
+ " )\n",
168
+ " (decoder): T5Stack(\n",
169
+ " (embed_tokens): Embedding(32128, 1024)\n",
170
+ " (block): ModuleList(\n",
171
+ " (0): T5Block(\n",
172
+ " (layer): ModuleList(\n",
173
+ " (0): T5LayerSelfAttention(\n",
174
+ " (SelfAttention): T5Attention(\n",
175
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
176
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
177
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
178
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
179
+ " (relative_attention_bias): Embedding(32, 16)\n",
180
+ " )\n",
181
+ " (layer_norm): T5LayerNorm()\n",
182
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
183
+ " )\n",
184
+ " (1): T5LayerCrossAttention(\n",
185
+ " (EncDecAttention): T5Attention(\n",
186
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
187
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
188
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
189
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
190
+ " )\n",
191
+ " (layer_norm): T5LayerNorm()\n",
192
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
193
+ " )\n",
194
+ " (2): T5LayerFF(\n",
195
+ " (DenseReluDense): T5DenseActDense(\n",
196
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
197
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
198
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
199
+ " (act): ReLU()\n",
200
+ " )\n",
201
+ " (layer_norm): T5LayerNorm()\n",
202
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
203
+ " )\n",
204
+ " )\n",
205
+ " )\n",
206
+ " (1-23): 23 x T5Block(\n",
207
+ " (layer): ModuleList(\n",
208
+ " (0): T5LayerSelfAttention(\n",
209
+ " (SelfAttention): T5Attention(\n",
210
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
211
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
212
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
213
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
214
+ " )\n",
215
+ " (layer_norm): T5LayerNorm()\n",
216
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
217
+ " )\n",
218
+ " (1): T5LayerCrossAttention(\n",
219
+ " (EncDecAttention): T5Attention(\n",
220
+ " (q): Linear(in_features=1024, out_features=1024, bias=False)\n",
221
+ " (k): Linear(in_features=1024, out_features=1024, bias=False)\n",
222
+ " (v): Linear(in_features=1024, out_features=1024, bias=False)\n",
223
+ " (o): Linear(in_features=1024, out_features=1024, bias=False)\n",
224
+ " )\n",
225
+ " (layer_norm): T5LayerNorm()\n",
226
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
227
+ " )\n",
228
+ " (2): T5LayerFF(\n",
229
+ " (DenseReluDense): T5DenseActDense(\n",
230
+ " (wi): Linear(in_features=1024, out_features=4096, bias=False)\n",
231
+ " (wo): Linear(in_features=4096, out_features=1024, bias=False)\n",
232
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
233
+ " (act): ReLU()\n",
234
+ " )\n",
235
+ " (layer_norm): T5LayerNorm()\n",
236
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
237
+ " )\n",
238
+ " )\n",
239
+ " )\n",
240
+ " )\n",
241
+ " (final_layer_norm): T5LayerNorm()\n",
242
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
243
+ " )\n",
244
+ " (lm_head): Linear(in_features=1024, out_features=32128, bias=False)\n",
245
+ " )\n",
246
+ " (prompt_encoder): ModuleDict(\n",
247
+ " (default): PromptEmbedding(\n",
248
+ " (embedding): Embedding(40, 1024)\n",
249
+ " )\n",
250
+ " )\n",
251
+ " (word_embeddings): Embedding(32128, 1024)\n",
252
+ ")"
253
+ ]
254
+ },
255
+ "execution_count": 2,
256
+ "metadata": {},
257
+ "output_type": "execute_result"
258
+ }
259
+ ],
260
+ "source": [
261
+ "# creating model\n",
262
+ "peft_config = peft_config = PromptTuningConfig(\n",
263
+ " task_type=TaskType.SEQ_2_SEQ_LM,\n",
264
+ " prompt_tuning_init=PromptTuningInit.TEXT,\n",
265
+ " num_virtual_tokens=20,\n",
266
+ " prompt_tuning_init_text=\"What is the sentiment of this article?\\n\",\n",
267
+ " inference_mode=False,\n",
268
+ " tokenizer_name_or_path=model_name_or_path,\n",
269
+ ")\n",
270
+ "\n",
271
+ "model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)\n",
272
+ "model = get_peft_model(model, peft_config)\n",
273
+ "model.print_trainable_parameters()\n",
274
+ "model"
275
+ ]
276
+ },
277
+ {
278
+ "cell_type": "code",
279
+ "execution_count": 3,
280
+ "id": "4ee2babf",
281
+ "metadata": {
282
+ "ExecuteTime": {
283
+ "end_time": "2023-05-30T09:50:09.224782Z",
284
+ "start_time": "2023-05-30T09:50:08.172611Z"
285
+ }
286
+ },
287
+ "outputs": [
288
+ {
289
+ "name": "stderr",
290
+ "output_type": "stream",
291
+ "text": [
292
+ "Found cached dataset financial_phrasebank (/data/proxem/huggingface/datasets/financial_phrasebank/sentences_allagree/1.0.0/550bde12e6c30e2674da973a55f57edde5181d53f5a5a34c1531c53f93b7e141)\n"
293
+ ]
294
+ },
295
+ {
296
+ "data": {
297
+ "application/vnd.jupyter.widget-view+json": {
298
+ "model_id": "d3a799c64a2c43258dc6166c90e2e49f",
299
+ "version_major": 2,
300
+ "version_minor": 0
301
+ },
302
+ "text/plain": [
303
+ " 0%| | 0/1 [00:00<?, ?it/s]"
304
+ ]
305
+ },
306
+ "metadata": {},
307
+ "output_type": "display_data"
308
+ },
309
+ {
310
+ "data": {
311
+ "application/vnd.jupyter.widget-view+json": {
312
+ "model_id": "",
313
+ "version_major": 2,
314
+ "version_minor": 0
315
+ },
316
+ "text/plain": [
317
+ "Map: 0%| | 0/2037 [00:00<?, ? examples/s]"
318
+ ]
319
+ },
320
+ "metadata": {},
321
+ "output_type": "display_data"
322
+ },
323
+ {
324
+ "data": {
325
+ "application/vnd.jupyter.widget-view+json": {
326
+ "model_id": "",
327
+ "version_major": 2,
328
+ "version_minor": 0
329
+ },
330
+ "text/plain": [
331
+ "Map: 0%| | 0/227 [00:00<?, ? examples/s]"
332
+ ]
333
+ },
334
+ "metadata": {},
335
+ "output_type": "display_data"
336
+ },
337
+ {
338
+ "data": {
339
+ "text/plain": [
340
+ "{'sentence': 'The price of the 10,000 kroon par value bonds was 9663,51 kroons in the primary issue .',\n",
341
+ " 'label': 1,\n",
342
+ " 'text_label': 'neutral'}"
343
+ ]
344
+ },
345
+ "execution_count": 3,
346
+ "metadata": {},
347
+ "output_type": "execute_result"
348
+ }
349
+ ],
350
+ "source": [
351
+ "# loading dataset\n",
352
+ "dataset = load_dataset(\"financial_phrasebank\", \"sentences_allagree\")\n",
353
+ "dataset = dataset[\"train\"].train_test_split(test_size=0.1)\n",
354
+ "dataset[\"validation\"] = dataset[\"test\"]\n",
355
+ "del dataset[\"test\"]\n",
356
+ "\n",
357
+ "classes = dataset[\"train\"].features[\"label\"].names\n",
358
+ "dataset = dataset.map(\n",
359
+ " lambda x: {\"text_label\": [classes[label] for label in x[\"label\"]]},\n",
360
+ " batched=True,\n",
361
+ " num_proc=1,\n",
362
+ ")\n",
363
+ "\n",
364
+ "dataset[\"train\"][0]"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": 4,
370
+ "id": "adf9608c",
371
+ "metadata": {
372
+ "ExecuteTime": {
373
+ "end_time": "2023-05-30T09:50:12.176663Z",
374
+ "start_time": "2023-05-30T09:50:11.421273Z"
375
+ }
376
+ },
377
+ "outputs": [
378
+ {
379
+ "name": "stderr",
380
+ "output_type": "stream",
381
+ "text": [
382
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/models/t5/tokenization_t5_fast.py:155: FutureWarning: This tokenizer was incorrectly instantiated with a model max length of 512 which will be corrected in Transformers v5.\n",
383
+ "For now, this behavior is kept to avoid breaking backwards compatibility when padding/encoding with `truncation is True`.\n",
384
+ "- Be aware that you SHOULD NOT rely on t5-large automatically truncating your input to 512 when padding/encoding.\n",
385
+ "- If you want to encode/pad to sequences longer than 512 you can either instantiate this tokenizer with `model_max_length` or pass `max_length` when encoding/padding.\n",
386
+ "- To avoid this warning, please instantiate this tokenizer with `model_max_length` set to your preferred value.\n",
387
+ " warnings.warn(\n"
388
+ ]
389
+ },
390
+ {
391
+ "data": {
392
+ "application/vnd.jupyter.widget-view+json": {
393
+ "model_id": "",
394
+ "version_major": 2,
395
+ "version_minor": 0
396
+ },
397
+ "text/plain": [
398
+ "Running tokenizer on dataset: 0%| | 0/2037 [00:00<?, ? examples/s]"
399
+ ]
400
+ },
401
+ "metadata": {},
402
+ "output_type": "display_data"
403
+ },
404
+ {
405
+ "data": {
406
+ "application/vnd.jupyter.widget-view+json": {
407
+ "model_id": "",
408
+ "version_major": 2,
409
+ "version_minor": 0
410
+ },
411
+ "text/plain": [
412
+ "Running tokenizer on dataset: 0%| | 0/227 [00:00<?, ? examples/s]"
413
+ ]
414
+ },
415
+ "metadata": {},
416
+ "output_type": "display_data"
417
+ }
418
+ ],
419
+ "source": [
420
+ "# data preprocessing\n",
421
+ "tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)\n",
422
+ "\n",
423
+ "\n",
424
+ "def preprocess_function(examples):\n",
425
+ " inputs = examples[text_column]\n",
426
+ " targets = examples[label_column]\n",
427
+ " model_inputs = tokenizer(inputs, max_length=max_length, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
428
+ " labels = tokenizer(targets, max_length=2, padding=\"max_length\", truncation=True, return_tensors=\"pt\")\n",
429
+ " labels = labels[\"input_ids\"]\n",
430
+ " labels[labels == tokenizer.pad_token_id] = -100\n",
431
+ " model_inputs[\"labels\"] = labels\n",
432
+ " return model_inputs\n",
433
+ "\n",
434
+ "\n",
435
+ "processed_datasets = dataset.map(\n",
436
+ " preprocess_function,\n",
437
+ " batched=True,\n",
438
+ " num_proc=1,\n",
439
+ " remove_columns=dataset[\"train\"].column_names,\n",
440
+ " load_from_cache_file=False,\n",
441
+ " desc=\"Running tokenizer on dataset\",\n",
442
+ ")\n",
443
+ "\n",
444
+ "train_dataset = processed_datasets[\"train\"].shuffle()\n",
445
+ "eval_dataset = processed_datasets[\"validation\"]"
446
+ ]
447
+ },
448
+ {
449
+ "cell_type": "code",
450
+ "execution_count": 5,
451
+ "id": "6b3a4090",
452
+ "metadata": {
453
+ "ExecuteTime": {
454
+ "end_time": "2023-05-30T09:53:10.336984Z",
455
+ "start_time": "2023-05-30T09:50:14.780995Z"
456
+ }
457
+ },
458
+ "outputs": [
459
+ {
460
+ "name": "stderr",
461
+ "output_type": "stream",
462
+ "text": [
463
+ "/udir/tschilla/anaconda3/envs/peft/lib/python3.9/site-packages/transformers/optimization.py:407: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning\n",
464
+ " warnings.warn(\n"
465
+ ]
466
+ },
467
+ {
468
+ "data": {
469
+ "text/html": [
470
+ "\n",
471
+ " <div>\n",
472
+ " \n",
473
+ " <progress value='1275' max='1275' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
474
+ " [1275/1275 02:52, Epoch 5/5]\n",
475
+ " </div>\n",
476
+ " <table border=\"1\" class=\"dataframe\">\n",
477
+ " <thead>\n",
478
+ " <tr style=\"text-align: left;\">\n",
479
+ " <th>Epoch</th>\n",
480
+ " <th>Training Loss</th>\n",
481
+ " <th>Validation Loss</th>\n",
482
+ " <th>Accuracy</th>\n",
483
+ " </tr>\n",
484
+ " </thead>\n",
485
+ " <tbody>\n",
486
+ " <tr>\n",
487
+ " <td>1</td>\n",
488
+ " <td>4.784800</td>\n",
489
+ " <td>0.576933</td>\n",
490
+ " <td>0.559471</td>\n",
491
+ " </tr>\n",
492
+ " <tr>\n",
493
+ " <td>2</td>\n",
494
+ " <td>0.648200</td>\n",
495
+ " <td>0.437575</td>\n",
496
+ " <td>0.577093</td>\n",
497
+ " </tr>\n",
498
+ " <tr>\n",
499
+ " <td>3</td>\n",
500
+ " <td>0.536200</td>\n",
501
+ " <td>0.397857</td>\n",
502
+ " <td>0.625551</td>\n",
503
+ " </tr>\n",
504
+ " <tr>\n",
505
+ " <td>4</td>\n",
506
+ " <td>0.472200</td>\n",
507
+ " <td>0.373160</td>\n",
508
+ " <td>0.643172</td>\n",
509
+ " </tr>\n",
510
+ " <tr>\n",
511
+ " <td>5</td>\n",
512
+ " <td>0.452500</td>\n",
513
+ " <td>0.370234</td>\n",
514
+ " <td>0.656388</td>\n",
515
+ " </tr>\n",
516
+ " </tbody>\n",
517
+ "</table><p>"
518
+ ],
519
+ "text/plain": [
520
+ "<IPython.core.display.HTML object>"
521
+ ]
522
+ },
523
+ "metadata": {},
524
+ "output_type": "display_data"
525
+ },
526
+ {
527
+ "data": {
528
+ "text/plain": [
529
+ "TrainOutput(global_step=1275, training_loss=1.3787811279296875, metrics={'train_runtime': 173.3699, 'train_samples_per_second': 58.747, 'train_steps_per_second': 7.354, 'total_flos': 344546979840000.0, 'train_loss': 1.3787811279296875, 'epoch': 5.0})"
530
+ ]
531
+ },
532
+ "execution_count": 5,
533
+ "metadata": {},
534
+ "output_type": "execute_result"
535
+ }
536
+ ],
537
+ "source": [
538
+ "# training and evaluation\n",
539
+ "\n",
540
+ "\n",
541
+ "def compute_metrics(eval_preds):\n",
542
+ " preds, labels = eval_preds\n",
543
+ " preds = tokenizer.batch_decode(preds, skip_special_tokens=True)\n",
544
+ " labels = tokenizer.batch_decode(labels, skip_special_tokens=True)\n",
545
+ "\n",
546
+ " correct = 0\n",
547
+ " total = 0\n",
548
+ " for pred, true in zip(preds, labels):\n",
549
+ " if pred.strip() == true.strip():\n",
550
+ " correct += 1\n",
551
+ " total += 1\n",
552
+ " accuracy = correct / total\n",
553
+ " return {\"accuracy\": accuracy}\n",
554
+ "\n",
555
+ "\n",
556
+ "training_args = Seq2SeqTrainingArguments(\n",
557
+ " \"out\",\n",
558
+ " per_device_train_batch_size=batch_size,\n",
559
+ " learning_rate=lr,\n",
560
+ " num_train_epochs=num_epochs,\n",
561
+ " evaluation_strategy=\"epoch\",\n",
562
+ " logging_strategy=\"epoch\",\n",
563
+ " save_strategy=\"no\",\n",
564
+ " report_to=[],\n",
565
+ " predict_with_generate=True,\n",
566
+ " generation_config=GenerationConfig(max_length=max_length),\n",
567
+ ")\n",
568
+ "trainer = Seq2SeqTrainer(\n",
569
+ " model=model,\n",
570
+ " tokenizer=tokenizer,\n",
571
+ " args=training_args,\n",
572
+ " train_dataset=train_dataset,\n",
573
+ " eval_dataset=eval_dataset,\n",
574
+ " data_collator=default_data_collator,\n",
575
+ " compute_metrics=compute_metrics,\n",
576
+ ")\n",
577
+ "trainer.train()"
578
+ ]
579
+ },
580
+ {
581
+ "cell_type": "code",
582
+ "execution_count": 6,
583
+ "id": "a8de6005",
584
+ "metadata": {
585
+ "ExecuteTime": {
586
+ "end_time": "2023-05-30T09:53:13.045146Z",
587
+ "start_time": "2023-05-30T09:53:13.035612Z"
588
+ }
589
+ },
590
+ "outputs": [],
591
+ "source": [
592
+ "# saving model\n",
593
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
594
+ "model.save_pretrained(peft_model_id)"
595
+ ]
596
+ },
597
+ {
598
+ "cell_type": "code",
599
+ "execution_count": 7,
600
+ "id": "bd20cd4c",
601
+ "metadata": {
602
+ "ExecuteTime": {
603
+ "end_time": "2023-05-30T09:53:15.240763Z",
604
+ "start_time": "2023-05-30T09:53:15.059304Z"
605
+ }
606
+ },
607
+ "outputs": [
608
+ {
609
+ "name": "stdout",
610
+ "output_type": "stream",
611
+ "text": [
612
+ "164K\tt5-large_PROMPT_TUNING_SEQ_2_SEQ_LM/adapter_model.bin\r\n"
613
+ ]
614
+ }
615
+ ],
616
+ "source": [
617
+ "ckpt = f\"{peft_model_id}/adapter_model.bin\"\n",
618
+ "!du -h $ckpt"
619
+ ]
620
+ },
621
+ {
622
+ "cell_type": "code",
623
+ "execution_count": 8,
624
+ "id": "76c2fc29",
625
+ "metadata": {
626
+ "ExecuteTime": {
627
+ "end_time": "2023-05-30T09:53:25.055105Z",
628
+ "start_time": "2023-05-30T09:53:17.797989Z"
629
+ }
630
+ },
631
+ "outputs": [],
632
+ "source": [
633
+ "from peft import PeftModel, PeftConfig\n",
634
+ "\n",
635
+ "peft_model_id = f\"{model_name_or_path}_{peft_config.peft_type}_{peft_config.task_type}\"\n",
636
+ "\n",
637
+ "config = PeftConfig.from_pretrained(peft_model_id)\n",
638
+ "model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)\n",
639
+ "model = PeftModel.from_pretrained(model, peft_model_id)"
640
+ ]
641
+ },
642
+ {
643
+ "cell_type": "code",
644
+ "execution_count": 9,
645
+ "id": "d997f1cc",
646
+ "metadata": {
647
+ "ExecuteTime": {
648
+ "end_time": "2023-05-30T09:53:26.777030Z",
649
+ "start_time": "2023-05-30T09:53:26.013697Z"
650
+ }
651
+ },
652
+ "outputs": [
653
+ {
654
+ "name": "stdout",
655
+ "output_type": "stream",
656
+ "text": [
657
+ "Aspocomp Group , headquartered in Helsinki , Finland , develops interconnection solutions for the electronics industry .\n",
658
+ "{'input_ids': tensor([[ 71, 7990, 7699, 1531, 3, 6, 3, 27630, 16, 29763,\n",
659
+ " 3, 6, 16458, 3, 6, 1344, 7, 1413, 28102, 1275,\n",
660
+ " 21, 8, 12800, 681, 3, 5, 1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
661
+ " 1, 1, 1]])}\n",
662
+ "tensor([[ 0, 7163, 1]])\n",
663
+ "['neutral']\n"
664
+ ]
665
+ }
666
+ ],
667
+ "source": [
668
+ "model.eval()\n",
669
+ "i = 107\n",
670
+ "inputs = tokenizer(dataset[\"validation\"][text_column][i], return_tensors=\"pt\")\n",
671
+ "print(dataset[\"validation\"][text_column][i])\n",
672
+ "print(inputs)\n",
673
+ "\n",
674
+ "with torch.no_grad():\n",
675
+ " outputs = model.generate(input_ids=inputs[\"input_ids\"], max_new_tokens=10)\n",
676
+ " print(outputs)\n",
677
+ " print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True))"
678
+ ]
679
+ },
680
+ {
681
+ "cell_type": "code",
682
+ "execution_count": null,
683
+ "id": "fb746c1e",
684
+ "metadata": {},
685
+ "outputs": [],
686
+ "source": []
687
+ }
688
+ ],
689
+ "metadata": {
690
+ "kernelspec": {
691
+ "display_name": "peft",
692
+ "language": "python",
693
+ "name": "peft"
694
+ },
695
+ "language_info": {
696
+ "codemirror_mode": {
697
+ "name": "ipython",
698
+ "version": 3
699
+ },
700
+ "file_extension": ".py",
701
+ "mimetype": "text/x-python",
702
+ "name": "python",
703
+ "nbconvert_exporter": "python",
704
+ "pygments_lexer": "ipython3",
705
+ "version": "3.9.16"
706
+ },
707
+ "toc": {
708
+ "base_numbering": 1,
709
+ "nav_menu": {},
710
+ "number_sections": true,
711
+ "sideBar": true,
712
+ "skip_h1_title": false,
713
+ "title_cell": "Table of Contents",
714
+ "title_sidebar": "Contents",
715
+ "toc_cell": false,
716
+ "toc_position": {},
717
+ "toc_section_display": true,
718
+ "toc_window_display": false
719
+ },
720
+ "varInspector": {
721
+ "cols": {
722
+ "lenName": 16,
723
+ "lenType": 16,
724
+ "lenVar": 40
725
+ },
726
+ "kernels_config": {
727
+ "python": {
728
+ "delete_cmd_postfix": "",
729
+ "delete_cmd_prefix": "del ",
730
+ "library": "var_list.py",
731
+ "varRefreshCmd": "print(var_dic_list())"
732
+ },
733
+ "r": {
734
+ "delete_cmd_postfix": ") ",
735
+ "delete_cmd_prefix": "rm(",
736
+ "library": "var_list.r",
737
+ "varRefreshCmd": "cat(var_dic_list()) "
738
+ }
739
+ },
740
+ "types_to_exclude": [
741
+ "module",
742
+ "function",
743
+ "builtin_function_or_method",
744
+ "instance",
745
+ "_Feature"
746
+ ],
747
+ "window_display": false
748
+ },
749
+ "vscode": {
750
+ "interpreter": {
751
+ "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
752
+ }
753
+ }
754
+ },
755
+ "nbformat": 4,
756
+ "nbformat_minor": 5
757
+ }