problem about geting hidden_states using TFBertModel
Author: jianrui2025
Pull request: no

## 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.0.2
- Platform: Pycharm
- Python version: 3.6
- PyTorch version (GPU?): None
- Tensorflow version (GPU?): 2.2.0 GPU
- 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.

 albert, bert, GPT2, XLM: @LysandreJik 
 tokenizers: @mfuntowicz
 Trainer: @sgugger
 Speed and Memory Benchmarks: @patrickvonplaten
 Model Cards: @julien-c
 Translation: @sshleifer
 Summarization: @sshleifer
 TextGeneration: @TevenLeScao 
 examples/distillation: @VictorSanh
 nlp datasets: [different repo](https://github.com/huggingface/nlp)
 rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)
 Text Generation: @TevenLeScao
 blenderbot: @mariamabarham
 Bart: @sshleifer
 Marian: @sshleifer
 T5: @patrickvonplaten
 Longformer/Reformer: @patrickvonplaten
 TransfoXL/XLNet: @TevenLeScao 
 examples/seq2seq: @sshleifer
 tensorflow: @jplu 
documentation: @sgugger
 -->
 @LysandreJik @jplu 
## Information

Model I am using Bert: I want to the outpts of **TFBertModel** class inclue **hidden_states**.  unfortunately， I use two method provided by transformers' document north can achieve this goal.

The first method:  **output_hidden_states=True** is passed to call(). code as follow:
```
class Model(tf.keras.Model):
    def __init__(self,init):
        super(Model,self).__init__()
        conf = transformers.BertConfig.from_json_file("model/chinese_L-12_H-768_A-12/config.json")
        self.bertmodel = transformers.TFBertModel.from_pretrained("bert-base-chinese")

    @tf.function
    def call(self, inputs, training=None, mask=None):
        out_bert = self.bertmodel(inputs,output_hidden_states=True)
        return out_bert

if __name__ == "__main__":
    tokenizer = transformers.BertTokenizer("model/chinese_L-12_H-768_A-12/vocab.txt")
    text_2 = tokenizer.batch_encode_plus(["你买啊，买了就是成都人", "你来啊，来了就是深圳人"], max_length=20, pad_to_max_length=True)
    print(text_2)
    model = Model()
    out = model([tf.convert_to_tensor(text_2["input_ids"]),tf.convert_to_tensor(text_2['attention_mask'])])
    print("out",out)
```
 the print in consol only two tensor :**last_hidden_state** and **pooler_output**

the second method： set **config.output_hidden_states=True**. code as follow:
config.json:
```
{
  "attention_probs_dropout_prob": 0.1, 
  "directionality": "bidi", 
  "hidden_act": "gelu", 
  "hidden_dropout_prob": 0.1, 
  "hidden_size": 768, 
  "initializer_range": 0.02, 
  "intermediate_size": 3072, 
  "max_position_embeddings": 512, 
  "num_attention_heads": 12, 
  "num_hidden_layers": 12, 
  "pooler_fc_size": 768, 
  "pooler_num_attention_heads": 12, 
  "pooler_num_fc_layers": 3, 
  "pooler_size_per_head": 128, 
  "pooler_type": "first_token_transform", 
  "type_vocab_size": 2, 
  "vocab_size": 21128,
  "output_hidden_states": true
}
```
I set **"output_hidden_states": true** at last line.
code:
```
class Model(tf.keras.Model):
    def __init__(self,init):
        super(Model,self).__init__()
        conf = transformers.BertConfig.from_json_file("model/chinese_L-12_H-768_A-12/config.json")
        self.bertmodel = transformers.TFBertModel.from_pretrained("bert-base-chinese",config=conf)


    @tf.function
    def call(self, inputs, training=None, mask=None):
        out_bert = self.bertmodel(inputs)
        return out_bert

if __name__ == "__main__":
    tokenizer = transformers.BertTokenizer("model/chinese_L-12_H-768_A-12/vocab.txt")
    text_2 = tokenizer.batch_encode_plus(["你买啊，买了就是成都人", "你来啊，来了就是深圳人"], max_length=20, pad_to_max_length=True)
    print(text_2)
    model = Model()
    out = model([tf.convert_to_tensor(text_2["input_ids"]),tf.convert_to_tensor(text_2['attention_mask'])])
    print("out",out)
```
the print in consol is same as method one.

but if I cancel **@tf.function** in front of call(). the print is what I expect.


## To reproduce

Steps to reproduce the behavior:

1.
2.
3.

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->


Comments:
Hello!

I should be solved here https://github.com/huggingface/transformers/pull/5468. Nevertheless, you have to avoid using boolean tensors for `output_hidden_states` and `output_attentions` otherwise it won't work.

Hello!

could you tell me more details about  how to avoid using boolean tensors? or how to achieve this goal in version 3.0.2 ? 
How could I got your fixed code?  or How long time we can see your fixed code in new version? 
thanks for your answers.

Fix has been merged in master!

Should be fixed on `master`

@jianrui1995 Hi, could you tell me how you solved the discrepancy in the behaviour with or without @tf.function, I am facing the same issue, tf version: 2.3.0 and transformers 3.0.2

```
class WrappedModel(tf.Module):
	def __init__(self):
		super(WrappedModel, self).__init__()
		self.model = TFDistilBertModel.from_pretrained('distilbert-base-uncased', output_hidden_states=True)
	# @tf.function
	def __call__(self, x):
		return self.model(x)
```

I didn't solved this problem in version 3.0.2 but I got the bug code in **line 765 modeling_tf_tuils.py** 
code as follow:
```
def cast_bool_to_primitive(bool_variable, default_tensor_to_true=False):
    """Function arguments can be inserted as boolean tensor
        and bool variables to cope with keras serialization
        we need to cast `output_attentions` to correct bool
        if it is a tensor

    Args:
        default_tensor_to_true: bool, if tensor should default to True
        in case tensor has no numpy attribute
    """
    # if bool variable is tensor and has numpy value
    if tf.is_tensor(bool_variable):
        if hasattr(bool_variable, "numpy"):
            return bool(bool_variable.numpy())
        elif default_tensor_to_true:
            return True

    # else variable is bool
    return bool_variable
```
the code witch calling the method in **line 407 in modeling_tf_bert.py** 
```
        if cast_bool_to_primitive(output_hidden_states) is True:
            all_hidden_states = all_hidden_states + (hidden_states,)
```
according to tensorflow2's document, if you add **tf.tfunction**, tf will change the model from enger to graph by Autograph. The method's return True will change to bool tensor. it is different from true.

according the jplu's answer, this problem has been solved in master. you colud get the master version.

@jianrui1995 Thanks for your response, yes I was able to do it with the updated code in master. Was more curious about the version of transformers that has the support for this, since maintainability is an issue.

This part is still a work in progress, what there is in master is just a tiny workaround and doesn't work for several cases. We will push an update when we will have found a proper solution.