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:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem is that
max_length
is not bigger thancur_len
so that model will not produce any text. This will fix the problem: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).