slashmili

slashmili

Configuration for Text Generation in Bumblebee

Howdy!

I’m following along Generative AI with Large Language Models and trying to implement the hands-on assignment using Nx.

However I’m not getting results with same quality to the python implementations, I understand that the answers could be different but the text generation I get is odd and deranged :smiley:

This is the python code:

from transformers import AutoModelForSeq2SeqLM
from transformers import AutoTokenizer
model_name = 'google/flan-t5-base'
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)

dialogue="#Person1#: What time is it, Tom?\n#Person2#: Just a minute. It's ten to nine by my watch.\n#Person1#: Is it? I had no idea it was so late. I must be off now.\n#Person2#: What's the hurry?\n#Person1#: I must catch the nine-thirty train.\n#Person2#: You've plenty of time yet. The railway station is very close. It won't take more than twenty minutes to get there."
inputs = tokenizer(dialogue, return_tensors='pt')
output = tokenizer.decode(
    model.generate(inputs["input_ids"],max_new_tokens=50)[0],
    skip_speical_tokens=True
)

print(f'MODEL GENERATION :\n{output}')

which prints:

MODEL GENERATION :
Person1: It’s ten to nine.

And the Elixir code is:

{:ok, model} = Bumblebee.load_model({:hf, "google/flan-t5-base"})
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "google/flan-t5-base"})
dialogue = """
#Person1#: What time is it, Tom?
#Person2#: Just a minute. It's ten to nine by my watch.
#Person1#: Is it? I had no idea it was so late. I must be off now.
#Person2#: What's the hurry?
#Person1#: I must catch the nine-thirty train.
#Person2#: You've plenty of time yet. The railway station is very close. It won't take more than twenty minutes to get there
"""
config =
  config = %Bumblebee.Text.GenerationConfig{
    max_new_tokens: 50,
    forced_token_ids: [],
    no_repeat_ngram_length: 3,
    bos_token_id: 0, # This is mandatory, I copied from a smart cell!
    pad_token_id: 1 # This is mandatory, I copied from a smart cell!
  }
serving = Bumblebee.Text.generation(model,tokenizer, config)
Nx.Serving.run(serving, dialogue) |> IO.inspect

Which I get this back:

%{
  results: [
    %{
      text: "Person1#: It's ten to nine. It'll be ten minutes before the train leaves.nt is ten.nd is nine.m is nine o'clock.f is nine thirty"
    }
  ]
}

Consistently in other dialogues in the dataset I get extra gibberish sentences. e.g on another input

python code output:

Person1: I’m worried about my future.

Elixir code output:

#Person1#: I’m worried about my future.tatta i apologise for my paleness.temo i’m very young.mo a

What kind of configuration here is needed to get the same result?

Marked As Solved

jonatanklosko

jonatanklosko

Creator of Livebook

Hey @slashmili :slight_smile: There are two differences between these versions:

  1. You are missing eos_token_id: 1 in the generation config. This is exactly the token that the model uses to indicate end of generation, so it makes sense that without it, you get extra continuation.
  2. The Python dialogue has a trailing dot, on the other hand the Elixir dialogue has a trailing newline. This isn’t particularly significant, but changes the output slightly.

Regarding 1), note that in general it’s best to load the predefined config from the model repository and then only override specific options (like :max_new_tokens), otherwise you need to know what token ids to set.

Here’s a version that matches the Python output (note the trailing backslash in the heredoc that prevents the final newline):

{:ok, model} = Bumblebee.load_model({:hf, "google/flan-t5-base"})
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "google/flan-t5-base"})
{:ok, config} = Bumblebee.load_generation_config({:hf, "google/flan-t5-base"})
config = Bumblebee.configure(config, max_new_tokens: 50)

dialogue = """
#Person1#: What time is it, Tom?
#Person2#: Just a minute. It's ten to nine by my watch.
#Person1#: Is it? I had no idea it was so late. I must be off now.
#Person2#: What's the hurry?
#Person1#: I must catch the nine-thirty train.
#Person2#: You've plenty of time yet. The railway station is very close. It won't take more than twenty minutes to get there.\
"""

serving = Bumblebee.Text.generation(model, tokenizer, config)
Nx.Serving.run(serving, dialogue) |> IO.inspect()

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement