File size: 2,050 Bytes
0d4ee70
 
 
 
f69cee3
0d4ee70
 
 
f69cee3
0d4ee70
 
 
 
8c9c2d1
0d4ee70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
from peft import LoraConfig, get_peft_model
from datasets import load_dataset

# === 1️⃣ MODEL VE TOKENIZER YÜKLEME ===
MODEL_NAME = "mistralai/Mistral-7B-v0.1"  # Hugging Face model adı
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)

# === 2️⃣ CPU OPTİMİZASYONU ===
device = "cpu"  # CPU kullanıyoruz
torch_dtype = torch.float32  # float32 seçtik çünkü CPU'da bf16 genelde yok
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch_dtype)

# === 3️⃣ LoRA AYARLARI ===
lora_config = LoraConfig(
    r=8,              # Düşük rank LoRA kullanımı
    lora_alpha=32,    # Alpha değeri
    lora_dropout=0.1, # Dropout oranı
    bias="none",
    target_modules=["q_proj", "v_proj"],  # Sadece attention katmanlarına LoRA uygula
)
model = get_peft_model(model, lora_config)

# === 4️⃣ VERİ SETİ ===
dataset = load_dataset("oscar", "unshuffled_deduplicated_tr")  # OSCAR Türkçe veri seti
train_data = dataset["train"].shuffle(seed=42).select(range(10000))  # Küçük subset

# === 5️⃣ TOKENLEŞTİRME FONKSİYONU ===
def tokenize_function(examples):
    return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)

tokenized_datasets = train_data.map(tokenize_function, batched=True)

# === 6️⃣ EĞİTİM AYARLARI ===
training_args = TrainingArguments(
    output_dir="./mistral_lora_cpu",
    per_device_train_batch_size=1,  # Küçük batch size
    gradient_accumulation_steps=16, # Daha büyük effective batch için
    learning_rate=5e-4,  # AdamW yerine SGD kullanacağımız için yüksek LR
    num_train_epochs=1,  # İlk deneme için sadece 1 epoch
    save_steps=500,
    save_total_limit=2,
    logging_dir="./logs",
    logging_steps=10,
    optim="sgd",  # AdamW yerine SGD kullan
)

# === 7️⃣ MODEL EĞİTİMİ ===
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_datasets,
)

trainer.train()