sehHeiden

sehHeiden

Create ONNX files. Howto use imported model?

Hi,

wanted to do a sentiment analysis using Elixir. Problem: Bumblebee has that only for English text.

Therefore, tried to load a [model] (oliverguhr/german-sentiment-bert · Hugging Face) from python and Pycharm and chatpgt tried to export the tokenizer and model with:


# Initialize the model
model = germansentiment.SentimentModel()

# Dummy input that matches the input dimensions of the model
dummy_input = torch.randint(0, 30_000, (1, 512), dtype=torch.long)

# Export to ONNX
torch.onnx.export(model.model, dummy_input, "german_sentiment_model.onnx")

# Export the vocab
with open('vocab.json', 'w') as f:
    json.dump(model.tokenizer.vocab, f)

With this I was able to export the model and the vocabulary. Now I try, infer in Elixir using Nx and Axon_onnx:

{model, params} = AxonOnnx.import("./models/models/german_sentiment_model.onnx")

{:ok, vocab_string} = File.read("./models/models/vocab.json")
{:ok, vocab_map} = Jason.decode(vocab_string)

# Tokenize
input_text = "Ein schlechter Film"
token_list = Enum.map(String.split(input_text, " "), fn x -> vocab_map[x] end)
token_tensor = Nx.tensor(List.duplicate(0, 512 - length(token_list)))
token_tensor = Nx.concatenate([Nx.tensor(token_list), token_tensor])

{init_fn, predict_fn} = Axon.build(model)

predict_fn.(params, token_tensor)

The output is:

#Nx.Tensor<
  f32[1][3]
  EXLA.Backend<host:0, 0.1233469648.4027973659.33323>
  [
    [-1.17998206615448, 5.767077922821045, -5.835022926330566]
  ]
>

I assume from the weblink, that the zeroth argument is positive, the first is negative and the last is neutral. Just the scale is off. I assumend a sum of one.

Because this is export and use in elixir is a lot of first times for. I would start, with asking:

  1. Did I do it right?
  2. Would I do a cross entropy on the output?
  3. Anything I could enhance?

Marked As Solved

jonatanklosko

jonatanklosko

Creator of Livebook

Hey @sehHeiden, here’s a complete example in Bumblebee:

# German sentiment analysis

```elixir
Mix.install(
  [
    {:bumblebee, "~> 0.3.1"},
    {:exla, "~> 0.6.0"}
  ],
  config: [nx: [default_backend: EXLA.Backend]]
)
```

## Prediction

```elixir
{:ok, model_info} = Bumblebee.load_model({:hf, "oliverguhr/german-sentiment-bert"})
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "bert-base-german-cased"})

serving =
  Bumblebee.Text.text_classification(model_info, tokenizer, defn_options: [compiler: EXLA])
```

```elixir
texts = [
  "Mit keinem guten Ergebniss",
  "Das ist gar nicht mal so gut",
  "Total awesome!",
  "nicht so schlecht wie erwartet",
  "Der Test verlief positiv.",
  "Sie fährt ein grünes Auto."
]

Nx.Serving.run(serving, texts)
```

Tokenizer details

There are two ways in which tokenizers can be stored on HF Hub. It’s either (1) tokenizer_config.json + vocab.txt + optional merges.txt (this is a dump of a “slow” tokenizer from hf/transformers), or (2) a single tokenizer.json file (this is a dump of a “fast” Rust tokenizer from hf/transformers). Oftentimes the repository includes both versions. In Python, hf/transformers have a logic to load (1) and convert to a fast tokenizer, but we always rely on tokenizer.json, which we hand to the underlying Rust library. When a repository doesn’t have tokenizer.json, it is usually possible to find another base repository with the same tokenizer, which does have that file. In this case I looked at their training code (ref), they fine-tune bert-base-german-cased, so they use the same tokenizer and we can load it from there just fine.

Also Liked

josevalim

josevalim

Creator of Elixir

Take a look at defn (numerical definitions). Those a functions where you can write numerical code that works with tensors using the regular Elixir operators. You would get something like (untested):

import Nx.Defn

defn political_score(predict_fn, params, token_tensor) do
  prediction = predict_fn.(params, token_tensor)
  one_hot = 2 ** prediction / Nx.sum(2 ** prediction)
  5 * (one_hot[0][0] - one_hot[0][2])
end
jonatanklosko

jonatanklosko

Creator of Livebook

As I ave the the vocab.json, is the the same as the tokenizer.json?

tokenizer.json is a single file with all information, other than vocabulary, it includes special tokens information, tokenizer model, pre/post processing, etc. See tokenizer.json for an example.

Which Rust library is used in Elixir to load it?

There is huggingface/tokenizers in Rust and it also has Python bindings. huggingface/transformers have two types of tokenizers, slow - implemented purely in Python and fast - calling out to the Rust library. We have elixir-nx/tokenizers with bindings to the Rust library.

Can you assume, why the onnx model has another output scale than the original/bumblebee version?

I may be missing something, but I don’t think the calls are equivalent. You are splitting on space and using the vocab. The tokenizer on the other hand does more, it will split longer words into parts and add special tokens.

Last Post!

jonatanklosko

jonatanklosko

Creator of Livebook

As I ave the the vocab.json, is the the same as the tokenizer.json?

tokenizer.json is a single file with all information, other than vocabulary, it includes special tokens information, tokenizer model, pre/post processing, etc. See tokenizer.json for an example.

Which Rust library is used in Elixir to load it?

There is huggingface/tokenizers in Rust and it also has Python bindings. huggingface/transformers have two types of tokenizers, slow - implemented purely in Python and fast - calling out to the Rust library. We have elixir-nx/tokenizers with bindings to the Rust library.

Can you assume, why the onnx model has another output scale than the original/bumblebee version?

I may be missing something, but I don’t think the calls are equivalent. You are splitting on space and using the vocab. The tokenizer on the other hand does more, it will split longer words into parts and add special tokens.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement