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7.0", "trackio", "transformers>=4.44.0", "datasets"]
|
| 3 |
+
# ///
|
| 4 |
+
|
| 5 |
+
import sys
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
from peft import LoraConfig
|
| 8 |
+
from trl import SFTTrainer, SFTConfig
|
| 9 |
+
import trackio
|
| 10 |
+
|
| 11 |
+
print("="*60)
|
| 12 |
+
print("π STARTING TRAINING JOB - VERBOSE MODE")
|
| 13 |
+
print("="*60)
|
| 14 |
+
|
| 15 |
+
# Step 1: Load dataset
|
| 16 |
+
print("\nπ₯ Step 1/5: Loading dataset...")
|
| 17 |
+
try:
|
| 18 |
+
dataset = load_dataset(
|
| 19 |
+
"open-r1/codeforces-cots",
|
| 20 |
+
name="solutions_w_editorials_decontaminated",
|
| 21 |
+
split="train[:500]" # Small subset for quick testing
|
| 22 |
+
)
|
| 23 |
+
print(f"β
Dataset loaded: {len(dataset)} examples")
|
| 24 |
+
print(f" Columns: {dataset.column_names}")
|
| 25 |
+
print(f" First example keys: {list(dataset[0].keys())}")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"β FAILED to load dataset: {e}")
|
| 28 |
+
sys.exit(1)
|
| 29 |
+
|
| 30 |
+
# Step 2: Create train/eval split
|
| 31 |
+
print("\nπ Step 2/5: Creating train/eval split...")
|
| 32 |
+
try:
|
| 33 |
+
dataset_split = dataset.train_test_split(test_size=0.1, seed=42)
|
| 34 |
+
print(f"β
Split created:")
|
| 35 |
+
print(f" Train: {len(dataset_split['train'])} examples")
|
| 36 |
+
print(f" Eval: {len(dataset_split['test'])} examples")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"β FAILED to create split: {e}")
|
| 39 |
+
sys.exit(1)
|
| 40 |
+
|
| 41 |
+
# Step 3: Configure LoRA
|
| 42 |
+
print("\nπ§ Step 3/5: Configuring LoRA...")
|
| 43 |
+
try:
|
| 44 |
+
peft_config = LoraConfig(
|
| 45 |
+
r=16,
|
| 46 |
+
lora_alpha=32,
|
| 47 |
+
lora_dropout=0.05,
|
| 48 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 49 |
+
task_type="CAUSAL_LM"
|
| 50 |
+
)
|
| 51 |
+
print(f"β
LoRA configured: r={peft_config.r}, alpha={peft_config.lora_alpha}")
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"β FAILED to configure LoRA: {e}")
|
| 54 |
+
sys.exit(1)
|
| 55 |
+
|
| 56 |
+
# Step 4: Configure training
|
| 57 |
+
print("\nβοΈ Step 4/5: Configuring training...")
|
| 58 |
+
try:
|
| 59 |
+
training_args = SFTConfig(
|
| 60 |
+
output_dir="qwen3-0.6b-codeforces-test",
|
| 61 |
+
|
| 62 |
+
# Quick training for testing
|
| 63 |
+
num_train_epochs=1, # Just 1 epoch for quick test
|
| 64 |
+
per_device_train_batch_size=2,
|
| 65 |
+
per_device_eval_batch_size=2,
|
| 66 |
+
gradient_accumulation_steps=2,
|
| 67 |
+
gradient_checkpointing=True,
|
| 68 |
+
|
| 69 |
+
# Learning rate
|
| 70 |
+
learning_rate=2e-4,
|
| 71 |
+
lr_scheduler_type="cosine",
|
| 72 |
+
warmup_ratio=0.1,
|
| 73 |
+
optim="paged_adamw_8bit",
|
| 74 |
+
|
| 75 |
+
# Frequent logging for visibility
|
| 76 |
+
eval_strategy="steps",
|
| 77 |
+
eval_steps=20,
|
| 78 |
+
logging_steps=5, # Log every 5 steps
|
| 79 |
+
save_strategy="steps",
|
| 80 |
+
save_steps=50,
|
| 81 |
+
save_total_limit=2,
|
| 82 |
+
|
| 83 |
+
# Hub integration
|
| 84 |
+
push_to_hub=True,
|
| 85 |
+
hub_model_id="kneeraj/qwen3-0.6b-codeforces-test",
|
| 86 |
+
hub_strategy="every_save",
|
| 87 |
+
hub_private_repo=False,
|
| 88 |
+
|
| 89 |
+
# Trackio monitoring
|
| 90 |
+
report_to="trackio",
|
| 91 |
+
project="codeforces-finetuning-test",
|
| 92 |
+
run_name="qwen3-quick-test",
|
| 93 |
+
|
| 94 |
+
# Performance
|
| 95 |
+
bf16=True,
|
| 96 |
+
max_grad_norm=1.0,
|
| 97 |
+
|
| 98 |
+
# Data processing
|
| 99 |
+
max_seq_length=1024, # Shorter for faster processing
|
| 100 |
+
dataset_text_field="messages",
|
| 101 |
+
packing=False,
|
| 102 |
+
)
|
| 103 |
+
print(f"β
Training config created")
|
| 104 |
+
print(f" Epochs: {training_args.num_train_epochs}")
|
| 105 |
+
print(f" Batch size: {training_args.per_device_train_batch_size}")
|
| 106 |
+
print(f" Output: {training_args.hub_model_id}")
|
| 107 |
+
except Exception as e:
|
| 108 |
+
print(f"β FAILED to configure training: {e}")
|
| 109 |
+
sys.exit(1)
|
| 110 |
+
|
| 111 |
+
# Step 5: Initialize trainer and train
|
| 112 |
+
print("\nποΈ Step 5/5: Initializing trainer and starting training...")
|
| 113 |
+
try:
|
| 114 |
+
print(" Loading model: Qwen/Qwen2.5-0.5B-Instruct...")
|
| 115 |
+
trainer = SFTTrainer(
|
| 116 |
+
model="Qwen/Qwen2.5-0.5B-Instruct",
|
| 117 |
+
train_dataset=dataset_split["train"],
|
| 118 |
+
eval_dataset=dataset_split["test"],
|
| 119 |
+
peft_config=peft_config,
|
| 120 |
+
args=training_args,
|
| 121 |
+
)
|
| 122 |
+
print(f"β
Trainer initialized")
|
| 123 |
+
print(f" Training samples: {len(dataset_split['train'])}")
|
| 124 |
+
print(f" Evaluation samples: {len(dataset_split['test'])}")
|
| 125 |
+
|
| 126 |
+
print("\n" + "="*60)
|
| 127 |
+
print("π― STARTING TRAINING...")
|
| 128 |
+
print("="*60 + "\n")
|
| 129 |
+
|
| 130 |
+
trainer.train()
|
| 131 |
+
|
| 132 |
+
print("\n" + "="*60)
|
| 133 |
+
print("πΎ Pushing final model to Hub...")
|
| 134 |
+
trainer.push_to_hub()
|
| 135 |
+
|
| 136 |
+
print("\n" + "="*60)
|
| 137 |
+
print("β
TRAINING COMPLETE!")
|
| 138 |
+
print("="*60)
|
| 139 |
+
print(f"Model saved to: kneeraj/qwen3-0.6b-codeforces-test")
|
| 140 |
+
print(f"View at: https://huggingface.co/kneeraj/qwen3-0.6b-codeforces-test")
|
| 141 |
+
|
| 142 |
+
except Exception as e:
|
| 143 |
+
print(f"\nβ TRAINING FAILED: {e}")
|
| 144 |
+
import traceback
|
| 145 |
+
traceback.print_exc()
|
| 146 |
+
sys.exit(1)
|