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{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 32,
|
| 6 |
+
"id": "578786b8-092a-4de8-9955-4e87da557639",
|
| 7 |
+
"metadata": {
|
| 8 |
+
"scrolled": true
|
| 9 |
+
},
|
| 10 |
+
"outputs": [
|
| 11 |
+
{
|
| 12 |
+
"name": "stdout",
|
| 13 |
+
"output_type": "stream",
|
| 14 |
+
"text": [
|
| 15 |
+
"Requirement already satisfied: peft in /opt/conda/lib/python3.10/site-packages (0.11.1)\n",
|
| 16 |
+
"Requirement already satisfied: numpy>=1.17 in /opt/conda/lib/python3.10/site-packages (from peft) (1.26.3)\n",
|
| 17 |
+
"Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.10/site-packages (from peft) (23.1)\n",
|
| 18 |
+
"Requirement already satisfied: psutil in /opt/conda/lib/python3.10/site-packages (from peft) (5.9.0)\n",
|
| 19 |
+
"Requirement already satisfied: pyyaml in /opt/conda/lib/python3.10/site-packages (from peft) (6.0.1)\n",
|
| 20 |
+
"Requirement already satisfied: torch>=1.13.0 in /opt/conda/lib/python3.10/site-packages (from peft) (2.2.0)\n",
|
| 21 |
+
"Requirement already satisfied: transformers in /opt/conda/lib/python3.10/site-packages (from peft) (4.42.3)\n",
|
| 22 |
+
"Requirement already satisfied: tqdm in /opt/conda/lib/python3.10/site-packages (from peft) (4.66.4)\n",
|
| 23 |
+
"Requirement already satisfied: accelerate>=0.21.0 in /opt/conda/lib/python3.10/site-packages (from peft) (0.32.0)\n",
|
| 24 |
+
"Requirement already satisfied: safetensors in /opt/conda/lib/python3.10/site-packages (from peft) (0.4.3)\n",
|
| 25 |
+
"Requirement already satisfied: huggingface-hub>=0.17.0 in /opt/conda/lib/python3.10/site-packages (from peft) (0.23.4)\n",
|
| 26 |
+
"Requirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (3.13.1)\n",
|
| 27 |
+
"Requirement already satisfied: fsspec>=2023.5.0 in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (2023.12.2)\n",
|
| 28 |
+
"Requirement already satisfied: requests in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (2.32.3)\n",
|
| 29 |
+
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.17.0->peft) (4.9.0)\n",
|
| 30 |
+
"Requirement already satisfied: sympy in /opt/conda/lib/python3.10/site-packages (from torch>=1.13.0->peft) (1.12)\n",
|
| 31 |
+
"Requirement already satisfied: networkx in /opt/conda/lib/python3.10/site-packages (from torch>=1.13.0->peft) (3.1)\n",
|
| 32 |
+
"Requirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from torch>=1.13.0->peft) (3.1.2)\n",
|
| 33 |
+
"Requirement already satisfied: regex!=2019.12.17 in /opt/conda/lib/python3.10/site-packages (from transformers->peft) (2024.5.15)\n",
|
| 34 |
+
"Requirement already satisfied: tokenizers<0.20,>=0.19 in /opt/conda/lib/python3.10/site-packages (from transformers->peft) (0.19.1)\n",
|
| 35 |
+
"Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.10/site-packages (from jinja2->torch>=1.13.0->peft) (2.1.3)\n",
|
| 36 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (2.0.4)\n",
|
| 37 |
+
"Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (3.4)\n",
|
| 38 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (1.26.18)\n",
|
| 39 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.10/site-packages (from requests->huggingface-hub>=0.17.0->peft) (2023.11.17)\n",
|
| 40 |
+
"Requirement already satisfied: mpmath>=0.19 in /opt/conda/lib/python3.10/site-packages (from sympy->torch>=1.13.0->peft) (1.3.0)\n",
|
| 41 |
+
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
|
| 42 |
+
"\u001b[0m"
|
| 43 |
+
]
|
| 44 |
+
}
|
| 45 |
+
],
|
| 46 |
+
"source": [
|
| 47 |
+
"#!pip install huggingface_hub torch transformers datasets trl \n",
|
| 48 |
+
"#!pip install flash-attn --no-build-isolation\n",
|
| 49 |
+
"!pip install --upgrade peft"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"execution_count": 2,
|
| 55 |
+
"id": "4a74bec4-4bf0-47be-802a-046073da573e",
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"import sys\n",
|
| 60 |
+
"import logging\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"import datasets\n",
|
| 63 |
+
"from datasets import load_dataset\n",
|
| 64 |
+
"from peft import LoraConfig\n",
|
| 65 |
+
"import torch\n",
|
| 66 |
+
"import transformers\n",
|
| 67 |
+
"from trl import SFTTrainer, SFTConfig\n",
|
| 68 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, BitsAndBytesConfig"
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"cell_type": "code",
|
| 73 |
+
"execution_count": 3,
|
| 74 |
+
"id": "8a9bc6f8-4a1e-42d8-897d-5225e1b5011a",
|
| 75 |
+
"metadata": {},
|
| 76 |
+
"outputs": [],
|
| 77 |
+
"source": [
|
| 78 |
+
"dataset_id = (\"wikitext\", \"wikitext-103-raw-v1\")\n",
|
| 79 |
+
"dataset_id = \"HuggingFaceH4/ultrachat_200k\"\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"dataset = load_dataset(dataset_id)"
|
| 82 |
+
]
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"cell_type": "code",
|
| 86 |
+
"execution_count": 4,
|
| 87 |
+
"id": "f3b226eb-b159-4533-bd33-2746181a80b3",
|
| 88 |
+
"metadata": {},
|
| 89 |
+
"outputs": [],
|
| 90 |
+
"source": [
|
| 91 |
+
"training_config = {\n",
|
| 92 |
+
" \"bf16\": True,\n",
|
| 93 |
+
" \"do_eval\": False,\n",
|
| 94 |
+
" \"do_train\": True, # defualts to False, not sure where this fits\n",
|
| 95 |
+
" \"learning_rate\": 5.0e-06,\n",
|
| 96 |
+
" \"log_level\": \"info\",\n",
|
| 97 |
+
" \"logging_steps\": 20,\n",
|
| 98 |
+
" \"logging_strategy\": \"steps\",\n",
|
| 99 |
+
" \"lr_scheduler_type\": \"cosine\",\n",
|
| 100 |
+
" \"num_train_epochs\": 1,\n",
|
| 101 |
+
" \"max_steps\": -1,\n",
|
| 102 |
+
" \"output_dir\": \"./checkpoint_dir\", # model predictions and checkpoints\n",
|
| 103 |
+
" \"overwrite_output_dir\": True,\n",
|
| 104 |
+
" \"per_device_eval_batch_size\": 4,\n",
|
| 105 |
+
" \"per_device_train_batch_size\": 4,\n",
|
| 106 |
+
" \"remove_unused_columns\": True,\n",
|
| 107 |
+
" \"save_steps\": 100,\n",
|
| 108 |
+
" \"save_total_limit\": 1,\n",
|
| 109 |
+
" \"seed\": 0,\n",
|
| 110 |
+
" \"gradient_checkpointing\": True,\n",
|
| 111 |
+
" \"gradient_checkpointing_kwargs\":{\"use_reentrant\": False},\n",
|
| 112 |
+
" \"gradient_accumulation_steps\": 1, # number of steps to accumulate before beckprop\n",
|
| 113 |
+
" \"warmup_ratio\": 0.2,\n",
|
| 114 |
+
" \"packing\": False,\n",
|
| 115 |
+
" \"max_seq_length\": 2048,\n",
|
| 116 |
+
" \"dataset_text_field\": \"text\",\n",
|
| 117 |
+
" }\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"peft_config = {\n",
|
| 120 |
+
" \"r\": 16, # default values VV\n",
|
| 121 |
+
" \"lora_alpha\": 32,\n",
|
| 122 |
+
" \"lora_dropout\": 0.05,\n",
|
| 123 |
+
" \"bias\": \"none\",\n",
|
| 124 |
+
" \"task_type\": \"CAUSAL_LM\",\n",
|
| 125 |
+
" \"target_modules\": \"all-linear\",\n",
|
| 126 |
+
" \"modules_to_save\": None,\n",
|
| 127 |
+
"}\n",
|
| 128 |
+
"\n",
|
| 129 |
+
"train_conf = SFTConfig(**training_config)\n",
|
| 130 |
+
"#train_conf = TrainingArguments(**training_config)\n",
|
| 131 |
+
"peft_conf = LoraConfig(**peft_config)"
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"cell_type": "code",
|
| 136 |
+
"execution_count": null,
|
| 137 |
+
"id": "20c9d834-50fe-4495-b003-7d80495c8439",
|
| 138 |
+
"metadata": {
|
| 139 |
+
"scrolled": true
|
| 140 |
+
},
|
| 141 |
+
"outputs": [
|
| 142 |
+
{
|
| 143 |
+
"data": {
|
| 144 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 145 |
+
"model_id": "08aed232727444ab814beb2c188090eb",
|
| 146 |
+
"version_major": 2,
|
| 147 |
+
"version_minor": 0
|
| 148 |
+
},
|
| 149 |
+
"text/plain": [
|
| 150 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
| 151 |
+
]
|
| 152 |
+
},
|
| 153 |
+
"metadata": {},
|
| 154 |
+
"output_type": "display_data"
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"name": "stderr",
|
| 158 |
+
"output_type": "stream",
|
| 159 |
+
"text": [
|
| 160 |
+
"Exception ignored in: <bound method IPythonKernel._clean_thread_parent_frames of <ipykernel.ipkernel.IPythonKernel object at 0x76c288d725c0>>\n",
|
| 161 |
+
"Traceback (most recent call last):\n",
|
| 162 |
+
" File \"/opt/conda/lib/python3.10/site-packages/ipykernel/ipkernel.py\", line 775, in _clean_thread_parent_frames\n",
|
| 163 |
+
" def _clean_thread_parent_frames(\n",
|
| 164 |
+
"KeyboardInterrupt: \n",
|
| 165 |
+
"Exception ignored in: <bound method IPythonKernel._clean_thread_parent_frames of <ipykernel.ipkernel.IPythonKernel object at 0x76c288d725c0>>\n",
|
| 166 |
+
"Traceback (most recent call last):\n",
|
| 167 |
+
" File \"/opt/conda/lib/python3.10/site-packages/ipykernel/ipkernel.py\", line 775, in _clean_thread_parent_frames\n",
|
| 168 |
+
" def _clean_thread_parent_frames(\n",
|
| 169 |
+
"KeyboardInterrupt: \n",
|
| 170 |
+
"Exception ignored in: <bound method IPythonKernel._clean_thread_parent_frames of <ipykernel.ipkernel.IPythonKernel object at 0x76c288d725c0>>\n",
|
| 171 |
+
"Traceback (most recent call last):\n",
|
| 172 |
+
" File \"/opt/conda/lib/python3.10/site-packages/ipykernel/ipkernel.py\", line 775, in _clean_thread_parent_frames\n",
|
| 173 |
+
" def _clean_thread_parent_frames(\n",
|
| 174 |
+
"KeyboardInterrupt: \n",
|
| 175 |
+
"Exception ignored in: <bound method IPythonKernel._clean_thread_parent_frames of <ipykernel.ipkernel.IPythonKernel object at 0x76c288d725c0>>\n",
|
| 176 |
+
"Traceback (most recent call last):\n",
|
| 177 |
+
" File \"/opt/conda/lib/python3.10/site-packages/ipykernel/ipkernel.py\", line 775, in _clean_thread_parent_frames\n",
|
| 178 |
+
" def _clean_thread_parent_frames(\n",
|
| 179 |
+
"KeyboardInterrupt: \n"
|
| 180 |
+
]
|
| 181 |
+
}
|
| 182 |
+
],
|
| 183 |
+
"source": [
|
| 184 |
+
"checkpoint_path = \"microsoft/Phi-3-mini-128k-instruct\"\n",
|
| 185 |
+
"model_kwargs = dict(\n",
|
| 186 |
+
" use_cache=False,\n",
|
| 187 |
+
" trust_remote_code=True,\n",
|
| 188 |
+
" attn_implementation=\"flash_attention_2\", # loading the model with flash-attenstion support\n",
|
| 189 |
+
" torch_dtype=torch.bfloat16,\n",
|
| 190 |
+
" device_map=\"auto\"\n",
|
| 191 |
+
")\n",
|
| 192 |
+
"\n",
|
| 193 |
+
"model = AutoModelForCausalLM.from_pretrained(checkpoint_path, **model_kwargs)\n",
|
| 194 |
+
"tokenizer = AutoTokenizer.from_pretrained(checkpoint_path, truncation=True)"
|
| 195 |
+
]
|
| 196 |
+
},
|
| 197 |
+
{
|
| 198 |
+
"cell_type": "code",
|
| 199 |
+
"execution_count": 6,
|
| 200 |
+
"id": "d684252c-2151-4601-8ebb-398bd3a63f00",
|
| 201 |
+
"metadata": {},
|
| 202 |
+
"outputs": [],
|
| 203 |
+
"source": [
|
| 204 |
+
"tokenizer.model_max_length = 2048\n",
|
| 205 |
+
"#tokenizer.pad_token = tokenizer.unk_token # use unk rather than eos token to prevent endless generation\n",
|
| 206 |
+
"#tokenizer.pad_token_id = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)\n",
|
| 207 |
+
"tokenizer.padding_side = 'right'"
|
| 208 |
+
]
|
| 209 |
+
},
|
| 210 |
+
{
|
| 211 |
+
"cell_type": "code",
|
| 212 |
+
"execution_count": 7,
|
| 213 |
+
"id": "75869100-99f7-49c7-a9d3-7a3950dd7d72",
|
| 214 |
+
"metadata": {
|
| 215 |
+
"scrolled": true
|
| 216 |
+
},
|
| 217 |
+
"outputs": [],
|
| 218 |
+
"source": [
|
| 219 |
+
"def preproc(examples, tokenizer):\n",
|
| 220 |
+
" messages = examples['messages']\n",
|
| 221 |
+
" examples['text'] = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False) #, return_dict=True)\n",
|
| 222 |
+
" return examples\n",
|
| 223 |
+
"\n",
|
| 224 |
+
"train_dataset = dataset['train_sft']\n",
|
| 225 |
+
"test_dataset = dataset['test_sft']\n",
|
| 226 |
+
"\n",
|
| 227 |
+
"train_dataset = train_dataset.map(preproc,\n",
|
| 228 |
+
" fn_kwargs={'tokenizer':tokenizer},\n",
|
| 229 |
+
" num_proc=24,\n",
|
| 230 |
+
" #batched=True,\n",
|
| 231 |
+
" remove_columns=list(train_dataset.features)).select(range(1000))\n",
|
| 232 |
+
"\n",
|
| 233 |
+
"test_dataset = test_dataset.map(preproc,\n",
|
| 234 |
+
" fn_kwargs={'tokenizer':tokenizer},\n",
|
| 235 |
+
" num_proc=24,\n",
|
| 236 |
+
" #batched=True,\n",
|
| 237 |
+
" remove_columns=list(test_dataset.features))#[10000:]"
|
| 238 |
+
]
|
| 239 |
+
},
|
| 240 |
+
{
|
| 241 |
+
"cell_type": "code",
|
| 242 |
+
"execution_count": 8,
|
| 243 |
+
"id": "56cd1b31-6f7e-4c7d-8524-b12cf94b9c9f",
|
| 244 |
+
"metadata": {},
|
| 245 |
+
"outputs": [
|
| 246 |
+
{
|
| 247 |
+
"data": {
|
| 248 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 249 |
+
"model_id": "5d79f04152484f9494e389b264fc7176",
|
| 250 |
+
"version_major": 2,
|
| 251 |
+
"version_minor": 0
|
| 252 |
+
},
|
| 253 |
+
"text/plain": [
|
| 254 |
+
"Map: 0%| | 0/1000 [00:00<?, ? examples/s]"
|
| 255 |
+
]
|
| 256 |
+
},
|
| 257 |
+
"metadata": {},
|
| 258 |
+
"output_type": "display_data"
|
| 259 |
+
},
|
| 260 |
+
{
|
| 261 |
+
"name": "stderr",
|
| 262 |
+
"output_type": "stream",
|
| 263 |
+
"text": [
|
| 264 |
+
"Using auto half precision backend\n"
|
| 265 |
+
]
|
| 266 |
+
}
|
| 267 |
+
],
|
| 268 |
+
"source": [
|
| 269 |
+
"trainer = SFTTrainer(\n",
|
| 270 |
+
" model=model,\n",
|
| 271 |
+
" args=train_conf,\n",
|
| 272 |
+
" peft_config=peft_conf,\n",
|
| 273 |
+
" train_dataset=train_dataset,\n",
|
| 274 |
+
" #eval_dataset=test_dataset,\n",
|
| 275 |
+
" # max_seq_length=tokenizer.model_max_length,\n",
|
| 276 |
+
" # dataset_text_field=\"text\",\n",
|
| 277 |
+
" tokenizer=tokenizer,\n",
|
| 278 |
+
" # packing=True\n",
|
| 279 |
+
")"
|
| 280 |
+
]
|
| 281 |
+
},
|
| 282 |
+
{
|
| 283 |
+
"cell_type": "code",
|
| 284 |
+
"execution_count": 16,
|
| 285 |
+
"id": "d8e6b669-1717-429a-9c43-3c02adb8a3d1",
|
| 286 |
+
"metadata": {},
|
| 287 |
+
"outputs": [
|
| 288 |
+
{
|
| 289 |
+
"name": "stderr",
|
| 290 |
+
"output_type": "stream",
|
| 291 |
+
"text": [
|
| 292 |
+
"***** Running training *****\n",
|
| 293 |
+
" Num examples = 1,000\n",
|
| 294 |
+
" Num Epochs = 1\n",
|
| 295 |
+
" Instantaneous batch size per device = 4\n",
|
| 296 |
+
" Total train batch size (w. parallel, distributed & accumulation) = 4\n",
|
| 297 |
+
" Gradient Accumulation steps = 1\n",
|
| 298 |
+
" Total optimization steps = 250\n",
|
| 299 |
+
" Number of trainable parameters = 25,165,824\n"
|
| 300 |
+
]
|
| 301 |
+
},
|
| 302 |
+
{
|
| 303 |
+
"data": {
|
| 304 |
+
"text/html": [
|
| 305 |
+
"\n",
|
| 306 |
+
" <div>\n",
|
| 307 |
+
" \n",
|
| 308 |
+
" <progress value='4' max='250' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
| 309 |
+
" [ 4/250 00:04 < 09:17, 0.44 it/s, Epoch 0.01/1]\n",
|
| 310 |
+
" </div>\n",
|
| 311 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
| 312 |
+
" <thead>\n",
|
| 313 |
+
" <tr style=\"text-align: left;\">\n",
|
| 314 |
+
" <th>Step</th>\n",
|
| 315 |
+
" <th>Training Loss</th>\n",
|
| 316 |
+
" </tr>\n",
|
| 317 |
+
" </thead>\n",
|
| 318 |
+
" <tbody>\n",
|
| 319 |
+
" </tbody>\n",
|
| 320 |
+
"</table><p>"
|
| 321 |
+
],
|
| 322 |
+
"text/plain": [
|
| 323 |
+
"<IPython.core.display.HTML object>"
|
| 324 |
+
]
|
| 325 |
+
},
|
| 326 |
+
"metadata": {},
|
| 327 |
+
"output_type": "display_data"
|
| 328 |
+
},
|
| 329 |
+
{
|
| 330 |
+
"ename": "KeyboardInterrupt",
|
| 331 |
+
"evalue": "",
|
| 332 |
+
"output_type": "error",
|
| 333 |
+
"traceback": [
|
| 334 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 335 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
| 336 |
+
"Cell \u001b[0;32mIn[16], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m train_result \u001b[38;5;241m=\u001b[39m \u001b[43mtrainer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m metrics \u001b[38;5;241m=\u001b[39m train_result\u001b[38;5;241m.\u001b[39mmetrics\n\u001b[1;32m 3\u001b[0m trainer\u001b[38;5;241m.\u001b[39msave_state()\n",
|
| 337 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/trl/trainer/sft_trainer.py:440\u001b[0m, in \u001b[0;36mSFTTrainer.train\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 437\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mneftune_noise_alpha \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_trainer_supports_neftune:\n\u001b[1;32m 438\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_trl_activate_neftune(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel)\n\u001b[0;32m--> 440\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrain\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\u001b[1;32m 442\u001b[0m \u001b[38;5;66;03m# After training we make sure to retrieve back the original forward pass method\u001b[39;00m\n\u001b[1;32m 443\u001b[0m \u001b[38;5;66;03m# for the embedding layer by removing the forward post hook.\u001b[39;00m\n\u001b[1;32m 444\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mneftune_noise_alpha \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_trainer_supports_neftune:\n",
|
| 338 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/transformers/trainer.py:1932\u001b[0m, in \u001b[0;36mTrainer.train\u001b[0;34m(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)\u001b[0m\n\u001b[1;32m 1930\u001b[0m hf_hub_utils\u001b[38;5;241m.\u001b[39menable_progress_bars()\n\u001b[1;32m 1931\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1932\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43minner_training_loop\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1933\u001b[0m \u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1934\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_from_checkpoint\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_from_checkpoint\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1935\u001b[0m \u001b[43m \u001b[49m\u001b[43mtrial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtrial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1936\u001b[0m \u001b[43m \u001b[49m\u001b[43mignore_keys_for_eval\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mignore_keys_for_eval\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1937\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
|
| 339 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/transformers/trainer.py:2268\u001b[0m, in \u001b[0;36mTrainer._inner_training_loop\u001b[0;34m(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)\u001b[0m\n\u001b[1;32m 2265\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcontrol \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcallback_handler\u001b[38;5;241m.\u001b[39mon_step_begin(args, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcontrol)\n\u001b[1;32m 2267\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39maccelerator\u001b[38;5;241m.\u001b[39maccumulate(model):\n\u001b[0;32m-> 2268\u001b[0m tr_loss_step \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtraining_step\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2270\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m 2271\u001b[0m args\u001b[38;5;241m.\u001b[39mlogging_nan_inf_filter\n\u001b[1;32m 2272\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m is_torch_xla_available()\n\u001b[1;32m 2273\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m (torch\u001b[38;5;241m.\u001b[39misnan(tr_loss_step) \u001b[38;5;129;01mor\u001b[39;00m torch\u001b[38;5;241m.\u001b[39misinf(tr_loss_step))\n\u001b[1;32m 2274\u001b[0m ):\n\u001b[1;32m 2275\u001b[0m \u001b[38;5;66;03m# if loss is nan or inf simply add the average of previous logged losses\u001b[39;00m\n\u001b[1;32m 2276\u001b[0m tr_loss \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m tr_loss \u001b[38;5;241m/\u001b[39m (\u001b[38;5;241m1\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate\u001b[38;5;241m.\u001b[39mglobal_step \u001b[38;5;241m-\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_globalstep_last_logged)\n",
|
| 340 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/transformers/trainer.py:3324\u001b[0m, in \u001b[0;36mTrainer.training_step\u001b[0;34m(***failed resolving arguments***)\u001b[0m\n\u001b[1;32m 3322\u001b[0m scaled_loss\u001b[38;5;241m.\u001b[39mbackward()\n\u001b[1;32m 3323\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 3324\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43maccelerator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbackward\u001b[49m\u001b[43m(\u001b[49m\u001b[43mloss\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\u001b[1;32m 3326\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m loss\u001b[38;5;241m.\u001b[39mdetach() \u001b[38;5;241m/\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39margs\u001b[38;5;241m.\u001b[39mgradient_accumulation_steps\n",
|
| 341 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/accelerate/accelerator.py:2151\u001b[0m, in \u001b[0;36mAccelerator.backward\u001b[0;34m(self, loss, **kwargs)\u001b[0m\n\u001b[1;32m 2149\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlomo_backward(loss, learning_rate)\n\u001b[1;32m 2150\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 2151\u001b[0m \u001b[43mloss\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbackward\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",
|
| 342 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/_tensor.py:522\u001b[0m, in \u001b[0;36mTensor.backward\u001b[0;34m(self, gradient, retain_graph, create_graph, inputs)\u001b[0m\n\u001b[1;32m 512\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m has_torch_function_unary(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m 513\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m handle_torch_function(\n\u001b[1;32m 514\u001b[0m Tensor\u001b[38;5;241m.\u001b[39mbackward,\n\u001b[1;32m 515\u001b[0m (\u001b[38;5;28mself\u001b[39m,),\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 520\u001b[0m inputs\u001b[38;5;241m=\u001b[39minputs,\n\u001b[1;32m 521\u001b[0m )\n\u001b[0;32m--> 522\u001b[0m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mautograd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbackward\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 523\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgradient\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mretain_graph\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcreate_graph\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minputs\u001b[49m\n\u001b[1;32m 524\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 343 |
+
"File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/autograd/__init__.py:266\u001b[0m, in \u001b[0;36mbackward\u001b[0;34m(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)\u001b[0m\n\u001b[1;32m 261\u001b[0m retain_graph \u001b[38;5;241m=\u001b[39m create_graph\n\u001b[1;32m 263\u001b[0m \u001b[38;5;66;03m# The reason we repeat the same comment below is that\u001b[39;00m\n\u001b[1;32m 264\u001b[0m \u001b[38;5;66;03m# some Python versions print out the first line of a multi-line function\u001b[39;00m\n\u001b[1;32m 265\u001b[0m \u001b[38;5;66;03m# calls in the traceback and some print out the last line\u001b[39;00m\n\u001b[0;32m--> 266\u001b[0m \u001b[43mVariable\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execution_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_backward\u001b[49m\u001b[43m(\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# Calls into the C++ engine to run the backward pass\u001b[39;49;00m\n\u001b[1;32m 267\u001b[0m \u001b[43m \u001b[49m\u001b[43mtensors\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 268\u001b[0m \u001b[43m \u001b[49m\u001b[43mgrad_tensors_\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 269\u001b[0m \u001b[43m \u001b[49m\u001b[43mretain_graph\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 270\u001b[0m \u001b[43m \u001b[49m\u001b[43mcreate_graph\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 271\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 272\u001b[0m \u001b[43m \u001b[49m\u001b[43mallow_unreachable\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 273\u001b[0m \u001b[43m \u001b[49m\u001b[43maccumulate_grad\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 274\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 344 |
+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
|
| 345 |
+
]
|
| 346 |
+
}
|
| 347 |
+
],
|
| 348 |
+
"source": [
|
| 349 |
+
"train_result = trainer.train()\n",
|
| 350 |
+
"metrics = train_result.metrics\n",
|
| 351 |
+
"trainer.save_state()"
|
| 352 |
+
]
|
| 353 |
+
},
|
| 354 |
+
{
|
| 355 |
+
"cell_type": "code",
|
| 356 |
+
"execution_count": 12,
|
| 357 |
+
"id": "4d4207fc-1578-4591-a480-467fd2a5855b",
|
| 358 |
+
"metadata": {},
|
| 359 |
+
"outputs": [
|
| 360 |
+
{
|
| 361 |
+
"data": {
|
| 362 |
+
"text/plain": [
|
| 363 |
+
"{'train_runtime': 506.2204,\n",
|
| 364 |
+
" 'train_samples_per_second': 1.975,\n",
|
| 365 |
+
" 'train_steps_per_second': 0.494,\n",
|
| 366 |
+
" 'total_flos': 4.041582948790272e+16,\n",
|
| 367 |
+
" 'train_loss': 1.1037534561157227,\n",
|
| 368 |
+
" 'epoch': 1.0}"
|
| 369 |
+
]
|
| 370 |
+
},
|
| 371 |
+
"execution_count": 12,
|
| 372 |
+
"metadata": {},
|
| 373 |
+
"output_type": "execute_result"
|
| 374 |
+
}
|
| 375 |
+
],
|
| 376 |
+
"source": [
|
| 377 |
+
"metrics"
|
| 378 |
+
]
|
| 379 |
+
},
|
| 380 |
+
{
|
| 381 |
+
"cell_type": "code",
|
| 382 |
+
"execution_count": null,
|
| 383 |
+
"id": "f92339ec-0448-40d2-9458-6242e35b9bdc",
|
| 384 |
+
"metadata": {},
|
| 385 |
+
"outputs": [],
|
| 386 |
+
"source": [
|
| 387 |
+
"from peft import PeftConfig, PeftModel \n",
|
| 388 |
+
"\n",
|
| 389 |
+
"checkpoint_path = \"microsoft/Phi-3-mini-128k-instruct\"\n",
|
| 390 |
+
"adapter_path = \"./checkpoint_dir/checkpoint-250\"\n",
|
| 391 |
+
"\n",
|
| 392 |
+
"model_kwargs = dict(\n",
|
| 393 |
+
" use_cache=False,\n",
|
| 394 |
+
" trust_remote_code=True,\n",
|
| 395 |
+
" attn_implementation=\"flash_attention_2\", # loading the model with flash-attenstion support\n",
|
| 396 |
+
" torch_dtype=torch.bfloat16,\n",
|
| 397 |
+
" device_map=\"auto\"\n",
|
| 398 |
+
")\n",
|
| 399 |
+
"\n",
|
| 400 |
+
"model = AutoModelForCausalLM.from_pretrained(checkpoint_path, **model_kwargs)"
|
| 401 |
+
]
|
| 402 |
+
},
|
| 403 |
+
{
|
| 404 |
+
"cell_type": "code",
|
| 405 |
+
"execution_count": 8,
|
| 406 |
+
"id": "f0cf458d-8b4f-4ff9-bd60-bbe510416cea",
|
| 407 |
+
"metadata": {},
|
| 408 |
+
"outputs": [
|
| 409 |
+
{
|
| 410 |
+
"name": "stderr",
|
| 411 |
+
"output_type": "stream",
|
| 412 |
+
"text": [
|
| 413 |
+
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
|
| 414 |
+
]
|
| 415 |
+
}
|
| 416 |
+
],
|
| 417 |
+
"source": [
|
| 418 |
+
"model = PeftModel.from_pretrained(model, adapter_path)\n",
|
| 419 |
+
"tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)"
|
| 420 |
+
]
|
| 421 |
+
},
|
| 422 |
+
{
|
| 423 |
+
"cell_type": "code",
|
| 424 |
+
"execution_count": 22,
|
| 425 |
+
"id": "b5ada882-b7d2-46c5-ba5b-54fab2556832",
|
| 426 |
+
"metadata": {},
|
| 427 |
+
"outputs": [],
|
| 428 |
+
"source": [
|
| 429 |
+
"input_text = [\n",
|
| 430 |
+
" {'role': 'user', 'content': \"Tell me about cats\"},\n",
|
| 431 |
+
"]\n",
|
| 432 |
+
"\n",
|
| 433 |
+
"input = \"Tell me about cats\"\n",
|
| 434 |
+
"\n",
|
| 435 |
+
"input = tokenizer(input, return_tensors='pt')\n",
|
| 436 |
+
"\n",
|
| 437 |
+
"output = model.generate(\n",
|
| 438 |
+
" input['input_ids'].to('cuda'),\n",
|
| 439 |
+
" max_length=50, # Maximum length of the generated text\n",
|
| 440 |
+
" num_return_sequences=1, # Number of sequences to generate\n",
|
| 441 |
+
")"
|
| 442 |
+
]
|
| 443 |
+
},
|
| 444 |
+
{
|
| 445 |
+
"cell_type": "code",
|
| 446 |
+
"execution_count": 23,
|
| 447 |
+
"id": "139e9973-003a-484f-95f8-42428dd436f5",
|
| 448 |
+
"metadata": {},
|
| 449 |
+
"outputs": [
|
| 450 |
+
{
|
| 451 |
+
"name": "stdout",
|
| 452 |
+
"output_type": "stream",
|
| 453 |
+
"text": [
|
| 454 |
+
"Tell me about cats.\n",
|
| 455 |
+
"\n",
|
| 456 |
+
"Chatbot: Cats are fascinating creatures! They are known for their agility, independence, and unique behaviors. They have a keen sense of hearing and can see well in low light\n"
|
| 457 |
+
]
|
| 458 |
+
}
|
| 459 |
+
],
|
| 460 |
+
"source": [
|
| 461 |
+
"generated_text = tokenizer.decode(output[0], skip_special_tokens=True)\n",
|
| 462 |
+
"\n",
|
| 463 |
+
"print(generated_text)"
|
| 464 |
+
]
|
| 465 |
+
},
|
| 466 |
+
{
|
| 467 |
+
"cell_type": "code",
|
| 468 |
+
"execution_count": null,
|
| 469 |
+
"id": "6dc4ddb3-3cbf-4d6e-9f57-45acb8acbe25",
|
| 470 |
+
"metadata": {},
|
| 471 |
+
"outputs": [],
|
| 472 |
+
"source": []
|
| 473 |
+
}
|
| 474 |
+
],
|
| 475 |
+
"metadata": {
|
| 476 |
+
"kernelspec": {
|
| 477 |
+
"display_name": "Python 3 (ipykernel)",
|
| 478 |
+
"language": "python",
|
| 479 |
+
"name": "python3"
|
| 480 |
+
},
|
| 481 |
+
"language_info": {
|
| 482 |
+
"codemirror_mode": {
|
| 483 |
+
"name": "ipython",
|
| 484 |
+
"version": 3
|
| 485 |
+
},
|
| 486 |
+
"file_extension": ".py",
|
| 487 |
+
"mimetype": "text/x-python",
|
| 488 |
+
"name": "python",
|
| 489 |
+
"nbconvert_exporter": "python",
|
| 490 |
+
"pygments_lexer": "ipython3",
|
| 491 |
+
"version": "3.10.13"
|
| 492 |
+
}
|
| 493 |
+
},
|
| 494 |
+
"nbformat": 4,
|
| 495 |
+
"nbformat_minor": 5
|
| 496 |
+
}
|