nathens commited on
Commit
d964b7b
ยท
verified ยท
1 Parent(s): 4f3139d

Upload train_qwen_hf_jobs.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_qwen_hf_jobs.py +86 -0
train_qwen_hf_jobs.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # dependencies = ["trl>=0.12.0", "peft>=0.7.0", "trackio", "transformers", "datasets", "torch"]
3
+ # ///
4
+
5
+ """
6
+ Fine-tune Qwen2.5-0.5B on open-r1/codeforces-cots for instruction following.
7
+ Production-ready script with LoRA, Trackio monitoring, and Hub saving.
8
+ """
9
+
10
+ from datasets import load_dataset
11
+ from peft import LoraConfig
12
+ from trl import SFTTrainer, SFTConfig
13
+ import trackio
14
+
15
+ # Load dataset - using the "messages" field for chat format
16
+ print("๐Ÿ“š Loading dataset: open-r1/codeforces-cots")
17
+ dataset = load_dataset("open-r1/codeforces-cots", "solutions", split="train")
18
+
19
+ # For demo purposes, use a subset. Remove this line for full training.
20
+ dataset = dataset.select(range(min(1000, len(dataset))))
21
+ print(f"๐Ÿ“Š Training on {len(dataset)} examples")
22
+
23
+ # Create train/eval split for monitoring
24
+ dataset_split = dataset.train_test_split(test_size=0.1, seed=42)
25
+
26
+ # Configure LoRA for efficient training
27
+ lora_config = LoraConfig(
28
+ r=16,
29
+ lora_alpha=32,
30
+ target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
31
+ lora_dropout=0.05,
32
+ bias="none",
33
+ task_type="CAUSAL_LM"
34
+ )
35
+
36
+ # Initialize trainer with SFT configuration
37
+ print("๐ŸŽฏ Initializing SFTTrainer")
38
+ trainer = SFTTrainer(
39
+ model="Qwen/Qwen2.5-0.5B",
40
+ train_dataset=dataset_split["train"],
41
+ eval_dataset=dataset_split["test"],
42
+ peft_config=lora_config,
43
+ args=SFTConfig(
44
+ # Output and Hub settings
45
+ output_dir="qwen-codeforces-sft",
46
+ push_to_hub=True,
47
+ hub_model_id="nathens/qwen-codeforces-sft",
48
+ hub_strategy="every_save",
49
+
50
+ # Training hyperparameters
51
+ num_train_epochs=1,
52
+ per_device_train_batch_size=2,
53
+ gradient_accumulation_steps=4,
54
+ learning_rate=2e-4,
55
+
56
+ # Evaluation and logging
57
+ eval_strategy="steps",
58
+ eval_steps=50,
59
+ logging_steps=10,
60
+ save_steps=100,
61
+ save_total_limit=2,
62
+
63
+ # Optimization settings
64
+ bf16=True,
65
+ gradient_checkpointing=True,
66
+ optim="adamw_torch",
67
+ lr_scheduler_type="cosine",
68
+ warmup_ratio=0.1,
69
+
70
+ # Monitoring with Trackio
71
+ report_to="trackio",
72
+ project="codeforces-instruction-tuning",
73
+ run_name="qwen-codeforces-v1",
74
+ )
75
+ )
76
+
77
+ # Train
78
+ print("๐Ÿ‹๏ธ Starting training...")
79
+ trainer.train()
80
+
81
+ # Save final model
82
+ print("๐Ÿ’พ Saving final model to Hub")
83
+ trainer.push_to_hub()
84
+
85
+ print("โœ… Training complete!")
86
+ print(f"๐Ÿ“ Model available at: https://huggingface.co/nathens/qwen-codeforces-sft")