Upload train.py
Browse files
train.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
from datasets import load_dataset
|
4 |
+
from peft import LoraConfig, TaskType
|
5 |
+
from trl import SFTTrainer, SFTConfig
|
6 |
+
import trackio
|
7 |
+
|
8 |
+
model_name = "./SmolLM3-3B-Base/"
|
9 |
+
dataset_path = "./MathInstruct/MathInstruct.json"
|
10 |
+
output_dir = "./SmolLMathematician-3B"
|
11 |
+
project_name = "SmolLMathematician-3B"
|
12 |
+
MAX_SEQ_LENGTH = 4096
|
13 |
+
|
14 |
+
trackio.init(project=project_name)
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
16 |
+
model_name,
|
17 |
+
device_map="auto",
|
18 |
+
dtype=torch.bfloat16,
|
19 |
+
low_cpu_mem_usage=True,
|
20 |
+
trust_remote_code=True,
|
21 |
+
attn_implementation="flash_attention_2",
|
22 |
+
)
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
24 |
+
if tokenizer.pad_token is None:
|
25 |
+
tokenizer.pad_token = tokenizer.eos_token
|
26 |
+
model.config.pad_token_id = model.config.eos_token_id
|
27 |
+
|
28 |
+
with open("chat_template.jinja", "r") as f:
|
29 |
+
chat_template = f.read()
|
30 |
+
tokenizer.chat_template = chat_template
|
31 |
+
|
32 |
+
model.gradient_checkpointing_enable()
|
33 |
+
dataset = load_dataset("json", data_files=dataset_path, split="train")
|
34 |
+
|
35 |
+
def formatInstructionWithTemplate(example: dict) -> str:
|
36 |
+
messages = [
|
37 |
+
{"role": "user", "content": example["instruction"]},
|
38 |
+
{"role": "assistant", "content": example["output"]},
|
39 |
+
]
|
40 |
+
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
|
41 |
+
|
42 |
+
|
43 |
+
def checkSequenceLength(example: dict) -> bool:
|
44 |
+
formatted_text = formatInstructionWithTemplate(example)
|
45 |
+
tokens = tokenizer(formatted_text)
|
46 |
+
return len(tokens['input_ids']) <= MAX_SEQ_LENGTH
|
47 |
+
|
48 |
+
original_size = len(dataset)
|
49 |
+
train_dataset = dataset.filter(checkSequenceLength)
|
50 |
+
new_size = len(train_dataset)
|
51 |
+
|
52 |
+
print(f"Dataset: {original_size} → {new_size} samples (removed: {original_size - new_size})")
|
53 |
+
|
54 |
+
torch.cuda.empty_cache()
|
55 |
+
peft_config = LoraConfig(
|
56 |
+
r=16,
|
57 |
+
lora_alpha=32,
|
58 |
+
lora_dropout=0.1,
|
59 |
+
target_modules=['q_proj', 'v_proj'],
|
60 |
+
bias="none",
|
61 |
+
task_type=TaskType.CAUSAL_LM,
|
62 |
+
)
|
63 |
+
|
64 |
+
training_args = SFTConfig(
|
65 |
+
output_dir=output_dir,
|
66 |
+
num_train_epochs=1,
|
67 |
+
per_device_train_batch_size=2,
|
68 |
+
gradient_accumulation_steps=8,
|
69 |
+
optim="paged_adamw_8bit",
|
70 |
+
learning_rate=2e-5,
|
71 |
+
weight_decay=0.01,
|
72 |
+
adam_epsilon=1e-6,
|
73 |
+
max_grad_norm=1.0,
|
74 |
+
lr_scheduler_type="cosine",
|
75 |
+
warmup_ratio=0.1,
|
76 |
+
logging_steps=8,
|
77 |
+
eval_strategy="no",
|
78 |
+
save_strategy="steps",
|
79 |
+
save_steps=32,
|
80 |
+
save_total_limit=4,
|
81 |
+
resume_from_checkpoint=True,
|
82 |
+
report_to="trackio",
|
83 |
+
bf16=True,
|
84 |
+
packing=True,
|
85 |
+
max_length=MAX_SEQ_LENGTH,
|
86 |
+
dataloader_pin_memory=False,
|
87 |
+
gradient_checkpointing_kwargs={"use_reentrant": False},
|
88 |
+
)
|
89 |
+
|
90 |
+
trainer = SFTTrainer(
|
91 |
+
model=model,
|
92 |
+
args=training_args,
|
93 |
+
train_dataset=train_dataset,
|
94 |
+
peft_config=peft_config,
|
95 |
+
formatting_func=formatInstructionWithTemplate,
|
96 |
+
)
|
97 |
+
|
98 |
+
torch.cuda.empty_cache()
|
99 |
+
trainer.train()
|
100 |
+
torch.cuda.empty_cache()
|
101 |
+
trainer.save_model(output_dir)
|
102 |
+
print(f"LoRA adapter saved to {output_dir}")
|
103 |
+
trackio.finish()
|