File size: 13,344 Bytes
cefb6af | 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 | import os
import shutil
import json
from unsloth import FastLanguageModel, FastModel
from typing import Dict, Any, Tuple, List, Callable
from datasets import Dataset
from unsloth.chat_templates import standardize_data_formats, get_chat_template
from trl import SFTTrainer, SFTConfig
from transformers import TrainingArguments, DataCollatorForSeq2Seq
from unsloth import is_bfloat16_supported
import numpy as np
def cleanup_directories(output_dir: str, save_model_dir: str):
"""Remove directories created for a failed run"""
for dir_path in [output_dir, save_model_dir]:
if os.path.exists(dir_path):
try:
shutil.rmtree(dir_path)
print(f"Cleaned up directory: {dir_path}")
except Exception as e:
print(f"Error cleaning up directory {dir_path}: {e}")
def update_run_log(log_file: str, run_data: dict):
"""Update the JSON log file with new run data"""
try:
if os.path.exists(log_file):
with open(log_file, 'r') as f:
log = json.load(f)
else:
log = {"runs": []}
log["runs"].append(run_data)
with open(log_file, 'w') as f:
json.dump(log, f, indent=2)
except Exception as e:
print(f"Error updating log file: {e}")
###############################################################################3
def load_model_for_family(family_name: str, model_name: str, max_seq_length: int, dtype=None, load_in_4bit=True):
"""Load the appropriate model based on model family"""
from unsloth import FastLanguageModel, FastModel
if family_name.lower() == "gemma3":
# Gemma needs FastModel, not FastLanguageModel
model, tokenizer = FastModel.from_pretrained(
model_name=model_name,
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=load_in_4bit,
)
elif family_name.lower() in ["llama3", "qwen2.5", "qwen3"]:
# These models use FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=model_name,
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=load_in_4bit,
)
else:
raise ValueError(f"Unsupported model family: {family_name}. Must be one of: gemma3, llama3, qwen2.5, qwen3")
return model, tokenizer
# -------------------
# Gemma3 Functions
# -------------------
def gemma3_data_prep(dataset: Dataset, tokenizer: Any) -> Dataset:
# Standardize data formats to ensure consistency
standardized_dataset = standardize_data_formats(dataset)
print(f"GEMMA3 DEBUG - Standardized dataset first item: {standardized_dataset[0]}")
# Apply chat template
def apply_chat_template(examples):
texts = tokenizer.apply_chat_template(examples["conversations"], tokenize=False)
return {"text": texts}
formatted_dataset = standardized_dataset.map(apply_chat_template, batched=True)
return formatted_dataset
def gemma3_model_config(
model: Any,
tokenizer: Any,
r: int = 64,
lora_alpha: int = 64,
random_state: int = 3407
) -> Tuple[Any, Any]:
"""
Configure a Gemma3 model with appropriate parameters.
"""
# Use FastModel for Gemma models
model = FastModel.get_peft_model(
model,
finetune_vision_layers = False, # Turn off for just text
finetune_language_layers = True, # Should leave on
finetune_attention_modules = True, # Attention good for training
finetune_mlp_modules = True, # Should leave on always
r = r, # LoRA rank
lora_alpha = lora_alpha, # Recommended alpha == r at least
lora_dropout = 0.05, # Optimized setting
bias = "none", # Optimized setting
random_state = random_state,
)
# Set the appropriate chat template
tokenizer = get_chat_template(
tokenizer,
chat_template = "gemma-3",
)
return model, tokenizer
def get_gemma3_trainer(model, tokenizer, dataset):
"""Get SFT trainer configured for Gemma3 models"""
return SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
eval_dataset=None,
args=SFTConfig(
dataset_text_field="text",
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
warmup_ratio=0.05,
num_train_epochs=1,
# max_steps = 300,
learning_rate=2e-4,
logging_steps=1,
optim="adamw_8bit",
weight_decay=0.01,
lr_scheduler_type="linear",
seed=3407,
report_to="wandb",
),
)
# -------------------
# Llama3 Functions
# -------------------
def llama3_data_prep(dataset: Dataset, tokenizer: Any) -> Dataset:
"""
Prepare data for Llama3 models.
Llama3 format uses header-based conversation style.
"""
# Standardize for ShareGPT format
standardized_dataset = standardize_data_formats(dataset)
# Apply formatting function
def formatting_prompts_func(examples):
convos = examples["conversations"]
texts = [tokenizer.apply_chat_template(convo, tokenize=False, add_generation_prompt=False)
for convo in convos]
return {"text": texts}
formatted_dataset = standardized_dataset.map(formatting_prompts_func, batched=True)
return formatted_dataset
def llama3_model_config(
model: Any,
tokenizer: Any,
r: int = 64,
lora_alpha: int = 64,
random_state: int = 3407
) -> Tuple[Any, Any]:
"""
Configure a Llama3 model with appropriate parameters.
"""
model = FastLanguageModel.get_peft_model(
model,
r = r, # Choose any number > 0! Suggested 8, 16, 32, 64, 128
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"],
lora_alpha = lora_alpha,
lora_dropout = 0.05, # Supports any, but = 0 is optimized
bias = "none", # Supports any, but = "none" is optimized
use_gradient_checkpointing = "unsloth", # Uses 30% less VRAM
random_state = random_state,
use_rslora = False, # We support rank stabilized LoRA
loftq_config = None, # And LoftQ
)
# Set the appropriate chat template
tokenizer = get_chat_template(
tokenizer,
chat_template = "llama-3.1",
)
return model, tokenizer
def get_llama3_trainer(model, tokenizer, dataset, max_seq_length=2048):
"""Get SFT trainer configured for Llama3 models"""
return SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
dataset_text_field="text",
max_seq_length=max_seq_length,
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),
dataset_num_proc=2,
packing=False,
args=TrainingArguments(
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
warmup_ratio=0.05,
num_train_epochs=1,
# max_steps = 300,
learning_rate=2e-4,
fp16=not is_bfloat16_supported(),
bf16=is_bfloat16_supported(),
logging_steps=1,
optim="adamw_8bit",
weight_decay=0.01,
lr_scheduler_type="linear",
seed=3407,
output_dir="outputs",
report_to="wandb",
),
)
# -------------------
# Qwen2.5 Functions
# -------------------
def qwen2_5_data_prep(dataset: Dataset, tokenizer: Any) -> Dataset:
"""
Prepare data for Qwen2.5 models.
Qwen2.5 uses im_start/im_end markers for conversation formatting.
"""
# Standardize for ShareGPT format
standardized_dataset = standardize_data_formats(dataset)
# Apply formatting function
def formatting_prompts_func(examples):
convos = examples["conversations"]
texts = [tokenizer.apply_chat_template(convo, tokenize=False, add_generation_prompt=False)
for convo in convos]
return {"text": texts}
formatted_dataset = standardized_dataset.map(formatting_prompts_func, batched=True)
return formatted_dataset
def qwen2_5_model_config(
model: Any,
tokenizer: Any,
r: int = 64,
lora_alpha: int = 64,
random_state: int = 3407
) -> Tuple[Any, Any]:
"""
Configure a Qwen2.5 model with appropriate parameters.
"""
model = FastLanguageModel.get_peft_model(
model,
r = r, # Choose any number > 0! Suggested 8, 16, 32, 64, 128
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"],
lora_alpha = lora_alpha,
lora_dropout = 0.05, # Supports any, but = 0 is optimized
bias = "none", # Supports any, but = "none" is optimized
use_gradient_checkpointing = "unsloth", # Uses 30% less VRAM
random_state = random_state,
use_rslora = False, # We support rank stabilized LoRA
loftq_config = None, # And LoftQ
)
# Set the appropriate chat template
tokenizer = get_chat_template(
tokenizer,
chat_template = "qwen-2.5",
)
return model, tokenizer
def get_qwen2_5_trainer(model, tokenizer, dataset, max_seq_length=2048):
"""Get SFT trainer configured for Qwen2.5 models"""
return SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
dataset_text_field="text",
max_seq_length=max_seq_length,
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),
dataset_num_proc=2,
packing=False,
args=TrainingArguments(
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
warmup_ratio=0.05,
num_train_epochs=1,
# max_steps = 300,
learning_rate=2e-4,
fp16=not is_bfloat16_supported(),
bf16=is_bfloat16_supported(),
logging_steps=1,
optim="adamw_8bit",
weight_decay=0.01,
lr_scheduler_type="linear",
seed=3407,
output_dir="outputs",
report_to="wandb",
),
)
# -------------------
# Qwen3 Functions
# -------------------
def qwen3_data_prep(dataset: Dataset, tokenizer: Any) -> Dataset:
"""
Prepare data for Qwen3 models.
Qwen3 uses im_start/im_end markers with potential 'think' sections in assistant responses.
"""
# Standardize for ShareGPT format
standardized_dataset = standardize_data_formats(dataset)
# Get the conversations with chat template applied
conversations = tokenizer.apply_chat_template(
standardized_dataset["conversations"],
tokenize=False,
)
# Convert to dataset format
from pandas import Series
from datasets import Dataset
dataset = Dataset.from_pandas(Series(conversations, name="text").to_frame())
return dataset
def qwen3_model_config(
model: Any,
tokenizer: Any,
r: int = 64,
lora_alpha: int = 64,
random_state: int = 3407
) -> Tuple[Any, Any]:
"""
Configure a Qwen3 model with appropriate parameters.
"""
model = FastLanguageModel.get_peft_model(
model,
r = r, # Choose any number > 0! Suggested 8, 16, 32, 64, 128
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"],
lora_alpha = lora_alpha,
lora_dropout = 0.05, # Supports any, but = 0 is optimized
bias = "none", # Supports any, but = "none" is optimized
use_gradient_checkpointing = "unsloth", # Uses 30% less VRAM
random_state = random_state,
use_rslora = False, # We support rank stabilized LoRA
loftq_config = None, # And LoftQ
)
return model, tokenizer
def get_qwen3_trainer(model, tokenizer, dataset):
"""Get SFT trainer configured for Qwen3 models"""
return SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
eval_dataset=None,
args=SFTConfig(
dataset_text_field="text",
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
warmup_ratio=0.05,
num_train_epochs=1,
# max_steps = 300,
learning_rate=2e-4,
logging_steps=1,
optim="adamw_8bit",
weight_decay=0.01,
lr_scheduler_type="linear",
seed=3407,
report_to="wandb",
),
)
# Function mapping for easy lookup
MODEL_DATA_PREP = {
"gemma3": gemma3_data_prep,
"llama3": llama3_data_prep,
"qwen2.5": qwen2_5_data_prep,
"qwen3": qwen3_data_prep
}
MODEL_CONFIG = {
"gemma3": gemma3_model_config,
"llama3": llama3_model_config,
"qwen2.5": qwen2_5_model_config,
"qwen3": qwen3_model_config
}
MODEL_TRAINERS = {
"gemma3": get_gemma3_trainer,
"llama3": get_llama3_trainer,
"qwen2.5": get_qwen2_5_trainer,
"qwen3": get_qwen3_trainer
} |