İçeriğe geç

Mistral 7B v0.3 + Mistral Small 3 (24B): Sliding Window Deprecation + Tool-Calling

Mistral 7B v0.3 (Apache 2.0, 32K context), Mistral Small 3 (24B, Apache 2.0, 32K). v0.3'te sliding window deprecation, function-calling chat template, tool-token training. RTX 4090'da Mistral 7B QLoRA 1 epoch ~45 dakika. Mistral Small 3 (24B): NF4 = 12 GB, QLoRA marjinal sığar (~22 GB peak).

Şükrü Yusuf KAYA
32 dakikalık okuma
İleri
Mistral 7B v0.3 + Mistral Small 3 (24B): Sliding Window Deprecation + Tool-Calling

1. Mistral Ailesi Kısa Tarihçe#

VersiyonÇıkışÖnemli farklar
Mistral 7B v0.12023-09Sliding window 4K, GQA
Mistral 7B v0.22024-03Sliding window kaldırıldı, 32K
Mistral 7B v0.32024-05Function-calling, vocab 32K → 32768 (BoS/Tools eklendi)
Mistral Small 32024-1024B, Apache 2.0, low-latency
Mistral Large 22024-07123B (Mistral lisans, ticari ücretli)
Cookbook'un kuralı: Apache 2.0 olanlar (7B v0.3, Small 3, Codestral Mamba) — diğerleri ticari kısıtlı.

2. Function-Calling Chat Template#

Mistral 7B v0.3 chat template:
<s>[INST] {system_message}\n\n{user_message} [/INST] {assistant_message}</s> [INST] [AVAILABLE_TOOLS] [...] [/AVAILABLE_TOOLS] {user_message} [/INST] [TOOL_CALLS] [...] </s>
Special tokens:
  • [INST]
    /
    [/INST]
    — instruction wrapper
  • [AVAILABLE_TOOLS]
    /
    [/AVAILABLE_TOOLS]
    — tools description
  • [TOOL_CALLS]
    — model'in tool çağrıları
  • [TOOL_RESULTS]
    /
    [/TOOL_RESULTS]
    — tool output
Tool-calling FT için dataset format:
{ "messages": [ {"role": "user", "content": "Bugün Istanbul'da hava?"}, {"role": "assistant", "tool_calls": [{ "function": {"name": "get_weather", "arguments": "{\"city\": \"Istanbul\"}"} }]}, {"role": "tool", "content": "{\"temp\": 18, \"condition\": \"partly cloudy\"}"}, {"role": "assistant", "content": "Istanbul'da hava 18°C, parçalı bulutlu."} ] }
python
# === Mistral 7B v0.3 Function-Calling FT Lab ===
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model
from trl import SFTTrainer, SFTConfig
from datasets import load_dataset
from transformers import BitsAndBytesConfig
import torch
 
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=torch.bfloat16)
 
model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-Instruct-v0.3",
quantization_config=bnb,
attn_implementation="flash_attention_2",
torch_dtype=torch.bfloat16,
device_map="cuda",
)
tok = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
tok.pad_token = tok.eos_token
 
lora = LoraConfig(r=32, lora_alpha=64, lora_dropout=0.05,
target_modules=["q_proj","k_proj","v_proj","o_proj",
"gate_proj","up_proj","down_proj"],
task_type="CAUSAL_LM")
model = get_peft_model(model, lora)
 
# Function-calling dataset (e.g. Glaive-Function-Calling-v2 TR-translated)
dataset = load_dataset("user/tr-function-calling", split="train")
# Format: {"messages": [...]}
 
def format_fc(example):
return {"text": tok.apply_chat_template(
example["messages"],
tools=example.get("tools"),
tokenize=False,
)}
 
dataset = dataset.map(format_fc, num_proc=8)
 
cfg = SFTConfig(
output_dir="mistral-7b-v03-fc-tr",
num_train_epochs=2,
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
learning_rate=2e-4,
bf16=True, optim="paged_adamw_8bit",
max_seq_length=4096, packing=True,
dataset_text_field="text",
logging_steps=5, report_to="wandb",
)
trainer = SFTTrainer(model=model, tokenizer=tok, train_dataset=dataset, args=cfg)
trainer.train()
Mistral 7B v0.3 function-calling FT

3. Mistral Small 3 (24B) — RTX 4090 Marjinal#

24B NF4 = 12 GB sadece W. Activation + LoRA + B ≈ 8-10 GB. Total ~22 GB, sığar ama gergin.
TermValue
W (NF4)12.0 GB
A (seq=2048, batch=1, grad-ckpt)5.5 GB
O + G + B3.5 GB
Total21 GB
Cookbook'un kuralı: 24B QLoRA için batch=1, seq=2048 ile başla — sonra kademeli artır.
✅ Teslim
  1. Mistral 7B v0.3'ü function-calling dataset üzerinde FT et. 2) Tool-call accuracy'i ölç. 3) Sonraki ders: 3.6 — Gemma 3 1B / 4B / 12B / 27B.

Yorumlar & Soru-Cevap

(0)
Yorum yazmak için giriş yap.
Yorumlar yükleniyor...

İlgili İçerikler

Bağlantılı Pillar Konular

Bu yazının bağlandığı pillar konular