-
Notifications
You must be signed in to change notification settings - Fork 0
/
starcoder2.py
80 lines (63 loc) · 2.65 KB
/
starcoder2.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
# Gerekli kütüphaneleri yükle
!pip install transformers
!pip install torch
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import AutoTokenizer, AutoModelForCausalLM, AdamW, get_scheduler
from tqdm.auto import tqdm
# Veri setini yükle ve ön işleme
df = pd.read_csv('path/to/your/dataset.csv') # Veri seti yolu
# `code1` ve `code2`'yi birleştir ve ayrıcı tokenlar ekleyerek tek bir metin olarak hazırla
df['input_output'] = df['code1'] + " <|separator|> " + df['code2']
print(df.head())
# Dataset ve Dataloader'ın Hazırlanması
class CodeDataset(Dataset):
def __init__(self, tokenizer, dataframe, max_length):
self.tokenizer = tokenizer
self.data = dataframe
self.max_length = max_length
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
encoding = self.tokenizer(self.data.iloc[idx]['input_output'],
return_tensors='pt',
max_length=self.max_length,
padding='max_length',
truncation=True)
labels = encoding.input_ids.clone()
labels[labels == self.tokenizer.pad_token_id] = -100
return {
'input_ids': encoding.input_ids.squeeze(),
'attention_mask': encoding.attention_mask.squeeze(),
'labels': labels.squeeze()
}
tokenizer = AutoTokenizer.from_pretrained('starcoder2-checkpoint')
dataset = CodeDataset(tokenizer, df, max_length=512)
loader = DataLoader(dataset, batch_size=4, shuffle=True)
# Modelin Hazırlanması ve Eğitimi
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = AutoModelForCausalLM.from_pretrained('starcoder2-checkpoint').to(device)
model.train()
optimizer = AdamW(model.parameters(), lr=5e-5)
num_epochs = 3
num_training_steps = num_epochs * len(loader)
lr_scheduler = get_scheduler("linear",
optimizer=optimizer,
num_warmup_steps=0,
num_training_steps=num_training_steps)
progress_bar = tqdm(range(num_training_steps))
for epoch in range(num_epochs):
for batch in loader:
batch = {k: v.to(device) for k, v in batch.items()}
outputs = model(**batch)
loss = outputs.loss
loss.backward()
optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
progress_bar.update(1)
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
# Modeli kaydet
model.save_pretrained('./fine_tuned_starcoder2')
tokenizer.save_pretrained('./fine_tuned_starcoder2')