-
Notifications
You must be signed in to change notification settings - Fork 0
/
stories_train_model_v9.py
131 lines (111 loc) · 3.18 KB
/
stories_train_model_v9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import torch
from datasets import load_dataset, Dataset
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
Trainer,
TrainingArguments,
)
from peft.tuners.lora import LoraConfig
from peft.mapping import get_peft_model
import wandb
from dotenv import load_dotenv
import polars as pl
from utils import stories_dataset
from liger_kernel.transformers import _apply_liger_kernel_to_instance
from training_helpers import compute_metrics
load_dotenv("/workspace/.env")
# Configuration
base_model = "Qwen/Qwen2.5-14B"
run_name = __file__.split("/")[-1].replace(".py", "")
output_dir = f"./models/{run_name}"
num_epochs = 1
batch_size = 2
gradient_accumulation_steps = 8
learning_rate = 2e-4
max_length = 4096
# Initialize wandb
wandb.init(project="hn_stories_model_training", name=run_name)
def create_dataset(split, num_rows, tokenizer):
stories = stories_dataset()
stories = stories.filter(pl.col("split") == split).head(num_rows)
stories = stories.with_columns(
[
pl.col("serialized").alias("text"),
pl.col("log_score").alias("label"),
]
)
stories = stories.with_columns(
[
pl.col("text")
.map_elements(
lambda x: tokenizer(x)["input_ids"], return_dtype=pl.List(pl.Int64)
)
.alias("input_ids"),
]
).select(["input_ids", "label"])
return Dataset.from_polars(stories)
print("Loading tokenizer and model...")
tokenizer = AutoTokenizer.from_pretrained(
base_model,
truncation=True,
padding=True,
max_length=max_length,
)
model = AutoModelForSequenceClassification.from_pretrained(
base_model,
num_labels=1, # Regression task
device_map="auto",
attn_implementation="flash_attention_2",
torch_dtype=torch.bfloat16,
)
_apply_liger_kernel_to_instance(model=model)
model.config.pad_token_id = tokenizer.pad_token_id
tokenizer.padding_side = "right"
print("Configuring LoRA...")
model = get_peft_model(
model,
LoraConfig(
task_type="SEQ_CLS",
r=8,
lora_alpha=16,
lora_dropout=0,
),
)
print("Loading dataset...")
train_stories = create_dataset("train", 1000000, tokenizer)
validation_stories = create_dataset("val", 1000, tokenizer)
# Configure training arguments
training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=num_epochs,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
learning_rate=learning_rate,
weight_decay=0,
evaluation_strategy="steps",
eval_steps=0.05,
logging_steps=100,
save_strategy="steps",
save_steps=1000,
report_to="wandb",
no_cuda=False,
bf16=True,
warmup_steps=100,
gradient_accumulation_steps=gradient_accumulation_steps,
)
print("Initializing Trainer...")
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_stories,
eval_dataset=validation_stories,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
print("Starting model training...")
trainer.train()
print("Saving final model...")
trainer.save_model(output_dir)
tokenizer.save_pretrained(output_dir)
print("Stories model training complete")