CAMeLBERT-mix-sentiment — Apple Core AI, Arabic sentiment
A Core AI (.aimodel) build of
CAMeL-Lab/bert-base-arabic-camelbert-mix-sentiment
for on-device Arabic sentiment on Apple Silicon.
Upstream ships pytorch_model.bin, flax_model.msgpack and tf_model.h5 — and no MLX, no ONNX,
no Core AI. 100,274 downloads, and this is the Core AI build.
Outputs
One graph, two outputs. Keeping both costs nothing at this size and means neither the trained classifier nor the reusable vectors are thrown away.
| Output | Shape | What it is |
|---|---|---|
| logits | float32[1, 3] |
the trained head, in id2label order: 0 = positive, 1 = negative, 2 = neutral |
| embeddings | float32[1, 768] |
mean-pooled over the mask, L2-normalised — our addition, not an upstream objective |
Read this before reading the outputs
Select outputs by shape, not by name. The runtime names tensors after the op that produced them.
On this graph that is div_1 for the vectors and addmm_73 for the logits — the names in the
wrapper's dict do not survive into the .aimodel. A one-line size == 3 / size == 768 pick is the
robust way, and it is what the measurement script does.
Verified equivalence
Compared against the original PyTorch model on six fixed inputs at the exported shape:
| Input | Predicted | logits max abs diff | embeddings max abs diff | cosine |
|---|---|---|---|---|
| الخدمة كانت ممتازة وأنصح به بشدة. | positive | 2.38e-06 | 1.14e-07 | 1.00000000 |
| تجربة سيئة جدا ولن أعود مرة أخرى. | negative | 1.43e-06 | 1.42e-07 | 1.00000000 |
| المنتج وصل أمس. | neutral | 7.15e-07 | 2.09e-07 | 1.00000000 |
| فريق الدعم ساعدني بسرعة وحل المشكلة. | positive | 5.96e-07 | 4.17e-07 | 1.00000000 |
| الطلب اتأخر أسبوعين وما حد رد عليا. | neutral | 1.43e-06 | 1.87e-07 | 1.00000000 |
| كيف أستعيد كلمة المرور؟ | neutral | 4.77e-07 | 1.06e-07 | 1.00000000 |
Worst across all six: 2.38e-06, and 6/6 labels agree with PyTorch. That residual is relative ~1e-6 on logits of magnitude ~3 — float32 rounding accumulated through the classifier layer, not a difference in the model.
One colloquial case, and what it does not show
الطلب اتأخر أسبوعين وما حد رد عليا. ("the order was two weeks late and nobody answered me") is
classified neutral by this model. PyTorch classifies it neutral too and the conversion reproduces
that exactly — so this is the model's own behaviour on a dialect complaint, and it carries through
unchanged.
One sentence, not a survey: dialect sentiment corpora were not run, and no claim is made about how often this happens.
Use
import asyncio, numpy as np
from coreai.runtime import AIModel, NDArray, SpecializationOptions
from transformers import AutoTokenizer
LABELS = ["positive", "negative", "neutral"]
SEQUENCE_LENGTH = 512 # fixed shape: pad to it
async def analyse(text: str):
tok = AutoTokenizer.from_pretrained(".")
model = await AIModel.load("camelbert-arabic-sentiment.aimodel", SpecializationOptions.default())
fn = model.load_function("main")
enc = tok([text], padding="max_length", truncation=True, max_length=SEQUENCE_LENGTH,
return_tensors="np")
out = await fn({"input_ids": NDArray(enc["input_ids"].astype("int32")),
"attention_mask": NDArray(enc["attention_mask"].astype("int32"))})
flat = {k: np.asarray(v.numpy()).reshape(-1) for k, v in out.items()}
logits = next(v for v in flat.values() if v.size == 3)
vectors = next(v for v in flat.values() if v.size == 768)
return LABELS[int(np.argmax(logits))], logits, vectors
print(asyncio.run(analyse("الخدمة كانت ممتازة وأنصح به بشدة."))[0]) # positive
What is in here
| File | |
|---|---|
camelbert-arabic-sentiment.aimodel/ |
the graph |
manifest.json |
source, revision, label order, output shapes, precision |
validation.json |
PyTorch-to-Core AI equivalence for each case above (not the download count) |
camelbert_sent_coreai.py |
the exporter and measurer — --check, --export, --measure |
vocab.txt, config.json, tokenizer files |
so the tokenizer loads from this repository |
Limits
- Fixed at 512 tokens; pad rather than passing a shorter sequence.
- Six inputs measured. Dialect variation, long documents and the 512-token boundary were not.
- No speed or memory benchmark. Equivalence only.
- The classification is 3-way and single-label; no confidence calibration was measured.
Licence and attribution
Apache-2.0, as declared by upstream. A derivative work of
CAMeL-Lab/bert-base-arabic-camelbert-mix-sentiment; credit for the model is theirs.