# Convert bert tokenizer to onnx

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-bert-tokenizer-to-onnx%2F8299%3E%5C
**Category:** Beginners\
**Created:** [July 13, 2021, 7:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-bert-tokenizer-to-onnx%2F8299 "2021-07-13T07:31:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasak](https://avatars.discourse-cdn.com/v4/letter/h/a698b9/32.png) [@hasak](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fhasak)%5C
**Post date:** [July 13, 2021, 7:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-bert-tokenizer-to-onnx%2F8299%2F1 "2021-07-13T07:31:07Z")

</div>

I was referring to the following blog to convert bert model to onnx.

> **[Accelerate your NLP pipelines using Hugging Face Transformers and ONNX Runtime](https://medium.com/microsoftazure/accelerate-your-nlp-pipelines-using-hugging-face-transformers-and-onnx-runtime-2443578f4333)**
>
> This post was written by Morgan Funtowicz from Hugging Face and Tianlei Wu from Microsoft

here, to take the inference of bert tokenizer, I’ll have to pass the 2d arrays.

Is there a way, where I’ll be able to pass sentence as input to the onnx tokenizer and get encodings as output, so that I’ll be able to use the model platform-independent

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [July 13, 2021, 12:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-bert-tokenizer-to-onnx%2F8299%2F2 "2021-07-13T12:06:06Z")

</div>

hi @hasak,

> [@hasak](#):
>
> Is there a way, where I’ll be able to pass sentence as input to the onnx tokenizer and get encodings as output, so that I’ll be able to use the model platform-independent

the tokenizer is independent of onnx / onnxruntime, so you could create a simple function that converts your string inputs into the numpy format that the onnxruntime session expects:

```auto
tokenizer = ...

def prepare_for_session(input: str):
    tokens = tokenizer(input, return_tensors="pt") 
    return {k: v.cpu().detach().numpy() for k, v in tokens.items()}

```

does this answer your question?

---

<div class="post-metadata">

**Author:** ![altozachmo](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/altozachmo/32/12401_2.png) [@altozachmo](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Faltozachmo)%5C
**Post date:** [November 4, 2022, 1:26am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-bert-tokenizer-to-onnx%2F8299%2F3 "2022-11-04T01:26:15Z")

</div>

I think using a separate tokenizer function does work, but bundling the tokenizer into the onnx object has it’s own benefits. For example, if someone wanted to take this model and deploy it client-side in the browser using ONNX.js.

There is currently preliminary support for Node bindings for the Tokenizers library, but that doesn’t support the latest LTS versions of node and won’t without a big overhaul of it’s “neon” dependency.

---

<div class="post-metadata">

**Author:** ![fxmarty](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/fxmarty/32/23782_2.png) [@fxmarty](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ffxmarty)%5C
**Post date:** [November 4, 2022, 9:24am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-bert-tokenizer-to-onnx%2F8299%2F4 "2022-11-04T09:24:48Z")

</div>

Interesting suggestion @aitordiaz . The node bindings you are mentioning are [tokenizers/README.md at main · huggingface/tokenizers · GitHub](https://github.com/huggingface/tokenizers/blob/main/bindings/node/README.md) ?

I agree bundling a tokenizer in a format portable-friendly could be great not only for client-side in-browser inference, but other use cases as well (if we don’t want to use python for example). It seems like huge work though.
