-
Notifications
You must be signed in to change notification settings - Fork 0
/
starcoder2_v2.py
223 lines (176 loc) · 6.67 KB
/
starcoder2_v2.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import os
import torch
import pandas as pd
from transformers import (
AutoModelForCausalLM,
Trainer,
TrainingArguments,
DataCollatorForLanguageModeling,
PreTrainedTokenizerFast,
)
from datasets import Dataset
from accelerate import Accelerator
# Assuming 'df' is your DataFrame with 'input' and 'output' columns
# Example: df = pd.DataFrame({'input': ['print("Hello, World!")'], 'output': ['console.log("Hello, World!");']})
def prepare_dataset(df):
# Convert the DataFrame into Hugging Face's Dataset format
dataset = Dataset.from_pandas(df)
# You might need to customize this function to properly format your dataset
def tokenize_function(examples):
# Concatenate input and output texts, separated by a special token (e.g., <|endoftext|>)
# Adjust this based on how your tokenizer expects the data
concatenated_examples = [inp + tokenizer.sep_token + out for inp, out in zip(examples['input'], examples['output'])]
return tokenizer(concatenated_examples, max_length=args.max_seq_length, truncation=True, padding="max_length")
tokenized_dataset = dataset.map(tokenize_function, batched=True)
return tokenized_dataset
def main():
# Replace 'your_tokenizer_model' with the appropriate tokenizer for your model
global tokenizer
tokenizer = PreTrainedTokenizerFast.from_pretrained("bigcode/starcoder2-3b")
# Convert DataFrame to tokenized dataset
tokenized_data = prepare_dataset(df)
model = AutoModelForCausalLM.from_pretrained("bigcode/starcoder2-3b")
# Use Accelerator for distributed training support
accelerator = Accelerator()
training_args = TrainingArguments(
output_dir="finetune_starcoder2",
overwrite_output_dir=True,
num_train_epochs=3,
per_device_train_batch_size=4,
save_steps=10_000,
save_total_limit=2,
prediction_loss_only=True,
)
# Prepare data collator
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm=False,
)
# Initialize Trainer
trainer = Trainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=tokenized_data,
)
# Train
trainer.train()
# Save the model
trainer.save_model("finetune_starcoder2_final")
print("Training Done! 💥")
if __name__ == "__main__":
main()
###########################################################################################################################################
from transformers import (
AutoModelForCausalLM,
Trainer,
TrainingArguments,
PreTrainedTokenizerFast,
BitsAndBytesConfig
)
# Other imports remain the same
def main():
global tokenizer
tokenizer = PreTrainedTokenizerFast.from_pretrained("bigcode/starcoder2-3b")
# Convert DataFrame to tokenized dataset
tokenized_data = prepare_dataset(df)
# BitsAndBytesConfig for model quantization
bnb_config = BitsAndBytesConfig(
load_in_4bit=True, # Enable loading the model in 4-bit quantization
bnb_4bit_quant_type="nf4", # Quantization type
bnb_4bit_compute_dtype=torch.bfloat16, # Compute data type
)
model = AutoModelForCausalLM.from_pretrained(
"bigcode/starcoder2-3b",
quantization_config=bnb_config, # Apply quantization configuration
)
# Accelerator, TrainingArguments, and Trainer initialization remain the same
# Initialize Trainer with model, args, data collator, and dataset
trainer = Trainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=tokenized_data,
)
# Training and model saving remain the same
if __name__ == "__main__":
main()
##############################################################################################################################
import argparse
import os
import pandas as pd
from transformers import (
AutoModelForCausalLM,
Trainer,
TrainingArguments,
TextDatasetForNextTokenPrediction,
DataCollatorForLanguageModeling,
PreTrainedTokenizerFast,
BitsAndBytesConfig,
LoraConfig,
)
from datasets import Dataset
import torch
# Örnek DataFrame (df) varsayımı
# df = pd.DataFrame({'input': ['kod parçası 1', 'kod parçası 2'], 'output': ['çevrilen kod 1', 'çevrilen kod 2']})
def prepare_dataset(df, tokenizer, max_seq_length):
# DataFrame'i Dataset formatına çevir
def tokenize_function(examples):
# Model için uygun formatta veriyi hazırla
concatenated_examples = [inp + tokenizer.sep_token + out for inp, out in zip(examples['input'], examples['output'])]
return tokenizer(concatenated_examples, max_length=max_seq_length, truncation=True, padding="max_length")
dataset = Dataset.from_pandas(df)
tokenized_dataset = dataset.map(tokenize_function, batched=True)
return tokenized_dataset
def main():
# Args değerlerini sabit olarak tanımla
args = {
"model_id": "bigcode/starcoder2-3b",
"max_seq_length": 1024,
"micro_batch_size": 1,
"gradient_accumulation_steps": 4,
"weight_decay": 0.01,
"bf16": True,
"attention_dropout": 0.1,
"learning_rate": 2e-4,
"lr_scheduler_type": "cosine",
"warmup_steps": 100,
"seed": 0,
"output_dir": "finetune_starcoder2",
"push_to_hub": False,
}
tokenizer = PreTrainedTokenizerFast.from_pretrained(args["model_id"])
model = AutoModelForCausalLM.from_pretrained(
args["model_id"],
attention_dropout=args["attention_dropout"],
torch_dtype=torch.bfloat16 if args["bf16"] else None,
)
# DataFrame'den dataset hazırla
df = pd.read_csv("your_dataset.csv") # DataFrame'inizi yükleyin
tokenized_dataset = prepare_dataset(df, tokenizer, args["max_seq_length"])
# TrainingArguments ve Trainer tanımla
training_args = TrainingArguments(
output_dir=args["output_dir"],
per_device_train_batch_size=args["micro_batch_size"],
gradient_accumulation_steps=args["gradient_accumulation_steps"],
learning_rate=args["learning_rate"],
num_train_epochs=3,
weight_decay=args["weight_decay"],
logging_dir="./logs",
)
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm=False
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset,
data_collator=data_collator,
)
# Modeli eğit
trainer.train()
# Modeli kaydet
trainer.save_model(args["output_dir"])
if args["push_to_hub"]:
trainer.push_to_hub()
if __name__ == "__main__":
main()