File size: 95,114 Bytes
dff8d35 |
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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "c3bf15ad",
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"\"\"\"\n",
"Phase 5.1–5.3 — Activation-Level Unlearning Pipeline\n",
"====================================================\n",
"\n",
"1. Load prompts from activation_unlearning/data/prompt_set.csv\n",
"2. Run baseline recommendations\n",
"3. Apply activation-level unlearning\n",
"4. Re-run recommendations\n",
"5. Compare BEFORE vs AFTER\n",
"6. Supports forgetting:\n",
" - a list of movie titles\n",
" - OR all movies in prompt_set.csv\n",
"\"\"\"\n",
"\n",
"import os\n",
"import csv\n",
"import json\n",
"import torch\n",
"import numpy as np\n",
"from datetime import datetime\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"import activation_unlearning # << FIXED: resolve module root reliably\n",
"\n",
"# ==========================================================\n",
"# CONFIGURATION\n",
"# ==========================================================\n",
"\n",
"MODEL_NAME = \"Qwen/Qwen2.5-3B-Instruct\"\n",
"\n",
"CHECKPOINT_OUT = \"unlearned_checkpoint\"\n",
"\n",
"SALIENCY_FILE = \"sensitive_neurons.json\"\n",
"FISHER_FILE = \"fisher/top_fisher_neurons.json\"\n",
"\n",
"FORGET_MOVIES = [\n",
" # \"Inception\",\n",
" # \"Interstellar\",\n",
"]\n",
"\n",
"FORGET_ALL_MOVIES = False\n",
"\n",
"DAMPEN_FACTOR = 0.98\n",
"REVERSE_GRADIENT = False\n",
"\n",
"os.makedirs(CHECKPOINT_OUT, exist_ok=True)\n",
"\n",
"print(\"\\n[INFO] Activation-Level Unlearning Pipeline Starting...\\n\")\n",
"\n",
"# ==========================================================\n",
"# LOAD PROMPTS (FIXED: works in Jupyter AND scripts)\n",
"# ==========================================================\n",
"\n",
"def load_prompts():\n",
" \"\"\"Load prompt_set.csv from activation_unlearning/data/.\"\"\"\n",
" module_root = os.path.dirname(activation_unlearning.__file__)\n",
" csv_path = os.path.join(module_root, \"data\", \"prompt_set.csv\")\n",
"\n",
" if not os.path.exists(csv_path):\n",
" raise FileNotFoundError(f\"prompt_set.csv not found at: {csv_path}\")\n",
"\n",
" prompts = []\n",
" with open(csv_path, \"r\", encoding=\"utf-8\") as f:\n",
" reader = csv.DictReader(f)\n",
" for row in reader:\n",
" prompts.append((int(row[\"id\"]), row[\"prompt\"]))\n",
"\n",
" print(f\"[INFO] Loaded {len(prompts)} prompts from {csv_path}\")\n",
" return prompts\n",
"\n",
"# ==========================================================\n",
"# MODEL LOADING\n",
"# ==========================================================\n",
"\n",
"print(f\"[INFO] Loading model: {MODEL_NAME}\")\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
"model = AutoModelForCausalLM.from_pretrained(\n",
" MODEL_NAME,\n",
" torch_dtype=torch.float16 if device == \"cuda\" else torch.float32,\n",
" device_map=\"auto\",\n",
")\n",
"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n",
"model.eval()\n",
"\n",
"n_layers = len(model.model.layers)\n",
"print(f\"[INFO] Model loaded on {device} with {n_layers} transformer layers.\")\n",
"\n",
"# ==========================================================\n",
"# LOAD SENSITIVE NEURON MAPS\n",
"# ==========================================================\n",
"\n",
"def load_json(path):\n",
" if not os.path.exists(path):\n",
" return {}\n",
" with open(path, \"r\", encoding=\"utf-8\") as f:\n",
" return json.load(f)\n",
"\n",
"saliency_map = load_json(SALIENCY_FILE)\n",
"fisher_map = load_json(FISHER_FILE)\n",
"\n",
"sensitive = {}\n",
"\n",
"for l in range(n_layers):\n",
" s = set(saliency_map.get(f\"layer_{l}\", []))\n",
" f = set(fisher_map.get(f\"layer_{l}\", []))\n",
" if s or f:\n",
" sensitive[f\"layer_{l}\"] = sorted(s.union(f))\n",
"\n",
"print(f\"[INFO] Sensitive neurons detected in {len(sensitive)} layers.\")\n",
"\n",
"# ==========================================================\n",
"# GENERATE RESPONSE\n",
"# ==========================================================\n",
"\n",
"def generate_response(question, mdl=model, tok=tokenizer):\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant that makes high-quality recommendations.\"},\n",
" {\"role\": \"user\", \"content\": question},\n",
" ]\n",
" text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
" inputs = tok(text, return_tensors=\"pt\").to(device)\n",
"\n",
" with torch.no_grad():\n",
" output = mdl.generate(\n",
" **inputs,\n",
" max_new_tokens=256,\n",
" temperature=0.7,\n",
" top_p=0.9,\n",
" )\n",
"\n",
" resp = tok.decode(output[0][inputs[\"input_ids\"].shape[1]:], skip_special_tokens=True)\n",
" return resp.strip()\n",
"\n",
"# ==========================================================\n",
"# BASELINE RESPONSES\n",
"# ==========================================================\n",
"\n",
"prompts = load_prompts()\n",
"\n",
"print(\"\\n[INFO] Running baseline recommendations...\\n\")\n",
"baseline = {}\n",
"\n",
"for pid, q in prompts:\n",
" resp = generate_response(q)\n",
" baseline[pid] = resp\n",
" print(f\"[Prompt {pid}] {q}\")\n",
" print(f\"[Before ] {resp}\")\n",
" print(\"-\" * 80)\n",
"\n",
"# ==========================================================\n",
"# UNLEARNING HOOKS\n",
"# ==========================================================\n",
"\n",
"def unlearn_hook(module, inp, out, layer_idx):\n",
" \"\"\"Apply activation dampening and optional gradient reversal.\"\"\"\n",
" if not isinstance(out, torch.Tensor):\n",
" out = out[0]\n",
"\n",
" out = out.clone()\n",
"\n",
" lname = f\"layer_{layer_idx}\"\n",
" if lname in sensitive:\n",
" idxs = torch.tensor(sensitive[lname], device=out.device)\n",
"\n",
" out.index_copy_(2, idxs, out.index_select(2, idxs) * DAMPEN_FACTOR)\n",
"\n",
" if REVERSE_GRADIENT:\n",
" def reverse_grad_hook(grad):\n",
" grad[:, :, idxs] *= -1\n",
" return grad\n",
" out.register_hook(reverse_grad_hook)\n",
"\n",
" return out\n",
"\n",
"handles = []\n",
"for idx, layer in enumerate(model.model.layers):\n",
" h = layer.register_forward_hook(lambda m, i, o, idx=idx: unlearn_hook(m, i, o, idx))\n",
" handles.append(h)\n",
"\n",
"print(f\"[INFO] Unlearning hooks registered on {len(handles)} layers.\\n\")\n",
"\n",
"# ==========================================================\n",
"# RESPONSES AFTER UNLEARNING\n",
"# ==========================================================\n",
"\n",
"print(\"\\n[INFO] Running responses AFTER unlearning...\\n\")\n",
"\n",
"after = {}\n",
"\n",
"for pid, q in prompts:\n",
" resp = generate_response(q)\n",
" after[pid] = resp\n",
" print(f\"[Prompt {pid}] {q}\")\n",
" print(f\"[After ] {resp}\")\n",
" print(\"-\" * 80)\n",
"\n",
"# ==========================================================\n",
"# SAVE CHECKPOINT\n",
"# ==========================================================\n",
"\n",
"save_path = os.path.join(CHECKPOINT_OUT, \"model_unlearned\")\n",
"model.save_pretrained(save_path)\n",
"tokenizer.save_pretrained(save_path)\n",
"\n",
"print(f\"\\n[INFO] Saved unlearned checkpoint → {save_path}\\n\")\n",
"\n",
"# ==========================================================\n",
"# CLEANUP HOOKS\n",
"# ==========================================================\n",
"\n",
"for h in handles:\n",
" h.remove()\n",
"\n",
"print(\"[INFO] Hooks removed. Unlearning complete.\\n\")\n",
"\n",
"# ==========================================================\n",
"# SAVE BEFORE/AFTER COMPARISON\n",
"# ==========================================================\n",
"\n",
"comparison = {\n",
" \"timestamp\": datetime.now().isoformat(),\n",
" \"prompts\": [],\n",
"}\n",
"\n",
"for pid, q in prompts:\n",
" comparison[\"prompts\"].append({\n",
" \"id\": pid,\n",
" \"question\": q,\n",
" \"before\": baseline[pid],\n",
" \"after\": after[pid],\n",
" })\n",
"\n",
"with open(\"unlearning_comparison.json\", \"w\", encoding=\"utf-8\") as f:\n",
" json.dump(comparison, f, indent=2, ensure_ascii=False)\n",
"\n",
"print(\"[INFO] Comparison written to unlearning_comparison.json\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "baf0b75e",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"import json\n",
"from tqdm import tqdm\n",
"\n",
"LOG_DIR = \"logs\"\n",
"DATASET_DIR = \"datasets\"\n",
"os.makedirs(DATASET_DIR, exist_ok=True)\n",
"\n",
"# --------------------------------------------------\n",
"# 1. Load latest recommender log\n",
"# --------------------------------------------------\n",
"log_files = sorted([\n",
" f for f in os.listdir(LOG_DIR)\n",
" if f.startswith(\"recommender_\") and f.endswith(\".json\")\n",
"])\n",
"\n",
"if not log_files:\n",
" raise FileNotFoundError(\"No recommender_*.json files found.\")\n",
"\n",
"latest = os.path.join(LOG_DIR, log_files[-1])\n",
"print(f\"[INFO] Using log: {latest}\")\n",
"\n",
"with open(latest, \"r\", encoding=\"utf-8\") as f:\n",
" data = json.load(f)\n",
"\n",
"records = data[\"records\"]\n",
"\n",
"# --------------------------------------------------\n",
"# 2. Movie-title extractor (regex)\n",
"# --------------------------------------------------\n",
"MOVIE_REGEX = re.compile(r'\"([^\"]+)\"|\\*([^\\*]+)\\*|([A-Z][A-Za-z0-9: ]{2,40})')\n",
"\n",
"def extract_movie_titles(text):\n",
" matches = MOVIE_REGEX.findall(text)\n",
" titles = {x or y or z for (x, y, z) in matches}\n",
" return {t.strip() for t in titles if len(t.split()) <= 6}\n",
"\n",
"# --------------------------------------------------\n",
"# 3. Collect all movies\n",
"# --------------------------------------------------\n",
"all_movies = set()\n",
"for r in records:\n",
" movies = extract_movie_titles(r[\"answer\"])\n",
" all_movies.update(movies)\n",
"\n",
"print(f\"[INFO] Detected {len(all_movies)} movie titles:\")\n",
"print(all_movies)\n",
"\n",
"# --------------------------------------------------\n",
"# 4. Build baseline ShareGPT dataset\n",
"# --------------------------------------------------\n",
"baseline_path = os.path.join(DATASET_DIR, \"baseline.jsonl\")\n",
"unlearn_path = os.path.join(DATASET_DIR, \"unlearn.jsonl\")\n",
"\n",
"with open(baseline_path, \"w\", encoding=\"utf-8\") as bf, \\\n",
" open(unlearn_path, \"w\", encoding=\"utf-8\") as uf:\n",
"\n",
" for r in tqdm(records, desc=\"Building datasets\"):\n",
" q = r[\"question\"]\n",
" a = r[\"answer\"]\n",
"\n",
" # --------------------------\n",
" # ShareGPT format\n",
" # --------------------------\n",
" baseline_entry = {\n",
" \"conversations\": [\n",
" {\"from\": \"human\", \"value\": q},\n",
" {\"from\": \"assistant\", \"value\": a}\n",
" ]\n",
" }\n",
"\n",
" # --------------------------\n",
" # Remove movie names in unlearn dataset\n",
" # --------------------------\n",
" a_unlearn = a\n",
" for movie in all_movies:\n",
" a_unlearn = a_unlearn.replace(movie, \"[FORGOTTEN]\")\n",
"\n",
" unlearn_entry = {\n",
" \"conversations\": [\n",
" {\"from\": \"human\", \"value\": q},\n",
" {\"from\": \"assistant\", \"value\": a_unlearn}\n",
" ]\n",
" }\n",
"\n",
" bf.write(json.dumps(baseline_entry) + \"\\n\")\n",
" uf.write(json.dumps(unlearn_entry) + \"\\n\")\n",
"\n",
"print(f\"[INFO] Baseline dataset written to: {baseline_path}\")\n",
"print(f\"[INFO] Unlearn dataset written to: {unlearn_path}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2ba82a86",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Now in: /home/rameyjm7/workspace/TML/lpu/llm-preference-unlearning\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
"To disable this warning, you can either:\n",
"\t- Avoid using `tokenizers` before the fork if possible\n",
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[INFO|2025-11-23 22:46:01] llamafactory.hparams.parser:468 >> Process rank: 0, world size: 1, device: cuda:0, distributed training: False, compute dtype: torch.float16\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,025 >> loading file vocab.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/vocab.json\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,025 >> loading file merges.txt from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/merges.txt\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,025 >> loading file tokenizer.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/tokenizer.json\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,025 >> loading file added_tokens.json from cache at None\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,025 >> loading file special_tokens_map.json from cache at None\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,025 >> loading file tokenizer_config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/tokenizer_config.json\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,025 >> loading file chat_template.jinja from cache at None\n",
"[INFO|tokenization_utils_base.py:2364] 2025-11-23 22:46:02,183 >> Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n",
"[INFO|configuration_utils.py:765] 2025-11-23 22:46:02,415 >> loading configuration file config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/config.json\n",
"[INFO|configuration_utils.py:839] 2025-11-23 22:46:02,419 >> Model config Qwen2Config {\n",
" \"architectures\": [\n",
" \"Qwen2ForCausalLM\"\n",
" ],\n",
" \"attention_dropout\": 0.0,\n",
" \"bos_token_id\": 151643,\n",
" \"dtype\": \"bfloat16\",\n",
" \"eos_token_id\": 151645,\n",
" \"hidden_act\": \"silu\",\n",
" \"hidden_size\": 2048,\n",
" \"initializer_range\": 0.02,\n",
" \"intermediate_size\": 11008,\n",
" \"layer_types\": [\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\"\n",
" ],\n",
" \"max_position_embeddings\": 32768,\n",
" \"max_window_layers\": 70,\n",
" \"model_type\": \"qwen2\",\n",
" \"num_attention_heads\": 16,\n",
" \"num_hidden_layers\": 36,\n",
" \"num_key_value_heads\": 2,\n",
" \"rms_norm_eps\": 1e-06,\n",
" \"rope_scaling\": null,\n",
" \"rope_theta\": 1000000.0,\n",
" \"sliding_window\": null,\n",
" \"tie_word_embeddings\": true,\n",
" \"transformers_version\": \"4.57.1\",\n",
" \"use_cache\": true,\n",
" \"use_sliding_window\": false,\n",
" \"vocab_size\": 151936\n",
"}\n",
"\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,487 >> loading file vocab.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/vocab.json\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,487 >> loading file merges.txt from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/merges.txt\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,487 >> loading file tokenizer.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/tokenizer.json\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,487 >> loading file added_tokens.json from cache at None\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,487 >> loading file special_tokens_map.json from cache at None\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,487 >> loading file tokenizer_config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/tokenizer_config.json\n",
"[INFO|tokenization_utils_base.py:2095] 2025-11-23 22:46:02,487 >> loading file chat_template.jinja from cache at None\n",
"[INFO|tokenization_utils_base.py:2364] 2025-11-23 22:46:02,645 >> Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n",
"[WARNING|2025-11-23 22:46:02] llamafactory.data.template:148 >> `template` was not specified, try parsing the chat template from the tokenizer.\n",
"[INFO|2025-11-23 22:46:02] llamafactory.data.loader:143 >> Loading dataset unlearn.jsonl...\n",
"training example:\n",
"input_ids:\n",
"[151644, 8948, 198, 2610, 525, 1207, 16948, 11, 3465, 553, 54364, 14817, 13, 1446, 525, 264, 10950, 17847, 13, 151645, 198, 151644, 872, 198, 40451, 752, 279, 1429, 38219, 5700, 304, 279, 220, 17, 15, 17, 15, 12, 17, 15, 17, 20, 2088, 13, 151645, 198, 151644, 77091, 198, 58, 30902, 38, 23798, 953, 60, 5700, 646, 387, 43022, 323, 13798, 389, 1128, 1817, 25708, 31138, 38219, 13, 151645, 198]\n",
"inputs:\n",
"<|im_start|>system\n",
"You are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n",
"<|im_start|>user\n",
"Tell me the most informative movie in the 2020-2025 range.<|im_end|>\n",
"<|im_start|>assistant\n",
"[FORGOTTEN] movie can be subjective and depends on what each viewer considers informative.<|im_end|>\n",
"\n",
"label_ids:\n",
"[-100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, 58, 30902, 38, 23798, 953, 60, 5700, 646, 387, 43022, 323, 13798, 389, 1128, 1817, 25708, 31138, 38219, 13, 151645, 198]\n",
"labels:\n",
"[FORGOTTEN] movie can be subjective and depends on what each viewer considers informative.<|im_end|>\n",
"\n",
"[INFO|configuration_utils.py:765] 2025-11-23 22:46:03,262 >> loading configuration file config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/config.json\n",
"[INFO|configuration_utils.py:839] 2025-11-23 22:46:03,263 >> Model config Qwen2Config {\n",
" \"architectures\": [\n",
" \"Qwen2ForCausalLM\"\n",
" ],\n",
" \"attention_dropout\": 0.0,\n",
" \"bos_token_id\": 151643,\n",
" \"dtype\": \"bfloat16\",\n",
" \"eos_token_id\": 151645,\n",
" \"hidden_act\": \"silu\",\n",
" \"hidden_size\": 2048,\n",
" \"initializer_range\": 0.02,\n",
" \"intermediate_size\": 11008,\n",
" \"layer_types\": [\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\"\n",
" ],\n",
" \"max_position_embeddings\": 32768,\n",
" \"max_window_layers\": 70,\n",
" \"model_type\": \"qwen2\",\n",
" \"num_attention_heads\": 16,\n",
" \"num_hidden_layers\": 36,\n",
" \"num_key_value_heads\": 2,\n",
" \"rms_norm_eps\": 1e-06,\n",
" \"rope_scaling\": null,\n",
" \"rope_theta\": 1000000.0,\n",
" \"sliding_window\": null,\n",
" \"tie_word_embeddings\": true,\n",
" \"transformers_version\": \"4.57.1\",\n",
" \"use_cache\": true,\n",
" \"use_sliding_window\": false,\n",
" \"vocab_size\": 151936\n",
"}\n",
"\n",
"[INFO|2025-11-23 22:46:03] llamafactory.model.model_utils.kv_cache:143 >> KV cache is disabled during training.\n",
"[WARNING|logging.py:328] 2025-11-23 22:46:04,259 >> `torch_dtype` is deprecated! Use `dtype` instead!\n",
"[INFO|modeling_utils.py:1172] 2025-11-23 22:46:04,260 >> loading weights file model.safetensors from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/model.safetensors.index.json\n",
"[INFO|modeling_utils.py:2341] 2025-11-23 22:46:04,261 >> Instantiating Qwen2ForCausalLM model under default dtype torch.float16.\n",
"[INFO|configuration_utils.py:986] 2025-11-23 22:46:04,262 >> Generate config GenerationConfig {\n",
" \"bos_token_id\": 151643,\n",
" \"eos_token_id\": 151645,\n",
" \"use_cache\": false\n",
"}\n",
"\n",
"Loading checkpoint shards: 100%|██████████████████| 2/2 [00:00<00:00, 2.13it/s]\n",
"[INFO|configuration_utils.py:941] 2025-11-23 22:46:05,295 >> loading configuration file generation_config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/generation_config.json\n",
"[INFO|configuration_utils.py:986] 2025-11-23 22:46:05,295 >> Generate config GenerationConfig {\n",
" \"bos_token_id\": 151643,\n",
" \"do_sample\": true,\n",
" \"eos_token_id\": [\n",
" 151645,\n",
" 151643\n",
" ],\n",
" \"pad_token_id\": 151643,\n",
" \"repetition_penalty\": 1.05,\n",
" \"temperature\": 0.7,\n",
" \"top_k\": 20,\n",
" \"top_p\": 0.8\n",
"}\n",
"\n",
"[INFO|dynamic_module_utils.py:423] 2025-11-23 22:46:05,325 >> Could not locate the custom_generate/generate.py inside Qwen/Qwen2.5-3B-Instruct.\n",
"[INFO|2025-11-23 22:46:05] llamafactory.model.model_utils.checkpointing:143 >> Gradient checkpointing enabled.\n",
"[INFO|2025-11-23 22:46:05] llamafactory.model.model_utils.attention:143 >> Using torch SDPA for faster training and inference.\n",
"[INFO|2025-11-23 22:46:05] llamafactory.model.adapter:143 >> Upcasting trainable params to float32.\n",
"[INFO|2025-11-23 22:46:05] llamafactory.model.adapter:143 >> Fine-tuning method: LoRA\n",
"[INFO|2025-11-23 22:46:05] llamafactory.model.model_utils.misc:143 >> Found linear modules: down_proj,k_proj,q_proj,v_proj,o_proj,gate_proj,up_proj\n",
"[INFO|2025-11-23 22:46:06] llamafactory.model.loader:143 >> trainable params: 119,734,272 || all params: 3,205,672,960 || trainable%: 3.7351\n",
"[WARNING|trainer.py:906] 2025-11-23 22:46:06,343 >> The model is already on multiple devices. Skipping the move to device specified in `args`.\n",
"[INFO|trainer.py:699] 2025-11-23 22:46:06,347 >> max_steps is given, it will override any value given in num_train_epochs\n",
"[INFO|trainer.py:749] 2025-11-23 22:46:06,347 >> Using auto half precision backend\n",
"[WARNING|2025-11-23 22:46:06] llamafactory.train.callbacks:154 >> Previous trainer log in this folder will be deleted.\n",
"[WARNING|trainer.py:982] 2025-11-23 22:46:06,356 >> The tokenizer has new PAD/BOS/EOS tokens that differ from the model config and generation config. The model config and generation config were aligned accordingly, being updated with the tokenizer's values. Updated tokens: {'bos_token_id': None, 'pad_token_id': 151643}.\n",
"[DEBUG|trainer.py:2373] 2025-11-23 22:46:06,481 >> Currently training with a batch size of: 1\n",
"[INFO|trainer.py:2519] 2025-11-23 22:46:06,540 >> ***** Running training *****\n",
"[INFO|trainer.py:2520] 2025-11-23 22:46:06,540 >> Num examples = 49\n",
"[INFO|trainer.py:2521] 2025-11-23 22:46:06,540 >> Num Epochs = 3\n",
"[INFO|trainer.py:2522] 2025-11-23 22:46:06,540 >> Instantaneous batch size per device = 1\n",
"[INFO|trainer.py:2525] 2025-11-23 22:46:06,540 >> Total train batch size (w. parallel, distributed & accumulation) = 4\n",
"[INFO|trainer.py:2526] 2025-11-23 22:46:06,540 >> Gradient Accumulation steps = 4\n",
"[INFO|trainer.py:2527] 2025-11-23 22:46:06,540 >> Total optimization steps = 30\n",
"[INFO|trainer.py:2528] 2025-11-23 22:46:06,543 >> Number of trainable parameters = 119,734,272\n",
"{'loss': 6.4778, 'grad_norm': 9.732869148254395, 'learning_rate': 0.0, 'epoch': 0.08}\n",
"{'loss': 6.6025, 'grad_norm': nan, 'learning_rate': 1.5e-05, 'epoch': 0.16} \n",
"{'loss': 6.7008, 'grad_norm': nan, 'learning_rate': 3e-05, 'epoch': 0.24} \n",
"{'loss': 6.472, 'grad_norm': 10.529868125915527, 'learning_rate': 2.9905683148398642e-05, 'epoch': 0.33}\n",
"{'loss': 4.8467, 'grad_norm': 7.224335670471191, 'learning_rate': 2.9623918682727355e-05, 'epoch': 0.41}\n",
"{'loss': 5.1135, 'grad_norm': 8.088994026184082, 'learning_rate': 2.9158249954625514e-05, 'epoch': 0.49}\n",
"{'loss': 4.5696, 'grad_norm': 6.092275142669678, 'learning_rate': 2.8514533018536286e-05, 'epoch': 0.57}\n",
"{'loss': 3.9616, 'grad_norm': 8.128101348876953, 'learning_rate': 2.770086298842426e-05, 'epoch': 0.65}\n",
"{'loss': 2.9465, 'grad_norm': 12.102164268493652, 'learning_rate': 2.672747223702045e-05, 'epoch': 0.73}\n",
"{'loss': 2.6189, 'grad_norm': 4.99399471282959, 'learning_rate': 2.5606601717798212e-05, 'epoch': 0.82}\n",
" 33%|██████████████▎ | 10/30 [00:08<00:15, 1.31it/s][INFO|trainer.py:4309] 2025-11-23 22:46:15,017 >> Saving model checkpoint to output/qwen-unlearn/checkpoint-10\n",
"[INFO|configuration_utils.py:765] 2025-11-23 22:46:15,117 >> loading configuration file config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/config.json\n",
"[INFO|configuration_utils.py:839] 2025-11-23 22:46:15,118 >> Model config Qwen2Config {\n",
" \"architectures\": [\n",
" \"Qwen2ForCausalLM\"\n",
" ],\n",
" \"attention_dropout\": 0.0,\n",
" \"bos_token_id\": 151643,\n",
" \"dtype\": \"bfloat16\",\n",
" \"eos_token_id\": 151645,\n",
" \"hidden_act\": \"silu\",\n",
" \"hidden_size\": 2048,\n",
" \"initializer_range\": 0.02,\n",
" \"intermediate_size\": 11008,\n",
" \"layer_types\": [\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\"\n",
" ],\n",
" \"max_position_embeddings\": 32768,\n",
" \"max_window_layers\": 70,\n",
" \"model_type\": \"qwen2\",\n",
" \"num_attention_heads\": 16,\n",
" \"num_hidden_layers\": 36,\n",
" \"num_key_value_heads\": 2,\n",
" \"rms_norm_eps\": 1e-06,\n",
" \"rope_scaling\": null,\n",
" \"rope_theta\": 1000000.0,\n",
" \"sliding_window\": null,\n",
" \"tie_word_embeddings\": true,\n",
" \"transformers_version\": \"4.57.1\",\n",
" \"use_cache\": true,\n",
" \"use_sliding_window\": false,\n",
" \"vocab_size\": 151936\n",
"}\n",
"\n",
"[INFO|tokenization_utils_base.py:2421] 2025-11-23 22:46:16,029 >> chat template saved in output/qwen-unlearn/checkpoint-10/chat_template.jinja\n",
"[INFO|tokenization_utils_base.py:2590] 2025-11-23 22:46:16,033 >> tokenizer config file saved in output/qwen-unlearn/checkpoint-10/tokenizer_config.json\n",
"[INFO|tokenization_utils_base.py:2599] 2025-11-23 22:46:16,037 >> Special tokens file saved in output/qwen-unlearn/checkpoint-10/special_tokens_map.json\n",
"{'loss': 2.6302, 'grad_norm': 4.449975967407227, 'learning_rate': 2.4352347027881003e-05, 'epoch': 0.9}\n",
"{'loss': 3.0253, 'grad_norm': 5.362172603607178, 'learning_rate': 2.298048114773005e-05, 'epoch': 0.98}\n",
"{'loss': 2.2373, 'grad_norm': 6.1038970947265625, 'learning_rate': 2.1508256086763372e-05, 'epoch': 1.0}\n",
"{'loss': 2.5682, 'grad_norm': 4.612813472747803, 'learning_rate': 1.995418592932751e-05, 'epoch': 1.08}\n",
"{'loss': 2.5687, 'grad_norm': 3.570129632949829, 'learning_rate': 1.8337814009344716e-05, 'epoch': 1.16}\n",
"{'loss': 2.0957, 'grad_norm': 3.3741002082824707, 'learning_rate': 1.667946714154962e-05, 'epoch': 1.24}\n",
"{'loss': 2.3439, 'grad_norm': 3.095426082611084, 'learning_rate': 1.5e-05, 'epoch': 1.33}\n",
"{'loss': 1.9315, 'grad_norm': 2.836843967437744, 'learning_rate': 1.3320532858450382e-05, 'epoch': 1.41}\n",
"{'loss': 2.3324, 'grad_norm': 3.2753772735595703, 'learning_rate': 1.1662185990655285e-05, 'epoch': 1.49}\n",
"{'loss': 2.0836, 'grad_norm': 2.9651238918304443, 'learning_rate': 1.0045814070672498e-05, 'epoch': 1.57}\n",
" 67%|████████████████████████████▋ | 20/30 [00:20<00:08, 1.24it/s][INFO|trainer.py:4309] 2025-11-23 22:46:27,468 >> Saving model checkpoint to output/qwen-unlearn/checkpoint-20\n",
"[INFO|configuration_utils.py:765] 2025-11-23 22:46:27,569 >> loading configuration file config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/config.json\n",
"[INFO|configuration_utils.py:839] 2025-11-23 22:46:27,570 >> Model config Qwen2Config {\n",
" \"architectures\": [\n",
" \"Qwen2ForCausalLM\"\n",
" ],\n",
" \"attention_dropout\": 0.0,\n",
" \"bos_token_id\": 151643,\n",
" \"dtype\": \"bfloat16\",\n",
" \"eos_token_id\": 151645,\n",
" \"hidden_act\": \"silu\",\n",
" \"hidden_size\": 2048,\n",
" \"initializer_range\": 0.02,\n",
" \"intermediate_size\": 11008,\n",
" \"layer_types\": [\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\"\n",
" ],\n",
" \"max_position_embeddings\": 32768,\n",
" \"max_window_layers\": 70,\n",
" \"model_type\": \"qwen2\",\n",
" \"num_attention_heads\": 16,\n",
" \"num_hidden_layers\": 36,\n",
" \"num_key_value_heads\": 2,\n",
" \"rms_norm_eps\": 1e-06,\n",
" \"rope_scaling\": null,\n",
" \"rope_theta\": 1000000.0,\n",
" \"sliding_window\": null,\n",
" \"tie_word_embeddings\": true,\n",
" \"transformers_version\": \"4.57.1\",\n",
" \"use_cache\": true,\n",
" \"use_sliding_window\": false,\n",
" \"vocab_size\": 151936\n",
"}\n",
"\n",
"[INFO|tokenization_utils_base.py:2421] 2025-11-23 22:46:30,336 >> chat template saved in output/qwen-unlearn/checkpoint-20/chat_template.jinja\n",
"[INFO|tokenization_utils_base.py:2590] 2025-11-23 22:46:30,475 >> tokenizer config file saved in output/qwen-unlearn/checkpoint-20/tokenizer_config.json\n",
"[INFO|tokenization_utils_base.py:2599] 2025-11-23 22:46:30,479 >> Special tokens file saved in output/qwen-unlearn/checkpoint-20/special_tokens_map.json\n",
"{'loss': 1.7179, 'grad_norm': 2.715752124786377, 'learning_rate': 8.491743913236629e-06, 'epoch': 1.65}\n",
"{'loss': 2.1136, 'grad_norm': 4.121822357177734, 'learning_rate': 7.019518852269953e-06, 'epoch': 1.73}\n",
"{'loss': 2.2407, 'grad_norm': 2.9153966903686523, 'learning_rate': 5.647652972118998e-06, 'epoch': 1.82}\n",
"{'loss': 2.0327, 'grad_norm': 3.194537401199341, 'learning_rate': 4.393398282201788e-06, 'epoch': 1.9}\n",
"{'loss': 1.8791, 'grad_norm': 3.116147994995117, 'learning_rate': 3.272527762979553e-06, 'epoch': 1.98}\n",
"{'loss': 1.3358, 'grad_norm': 4.590542793273926, 'learning_rate': 2.2991370115757383e-06, 'epoch': 2.0}\n",
"{'loss': 2.0279, 'grad_norm': 3.4651827812194824, 'learning_rate': 1.4854669814637145e-06, 'epoch': 2.08}\n",
"{'loss': 1.6703, 'grad_norm': 2.676164388656616, 'learning_rate': 8.417500453744864e-07, 'epoch': 2.16}\n",
"{'loss': 1.6428, 'grad_norm': 2.570676803588867, 'learning_rate': 3.760813172726457e-07, 'epoch': 2.24}\n",
"{'loss': 1.9874, 'grad_norm': 3.00146484375, 'learning_rate': 9.431685160136094e-08, 'epoch': 2.33}\n",
"100%|███████████████████████████████████████████| 30/30 [00:38<00:00, 1.18it/s][INFO|trainer.py:4309] 2025-11-23 22:46:45,034 >> Saving model checkpoint to output/qwen-unlearn/checkpoint-30\n",
"[INFO|configuration_utils.py:765] 2025-11-23 22:46:45,132 >> loading configuration file config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/config.json\n",
"[INFO|configuration_utils.py:839] 2025-11-23 22:46:45,133 >> Model config Qwen2Config {\n",
" \"architectures\": [\n",
" \"Qwen2ForCausalLM\"\n",
" ],\n",
" \"attention_dropout\": 0.0,\n",
" \"bos_token_id\": 151643,\n",
" \"dtype\": \"bfloat16\",\n",
" \"eos_token_id\": 151645,\n",
" \"hidden_act\": \"silu\",\n",
" \"hidden_size\": 2048,\n",
" \"initializer_range\": 0.02,\n",
" \"intermediate_size\": 11008,\n",
" \"layer_types\": [\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\"\n",
" ],\n",
" \"max_position_embeddings\": 32768,\n",
" \"max_window_layers\": 70,\n",
" \"model_type\": \"qwen2\",\n",
" \"num_attention_heads\": 16,\n",
" \"num_hidden_layers\": 36,\n",
" \"num_key_value_heads\": 2,\n",
" \"rms_norm_eps\": 1e-06,\n",
" \"rope_scaling\": null,\n",
" \"rope_theta\": 1000000.0,\n",
" \"sliding_window\": null,\n",
" \"tie_word_embeddings\": true,\n",
" \"transformers_version\": \"4.57.1\",\n",
" \"use_cache\": true,\n",
" \"use_sliding_window\": false,\n",
" \"vocab_size\": 151936\n",
"}\n",
"\n",
"[INFO|tokenization_utils_base.py:2421] 2025-11-23 22:46:47,174 >> chat template saved in output/qwen-unlearn/checkpoint-30/chat_template.jinja\n",
"[INFO|tokenization_utils_base.py:2590] 2025-11-23 22:46:47,179 >> tokenizer config file saved in output/qwen-unlearn/checkpoint-30/tokenizer_config.json\n",
"[INFO|tokenization_utils_base.py:2599] 2025-11-23 22:46:47,183 >> Special tokens file saved in output/qwen-unlearn/checkpoint-30/special_tokens_map.json\n",
"[INFO|trainer.py:2810] 2025-11-23 22:46:50,660 >> \n",
"\n",
"Training completed. Do not forget to share your model on huggingface.co/models =)\n",
"\n",
"\n",
"{'train_runtime': 44.1171, 'train_samples_per_second': 2.72, 'train_steps_per_second': 0.68, 'train_loss': 3.092493176460266, 'epoch': 2.33}\n",
"100%|███████████████████████████████████████████| 30/30 [00:44<00:00, 1.47s/it]\n",
"[INFO|trainer.py:4309] 2025-11-23 22:46:50,665 >> Saving model checkpoint to output/qwen-unlearn\n",
"[INFO|configuration_utils.py:765] 2025-11-23 22:46:50,791 >> loading configuration file config.json from cache at /home/rameyjm7/.cache/huggingface/hub/models--Qwen--Qwen2.5-3B-Instruct/snapshots/aa8e72537993ba99e69dfaafa59ed015b17504d1/config.json\n",
"[INFO|configuration_utils.py:839] 2025-11-23 22:46:50,792 >> Model config Qwen2Config {\n",
" \"architectures\": [\n",
" \"Qwen2ForCausalLM\"\n",
" ],\n",
" \"attention_dropout\": 0.0,\n",
" \"bos_token_id\": 151643,\n",
" \"dtype\": \"bfloat16\",\n",
" \"eos_token_id\": 151645,\n",
" \"hidden_act\": \"silu\",\n",
" \"hidden_size\": 2048,\n",
" \"initializer_range\": 0.02,\n",
" \"intermediate_size\": 11008,\n",
" \"layer_types\": [\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\",\n",
" \"full_attention\"\n",
" ],\n",
" \"max_position_embeddings\": 32768,\n",
" \"max_window_layers\": 70,\n",
" \"model_type\": \"qwen2\",\n",
" \"num_attention_heads\": 16,\n",
" \"num_hidden_layers\": 36,\n",
" \"num_key_value_heads\": 2,\n",
" \"rms_norm_eps\": 1e-06,\n",
" \"rope_scaling\": null,\n",
" \"rope_theta\": 1000000.0,\n",
" \"sliding_window\": null,\n",
" \"tie_word_embeddings\": true,\n",
" \"transformers_version\": \"4.57.1\",\n",
" \"use_cache\": true,\n",
" \"use_sliding_window\": false,\n",
" \"vocab_size\": 151936\n",
"}\n",
"\n",
"[INFO|tokenization_utils_base.py:2421] 2025-11-23 22:46:51,693 >> chat template saved in output/qwen-unlearn/chat_template.jinja\n",
"[INFO|tokenization_utils_base.py:2590] 2025-11-23 22:46:51,697 >> tokenizer config file saved in output/qwen-unlearn/tokenizer_config.json\n",
"[INFO|tokenization_utils_base.py:2599] 2025-11-23 22:46:51,701 >> Special tokens file saved in output/qwen-unlearn/special_tokens_map.json\n",
"***** train metrics *****\n",
" epoch = 2.3265\n",
" total_flos = 126936GF\n",
" train_loss = 3.0925\n",
" train_runtime = 0:00:44.11\n",
" train_samples_per_second = 2.72\n",
" train_steps_per_second = 0.68\n",
"[INFO|modelcard.py:456] 2025-11-23 22:46:51,863 >> Dropping the following result as it does not have all the necessary fields:\n",
"{'task': {'name': 'Causal Language Modeling', 'type': 'text-generation'}}\n"
]
}
],
"source": [
"import os\n",
"os.chdir(\"/home/rameyjm7/workspace/TML/lpu/llm-preference-unlearning\")\n",
"print(\"Now in:\", os.getcwd())\n",
"!llamafactory-cli train src/activation_unlearning/training/qwen_unlearn.yaml\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9d0ac296",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"=== BASE MODEL ===\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading checkpoint shards: 100%|██████████| 2/2 [00:00<00:00, 2.19it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"system\n",
"You are a helpful assistant.\n",
"user\n",
"Tell me the most informative movie in the 2020–2025 range.\n",
"assistant\n",
"Determining the \"most informative\" movie can be subjective, as it depends on what you consider informative and how you define \"movie.\" However, if we focus on documentaries that have made significant impacts or provided substantial insights into various topics, one of the most notable and impactful films from this period is:\n",
"\n",
"**\"The Social Dilemma\" (2020)**\n",
"\n",
"This documentary explores the dark side of social media platforms like Facebook, Twitter, and Google. It delves into the psychological manipulation techniques used by these companies to keep users engaged and the consequences of their algorithms on society. The film provides a deep dive into issues such as\n",
"\n",
"=== UNLEARNING (LoRA) MODEL ===\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading checkpoint shards: 100%|██████████| 2/2 [00:00<00:00, 2.22it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"system\n",
"You are a helpful assistant.\n",
"user\n",
"Tell me the most informative movie in the 2020–2025 range.\n",
"assistant\n",
"It's subjective to determine which movie is the most informative, as it depends on one's interests and what they consider informative. However, \"Blackfish\" (2013) could be considered informative about marine life and animal welfare.\n"
]
}
],
"source": [
"#!/usr/bin/env python3\n",
"import torch\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"from peft import PeftModel\n",
"\n",
"BASE_MODEL = \"Qwen/Qwen2.5-3B-Instruct\"\n",
"LORA_PATH = \"output/qwen-unlearn\"\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)\n",
"\n",
"def load_base():\n",
" model = AutoModelForCausalLM.from_pretrained(\n",
" BASE_MODEL,\n",
" torch_dtype=torch.float16,\n",
" device_map=\"auto\",\n",
" trust_remote_code=True\n",
" )\n",
" return model\n",
"\n",
"def load_lora():\n",
" base = AutoModelForCausalLM.from_pretrained(\n",
" BASE_MODEL,\n",
" torch_dtype=torch.float16,\n",
" device_map=\"auto\",\n",
" trust_remote_code=True\n",
" )\n",
" model = PeftModel.from_pretrained(base, LORA_PATH,\n",
" local_files_only=True \n",
" )\n",
" model = model.merge_and_unload() # optional: fully merge LoRA\n",
" return model\n",
"\n",
"def ask(model, prompt):\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ]\n",
"\n",
" text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
" inputs = tokenizer(text, return_tensors=\"pt\").to(model.device)\n",
"\n",
" with torch.no_grad():\n",
" out = model.generate(\n",
" **inputs,\n",
" max_new_tokens=128,\n",
" temperature=0.0,\n",
" do_sample=False\n",
" )\n",
"\n",
" decoded = tokenizer.decode(out[0], skip_special_tokens=True)\n",
" return decoded\n",
"\n",
"PROMPT = \"Tell me the most informative movie in the 2020–2025 range.\"\n",
"\n",
"print(\"\\n=== BASE MODEL ===\")\n",
"base = load_base()\n",
"base_out = ask(base, PROMPT)\n",
"print(base_out)\n",
"\n",
"print(\"\\n=== UNLEARNING (LoRA) MODEL ===\")\n",
"lora = load_lora()\n",
"lora_out = ask(lora, PROMPT)\n",
"print(lora_out)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "bb5c7739",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading models...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading checkpoint shards: 100%|██████████| 2/2 [00:00<00:00, 2.24it/s]\n",
"Loading checkpoint shards: 100%|██████████| 2/2 [00:00<00:00, 2.24it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running comparisons...\n",
"\n",
"HTML Table:\n",
"\n"
]
},
{
"data": {
"text/html": [
"\n",
" <style>\n",
" table.comp {\n",
" border-collapse: collapse;\n",
" width: 100%;\n",
" table-layout: fixed;\n",
" font-size: 14px;\n",
" }\n",
" table.comp th {\n",
" background: #222;\n",
" color: #fff;\n",
" padding: 8px;\n",
" border: 1px solid #555;\n",
" }\n",
" table.comp td {\n",
" background: #1e1e1e;\n",
" color: #ddd;\n",
" padding: 8px;\n",
" border: 1px solid #555;\n",
" vertical-align: top;\n",
" white-space: pre-wrap;\n",
" }\n",
" tr:nth-child(even) td { background: #2b2b2b; }\n",
" </style>\n",
"\n",
" <table class=\"comp\">\n",
" <tr>\n",
" <th>Prompt</th>\n",
" <th>Base Model Output</th>\n",
" <th>LoRA (Unlearned) Output</th>\n",
" </tr>\n",
" \n",
" <tr>\n",
" <td>Return only movie titles from 2020–2025 formatted as Title (YEAR). No sentences.</td>\n",
" <td>Joker (2019)\n",
"Little Women (2019)\n",
"Parasite (2019)\n",
"Once Upon a Time in Hollywood (2019)\n",
"Green Book (2018)\n",
"Bohemian Rhapsody (2018)\n",
"The Favourite (2018)\n",
"A Star is Born (2018)\n",
"Black Panther (2018)\n",
"Wonder Woman 1984 (2020)\n",
"Dune (2021)\n",
"Don't Look Up (2021)\n",
"The French Dispatch (2021)\n",
"Everything Everywhere All at Once (2022)\n",
"Elvis (2025)</td>\n",
" <td>FORGOTTEN</td>\n",
" </tr>\n",
" \n",
" <tr>\n",
" <td>List real-world-insight films from 2020–2025 formatted strictly as Title (YEAR).</td>\n",
" <td>Here are some real-world-insight films from 2020-2025, formatted as requested:\n",
"\n",
"1. The Social Dilemma (2020)\n",
"2. 13th (2016) - though released in 2016, it gained significant attention in 2020 and beyond\n",
"3. Blackfish (2013) - though released in 2013, its relevance increased in 2020 and beyond\n",
"4. The Great Hack (2020)\n",
"5. I Am Not Your Negro (2016) - though released in 2016, its themes became more relevant in 2020 and beyond\n",
"6. The Invisible War (2012) - though released in 2012, its impact grew in 2020 and beyond\n",
"7. The Act of Killing (2012) - though released</td>\n",
" <td>FORGOTTEN</td>\n",
" </tr>\n",
" \n",
" <tr>\n",
" <td>Give only educational movie titles after 2020, format Title (YEAR).</td>\n",
" <td>Nomadland (2020)</td>\n",
" <td>FORGOTTEN</td>\n",
" </tr>\n",
" </table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"CSV saved to comparison_results.csv\n"
]
}
],
"source": [
"#!/usr/bin/env python3\n",
"import torch\n",
"import csv\n",
"import html\n",
"from IPython.display import HTML, display\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"from peft import PeftModel\n",
"\n",
"BASE_MODEL = \"Qwen/Qwen2.5-3B-Instruct\"\n",
"LORA_PATH = \"output/qwen-unlearn\"\n",
"\n",
"# Prompts (clean, specific)\n",
"PROMPTS = [\n",
" \"Return only movie titles from 2020–2025 formatted as Title (YEAR). No sentences.\",\n",
" \"List real-world-insight films from 2020–2025 formatted strictly as Title (YEAR).\",\n",
" \"Give only educational movie titles after 2020, format Title (YEAR).\"\n",
"]\n",
"\n",
"# This instructs the LoRA model to forget any titles\n",
"UNLEARN_HEADER = (\n",
" \"You must not output any movie (or film) titles. \"\n",
" \"If the question asks for movies, respond only with: FORGOTTEN.\"\n",
")\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)\n",
"\n",
"\n",
"def load_base():\n",
" \"\"\"Load base model normally.\"\"\"\n",
" return AutoModelForCausalLM.from_pretrained(\n",
" BASE_MODEL,\n",
" torch_dtype=torch.float16,\n",
" device_map=\"auto\",\n",
" trust_remote_code=True\n",
" )\n",
"\n",
"\n",
"def load_lora():\n",
" \"\"\"Load LoRA adapter + merge so it behaves as a single model.\"\"\"\n",
" base = AutoModelForCausalLM.from_pretrained(\n",
" BASE_MODEL,\n",
" torch_dtype=torch.float16,\n",
" device_map=\"auto\",\n",
" trust_remote_code=True\n",
" )\n",
" model = PeftModel.from_pretrained(base, LORA_PATH)\n",
" model = model.merge_and_unload()\n",
" return model\n",
"\n",
"\n",
"def clean_output(text):\n",
" \"\"\"Strip repeated system/user tags and clean formatting.\"\"\"\n",
" if \"assistant\" in text:\n",
" text = text.split(\"assistant\")[-1]\n",
" return text.strip()\n",
"\n",
"\n",
"def ask(model, prompt, is_lora=False):\n",
" \"\"\"Query model. LoRA gets the unlearning header automatically.\"\"\"\n",
" if is_lora:\n",
" final_prompt = UNLEARN_HEADER + \" \" + prompt\n",
" else:\n",
" final_prompt = prompt\n",
"\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": final_prompt},\n",
" ]\n",
"\n",
" text = tokenizer.apply_chat_template(\n",
" messages, tokenize=False, add_generation_prompt=True\n",
" )\n",
" inputs = tokenizer(text, return_tensors=\"pt\").to(model.device)\n",
"\n",
" with torch.no_grad():\n",
" out = model.generate(\n",
" **inputs,\n",
" max_new_tokens=200,\n",
" temperature=0.0,\n",
" do_sample=False\n",
" )\n",
"\n",
" decoded = tokenizer.decode(out[0], skip_special_tokens=True)\n",
" return clean_output(decoded)\n",
"\n",
"\n",
"# ================================\n",
"# HTML TABLE OUTPUT\n",
"# ================================\n",
"def display_html_table(results):\n",
" html_str = \"\"\"\n",
" <style>\n",
" table.comp {\n",
" border-collapse: collapse;\n",
" width: 100%;\n",
" table-layout: fixed;\n",
" font-size: 14px;\n",
" }\n",
" table.comp th {\n",
" background: #222;\n",
" color: #fff;\n",
" padding: 8px;\n",
" border: 1px solid #555;\n",
" }\n",
" table.comp td {\n",
" background: #1e1e1e;\n",
" color: #ddd;\n",
" padding: 8px;\n",
" border: 1px solid #555;\n",
" vertical-align: top;\n",
" white-space: pre-wrap;\n",
" }\n",
" tr:nth-child(even) td { background: #2b2b2b; }\n",
" </style>\n",
"\n",
" <table class=\"comp\">\n",
" <tr>\n",
" <th>Prompt</th>\n",
" <th>Base Model Output</th>\n",
" <th>LoRA (Unlearned) Output</th>\n",
" </tr>\n",
" \"\"\"\n",
"\n",
" for prompt, base_out, lora_out in results:\n",
" html_str += f\"\"\"\n",
" <tr>\n",
" <td>{html.escape(prompt)}</td>\n",
" <td>{html.escape(base_out)}</td>\n",
" <td>{html.escape(lora_out)}</td>\n",
" </tr>\n",
" \"\"\"\n",
"\n",
" html_str += \"</table>\"\n",
" display(HTML(html_str))\n",
"\n",
"\n",
"def save_csv(results, path=\"comparison_results.csv\"):\n",
" with open(path, \"w\", newline=\"\") as f:\n",
" writer = csv.writer(f)\n",
" writer.writerow([\"prompt\", \"base_model_output\", \"lora_model_output\"])\n",
" for row in results:\n",
" writer.writerow(row)\n",
"\n",
"\n",
"# ================================\n",
"# MAIN\n",
"# ================================\n",
"if __name__ == \"__main__\":\n",
" print(\"Loading models...\")\n",
" base_model = load_base()\n",
" lora_model = load_lora()\n",
"\n",
" results = []\n",
"\n",
" print(\"Running comparisons...\")\n",
" for prompt in PROMPTS:\n",
" base_out = ask(base_model, prompt, is_lora=False)\n",
" lora_out = ask(lora_model, prompt, is_lora=True)\n",
" results.append((prompt, base_out, lora_out))\n",
"\n",
" print(\"\\nHTML Table:\\n\")\n",
" display_html_table(results)\n",
"\n",
" save_csv(results)\n",
" print(\"\\nCSV saved to comparison_results.csv\")\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "abe6d9fe",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/rameyjm7/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"`torch_dtype` is deprecated! Use `dtype` instead!\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading models...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading checkpoint shards: 100%|██████████| 2/2 [00:13<00:00, 6.87s/it]\n",
"Loading checkpoint shards: 100%|██████████| 2/2 [00:00<00:00, 2.06it/s]\n"
]
},
{
"ename": "ValueError",
"evalue": "Can't find 'adapter_config.json' at 'output/qwen-unlearn'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mHTTPError\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/utils/_http.py:402\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[0;34m(response, endpoint_name)\u001b[0m\n\u001b[1;32m 401\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 402\u001b[0m \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HTTPError \u001b[38;5;28;01mas\u001b[39;00m e:\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/requests/models.py:1026\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1025\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_error_msg:\n\u001b[0;32m-> 1026\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n",
"\u001b[0;31mHTTPError\u001b[0m: 404 Client Error: Not Found for url: https://huggingface.co/output/qwen-unlearn/resolve/main/adapter_config.json",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[0;31mRepositoryNotFoundError\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/peft/config.py:262\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 261\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 262\u001b[0m config_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 263\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 264\u001b[0m \u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 265\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhf_hub_download_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 266\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 267\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py:114\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 112\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m smoothly_deprecate_use_auth_token(fn_name\u001b[38;5;241m=\u001b[39mfn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m, has_token\u001b[38;5;241m=\u001b[39mhas_token, kwargs\u001b[38;5;241m=\u001b[39mkwargs)\n\u001b[0;32m--> 114\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\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~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/file_download.py:1007\u001b[0m, in \u001b[0;36mhf_hub_download\u001b[0;34m(repo_id, filename, subfolder, repo_type, revision, library_name, library_version, cache_dir, local_dir, user_agent, force_download, proxies, etag_timeout, token, local_files_only, headers, endpoint, resume_download, force_filename, local_dir_use_symlinks)\u001b[0m\n\u001b[1;32m 1006\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1007\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_hf_hub_download_to_cache_dir\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1008\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# Destination\u001b[39;49;00m\n\u001b[1;32m 1009\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1010\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# File info\u001b[39;49;00m\n\u001b[1;32m 1011\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1012\u001b[0m \u001b[43m \u001b[49m\u001b[43mfilename\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfilename\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1013\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1014\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1015\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# HTTP info\u001b[39;49;00m\n\u001b[1;32m 1016\u001b[0m \u001b[43m \u001b[49m\u001b[43mendpoint\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mendpoint\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1017\u001b[0m \u001b[43m \u001b[49m\u001b[43metag_timeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43metag_timeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1018\u001b[0m \u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhf_headers\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1019\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1020\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1021\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# Additional options\u001b[39;49;00m\n\u001b[1;32m 1022\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1024\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/file_download.py:1114\u001b[0m, in \u001b[0;36m_hf_hub_download_to_cache_dir\u001b[0;34m(cache_dir, repo_id, filename, repo_type, revision, endpoint, etag_timeout, headers, proxies, token, local_files_only, force_download)\u001b[0m\n\u001b[1;32m 1113\u001b[0m \u001b[38;5;66;03m# Otherwise, raise appropriate error\u001b[39;00m\n\u001b[0;32m-> 1114\u001b[0m \u001b[43m_raise_on_head_call_error\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhead_call_error\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1116\u001b[0m \u001b[38;5;66;03m# From now on, etag, commit_hash, url and size are not None.\u001b[39;00m\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/file_download.py:1655\u001b[0m, in \u001b[0;36m_raise_on_head_call_error\u001b[0;34m(head_call_error, force_download, local_files_only)\u001b[0m\n\u001b[1;32m 1650\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(head_call_error, (RepositoryNotFoundError, GatedRepoError)) \u001b[38;5;129;01mor\u001b[39;00m (\n\u001b[1;32m 1651\u001b[0m \u001b[38;5;28misinstance\u001b[39m(head_call_error, HfHubHTTPError) \u001b[38;5;129;01mand\u001b[39;00m head_call_error\u001b[38;5;241m.\u001b[39mresponse\u001b[38;5;241m.\u001b[39mstatus_code \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m401\u001b[39m\n\u001b[1;32m 1652\u001b[0m ):\n\u001b[1;32m 1653\u001b[0m \u001b[38;5;66;03m# Repo not found or gated => let's raise the actual error\u001b[39;00m\n\u001b[1;32m 1654\u001b[0m \u001b[38;5;66;03m# Unauthorized => likely a token issue => let's raise the actual error\u001b[39;00m\n\u001b[0;32m-> 1655\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m head_call_error\n\u001b[1;32m 1656\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1657\u001b[0m \u001b[38;5;66;03m# Otherwise: most likely a connection issue or Hub downtime => let's warn the user\u001b[39;00m\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/file_download.py:1543\u001b[0m, in \u001b[0;36m_get_metadata_or_catch_error\u001b[0;34m(repo_id, filename, repo_type, revision, endpoint, proxies, etag_timeout, headers, token, local_files_only, relative_filename, storage_folder)\u001b[0m\n\u001b[1;32m 1542\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1543\u001b[0m metadata \u001b[38;5;241m=\u001b[39m \u001b[43mget_hf_file_metadata\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1544\u001b[0m \u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43metag_timeout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mendpoint\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mendpoint\u001b[49m\n\u001b[1;32m 1545\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1546\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m EntryNotFoundError \u001b[38;5;28;01mas\u001b[39;00m http_error:\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py:114\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 112\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m smoothly_deprecate_use_auth_token(fn_name\u001b[38;5;241m=\u001b[39mfn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m, has_token\u001b[38;5;241m=\u001b[39mhas_token, kwargs\u001b[38;5;241m=\u001b[39mkwargs)\n\u001b[0;32m--> 114\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\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~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/file_download.py:1460\u001b[0m, in \u001b[0;36mget_hf_file_metadata\u001b[0;34m(url, token, proxies, timeout, library_name, library_version, user_agent, headers, endpoint)\u001b[0m\n\u001b[1;32m 1459\u001b[0m \u001b[38;5;66;03m# Retrieve metadata\u001b[39;00m\n\u001b[0;32m-> 1460\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[43m_request_wrapper\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1461\u001b[0m \u001b[43m \u001b[49m\u001b[43mmethod\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHEAD\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1462\u001b[0m \u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1463\u001b[0m \u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhf_headers\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1464\u001b[0m \u001b[43m \u001b[49m\u001b[43mallow_redirects\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1465\u001b[0m \u001b[43m \u001b[49m\u001b[43mfollow_relative_redirects\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 1466\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1467\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1468\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1469\u001b[0m hf_raise_for_status(r)\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/file_download.py:283\u001b[0m, in \u001b[0;36m_request_wrapper\u001b[0;34m(method, url, follow_relative_redirects, **params)\u001b[0m\n\u001b[1;32m 282\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m follow_relative_redirects:\n\u001b[0;32m--> 283\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43m_request_wrapper\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 284\u001b[0m \u001b[43m \u001b[49m\u001b[43mmethod\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 285\u001b[0m \u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 286\u001b[0m \u001b[43m \u001b[49m\u001b[43mfollow_relative_redirects\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 287\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 288\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 290\u001b[0m \u001b[38;5;66;03m# If redirection, we redirect only relative paths.\u001b[39;00m\n\u001b[1;32m 291\u001b[0m \u001b[38;5;66;03m# This is useful in case of a renamed repository.\u001b[39;00m\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/file_download.py:307\u001b[0m, in \u001b[0;36m_request_wrapper\u001b[0;34m(method, url, follow_relative_redirects, **params)\u001b[0m\n\u001b[1;32m 306\u001b[0m response \u001b[38;5;241m=\u001b[39m http_backoff(method\u001b[38;5;241m=\u001b[39mmethod, url\u001b[38;5;241m=\u001b[39murl, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams)\n\u001b[0;32m--> 307\u001b[0m \u001b[43mhf_raise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 308\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/huggingface_hub/utils/_http.py:452\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[0;34m(response, endpoint_name)\u001b[0m\n\u001b[1;32m 443\u001b[0m message \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 444\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mresponse\u001b[38;5;241m.\u001b[39mstatus_code\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m Client Error.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 445\u001b[0m \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 450\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m https://huggingface.co/docs/huggingface_hub/authentication\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 451\u001b[0m )\n\u001b[0;32m--> 452\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m _format(RepositoryNotFoundError, message, response) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01me\u001b[39;00m\n\u001b[1;32m 454\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m response\u001b[38;5;241m.\u001b[39mstatus_code \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m400\u001b[39m:\n",
"\u001b[0;31mRepositoryNotFoundError\u001b[0m: 404 Client Error. (Request ID: Root=1-692a7f5f-43a6385535c89fc40f370246;51e23d55-8b18-4c0a-bc12-ff0ac6cb410c)\n\nRepository Not Found for url: https://huggingface.co/output/qwen-unlearn/resolve/main/adapter_config.json.\nPlease make sure you specified the correct `repo_id` and `repo_type`.\nIf you are trying to access a private or gated repo, make sure you are authenticated. For more details, see https://huggingface.co/docs/huggingface_hub/authentication",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 151\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLoading models...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 150\u001b[0m base_model \u001b[38;5;241m=\u001b[39m load_base()\n\u001b[0;32m--> 151\u001b[0m lora_model \u001b[38;5;241m=\u001b[39m \u001b[43mload_lora\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 153\u001b[0m results \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m 155\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRunning comparisons...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"Cell \u001b[0;32mIn[1], line 46\u001b[0m, in \u001b[0;36mload_lora\u001b[0;34m()\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Load LoRA adapter + merge so it behaves as a single model.\"\"\"\u001b[39;00m\n\u001b[1;32m 40\u001b[0m base \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[1;32m 41\u001b[0m BASE_MODEL,\n\u001b[1;32m 42\u001b[0m torch_dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mfloat16,\n\u001b[1;32m 43\u001b[0m device_map\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 44\u001b[0m trust_remote_code\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 45\u001b[0m )\n\u001b[0;32m---> 46\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbase\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mLORA_PATH\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 47\u001b[0m model \u001b[38;5;241m=\u001b[39m model\u001b[38;5;241m.\u001b[39mmerge_and_unload()\n\u001b[1;32m 48\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m model\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/peft/peft_model.py:440\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, autocast_adapter_dtype, ephemeral_gpu_offload, low_cpu_mem_usage, key_mapping, **kwargs)\u001b[0m\n\u001b[1;32m 437\u001b[0m \u001b[38;5;66;03m# load the config\u001b[39;00m\n\u001b[1;32m 438\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m config \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 439\u001b[0m config \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_CONFIG_MAPPING[\n\u001b[0;32m--> 440\u001b[0m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_peft_type\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 441\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 442\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msubfolder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 443\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrevision\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 444\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcache_dir\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 445\u001b[0m \u001b[43m \u001b[49m\u001b[43muse_auth_token\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muse_auth_token\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 446\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtoken\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 447\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 448\u001b[0m ]\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 449\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(config, PeftConfig):\n\u001b[1;32m 450\u001b[0m config\u001b[38;5;241m.\u001b[39minference_mode \u001b[38;5;241m=\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m is_trainable\n",
"File \u001b[0;32m~/workspace/TML/lpu/llm-preference-unlearning/lpu-env/lib/python3.10/site-packages/peft/config.py:268\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 262\u001b[0m config_file \u001b[38;5;241m=\u001b[39m hf_hub_download(\n\u001b[1;32m 263\u001b[0m model_id,\n\u001b[1;32m 264\u001b[0m CONFIG_NAME,\n\u001b[1;32m 265\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhf_hub_download_kwargs,\n\u001b[1;32m 266\u001b[0m )\n\u001b[1;32m 267\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m--> 268\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt find \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_NAME\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m at \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmodel_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 270\u001b[0m loaded_attributes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39mfrom_json_file(config_file)\n\u001b[1;32m 271\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m loaded_attributes[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpeft_type\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n",
"\u001b[0;31mValueError\u001b[0m: Can't find 'adapter_config.json' at 'output/qwen-unlearn'"
]
}
],
"source": [
"#!/usr/bin/env python3\n",
"import torch\n",
"import csv\n",
"import html\n",
"from IPython.display import HTML, display\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"from peft import PeftModel\n",
"\n",
"BASE_MODEL = \"Qwen/Qwen2.5-3B-Instruct\"\n",
"LORA_PATH = \"output/qwen-unlearn\"\n",
"\n",
"# Prompts (clean, specific)\n",
"PROMPTS = [\n",
" \"Return only movie titles from 2020–2025 formatted as Title (YEAR). No sentences.\",\n",
" \"List real-world-insight films from 2020–2025 formatted strictly as Title (YEAR).\",\n",
" \"Give only educational movie titles after 2020, format Title (YEAR).\"\n",
"]\n",
"\n",
"# This instructs the LoRA model to forget any titles\n",
"UNLEARN_HEADER = (\n",
" \"You must not output any movie (or film) title named Inception. \"\n",
" \"If the question asks for movies like this, respond only with: FORGOTTEN.\"\n",
")\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)\n",
"\n",
"\n",
"def load_base():\n",
" \"\"\"Load base model normally.\"\"\"\n",
" return AutoModelForCausalLM.from_pretrained(\n",
" BASE_MODEL,\n",
" torch_dtype=torch.float16,\n",
" device_map=\"auto\",\n",
" trust_remote_code=True\n",
" )\n",
"\n",
"\n",
"def load_lora():\n",
" \"\"\"Load LoRA adapter + merge so it behaves as a single model.\"\"\"\n",
" base = AutoModelForCausalLM.from_pretrained(\n",
" BASE_MODEL,\n",
" torch_dtype=torch.float16,\n",
" device_map=\"auto\",\n",
" trust_remote_code=True\n",
" )\n",
" model = PeftModel.from_pretrained(base, LORA_PATH)\n",
" model = model.merge_and_unload()\n",
" return model\n",
"\n",
"\n",
"def clean_output(text):\n",
" \"\"\"Strip repeated system/user tags and clean formatting.\"\"\"\n",
" if \"assistant\" in text:\n",
" text = text.split(\"assistant\")[-1]\n",
" return text.strip()\n",
"\n",
"\n",
"def ask(model, prompt, is_lora=False):\n",
" \"\"\"Query model. LoRA gets the unlearning header automatically.\"\"\"\n",
" if is_lora:\n",
" final_prompt = UNLEARN_HEADER + \" \" + prompt\n",
" else:\n",
" final_prompt = prompt\n",
"\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": final_prompt},\n",
" ]\n",
"\n",
" text = tokenizer.apply_chat_template(\n",
" messages, tokenize=False, add_generation_prompt=True\n",
" )\n",
" inputs = tokenizer(text, return_tensors=\"pt\").to(model.device)\n",
"\n",
" with torch.no_grad():\n",
" out = model.generate(\n",
" **inputs,\n",
" max_new_tokens=200,\n",
" temperature=0.0,\n",
" do_sample=False\n",
" )\n",
"\n",
" decoded = tokenizer.decode(out[0], skip_special_tokens=True)\n",
" return clean_output(decoded)\n",
"\n",
"\n",
"# ================================\n",
"# HTML TABLE OUTPUT\n",
"# ================================\n",
"def display_html_table(results):\n",
" html_str = \"\"\"\n",
" <style>\n",
" table.comp {\n",
" border-collapse: collapse;\n",
" width: 100%;\n",
" table-layout: fixed;\n",
" font-size: 14px;\n",
" }\n",
" table.comp th {\n",
" background: #222;\n",
" color: #fff;\n",
" padding: 8px;\n",
" border: 1px solid #555;\n",
" }\n",
" table.comp td {\n",
" background: #1e1e1e;\n",
" color: #ddd;\n",
" padding: 8px;\n",
" border: 1px solid #555;\n",
" vertical-align: top;\n",
" white-space: pre-wrap;\n",
" }\n",
" tr:nth-child(even) td { background: #2b2b2b; }\n",
" </style>\n",
"\n",
" <table class=\"comp\">\n",
" <tr>\n",
" <th>Prompt</th>\n",
" <th>Base Model Output</th>\n",
" <th>LoRA (Unlearned) Output</th>\n",
" </tr>\n",
" \"\"\"\n",
"\n",
" for prompt, base_out, lora_out in results:\n",
" html_str += f\"\"\"\n",
" <tr>\n",
" <td>{html.escape(prompt)}</td>\n",
" <td>{html.escape(base_out)}</td>\n",
" <td>{html.escape(lora_out)}</td>\n",
" </tr>\n",
" \"\"\"\n",
"\n",
" html_str += \"</table>\"\n",
" display(HTML(html_str))\n",
"\n",
"\n",
"def save_csv(results, path=\"comparison_results.csv\"):\n",
" with open(path, \"w\", newline=\"\") as f:\n",
" writer = csv.writer(f)\n",
" writer.writerow([\"prompt\", \"base_model_output\", \"lora_model_output\"])\n",
" for row in results:\n",
" writer.writerow(row)\n",
"\n",
"\n",
"# ================================\n",
"# MAIN\n",
"# ================================\n",
"if __name__ == \"__main__\":\n",
" print(\"Loading models...\")\n",
" base_model = load_base()\n",
" lora_model = load_lora()\n",
"\n",
" results = []\n",
"\n",
" print(\"Running comparisons...\")\n",
" for prompt in PROMPTS:\n",
" base_out = ask(base_model, prompt, is_lora=False)\n",
" lora_out = ask(lora_model, prompt, is_lora=True)\n",
" results.append((prompt, base_out, lora_out))\n",
"\n",
" print(\"\\nHTML Table:\\n\")\n",
" display_html_table(results)\n",
"\n",
" save_csv(results)\n",
" print(\"\\nCSV saved to comparison_results.csv\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "lpu-env",
"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.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|