jonpauldavies commited on
Commit
4992055
·
verified ·
1 Parent(s): 6d1a012

Upload train_qwen3_codeforces.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_qwen3_codeforces.py +120 -0
train_qwen3_codeforces.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # "datasets",
8
+ # "trackio",
9
+ # "torch",
10
+ # ]
11
+ # ///
12
+
13
+ """
14
+ SFT training for Qwen3-0.6B on open-r1/codeforces-cots dataset.
15
+ Fine-tunes for instruction following on competitive programming tasks.
16
+ """
17
+
18
+ import os
19
+ import trackio
20
+ from datasets import load_dataset
21
+ from peft import LoraConfig
22
+ from trl import SFTTrainer, SFTConfig
23
+
24
+ # Get HF username for model repo
25
+ HF_USERNAME = os.environ.get("HF_USERNAME", "jonpauldavies")
26
+
27
+ # Load dataset - using the configuration with editorials for richer training
28
+ print("Loading dataset...")
29
+ dataset = load_dataset(
30
+ "open-r1/codeforces-cots",
31
+ "solutions_w_editorials", # Config with editorials for better instruction data
32
+ split="train"
33
+ )
34
+ print(f"Dataset loaded: {len(dataset)} examples")
35
+
36
+ # Filter to only examples with messages (some may be empty)
37
+ dataset = dataset.filter(lambda x: x.get("messages") and len(x["messages"]) > 0)
38
+ print(f"After filtering: {len(dataset)} examples with messages")
39
+
40
+ # Limit dataset size for t4-small training
41
+ MAX_SAMPLES = 5000
42
+ if len(dataset) > MAX_SAMPLES:
43
+ dataset = dataset.shuffle(seed=42).select(range(MAX_SAMPLES))
44
+ print(f"Limited to {MAX_SAMPLES} samples for training")
45
+
46
+ # Create train/eval split
47
+ print("Creating train/eval split...")
48
+ dataset_split = dataset.train_test_split(test_size=0.05, seed=42)
49
+ train_dataset = dataset_split["train"]
50
+ eval_dataset = dataset_split["test"]
51
+ print(f"Train: {len(train_dataset)} examples")
52
+ print(f"Eval: {len(eval_dataset)} examples")
53
+
54
+ # Training configuration
55
+ config = SFTConfig(
56
+ # Hub settings - CRITICAL for saving results
57
+ output_dir="qwen3-codeforces-sft",
58
+ push_to_hub=True,
59
+ hub_model_id=f"{HF_USERNAME}/qwen3-0.6b-codeforces-sft",
60
+ hub_strategy="every_save",
61
+ hub_private_repo=False,
62
+
63
+ # Training parameters (optimized for t4-small)
64
+ num_train_epochs=1,
65
+ per_device_train_batch_size=1,
66
+ gradient_accumulation_steps=8, # Effective batch size = 8
67
+ learning_rate=2e-5,
68
+ max_length=1024, # Moderate context for t4-small memory
69
+
70
+ # Logging & checkpointing
71
+ logging_steps=25,
72
+ save_strategy="steps",
73
+ save_steps=200,
74
+ save_total_limit=2,
75
+
76
+ # Skip evaluation for demo to save memory on t4-small
77
+ # eval_strategy="steps",
78
+ # eval_steps=200,
79
+
80
+ # Optimization
81
+ warmup_ratio=0.1,
82
+ lr_scheduler_type="cosine",
83
+ gradient_checkpointing=True, # Save memory
84
+ fp16=True, # T4 doesn't support bf16, use fp16
85
+
86
+ # Monitoring with Trackio
87
+ report_to="trackio",
88
+ project="qwen3-codeforces-training",
89
+ run_name="sft-qwen3-0.6b-codeforces",
90
+ )
91
+
92
+ # LoRA configuration for efficient training
93
+ peft_config = LoraConfig(
94
+ r=32,
95
+ lora_alpha=64,
96
+ lora_dropout=0.05,
97
+ bias="none",
98
+ task_type="CAUSAL_LM",
99
+ target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
100
+ )
101
+
102
+ # Initialize trainer (no eval_dataset for demo to save memory)
103
+ print("Initializing trainer...")
104
+ trainer = SFTTrainer(
105
+ model="Qwen/Qwen3-0.6B",
106
+ train_dataset=train_dataset,
107
+ args=config,
108
+ peft_config=peft_config,
109
+ )
110
+
111
+ # Train
112
+ print("Starting training...")
113
+ trainer.train()
114
+
115
+ # Push final model to Hub
116
+ print("Pushing to Hub...")
117
+ trainer.push_to_hub()
118
+
119
+ print(f"Training complete!")
120
+ print(f"Model saved to: https://huggingface.co/{HF_USERNAME}/qwen3-0.6b-codeforces-sft")