mjschock commited on
Commit
7749830
·
unverified ·
1 Parent(s): 352f525

Add training script for SmolLM2-135M model using Unsloth. Includes model loading, dataset preparation, and training configuration. Provides detailed instructions for setup and execution.

Browse files
Files changed (1) hide show
  1. train.py +174 -0
train.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Fine-tuning script for SmolLM2-135M model using Unsloth.
4
+
5
+ This script demonstrates how to:
6
+ 1. Install and configure Unsloth
7
+ 2. Prepare and format training data
8
+ 3. Configure and run the training process
9
+ 4. Save and evaluate the model
10
+
11
+ To run this script:
12
+ 1. Install dependencies: pip install -r requirements.txt
13
+ 2. Run: python train.py
14
+ """
15
+
16
+ import os
17
+ from typing import Union
18
+
19
+ from datasets import (
20
+ Dataset,
21
+ DatasetDict,
22
+ IterableDataset,
23
+ IterableDatasetDict,
24
+ load_dataset,
25
+ )
26
+ from transformers import AutoTokenizer, Trainer, TrainingArguments
27
+ from trl import SFTTrainer
28
+ from unsloth import FastLanguageModel, is_bfloat16_supported
29
+ from unsloth.chat_templates import get_chat_template
30
+
31
+ # Configuration
32
+ max_seq_length = 2048 # Auto supports RoPE Scaling internally
33
+ dtype = (
34
+ None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
35
+ )
36
+ load_in_4bit = True # Use 4bit quantization to reduce memory usage
37
+
38
+ # def install_dependencies():
39
+ # """Install required dependencies."""
40
+ # os.system('pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"')
41
+ # os.system('pip install --no-deps xformers trl peft accelerate bitsandbytes')
42
+
43
+
44
+ def load_model() -> tuple[FastLanguageModel, AutoTokenizer]:
45
+ """Load and configure the model."""
46
+ model, tokenizer = FastLanguageModel.from_pretrained(
47
+ model_name="unsloth/SmolLM2-135M-Instruct-bnb-4bit",
48
+ max_seq_length=max_seq_length,
49
+ dtype=dtype,
50
+ load_in_4bit=load_in_4bit,
51
+ )
52
+
53
+ # Configure LoRA
54
+ model = FastLanguageModel.get_peft_model(
55
+ model,
56
+ r=64,
57
+ target_modules=[
58
+ "q_proj",
59
+ "k_proj",
60
+ "v_proj",
61
+ "o_proj",
62
+ "gate_proj",
63
+ "up_proj",
64
+ "down_proj",
65
+ ],
66
+ lora_alpha=128,
67
+ lora_dropout=0.05,
68
+ bias="none",
69
+ use_gradient_checkpointing="unsloth",
70
+ random_state=3407,
71
+ use_rslora=True,
72
+ loftq_config=None,
73
+ )
74
+
75
+ return model, tokenizer
76
+
77
+
78
+ def load_and_format_dataset(
79
+ tokenizer: AutoTokenizer,
80
+ ) -> tuple[
81
+ Union[DatasetDict, Dataset, IterableDatasetDict, IterableDataset], AutoTokenizer
82
+ ]:
83
+ """Load and format the training dataset."""
84
+ # Load the code-act dataset
85
+ dataset = load_dataset("xingyaoww/code-act", split="codeact")
86
+
87
+ # Configure chat template
88
+ tokenizer = get_chat_template(
89
+ tokenizer,
90
+ chat_template="chatml", # Supports zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
91
+ mapping={
92
+ "role": "from",
93
+ "content": "value",
94
+ "user": "human",
95
+ "assistant": "gpt",
96
+ }, # ShareGPT style
97
+ map_eos_token=True, # Maps <|im_end|> to </s> instead
98
+ )
99
+
100
+ def formatting_prompts_func(examples):
101
+ convos = examples["conversations"]
102
+ texts = [
103
+ tokenizer.apply_chat_template(
104
+ convo, tokenize=False, add_generation_prompt=False
105
+ )
106
+ for convo in convos
107
+ ]
108
+ return {"text": texts}
109
+
110
+ # Apply formatting to dataset
111
+ dataset = dataset.map(formatting_prompts_func, batched=True)
112
+
113
+ return dataset, tokenizer
114
+
115
+
116
+ def create_trainer(
117
+ model: FastLanguageModel,
118
+ tokenizer: AutoTokenizer,
119
+ dataset: Union[DatasetDict, Dataset, IterableDatasetDict, IterableDataset],
120
+ ) -> Trainer:
121
+ """Create and configure the SFTTrainer."""
122
+ return SFTTrainer(
123
+ model=model,
124
+ tokenizer=tokenizer,
125
+ train_dataset=dataset,
126
+ dataset_text_field="text",
127
+ max_seq_length=max_seq_length,
128
+ dataset_num_proc=2,
129
+ packing=False,
130
+ args=TrainingArguments(
131
+ per_device_train_batch_size=2,
132
+ gradient_accumulation_steps=16,
133
+ warmup_steps=100,
134
+ max_steps=120,
135
+ learning_rate=5e-5,
136
+ fp16=not is_bfloat16_supported(),
137
+ bf16=is_bfloat16_supported(),
138
+ logging_steps=1,
139
+ optim="adamw_8bit",
140
+ weight_decay=0.01,
141
+ lr_scheduler_type="cosine_with_restarts",
142
+ seed=3407,
143
+ output_dir="outputs",
144
+ gradient_checkpointing=True,
145
+ save_strategy="steps",
146
+ save_steps=30,
147
+ save_total_limit=2,
148
+ ),
149
+ )
150
+
151
+
152
+ def main():
153
+ """Main training function."""
154
+ # Install dependencies
155
+ # install_dependencies()
156
+
157
+ # Load model and tokenizer
158
+ model, tokenizer = load_model()
159
+
160
+ # Load and prepare dataset
161
+ dataset, tokenizer = load_and_format_dataset(tokenizer)
162
+
163
+ # Create trainer
164
+ trainer: Trainer = create_trainer(model, tokenizer, dataset)
165
+
166
+ # Train
167
+ trainer.train()
168
+
169
+ # Save model
170
+ trainer.save_model("final_model")
171
+
172
+
173
+ if __name__ == "__main__":
174
+ main()