Metadata-Version: 2.5
Name: sarfchat
Version: 2.0.0
Summary: SarfChat-v2 (صرف-شات): Algebraic Semitic Root×Pattern Transformer, Certified Byte-Lossless Sarf-BPE, & Iso-FLOP Scaling-Law Harness (Maqayis)
Project-URL: Homepage, https://maqayis.org
Project-URL: Repository, https://github.com/maqayis/sarfchat
Project-URL: Mirror, https://codeberg.org/maqayis/sarfchat
Author-email: Salah Abdo <Salah.Abdo.Tech@gmail.com>
License: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Keywords: arabic,grassmannian,maqayis,morphology,sarfchat,scaling-laws,transformer
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Natural Language :: Arabic
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.11
Requires-Dist: huggingface-hub>=0.26.0
Requires-Dist: numpy>=1.26.0
Requires-Dist: pyarrow>=15.0.0
Requires-Dist: safetensors>=0.4.5
Requires-Dist: scikit-learn>=1.4.0
Requires-Dist: scipy>=1.12.0
Requires-Dist: tarkib>=2.0.0
Requires-Dist: tokenizers>=0.20.0
Requires-Dist: torch>=2.4.0
Provides-Extra: data
Requires-Dist: datasets>=3.0.0; extra == 'data'
Provides-Extra: rag
Requires-Dist: tayf>=1.0.0; extra == 'rag'
Description-Content-Type: text/markdown

# SarfChat-v2 (`صرف-شات`) v2.0.0 — المحوِّل الصرفي–الغراسماني التأسيسي وقوانين التوسّع التوليدي

<div align="center">

