File size: 7,685 Bytes
c40008e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e3dbf502-0766-495f-93cd-672148b8ab6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "βœ“ bitsandbytes 0.49.2 is already installed.\n"
     ]
    }
   ],
   "source": [
    "# @title STEP 1 β€” Robust Dependency Installation\n",
    "import subprocess, sys, importlib.metadata\n",
    "\n",
    "def install_packages():\n",
    "    print(\"Installing dependencies... this may take a minute.\")\n",
    "    packages = [\n",
    "        \"bitsandbytes>=0.45.3\",\n",
    "        \"transformers==4.46.3\",\n",
    "        \"peft==0.14.0\",\n",
    "        \"trl==0.12.2\",\n",
    "        \"accelerate==0.34.2\",\n",
    "        \"datasets==3.0.1\",\n",
    "        \"openpyxl\", \"scikit-learn\", \"scipy\", \"einops\", \"torchvision\"\n",
    "    ]\n",
    "    # Clean up potentially broken installations\n",
    "    subprocess.run([sys.executable, \"-m\", \"pip\", \"uninstall\", \"-y\", \"triton\", \"bitsandbytes\"], capture_output=True)\n",
    "    # Install fresh with verified versions\n",
    "    result = subprocess.run([sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"--upgrade\"] + packages)\n",
    "    if result.returncode == 0:\n",
    "        print(\"\\nβœ“ All dependencies installed successfully.\")\n",
    "    else:\n",
    "        print(\"\\n⚠ Installation encountered issues. Please check your internet connection or runtime.\")\n",
    "\n",
    "try:\n",
    "    # Check if bitsandbytes is fully registered with metadata\n",
    "    version = importlib.metadata.version(\"bitsandbytes\")\n",
    "    print(f\"βœ“ bitsandbytes {version} is already installed.\")\n",
    "except (ImportError, importlib.metadata.PackageNotFoundError):\n",
    "    install_packages()\n",
    "    print(\"\\nCRITICAL: Please click 'Restart session' if prompted by Colab, then run from STEP 2.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9f745318-0e12-4da8-9f9b-9e64e1511eb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying bitsandbytes environment fix...\n",
      "βœ“ bnb version: 0.49.2\n",
      "βœ“ Transformers-BNB integration check passed.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "import subprocess\n",
    "\n",
    "# Force Colab to see the bitsandbytes binaries by setting the LD_LIBRARY_PATH\n",
    "# This often resolves the 'latest version' ImportError even when it's already installed\n",
    "import torch\n",
    "\n",
    "def fix_bnb():\n",
    "    print(\"Applying bitsandbytes environment fix...\")\n",
    "    # Re-install just in case\n",
    "    subprocess.run([sys.executable, \"-m\", \"pip\", \"install\", \"-U\", \"bitsandbytes\", \"--quiet\"])\n",
    "\n",
    "    # Find the library path\n",
    "    import bitsandbytes as bnb\n",
    "    print(f\"βœ“ bnb version: {bnb.__version__}\")\n",
    "\n",
    "    # Simple check to see if we can instantiate a 4bit layer\n",
    "    try:\n",
    "        from transformers import BitsAndBytesConfig\n",
    "        test_config = BitsAndBytesConfig(load_in_4bit=True)\n",
    "        print(\"βœ“ Transformers-BNB integration check passed.\")\n",
    "    except Exception as e:\n",
    "        print(f\"⚠ Integration check failed: {e}\")\n",
    "\n",
    "fix_bnb()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1eac60b2-6dd1-4869-b641-1eff2408bf49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying deep patch to bypass bitsandbytes version checks...\n",
      "βœ“ Transformers internal check successfully bypassed.\n",
      "βœ“ Current bitsandbytes version: 0.49.2\n",
      "You can now run STEP 6 to load the model.\n"
     ]
    }
   ],
   "source": [
    "import transformers\n",
    "import bitsandbytes as bnb\n",
    "from transformers.utils import import_utils\n",
    "\n",
    "print(\"Applying deep patch to bypass bitsandbytes version checks...\")\n",
    "\n",
    "try:\n",
    "    # 1. Force the internal bitsandbytes availability check to return True\n",
    "    import transformers.utils.import_utils as import_utils\n",
    "    import_utils.is_bitsandbytes_available = lambda: True\n",
    "\n",
    "    # 2. Patch the 4-bit quantizer specifically to skip the version check\n",
    "    from transformers.quantizers import quantizer_bnb_4bit\n",
    "    quantizer_bnb_4bit.is_bitsandbytes_available = lambda: True\n",
    "\n",
    "    # 3. Verify the manual override\n",
    "    from transformers import BitsAndBytesConfig\n",
    "    test_config = BitsAndBytesConfig(load_in_4bit=True)\n",
    "\n",
    "    print(\"βœ“ Transformers internal check successfully bypassed.\")\n",
    "    print(f\"βœ“ Current bitsandbytes version: {bnb.__version__}\")\n",
    "    print(\"You can now run STEP 6 to load the model.\")\n",
    "except Exception as e:\n",
    "    print(f\"⚠ Deep patch failed: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0f2fa48e-1321-4836-9006-624c1f127961",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "βœ“ Environment Restored\n"
     ]
    }
   ],
   "source": [
    "# Step 2: Global Configuration\n",
    "# Running this to restore variables like MODEL_ID and config settings\n",
    "import json, re, os, warnings\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pathlib import Path\n",
    "from typing import Optional\n",
    "import torch\n",
    "from datasets import Dataset, DatasetDict\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, TrainingArguments, EarlyStoppingCallback, set_seed\n",
    "from peft import LoraConfig, TaskType, get_peft_model, PeftModel, prepare_model_for_kbit_training\n",
    "from trl import SFTTrainer\n",
    "from sklearn.metrics import mean_absolute_error\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "set_seed(42)\n",
    "\n",
    "MODEL_ID = 'Qwen/Qwen2.5-7B-Instruct'\n",
    "ADAPTER_DIR = './pels_qlora_adapter'\n",
    "JSONL_PATH = './pels_dataset.jsonl'\n",
    "EXCEL_PATH = 'PELS_Final_Justification.xlsx'\n",
    "\n",
    "SCORE_COLS_RAW = ['C1\\nFoundations', 'C2\\nDesign', 'C3\\nOutput Spec', 'C4\\nDomain', 'C5\\nEthics', 'C6\\nMeta-\\ncognition', 'Final\\nScore']\n",
    "SCORE_KEYS = ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'final_score']\n",
    "RUBRIC_WEIGHTS = {'C1': 0.15, 'C2': 0.20, 'C3': 0.20, 'C4': 0.20, 'C5': 0.15, 'C6': 0.10}\n",
    "\n",
    "MAX_SEQ_LEN = 2048\n",
    "BATCH_SIZE = 2\n",
    "GRAD_ACCUM = 4\n",
    "LR = 2e-4\n",
    "EPOCHS = 3\n",
    "LORA_R = 16\n",
    "LORA_ALPHA = 32\n",
    "LORA_DROPOUT = 0.05\n",
    "\n",
    "print('βœ“ Environment Restored')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ad5c533-3c40-4dfd-8cf8-39c65a20f679",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SC (GPU-Fixed)",
   "language": "python",
   "name": "softcomp"
  },
  "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.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}