Upload train_qwen3_codeforces.py with huggingface_hub
Browse files- train_qwen3_codeforces.py +102 -0
train_qwen3_codeforces.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = ["trl>=0.12.0", "peft>=0.7.0", "trackio", "transformers>=4.44.0", "datasets>=2.14.0", "torch>=2.0.0", "accelerate>=0.24.0"]
|
| 3 |
+
# ///
|
| 4 |
+
|
| 5 |
+
"""
|
| 6 |
+
Fine-tune Qwen3-0.6B on open-r1/codeforces-cots for instruction following.
|
| 7 |
+
This script uses SFT (Supervised Fine-Tuning) with LoRA for efficient training.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from datasets import load_dataset
|
| 11 |
+
from peft import LoraConfig
|
| 12 |
+
from trl import SFTTrainer, SFTConfig
|
| 13 |
+
import trackio
|
| 14 |
+
|
| 15 |
+
print("Loading dataset...")
|
| 16 |
+
dataset = load_dataset("open-r1/codeforces-cots", name="solutions_py_decontaminated", split="train")
|
| 17 |
+
|
| 18 |
+
print(f"Dataset size: {len(dataset)}")
|
| 19 |
+
|
| 20 |
+
# Take a manageable subset for initial training
|
| 21 |
+
# You can increase this later for production training
|
| 22 |
+
dataset = dataset.select(range(min(5000, len(dataset))))
|
| 23 |
+
print(f"Using {len(dataset)} examples for training")
|
| 24 |
+
|
| 25 |
+
# Create train/eval split for monitoring
|
| 26 |
+
dataset_split = dataset.train_test_split(test_size=0.05, seed=42)
|
| 27 |
+
print(f"Train: {len(dataset_split['train'])}, Eval: {len(dataset_split['test'])}")
|
| 28 |
+
|
| 29 |
+
# Configure LoRA for efficient fine-tuning
|
| 30 |
+
peft_config = LoraConfig(
|
| 31 |
+
r=16,
|
| 32 |
+
lora_alpha=32,
|
| 33 |
+
lora_dropout=0.05,
|
| 34 |
+
bias="none",
|
| 35 |
+
task_type="CAUSAL_LM",
|
| 36 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Training configuration
|
| 40 |
+
training_args = SFTConfig(
|
| 41 |
+
output_dir="qwen3-codeforces-sft",
|
| 42 |
+
|
| 43 |
+
# Training parameters
|
| 44 |
+
num_train_epochs=3,
|
| 45 |
+
per_device_train_batch_size=2,
|
| 46 |
+
per_device_eval_batch_size=2,
|
| 47 |
+
gradient_accumulation_steps=8, # Effective batch size = 16
|
| 48 |
+
gradient_checkpointing=True,
|
| 49 |
+
|
| 50 |
+
# Optimization
|
| 51 |
+
learning_rate=2e-4,
|
| 52 |
+
lr_scheduler_type="cosine",
|
| 53 |
+
warmup_ratio=0.1,
|
| 54 |
+
optim="adamw_torch",
|
| 55 |
+
|
| 56 |
+
# Evaluation and saving
|
| 57 |
+
eval_strategy="steps",
|
| 58 |
+
eval_steps=100,
|
| 59 |
+
save_strategy="steps",
|
| 60 |
+
save_steps=200,
|
| 61 |
+
save_total_limit=3,
|
| 62 |
+
|
| 63 |
+
# Logging
|
| 64 |
+
logging_steps=10,
|
| 65 |
+
report_to="trackio",
|
| 66 |
+
|
| 67 |
+
# Hub configuration - CRITICAL for saving results
|
| 68 |
+
push_to_hub=True,
|
| 69 |
+
hub_model_id="udaykiran212/qwen3-0.6b-codeforces-sft",
|
| 70 |
+
hub_strategy="every_save",
|
| 71 |
+
hub_private_repo=False,
|
| 72 |
+
|
| 73 |
+
# Performance
|
| 74 |
+
bf16=True,
|
| 75 |
+
max_grad_norm=1.0,
|
| 76 |
+
|
| 77 |
+
# Dataset formatting
|
| 78 |
+
dataset_text_field="", # We use messages format
|
| 79 |
+
packing=False,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Initialize trainer
|
| 83 |
+
print("Initializing trainer...")
|
| 84 |
+
trainer = SFTTrainer(
|
| 85 |
+
model="Qwen/Qwen2.5-0.5B", # Using Qwen2.5-0.5B as base (closest to 0.6B)
|
| 86 |
+
train_dataset=dataset_split["train"],
|
| 87 |
+
eval_dataset=dataset_split["test"],
|
| 88 |
+
peft_config=peft_config,
|
| 89 |
+
args=training_args,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Start training
|
| 93 |
+
print("Starting training...")
|
| 94 |
+
trainer.train()
|
| 95 |
+
|
| 96 |
+
# Save and push final model
|
| 97 |
+
print("Saving final model...")
|
| 98 |
+
trainer.save_model()
|
| 99 |
+
trainer.push_to_hub()
|
| 100 |
+
|
| 101 |
+
print("Training completed successfully!")
|
| 102 |
+
print(f"Model saved to: https://huggingface.co/udaykiran212/qwen3-0.6b-codeforces-sft")
|