كيفية ضبط نموذج Llama-2 (7B) دقيقًا ليتفوق على ChatGPT

Built for Speed: ~10ms Latency, Even Under Load
Blazingly fast way to build, track and deploy your models!
- Handles 350+ RPS on just 1 vCPU — no tuning needed
- Production-ready with full enterprise support
تزداد النماذج اللغوية الكبيرة (LLMs) قوة يومًا بعد يوم، وتُستخدم في مجموعة متنوعة من المهام، بما في ذلك روبوتات الدردشة، وتوليد النصوص، والإجابة عن الأسئلة. ومع ذلك، قد تكون تدريبات LLMs مكلفة وتتطلب موارد ضخمة. في منشور المدونة هذا، سنوضح لك كيفية ضبط نموذج LLM أصغر (7B) دقيقًا ليعمل بشكل أفضل من ChatGPT.
الضبط الدقيق (Fine-tuning) هو عملية تدريب نموذج LLM على مجموعة بيانات محددة لتحسين أدائه في مهمة معينة. في هذه الحالة، سنقوم بضبط نموذج 7B LLM دقيقًا لضرب رقمين.
فلنبدأ بأداء النماذج اللغوية الكبيرة المختلفة في مهمة ضرب بسيطة: 458*987 = 452046.
في البداية، سننظر إلى نموذج Meta الذي تم إصداره مؤخرًا Llama-2-7B. قمنا بنشر Llama-2 على TrueFoundry وجربناه باستخدام تكاملات LangChain الخاصة بـ TrueFoundry. إليك النتيجة لهذه العملية.

كما يمكننا أن نرى بوضوح، هذا النموذج لا يؤدي المهمة بشكل جيد. (وهو أمر متوقع أيضًا نظرًا لحجم النموذج). دعونا نرى كيف تؤدي النماذج الأكثر تطورًا (state of the art) في نفس المهمة:
دعونا ننظر إلى النتيجة من ChatGPT (GPT3.5 Turbo):

الآن الإجابة (451,086) قريبة جدًا من الإجابة الفعلية وهي 452,046 ولكن الإجابة غير صحيحة.
دعونا نجرب حثًا (prompt) مختلفًا (للقيام بحساب خطوة بخطوة ورؤية ما يفعله):
0:00/1×
ChatGPT مع prompt مخصص للضرب
ولكن مرة أخرى، يصل إلى نتيجة غير صحيحة: 450,606 🤨
أخيرًا، دعونا نجرب النموذج الأكثر تطورًا (GPT-4) ونفحص أداءه في هذه المهمة:

