File size: 13,992 Bytes
e06aa3c |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "859a9005-4458-4b87-b773-e06738fcea43",
"metadata": {},
"outputs": [],
"source": [
"from tqdm.auto import tqdm as auto_tqdm\n",
"import tqdm\n",
"\n",
"tqdm.tqdm = auto_tqdm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d5a4ce9e-de32-4719-87f6-1cb01eb9e797",
"metadata": {},
"outputs": [],
"source": [
"from typing import List, Optional\n",
"from dataclasses import dataclass, field\n",
"\n",
"@dataclass\n",
"class ScriptArguments:\n",
" \"\"\"\n",
" The name of the Casual LM model we wish to fine with SFTTrainer\n",
" \"\"\"\n",
"\n",
" model_name: Optional[str] = field(default=\"facebook/opt-350m\", metadata={\"help\": \"the model name\"})\n",
" dataset_name: Optional[str] = field(\n",
" default=\"timdettmers/openassistant-guanaco\", metadata={\"help\": \"the dataset name\"}\n",
" )\n",
" dataset_text_field: Optional[str] = field(default=\"text\", metadata={\"help\": \"the text field of the dataset\"})\n",
" report_to: Optional[str] = field(default=\"none\", metadata={\"help\": \"use 'wandb' to log with wandb\"})\n",
" learning_rate: Optional[float] = field(default=1.41e-5, metadata={\"help\": \"the learning rate\"})\n",
" batch_size: Optional[int] = field(default=64, metadata={\"help\": \"the batch size\"})\n",
" seq_length: Optional[int] = field(default=512, metadata={\"help\": \"Input sequence length\"})\n",
" gradient_accumulation_steps: Optional[int] = field(\n",
" default=1, metadata={\"help\": \"the number of gradient accumulation steps\"}\n",
" )\n",
" load_in_8bit: Optional[bool] = field(default=False, metadata={\"help\": \"load the model in 8 bits precision\"})\n",
" load_in_4bit: Optional[bool] = field(default=False, metadata={\"help\": \"load the model in 4 bits precision\"})\n",
" use_peft: Optional[bool] = field(default=False, metadata={\"help\": \"Wether to use PEFT or not to train adapters\"})\n",
" trust_remote_code: Optional[bool] = field(default=False, metadata={\"help\": \"Enable `trust_remote_code`\"})\n",
" output_dir: Optional[str] = field(default=\"output\", metadata={\"help\": \"the output directory\"})\n",
" peft_lora_r: Optional[int] = field(default=64, metadata={\"help\": \"the r parameter of the LoRA adapters\"})\n",
" peft_lora_alpha: Optional[int] = field(default=16, metadata={\"help\": \"the alpha parameter of the LoRA adapters\"})\n",
" logging_steps: Optional[int] = field(default=1, metadata={\"help\": \"the number of logging steps\"})\n",
" use_auth_token: Optional[bool] = field(default=False, metadata={\"help\": \"Use HF auth token to access the model\"})\n",
" num_train_epochs: Optional[int] = field(default=3, metadata={\"help\": \"the number of training epochs\"})\n",
" max_steps: Optional[int] = field(default=-1, metadata={\"help\": \"the number of training steps\"})\n",
" save_steps: Optional[int] = field(\n",
" default=100, metadata={\"help\": \"Number of updates steps before two checkpoint saves\"}\n",
" )\n",
" save_total_limit: Optional[int] = field(default=10, metadata={\"help\": \"Limits total number of checkpoints.\"})\n",
" push_to_hub: Optional[bool] = field(default=False, metadata={\"help\": \"Push the model to HF Hub\"})\n",
" fp16: Optional[bool] = field(default=False, metadata={\"help\": \"Whether to activate fp16 mixed precision\"})\n",
" bf16: Optional[bool] = field(default=False, metadata={\"help\": \"Whether to activate bf16 mixed precision\"})\n",
" gradient_checkpointing: Optional[bool] = field(\n",
" default=False, metadata={\"help\": \"Whether to use gradient checkpointing or no\"}\n",
" )\n",
" gradient_checkpointing_kwargs: Optional[dict] = field(\n",
" default=None,\n",
" metadata={\n",
" \"help\": \"key word arguments to be passed along `torch.utils.checkpoint.checkpoint` method - e.g. `use_reentrant=False`\"\n",
" },\n",
" )\n",
" hub_model_id: Optional[str] = field(default=None, metadata={\"help\": \"The name of the model on HF Hub\"})\n",
" mixed_precision: Optional[str] = field(default=\"bf16\", metadata={\"help\": \"Mixed precision training\"})\n",
" target_modules: Optional[List[str]] = field(default=None, metadata={\"help\": \"Target modules for LoRA adapters\"})\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "94af6f10-a793-48d5-9de9-c16438137354",
"metadata": {},
"outputs": [],
"source": [
"# accelerate launch --config_file examples/accelerate_configs/multi_gpu.yaml --num_processes=1 \\\n",
"# \texamples/scripts/sft.py \\\n",
"# \t--model_name mistralai/Mixtral-8x7B-Instruct-v0.1 \\\n",
"# \t--dataset_name trl-lib/ultrachat_200k_chatml \\\n",
"# \t--batch_size 2 \\\n",
"# \t--gradient_accumulation_steps 1 \\\n",
"# \t--learning_rate 2e-4 \\\n",
"# \t--save_steps 200_000 \\\n",
"# \t--use_peft \\\n",
"# \t--peft_lora_r 16 --peft_lora_alpha 32 \\\n",
"# \t--target_modules q_proj k_proj v_proj o_proj \\\n",
"# \t--load_in_4bit"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c0a87544-6451-4528-8fce-342916e904e9",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6979a4133b1341a18d070818593d06a7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/19 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import torch\n",
"from accelerate import Accelerator\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TrainingArguments\n",
"from peft import LoraConfig\n",
"from trl import is_xpu_available\n",
"\n",
"# sft.py script version 0.7.10\n",
"\n",
"device_map = (\n",
" {\"\": f\"xpu:{Accelerator().local_process_index}\"}\n",
" if is_xpu_available()\n",
" else {\"\": Accelerator().local_process_index}\n",
")\n",
"\n",
"torch_dtype = torch.bfloat16\n",
"\n",
"quantization_config = BitsAndBytesConfig(\n",
" load_in_4bit=True\n",
")\n",
"\n",
"training_args = TrainingArguments(\n",
" output_dir = './output1',\n",
" per_device_train_batch_size = 2,\n",
" gradient_accumulation_steps = 1,\n",
" learning_rate = 2e-4,\n",
" logging_steps = 1,\n",
" num_train_epochs = 3,\n",
" max_steps = -1,\n",
" #report_to = 'wandb',\n",
" save_steps = 200_000,\n",
" save_total_limit = 10,\n",
" push_to_hub = False,\n",
" hub_model_id = None,\n",
" gradient_checkpointing = False,\n",
" gradient_checkpointing_kwargs = dict(use_reentrant=False),\n",
" fp16 = False,\n",
" bf16 = False,\n",
")\n",
"\n",
"peft_config = LoraConfig(\n",
" r = 16,\n",
" lora_alpha = 32,\n",
" bias = \"none\",\n",
" task_type = \"CAUSAL_LM\",\n",
" target_modules = ['q_proj', 'k_proj', 'v_proj', 'o_proj']\n",
")\n",
"\n",
"model_name = 'mistralai/Mixtral-8x7B-Instruct-v0.1'\n",
"model = AutoModelForCausalLM.from_pretrained(\n",
" model_name,\n",
" quantization_config = quantization_config,\n",
" device_map = device_map,\n",
" trust_remote_code = False,\n",
" torch_dtype = torch_dtype\n",
")\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)\n",
"tokenizer.pad_token = tokenizer.eos_token\n",
"tokenizer.padding_side = 'right'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "46b10959-35bd-4b8a-a258-2ecbf7051854",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mantonvo\u001b[0m (\u001b[33mnorthern-light\u001b[0m). Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n"
]
}
],
"source": [
"import wandb, os\n",
"wandb.login()\n",
"\n",
"wandb_project = \"mixtral-select-finetune\"\n",
"if len(wandb_project) > 0:\n",
" os.environ[\"WANDB_PROJECT\"] = wandb_project"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "00889d6a-1089-4686-abf8-7ce1bfc07d7c",
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset, DatasetDict, concatenate_datasets\n",
"import pandas as pd\n",
"\n",
"# Filenames of your CSV files\n",
"filenames = ['./select-1.csv', './select-2.csv', './select-3.csv']\n",
"\n",
"# Load datasets and split each\n",
"split_datasets = {'train': [], 'validation': []}\n",
"\n",
"for filename in filenames:\n",
" # Load the CSV file as a Dataset\n",
" dataset = load_dataset('csv', data_files=filename, split='train')\n",
"\n",
" # Split the dataset into training and validation\n",
" split = dataset.train_test_split(test_size=0.2, seed=42)\n",
" \n",
" # Append the split datasets to the corresponding lists\n",
" split_datasets['train'].append(split['train'])\n",
" split_datasets['validation'].append(split['test'])\n",
"\n",
"# Concatenate the datasets for training and validation\n",
"train_dataset = concatenate_datasets(split_datasets['train'])\n",
"eval_dataset = concatenate_datasets(split_datasets['validation'])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2c053ed9-44d8-4675-b387-293759e71aeb",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "11f151e07a914fd783e585860f2a1f3b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/24000 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "70f879fc29db4fc08dcb8514dc8d62ed",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/6000 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from trl import SFTTrainer\n",
"\n",
"trainer = SFTTrainer(\n",
" model = model,\n",
" args = training_args,\n",
" max_seq_length = 512, #32 * 1024,\n",
" train_dataset = train_dataset,\n",
" eval_dataset = eval_dataset,\n",
" dataset_text_field = 'text',\n",
" peft_config = peft_config,\n",
" tokenizer = tokenizer\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4026c950-8034-4e02-9138-51f11588790b",
"metadata": {},
"outputs": [],
"source": [
"trainer.train()\n",
"trainer.save_model('./output1')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "3ad28a98-4323-46ee-9957-c299fb26686d",
"metadata": {},
"outputs": [],
"source": [
"from peft import PeftModel\n",
"\n",
"ft_model = PeftModel.from_pretrained(model, \"./output1\")\n",
"\n",
"def generate_response(prompt):\n",
" encoded_input = tokenizer(prompt, return_tensors=\"pt\", add_special_tokens=True)\n",
" model_inputs = encoded_input.to('cuda')\n",
"\n",
" generated_ids = ft_model.generate(**model_inputs,\n",
" max_new_tokens=150,\n",
" do_sample=True,\n",
" pad_token_id=tokenizer.eos_token_id)\n",
" decoded_output = tokenizer.batch_decode(generated_ids)\n",
" return decoded_output[0]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "5c5d50d4-8732-4939-a4bd-f6fd7bdbc498",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'<s> [INST] Hey, what\\'s up? [/INST] Hello! I\\'m doing well, thank you for asking. How about you? 🙂\\n\\nAs for the topic, since it\\'s Easter Sunday today, I thought it would be appropriate to share a song about the holiday. This one is called \"Jesus and Rosemary\" and it\\'s a traditional Irish tune featuring the accordion, tin whistle, and bodhrán instruments. The lyrics tell the story of Jesus, his mother Mary, and the resurrection that Easter commemorates. Here they are in case you want to follow along:\\n\\nUpon a golden Sunday, As morning broke so soon, Rosemary\\'s arms were thrown around a tree, As if to claim it\\'s tune.'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"[INST] Hey, what's up? [/INST]\"\n",
"generate_response(prompt)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "f8e1e52b-6dee-48f4-9ad2-9161de24ab70",
"metadata": {},
"outputs": [],
"source": [
"model.config.to_json_file('config.json')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|