gilbaes commited on
Commit
413b449
·
verified ·
1 Parent(s): 60f3f5e

Upload train_qwen3_hf_v2.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_qwen3_hf_v2.py +83 -0
train_qwen3_hf_v2.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # dependencies = ["trl>=0.12.0", "peft>=0.7.0", "transformers>=4.45.0", "datasets", "accelerate", "torch"]
3
+ # ///
4
+ """Fine-tune Qwen3-0.6B on CodeForces-CoTS (100 examples)"""
5
+
6
+ import os
7
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
8
+
9
+ from datasets import load_dataset
10
+ from peft import LoraConfig
11
+ from trl import SFTTrainer, SFTConfig
12
+ import torch
13
+
14
+ print(f"CUDA available: {torch.cuda.is_available()}")
15
+ if torch.cuda.is_available():
16
+ print(f"GPU: {torch.cuda.get_device_name(0)}")
17
+ print(f"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB")
18
+
19
+ # Load 100 examples
20
+ print("\nLoading dataset...")
21
+ dataset = load_dataset("open-r1/codeforces-cots", "solutions", split="train").select(range(100))
22
+ print(f"Dataset: {len(dataset)} examples")
23
+
24
+ # Split: 90 train, 10 val
25
+ splits = dataset.train_test_split(test_size=0.1, seed=42)
26
+ train_ds, val_ds = splits["train"], splits["test"]
27
+ print(f"Train: {len(train_ds)}, Val: {len(val_ds)}")
28
+
29
+ peft_config = LoraConfig(
30
+ r=8,
31
+ lora_alpha=16,
32
+ lora_dropout=0.05,
33
+ target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
34
+ bias="none",
35
+ task_type="CAUSAL_LM"
36
+ )
37
+
38
+ # 90 examples, batch=1, accum=4 -> ~22 steps/epoch
39
+ # logging every 2 steps = every ~8 examples
40
+ training_args = SFTConfig(
41
+ output_dir="./qwen3-0.6b-codeforces-cots",
42
+ num_train_epochs=1,
43
+ per_device_train_batch_size=1,
44
+ gradient_accumulation_steps=4,
45
+ learning_rate=2e-4,
46
+ warmup_ratio=0.1,
47
+ logging_steps=2, # Log every ~8 examples
48
+ logging_first_step=True,
49
+ save_strategy="no",
50
+ eval_strategy="steps",
51
+ eval_steps=5, # Eval every ~20 examples
52
+ max_length=1024,
53
+ push_to_hub=True,
54
+ hub_model_id="gilbaes/qwen3-0.6b-codeforces-cots",
55
+ report_to="none",
56
+ bf16=True,
57
+ gradient_checkpointing=True,
58
+ optim="adamw_torch_fused",
59
+ )
60
+
61
+ print("\nInitializing trainer...")
62
+ trainer = SFTTrainer(
63
+ model="Qwen/Qwen3-0.6B",
64
+ train_dataset=train_ds,
65
+ eval_dataset=val_ds,
66
+ peft_config=peft_config,
67
+ args=training_args,
68
+ )
69
+
70
+ print(f"Trainable params: {trainer.model.num_parameters(only_trainable=True):,}")
71
+ print(f"Total params: {trainer.model.num_parameters():,}")
72
+
73
+ print("\n" + "="*50)
74
+ print("TRAINING START")
75
+ print("="*50 + "\n")
76
+
77
+ trainer.train()
78
+
79
+ print("\n" + "="*50)
80
+ print("PUSHING TO HUB")
81
+ print("="*50)
82
+ trainer.push_to_hub()
83
+ print("\nDone! Model: https://huggingface.co/gilbaes/qwen3-0.6b-codeforces-cots")