stmasson commited on
Commit
f5fa8ef
·
verified ·
1 Parent(s): 0882380

Upload train_qwen3_codeforces.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_qwen3_codeforces.py +103 -0
train_qwen3_codeforces.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # dependencies = [
3
+ # "trl>=0.12.0",
4
+ # "peft>=0.7.0",
5
+ # "transformers>=4.50.0",
6
+ # "accelerate>=0.24.0",
7
+ # "trackio",
8
+ # "bitsandbytes",
9
+ # ]
10
+ # ///
11
+
12
+ """
13
+ Fine-tune Qwen3-0.6B on open-r1/codeforces-cots for instruction following.
14
+ Dataset: Competitive programming with chain-of-thought reasoning.
15
+ """
16
+
17
+ import trackio
18
+ from datasets import load_dataset
19
+ from peft import LoraConfig
20
+ from trl import SFTTrainer, SFTConfig
21
+
22
+ # Load dataset with Python solutions (decontaminated)
23
+ print("Loading dataset open-r1/codeforces-cots...")
24
+ dataset = load_dataset(
25
+ "open-r1/codeforces-cots",
26
+ name="solutions_py_decontaminated",
27
+ split="train"
28
+ )
29
+ print(f"Dataset loaded: {len(dataset)} examples")
30
+
31
+ # Create train/eval split
32
+ print("Creating train/eval split...")
33
+ dataset_split = dataset.train_test_split(test_size=0.05, seed=42)
34
+ train_dataset = dataset_split["train"]
35
+ eval_dataset = dataset_split["test"]
36
+ print(f" Train: {len(train_dataset)} examples")
37
+ print(f" Eval: {len(eval_dataset)} examples")
38
+
39
+ # Training configuration
40
+ config = SFTConfig(
41
+ # Hub settings - CRITICAL
42
+ output_dir="qwen3-0.6b-codeforces-cots",
43
+ push_to_hub=True,
44
+ hub_model_id="stmasson/qwen3-0.6b-codeforces-cots",
45
+ hub_strategy="every_save",
46
+
47
+ # Training parameters
48
+ num_train_epochs=1,
49
+ per_device_train_batch_size=2,
50
+ gradient_accumulation_steps=8,
51
+ learning_rate=2e-4,
52
+ max_length=2048,
53
+
54
+ # Logging & checkpointing
55
+ logging_steps=25,
56
+ save_strategy="steps",
57
+ save_steps=500,
58
+ save_total_limit=2,
59
+
60
+ # Evaluation
61
+ eval_strategy="steps",
62
+ eval_steps=500,
63
+
64
+ # Optimization
65
+ warmup_ratio=0.1,
66
+ lr_scheduler_type="cosine",
67
+ bf16=True,
68
+ gradient_checkpointing=True,
69
+
70
+ # Monitoring
71
+ report_to="trackio",
72
+ project="codeforces-finetuning",
73
+ run_name="qwen3-0.6b-codeforces-sft",
74
+ )
75
+
76
+ # LoRA configuration for efficient training
77
+ peft_config = LoraConfig(
78
+ r=32,
79
+ lora_alpha=64,
80
+ lora_dropout=0.05,
81
+ bias="none",
82
+ task_type="CAUSAL_LM",
83
+ target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
84
+ )
85
+
86
+ # Initialize trainer
87
+ print("Initializing trainer with Qwen/Qwen3-0.6B...")
88
+ trainer = SFTTrainer(
89
+ model="Qwen/Qwen3-0.6B",
90
+ train_dataset=train_dataset,
91
+ eval_dataset=eval_dataset,
92
+ args=config,
93
+ peft_config=peft_config,
94
+ )
95
+
96
+ print("Starting training...")
97
+ trainer.train()
98
+
99
+ print("Pushing final model to Hub...")
100
+ trainer.push_to_hub()
101
+
102
+ print("Training complete! Model at: https://huggingface.co/stmasson/qwen3-0.6b-codeforces-cots")
103
+ print("View metrics at: https://huggingface.co/spaces/stmasson/trackio")