atlaswang commited on
Commit
b4240d5
·
verified ·
1 Parent(s): 19225c3

Upload train_qwen3_codeforces.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_qwen3_codeforces.py +105 -0
train_qwen3_codeforces.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "trl>=0.12.0",
5
+ # "peft>=0.7.0",
6
+ # "transformers>=4.45.0",
7
+ # "accelerate>=0.24.0",
8
+ # "trackio",
9
+ # ]
10
+ # ///
11
+
12
+ """
13
+ Fine-tune Qwen3-0.6B on open-r1/codeforces-cots for instruction following.
14
+ Uses SFT with LoRA for efficient training.
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 - uses messages format which is TRL-compatible
23
+ print("Loading open-r1/codeforces-cots dataset...")
24
+ dataset = load_dataset("open-r1/codeforces-cots", split="train")
25
+ print(f"Dataset loaded: {len(dataset)} examples")
26
+
27
+ # Sample for faster training (adjust as needed)
28
+ if len(dataset) > 10000:
29
+ dataset = dataset.shuffle(seed=42).select(range(10000))
30
+ print(f"Sampled to 10,000 examples for training")
31
+
32
+ # Create train/eval split
33
+ print("Creating train/eval split...")
34
+ dataset_split = dataset.train_test_split(test_size=0.05, seed=42)
35
+ train_dataset = dataset_split["train"]
36
+ eval_dataset = dataset_split["test"]
37
+ print(f"Train: {len(train_dataset)} examples")
38
+ print(f"Eval: {len(eval_dataset)} examples")
39
+
40
+ # Training configuration
41
+ config = SFTConfig(
42
+ # Hub settings
43
+ output_dir="qwen3-0.6b-codeforces-sft",
44
+ push_to_hub=True,
45
+ hub_model_id="atlaswang/qwen3-0.6b-codeforces-sft",
46
+ hub_strategy="every_save",
47
+
48
+ # Training parameters
49
+ num_train_epochs=3,
50
+ per_device_train_batch_size=2,
51
+ gradient_accumulation_steps=8,
52
+ learning_rate=2e-4,
53
+ max_length=2048,
54
+
55
+ # Logging & checkpointing
56
+ logging_steps=10,
57
+ save_strategy="steps",
58
+ save_steps=200,
59
+ save_total_limit=3,
60
+
61
+ # Evaluation
62
+ eval_strategy="steps",
63
+ eval_steps=200,
64
+
65
+ # Optimization
66
+ warmup_ratio=0.1,
67
+ lr_scheduler_type="cosine",
68
+ gradient_checkpointing=True,
69
+ bf16=True,
70
+
71
+ # Monitoring
72
+ report_to="trackio",
73
+ project="qwen3-codeforces-sft",
74
+ run_name="qwen3-0.6b-codeforces-instruction-tuning",
75
+ )
76
+
77
+ # LoRA configuration
78
+ peft_config = LoraConfig(
79
+ r=32,
80
+ lora_alpha=64,
81
+ lora_dropout=0.05,
82
+ bias="none",
83
+ task_type="CAUSAL_LM",
84
+ target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
85
+ )
86
+
87
+ # Initialize and train
88
+ print("Initializing trainer with Qwen/Qwen3-0.6B...")
89
+ trainer = SFTTrainer(
90
+ model="Qwen/Qwen3-0.6B",
91
+ train_dataset=train_dataset,
92
+ eval_dataset=eval_dataset,
93
+ args=config,
94
+ peft_config=peft_config,
95
+ )
96
+
97
+ print("Starting training...")
98
+ trainer.train()
99
+
100
+ print("Pushing to Hub...")
101
+ trainer.push_to_hub()
102
+
103
+ trackio.finish()
104
+
105
+ print("Training complete! Model at: https://huggingface.co/atlaswang/qwen3-0.6b-codeforces-sft")