Instructions to use Hatim2221/qwen2.5-7b-arabic-math-cot with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use Hatim2221/qwen2.5-7b-arabic-math-cot with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-7B-Instruct") model = PeftModel.from_pretrained(base_model, "Hatim2221/qwen2.5-7b-arabic-math-cot") - Notebooks
- Google Colab
- Kaggle
Qwen2.5-7B Arabic Math Chain-of-Thought (LoRA)
QLoRA fine-tune of Qwen/Qwen2.5-7B-Instruct for step-by-step math reasoning
in both Arabic and English.
Model Details
- Base model: Qwen/Qwen2.5-7B-Instruct
- Method: QLoRA (4-bit NF4, LoRA r=64, alpha=16)
- Dataset: miscovery/Math_CoT_Arabic_English_Reasoning
- Languages: Arabic, English
- Task: Step-by-step (chain-of-thought) math reasoning
Base Model vs. Fine-Tuned Model: Side-by-Side Comparison
The same 8 questions used in the qualitative examples above were run through
the raw base model (Qwen2.5-7B-Instruct, no adapter) for direct comparison.
Both used greedy decoding.
---
مقارنة مباشرة: النموذج الأساسي مقابل النموذج المدرب
تم اختبار نفس 8 أسئلة على كلا النموذجين — النموذج الأساسي (Qwen2.5-7B-Instruct بدون أي تدريب إضافي) والنموذج المدرب — باستخدام greedy decoding لضمان نتائج قابلة للتكرار.
1. حساب الكسور
السؤال: أكل أحمد ثلث التفاحة وأكل خالد ربع التفاحة، كم باقي من التفاحة؟
| خطوات الحل | الإجابة النهائية | |
|---|---|---|
| النموذج الأساسي | التفاحة = 1 ← ثلث = 1/3 ← ربع = 1/4 ← المجموع = 1/3+1/4 = 7/12 ← الباقي = 1 - 7/12 | 5/12 |
| النموذج المدرب | 1/3 + 1/4 = 7/12 ← 1 - 7/12 | 5/12 |
التقييم: متطابقتان تمامًا. النموذج الأساسي أكثر إسهابًا (5 خطوات)، والنموذج المدرب أكثر إيجازًا (خطوتان).
2. مسألة كلامية متعددة الخطوات (مع رقم مُشتت)
السؤال: في مزرعة، يوجد 3 أضعاف عدد الدجاج مقارنة بالبط. عدد الأغنام في المزرعة هو 15، وهو أقل بـ 5 من عدد البط. إذا باع المزارع نصف الدجاج، فكم عدد الدجاج المتبقي؟
| خطوات الحل | الإجابة النهائية | |
|---|---|---|
| النموذج الأساسي | البط = 15+5 = 20 ← الدجاج = 3×20 = 60 ← بعد البيع = 60÷2 | 30 |
| النموذج المدرب | البط = 15+5 = 20 ← الدجاج = 3×20 = 60 ← بعد البيع = 60/2 | 30 |
التقييم: متطابقتان، وتجاهل كلا النموذجين عدد الأغنام (الرقم غير ذي الصلة) بشكل صحيح.
3. معادلة جبرية متسلسلة
السؤال: إذا كان 2س + 5 = 3ص، وكان ص = 7، فما قيمة س؟ ثم احسب قيمة س² - 4.
| خطوات الحل | الإجابة النهائية | |
|---|---|---|
| النموذج الأساسي | 2س+5 = 21 ← 2س = 16 ← س = 8 ← س²-4 = 60 | س=8، والناتج=60 |
| النموذج المدرب | 2س+5 = 21 ← 2س = 16 ← س = 8 ← س²-4 = 60 | 60 |
التقييم: متطابقتان تمامًا في المنطق والنتيجة.
4. استدلال منطقي (فرضية خاطئة)
السؤال: جميع الطيور تستطيع الطيران. البطريق طائر. فهل يستطيع البطريق الطيران؟ اشرح التناقض إن وجد.
| الإجابة | ملاحظة | |
|---|---|---|
| النموذج الأساسي | لا، البطريق لا يطير — التناقض ناتج عن قاعدة غير دقيقة | أضاف مثالًا خاطئًا: ذكر أن الببغاء لا يطير (وهو خطأ واقعي، الببغاء يطير فعلًا) |
| النموذج المدرب | لا، البطريق لا يطير — التناقض ناتج عن قاعدة خاطئة أدت لاستنتاج خاطئ | لم يُضِف أي معلومة خاطئة |
التقييم: لصالح النموذج المدرب. كلاهما حدّد التناقض بشكل صحيح، لكن النموذج الأساسي أقحم معلومة خاطئة غير مطلوبة.
5. مفارقة الكاذب (Liar's Paradox)
السؤال: هذه الجملة كاذبة. هل الجملة صحيحة أم خاطئة؟
| الإجابة | ملاحظة | |
|---|---|---|
| النموذج الأساسي | لا يمكن تحديد الحقيقة أو الكذب بشكل قاطع بسبب التناقض الداخلي | وصف واضح وصحيح لطبيعة المفارقة |
| النموذج المدرب | جملة تكرارية (tautology)، وتعتمد على الموقف | إجابة مراوغة، لم يصل لنفس درجة الوضوح |
التقييم: لصالح النموذج الأساسي. هذا المثال الوحيد الذي تفوّق فيه النموذج الأساسي بوضوح — وصل لتوصيف أدق وأكثر حسمًا للمفارقة، بينما تهرّب النموذج المدرب من إجابة حاسمة.
6. معرفة عامة
السؤال: كم عمر كوكب الأرض؟
| الإجابة النهائية | ملاحظة | |
|---|---|---|
| النموذج الأساسي | حوالي 4.54 مليار سنة | أضاف أدلة علمية داعمة (صخور قمرية، تكتونية) |
| النموذج المدرب | حوالي 4.5 مليار سنة | إجابة مباشرة ومختصرة |
التقييم: كلاهما صحيح، فرق طفيف فقط في مستوى التفصيل.
7. استدلال سببي متعدد الخطوات
السؤال: لو لم تُخترع الكهرباء، كيف كانت لتختلف طريقة تواصل الناس اليوم؟ اذكر ثلاث نتائج منطقية مترابطة.
| النتائج الثلاث | ملاحظة | |
|---|---|---|
| النموذج الأساسي | (1) اتصال صوتي تقليدي (2) رسائل كتابية وبريد ورقي (3) وسائل مرئية/سمعية غير كهربائية | نقطتا 1 و3 متداخلتان مفاهيميًا |
| النموذج المدرب | (1) اتصال صوتي فقط (2) تأخر نشر المعلومات (3) تقليل التفاعل المباشر | ثلاث نقاط متمايزة بوضوح |
التقييم: فرق طفيف لصالح النموذج المدرب. نتائجه أكثر تمايزًا وأقل تكرارًا.
8. تفسير فيزيائي
السؤال: إذا وضعت مكعب ثلج في كوب ماء دافئ، ماذا سيحدث خلال الدقائق القليلة القادمة؟ ولماذا يستمر الماء بالتبريد حتى بعد ذوبان الجليد بالكامل؟
| الآلية المذكورة | الخطأ الفيزيائي | |
|---|---|---|
| النموذج الأساسي | الذوبان يمتص الحرارة من الماء فيبرده | ادّعى أن الماء الناتج يكون بنفس درجة حرارة الثلج (0°م) |
| النموذج المدرب | نفس الآلية: الذوبان يمتص الحرارة فيبرد الماء | ادّعى أن الماء يبقى "أعلى من درجة الغليان" |
التقييم: كلا النموذجين ذكر الآلية الأساسية الصحيحة، لكن كلاهما ارتكب خطأً فيزيائيًا مختلفًا. التدريب لم يُصلح هذا الضعف، بل غيّر شكل الخطأ فقط.
جدول الملخص
| # | الفئة | النتيجة |
|---|---|---|
| 1 | كسور | متطابقتان |
| 2 | مسألة كلامية + رقم مُشتت | متطابقتان |
| 3 | جبر | متطابقتان |
| 4 | منطق (فرضية خاطئة) | لصالح النموذج المدرب |
| 5 | مفارقة الكاذب | لصالح النموذج الأساسي |
| 6 | معرفة عامة | متطابقتان |
| 7 | استدلال سببي | لصالح النموذج المدرب (طفيف) |
| 8 | تفسير فيزيائي | ضعف مشترك، بأخطاء مختلفة |
الخلاصة الصادقة: التدريب حسّن التنسيق والإيجاز بوضوح، وقلّل إضافة معلومات خاطئة غير مطلوبة في بعض الحالات. لكنه لم يُحسّن الاستدلال المنطقي أو الفيزيائي في كل الحالات — بل كان أضعف في حالة واحدة (مفارقة الكاذب). الفائدة الأساسية لهذا التدريب تظهر في التنسيق والدقة الحسابية أكثر من كونها تحسينًا شاملاً في كل أنواع الاستدلال.
How to Use
1. Install dependencies
pip install -U transformers peft accelerate bitsandbytes torch
2. Load the base model and adapter
This model is a LoRA adapter, not a standalone model — you need to load the base model first, then attach the adapter on top of it.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
BASE_MODEL = "Qwen/Qwen2.5-7B-Instruct"
ADAPTER_REPO = "Hatim2221/qwen2.5-7b-arabic-math-cot"
# Load the base model
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
torch_dtype=torch.bfloat16,
device_map="auto",
)
# Attach the fine-tuned LoRA adapter
model = PeftModel.from_pretrained(base_model, ADAPTER_REPO)
# Load the tokenizer (includes the chat template used during training)
tokenizer = AutoTokenizer.from_pretrained(ADAPTER_REPO)
3. Run inference
system_prompt = (
"أنت مساعد رياضيات بارع. فكّر خطوة بخطوة بوضوح قبل إعطاء الإجابة النهائية."
)
user_question = (
"إذا كان لدى أحمد 3 أضعاف عدد التفاحات التي لدى سارة، ولدى سارة 12 تفاحة، "
"فكم تفاحة لدى أحمد وسارة معًا؟"
)
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_question},
]
prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=512, do_sample=False)
# Slice off the input prompt so only the newly generated text is printed
response = tokenizer.decode(
output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True
)
print(response)
Training Details
- Epochs: 3
- Learning rate: 2e-4
- Effective batch size: 16 (batch size 2 × 8 gradient accumulation steps)
- Sequence length: 2048
Limitations
This is a LoRA adapter, not a merged model — you need the base model
(Qwen/Qwen2.5-7B-Instruct) loaded alongside it. Reasoning quality is
bounded by the base model's native Arabic capability plus dataset coverage;
not extensively evaluated against held-out benchmarks.
Evaluation Results
Evaluated on a held-out split (5%) of miscovery/Math_CoT_Arabic_English_Reasoning, split on the original problem rows before separating into Arabic/English copies (so no problem appears in both languages across the train/test boundary), using exact-match on the final numeric answer, greedy decoding, n=50.
| Model | Overall Acc. | Arabic Acc. | English Acc. |
|---|---|---|---|
| Qwen2.5-7B-Instruct (base) | 42.00% | 47.83% | 37.04% |
| Qwen2.5-7B Arabic Math CoT (this model) | 68.00% | 65.22% | 70.37% |
Fine-tuning improved overall accuracy by +26 points over the base model on this held-out set.
External Benchmarks
In addition to the held-out evaluation on our own dataset (see above), we ran
standardized external benchmarks using EleutherAI's lm-evaluation-harness
to check whether fine-tuning affected general capabilities, independent of our
training data.
| Benchmark | Base (Qwen2.5-7B-Instruct) | Fine-tuned (this model) | Change |
|---|---|---|---|
| GSM8K (English, exact match) | 0.7800 | 0.8400 | +6.0 pts |
| Arabic MMLU (accuracy) | 0.6346 | 0.6288 | -0.6 pts |
n = 100 samples per benchmark (diagnostic run via --limit 100, not the full test sets).
What these results actually show
- GSM8K (English) improved meaningfully, indicating the CoT fine-tuning strengthened general step-by-step math reasoning without degrading the base model's core math capability. Note this benchmark is in English, so it does not directly measure Arabic performance — it mainly confirms fine-tuning didn't come at the cost of general reasoning ability.
- Arabic MMLU came out essentially flat, with a small decrease (-0.6 points) that is likely within noise at this sample size, but is reported as-is rather than rounded up to "no change." Arabic MMLU is a knowledge/multiple-choice benchmark, not a chain-of-thought reasoning benchmark, so it is a weak proxy for the model's actual target skill (Arabic step-by-step reasoning) — it mainly confirms fine-tuning did not damage the base model's general Arabic knowledge.
- Neither benchmark directly measures Arabic chain-of-thought math
reasoning, which is the model's actual fine-tuning target. The held-out
evaluation on
miscovery/Math_CoT_Arabic_English_Reasoningabove remains the most direct evidence for that specific capability. - These are diagnostic-scale runs (n=100). Full-benchmark runs (1,319 questions for GSM8K, thousands for Arabic MMLU) would give more reliable numbers, especially for the Arabic MMLU comparison, where the current gap is small enough that sample noise cannot be ruled out.
Notes / limitations:
- n=50 is a small sample; a larger eval set would give tighter confidence in the exact numbers, especially the per-language breakdown.
- The scoring method extracts the last numeric value from the response and compares it to the reference answer. This works well for numeric word problems but under-scores correct responses to non-numeric questions (e.g. conceptual, logical, or open-ended reasoning items in the dataset), so these numbers likely underestimate true accuracy on those categories.
- Current results show English accuracy higher than Arabic accuracy; closing this gap is an active area of further tuning.
- Downloads last month
- 133
