File size: 3,030 Bytes
e620ebc 74e3f10 e620ebc 74e3f10 e620ebc beebee9 e620ebc cb94b3f e620ebc 74e3f10 cb94b3f 74e3f10 cb94b3f 74e3f10 beebee9 e620ebc beebee9 cb94b3f beebee9 e620ebc beebee9 e620ebc beebee9 e620ebc beebee9 e620ebc beebee9 |
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 |
# /// script
# dependencies = ["trl>=0.12.0", "peft>=0.7.0", "trackio>=0.1.0", "datasets>=2.0.0", "transformers>=4.36.0"]
# ///
from datasets import load_dataset
from peft import LoraConfig
from trl import SFTTrainer, SFTConfig
from transformers import AutoTokenizer
import trackio
# Load dataset - 1000 examples for ~20 min training
print("π¦ Loading dataset...")
dataset = load_dataset(
"open-r1/codeforces-cots",
"solutions_w_editorials_py_decontaminated",
split="train[:1000]"
)
print(f"π Loaded {len(dataset)} examples")
# Load tokenizer to get chat template
print("π€ Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")
# Pre-process dataset - convert messages to text format
print("π Converting messages to text format...")
def convert_messages_to_text(example):
"""Convert messages format to text using chat template."""
if "messages" in example and example["messages"]:
text = tokenizer.apply_chat_template(
example["messages"],
tokenize=False,
add_generation_prompt=False
)
return {"text": text}
return {"text": ""}
# Apply the conversion
dataset = dataset.map(convert_messages_to_text, remove_columns=dataset.column_names)
print(f"β
Dataset preprocessed - training on {len(dataset)} examples for 3 epochs")
# LoRA configuration for efficient training
peft_config = LoraConfig(
r=8,
lora_alpha=16,
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"]
)
# Training configuration - optimized for T4 small
config = SFTConfig(
# Hub settings - CRITICAL for saving results
output_dir="qwen-codeforces-finetuned",
push_to_hub=True,
hub_model_id="papebaba/qwen-codeforces-finetuned",
hub_strategy="end",
hub_private_repo=False,
# Training parameters
num_train_epochs=3,
per_device_train_batch_size=1,
gradient_accumulation_steps=8, # Effective batch size = 8
learning_rate=2e-4,
max_length=512, # Shorter sequences for T4 small
# Checkpointing
logging_steps=10,
save_strategy="epoch",
save_total_limit=1,
# Optimization for T4 small
gradient_checkpointing=True,
bf16=True,
max_grad_norm=1.0,
warmup_ratio=0.1,
lr_scheduler_type="cosine",
optim="adamw_torch",
# Trackio monitoring
report_to="trackio",
run_name="qwen-codeforces-sft-1k",
)
# Initialize trainer with preprocessed dataset
print("π― Initializing trainer...")
trainer = SFTTrainer(
model="Qwen/Qwen2.5-0.5B",
train_dataset=dataset,
args=config,
peft_config=peft_config,
)
# Train
print("π Starting training on T4 small...")
trainer.train()
# Push to Hub
print("π€ Pushing final model to Hub...")
trainer.push_to_hub()
print("β
Training complete!")
print("π View metrics at: https://huggingface.co/spaces/papebaba/trackio")
print("π€ Model at: https://huggingface.co/papebaba/qwen-codeforces-finetuned")
|