question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

UnboundLocalError: local variable 'next_tokens' referenced before assignment when using Generate()

See original GitHub issue

šŸ› Bug

I have pre-trained GPT2 on a summarisation dataset such that summarisation is a language modelling task i.e. input = concat( padded_to_max_len(body , ā€œTL;DR:ā€ , summary)).

For some reason, this error occurs when I try to generate via beam search using GPT2ā€™s with language modelling head. Here is my code:

from transformers import AutoTokenizer, GPT2LMHeadModel
import torch

# define tokenizer
tokenizer_kwargs = {"bos_token": "<|startoftext|>", "eos_token": "<|endoftext|>", "pad_token": "<|pad|>"}
tokenizer = AutoTokenizer.from_pretrained("gpt2", **tokenizer_kwargs)

# define model
model = GPT2LMHeadModel.from_pretrained("gpt2", pad_token_id=tokenizer.eos_token_id)

# define input 
input_ids = torch.tensor(tokenizer.encode("some text that ends in TL;DR:")).unsqueeze(0)

# attempt to generate
y_pred_tensor = model.generate(input_ids=input_ids,
                                                num_beams=5,
                                                early_stopping=True,
                                                no_repeat_ngram_size=2,
                                                max_length=100
                                                )
File "/Users/christopherdoyle/cp_projects/scribbl-ai/Scribbl/Scribbl/summarizer/models.py", line 147, in summarize
    y_pred_tensor = self.model.generate(input_ids=input_tensor,
  File "/Users/christopherdoyle/cp_projects/scribbl-ai/Scribbl/venv/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 15, in decorate_context
    return func(*args, **kwargs)
  File "/Users/christopherdoyle/cp_projects/scribbl-ai/Scribbl/venv/lib/python3.8/site-packages/transformers/modeling_utils.py", line 1100, in generate
    output = self._generate_beam_search(
  File "/Users/christopherdoyle/cp_projects/scribbl-ai/Scribbl/venv/lib/python3.8/site-packages/transformers/modeling_utils.py", line 1499, in _generate_beam_search
    (token_id % vocab_size).item() is not eos_token_id for token_id in next_tokens[batch_idx]
UnboundLocalError: local variable 'next_tokens' referenced before assignment

Model I am using: GPT2LMHeadModel

Language I am using the model on (English, Chinese ā€¦): en

Rather strangely, it works when max_length = 1024, but not with smaller values.

To reproduce

Running on CPU python==3.8, transformers==2.11.0, torch==1.5.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
patrickvonplatencommented, Jul 3, 2020

The problem is that max_length is not bigger than cur_len so that model will not produce any text. This will fix the problem:

outputs = model.generate(input_ids=input_ids, num_beams=3, max_length=75)
1reaction
chrisdoyleIEcommented, Jun 19, 2020

Hey @patrickvonplaten ,

Iā€™ll do some digging and see if I canā€™t reproduce it myself such that itā€™s easily paste-able and then I can share this code (I currently have a few custom packages calling eachother which is hairier than iā€™d like and not trivial to insert into an issue).

Read more comments on GitHub >

github_iconTop Results From Across the Web

how can i fix: UnboundLocalError: local variable 'generate ...
In generate() you create also local variable when you use ... and this gives error local variable 'caratteri' referenced before assignment .
Read more >
Local Variable Referenced Before Assignment - STechies
The ā€œlocal variable referenced before assignmentā€ error occurs when you give reference of a local variable without assigning any value. Example:
Read more >
Python local variable referenced before assignment Solution
This error is raised when you try to use a variable before it has been assigned in the local context.
Read more >
local variable 'x' referenced before assignment - Python ...
This video covers the UnboundLocalError in python. ... UnboundLocalError : local variable 'x' referenced before assignment - Python Debugging.
Read more >
Solving Python Error - UnboundLocalError: local variable 'x ...
UnboundLocalError : local variable 'x' referenced before assignment, ... printx() 5 >>>. Now you added an assignment statement inside printx function whereĀ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found