UnboundLocalError when running run_glue.py
Author: KeremZaman
Pull request: no
Labels: name: bug

### System Info

```shell
- `transformers` version: 4.20.0.dev0
- Platform: Linux-5.4.0-113-generic-x86_64-with-glibc2.31
- Python version: 3.9.12
- Huggingface_hub version: 0.5.1
- PyTorch version (GPU?): 1.11.0 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: yes
- Using distributed or parallel set-up in script?: yes
```


### Who can help?

@LysandreJik @sgugger @patil-suraj

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

Steps to reproduce the behavior:

1. Run `run_glue.py` for `snli` dataset:

```bash
python run_glue.py \
  --model_name_or_path bert-base-uncased \
  --dataset_name snli  \
  --do_train \
  --do_eval \
  --max_seq_length 128 \
  --per_device_train_batch_size 128 \
  --learning_rate 2e-5 \
  --num_train_epochs 3 \
  --output_dir ./bert_base_uncased_snli_53 \
  --save_steps 1500 \
  --seed 53
```

3. The error code is raised:

```
  0%|                                                                                               | 0/6450 [00:00<?, ?it/s]Traceback (most recent call last):
  File "/home/myuser/transformers/examples/pytorch/text-classification/run_glue.py", line 617, in <module>
    main()
  File "/home/creativegan/kerem/download/download/finetuning/transformers/examples/pytorch/text-classification/run_glue.py", line 529, in main
    train_result = trainer.train(resume_from_checkpoint=checkpoint)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/trainer.py", line 1367, in train
    return inner_training_loop(
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/trainer.py", line 1609, in _inner_training_loop
    tr_loss_step = self.training_step(model, inputs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/trainer.py", line 2300, in training_step
    loss = self.compute_loss(model, inputs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/trainer.py", line 2332, in compute_loss
    outputs = model(**inputs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/parallel/data_parallel.py", line 168, in forward
    outputs = self.parallel_apply(replicas, inputs, kwargs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/parallel/data_parallel.py", line 178, in parallel_apply
    return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/parallel/parallel_apply.py", line 86, in parallel_apply
    output.reraise()
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/_utils.py", line 457, in reraise
    raise exception
UnboundLocalError: Caught UnboundLocalError in replica 0 on device 0.
Original Traceback (most recent call last):
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/parallel/parallel_apply.py", line 61, in _worker
    output = module(*input, **kwargs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/models/bert/modeling_bert.py", line 1556, in forward
    outputs = self.bert(
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/models/bert/modeling_bert.py", line 991, in forward
    extended_attention_mask: torch.Tensor = self.get_extended_attention_mask(attention_mask, input_shape)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/modeling_utils.py", line 836, in get_extended_attention_mask
    extended_attention_mask = extended_attention_mask.to(dtype=self.dtype)  # fp16 compatibility
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/modeling_utils.py", line 729, in dtype
    return get_parameter_dtype(self)
  File "/home/myuser/miniconda3/envs/nlp/lib/python3.9/site-packages/transformers/modeling_utils.py", line 163, in get_parameter_dtype
    return t.dtype
UnboundLocalError: local variable 't' referenced before assignment
```

### Expected behavior

```shell
Normally, it should continue training but in `modeling_utils.py`, it gives the error for the line after `else` statement.

def get_parameter_dtype(parameter: Union[nn.Module, GenerationMixin, "ModuleUtilsMixin"]):
    """
    Returns the first found floating dtype in parameters if there is one, otherwise returns the last dtype it found.
    """
    try:
        for t in parameter.parameters():
            if t.is_floating_point():
                return t.dtype
        # if no floating dtype was found return whatever the first dtype is
        else:
            return t.dtype
```
```


Comments:
I can reproduce, will try to fix this morning!