# BlenderBot forward method crashing

**URL:** <https://discuss.huggingface.co/t/blenderbot-forward-method-crashing/18410>\
**Category:** Models\
**Created:** [May 26, 2022, 8:14pm UTC](https://discuss.huggingface.co/t/blenderbot-forward-method-crashing/18410 "2022-05-26T20:14:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![0xrushi](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/0xrushi/32/43372_2.png) [@0xrushi](https://discuss.huggingface.co/u/0xrushi)\
**Post date:** [May 26, 2022, 8:14pm UTC](https://discuss.huggingface.co/t/blenderbot-forward-method-crashing/18410/1 "2022-05-26T20:14:29Z")

</div>

I am trying to use [BlenderbotForConditionalGeneration](https://huggingface.co/docs/transformers/v4.19.2/en/model_doc/blenderbot#transformers.BlenderbotForConditionalGeneration), and I’m getting the following error

```auto
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-51-b8db07c0c647> in <module>()
      2 input_ids = encoding['input_ids'],
      3 attention_mask = encoding['attention_mask'],
----> 4 labels=labels)

9 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
   2181 # remove once script supports set_grad_enabled
   2182 _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 2183 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
   2184 
   2185 

IndexError: index out of range in self

```

**Actual Code**

```auto

MODEL_NAME = "facebook/blenderbot-400M-distill"
tokenizer = BlenderbotTokenizer.from_pretrained(MODEL_NAME)

encoding = tokenizer(
    sample_question['question'],
    sample_question['context'],
    max_length=1024,
    padding='max_length',
    truncation="only_second",
    return_attention_mask=True,
    add_special_tokens=True,
    return_tensors="pt"
)

answer_encoding = tokenizer(
    sample_question['answer_text'],
    max_length=1024,
    padding='max_length',
    truncation=True,
    return_attention_mask=True,
    add_special_tokens=True,
    return_tensors="pt"
)
labels = answer_encoding["input_ids"]

model = BlenderbotForConditionalGeneration.from_pretrained(MODEL_NAME, return_dict = True)

output = model(
    input_ids = encoding['input_ids'],
    attention_mask = encoding['attention_mask'],
    labels = labels
)

```

---

<div class="post-metadata">

**Author:** ![thies](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@thies](https://discuss.huggingface.co/u/thies)\
**Post date:** [May 27, 2022, 12:50pm UTC](https://discuss.huggingface.co/t/blenderbot-forward-method-crashing/18410/2 "2022-05-27T12:50:16Z")

</div>

As it crashes during the embedding lookup, did you check that the vocabulary file is the correct one?

---

<div class="post-metadata">

**Author:** ![0xrushi](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/0xrushi/32/43372_2.png) [@0xrushi](https://discuss.huggingface.co/u/0xrushi)\
**Post date:** [May 27, 2022, 2:27pm UTC](https://discuss.huggingface.co/t/blenderbot-forward-method-crashing/18410/3 "2022-05-27T14:27:51Z")

</div>

Hi @thies!,

Thanks for checking. I got it resolved over the discord, The issue was in the tokenizer max\_length was wrong. The best way to find this would be checking `model.config.max_position_embeddings`.

```auto
tokenizer(
      data_row['question'],
      data_row['context'],
      max_length=128
      )

```

---

<div class="post-metadata">

**Author:** ![0xrushi](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/0xrushi/32/43372_2.png) [@0xrushi](https://discuss.huggingface.co/u/0xrushi)\
**Post date:** [May 27, 2022, 2:41pm UTC](https://discuss.huggingface.co/t/blenderbot-forward-method-crashing/18410/4 "2022-05-27T14:41:02Z")

</div>

If anyone is interested in the notebook link: [here](https://github.com/rushic24/deep-learning-notebooks/blob/main/temp-notebooks/BlenderBotQuestionAnswering.ipynb).  
I hardcoded max\_len to 128, by looking at the model.config.
