Ruqiya commited on
Commit
4e7a22a
·
verified ·
1 Parent(s): 8103c5e

English card: live demo up top, tested Python example

Browse files
Files changed (1) hide show
  1. README.md +124 -42
README.md CHANGED
@@ -3,75 +3,157 @@ license: mit
3
  language: [ar]
4
  library_name: onnx
5
  pipeline_tag: text-generation
6
- tags: [arabic, story-generation, scaling-laws, onnx]
7
  ---
8
 
9
- # RuqLM
10
 
11
- سلّم من خمسة نماذج لغة عربية، من **0.74M** إلى **29.89M** بارامتر، مدرَّبة من
12
- الصفر على كوربوس من **50,003 قصة عربية** بميزانية توكنات ثابتة (76.8M توكن).
13
 
14
- الأوزان بصيغة ONNX **بدقة كاملة (fp32)** — بلا تكميم. قِيست الحيرة على مجموعة
15
- التحقّق وطابقت PyTorch إلى ثلاث خانات عشرية (9.2009 مقابل 9.2009)، فالمخرجات
16
- هي مخرجات النموذج الأصلي نفسه.
 
17
 
18
- تعمل **داخل المتصفح** بلا خادم عبر ONNX Runtime Web.
 
 
19
 
20
- ## السلّم
 
 
21
 
22
- | النموذج | بارامتر | d_model | طبقات | رؤوس | الحجم |
23
  |---|---|---|---|---|---|
24
- | `ruq-30m.onnx` | 29.89M | 512 | 8 | 8 | 114.8MB |
25
- | `ruq-15m.onnx` | 13.77M | 384 | 6 | 6 | 53.1MB |
26
- | `ruq-5m.onnx` | 5.31M | 256 | 4 | 4 | 20.7MB |
27
- | `ruq-2m.onnx` | 1.90M | 128 | 4 | 4 | 7.7MB |
28
- | `ruq-0.7m.onnx` | 0.74M | 64 | 4 | 4 | 3.2MB |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- المعمارية: Transformer فك-تشفير فقط، مع RMSNorm و RoPE و SwiGLU وتضمينات
31
- مربوطة. طول السياق 512. المُرمِّز BPE بمفردات 8,192 مدرَّب على الكوربوس نفسه
32
- (1.41 توكن/كلمة).
33
 
34
- ## الواجهة
 
 
 
35
 
36
- الرسم البياني يأخذ `input_ids` بشكل `[1, seq]` ويُخرج `logits` للموضع الأخير
37
- وحده بشكل `[1, 8192]`. حلقة أخذ العيّنات خارج النموذج، فتغيير الحرارة أو
38
- top-k لا يتطلب إعادة تصدير.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  ```js
41
  const sess = await ort.InferenceSession.create('ruq-30m.onnx');
42
- const out = await sess.run({ input_ids: new ort.Tensor('int64', ids, [1, ids.length]) });
 
 
43
  ```
44
 
45
- ## حدود النموذج
 
 
46
 
47
- هذه نماذج **إكمال نصّ** (base)، لا نماذج محادثة. دُرِّبت على قصص عربية فقط،
48
- فتُكمل ما تبدأه ولا تُجيب عن أسئلة ولا تتبع تعليمات — لم تمرّ بأي ضبط تعليمات
49
- (SFT) أو RLHF. توقّع منها سرداً قصصياً، لا إجابات.
 
50
 
51
- النحو والصرف قويّان؛ التماسك الدلالي والمرجعي أضعف — وهو ما يُتوقَّع عند هذا
52
- الحجم، ولا تلتقطه مقاييس التنوّع وحدها.
 
 
53
 
54
- الكوربوس مُولَّد بمعلّمَين: **ALLaM 2.7B** (75%) و **Qwen 3.6-27B** (25%)،
55
- مع بوّابات جودة ترفض المحارف الأجنبية والافتتاحيات القالبية.
 
 
 
56
 
57
- ## التقييم
58
 
59
- | | الكوربوس | ruq-30m | ruq-0.7m |
60
- |---|---|---|---|
61
- | تنوّع المفردات (TTR) | 0.736 | 0.708 | 0.698 |
62
- | تكرار الـ 5-gram | 0.2% | 0.1% | 0.0% |
63
 
