File size: 2,285 Bytes
0d4ee70
 
 
c139ec7
66c7691
9cb9d28
0d4ee70
66c7691
0d4ee70
f69cee3
66c7691
 
 
 
 
b94fa53
66c7691
 
 
b94fa53
66c7691
b94fa53
66c7691
b94fa53
66c7691
 
 
536a6d6
66c7691
4baf273
b94fa53
 
 
66c7691
b94fa53
66c7691
 
 
 
 
 
50be0cf
b94fa53
c139ec7
66c7691
 
 
 
 
b94fa53
 
 
 
9cb9d28
a27ab3b
 
66c7691
b94fa53
 
66c7691
4baf273
7489b32
9cb9d28
b94fa53
 
 
94c5b67
9cb9d28
fd473a9
66c7691
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
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
from peft import LoraConfig, get_peft_model
from datasets import load_dataset
import gradio as gr

# === 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 ===
torch_dtype = torch.float32  # CPU için uygun dtype
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch_dtype)

# === 3️⃣ LoRA AYARLARI ===
lora_config = LoraConfig(
    r=8,              
    lora_alpha=32,    
    lora_dropout=0.1, 
    bias="none",
    target_modules=["q_proj", "v_proj"], 
)
model = get_peft_model(model, lora_config)

# === 4️⃣ VERİ SETİ ===
dataset = load_dataset("oscar", "unshuffled_deduplicated_tr", trust_remote_code=True)  # trust_remote_code=True
subset = dataset["train"].shuffle(seed=42).select(range(10000))  # Küçük subset seçiyoruz (10.000 örnek)

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

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

# === 6️⃣ EĞİTİM AYARLARI ===
# Eğitimde kaç adım olduğunu hesaplayalım
train_size = len(tokenized_datasets)  # 10,000 örnek
batch_size = 1  # Batch size 1
num_epochs = 1  # 1 epoch eğitimi
max_steps = (train_size // batch_size) * num_epochs  # max_steps hesapla

training_args = TrainingArguments(
    output_dir="./mistral_lora",
    per_device_train_batch_size=1,  
    gradient_accumulation_steps=16, 
    learning_rate=5e-4,  
    num_train_epochs=1,  
    max_steps=max_steps,  # Buraya max_steps parametresini ekliyoruz
    save_steps=500,
    save_total_limit=2,
    logging_dir="./logs",
    logging_steps=10,
    optim="adamw_torch",
    train_dataset=split_dataset["train"],
    eval_dataset=split_dataset["test"],
    no_cuda=True,  # GPU kullanılmıyor
)

# === 7️⃣ MODEL EĞİTİMİ ===
@spaces.GPU
def train_model():
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_datasets,
    )
    trainer.train()

train_model()  # Eğitimi başlat