papebaba commited on
Commit
74e3f10
·
verified ·
1 Parent(s): beebee9

Upload train_qwen_codeforces.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_qwen_codeforces.py +20 -1
train_qwen_codeforces.py CHANGED
@@ -1,10 +1,11 @@
1
  # /// script
2
- # dependencies = ["trl>=0.12.0", "peft>=0.7.0", "trackio>=0.1.0", "datasets>=2.0.0"]
3
  # ///
4
 
5
  from datasets import load_dataset
6
  from peft import LoraConfig
7
  from trl import SFTTrainer, SFTConfig
 
8
  import trackio
9
 
10
  # Load dataset - 1000 examples for ~20 min training
@@ -16,6 +17,23 @@ dataset = load_dataset(
16
  )
17
  print(f"📊 Training on {len(dataset)} examples for 3 epochs")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # LoRA configuration for efficient training
20
  peft_config = LoraConfig(
21
  r=8,
@@ -67,6 +85,7 @@ trainer = SFTTrainer(
67
  train_dataset=dataset,
68
  args=config,
69
  peft_config=peft_config,
 
70
  )
71
 
72
  # Train
 
1
  # /// script
2
+ # dependencies = ["trl>=0.12.0", "peft>=0.7.0", "trackio>=0.1.0", "datasets>=2.0.0", "transformers>=4.36.0"]
3
  # ///
4
 
5
  from datasets import load_dataset
6
  from peft import LoraConfig
7
  from trl import SFTTrainer, SFTConfig
8
+ from transformers import AutoTokenizer
9
  import trackio
10
 
11
  # Load dataset - 1000 examples for ~20 min training
 
17
  )
18
  print(f"📊 Training on {len(dataset)} examples for 3 epochs")
19
 
20
+ # Load tokenizer to get chat template
21
+ print("🔤 Loading tokenizer...")
22
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")
23
+
24
+ # Define formatting function for messages
25
+ def formatting_func(example):
26
+ """Convert messages format to text using chat template."""
27
+ if "messages" in example and example["messages"]:
28
+ # Use the tokenizer's chat template to format messages
29
+ text = tokenizer.apply_chat_template(
30
+ example["messages"],
31
+ tokenize=False,
32
+ add_generation_prompt=False
33
+ )
34
+ return {"text": text}
35
+ return {"text": ""}
36
+
37
  # LoRA configuration for efficient training
38
  peft_config = LoraConfig(
39
  r=8,
 
85
  train_dataset=dataset,
86
  args=config,
87
  peft_config=peft_config,
88
+ formatting_func=formatting_func, # Use formatting function for messages
89
  )
90
 
91
  # Train