إنه قريب جدًا من الإجابة الصحيحة (452,046) وقد يبدو صحيحًا لأي شخص. ولكن بوضوح، الإجابة غير صحيحة.
لماذا تؤدي LLMs بشكل ضعيف في مهام الضرب البسيطة؟
الإجابة على هذا السؤال بسيطة جدًا. إنها ليست "مدربة" على ذلك.
يتم تدريب LLMs على مجموعات بيانات ضخمة من النصوص والكود، ولكن هذه البيانات ليست دائمًا منسقة بطريقة مفيدة للاستدلال الرياضي. على سبيل المثال، قد لا تتضمن البيانات تمثيلات صريحة للمفاهيم الرياضية، مثل الجمع والطرح والضرب والقسمة. هذا يمكن أن يجعل من الصعب على LLMs تعلم كيفية تنفيذ هذه العمليات بشكل صحيح.
لذلك فكرنا: هل من الممكن تدريب نموذج LLM على إجراء العمليات الرياضية؟ قمنا بإجراء تجربة وكانت النتائج واعدة للغاية!
ضبط Llama-2 7B دقيقًا لتعلم الضرب
تجهيز مجموعة البيانات
للأخذ بزمام المبادرة، قمنا بتنسيق مجموعة بيانات مكونة من 40,000 صف من الـ prompts والتعليمات. حاولنا محاكاة الخطوات الدقيقة المتبعة في طريقة الضرب المطول، والتي تتضمن تقسيم المضروب والمضروب فيه إلى أرقامهما الفردية، ثم ضرب كل رقم من المضروب في كل رقم من المضروب فيه ووضع النتائج في المواضع المناسبة بناءً على قيمها المنزلية. وأخيرًا، يتم جمع جميع الحواصل الفردية للحصول على النتيجة النهائية.
إليك نموذج prompt:
You are a helpful assistant providing detailed answers to multiplication questions. Ensure you provide a thorough explanation using the long multiplication method for the math problem given below.
If you don't know the answer to a question, please don't share false information. What is 5244 * 1932?
وهنا نموذج استجابة:
Here are the steps for multiplying 5244 * 1932 using the long multiplication method.
Step 1: Break 5244 into its individual digits: 5 (thousands place), 2 (hundreds place), 4 (tens place), and 4 (ones place).
Step 2: Similarly, break 1932 into its individual digits: 1 (thousands place), 9 (hundreds place), 3 (tens place), and 2 (ones place).
- 4 (from 1 place of first number) * 2 (from 1 place of second number) = 8 -> Shift it 0 places to the left (because we are multiplying with the 1 place of the second number) to get 8
- 4 (from 1 place of first number) * 3 (from 10 place of second number) = 12 -> Shift it 1 places to the left (because we are multiplying with the 10 place of the second number) to get 120
- 4 (from 1 place of first number) * 9 (from 100 place of second number) = 36 -> Shift it 2 places to the left (because we are multiplying with the 100 place of the second number) to get 3600
- 4 (from 1 place of first number) * 1 (from 1000 place of second number) = 4 -> Shift it 3 places to the left (because we are multiplying with the 1000 place of the second number) to get 4000
- 4 (from 10 place of first number) * 2 (from 1 place of second number) = 8 -> Shift it 1 places to the left (because we are multiplying with the 1 place of the second number) to get 80
- 4 (from 10 place of first number) * 3 (from 10 place of second number) = 12 -> Shift it 2 places to the left (because we are multiplying with the 10 place of the second number) to get 1200
- 4 (from 10 place of first number) * 9 (from 100 place of second number) = 36 -> Shift it 3 places to the left (because we are multiplying with the 100 place of the second number) to get 36000
- 4 (from 10 place of first number) * 1 (from 1000 place of second number) = 4 -> Shift it 4 places to the left (because we are multiplying with the 1000 place of the second number) to get 40000
- 2 (from 100 place of first number) * 2 (from 1 place of second number) = 4 -> Shift it 2 places to the left (because we are multiplying with the 1 place of the second number) to get 400
- 2 (from 100 place of first number) * 3 (from 10 place of second number) = 6 -> Shift it 3 places to the left (because we are multiplying with the 10 place of the second number) to get 6000
- 2 (from 100 place of first number) * 9 (from 100 place of second number) = 18 -> Shift it 4 places to the left (because we are multiplying with the 100 place of the second number) to get 180000
- 2 (from 100 place of first number) * 1 (from 1000 place of second number) = 2 -> Shift it 5 places to the left (because we are multiplying with the 1000 place of the second number) to get 200000
- 5 (from 1000 place of first number) * 2 (from 1 place of second number) = 10 -> Shift it 3 places to the left (because we are multiplying with the 1 place of the second number) to get 10000
- 5 (from 1000 place of first number) * 3 (from 10 place of second number) = 15 -> Shift it 4 places to the left (because we are multiplying with the 10 place of the second number) to get 150000
- 5 (from 1000 place of first number) * 9 (from 100 place of second number) = 45 -> Shift it 5 places to the left (because we are multiplying with the 100 place of the second number) to get 4500000
- 5 (from 1000 place of first number) * 1 (from 1000 place of second number) = 5 -> Shift it 6 places to the left (because we are multiplying with the 1000 place of the second number) to get 5000000
Now, add all these results together:
The sum of 8 + 120 + 3600 + 4000 + 80 + 1200 + 36000 + 40000 + 400 + 6000 + 180000 + 200000 + 10000 + 150000 + 4500000 + 5000000 = 10131408.
ثم كتبنا سكريبت Python يولد أزواج prompt-response عن طريق اختيار رقمين عشوائيًا للضرب. قمنا بتنسيق مجموعة بيانات تحتوي على 40,000 صف.
ضبط النموذج دقيقًا
الآن، بمجرد تجهيز مجموعة البيانات، نحتاج إلى ضبط النموذج دقيقًا.
نحن نستخدم الإصدار المعدل للمحادثة من Meta (7 مليارات معلمة) من Llama-2 كنموذج أساسي.
قمنا بإجراء الضبط الدقيق باستخدام QLora finetuning باستخدام مكتبتي BitsAndBytes و Peft. إليك تكوين lora الذي استخدمناه:
LoraConfig(
lora_alpha=16,
lora_dropout=0.1,
r=64,
bias="none",
task_type="CAUSAL_LM",
target_modules=[
"q_proj",
"k_proj",
"v_proj",
"o_proj",
],
)
استغرق التدريب حوالي 8 ساعات على جهاز بمزود GPU من نوع A100 سعة 40GB لمجموعة بيانات مكونة من 40,000 صف.
أخيرًا، قمنا بنشر النموذج المضبوط دقيقًا على TrueFoundry مرة أخرى، وهناك النتائج:

وأخيرًا!! يمكننا أن نرى أن النموذج المضبوط دقيقًا قادر على حساب النتيجة بشكل صحيح.
الخاتمة
على الرغم من أن الحساب ليس مهمة نستخدم LLM من أجلها عادةً، إلا أن هذا المثال يوضح كيف يمكن لـ LLM "صغير" (7B معلمات) تم ضبطه دقيقًا بشكل صحيح لمهمة محددة أن يتفوق على LLMs "الكبيرة" (مثل GPT3.5 turbo - 175B معلمات و GPT-4) في مهمة متخصصة.
النماذج الأصغر المضبوطة دقيقًا رخيصة في الاستدلال (inference)، وأفضل في المهام المتخصصة، ويمكن نشرها بسهولة على السحابة الخاصة بك!
كتبنا مدونة تفصيلية حول ضبط Llama 2 دقيقًا
TrueFoundry AI Gateway delivers ~3–4 ms latency, handles 350+ RPS on 1 vCPU, scales horizontally with ease, and is production-ready, while LiteLLM suffers from high latency, struggles beyond moderate RPS, lacks built-in scaling, and is best for light or prototype workloads.


















.webp)





.webp)
.webp)
.png)

.png)
.png)
.png)
.png)
.png)
.webp)
.webp)






