Index out of range in self with fine-tuned DPR Context Encoder
Author: benscottie
Pull request: no

## Environment info

- `transformers` version: 4.5.1
- Platform: Linux-5.4.0-72-generic-x86_64-with-glibc2.27
- Python version: 3.8.6
- PyTorch version (GPU?): 1.6.0 (True)
- Tensorflow version (GPU?): 2.3.1 (True)
- Using GPU in script?: Have tried with and without
- Using distributed or parallel set-up in script?: No

### Who can help
@LysandreJik 

## Information
Model I am using: `DPR Question and Context Encoders`
Getting Index out of range in self error for embeddings when trying to apply a locally fine-tuned version for each of the DPR encoders. Previous issues point to a difference in `vocab lengths` or tokens but nothing has been changed there. The `model max length` also is consistent at 512.

Code:
```
from transformers import DPRContextEncoderTokenizerFast, DPRContextEncoder
ctx_tok = DPRContextEncoderTokenizerFast.from_pretrained('/data/riddler/checkpoints/adapted_dpr/ctx-encoder-2021-04-19-checkpoint-11000')
model = DPRContextEncoder.from_pretrained('/data/riddler/checkpoints/adapted_dpr/ctx-encoder-2021-04-19-checkpoint-11000')
input_ids = ctx_tok("Hello, is my dog cute ?", return_tensors='pt')["input_ids"]
embeddings = model(input_ids).pooler_output
```
Error:
```
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-91-24fb6846809e> in <module>
----> 1 embeddings = model(input_ids['input_ids'])

/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/venv/lib/python3.8/site-packages/transformers/models/dpr/modeling_dpr.py in forward(self, input_ids, attention_mask, token_type_ids, inputs_embeds, output_attentions, output_hidden_states, return_dict)
    573             return_dict=return_dict,
    574         )
--> 575 
    576         if not return_dict:
    577             return outputs[1:]

/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/venv/lib/python3.8/site-packages/transformers/models/dpr/modeling_dpr.py in forward(self, input_ids, attention_mask, token_type_ids, inputs_embeds, output_attentions, output_hidden_states, return_dict)
    170         return_dict: bool = False,
    171     ) -> Union[BaseModelOutputWithPooling, Tuple[Tensor, ...]]:
--> 172         outputs = self.bert_model(
    173             input_ids=input_ids,
    174             attention_mask=attention_mask,

/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/venv/lib/python3.8/site-packages/transformers/models/bert/modeling_bert.py in forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, past_key_values, use_cache, output_attentions, output_hidden_states, return_dict)
    962             encoder_hidden_states=encoder_hidden_states,
    963             encoder_attention_mask=encoder_extended_attention_mask,
--> 964             past_key_values=past_key_values,
    965             use_cache=use_cache,
    966             output_attentions=output_attentions,

/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/venv/lib/python3.8/site-packages/transformers/models/bert/modeling_bert.py in forward(self, input_ids, token_type_ids, position_ids, inputs_embeds, past_key_values_length)
    204         if self.position_embedding_type == "absolute":
    205             position_embeddings = self.position_embeddings(position_ids)
--> 206             embeddings += position_embeddings
    207         embeddings = self.LayerNorm(embeddings)
    208         embeddings = self.dropout(embeddings)

/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/venv/lib/python3.8/site-packages/torch/nn/modules/sparse.py in forward(self, input)
    122 
    123     def forward(self, input: Tensor) -> Tensor:
--> 124         return F.embedding(
    125             input, self.weight, self.padding_idx, self.max_norm,
    126             self.norm_type, self.scale_grad_by_freq, self.sparse)

/venv/lib/python3.8/site-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
   1812         # remove once script supports set_grad_enabled
   1813         _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 1814     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
   1815 
   1816 

IndexError: index out of range in self
```


Comments:
@LysandreJik do you have insight on what could be causing this error? Thanks!

Hi! I'm trying to reproduce but as I don't have your checkpoint this is proving complicated. Could you provide a reproducible example/colab so I can take a look?

Also, it seems you've shared some of the stack trace but not the entire stack trace. It would be helpful to see the full error to see where the issue originates from.

@LysandreJik thanks for your response! I just updated the error to include the entire stack trace. Hope that is helpful.

I'm not sure how to create a reproducible example since the error is based on the fine-tuned checkpoint. I can send what the config file looks like for that model if that is helpful

Here is my code for creating the BiEncoder model object from the separate context and question encoders in order to fine-tune DPR as well as the code for saving the checkpoints. Maybe that will help. `self.model` refers to the biencoder.
```
# DPR BiEncoder Model Class
class BiEncoder(torch.nn.Module):

    def __init__(self, query_model, ctx_model):
        super(BiEncoder, self).__init__()
        self.query_model = query_model
        self.ctx_model = ctx_model

    def forward(self, query_ids, query_attn_mask, ctx_ids, ctx_attn_mask):
        #query_embed = self.question_model(query_ids).pooler_output
        query_embed = self.query_model(query_ids, attention_mask=query_attn_mask)[0]
        #ctx_embed = self.ctx_model(ctx_ids).pooler_output
        ctx_embed = self.ctx_model(ctx_ids, attention_mask=ctx_attn_mask)[0]
        
        return query_embed, ctx_embed

# Load Model
def get_model(query_encoder_path, ctx_encoder_path):

  # Question Encoder
  query_encoder = DPRQuestionEncoder.from_pretrained(query_encoder_path)

  # Context Encoder
  ctx_encoder = DPRContextEncoder.from_pretrained(ctx_encoder_path)

  # Initialize Dual Encoder
  biencoder = BiEncoder(query_encoder, ctx_encoder)

  return biencoder

# Get Optimizer
def get_optimizer(self):
  optimizer_grouped_parameters = [{'params': [p for n,p in self.model.named_parameters()],
                                   'params': [p for n,p in self.model.named_parameters()]}]
  return AdamW(optimizer_grouped_parameters, lr=self.lr)

# Save Checkpoint
query_encoder = self.model.query_model
ctx_encoder = self.model.ctx_model
query_model_path = os.path.join(self.cp_subdir, f'query-encoder-{self.exp_date}-checkpoint-{self.global_step}')
ctx_model_path = os.path.join(self.cp_subdir, f'ctx-encoder-{self.exp_date}-checkpoint-{self.global_step}')
query_encoder.save_pretrained(query_model_path) #save encoders
ctx_encoder.save_pretrained(ctx_model_path)
```

Is there a way for you to upload your checkpoints on the hub so that I can take a look and try to reproduce locally? I'm curious to see if the configuration has a too small `max_position_embeddings` leading to the overflow.

The `max_position_embeddings` in the config file are 512, which matches the DPR default. The vocab size also matches the default.

I encountered the same issue in Bertweet:
https://colab.research.google.com/drive/1cEtC98hIfB-2I-Tcxsp_OEau4rNmjXJw?usp=sharing

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.