Added fsck_etags to verify cache consistency.
Author: xloem
Pull request: yes

# What does this PR do?

A prototype function is added to check cache consistency.
Note that it does not verify the data is correct, just that the data hashes are consistent.

It may be used as: `python -c 'from transformers import file_utils; file_utils.fsck_etags()'`

No output means all the local etags match their data.  Shown file by file if info logging is enabled.
<!--
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 -->
#12557


## 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/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:
Hello, thank you for your contribution! How do you envision using this? Running this locally on a smallish 23GB cache takes 46 seconds on my machine, so this isn't something that can be ran every time.

How would you recommend we approach solving #12557 using your proposal?

Hey, thanks for your reply.

Personally I was running into crashes from data corruption and needed a way to handle that situation.  I thought others might get into the same scenario, so shared this code.  When it moves the corrupt files away they get redownloaded.

This might work better if a standalone tool were added that users could run.  I have severe cognitive and computer issues and contribute in very small bursts.

Regarding thoughts like 12557:
- this introduces the concept of fscking data, but doesn't solve the issue
- checking etags from the network could help, certificate pinning helps.  The fscking could be merged into an 'update all models' function, since the network only has the latest etags.
- regarding automation speed, it might be faster to check only the files that are loaded, when they are loaded, or only after download or at user request, or have it disableable.
- things could be made more normative if the cache used git repositories in some way.  git already has fsck and signatures to some degree, and users can make their own git repositories easily.  not sure why the design decision was made not to do that, but the hashed filenames certainly provide more ways to verify.
- regarding 12557, i can come up with lots of ideas, but if there were some public messaging around the concept then more experienced cryptographers and security specialists would likely eventually weigh in.  i'm just an old disabled hobbyist.

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.