64
- ## التجربة
 
 
 
 
 
 
 
 
65
 
66
- <!-- TODO: رابط arXiv عند النشر -->
67
- الورقة قيد الإعداد. تجربة حيّة: https://ruqlm.web.app
68
 
69
- ## الاستشهاد
 
70
 
71
  ```bibtex
72
  @misc{binsafi2026ruqlm,
73
- title = {Ruq-LM: A Scaling Study of Arabic Language Models
74
- with Morphology-Aware Evaluation},
75
  author = {Bin Safi, Ruqiya},
76
  year = {2026},
77
  note = {Preprint}
 
3
  language: [ar]
4
  library_name: onnx
5
  pipeline_tag: text-generation
6
+ tags: [arabic, story-generation, scaling-laws, onnx, small-language-model]
7
  ---
8
 
9
+ # RuqLM — small Arabic language models
10
 
11
+ **▶ Try it live: [ruqlm.web.app](https://ruqlm.web.app)** — runs entirely in your
12
+ browser, no server, no signup needed for guest access.
13
 
14
+ A ladder of five Arabic language models, **0.74M to 29.89M parameters**, trained
15
+ from scratch on a corpus of **50,003 Arabic stories** under a fixed budget of 76.8M
16
+ training tokens. The tokenizer, the corpus and the weights were all built for this
17
+ project.
18
 
19
+ Weights are **full-precision ONNX** — no quantisation. Perplexity matches the
20
+ original PyTorch checkpoints to three decimals (9.2009 vs 9.2009), so these are the
21
+ trained models, not an approximation of them.
22
 
23
+ ---
24
+
25
+ ## The ladder
26
 
27
+ | Model | Parameters | d_model | Layers | Heads | Size |
28
  |---|---|---|---|---|---|
29
+ | `ruq-30m.onnx` | 29.89M | 512 | 8 | 8 | 114.8 MB |
30
+ | `ruq-15m.onnx` | 13.77M | 384 | 6 | 6 | 53.1 MB |
31
+ | `ruq-5m.onnx` | 5.31M | 256 | 4 | 4 | 20.7 MB |
32
+ | `ruq-2m.onnx` | 1.90M | 128 | 4 | 4 | 7.7 MB |
33
+ | `ruq-0.7m.onnx` | 0.74M | 64 | 4 | 4 | 3.2 MB |
34
+
35
+ Decoder-only transformer: RMSNorm, RoPE, SwiGLU, tied embeddings, 512-token context.
36
+ Tokenizer is a byte-level BPE with an 8,192 vocabulary trained on the same corpus
37
+ (1.411 tokens per word).
38
+
39
+ ---
40
+
41
+ ## Usage
42
+
43
+ Install `onnxruntime`, `tokenizers` and `huggingface_hub`, then:
44
+
45
+ ```python
46
+ import re, unicodedata
47
+ import numpy as np, onnxruntime as ort
48
+ from huggingface_hub import hf_hub_download
49
+ from tokenizers import Tokenizer
50
+
51
+ REPO, EOS = "Ruqiya/ruqlm", 2
52
+
53
+ tok = Tokenizer.from_file(hf_hub_download(REPO, "tokenizer.json"))
54
+ sess = ort.InferenceSession(hf_hub_download(REPO, "ruq-30m.onnx"))
55
+
56
+ # The tokenizer was trained on normalised text: diacritics and tatweel
57
+ # removed. Skipping this step gives a different, worse tokenisation.
58
+ _DIACRITICS = re.compile(r"[ً-ْٰٓ-ٕ]")
59
+ _ZERO_WIDTH = re.compile(r"[​-‏‪-‮]")
60
+
61
+ def normalize(text):
62
+ text = unicodedata.normalize("NFC", text)
63
+ text = _ZERO_WIDTH.sub("", text).replace("ـ", "")
64
+ return re.sub(r"\s+", " ", _DIACRITICS.sub("", text)).strip()
65
+
66
+ def generate(prompt, max_new_tokens=120, temperature=0.85, top_k=50, seed=None):
67
+ """The model outputs logits for the last position only; sampling is here."""
68
+ rng = np.random.default_rng(seed)
69
+ ids = tok.encode(normalize(prompt)).ids
70
+ if ids and ids[-1] == EOS: # drop the trailing </s> so it continues
71
+ ids = ids[:-1]
72
+
73
+ for _ in range(max_new_tokens):
74
+ logits = sess.run(None, {"input_ids": np.array([ids], dtype=np.int64)})[0][0]
75
+ logits = logits.astype(np.float64) / temperature
76
+
77
+ kth = np.partition(logits, -top_k)[-top_k] # top-k filter
78
+ logits[logits < kth] = -np.inf
79
 
