Llama-ArabicSci-70M
Llama-ArabicSci-70M is a Small Language Model built from scratch using the Llama architecture (Grouped-Query Attention, SwiGLU activations, RMSNorm, and RoPE) It is trained specifically on Modern Standard Arabic (MSA) to generate child-friendly stories enriched with simplified scientific facts.
1. Model Architecture
- Architecture Type: LlamaForCausalLM (~70M parameters)
- Total Trainable Parameters: 75,163,648 (~75.16M)
- Number of Layers (
num_hidden_layers): 18 - Hidden Size (
hidden_size): 512 - Intermediate Size (
intermediate_size): 1,408 (SwiGLU) - Attention Heads (
num_attention_heads): 8 (Head dimension = 64) - Key-Value Heads (
num_key_value_heads): 4 (Grouped-Query Attention, GQA) - Vocabulary Size (
vocab_size): 8,192 (Byte-Level BPE tokenizer) - Context Length (
context_length): 256 tokens (max_position_embeddings = 512)
2. Dataset & Preprocessing
-
Dataset: AtheerSky/ArabicSci_Story (30,000 Arabic scientific children's stories)
-
Dataset Splits (90% / 5% / 5%,
seed=42):- Train: 27,000 stories (packed into 256-token blocks)
- Validation: 1,500 stories (172,890 target tokens)
- Test: 1,500 stories (171,360 target tokens)
-
Source & Preprocessing:
- Filtered 30,000 clean, coherent Arabic children's stories (120–400 characters) from
sboughorbel/tinystories_dataset_arabic, removing formatting artifacts, non-Arabic characters, and exact duplicates - Added 140 simple Arabic science facts (about space, nature, physics, and animals) to the end of each story using natural Arabic closing sentences.
- Filtered 30,000 clean, coherent Arabic children's stories (120–400 characters) from
3. Scaling-Law Calculations
Using the compute-optimal rule (C ≈ 6ND and D = 20N):
- Compute Budget (C): 6.75 × 10¹⁷ FLOPs
- Planned Parameters (N): 75,000,000 parameters
- Planned Token Budget (D): 1,500,000,000 tokens
- Actual Tokens Trained (Lab Run): 4,915,200 input tokens (4,896,000 target tokens; ≈ 2.212 × 10¹⁵ FLOPs)
4. Training Setup
- Hardware: NVIDIA T4 GPU (Peak GPU memory used: 1.48 GiB)
- Learning Rate:
3e-4(Cosine schedule with20warmup steps) - Effective Batch Size:
16(4micro-batch ×4gradient accumulation = 4,096 tokens/step) - Optimizer: AdamW (
weight_decay = 0.1,beta1 = 0.9,beta2 = 0.95) - Training Steps:
1,200steps (evaluated every100steps) - Training Duration: 27 minutes (~3,052 tokens/second)
5. Evaluation Results
Results on the held-out validation and test splits after 1,200 steps:
| Split | Target Tokens | Loss | Perplexity |
|---|---|---|---|
| Baseline (Step 0, Validation) | 20,400 | 9.1124 | 9,066.65 |
| Full Validation Split (Step 1,200) | 172,890 | 3.0414 | 20.93 |
| Full Test Split (Step 1,200) | 171,360 | 3.0283 | 20.66 |
The final training loss (2.997) and full test loss (3.028) are very close, which shows good learning and no overfitting.
6. Training & Validation Loss Curve

7. Example Outputs & Limitations
Sample Generations
-
Before Training (Step 0 — Random Weights):
Prompt:
ذات مرة، كانت أثير الصغيرة تحب استكشاف الفضاء.
Output:ذات مرة، كانت أثير الصغيرة تحب استكشاف الفضاء. يحصل يحصل يحصل... الحكيمة الحكيمة...(repetitive random tokens) -
After Training (Step 1,200 — Stochastic Sampling,
temp=0.7,top_k=50,repetition_penalty=1.2):Prompt:
في يوم من الأيام، خرج نواف إلى الحديقة وشاهده
Output:في يوم من الأيام، خرج نواف إلى الحديقة وشاهده يشعر بالسعادة. أراد أن يصنع شيئاً جديداً. ابتسم وقال: "لا تقلق، يمكننا أن نفعل ذلك!" عندما حان وقت العودة، كان الطفل يشعر بعدم الارتياح والسعادة. وفي نهاية القصة، أضافت الجدة: وتذكروا دائماً أن يمتلك القرش حاسة شم قوية جداً تمكنه من اكتشاف فريسته من مسافات بعيدة في الماء. كانت تتحدث عن القرش.
Observed Limitations
- Repetition in Greedy Mode: When
do_sample=False, the model sometimes repeats phrases (like"كانت متحمسة جداً"). Usingdo_sample=True,temperature=0.7, andrepetition_penalty=1.2fixes this issue. - Limited Scope: Because the model is small (~75M parameters) and trained on 4.9M tokens from 30,000 stories, it is best for short Arabic children's stories and basic science facts, not general question-answering.
- Minor Grammar Issues: Some sentences may sound slightly unnatural due to translated text in the base dataset.
8. Usage Example
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model_id = "AtheerSky/Llama-ArabicSci-70M"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
prompt = "كان يا مكان تحت سماءالليل المرصعة بالنجوم فتاة تساءلت عن "
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=90,
do_sample=True,
temperature=0.7,
top_k=50,
repetition_penalty=1.2,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))