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.

Big Bird generate() "local variable 'next_tokens' referenced before assignment"

See original GitHub issue

I am facing this problem when doing text summarization. I am using google/bigbird-roberta-base and I get the following error when calling model.generate(input, max_length = 4096, num_beams=4, early_stopping=True, length_penalty = 0.8):

Input length of input_ids is 4096, but ``max_length`` is set to 4096.This can lead to unexpected behavior. You should consider increasing ``config.max_length`` or ``max_length``.
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-13-90a633800ba7> in <module>()
----> 1 get_ipython().run_cell_magic('time', '', ' \ni = 0\nsize = 1\nout = []\nend = False\nprint_iters = 100\nsave_iters = 5\n \nwhile True:\n  if (i+size) >= n:\n    last = n\n    end = True\n  else:\n    last = i + size   \n \n  result = make_gen(   model_sum, tokens[i:last, :].detach().clone()  )\n \n  for j in range(result.shape[0]):\n    out.append(result[j])\n \n  if last % (print_iters*size) == 0:\n    print(last)\n    gc.collect()\n    torch.cuda.empty_cache()\n    torch.cuda.synchronize()\n    if last % (print_iters*size*save_iters) == 0:\n      with open(path_output + name + ".pkl", \'wb\') as f:\n        pickle.dump(out, f)\n      print("Saved to disk")\n \n  if end:\n    break\n  i = last')

6 frames
<decorator-gen-53> in time(self, line, cell, local_ns)

<timed exec> in <module>()

/usr/local/lib/python3.7/dist-packages/transformers/generation_utils.py in beam_search(self, input_ids, beam_scorer, logits_processor, stopping_criteria, max_length, pad_token_id, eos_token_id, output_attentions, output_hidden_states, output_scores, return_dict_in_generate, **model_kwargs)
   1808 
   1809         sequence_outputs = beam_scorer.finalize(
-> 1810             input_ids, beam_scores, next_tokens, next_indices, pad_token_id=pad_token_id, eos_token_id=eos_token_id
   1811         )
   1812 

UnboundLocalError: local variable 'next_tokens' referenced before assignment

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
OscarGarciaFcommented, Apr 17, 2021

@vasudevgupta7 I was using AutoModelForSeq2SeqLM (this is what you use for summarization right?)

I have now changed to EncoderDecoderModel but now I face a new error

      1 input = tokens[0:1, :].to(device)
----> 2 generated = model_sum.generate(input, decoder_start_token_id = model_sum.config.decoder.pad_token_id, max_length = 512, num_beams = 4, early_stopping = True)

10 frames
/usr/local/lib/python3.7/dist-packages/transformers/models/big_bird/modeling_big_bird.py in forward(self, input_ids, token_type_ids, position_ids, inputs_embeds, past_key_values_length)
    293 
    294         position_embeddings = self.position_embeddings(position_ids)
--> 295         embeddings += position_embeddings
    296 
    297         embeddings = self.dropout(embeddings)

RuntimeError: output with shape [4, 1, 768] doesn't match the broadcast shape [4, 0, 768]
0reactions
ilos-vigilcommented, Nov 15, 2022

I also got exact same error (output with shape...) when i generate on custom BigBird model. I fixed it by reducing model_max_length value from 4096 to 4094 and afterwards i can use pipeline for inference without any problem.

>>> tokenizer.max_len_single_sentence
4094
>>> tokenizer.model_max_length
4096
>>> tokenizer.model_max_length = 4094
Read more comments on GitHub >

github_iconTop Results From Across the Web

local variable 'next_tokens' referenced before assignment ...
For some reason, this error occurs when I try to generate via beam search using GPT2's with language modelling head. Here is my...
Read more >
how can i fix: UnboundLocalError: local variable 'generate ...
but you do this after trying to get value from caratteri - and this gives error local variable 'caratteri' referenced before assignment ....
Read more >
Solving Python Error - UnboundLocalError: local variable 'x ...
Lets understand few things first. In python all the variables inside a function are global if they are not assigned any value to...
Read more >
CSC519 Programming Languages - Computer Science
A grammar for a language describes how to generate words from it. ... The parameters are treated as local variables with the values...
Read more >
Learn to Program In GSoft BASIC - The Byte Works
Before starting, let's make sure you have everything you will need. First, you need an Apple IIGS computer. (An emulator is fine, as...
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