Upload train_qwen3_8b_hf.py with huggingface_hub
Browse files- train_qwen3_8b_hf.py +118 -0
train_qwen3_8b_hf.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = [
|
| 3 |
+
# "trl>=0.12.0",
|
| 4 |
+
# "peft>=0.7.0",
|
| 5 |
+
# "transformers>=4.36.0",
|
| 6 |
+
# "accelerate>=0.24.0",
|
| 7 |
+
# "trackio",
|
| 8 |
+
# "bitsandbytes",
|
| 9 |
+
# "datasets"
|
| 10 |
+
# ]
|
| 11 |
+
# ///
|
| 12 |
+
|
| 13 |
+
"""
|
| 14 |
+
Fine-tune Qwen3-8B on Vyvo Life CoPilot conversations dataset.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import json
|
| 18 |
+
from datasets import load_dataset, Dataset
|
| 19 |
+
from peft import LoraConfig
|
| 20 |
+
from trl import SFTTrainer, SFTConfig
|
| 21 |
+
|
| 22 |
+
print("📦 Loading dataset from Hub...")
|
| 23 |
+
raw_dataset = load_dataset("Codyfederer/vyvo-text-conversations", split="train")
|
| 24 |
+
print(f"✅ Loaded {len(raw_dataset)} conversations")
|
| 25 |
+
|
| 26 |
+
# Convert to messages format
|
| 27 |
+
print("🔄 Converting to messages format...")
|
| 28 |
+
conversations = []
|
| 29 |
+
for item in raw_dataset:
|
| 30 |
+
messages = []
|
| 31 |
+
for turn in item['turns']:
|
| 32 |
+
messages.append({
|
| 33 |
+
'role': turn['role'],
|
| 34 |
+
'content': turn['content']
|
| 35 |
+
})
|
| 36 |
+
conversations.append({'messages': messages})
|
| 37 |
+
|
| 38 |
+
dataset = Dataset.from_list(conversations)
|
| 39 |
+
print(f"✅ Converted {len(dataset)} conversations")
|
| 40 |
+
|
| 41 |
+
# Create train/eval split
|
| 42 |
+
print("🔀 Creating train/eval split...")
|
| 43 |
+
dataset_split = dataset.train_test_split(test_size=0.05, seed=42)
|
| 44 |
+
train_dataset = dataset_split["train"]
|
| 45 |
+
eval_dataset = dataset_split["test"]
|
| 46 |
+
print(f" Train: {len(train_dataset)} examples")
|
| 47 |
+
print(f" Eval: {len(eval_dataset)} examples")
|
| 48 |
+
|
| 49 |
+
# Training configuration
|
| 50 |
+
config = SFTConfig(
|
| 51 |
+
# Hub settings
|
| 52 |
+
output_dir="qwen3-8b-vyvo-copilot",
|
| 53 |
+
push_to_hub=True,
|
| 54 |
+
hub_model_id="Codyfederer/qwen3-8b-vyvo-copilot",
|
| 55 |
+
hub_strategy="every_save",
|
| 56 |
+
hub_private_repo=False,
|
| 57 |
+
|
| 58 |
+
# Training parameters - optimized for 8B model with LoRA
|
| 59 |
+
num_train_epochs=3,
|
| 60 |
+
per_device_train_batch_size=2,
|
| 61 |
+
gradient_accumulation_steps=8, # Effective batch size = 16
|
| 62 |
+
learning_rate=2e-4,
|
| 63 |
+
max_length=2048, # Good context for multi-turn conversations
|
| 64 |
+
|
| 65 |
+
# Memory optimization
|
| 66 |
+
gradient_checkpointing=True,
|
| 67 |
+
bf16=True,
|
| 68 |
+
|
| 69 |
+
# Logging & checkpointing
|
| 70 |
+
logging_steps=10,
|
| 71 |
+
save_strategy="steps",
|
| 72 |
+
save_steps=200,
|
| 73 |
+
save_total_limit=3,
|
| 74 |
+
|
| 75 |
+
# Evaluation
|
| 76 |
+
eval_strategy="steps",
|
| 77 |
+
eval_steps=200,
|
| 78 |
+
|
| 79 |
+
# Optimization
|
| 80 |
+
warmup_ratio=0.05,
|
| 81 |
+
lr_scheduler_type="cosine",
|
| 82 |
+
weight_decay=0.01,
|
| 83 |
+
|
| 84 |
+
# Monitoring
|
| 85 |
+
report_to="trackio",
|
| 86 |
+
project="vyvo-copilot-training",
|
| 87 |
+
run_name="qwen3-8b-sft-v1",
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# LoRA configuration - optimized for Qwen3
|
| 91 |
+
peft_config = LoraConfig(
|
| 92 |
+
r=32,
|
| 93 |
+
lora_alpha=64,
|
| 94 |
+
lora_dropout=0.05,
|
| 95 |
+
bias="none",
|
| 96 |
+
task_type="CAUSAL_LM",
|
| 97 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Initialize and train
|
| 101 |
+
print("🎯 Initializing trainer with Qwen/Qwen3-8B...")
|
| 102 |
+
trainer = SFTTrainer(
|
| 103 |
+
model="Qwen/Qwen3-8B",
|
| 104 |
+
train_dataset=train_dataset,
|
| 105 |
+
eval_dataset=eval_dataset,
|
| 106 |
+
args=config,
|
| 107 |
+
peft_config=peft_config,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
print("🚀 Starting training...")
|
| 111 |
+
trainer.train()
|
| 112 |
+
|
| 113 |
+
print("💾 Pushing final model to Hub...")
|
| 114 |
+
trainer.push_to_hub()
|
| 115 |
+
|
| 116 |
+
print("✅ Training complete!")
|
| 117 |
+
print("📦 Model saved to: https://huggingface.co/Codyfederer/qwen3-8b-vyvo-copilot")
|
| 118 |
+
print("📊 View metrics at: https://huggingface.co/spaces/Codyfederer/trackio")
|