[Adafactor] Fix adafactor
Author: patrickvonplaten
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes #11536


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/master/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/master/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR 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.

Models:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->


Comments:
I've tested this fix on the following script: 

```
CUDA_VISIBLE_DEVICES="0" python run_speech_recognition_ctc.py \
        --dataset_name="common_voice" \
        --model_name_or_path="ntu-spml/distilhubert" \
        --dataset_config_name="ab" \
        --output_dir="./dummy" \
        --overwrite_output_dir \
        --num_train_epochs="3" \
        --per_device_train_batch_size="4" \
        --gradient_accumulation_steps="1" \
        --learning_rate="5e-5" \
        --warmup_steps="500" \
        --evaluation_strategy="steps" \
        --text_column_name="sentence" \
        --save_steps="500" \
        --eval_steps="500" \
        --logging_steps="1" \
        --layerdrop="0.0" \
        --save_total_limit="1" \
        --mask_time_prob="0.3" \
        --mask_time_length="10" \
        --mask_feature_prob="0.1" \
        --mask_feature_length="64" \
        --freeze_feature_extractor \
        --chars_to_ignore , ? . ! - \; \: \" “ % ‘ ” � \
        --fp16 \
        --group_by_length \
        --do_train --do_eval \
        --gradient_checkpointing \
        --adafactor
```

and while this does **not** work on master:

```
  File "/home/patrick/python_bin/transformers/trainer.py", line 1377, in train                
    self.scaler.step(self.optimizer)                                                          
  File "/home/patrick/anaconda3/lib/python3.9/site-packages/torch/cuda/amp/grad_scaler.py", line 338, in step
    retval = self._maybe_opt_step(optimizer, optimizer_state, *args, **kwargs)                 
  File "/home/patrick/anaconda3/lib/python3.9/site-packages/torch/cuda/amp/grad_scaler.py", line 285, in _maybe_opt_step
    retval = optimizer.step(*args, **kwargs)
  File "/home/patrick/anaconda3/lib/python3.9/site-packages/torch/optim/lr_scheduler.py", line 65, in wrapper
    return wrapped(*args, **kwargs)
  File "/home/patrick/anaconda3/lib/python3.9/site-packages/torch/optim/optimizer.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/home/patrick/python_bin/transformers/optimization.py", line 577, in step              
    update = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col)                              
  File "/home/patrick/python_bin/transformers/optimization.py", line 508, in _approx_sq_grad   
    return torch.mm(r_factor.unsqueeze(-1), c_factor.unsqueeze(0))                             
RuntimeError: self must be a matrix

```

 it does work with this fix as proposed by https://github.com/huggingface/transformers/issues/11536#issuecomment-958971693

In general, I think we should not have this optimizer in `transformers` at all (like @sgugger I think), but for now it's better to have something working than something broken IMO.