File size: 52,302 Bytes
27c46c6 | 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 | # import torch
# from transformers import (
# AutoModelForCausalLM,
# AutoTokenizer,
# TrainingArguments,
# Trainer,
# DataCollatorForLanguageModeling,
# BitsAndBytesConfig
# )
# from peft import (
# LoraConfig,
# get_peft_model,
# prepare_model_for_kbit_training,
# TaskType
# )
# from datasets import load_dataset, Dataset
# import os
# from typing import Dict, List, Optional
# import numpy as np
# from tqdm import tqdm
# import json
# import gc
# import warnings
# warnings.filterwarnings('ignore')
# class LFMCounselorFineTuner:
# def __init__(self, model_name: str = "LiquidAI/LFM2-2.6B", use_4bit: bool = True):
# """
# Initialize the fine-tuner for LFM models
# Args:
# model_name: Name of the base model
# use_4bit: Whether to use 4-bit quantization for memory efficiency
# """
# self.model_name = model_name
# self.use_4bit = use_4bit
# self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# print(f"Using device: {self.device}")
# if torch.cuda.is_available():
# print(f"GPU: {torch.cuda.get_device_name(0)}")
# print(f"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.2f} GB")
# # Disable wandb for simplicity
# os.environ["WANDB_DISABLED"] = "true"
# def setup_model_and_tokenizer(self):
# """Setup model with quantization and LoRA"""
# print("Loading tokenizer...")
# # Tokenizer setup
# try:
# self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
# except:
# # Fallback to a known working tokenizer if model-specific one fails
# print("Using fallback tokenizer...")
# self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
# # Add padding token if it doesn't exist
# if self.tokenizer.pad_token is None:
# self.tokenizer.pad_token = self.tokenizer.eos_token
# if self.tokenizer.eos_token is None:
# self.tokenizer.eos_token = "</s>"
# self.tokenizer.pad_token = "</s>"
# self.tokenizer.padding_side = "right"
# # Quantization config for memory efficiency
# if self.use_4bit:
# print("Setting up 4-bit quantization...")
# bnb_config = BitsAndBytesConfig(
# load_in_4bit=True,
# bnb_4bit_quant_type="nf4",
# bnb_4bit_compute_dtype=torch.float16, # Use float16 for better compatibility
# bnb_4bit_use_double_quant=True
# )
# else:
# bnb_config = None
# # Load model
# print(f"Loading model: {self.model_name}...")
# try:
# self.model = AutoModelForCausalLM.from_pretrained(
# self.model_name,
# quantization_config=bnb_config,
# device_map="auto",
# trust_remote_code=True,
# torch_dtype=torch.float16
# )
# except Exception as e:
# print(f"Error loading model: {e}")
# print("Attempting to load without quantization...")
# self.model = AutoModelForCausalLM.from_pretrained(
# self.model_name,
# device_map="auto",
# trust_remote_code=True,
# torch_dtype=torch.float16,
# low_cpu_mem_usage=True
# )
# # Enable gradient checkpointing to save memory
# if hasattr(self.model, 'gradient_checkpointing_enable'):
# self.model.gradient_checkpointing_enable()
# # Prepare model for k-bit training
# if self.use_4bit:
# print("Preparing model for 4-bit training...")
# self.model = prepare_model_for_kbit_training(self.model)
# # LoRA configuration - optimized for counseling task
# print("Applying LoRA configuration...")
# # Find the target modules dynamically
# target_modules = self.find_target_modules()
# lora_config = LoraConfig(
# r=16, # Reduced rank for stability
# lora_alpha=32, # Alpha parameter for LoRA scaling
# target_modules=target_modules,
# lora_dropout=0.05,
# bias="none",
# task_type=TaskType.CAUSAL_LM,
# inference_mode=False
# )
# # Apply LoRA
# self.model = get_peft_model(self.model, lora_config)
# # Print trainable parameters
# self.model.print_trainable_parameters()
# def find_target_modules(self):
# """Find linear modules to apply LoRA to"""
# target_modules = []
# for name, module in self.model.named_modules():
# if isinstance(module, torch.nn.Linear):
# # Extract the module name
# names = name.split('.')
# if len(names) > 0:
# target_modules.append(names[-1])
# # Remove duplicates and filter common patterns
# target_modules = list(set(target_modules))
# # Common patterns for transformer models
# common_targets = ["q_proj", "v_proj", "k_proj", "o_proj",
# "gate_proj", "up_proj", "down_proj",
# "fc1", "fc2", "query", "key", "value", "dense"]
# # Filter to only include common targets if they exist
# final_targets = [t for t in target_modules if any(ct in t.lower() for ct in common_targets)]
# # If no common targets found, use all linear layers
# if not final_targets:
# final_targets = target_modules[:6] # Limit to prevent too many parameters
# print(f"LoRA target modules: {final_targets}")
# return final_targets if final_targets else ["q_proj", "v_proj"] # Fallback
# def load_and_process_datasets(self, data_path: str):
# """Load and process datasets without multiprocessing issues"""
# print(f"Loading datasets from {data_path}...")
# # Load train dataset
# train_texts = []
# with open(f'{data_path}/train.jsonl', 'r', encoding='utf-8') as f:
# for line in tqdm(f, desc="Loading training data"):
# data = json.loads(line)
# train_texts.append(data['text'])
# # Load validation dataset
# val_texts = []
# with open(f'{data_path}/validation.jsonl', 'r', encoding='utf-8') as f:
# for line in tqdm(f, desc="Loading validation data"):
# data = json.loads(line)
# val_texts.append(data['text'])
# print(f"Loaded {len(train_texts)} training examples")
# print(f"Loaded {len(val_texts)} validation examples")
# # Tokenize datasets in batches (avoiding multiprocessing)
# print("Tokenizing training dataset...")
# train_encodings = self.tokenize_texts(train_texts)
# print("Tokenizing validation dataset...")
# val_encodings = self.tokenize_texts(val_texts)
# # Create datasets
# self.train_dataset = Dataset.from_dict(train_encodings)
# self.val_dataset = Dataset.from_dict(val_encodings)
# # Set format for PyTorch
# self.train_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
# self.val_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
# # Clean up memory
# del train_texts, val_texts, train_encodings, val_encodings
# gc.collect()
# def tokenize_texts(self, texts: List[str], batch_size: int = 100):
# """Tokenize texts in batches to avoid memory issues"""
# all_input_ids = []
# all_attention_masks = []
# for i in tqdm(range(0, len(texts), batch_size), desc="Tokenizing"):
# batch_texts = texts[i:i + batch_size]
# # Tokenize batch
# encodings = self.tokenizer(
# batch_texts,
# truncation=True,
# padding='max_length',
# max_length=512,
# return_tensors='pt'
# )
# # Convert to lists
# all_input_ids.extend(encodings['input_ids'].tolist())
# all_attention_masks.extend(encodings['attention_mask'].tolist())
# # Create labels (same as input_ids for language modeling)
# labels = all_input_ids.copy()
# return {
# 'input_ids': all_input_ids,
# 'attention_mask': all_attention_masks,
# 'labels': labels
# }
# def setup_training_args(self, output_dir: str = "./counselor_model_2b"):
# """Setup training arguments optimized for counseling task"""
# print("Setting up training arguments...")
# # Calculate batch sizes based on available memory
# if torch.cuda.is_available():
# gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9
# if gpu_memory < 16: # Less than 16GB
# batch_size = 1
# gradient_accumulation = 16
# elif gpu_memory < 24: # Less than 24GB
# batch_size = 2
# gradient_accumulation = 8
# else: # 24GB or more
# batch_size = 4
# gradient_accumulation = 4
# else:
# batch_size = 1
# gradient_accumulation = 16
# print(f"Using batch_size={batch_size}, gradient_accumulation={gradient_accumulation}")
# self.training_args = TrainingArguments(
# output_dir=output_dir,
# num_train_epochs=3,
# per_device_train_batch_size=batch_size,
# per_device_eval_batch_size=batch_size,
# gradient_accumulation_steps=gradient_accumulation,
# gradient_checkpointing=True,
# warmup_steps=100,
# learning_rate=5e-5, # Conservative learning rate
# fp16=True,
# logging_steps=50,
# eval_strategy="steps",
# eval_steps=200,
# save_strategy="steps",
# save_steps=400,
# save_total_limit=2,
# load_best_model_at_end=True,
# metric_for_best_model="eval_loss",
# greater_is_better=False,
# report_to="none", # Disable all reporting
# push_to_hub=False,
# optim="adamw_torch", # Use standard optimizer
# lr_scheduler_type="linear",
# weight_decay=0.01,
# max_grad_norm=1.0,
# remove_unused_columns=False,
# label_names=["labels"],
# dataloader_num_workers=0, # Disable multiprocessing in dataloader
# dataloader_pin_memory=False, # Disable pinned memory to avoid issues
# )
# def train(self):
# """Execute training"""
# print("Initializing trainer...")
# # Data collator for language modeling
# data_collator = DataCollatorForLanguageModeling(
# tokenizer=self.tokenizer,
# mlm=False,
# pad_to_multiple_of=8
# )
# # Custom training to handle potential issues
# try:
# # Initialize trainer
# trainer = Trainer(
# model=self.model,
# args=self.training_args,
# train_dataset=self.train_dataset,
# eval_dataset=self.val_dataset,
# data_collator=data_collator,
# tokenizer=self.tokenizer,
# )
# # Start training
# print("="*50)
# print("Starting fine-tuning...")
# print("="*50)
# # Train with error handling
# train_result = trainer.train()
# # Save the final model
# print("\nSaving fine-tuned model...")
# trainer.save_model(f"{self.training_args.output_dir}/final_model_2b")
# self.tokenizer.save_pretrained(f"{self.training_args.output_dir}/final_model_2b")
# # Save training metrics
# with open(f"{self.training_args.output_dir}/training_metrics.json", 'w') as f:
# json.dump(train_result.metrics, f, indent=2)
# print("\n" + "="*50)
# print("Training completed successfully!")
# print(f"Model saved to: {self.training_args.output_dir}/final_model_2b")
# print("="*50)
# return trainer
# except Exception as e:
# print(f"Error during training: {e}")
# print("Attempting to save checkpoint...")
# # Try to save whatever we have
# try:
# self.model.save_pretrained(f"{self.training_args.output_dir}/checkpoint_emergency")
# self.tokenizer.save_pretrained(f"{self.training_args.output_dir}/checkpoint_emergency")
# print(f"Emergency checkpoint saved to: {self.training_args.output_dir}/checkpoint_emergency")
# except:
# print("Could not save emergency checkpoint")
# raise e
# def test_model(model_path: str, tokenizer_path: str):
# """Test the fine-tuned model with a sample input"""
# print("\n" + "="*50)
# print("Testing fine-tuned model...")
# print("="*50)
# # Load model and tokenizer
# from peft import PeftModel, PeftConfig
# tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
# # Try to load as PEFT model
# try:
# config = PeftConfig.from_pretrained(model_path)
# model = AutoModelForCausalLM.from_pretrained(
# config.base_model_name_or_path,
# torch_dtype=torch.float16,
# device_map="auto"
# )
# model = PeftModel.from_pretrained(model, model_path)
# except:
# # Load as regular model
# model = AutoModelForCausalLM.from_pretrained(
# model_path,
# torch_dtype=torch.float16,
# device_map="auto"
# )
# model.eval()
# # Test input
# test_input = "γγγ«γ‘γ―γζθΏγΉγγ¬γΉγζγγ¦γγΎγγ"
# # Generate response
# inputs = tokenizer(test_input, return_tensors="pt")
# inputs = {k: v.cuda() if torch.cuda.is_available() else v for k, v in inputs.items()}
# with torch.no_grad():
# outputs = model.generate(
# **inputs,
# max_new_tokens=100,
# temperature=0.1,
# do_sample=True,
# top_p=0.9
# )
# response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# print(f"Input: {test_input}")
# print(f"Response: {response}")
# print("="*50)
# # Main training script
# if __name__ == "__main__":
# import argparse
# parser = argparse.ArgumentParser(description='Fine-tune LFM model for counseling')
# parser.add_argument('--model_name', type=str, default='gpt2', # Using GPT2 as fallback
# help='Base model name (use gpt2 if liquid model fails)')
# parser.add_argument('--data_path', type=str, default='./processed_data_score80',
# help='Path to processed data')
# parser.add_argument('--output_dir', type=str, default='./counselor_model_2b',
# help='Output directory for fine-tuned model')
# parser.add_argument('--use_4bit', action='store_true', default=False,
# help='Use 4-bit quantization (set to False for stability)')
# parser.add_argument('--test_only', action='store_true',
# help='Only test existing model')
# args = parser.parse_args()
# if args.test_only:
# # Test existing model
# test_model(
# f"{args.output_dir}/final_model_2b",
# f"{args.output_dir}/final_model_2b"
# )
# else:
# # Check if CUDA is available
# if not torch.cuda.is_available():
# print("Warning: CUDA is not available. Training will be very slow on CPU.")
# print("It's highly recommended to use a GPU for training.")
# response = input("Do you want to continue anyway? (y/n): ")
# if response.lower() != 'y':
# exit()
# try:
# # Clear GPU cache
# if torch.cuda.is_available():
# torch.cuda.empty_cache()
# # Initialize fine-tuner
# print(f"Initializing fine-tuner with model: {args.model_name}")
# finetuner = LFMCounselorFineTuner(
# model_name=args.model_name,
# use_4bit=args.use_4bit
# )
# # Setup model
# print("\nSetting up model and tokenizer...")
# finetuner.setup_model_and_tokenizer()
# # Load datasets (using new method without multiprocessing)
# print("\nLoading and processing datasets...")
# finetuner.load_and_process_datasets(args.data_path)
# # Setup training arguments
# print("\nSetting up training arguments...")
# finetuner.setup_training_args(args.output_dir)
# # Train
# trainer = finetuner.train()
# # Test the model
# print("\nTesting the fine-tuned model...")
# test_model(
# f"{args.output_dir}/final_model_2b",
# f"{args.output_dir}/final_model_2b"
# )
# print("\nβ
Fine-tuning completed successfully!")
# print(f"π Model saved to: {args.output_dir}/final_model_2b")
# print("\nNext steps:")
# print("1. Test more: python finetune_lfm.py --test_only")
# print("2. Run benchmarking: python benchmark_model.py")
# print("3. Optimize for mobile: python optimize_for_mobile.py")
# except KeyboardInterrupt:
# print("\n\nTraining interrupted by user.")
# print("Partial model may be saved in checkpoints.")
# except Exception as e:
# print(f"\nβ Error during fine-tuning: {e}")
# import traceback
# traceback.print_exc()
# print("\nTroubleshooting tips:")
# print("1. Try reducing batch size")
# print("2. Try without 4-bit quantization: remove --use_4bit")
# print("3. Try with a smaller model like gpt2")
# print("4. Ensure you have enough GPU memory")
###### wandb login ######
import torch
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
TrainingArguments,
Trainer,
DataCollatorForLanguageModeling,
BitsAndBytesConfig,
TrainerCallback
)
from peft import (
LoraConfig,
get_peft_model,
prepare_model_for_kbit_training,
TaskType
)
from datasets import load_dataset, Dataset
import os
from typing import Dict, List, Optional
import numpy as np
from tqdm import tqdm
import json
import gc
import warnings
import wandb
from datetime import datetime
warnings.filterwarnings('ignore')
class LFMCounselorFineTuner:
def __init__(self, model_name: str = "LiquidAI/LFM2-2.6B", use_4bit: bool = True):
"""
Initialize the fine-tuner for LFM models
Args:
model_name: Name of the base model
use_4bit: Whether to use 4-bit quantization for memory efficiency
"""
self.model_name = model_name
self.use_4bit = use_4bit
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {self.device}")
gpu_memory = 0
if torch.cuda.is_available():
gpu_name = torch.cuda.get_device_name(0)
gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9
print(f"GPU: {gpu_name}")
print(f"GPU Memory: {gpu_memory:.2f} GB")
# Initialize WandB (always enabled)
try:
# Create a unique run name with timestamp
run_name = f"lfm-counselor-{datetime.now().strftime('%Y%m%d-%H%M%S')}"
# Initialize wandb with comprehensive config
wandb.init(
project="liquid-counselor-hackathon",
name=run_name,
config={
"model_name": model_name,
"use_4bit_quantization": use_4bit,
"device": str(self.device),
"gpu": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "CPU",
"gpu_memory_gb": gpu_memory,
"framework": "transformers",
"peft_method": "LoRA",
"task": "japanese_counseling",
"dataset": "KokoroChat"
},
tags=["counseling", "japanese", "lfm", "finetune", "hackathon"]
)
print(f"β
WandB initialized: {wandb.run.name}")
print(f"π View run at: {wandb.run.get_url()}")
self.wandb_enabled = True
except Exception as e:
print(f"β οΈ WandB initialization failed: {e}")
print("Continuing without WandB logging...")
self.wandb_enabled = False
os.environ["WANDB_DISABLED"] = "true"
def setup_model_and_tokenizer(self):
"""Setup model with quantization and LoRA"""
print("Loading tokenizer...")
# Tokenizer setup
try:
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
except:
# Fallback to a known working tokenizer if model-specific one fails
print("Using fallback tokenizer...")
self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
# Add padding token if it doesn't exist
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
if self.tokenizer.eos_token is None:
self.tokenizer.eos_token = "</s>"
self.tokenizer.pad_token = "</s>"
self.tokenizer.padding_side = "right"
# Quantization config for memory efficiency
if self.use_4bit:
print("Setting up 4-bit quantization...")
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True
)
else:
bnb_config = None
# Load model
print(f"Loading model: {self.model_name}...")
try:
self.model = AutoModelForCausalLM.from_pretrained(
self.model_name,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.float16
)
except Exception as e:
print(f"Error loading model: {e}")
print("Attempting to load without quantization...")
self.model = AutoModelForCausalLM.from_pretrained(
self.model_name,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.float16,
low_cpu_mem_usage=True
)
# Enable gradient checkpointing to save memory
if hasattr(self.model, 'gradient_checkpointing_enable'):
self.model.gradient_checkpointing_enable()
# Prepare model for k-bit training
if self.use_4bit:
print("Preparing model for 4-bit training...")
self.model = prepare_model_for_kbit_training(self.model)
# LoRA configuration - optimized for counseling task
print("Applying LoRA configuration...")
# Find the target modules dynamically
target_modules = self.find_target_modules()
lora_config = LoraConfig(
r=16, # Reduced rank for stability
lora_alpha=32, # Alpha parameter for LoRA scaling
target_modules=target_modules,
lora_dropout=0.05,
bias="none",
task_type=TaskType.CAUSAL_LM,
inference_mode=False
)
# Apply LoRA
self.model = get_peft_model(self.model, lora_config)
# Get trainable parameters info
trainable_params = 0
all_params = 0
for _, param in self.model.named_parameters():
all_params += param.numel()
if param.requires_grad:
trainable_params += param.numel()
trainable_percentage = 100 * trainable_params / all_params if all_params > 0 else 0
print(f"Trainable parameters: {trainable_params:,} / {all_params:,} ({trainable_percentage:.2f}%)")
# Log model architecture to WandB
if self.wandb_enabled:
wandb.config.update({
"lora_r": lora_config.r,
"lora_alpha": lora_config.lora_alpha,
"lora_dropout": lora_config.lora_dropout,
"lora_target_modules": target_modules,
"total_parameters": all_params,
"trainable_parameters": trainable_params,
"trainable_percentage": trainable_percentage
})
self.model.print_trainable_parameters()
def find_target_modules(self):
"""Find linear modules to apply LoRA to"""
target_modules = []
for name, module in self.model.named_modules():
if isinstance(module, torch.nn.Linear):
# Extract the module name
names = name.split('.')
if len(names) > 0:
target_modules.append(names[-1])
# Remove duplicates and filter common patterns
target_modules = list(set(target_modules))
# Common patterns for transformer models
common_targets = ["q_proj", "v_proj", "k_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",
"fc1", "fc2", "query", "key", "value", "dense"]
# Filter to only include common targets if they exist
final_targets = [t for t in target_modules if any(ct in t.lower() for ct in common_targets)]
# If no common targets found, use all linear layers
if not final_targets:
final_targets = target_modules[:6] # Limit to prevent too many parameters
print(f"LoRA target modules: {final_targets}")
return final_targets if final_targets else ["q_proj", "v_proj"] # Fallback
def load_and_process_datasets(self, data_path: str):
"""Load and process datasets without multiprocessing issues"""
print(f"Loading datasets from {data_path}...")
# Load train dataset
train_texts = []
train_scores = []
train_topics = []
with open(f'{data_path}/train.jsonl', 'r', encoding='utf-8') as f:
for line in tqdm(f, desc="Loading training data"):
data = json.loads(line)
train_texts.append(data['text'])
train_scores.append(data.get('score', 0))
train_topics.append(data.get('topic', 'Unknown'))
# Load validation dataset
val_texts = []
val_scores = []
val_topics = []
with open(f'{data_path}/validation.jsonl', 'r', encoding='utf-8') as f:
for line in tqdm(f, desc="Loading validation data"):
data = json.loads(line)
val_texts.append(data['text'])
val_scores.append(data.get('score', 0))
val_topics.append(data.get('topic', 'Unknown'))
print(f"Loaded {len(train_texts)} training examples")
print(f"Loaded {len(val_texts)} validation examples")
# Log dataset statistics to WandB
if self.wandb_enabled:
# Calculate score statistics
train_score_stats = {
"train_examples": len(train_texts),
"train_avg_score": float(np.mean(train_scores)),
"train_min_score": float(np.min(train_scores)),
"train_max_score": float(np.max(train_scores)),
"train_std_score": float(np.std(train_scores))
}
val_score_stats = {
"val_examples": len(val_texts),
"val_avg_score": float(np.mean(val_scores)),
"val_min_score": float(np.min(val_scores)),
"val_max_score": float(np.max(val_scores)),
"val_std_score": float(np.std(val_scores))
}
wandb.config.update(train_score_stats)
wandb.config.update(val_score_stats)
# Log score distribution histogram
wandb.log({
"train_score_distribution": wandb.Histogram(train_scores),
"val_score_distribution": wandb.Histogram(val_scores)
})
# Log topic distribution
train_topic_counts = {}
for topic in train_topics:
train_topic_counts[topic] = train_topic_counts.get(topic, 0) + 1
# Create a bar chart for topics (top 20)
if len(train_topic_counts) > 0:
top_topics = sorted(train_topic_counts.items(), key=lambda x: x[1], reverse=True)[:20]
wandb.log({
"topic_distribution": wandb.plot.bar(
wandb.Table(data=[[k, v] for k, v in top_topics],
columns=["Topic", "Count"]),
"Topic", "Count", title="Training Topic Distribution (Top 20)"
)
})
# Tokenize datasets in batches (avoiding multiprocessing)
print("Tokenizing training dataset...")
train_encodings = self.tokenize_texts(train_texts)
print("Tokenizing validation dataset...")
val_encodings = self.tokenize_texts(val_texts)
# Create datasets
self.train_dataset = Dataset.from_dict(train_encodings)
self.val_dataset = Dataset.from_dict(val_encodings)
# Set format for PyTorch
self.train_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
self.val_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
# Clean up memory
del train_texts, val_texts, train_encodings, val_encodings
gc.collect()
def tokenize_texts(self, texts: List[str], batch_size: int = 100):
"""Tokenize texts in batches to avoid memory issues"""
all_input_ids = []
all_attention_masks = []
for i in tqdm(range(0, len(texts), batch_size), desc="Tokenizing"):
batch_texts = texts[i:i + batch_size]
# Tokenize batch
encodings = self.tokenizer(
batch_texts,
truncation=True,
padding='max_length',
max_length=512,
return_tensors='pt'
)
# Convert to lists
all_input_ids.extend(encodings['input_ids'].tolist())
all_attention_masks.extend(encodings['attention_mask'].tolist())
# Create labels (same as input_ids for language modeling)
labels = all_input_ids.copy()
return {
'input_ids': all_input_ids,
'attention_mask': all_attention_masks,
'labels': labels
}
def setup_training_args(self, output_dir: str = "./counselor_model_2b"):
"""Setup training arguments optimized for counseling task"""
print("Setting up training arguments...")
# Calculate batch sizes based on available memory
if torch.cuda.is_available():
gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9
if gpu_memory < 16: # Less than 16GB
batch_size = 1
gradient_accumulation = 16
elif gpu_memory < 24: # Less than 24GB
batch_size = 2
gradient_accumulation = 8
else: # 24GB or more
batch_size = 4
gradient_accumulation = 4
else:
batch_size = 1
gradient_accumulation = 16
print(f"Using batch_size={batch_size}, gradient_accumulation={gradient_accumulation}")
# Update WandB config with training hyperparameters
if self.wandb_enabled:
wandb.config.update({
"batch_size": batch_size,
"gradient_accumulation_steps": gradient_accumulation,
"effective_batch_size": batch_size * gradient_accumulation,
"num_epochs": 3,
"learning_rate": 5e-5,
"warmup_steps": 100,
"weight_decay": 0.01,
"max_grad_norm": 1.0,
"lr_scheduler": "linear",
"optimizer": "adamw_torch",
"fp16": True,
"max_length": 512
})
# Set report_to based on wandb availability
report_to = "wandb" if self.wandb_enabled else "none"
self.training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=3,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
gradient_accumulation_steps=gradient_accumulation,
gradient_checkpointing=True,
warmup_steps=100,
learning_rate=5e-5,
fp16=True,
logging_steps=50,
logging_first_step=True,
eval_strategy="steps",
eval_steps=200,
save_strategy="steps",
save_steps=400,
save_total_limit=2,
load_best_model_at_end=True,
metric_for_best_model="eval_loss",
greater_is_better=False,
report_to=report_to,
run_name=wandb.run.name if self.wandb_enabled and wandb.run else "local_run",
push_to_hub=False,
optim="adamw_torch",
lr_scheduler_type="linear",
weight_decay=0.01,
max_grad_norm=1.0,
remove_unused_columns=False,
label_names=["labels"],
dataloader_num_workers=0,
dataloader_pin_memory=False,
)
def train(self):
"""Execute training"""
print("Initializing trainer...")
# Data collator for language modeling
data_collator = DataCollatorForLanguageModeling(
tokenizer=self.tokenizer,
mlm=False,
pad_to_multiple_of=8
)
# Custom callback for additional metrics (properly inheriting from TrainerCallback)
class CustomMetricsCallback(TrainerCallback):
def on_log(self, args, state, control, logs=None, **kwargs):
if logs and self.wandb_enabled:
# Add perplexity metrics
if "loss" in logs:
logs["perplexity"] = np.exp(logs["loss"])
if "eval_loss" in logs:
logs["eval_perplexity"] = np.exp(logs["eval_loss"])
return control
# Create callback instance with wandb_enabled flag
custom_callback = CustomMetricsCallback()
custom_callback.wandb_enabled = self.wandb_enabled
# Custom training to handle potential issues
try:
# Initialize trainer with callbacks
trainer = Trainer(
model=self.model,
args=self.training_args,
train_dataset=self.train_dataset,
eval_dataset=self.val_dataset,
data_collator=data_collator,
tokenizer=self.tokenizer,
callbacks=[custom_callback] if self.wandb_enabled else [],
)
# Calculate total training steps
total_steps = len(self.train_dataset) // (self.training_args.per_device_train_batch_size * self.training_args.gradient_accumulation_steps) * self.training_args.num_train_epochs
# Start training
print("="*50)
print("Starting fine-tuning...")
print(f"Total training samples: {len(self.train_dataset)}")
print(f"Total validation samples: {len(self.val_dataset)}")
print(f"Total training steps: {total_steps}")
print("="*50)
# Log training start
if self.wandb_enabled:
wandb.log({"training_status": "started", "total_steps": total_steps})
# Train with error handling
train_result = trainer.train()
# Save the final model
print("\nSaving fine-tuned model...")
trainer.save_model(f"{self.training_args.output_dir}/final_model_2b")
self.tokenizer.save_pretrained(f"{self.training_args.output_dir}/final_model_2b")
# Save training metrics
with open(f"{self.training_args.output_dir}/training_metrics.json", 'w') as f:
json.dump(train_result.metrics, f, indent=2)
# Final evaluation
print("\nRunning final evaluation...")
eval_results = trainer.evaluate()
# Save evaluation metrics
with open(f"{self.training_args.output_dir}/eval_metrics.json", 'w') as f:
json.dump(eval_results, f, indent=2)
# Log final metrics to WandB
if self.wandb_enabled:
# Log final metrics
wandb.run.summary.update({
"final_train_loss": train_result.metrics.get("train_loss", 0),
"final_eval_loss": eval_results.get("eval_loss", 0),
"final_eval_perplexity": np.exp(eval_results.get("eval_loss", 0)),
"total_training_time": train_result.metrics.get("train_runtime", 0),
"training_samples_per_second": train_result.metrics.get("train_samples_per_second", 0),
"training_status": "completed"
})
# Create a summary table
summary_table = wandb.Table(
columns=["Metric", "Value"],
data=[
["Final Training Loss", f"{train_result.metrics.get('train_loss', 0):.4f}"],
["Final Eval Loss", f"{eval_results.get('eval_loss', 0):.4f}"],
["Final Perplexity", f"{np.exp(eval_results.get('eval_loss', 0)):.2f}"],
["Training Time (seconds)", f"{train_result.metrics.get('train_runtime', 0):.0f}"],
["Training Samples/Second", f"{train_result.metrics.get('train_samples_per_second', 0):.2f}"]
]
)
wandb.log({"training_summary": summary_table})
# Save model artifact
try:
artifact = wandb.Artifact(
name=f"counselor-model-{wandb.run.id}",
type="model",
description="Fine-tuned Japanese counseling model",
metadata={
"base_model": self.model_name,
"final_loss": float(eval_results.get("eval_loss", 0)),
"final_perplexity": float(np.exp(eval_results.get("eval_loss", 0))),
"dataset": "KokoroChat"
}
)
artifact.add_dir(f"{self.training_args.output_dir}/final_model_2b")
wandb.log_artifact(artifact)
except Exception as e:
print(f"Warning: Could not save model artifact: {e}")
print("\n" + "="*50)
print("β
Training completed successfully!")
print(f"π Model saved to: {self.training_args.output_dir}/final_model_2b")
print(f"π Final eval loss: {eval_results.get('eval_loss', 0):.4f}")
print(f"π Final perplexity: {np.exp(eval_results.get('eval_loss', 0)):.2f}")
if self.wandb_enabled and wandb.run:
print(f"π View results at: {wandb.run.get_url()}")
print("="*50)
return trainer
except Exception as e:
print(f"β Error during training: {e}")
# Log error to WandB
if self.wandb_enabled:
wandb.run.summary["training_status"] = "failed"
wandb.run.summary["error"] = str(e)
print("Attempting to save checkpoint...")
# Try to save whatever we have
try:
self.model.save_pretrained(f"{self.training_args.output_dir}/checkpoint_emergency")
self.tokenizer.save_pretrained(f"{self.training_args.output_dir}/checkpoint_emergency")
print(f"πΎ Emergency checkpoint saved to: {self.training_args.output_dir}/checkpoint_emergency")
except:
print("β Could not save emergency checkpoint")
raise e
finally:
# Ensure WandB run is finished
if self.wandb_enabled:
wandb.finish()
# def test_model(model_path: str, tokenizer_path: str):
# """Test the fine-tuned model with sample inputs"""
# print("\n" + "="*50)
# print("Testing fine-tuned model...")
# print("="*50)
# # Load model and tokenizer
# from peft import PeftModel, PeftConfig
# tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
# if tokenizer.pad_token is None:
# tokenizer.pad_token = tokenizer.eos_token
# # Try to load as PEFT model
# try:
# config = PeftConfig.from_pretrained(model_path)
# model = AutoModelForCausalLM.from_pretrained(
# config.base_model_name_or_path,
# torch_dtype=torch.float16,
# device_map="auto"
# )
# model = PeftModel.from_pretrained(model, model_path)
# except:
# # Load as regular model
# model = AutoModelForCausalLM.from_pretrained(
# model_path,
# torch_dtype=torch.float16,
# device_map="auto"
# )
# model.eval()
# # Test inputs
# test_cases = [
# "γγγ«γ‘γ―γζθΏγΉγγ¬γΉγζγγ¦γγΎγγ",
# "δ»δΊγγγΎγγγγͺγγ¦ζ©γγ§γγΎγγ",
# "δΊΊιι’δΏγ§ε°γ£γ¦γγΎγγγ©γγγγ°γγγ§γγγγγ"
# ]
# print("Sample conversations:")
# print("-" * 50)
def test_model(model_path: str, tokenizer_path: str):
"""Test the fine-tuned model with sample inputs"""
print("\n" + "="*50)
print("Testing fine-tuned model...")
print("="*50)
# Load model and tokenizer with proper local path handling
from peft import PeftModel, PeftConfig
import os
# Fix tokenizer loading for local paths
try:
# Check if tokenizer files exist in the path
if os.path.exists(os.path.join(tokenizer_path, "tokenizer_config.json")):
print(f"Loading tokenizer from {tokenizer_path}")
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path, local_files_only=True)
else:
print(f"Tokenizer not found at {tokenizer_path}, using base model tokenizer")
# Fallback to base model tokenizer
tokenizer = AutoTokenizer.from_pretrained("gpt2")
except Exception as e:
print(f"Error loading tokenizer: {e}")
print("Using fallback GPT-2 tokenizer")
tokenizer = AutoTokenizer.from_pretrained("gpt2")
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
# Try to load model
try:
# Check if it's a PEFT model
adapter_config_path = os.path.join(model_path, "adapter_config.json")
if os.path.exists(adapter_config_path):
print("Loading as PEFT model...")
config = PeftConfig.from_pretrained(model_path)
base_model = AutoModelForCausalLM.from_pretrained(
config.base_model_name_or_path,
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=True
)
model = PeftModel.from_pretrained(base_model, model_path)
else:
# Load as regular model
print("Loading as regular model...")
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16,
device_map="auto",
local_files_only=True,
trust_remote_code=True
)
except Exception as e:
print(f"Error loading model: {e}")
raise
model.eval()
# Test inputs
test_cases = [
"γγγ«γ‘γ―γζθΏγΉγγ¬γΉγζγγ¦γγΎγγ",
"δ»δΊγγγΎγγγγͺγγ¦ζ©γγ§γγΎγγ",
"δΊΊιι’δΏγ§ε°γ£γ¦γγΎγγγ©γγγγ°γγγ§γγγγγ"
]
print("Sample conversations:")
print("-" * 50)
for test_input in test_cases:
# Generate response
inputs = tokenizer(test_input, return_tensors="pt", truncation=True, max_length=512)
inputs = {k: v.cuda() if torch.cuda.is_available() else v for k, v in inputs.items()}
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=150,
temperature=0.1,
do_sample=True,
top_p=0.9,
pad_token_id=tokenizer.pad_token_id
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
response = response[len(test_input):].strip() # Remove input from response
print(f"Client: {test_input}")
print(f"Counselor: {response[:200]}...")
print("-" * 50)
print("="*50)
for test_input in test_cases:
# Generate response
inputs = tokenizer(test_input, return_tensors="pt", truncation=True, max_length=512)
inputs = {k: v.cuda() if torch.cuda.is_available() else v for k, v in inputs.items()}
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=150,
temperature=0.1,
do_sample=True,
top_p=0.9,
pad_token_id=tokenizer.pad_token_id
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
response = response[len(test_input):].strip() # Remove input from response
print(f"Client: {test_input}")
print(f"Counselor: {response[:200]}...")
print("-" * 50)
print("="*50)
# Main training script
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Fine-tune LFM model for counseling')
parser.add_argument('--model_name', type=str, default='LiquidAI/LFM2-2.6B',
help='Base model name')
parser.add_argument('--data_path', type=str, default='./processed_data_score80',
help='Path to processed data')
parser.add_argument('--output_dir', type=str, default='./counselor_model_2b',
help='Output directory for fine-tuned model')
parser.add_argument('--use_4bit', action='store_true', default=False,
help='Use 4-bit quantization')
parser.add_argument('--wandb_api_key', type=str, default=None,
help='WandB API key (optional, can use wandb login instead)')
parser.add_argument('--test_only', action='store_true',
help='Only test existing model')
args = parser.parse_args()
# Set WandB API key if provided
if args.wandb_api_key:
os.environ["WANDB_API_KEY"] = args.wandb_api_key
if args.test_only:
# Test existing model
test_model(
f"{args.output_dir}/final_model_2b",
f"{args.output_dir}/final_model_2b"
)
else:
# Check if CUDA is available
if not torch.cuda.is_available():
print("β οΈ Warning: CUDA is not available. Training will be very slow on CPU.")
print("It's highly recommended to use a GPU for training.")
response = input("Do you want to continue anyway? (y/n): ")
if response.lower() != 'y':
exit()
try:
# Clear GPU cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
# Initialize fine-tuner (WandB is enabled by default)
print(f"π Initializing fine-tuner with model: {args.model_name}")
finetuner = LFMCounselorFineTuner(
model_name=args.model_name,
use_4bit=args.use_4bit
)
# Setup model
print("\nπ§ Setting up model and tokenizer...")
finetuner.setup_model_and_tokenizer()
# Load datasets
print("\nπ Loading and processing datasets...")
finetuner.load_and_process_datasets(args.data_path)
# Setup training arguments
print("\nβοΈ Setting up training arguments...")
finetuner.setup_training_args(args.output_dir)
# Train
trainer = finetuner.train()
# Test the model
print("\nπ§ͺ Testing the fine-tuned model...")
test_model(
f"{args.output_dir}/final_model_2b_v2",
f"{args.output_dir}/final_model_2b_v2"
)
print("\nβ
Fine-tuning completed successfully!")
print(f"π Model saved to: {args.output_dir}/final_model_2b_v2")
print("\nπ Next steps:")
print("1. Test more: python finetune_lfm.py --test_only")
print("2. Run benchmarking: python benchmark_model.py")
print("3. Optimize for mobile: python optimize_for_mobile.py")
except KeyboardInterrupt:
print("\n\nβ οΈ Training interrupted by user.")
print("Partial model may be saved in checkpoints.")
if wandb.run:
wandb.finish()
except Exception as e:
print(f"\nβ Error during fine-tuning: {e}")
import traceback
traceback.print_exc()
if wandb.run:
wandb.finish()
|