alephbert-base-onnx / README.md
aiterra's picture
Add model card, config and tokenizer
5c15c1a verified
|
Raw History Blame Contribute Delete
4.55 kB
metadata
license: apache-2.0
language:
  - he
library_name: transformers.js
pipeline_tag: fill-mask
base_model: onlplab/alephbert-base
base_model_relation: quantized
tags:
  - onnx
  - hebrew
  - bert
  - alephbert
  - transformers.js

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.