**Algebraic Semitic Root×Pattern Foundation Transformer (`d16`, `261.75M` Params, `5.10B` Tokens), Certified `100%`-Byte-Lossless `Ṣarf-BPE`, `Grassmann-VE` Routing, & `23.8 μs` GPU `Tarkīb` Shield**  
Part of the **Maqāyīs (`مَقاييس`)** Open-Source Ecosystem · [maqayis.org](https://maqayis.org) · [github.com/maqayis/sarfchat](https://github.com/maqayis/sarfchat) · [codeberg.org/maqayis/sarfchat](https://codeberg.org/maqayis/sarfchat)

[![License: Apache-2.0](https://img.shields.io/badge/Code-Apache--2.0-blue.svg)](LICENSE)
[![Paper & Results: CC BY 4.0](https://img.shields.io/badge/Paper_%26_Results-CC_BY_4.0-246B45.svg)](NOTICE)
[![Python 3.11+](https://img.shields.io/badge/Python-3.11%2B-3776AB.svg)](pyproject.toml)

</div>

---

## نظرة عامّة | Overview

**`SarfChat-v2` (`صرف-شات v2.0.0`)** هو المحوِّل التوليدي التأسيسي ومختبر قوانين التوسّع متساوية الحوسبة (`Iso-FLOP Scaling Laws`) في منظومة **مَقاييس (`Maqāyīs`)** من تأليف وتطوير **صلاح عبده (`Salah Abdo`)**. بدلاً من الاكتفاء بترقيع النماذج الأجنبية من الخارج أو استنزاف ميزانية المعلمات في جداول التقطيع الإحصائي الصمّاء (`Byte-BPE`)، يدمج `SarfChat-v2` جبر الجذر والوزن الريماني وألياف غراسمان المتعدّدة الأصول (`Gr(k_R, 256)`) والسمات النطقية لسيبويه في صميم طبقات المحوِّل ذاته.

**`SarfChat-v2` (`v2.0.0`)** replaces monolithic subword memorization tables with **asymmetric morphological-Grassmannian inductive biases**:
1. **Certified `100.0%`-Byte-Lossless `Ṣarf-BPE` & `FactoredSarfDAGHead` (`Exp S1`):** Folds `65.67%` of running Arabic words (`36,186 / 55,103`) and `100.0%` (`534 / 534`) of unseen OOV-root words into a single algebraic frame `(p, C1, C2, C3, w, e)`, eliminating OOV radical fracture (`2.41 -> 1.00` steps/word) and saving `1.22×` quadratic attention FLOPs while guaranteeing `100.000%` UTF-8 byte round-trip identity.
2. **Dynamic `Grassmann-VE` Routing (`GrassmannFiberValueRouter`, `Exp S2–S3`):** Projects hidden states onto Ibn Fāris's orthonormal multi-*aṣl* sense bundles `Gr(k_R, 256)` across `L/2` alternating layers, compressing deep value tables by **`-93.6%`** (`75.50M -> 4.83M` at `d12`; `134.22M -> 9.54M` at `d16`).
3. **19-Run Iso-FLOP Scaling & Causal Permutation Campaign (`Exp S2–S3`):** Breaks the multi-epoch flat-BPE memorization wall (`1.0962 -> 0.9716 bpb`, **`-0.1246 bpb`** at `d12`) and causally separates low-rank value regularization on seen words (`token_perm` / `root_perm` / `true_tarkib` within `±0.003 bpb` overall) from authentic sub-root zero-shot generalization on unseen OOV-root target words (**`2.6677 bpb`** vs `2.7009` `token_perm`, `2.7007` `root_perm`, and `2.8620` flat BPE; paired `t = -5.94, p = 2.83×10⁻⁹` across `97` roots).
4. **Asymmetric Flagship `SarfChat-v2` (`d16`, `261.75M` Params, `5.10B` Tokens, `Exp S4–S5`):** Pairs `TarkibSubwordInjector` (`23,253 / 32,768` root-grounded subwords = `70.96%`, `29.49%` interface share vs `64.52%` flat equivalent) with single-pass `_FusedChunkedSoftcapLinearCE` (`8.59 GB -> 0.54 GB` logit VRAM, **`-93.7%`**, `105.0k tok/s`, **`58.3% MFU`**) and paragraph-level OOV surgical isolation (`1.4% -> 57.7%` long-book retention = **`41.2×`**, `0.0%` root leakage).
5. **`23.8 μs` GPU `Tarkīb` Morpho-Phonotactic Shield & `Tayf` Neuro-Symbolic Prism (`Exp S6–S7`):** Lifts morpho-phonotactic & ontological legality to **`100.0%`** (`40/40` core suite: `97.5% -> 100.0%`; `200/200` extended suite: `98.0% / 96.5% -> 100.0%`) in `23.82 μs` per step, and lowers grounded answer entropy from `1.238 -> 0.673 bpb` (`95.0%` homonym trap survival) when paired with Zero-LLM [`maqayis/tayf`](https://github.com/maqayis/tayf).

---

## النتائج المرجعية | Headline `SarfChat-v2` Metrics

| Metric / Stage | Specification / Split | Value |
| :--- | :--- | :--- |
| **Architecture (`sarfchat_v2`)** | `d16` (`L=16, d=1024, n_q=16, n_kv=8, T=2048, V=32,768`) | **`261,751,467`** total / **`221,788,952`** active matmul |
| **Interface Parameter Tax** | `SarfChat-v2` (`77.20M`) vs Flat Equivalent (`335.54M`) | **`29.49%`** vs `64.52%` (`-258.3M` params saved) |
| **Pretraining Scale (Stages 1 + 2)** | `WSD Plateau` (`4.40B`) + `WSD Cooldown` (`0.70B`) | **`5,095,292,928` tokens** (**`33.63 GB`** UTF-8, `8.83e18` FLOPs) |
| **Overall Validation `val_bpb`** | Held-out multi-register `val_v2` (`0.8157` plateau -> cooldown) | **`0.7353 bpb`** (`0.73529`, `-0.0804` cooldown drop) |
| **Register Breakdown (`val_bpb`)** | `msa_edu` / `msa_wiki_books` / `classical_turath` | **`0.7139`** / **`0.7321`** / **`0.7598 bpb`** |
| **Zero-Shot Unseen OOV-Root `bpb`** | `511` OOV-root docs (`100` strictly held-out roots, `0%` leakage) | **`0.6976 bpb`** (`0.69757` doc) · **`1.8861 bpb`** (`643` OOV words) |
| **Stage 3 Arabic Chat SFT** | `46,858` train / `600` val multi-turn dialogues (`342` steps) | `sft_val_loss`: `4.0122 ->` **`2.4743`** (`PPL = 11.87`) |
| **GPU `Tarkīb` Guard (`23.82 μs`)** | `40`-pair core suite (`base`) · `200`-pair fortification (`base` / `sft`) | `97.5% ->` **`100.0%`** (`40/40`) · `98.0% / 96.5% ->` **`100.0%`** (`200/200`) |
| **Neuro-Symbolic `Tayf + SarfChat-v2`** | `20`-query adversarial homonym-trap grounded QA (`Exp S7-B`) | `1.238 ->` **`0.673 bpb`** (`-45.7%`) · **`95.0%`** (`19/20`) trap survival |

> **Book `v2.0` Sync Note:** *The Geometry of the Arabic Tongue* (`Book v2.0`, [`maqayis/tarkib`](https://github.com/maqayis/tarkib)) Ch. 21 reports the foundational `SarfChat-v1` `d12` Iso-FLOP campaign and early `d16` specification, while Part X (Ch. 22–23) and this repository (`v2.0.0`) present the completed `5.10B`-token 3-stage `d16` WSD cooldown + Chat SFT campaign (`val_bpb = 0.7353`), `19`-run causal permutation matrix, and `200`-pair GPU guard audit.

---

## التثبيت والتشغيل السريع | Installation & Quickstart

```bash
pip install sarfchat
```

### 1. Offline CPU Unit Tests (`<200 MB` RAM, Zero Downloads Required)
```bash
pytest -q
```

### 2. Hydrate Checkpoints & Tokenizers (SHA-256 Verified via `data-manifest.tsv`)
```bash
./fetch-data.sh data   # corpus splits, bpe16k/bpe32k tokenizers, and Tarkīb priors (~499 MB)
./fetch-data.sh runs   # all 8 safetensors checkpoints incl. sarfchat_v2_d16_base & _sft (~2.96 GB)
```

### 3. Launch Interactive RTL Web Arena or Terminal Chat
```bash
sarfchat-arena --host 127.0.0.1 --port 8787
sarfchat-chat --checkpoint checkpoints/sarfchat_v2_d16_sft
```

See [`WEIGHTS_AND_REPRODUCIBILITY.md`](WEIGHTS_AND_REPRODUCIBILITY.md) for complete `SHA-256` checkpoint manifests, model cards, and exact reproduction commands, and [`paper/main.pdf`](paper/main.pdf) (`paper/main.tex`) for the full Arabic monograph study.

---

## الترخيص وحقوق الملكية | License & Attribution

- **Software & Source Code (`src/`, `scripts/`, `tests/`):** [Apache License 2.0](LICENSE)
- **Research Paper, Figures, & Empirical Result Ledgers (`paper/`, `results/`):** [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **Author:** Salah Abdo (`صلاح عبده`) `<Salah.Abdo.Tech@gmail.com>` · `Copyright 2026 Salah Abdo and successor entity` · [`NOTICE`](NOTICE) · [`CITATION.cff`](CITATION.cff)