80
+ probs = np.exp(logits - logits.max())
81
+ probs /= probs.sum()
 
82
 
83
+ nxt = int(rng.choice(len(probs), p=probs))
84
+ if nxt == EOS:
85
+ break
86
+ ids.append(nxt)
87
 
88
+ return tok.decode(ids)
89
+
90
+ print(generate("كان يا ما كان"))
91
+ ```
92
+
93
+ Output (`ruq-5m`, seed 0):
94
+
95
+ > كان يا ما كان في حديقة بيت. كان النجار يعمل بجد ليطعم عائلته. وفي يوم من الأيام،
96
+ > وأثناء عمله، وجد النجار سلحفاة صغيرة ضائعة…
97
+
98
+ ### Interface
99
+
100
+ The graph takes `input_ids` of shape `[1, seq]` and returns `logits` for the **last
101
+ position only**, shape `[1, 8192]`. The sampling loop lives outside the model, so
102
+ temperature and top-k can change without re-exporting.
103
+
104
+ ### In the browser
105
 
106
  ```js
107
  const sess = await ort.InferenceSession.create('ruq-30m.onnx');
108
+ const out = await sess.run({
109
+ input_ids: new ort.Tensor('int64', BigInt64Array.from(ids, BigInt), [1, ids.length])
110
+ });
111
  ```
112
 
113
+ ---
114
+
115
+ ## Limitations
116
 
117
+ These are **base text-completion models, not chat models.** They were trained on
118
+ Arabic stories only, so they continue what you start; they do not answer questions or
119
+ follow instructions, having had no instruction tuning (SFT) or RLHF. Expect
120
+ narrative, not answers.
121
 
122
+ Morphology and orthography are strong. **Semantic and referential coherence are
123
+ weaker** — which is what the accompanying study measures, and what these models were
124
+ built to quantify rather than to solve. Even the largest rung abandons 5.9% of the
125
+ characters it introduces, where the training corpus abandons 0.0%.
126
 
127
+ The corpus is synthetic, generated by two teacher models: **ALLaM 2.7B** (75%) and
128
+ **Qwen3.6-27B** (25%), with quality gates rejecting foreign characters and template
129
+ openings.
130
+
131
+ ---
132
 
133
+ ## What the models are for
134
 
135
+ They were built to answer a measurement question: across a 40× range of parameters
136
+ at fixed data, which linguistic competence scales and which saturates?
 
 
137
 
138
+ | Axis | Behaviour across the ladder |
139
+ |---|---|
140
+ | Lexical diversity (TTR) | **saturated** — 0.719 ± 0.005 → 0.721 ± 0.005 (0.4σ) |
141
+ | Character tracking | **scales** — 31.6% → 5.9% abandoned (6.3σ) |
142
+ | Orthography, agreement | saturated below 0.74M |
143
+
144
+ Capacity buys coherence, not vocabulary.
145
+
146
+ ---
147
 
148
+ ## Paper
 
149
 
150
+ <!-- TODO: arXiv link on release -->
151
+ *In preparation.* Corpus, metrics, checkpoints and every measurement are released.
152
 
153
  ```bibtex
154
  @misc{binsafi2026ruqlm,
155
+ title = {What Scales in a Small Arabic Language Model?
156
+ Morphology Saturates, Coherence Does Not},
157
  author = {Bin Safi, Ruqiya},
158
  year = {2026},
159
  note = {Preprint}