fine tuning llm

entry3 galeri
    2.
  1. çok basit bi fine tuning işlemi:

    import torch
    from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments
    from peft import LoraConfig, get_peft_model
    from trl import SFTTrainer
    from datasets import Dataset

    # 1. Eğitim verisi: Komplo teorileri üzerine, uzun diyaloglu, mizahi sitcom sahneleri (ingilizce)
    veri = {
    "girdi": [
    "<|system|> You are a funny AI.\n<|user|> Write a comedic sitcom scene in English about conspiracy theories with long dialogues.\n<|assistant|>",
    "<|system|> You are a funny AI.\n<|user|> Write a comedic sitcom scene in English about conspiracy theories with long dialogues.\n<|assistant|>",
    "<|system|> You are a funny AI.\n<|user|> Write a comedic sitcom scene in English about conspiracy theories with long dialogues.\n<|assistant|>"
    ],
    "cevap": [
    """INT. LIVING ROOM - DAY

    SAM and ALEX sit on a couch, surrounded by conspiracy theory books and snack wrappers.

    SAM
    (grinning)
    So, I’m telling you, the moon landing was staged. The shadows don’t add up!

    ALEX
    (rolling eyes)
    Yeah, and Elvis is running a secret taco truck somewhere in Mexico.

    SAM
    Exactly! And that’s the real reason the government wants to keep tortillas classified!

    They both laugh hysterically as a dog barks in the background.""",

    """EXT. COFFEE SHOP - AFTERNOON

    JESSICA sips her latte, animated.

    JESSICA
    You know the pigeons? Not real birds. They're drones. Government spies disguised as fluffy informants.

    MARK
    (nods seriously)
    I always suspected something was off. Ever notice how they bob their heads like they're buffering?

    JESSICA
    Exactly! And that’s why I only trust parrots—they don’t have Wi-Fi.

    MARK
    (smirks)
    Unless the parrots are in on it too. Mind blown.""",

    """INT. OFFICE KITCHEN - MORNING

    LISA and DAVID argue over a donut box.

    LISA
    I swear, the reason they put holes in donuts is to track our sugar intake.

    DAVID
    (skeptical)
    Or maybe it’s a secret portal to the donut dimension? Think about it.

    LISA
    (whispers)
    We might be pawns in a glazed game, David.

    DAVID
    (sips coffee)
    I’m officially never eating donuts again."""
    ]
    }

    # Dataset nesnesine çeviriyoruz
    veri_seti = Dataset.from_dict(veri)

    # 2. Model ve Tokenizer yükleniyor
    model_adi = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
    tokenizer = AutoTokenizer.from_pretrained(model_adi)
    tokenizer.pad_token = tokenizer.eos_token # PAD token'ı olarak EOS (bitiş) token'ı kullanılıyor

    # Modeli yükle (CUDA ile GPU'ya, FP16 ile daha hızlı ve düşük VRAM tüketimiyle)
    model = AutoModelForCausalLM.from_pretrained(
    model_adi,
    device_map={"": "cuda"},
    torch_dtype=torch.float16
    )

    # 3. Tokenizasyon fonksiyonu: Girdi ve cevapları birleştirip token'lara çeviriyoruz
    def tokenize_fonksiyonu(ornekler):
    girisler = ornekler["girdi"]
    cevaplar = ornekler["cevap"]
    tam_metinler = [g + c for g, c in zip(girisler, cevaplar) # Girdi ve cevapları yan yana birleştiriyoruz
    tokenli = tokenizer(tam_metinler, truncation=True, padding="max_length", max_length=512)
    tokenli["labels"] = tokenli["input_ids"].copy() # Eğitimde kullanılacak hedefler (labels) input_ids'in kopyası olacak
    return tokenli

    # Dataset token'lanıyor
    tokenli_veri_seti = veri_seti.map(tokenize_fonksiyonu, batched=True)

    # 4. LoRA (Low Rank Adaptation) konfigürasyonu
    lora_ayarlari = LoraConfig(
    r=8, # Rank değeri (LoRA katmanlarının boyutu)
    lora_alpha=32, # LoRA ağırlıklarının etkisini belirleyen katsayı
    target_modules=["q_proj", "v_proj", "k_proj", "o_proj"], # LoRA uygulanacak katmanlar
    lora_dropout=0.05, # Eğitim sırasında %5 dropout
    bias="none", # Bias'a dokunulmayacak
    task_type="CAUSAL_LM" # Görev tipi: Causal Language Modeling (Oto-regresif dil modeli)
    )
    # LoRA uyguluyoruz
    model = get_peft_model(model, lora_ayarlari)

    # 5. Eğitim parametreleri
    egitim_ayarlari = TrainingArguments(
    output_dir="./tinyllama-lora-komik-tv-senaryosu", # Eğitilen modelin kaydedileceği klasör
    per_device_train_batch_size=1, # GPU başına batch boyutu
    num_train_epochs=5, # Eğitim döngüsü (epok) sayısı
    learning_rate=2e-5, # Öğrenme oranı
    logging_steps=1, # Kaç adımda bir log kaydı yapacağı
    save_steps=100, # Kaç adımda bir modeli kaydedeceği
    fp16=True # 16-bit floating point ile eğitim (daha az VRAM kullanımı)
    )

    # 6. Trainer nesnesi ile modeli eğitiyoruz
    egitici = SFTTrainer(
    model=model,
    args=egitim_ayarlari,
    train_dataset=tokenli_veri_seti
    )
    egitici.train()

    # 7. Eğitilen modeli kaydet
    model.save_pretrained("./tinyllama-lora-komik-tv-senaryosu")

    # 8. Üretim (Inference): Komik sitcom sahnesi üretmek için parametrelerle text generation
    model.eval() # Modeli değerlendirme moduna alıyoruz
    girdi_metni = "<|system|> You are a funny AI.\n<|user|> Write a comedic sitcom scene in English about conspiracy theories with long dialogues.\n<|assistant|>"

    # Girdi metnini token'lara çevirip CUDA'ya gönderiyoruz
    girdiler = tokenizer(girdi_metni, return_tensors="pt").to("cuda")

    # Metin üretimi (text generation)
    cikti = model.generate(
    **girdiler,
    max_new_tokens=800, # Üretilecek maksimum yeni token sayısı
    do_sample=True, # Örnekleme yaparak (sampling) üretim
    top_p=0.85, # Nucleus Sampling (top-p) ile çeşitliliği kısıtlıyoruz
    temperature=0.65, # Sıcaklık değeri (düşük sıcaklık -> daha tutarlı, az kaotik cevaplar)
    pad_token_id=tokenizer.eos_token_id, # Pad token için EOS token kullanılıyor
    eos_token_id=tokenizer.eos_token_id, # Çıkış token olarak EOS kullanılıyor
    repetition_penalty=1.2 # Aynı ifadeleri tekrar etmemesi için ceza uygulanıyor
    )

    # Üretilen sonucu ekrana yazdırıyoruz
    print("\n=== Üretilen Komik Sitcom Sahnesi ===\n")
    print(tokenizer.decode(cikti[0], skip_special_tokens=True))
    0 ...