المستوى: متوسط · الدرس 8 من 14
أهداف الدرس
- أن تضبط نموذجاً مفتوحاً بموارد قليلة.
- أن تختار بين LoRA والتكميم والتقطير.
بدل تعديل مليارات الأوزان، تضيف LoRA مصفوفتين صغيرتين بجانب الأوزان المجمّدة، وتدرّبهما وحدهما. فتحتاج ذاكرة أقل بكثير، ويمكن أن تحفظ لكل مهمة «محوّلاً» صغيراً.
from transformers import AutoModelForCausalLM
from peft import LoraConfig, get_peft_model
model = AutoModelForCausalLM.from_pretrained("path/to/an-open-model")
config = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05,
target_modules=["q_proj", "v_proj"], task_type="CAUSAL_LM")
model = get_peft_model(model, config)
model.print_trainable_parameters() # a fraction of 1% of the weights| التقنية | ماذا تفعل | متى |
|---|---|---|
| LoRA | تدرّب جزءاً صغيراً | أسلوب أو شكل أو مهمة محدّدة |
| QLoRA | LoRA فوق نموذج مضغوط | ذاكرة رسومية محدودة |
| التكميم | يضغط الأوزان إلى 8 أو 4 بت | تشغيل أسرع وأرخص |
| التقطير | نموذج كبير يعلّم نموذجاً صغيراً | نموذج صغير على جهاز أو هاتف |
الضبط الدقيق يعلّم الأسلوب والشكل أفضل مما يعلّم المعرفة الجديدة. لمعرفة تتغيّر — لوائح ومواعيد — استعمل الاسترجاع (RAG) في الدرس العاشر.
تمرين
تريد مساعداً يكتب بأسلوب الكلية الرسمي. ضبط دقيق أم RAG؟
الإجابة
ضبط دقيق بـ LoRA: الأسلوب شكلٌ ثابت يتعلّمه النموذج جيداً.
Level: Intermediate · Lesson 8 of 14
Lesson goals
- Fine-tune an open model on modest hardware.
- Choose between LoRA, quantization and distillation.
Instead of changing billions of weights, LoRA adds two small matrices beside the frozen weights and trains only those. You need far less memory, and can keep a small «adapter» per task.
from transformers import AutoModelForCausalLM
from peft import LoraConfig, get_peft_model
model = AutoModelForCausalLM.from_pretrained("path/to/an-open-model")
config = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05,
target_modules=["q_proj", "v_proj"], task_type="CAUSAL_LM")
model = get_peft_model(model, config)
model.print_trainable_parameters() # a fraction of 1% of the weights| Technique | What it does | When |
|---|---|---|
| LoRA | Trains a small part | A style, format or narrow task |
| QLoRA | LoRA on top of a compressed model | Limited GPU memory |
| Quantization | Compresses weights to 8 or 4 bits | Faster, cheaper running |
| Distillation | A big model teaches a small one | A small model on a device or phone |
Fine-tuning teaches style and format better than new knowledge. For knowledge that changes — rules and dates — use retrieval (RAG) in lesson 10.
Exercise
You want an assistant that writes in the college's formal style. Fine-tuning or RAG?
Answer
LoRA fine-tuning: style is a stable shape the model learns well.
التعليقات / Comments
لا تعليقات بعد. كن أول من يسأل. / No comments yet. Be the first to ask.