refactoring & fixing Pytorch QA examples
Author: searchivarius
Pull request: yes

# What does this PR do?

This PR improves/fixes Pytorch QA examples in several ways:
1. extracted duplicating post-processing functions (now shared between regular and no-trainer version)
2. added/fixed code to save statistics to some no-trainer versions (one was even buggy)
3. fixed squad v2-eval error in utils_qa.py (one was newly encountered and one that was previously reported, it was fixed for regular squad QA model, but not for the model that uses beam search: #15401)
4. added forgotten model.eval() in the "no-trainer" versions. 

<!-- Remove if not applicable -->

Fixes # 15401 (that was only partially fixed)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [X] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/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.
- [ ] **I believe examples aren't covered by the documentation** you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [X] **Examples do not have tests, however, I trained squad and squad v2 models and compare results (see the discussion below)** 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. Perhaps, this can be of interest for  @sgugger, @patil-suraj.

## Comparing old and new performance

Let me for simplicity just dump all the numbers here. Old means old example code, the new means the new one (refactored and with model.eval() added). As you can see in all the cases, the new code produces the same or better scores. **The only weird case is SQUAD v2** in the no-trainer mode.

Some remaining issues:
1. Frankly speaking, both old and new numbers look weird to me. 
2. Despite the fixes & improvements, there's still a discrepancy between no-trainer and original version. 

Also note that to be able to run SQuAD v2, **I had to apply utils_qa.py fixes to the old code as well**. Otherwise, it would have just failed:

```
./old/run_qa_beam_search/1/eval_results.json:    "eval_f1": 92.13525592586892,
./old/run_qa_beam_search_no_trainer/1/eval_results.json:    "f1": 90.18714704272388
./old/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_HasAns_f1": 85.72401519644451,
./old/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_NoAns_f1": 80.7569386038688,
./old/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_best_f1": 84.39435986584854,
./old/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_best_f1_thresh": -15.705571174621582,
./old/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_f1": 83.23692092011478,
./old/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "f1": 4.879995879559425,
./old/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "HasAns_f1": 5.23619957456295,
./old/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "NoAns_f1": 4.524810765349033,
./old/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "best_f1": 50.07346266505704,
./old/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "best_f1_thresh": -18.9681339263916
./old/run_qa/1/eval_results.json:    "eval_f1": 88.3974945885421,
./old/run_qa_notrainer/1/eval_results.json:    "eval_f1": 86.7048555821845,
./new/run_qa_beam_search/1/eval_results.json:    "eval_f1": 92.13525592586892,
./new/run_qa_beam_search_no_trainer/1/eval_results.json:    "f1": 91.04994418518018
./new/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_HasAns_f1": 85.72401519644451,
./new/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_NoAns_f1": 80.7569386038688,
./new/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_best_f1": 84.39435986584854,
./new/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_best_f1_thresh": -15.705571174621582,
./new/run_qa_beam_search_squad_v2/1/eval_results.json:    "eval_f1": 83.23692092011478,
./new/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "f1": 50.07159100480081,
./new/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "HasAns_f1": 0.0,
./new/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "NoAns_f1": 100.0,
./new/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "best_f1": 50.07159100480081,
./new/run_qa_beam_search_no_trainer_squad_v2/1/eval_results.json:    "best_f1_thresh": 0.0
./new/run_qa/1/eval_results.json:    "eval_f1": 88.3974945885421,
./new/run_qa_notrainer/1/eval_results.json:    "f1": 88.45989105569917
```

Comments:
_The documentation is not available anymore as the PR was closed or merged._