File size: 2,329 Bytes
0d4ee70
7489b32
0d4ee70
 
c139ec7
9cb9d28
0d4ee70
9cb9d28
0d4ee70
f69cee3
7489b32
b94fa53
9cb9d28
 
 
b94fa53
9cb9d28
b94fa53
 
7489b32
c139ec7
 
536a6d6
b94fa53
 
 
7489b32
b94fa53
fd473a9
c139ec7
b94fa53
c139ec7
9cb9d28
 
 
 
b94fa53
 
 
 
9cb9d28
fd473a9
b94fa53
 
fd473a9
c139ec7
7489b32
 
fd473a9
 
 
c139ec7
 
 
fd473a9
 
 
 
 
 
 
 
 
 
 
 
 
 
9cb9d28
b94fa53
 
 
94c5b67
9cb9d28
7489b32
 
fd473a9
c139ec7
7489b32
 
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
import torch
import spaces
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"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)

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

# === 3️⃣ VERΔ° SETΔ° ===
dataset = load_dataset("oscar", "unshuffled_deduplicated_tr", split="train", streaming=True, trust_remote_code=True)
dataset = dataset.shuffle(seed=42).take(10000)

def tokenize_function(examples):
    return tokenizer(examples["text"], truncation=True, max_length=512)

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

# === 4️⃣ EĞİTΔ°M AYARLARI ===
# === 4️⃣ EĞİTΔ°M AYARLARI ===
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,
    save_steps=500,
    save_total_limit=2,
    logging_dir="./logs",
    logging_steps=10,
    optim="adamw_torch",
    no_cuda=True,  # πŸ”₯ ÇÖZÜM: Ana sΓΌreΓ§te GPU'yu başlatmayΔ± engelle
)


# === 5️⃣ GPU BAŞLATMA VE EĞİTΔ°M ===
@spaces.GPU
def train_model():
    device = "cuda" if torch.cuda.is_available() else "cpu"
    
    # Modeli burada yΓΌkle
    model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float32).to(device)
    model = get_peft_model(model, lora_config)

    # TrainingArguments burada tanΔ±mlandΔ±!
    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,
        save_steps=500,
        save_total_limit=2,
        logging_dir="./logs",
        logging_steps=10,
        optim="adamw_torch",
    )

    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_datasets,
    )
    trainer.train()
    return "βœ… Model Eğitimi TamamlandΔ±!"


# === 6️⃣ BAŞLATMA ===
if __name__ == "__main__":
    train_model()  # Eğitimi başlat