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?

Showing Posts 1 to 2

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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews