slashmili

slashmili

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?

First 2 of 2 Posts Switch mode

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()
slashmili

slashmili OP

It works now! Thanks a lot for your help.

Now I’m back on track with the course :v:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement