Upload train_qwen3_codeforces.py with huggingface_hub
Browse files- train_qwen3_codeforces.py +86 -0
train_qwen3_codeforces.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.10"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "trl>=0.12.0",
|
| 5 |
+
# "peft>=0.7.0",
|
| 6 |
+
# "transformers>=4.36.0",
|
| 7 |
+
# "accelerate>=0.24.0",
|
| 8 |
+
# "trackio",
|
| 9 |
+
# ]
|
| 10 |
+
# ///
|
| 11 |
+
|
| 12 |
+
import trackio
|
| 13 |
+
from datasets import load_dataset
|
| 14 |
+
from peft import LoraConfig
|
| 15 |
+
from trl import SFTTrainer, SFTConfig
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Load dataset - use only 20 examples for quick demo
|
| 19 |
+
print("📦 Loading dataset...")
|
| 20 |
+
full_dataset = load_dataset("open-r1/codeforces-cots", split="train")
|
| 21 |
+
# Take only first 20 examples for quick demo
|
| 22 |
+
dataset = full_dataset.select(range(20))
|
| 23 |
+
print(f"✅ Dataset loaded: {len(dataset)} examples")
|
| 24 |
+
|
| 25 |
+
# Use messages column directly - TRL SFT supports this format
|
| 26 |
+
# No need for train/eval split for quick demo - use full dataset
|
| 27 |
+
|
| 28 |
+
# Training configuration
|
| 29 |
+
config = SFTConfig(
|
| 30 |
+
# CRITICAL: Hub settings
|
| 31 |
+
output_dir="qwen3-0.6b-codeforces-sft",
|
| 32 |
+
push_to_hub=True,
|
| 33 |
+
hub_model_id="albertlieadrian/qwen3-0.6b-codeforces-sft",
|
| 34 |
+
hub_strategy="every_save",
|
| 35 |
+
|
| 36 |
+
# Training parameters - optimized for small dataset demo
|
| 37 |
+
num_train_epochs=3,
|
| 38 |
+
per_device_train_batch_size=2,
|
| 39 |
+
gradient_accumulation_steps=4,
|
| 40 |
+
learning_rate=2e-5,
|
| 41 |
+
max_length=2048, # Code problems need longer context
|
| 42 |
+
|
| 43 |
+
# Logging & checkpointing
|
| 44 |
+
logging_steps=5,
|
| 45 |
+
save_strategy="no", # Skip saving for quick demo
|
| 46 |
+
save_total_limit=0,
|
| 47 |
+
|
| 48 |
+
# Optimization
|
| 49 |
+
warmup_ratio=0.1,
|
| 50 |
+
lr_scheduler_type="cosine",
|
| 51 |
+
|
| 52 |
+
# Monitoring
|
| 53 |
+
report_to="trackio",
|
| 54 |
+
project="qwen3-codeforces-demo",
|
| 55 |
+
run_name="20-examples-demo",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# LoRA configuration - efficient for 0.6B model
|
| 59 |
+
peft_config = LoraConfig(
|
| 60 |
+
r=16,
|
| 61 |
+
lora_alpha=32,
|
| 62 |
+
lora_dropout=0.05,
|
| 63 |
+
bias="none",
|
| 64 |
+
task_type="CAUSAL_LM",
|
| 65 |
+
target_modules=["q_proj", "v_proj"],
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Initialize and train
|
| 69 |
+
print("🎯 Initializing trainer...")
|
| 70 |
+
trainer = SFTTrainer(
|
| 71 |
+
model="Qwen/Qwen3-0.6B",
|
| 72 |
+
train_dataset=dataset,
|
| 73 |
+
args=config,
|
| 74 |
+
peft_config=peft_config,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
print("🚀 Starting training...")
|
| 78 |
+
trainer.train()
|
| 79 |
+
|
| 80 |
+
print("💾 Pushing to Hub...")
|
| 81 |
+
trainer.push_to_hub()
|
| 82 |
+
|
| 83 |
+
# Finish Trackio tracking
|
| 84 |
+
trackio.finish()
|
| 85 |
+
|
| 86 |
+
print("✅ Complete! Model at: https://huggingface.co/albertlieadrian/qwen3-0.6b-codeforces-sft")
|