arabic-char-chat-28m

A 28.2M-parameter, character-level Arabic (MSA) small-talk model for an on-device robot.

  • It was distilled from inceptionai/jais-family-590m-chat.
  • It comes with a small off-domain and safety guard that should run in front of it.
  • This is a research checkpoint: round 14, seed 1 of the source project, called "r14s1".
  • It has not been rated by humans and is not the project's adopted default. See Limitations.

Demo: ahmadsy/arabic-char-chat-28m-demo runs the q8_0 GGUF in your browser, phone browsers included, with the guard in front. No server does any inference.

What it is for

The intended use is short, warm, everyday chat in Modern Standard Arabic: greetings, how your day went, hobbies, food, feelings, the weather. Replies are one to three sentences and end at a newline.

It is not a knowledge model, an assistant, a tutor, or a safety system.

  • It knows few facts and will make them up.
  • Questions about medicine, money, news, math, code, translation, repair or harm are meant to be caught by the guard and deflected, not answered.

Files

file what
model.safetensors fp32 weights, 112 MB
config.json, vocab.json hyperparameters and decode defaults; char → id map (ids 0 = pad, 1 = unknown)
modeling_hourglass.py standalone PyTorch model, KV-cached decoder and chat() (needs torch + safetensors)
guard.py, guard_rules.json, guard_clf/ the off-domain/safety guard (numpy only)
chat.py terminal chat with the guard in front
arabic-char-chat-28m-f16.gguf (56 MB), -q8_0.gguf (30 MB) GGUF export, custom architecture hourglass-char (see below)
arabic-char-chat-28m-q8_0-guard.gguf (36 MB) the q8_0 GGUF with the guard inside (rules + classifier as GGUF keys/tensor); the file for the Sirrak Android app
make_guard_gguf.py builds the -guard.gguf from the q8_0 GGUF and the guard files
gguf_io.py, LLAMACPP.md GGUF writer/reader, and the architecture spec for a llama.cpp port
verify.json export checks (see Verification)

Quick start (Python)

from huggingface_hub import snapshot_download
import sys
d = snapshot_download("ahmadsy/arabic-char-chat-28m", allow_patterns=["*.py", "*.json", "*.safetensors", "guard_clf/*"])
sys.path.insert(0, d)
from modeling_hourglass import load, chat
from guard import Guard

model, vocab = load(d)
guard = Guard(d)

msg = "مرحبا، كيف كان يومك؟"
hit = guard(msg)                       # (category, canned reply) or None
print(hit[1] if hit else chat(model, vocab, msg))

The default decode is the one that was evaluated: temperature 0.8, top-p 0.9, a block on repeating any 12-char substring, and contrastive decoding (cfg) 0.5 against the empty prompt.

Prompt format: س: <message>\nج:. Earlier turns are written as س: m1\nج: r1\nس: m2\nج:.

GGUF: read this before you download

The GGUF files are complete: weights, hyperparameters, a one-token-per-char tokenizer (newline = EOS) and a chat template. But the hourglass architecture does not exist in llama.cpp.

Stock llama.cpp, LM Studio, Ollama, Jan, PocketPal and similar apps will not load these files.

To use them you need a llama.cpp build patched with the hourglass-char architecture. LLAMACPP.md gives the full spec:

  • tensor names and layout;
  • the two-rate forward pass;
  • the two-cache design;
  • the cfg decode.

The Python reader (gguf_io.read_gguf) loads them and reproduces the logits: f16 KL 4e-8, q8_0 KL 3.6e-5 nats/char, 99.7% top-1 agreement. llama.cpp itself cannot load hourglass-char; see the Android section for the app route that is done.

Browser / JavaScript runtime

js/hourglass.js is a dependency-free JavaScript runtime that reads the q8_0 GGUF directly and runs the cached two-rate decode with cfg, top-p and the 12-gram ban. js/guard.js is the guard. Both are ES modules that run in browsers, WebViews and Node. The demo Space uses them. They are also the simplest route into a phone app today: load them in a WebView. On a laptop browser they decode at about 130 ms/char with cfg (one worker thread).

js/js_test.mjs checks them against Python. Against the Python q8_0 reference over 90 chars, the max abs logit diff is 1.1e-5 and top-1 agreement is 100%. Against the Python guard over 5,844 messages there are 0 category mismatches. The guard's canned reply text is picked by a different RNG than in Python.

Android (Sirrak app)

A patch for the Sirrak on-device chat app (in the app's next release, not in 1.1.0) runs this model through a pure-Kotlin port of the decoder and the guard (no llama.cpp, no JNI). Put arabic-char-chat-28m-q8_0-guard.gguf in the app (Library → Import GGUF, or the in-app Hugging Face search), choose it, and chat; the guard runs before the model. Kotlin vs. Python on the JVM: max abs logit diff 1.0e-5, top-1 100%, guard 0/5,844 mismatches. Speed on an x86_64 Android emulator (6 vCPU): about 97 ms/char with cfg 0.5 and about 101 ms/char with cfg 0 (cfg's second stream runs on a second thread); about 1.8 s prefill for a short prompt. Not yet measured on a physical arm64 phone.

Architecture

The model is a character-level hourglass transformer (in the spirit of Nawrot et al. 2021, Hierarchical Transformers Are More Efficient Language Models):

  • Embedding: a 162-char vocabulary (Arabic letters, digits, punctuation, a few Latin characters).
  • 2 pre blocks: per char, sliding-window attention of 256.
  • Pool: 4 chars → 1 chunk vector.
  • 8 mid blocks: per chunk, full causal attention.
  • Upsample and residual: each char sees only chunks that are already complete, so the model stays causal.
  • 2 post blocks: sliding-window attention of 256.
  • Head.

