Repository: huggingface/transformers

Issue:
AlbertTransformer head_mask not subscriptable error when not passed
## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 3.4.0
- Platform: Linux version 4.4.0-193-generic / ubuntu
- Python version: 3.6.5
- PyTorch version (GPU?): 1.4.0 (cuda 10.0)
- Tensorflow version (GPU?): not installed (NA)
- Using GPU in script?: no
- Using distributed or parallel set-up in script?: no

### Who can help
<!-- Your issue will be replied to more quickly if you can figure out the right person to tag with @
 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.\
-->
@LysandreJik 

## Information
AlbertTransformer has `head_mask` arguments with default value of None. [(modeling_albert.py#L416)](https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_albert.py#L416)
But in forward, `head_mask` is indexed by group_idx without checking if head_mask is None. [(modeling_albert.py#L436)](https://github.com/huggingface/transformers/blob/master/src/transformers/modeling_albert.py#L436)
So I got the error as below.
(it seems like a similar issue [#1188](https://github.com/huggingface/transformers/issues/1188))

The problem arises when using:
* [-] the official example scripts: (give details below)
* [O] my own modified scripts: (give details below)

The tasks I am working on is:
* [-] an official GLUE/SQUaD task: (give the name)
* [O] my own task or dataset: (give details below)

## To reproduce
```python
from transformers import AlbertTokenizer, AlbertModel

tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')
model = AlbertModel.from_pretrained('albert-base-v2', return_dict=True)

inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
inputs2 = model.embeddings(input_ids=inputs.input_ids,
                 token_type_ids=inputs.token_type_ids,
                 position_ids=None,
                 inputs_embeds=None)
model.encoder(inputs2)
```
error message when i execute the above script.
```python
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-4fc3cd45df51> in <module>()
      9                  position_ids=None,
     10                  inputs_embeds=None)
---> 11 model.encoder(inputs2)

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/transformers/modeling_albert.py in forward(self, hidden_states, attention_mask, head_mask, output_attentions, output_hidden_states, return_dict)
    434                 hidden_states,
    435                 attention_mask,
--> 436                 head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group],
    437                 output_attentions,
    438                 output_hidden_states,

TypeError: 'NoneType' object is not subscriptable
```

## Expected behavior
`model.encoder(inputs2)` encoder will be output of tensor.
Espeically, shape of the tensor will be (1, 8, 768)

Personally, I think we could add one more line of code before `for loop`
```python
head_mask = [None] * self.config.num_hidden_layers if head_mask is None else head_mask
```




Discussion:
@LysandreJik Could I put this issue on PR?
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.
I am facing the same issue while using `transformers 4.5.1`. Is there any workaround for this issue ?
Hi, @harikc456 
You can try to modify the source code. https://github.com/huggingface/transformers/blob/master/src/transformers/models/albert/modeling_albert.py#L463
There are no indices in None object (`head_mask` ). 
So you can try like the followings:
```python
head_mask = [None] * self.config.num_hidden_layers if head_mask is None else head_mask
```
@baeseongsu, sorry to have missed this. If you still feel like opening a PR, we would welcome it! It seems many models have this issue, as using the encoder call separately isn't something that tends to happen often.
Hello, @LysandreJik. Thank you for checking 😄 
Exactly, right. In most of the cases, we don't need to call the internal classes.
Anyway, I will try asap !!

Resolving patch:
diff --git a/src/transformers/models/albert/modeling_albert.py b/src/transformers/models/albert/modeling_albert.py
--- a/src/transformers/models/albert/modeling_albert.py
+++ b/src/transformers/models/albert/modeling_albert.py
@@ -450,6 +450,8 @@ def forward(
         all_hidden_states = (hidden_states,) if output_hidden_states else None
         all_attentions = () if output_attentions else None
 
+        head_mask = [None] * self.config.num_hidden_layers if head_mask is None else head_mask
+
         for i in range(self.config.num_hidden_layers):
             # Number of layers in a hidden group
             layers_per_group = int(self.config.num_hidden_layers / self.config.num_hidden_groups)


Test patch: