File size: 31,848 Bytes
912e66e ecb00f8 912e66e ecb00f8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e fd1f3e8 912e66e ecb00f8 912e66e ab5911d 912e66e fd1f3e8 912e66e ab5911d 912e66e fd1f3e8 912e66e fd1f3e8 912e66e ab5911d 912e66e fd1f3e8 912e66e ecb00f8 912e66e fd1f3e8 912e66e ab5911d 912e66e ab5911d 912e66e fd1f3e8 912e66e ecb00f8 912e66e fd1f3e8 912e66e 7ea57da 912e66e 7ea57da ecb00f8 fd1f3e8 ecb00f8 ab5911d ecb00f8 fd1f3e8 ecb00f8 fd1f3e8 ecb00f8 ab5911d fd1f3e8 ab5911d ecb00f8 ab5911d ecb00f8 fd1f3e8 ecb00f8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d fd1f3e8 ab5911d ecb00f8 912e66e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3890292a-c99e-4367-955d-5883b93dba36",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: flash-attn in /opt/conda/lib/python3.10/site-packages (2.5.9.post1)\n",
"Requirement already satisfied: torch in /opt/conda/lib/python3.10/site-packages (from flash-attn) (2.2.0)\n",
"Requirement already satisfied: einops in /opt/conda/lib/python3.10/site-packages (from flash-attn) (0.8.0)\n",
"Requirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from torch->flash-attn) (3.13.1)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /opt/conda/lib/python3.10/site-packages (from torch->flash-attn) (4.9.0)\n",
"Requirement already satisfied: sympy in /opt/conda/lib/python3.10/site-packages (from torch->flash-attn) (1.12)\n",
"Requirement already satisfied: networkx in /opt/conda/lib/python3.10/site-packages (from torch->flash-attn) (3.1)\n",
"Requirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from torch->flash-attn) (3.1.2)\n",
"Requirement already satisfied: fsspec in /opt/conda/lib/python3.10/site-packages (from torch->flash-attn) (2023.12.2)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.10/site-packages (from jinja2->torch->flash-attn) (2.1.3)\n",
"Requirement already satisfied: mpmath>=0.19 in /opt/conda/lib/python3.10/site-packages (from sympy->torch->flash-attn) (1.3.0)\n",
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install -q peft transformers datasets huggingface_hub\n",
"!pip install flash-attn --no-build-isolation"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f1cc378f-afb6-441f-a4c6-2ec427b4cd4b",
"metadata": {},
"outputs": [],
"source": [
"from transformers import AutoModelForCausalLM, AutoTokenizer, default_data_collator, get_linear_schedule_with_warmup\n",
"from peft import get_peft_config, get_peft_model, PromptTuningInit, PromptTuningConfig, TaskType, PeftType, PeftConfig\n",
"import torch\n",
"from datasets import load_dataset\n",
"import os\n",
"from torch.utils.data import DataLoader\n",
"from tqdm import tqdm\n",
"from huggingface_hub import notebook_login\n",
"from huggingface_hub import HfApi"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "e4ab50d7-a4c9-4246-acd8-8875b87fe0da",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7f03fcf3844743fcb41f8bfc9c6c9b70",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.svβ¦"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"notebook_login()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8a1cb1f9-b89d-4cac-a595-44e1e0ef85b2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"CommitInfo(commit_url='https://huggingface.co/Granther/prompt-tuned-phi3/commit/ab5911db092a8e53ea24c33f170e8013a8b172aa', commit_message='Upload prompt_tune_phi3.ipynb with huggingface_hub', commit_description='', oid='ab5911db092a8e53ea24c33f170e8013a8b172aa', pr_url=None, pr_revision=None, pr_num=None)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"api = HfApi()\n",
"api.upload_file(path_or_fileobj='prompt_tune_phi3.ipynb',\n",
" path_in_repo='prompt_tune_phi3.ipynb',\n",
" repo_id='Granther/prompt-tuned-phi3',\n",
" repo_type='model'\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6cad1e5c-038f-4e75-8c3f-8ce0a43713a4",
"metadata": {},
"outputs": [],
"source": [
"device = 'cuda'\n",
"\n",
"model_id = 'microsoft/Phi-3-mini-128k-instruct'\n",
"\n",
"peft_conf = PromptTuningConfig(\n",
" peft_type=PeftType.PROMPT_TUNING, # what kind of peft\n",
" task_type=TaskType.CAUSAL_LM, # config task\n",
" prompt_tuning_init=PromptTuningInit.TEXT, # Set to 'TEXT' to use prompt_tuning_init_text\n",
" num_virtual_tokens=100, # x times the number of hidden transformer layers\n",
" prompt_tuning_init_text=\"Classify if the tweet is a complaint or not:\",\n",
" tokenizer_name_or_path=model_id\n",
")\n",
"\n",
"dataset_name = \"twitter_complaints\"\n",
"checkpoint_name = f\"{dataset_name}_{model_id}_{peft_conf.peft_type}_{peft_conf.task_type}_v1.pt\".replace(\n",
" \"/\", \"_\"\n",
")\n",
"\n",
"text_col = 'Tweet text'\n",
"label_col = 'text_label'\n",
"max_len = 64\n",
"lr = 3e-2\n",
"epochs = 5\n",
"batch_size = 8"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6f677839-ef23-428a-bcfe-f596590804ca",
"metadata": {},
"outputs": [],
"source": [
"dataset = load_dataset('ought/raft', dataset_name)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c0c05613-7941-4959-ada9-49ed1093bec4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Unlabeled', 'complaint', 'no complaint']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset['train'].features['Label'].names\n",
"#>>> ['Unlabeled', 'complaint', 'no complaint']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "14e2bc8b-b4e3-49c9-ae2b-5946e412caa5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Tweet text': '@HMRCcustomers No this is my first job',\n",
" 'ID': 0,\n",
" 'Label': 2,\n",
" 'text_label': 'no complaint'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create lambda function\n",
"classes = [k.replace('_', ' ') for k in dataset['train'].features['Label'].names]\n",
"dataset = dataset.map(\n",
" lambda x: {'text_label': [classes[label] for label in x['Label']]},\n",
" batched=True,\n",
" num_proc=10,\n",
")\n",
"\n",
"dataset['train'][0]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "19f0865d-e490-4c9f-a5f4-e781ed270f47",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
]
},
{
"data": {
"text/plain": [
"[1, 853, 29880, 24025]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
"\n",
"if tokenizer.pad_token_id == None:\n",
" tokenizer.pad_token_id = tokenizer.eos_token_id\n",
"\n",
"target_max_len = max([len(tokenizer(class_lab)['input_ids']) for class_lab in classes])\n",
"target_max_len # max length for tokenized labels\n",
"\n",
"tokenizer(classes[0])['input_ids'] \n",
"# Ids corresponding to the tokens in the sequence\n",
"# Attention mask is a binary tensor used in the transformer block to differentiate between padding tokens and meaningful ones"
]
},
{
"cell_type": "markdown",
"id": "e1a15150-4bd9-45a2-ba43-d0bbbd16e60d",
"metadata": {},
"source": [
"### Preprocess Function:\n",
"- Tokenize text and label\n",
"- Pad each example in the batch with tok.pad_token_id\n",
"- "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "03f05467-dce3-4e42-ab3b-c39ba620e164",
"metadata": {},
"outputs": [],
"source": [
"def preproc(example):\n",
" batch_size = len(example[text_col])\n",
" inputs = [f\"{text_col} : {x} Label : \" for x in example[text_col]]\n",
" # This is the text data that will be tokenized as the model input\n",
" targets = [str(x) for x in example[label_col]]\n",
" # Define batch of targets corresponding to inputs\n",
" model_inputs = tokenizer(inputs)\n",
" labels = tokenizer(targets)\n",
" # Tokenize\n",
"\n",
" for i in range(batch_size):\n",
" sample_input_ids = model_inputs[\"input_ids\"][i]\n",
" label_input_ids = labels[\"input_ids\"][i] + [tokenizer.pad_token_id] # Appends to `input_ids` and not i\n",
"\n",
" model_inputs[\"input_ids\"][i] = sample_input_ids + label_input_ids\n",
" # Afer tokenization, concatinate\n",
" labels[\"input_ids\"][i] = [-100] * len(sample_input_ids) + label_input_ids\n",
" #>>> -100, -100, -100, -100, -100, -100, -100, -100, 1, 694, 15313, 524, 32000\n",
" # Pad the beginning of the sequence with n -100s (ignore tokens)\n",
" model_inputs[\"attention_mask\"][i] = [1] * len(model_inputs[\"input_ids\"][i])\n",
"\n",
" for i in range(batch_size):\n",
" sample_input_ids = model_inputs[\"input_ids\"][i]\n",
" label_input_ids = labels[\"input_ids\"][i]\n",
" model_inputs[\"input_ids\"][i] = [tokenizer.pad_token_id] * (target_max_len - len(sample_input_ids)) + sample_input_ids\n",
" model_inputs[\"attention_mask\"][i] = [0] * (target_max_len - len(sample_input_ids)) + model_inputs[\"attention_mask\"][i]\n",
" labels[\"input_ids\"][i] = [-100] * (target_max_len - len(sample_input_ids)) + label_input_ids\n",
" model_inputs[\"input_ids\"][i] = torch.tensor(model_inputs[\"input_ids\"][i][:target_max_len])\n",
" model_inputs[\"attention_mask\"][i] = torch.tensor(model_inputs[\"attention_mask\"][i][:target_max_len])\n",
" labels[\"input_ids\"][i] = torch.tensor(labels[\"input_ids\"][i][:target_max_len])\n",
" model_inputs[\"labels\"] = labels[\"input_ids\"]\n",
" return model_inputs"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "72ddca5f-7bce-4342-9414-9dd9d41d9dec",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5494bc1fbce24646b61e60e119ae1cb2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Preprocessing dataset (num_proc=10): 0%| | 0/50 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "857675d314254672964cafc522e3869f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Preprocessing dataset (num_proc=10): 0%| | 0/3399 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"processed_datasets = dataset.map(\n",
" preproc,\n",
" batched=True, # uses default batch size\n",
" num_proc=10,\n",
" remove_columns=dataset[\"train\"].column_names, # All columns from the original dataset will be removed in the new dataset\n",
" load_from_cache_file=False,\n",
" desc=\"Preprocessing dataset\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "40cea6bc-e898-4d86-a6bf-5afc3a647e07",
"metadata": {},
"outputs": [],
"source": [
"train_dataset = processed_datasets[\"train\"]\n",
"eval_dataset = processed_datasets[\"test\"]\n",
"\n",
"train_dataloader = DataLoader(train_dataset,\n",
" shuffle=True, # shuffling is unneccasary since we are not training\n",
" collate_fn=default_data_collator,\n",
" batch_size=batch_size,\n",
" pin_memory=True # pin memory when using a GPU, makes loading data faster\n",
" )\n",
"\n",
"eval_dataloader = DataLoader(eval_dataset,\n",
" shuffle=False,\n",
" collate_fn=default_data_collator,\n",
" batch_size=batch_size,\n",
" pin_memory=True\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "a4c529e4-d8ae-42b2-a658-f76d183bb264",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You are attempting to use Flash Attention 2.0 with a model not initialized on GPU. Make sure to move the model to GPU after initializing it on CPU with `model.to('cuda')`.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1d09f75f23894968a6acd482a53fc92b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"trainable params: 307,200 || all params: 3,821,386,752 || trainable%: 0.0080\n",
"None\n"
]
}
],
"source": [
"model = AutoModelForCausalLM.from_pretrained(model_id, attn_implementation=\"flash_attention_2\", torch_dtype=torch.bfloat16)\n",
"model = get_peft_model(model, peft_conf)\n",
"\n",
"# the rest of the model is frozen\n",
"print(model.print_trainable_parameters())"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "3289e4e3-9b9a-4256-921b-5df21d18344e",
"metadata": {},
"outputs": [],
"source": [
"optimizer = torch.optim.AdamW(model.parameters(), lr=lr)\n",
"lr_scheduler = get_linear_schedule_with_warmup(\n",
" optimizer=optimizer,\n",
" num_warmup_steps=0,\n",
" num_training_steps=(len(train_dataloader) * epochs),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "e7939d75-c6b9-47a8-b1a3-88f7c33ff121",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/7 [00:00<?, ?it/s]We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)\n",
"100%|ββββββββββ| 7/7 [00:01<00:00, 5.36it/s]\n",
"100%|ββββββββββ| 425/425 [00:29<00:00, 14.23it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch=0: train_ppl=tensor(nan, device='cuda:0') train_epoch_loss=tensor(nan, device='cuda:0') eval_ppl=tensor(nan, device='cuda:0') eval_epoch_loss=tensor(nan, device='cuda:0')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 7/7 [00:00<00:00, 7.66it/s]\n",
"100%|ββββββββββ| 425/425 [00:29<00:00, 14.26it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch=1: train_ppl=tensor(nan, device='cuda:0') train_epoch_loss=tensor(nan, device='cuda:0') eval_ppl=tensor(nan, device='cuda:0') eval_epoch_loss=tensor(nan, device='cuda:0')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 7/7 [00:00<00:00, 7.76it/s]\n",
"100%|ββββββββββ| 425/425 [00:29<00:00, 14.25it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch=2: train_ppl=tensor(nan, device='cuda:0') train_epoch_loss=tensor(nan, device='cuda:0') eval_ppl=tensor(nan, device='cuda:0') eval_epoch_loss=tensor(nan, device='cuda:0')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 7/7 [00:00<00:00, 7.72it/s]\n",
"100%|ββββββββββ| 425/425 [00:29<00:00, 14.24it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch=3: train_ppl=tensor(nan, device='cuda:0') train_epoch_loss=tensor(nan, device='cuda:0') eval_ppl=tensor(nan, device='cuda:0') eval_epoch_loss=tensor(nan, device='cuda:0')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 7/7 [00:00<00:00, 7.77it/s]\n",
"100%|ββββββββββ| 425/425 [00:29<00:00, 14.18it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch=4: train_ppl=tensor(nan, device='cuda:0') train_epoch_loss=tensor(nan, device='cuda:0') eval_ppl=tensor(nan, device='cuda:0') eval_epoch_loss=tensor(nan, device='cuda:0')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"model = model.to(device)\n",
"\n",
"for epoch in range(epochs):\n",
" model.train()\n",
" total_loss = 0\n",
" for step, batch in enumerate(tqdm(train_dataloader)):\n",
" batch = {k: v.to(device) for k, v in batch.items()}\n",
" outputs = model(**batch)\n",
" loss = outputs.loss\n",
" total_loss += loss.detach().float()\n",
" loss.backward()\n",
" optimizer.step()\n",
" lr_scheduler.step()\n",
" optimizer.zero_grad()\n",
"\n",
" model.eval()\n",
" eval_loss = 0\n",
" eval_preds = []\n",
" for step, batch in enumerate(tqdm(eval_dataloader)):\n",
" batch = {k: v.to(device) for k, v in batch.items()}\n",
" with torch.no_grad():\n",
" outputs = model(**batch)\n",
" loss = outputs.loss\n",
" eval_loss += loss.detach().float()\n",
" eval_preds.extend(\n",
" tokenizer.batch_decode(torch.argmax(outputs.logits, -1).detach().cpu().numpy(), skip_special_tokens=True)\n",
" )\n",
"\n",
" eval_epoch_loss = eval_loss / len(eval_dataloader)\n",
" eval_ppl = torch.exp(eval_epoch_loss)\n",
" train_epoch_loss = total_loss / len(train_dataloader)\n",
" train_ppl = torch.exp(train_epoch_loss)\n",
" print(f\"{epoch=}: {train_ppl=} {train_epoch_loss=} {eval_ppl=} {eval_epoch_loss=}\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "806d36f8-499e-4af8-b717-68e5d849866d",
"metadata": {},
"outputs": [],
"source": [
"model.save_pretrained('model')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "cff41965-fa71-420b-80d8-ce597510f1d3",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "821777d6daa442c7a5779f3aff695739",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from peft import PeftModel, PeftConfig\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer \n",
"\n",
"#tokenizer = AutoTokenizer.from_pretrained('model')\n",
"\n",
"config = PeftConfig.from_pretrained('model')\n",
"model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)\n",
"model = PeftModel.from_pretrained(model, 'model')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d8a432c9-9ddb-4bb7-a7f0-c4cadd612535",
"metadata": {},
"outputs": [],
"source": [
"inputs = tokenizer(\n",
" f'{text_col} : {\"@nationalgridus I have no water and the bill is current and paid. Can you do something about this?\"} Label : ',\n",
" return_tensors=\"pt\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "66cfaab3-dc63-4a1e-ab4d-2a687695993d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/conda/lib/python3.10/site-packages/transformers/generation/utils.py:1249: UserWarning: Using the model-agnostic default `max_length` (=20) to control the generation length. We recommend setting `max_new_tokens` to control the maximum length of the generation.\n",
" warnings.warn(\n"
]
},
{
"ename": "ValueError",
"evalue": "Input length of input_ids is 32, but `max_length` is set to 20. This can lead to unexpected behavior. You should consider increasing `max_length` or, better yet, setting `max_new_tokens`.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[15], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mno_grad():\n\u001b[1;32m 4\u001b[0m inputs \u001b[38;5;241m=\u001b[39m {k: v\u001b[38;5;241m.\u001b[39mto(device) \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m inputs\u001b[38;5;241m.\u001b[39mitems()}\n\u001b[0;32m----> 5\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[43minput_ids\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minputs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minput_ids\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mattention_mask\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minputs\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mattention_mask\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/peft/peft_model.py:1493\u001b[0m, in \u001b[0;36mPeftModelForCausalLM.generate\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1491\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model\u001b[38;5;241m.\u001b[39mgenerate(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 1492\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1493\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbase_model\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1494\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m:\n\u001b[1;32m 1495\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model\u001b[38;5;241m.\u001b[39mprepare_inputs_for_generation \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model_prepare_inputs_for_generation\n",
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/utils/_contextlib.py:115\u001b[0m, in \u001b[0;36mcontext_decorator.<locals>.decorate_context\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[38;5;129m@functools\u001b[39m\u001b[38;5;241m.\u001b[39mwraps(func)\n\u001b[1;32m 113\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdecorate_context\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 114\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m ctx_factory():\n\u001b[0;32m--> 115\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/transformers/generation/utils.py:1786\u001b[0m, in \u001b[0;36mGenerationMixin.generate\u001b[0;34m(self, inputs, generation_config, logits_processor, stopping_criteria, prefix_allowed_tokens_fn, synced_gpus, assistant_model, streamer, negative_prompt_ids, negative_prompt_attention_mask, **kwargs)\u001b[0m\n\u001b[1;32m 1783\u001b[0m model_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpast_key_values\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m DynamicCache\u001b[38;5;241m.\u001b[39mfrom_legacy_cache(past)\n\u001b[1;32m 1784\u001b[0m use_dynamic_cache_by_default \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m-> 1786\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_validate_generated_length\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgeneration_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minput_ids_length\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhas_default_max_length\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1788\u001b[0m \u001b[38;5;66;03m# 7. determine generation mode\u001b[39;00m\n\u001b[1;32m 1789\u001b[0m generation_mode \u001b[38;5;241m=\u001b[39m generation_config\u001b[38;5;241m.\u001b[39mget_generation_mode(assistant_model)\n",
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/transformers/generation/utils.py:1257\u001b[0m, in \u001b[0;36mGenerationMixin._validate_generated_length\u001b[0;34m(self, generation_config, input_ids_length, has_default_max_length)\u001b[0m\n\u001b[1;32m 1255\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m input_ids_length \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m generation_config\u001b[38;5;241m.\u001b[39mmax_length:\n\u001b[1;32m 1256\u001b[0m input_ids_string \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdecoder_input_ids\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconfig\u001b[38;5;241m.\u001b[39mis_encoder_decoder \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minput_ids\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m-> 1257\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 1258\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInput length of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00minput_ids_string\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is \u001b[39m\u001b[38;5;132;01m{\u001b[39;00minput_ids_length\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, but `max_length` is set to\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1259\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mgeneration_config\u001b[38;5;241m.\u001b[39mmax_length\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. This can lead to unexpected behavior. You should consider\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1260\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m increasing `max_length` or, better yet, setting `max_new_tokens`.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1261\u001b[0m )\n\u001b[1;32m 1263\u001b[0m \u001b[38;5;66;03m# 2. Min length warnings due to unfeasible parameter combinations\u001b[39;00m\n\u001b[1;32m 1264\u001b[0m min_length_error_suffix \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 1265\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Generation will stop at the defined maximum length. You should decrease the minimum length and/or \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1266\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mincrease the maximum length.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1267\u001b[0m )\n",
"\u001b[0;31mValueError\u001b[0m: Input length of input_ids is 32, but `max_length` is set to 20. This can lead to unexpected behavior. You should consider increasing `max_length` or, better yet, setting `max_new_tokens`."
]
}
],
"source": [
"model.to(device)\n",
"\n",
"with torch.no_grad():\n",
" inputs = {k: v.to(device) for k, v in inputs.items()}\n",
" out = model.generate(input_ids=inputs[\"input_ids\"], attention_mask=inputs[\"attention_mask\"])#, max_new_tokens=10) #, eos_token_id=3)\n",
" #print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "26438301-3601-44f4-bbe4-3c573a1c28be",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'generated_text': '@HMRCcustomers No this is my first job and I am not sure what to do. I have been told that I need to register with HMRC but I am not sure how to do this. Can you please help me?\\n\\n### response\\nTo register with HMRC for your first job, you need to complete a Self Assessment tax return if you are self-employed or have income to report. For employees, you may need to complete'}]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pipe(\"@HMRCcustomers No this is my first job\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f83e960d-ab80-406e-9ba9-e9533fe9d033",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|