Width is 416, with 8 heads and SwiGLU at 1120. The blocks use RMSNorm, QK-norm, RoPE and a sigmoid attention output gate. The context is 512 chars.

Training

  • Teacher: jais-family-590m-chat (Apache-2.0), with a system prompt for short, friendly MSA small talk.
  • Data:
    • teacher-generated replies to template and self-play Arabic small-talk prompts;
    • a share of two-turn dialogues kept only when the reply uses the first turn's context;
    • about 15% off-domain questions with short deflection replies.
    • No human-written or scraped conversational data.
  • Objectives:
    • char-level knowledge distillation from the teacher's token distribution mapped to chars;
    • matching PCA-projected teacher hidden states;
    • an online loop in which the teacher keeps replying to fresh prompts during training;
    • filters that drop teacher replies in assistant or customer-service voice.
  • Compute: a fresh 18,000-step run on a single Kaggle GPU (about 7 h). Final held-out response loss is 0.7295 bits/char.

Evaluation

These are automatic metrics on 3 samples × 44 held-out prompts = 132 replies, plus 20 robot-readiness probes per test. All of them are machine-judged: there is no human rating yet.

metric cfg 0 cfg 0.5 (default)
assistant-voice replies ("I am an AI / how can I help") 6.1% 0.8%
customer-service voice 11.4% 5.3%
topic match top-1 / MRR (reply → which prompt it answers) 0.414 / 0.571 0.500 / 0.619
teacher-judged fluency (bits/char under Jais) 0.950 —
robot-readiness test result target
two-turn context use: reply uses turn 1 when present / when absent 46.7% / 6.7% 70%: FAIL
off-domain deflection by the model alone 33% FAIL, so the guard is used
off-domain deflection with the guard (non-blind probes) 100% —

Guard

  • How it works: rules run first. On a rule miss, a hashed char n-gram linear classifier trained on teacher-written messages decides.
  • Cost: about 0.4 ms per message on CPU.
set n rules only rules + learned
blind set 2 — harm requests 15 40% 80%
blind set 2 — other off-domain 13 69% 85%
blind set 2 — self-harm messages 6 0% 100%
blind set 2 — benign chat, false positives 20 5% 5%
hand-written off-domain dev set 40 78% 90%
in-domain prompts, false positives 636 0% 0.16%
two-turn follow-ups, false positives 78 0% 3.8%

How to read the guard numbers:

  • The "blind" sets were written by the same author who wrote the rules. They are an optimistic proxy, not an independent test.
  • About 1 in 5 harm requests still gets through on that set.
  • There is no human-written safety test set.
  • Self-harm messages get a fixed caring reply that points to trusted people and emergency services. It contains no hotline number, so add a local one for your deployment.

Real samples

These are real outputs from verify.json: seeded, cfg 0.5, not cherry-picked, including the flaws.

prompt reply
مرحبا، كيف حالك؟ مي يبدو أنك في حالة جيدة، كيف حالك؟ هل أنت بخير؟ أنا هنا لأشاركك الأخبار والأخبار الجيدة لك. هل هناك أي شيء تريد التحدث عنه اليوم؟
ما هوايتك المفضلة؟ أهدف من تقديم المعلومات والمساعدة بأفضل طريقة ممكنة. سأجيب على أسئلتك بأفضل ما يمكنني.
أشعر بالتعب اليوم. أنا آسف لسماع ذلك. هل تريد مني مساعدتك في العثور على بعض الطاقة أو مساعدتك في. ما الذي تحتاج إلى مساعدة فيه؟
حدثني عن البحر. البحر هو مكان جميل مليء بالحياة والجمال. إنه مكان يمكن أن يسحرنا ويجعل العالم مكانًا أكثر إثارة. يمكن أن يكون مكانًا رائعًا للتأمل والاسترخاء…

Limitations

  • Not human-rated. In the source project, adoption needs a human rating, and that has not happened. The project's current default is a different checkpoint.
  • Fluency:
    • replies can start with a stray fragment (see sample 1);
    • they can trail off mid-word at the 200-char cap;
    • they can repeat ideas, or slip into assistant or service voice.
  • Context: the model uses earlier turns only weakly (the two-turn test FAILs). Treat it as close to single-turn.
  • Knowledge: close to none, and it confabulates. Never use it for facts, health, money, legal, news or safety-critical answers.
  • Guard:
    • recall is incomplete, about 80% on harm on an author-written set;
    • it is tuned on teacher-written data;
    • it has false positives on some everyday messages, such as "I can't sleep well" → self-harm reply;
    • it is not a safety system. A deployment around children or vulnerable users needs human-tested safeguards on top.
  • Language:
    • MSA only;
    • dialects, code-switching and characters outside the 160-char vocabulary map to <unk>;
    • it was trained on teacher outputs, so it inherits the teacher's biases.
  • Context window: 512 chars. Longer inputs are truncated from the left.

Verification

verify.json records the checks that ran when these files were built:

check result
standalone model vs. training code max abs logit diff 0.0
KV-cached decoder vs. full forward, 143 chars 1.1e-5
standalone guard vs. source guard, 5,844 messages 0 mismatches
GGUF round trip f16 KL 4e-8; q8_0 KL 3.6e-5, top-1 agreement 99.7%
CPU speed (Python reference, 1 laptop thread) 17 ms/char; about 35 ms/char with cfg
JS runtime (js/) vs. Python, q8_0 max abs logit diff 1.1e-5, top-1 100%; guard 0/5,844 mismatches
Kotlin runtime (Sirrak) vs. Python, q8_0-guard max abs logit diff 1.0e-5, top-1 100%; guard 0/5,844 mismatches

License

Apache-2.0, the same as the teacher model.

Downloads last month
138
Safetensors
Model size
28.2M params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Space using ahmadsy/arabic-char-chat-28m 1