AlephBERT base, ONNX

ONNX build of onlplab/alephbert-base, the Hebrew BERT model from the ONLP Lab at Bar-Ilan University. It runs with Transformers.js in the browser and in Node.js, and with ONNX Runtime in Python, without PyTorch.

The weights are the original ones. Only the format changed, and one of the two files is quantized.

File Size Precision
onnx/model.onnx 664 MB fp32, same numbers as the PyTorch model
onnx/model_quantized.onnx 167 MB int8 dynamic quantization, 4 times smaller

Transformers.js

npm install @huggingface/transformers
import { pipeline } from '@huggingface/transformers'

const unmasker = await pipeline('fill-mask', 'aiterra/alephbert-base-onnx', { dtype: 'q8' })
const result = await unmasker('ירושלים היא [MASK] של ישראל.', { top_k: 5 })

dtype: 'q8' loads the 167 MB file, dtype: 'fp32' loads the full one. Checked with @huggingface/transformers 4.3.2 on Node.js.

Python, ONNX Runtime

import numpy as np
import onnxruntime as ort
from huggingface_hub import hf_hub_download
from transformers import AutoTokenizer

repo = "aiterra/alephbert-base-onnx"
tokenizer = AutoTokenizer.from_pretrained(repo)
session = ort.InferenceSession(hf_hub_download(repo, "onnx/model_quantized.onnx"))

inputs = tokenizer("ירושלים היא [MASK] של ישראל.", return_tensors="np")
feed = {name: value.astype(np.int64) for name, value in inputs.items()}
logits = session.run(None, feed)[0]

mask = int(np.where(inputs["input_ids"][0] == tokenizer.mask_token_id)[0][0])
print(tokenizer.convert_ids_to_tokens(np.argsort(-logits[0, mask])[:5]))

The model takes input_ids, attention_mask and token_type_ids as int64 and returns logits of shape (batch, tokens, 52000).

How close it is to the original

Top 5 predictions for the masked word, original PyTorch model against the two ONNX files:

Sentence PyTorch and model.onnx model_quantized.onnx
ירושלים היא [MASK] של ישראל. הבירה, הבית, בירת, הלב, ביתם הבירה, הבית, בירת, העתיד, ביתם
הילדים הלכו לבית [MASK] בבוקר. הספר, ספר, הכנסת, החולים, חולים הספר, ספר, הכנסת, החולים, הקפה
אני רוצה לשתות כוס [MASK]. מים, קפה, יין, תה, מיץ מים, קפה, יין, תה, מיץ
  • model.onnx: the largest difference in any logit against PyTorch is below 0.00005 on these sentences, and the top 5 is identical.
  • model_quantized.onnx: the first three predictions stay the same, the fourth or fifth can change.

Three sentences are a sanity check, not a benchmark. If you fine-tune or measure accuracy on a task, compare the two files on your own data.

Limits

  • This is the masked language model, so the output is vocabulary logits. It does not return sentence embeddings. For search or similarity use a model trained for embeddings.
  • Inputs longer than 512 tokens are not supported, as in the original.
  • The model was trained on Hebrew Wikipedia, the Hebrew part of OSCAR and Hebrew tweets, and it carries the biases of that text.

How it was converted

torch.onnx.export with opset 14 and dynamic batch and sequence axes, then onnxruntime.quantization.quantize_dynamic with QuantType.QUInt8. Versions: PyTorch 2.1.0, Transformers 4.35.0, ONNX 1.17.0, ONNX Runtime 1.23.0.

Credits and licence

The model was trained by the ONLP Lab at Bar-Ilan University. If you use it, cite the original paper:

@misc{seker2021alephbert,
  title={AlephBERT: A Hebrew Large Pre-Trained Language Model to Start-off your Hebrew NLP Application With},
  author={Amit Seker and Elron Bandel and Dan Bareket and Idan Brusilovsky and Refael Shaked Greenfeld and Reut Tsarfaty},
  year={2021},
  eprint={2104.04052},
  archivePrefix={arXiv},
  primaryClass={cs.CL}
}

Apache-2.0, the same licence as the original model.

Converted by Aiterra, an Israeli studio that builds AI agents, automation and websites. More open Hebrew and Israeli tools: aiterra.co.il/tools.

Downloads last month
20
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for aiterra/alephbert-base-onnx

Quantized
(1)
this model

Paper for aiterra/alephbert-base-onnx