transformers-cli depends on torchaudio optional deps
Author: stas00
Pull request: no

Looks like we somewhere load some imports that shouldn't be imported when invoking `transformers-cli`

Traceback:

```
 transformers-cli login
2021-08-06 01:24:22.009842: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
Traceback (most recent call last):
  File "/gpfswork/rech/six/commun/conda/hf-prod/bin/transformers-cli", line 33, in <module>
    sys.exit(load_entry_point('transformers', 'console_scripts', 'transformers-cli')())
  File "/gpfswork/rech/six/commun/conda/hf-prod/bin/transformers-cli", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/importlib/metadata.py", line 77, in load
    module = import_module(match.group('module'))
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/commands/transformers_cli.py", line 23, in <module>
    from .run import RunCommand
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/commands/run.py", line 17, in <module>
    from ..pipelines import SUPPORTED_TASKS, TASK_ALIASES, Pipeline, PipelineDataFormat, pipeline
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/pipelines/__init__.py", line 26, in <module>
    from ..models.auto.feature_extraction_auto import FEATURE_EXTRACTOR_MAPPING, AutoFeatureExtractor
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/models/auto/feature_extraction_auto.py", line 20, in <module>
    from transformers import DeiTFeatureExtractor, Speech2TextFeatureExtractor, ViTFeatureExtractor
  File "<frozen importlib._bootstrap>", line 1039, in _handle_fromlist
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/file_utils.py", line 1978, in __getattr__
    value = getattr(module, name)
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/file_utils.py", line 1977, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/file_utils.py", line 1986, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/gpfsssd/worksf/projects/rech/six/commun/code/transformers/src/transformers/models/speech_to_text/feature_extraction_speech_to_text.py", line 23, in <module>
    import torchaudio.compliance.kaldi as ta_kaldi
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/site-packages/torchaudio/__init__.py", line 15, in <module>
    from torchaudio.backend import (
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/site-packages/torchaudio/backend/__init__.py", line 2, in <module>
    from . import utils
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/site-packages/torchaudio/backend/utils.py", line 7, in <module>
    from . import (
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/site-packages/torchaudio/backend/soundfile_backend.py", line 15, in <module>
    import soundfile
  File "/gpfswork/rech/six/commun/conda/hf-prod/lib/python3.8/site-packages/soundfile.py", line 142, in <module>
    raise OSError('sndfile library not found')
OSError: sndfile library not found
```

`sndfile` is an optional dependency of `torchaudio`, so it might not be installed.

Thank you!

I'm pretty sure it's a recent version, but it wasn't me who had this problem, so pasting it as it was given to me. This comes from JeanZay - I installed `libsndfile` to overcome it as a temp workfix. But this tool is a "long way" from needing `libsndfile` to function properly - functionality-wise.

update:
```
python -c "import transformers; print(transformers.__version__)"
4.10.0.dev0
```
@sgugger 

Comments:
This looks like `torchaudio` is installed without `sndfile`, in which case there is little we can do on our side. If `torchaudio` is not installed, this code is not executed and the command runs normally, just tried in an environment with or without it.

I would need more to reproduce the problem if I misdiagnosed the issue.

right, it didn't get installed, because `sndfile` is not in `torchaudio` requirements. Only some of its modules require `sndfile` and that's why it's made optional. At least that's the answer I got on torchaudio slack, where I asked first.

Specifically on JZ, I don't think we need `torchaudio` so it's probably safe to just remove it. But it might not be the case for other envs.

If you can't think of a way to overcome this let's close this then. Thank you for looking into this, @sgugger 

Ah understood then. Not sure how we can check a bit better for the `is_torch_audio_available` check. It should make sure "Soundfile" is installed too maybe? What do you think @patrickvonplaten ?

That's a great idea! It should probably include checks for all the optional torchaudio deps that transformers audio models use.

But also why does `transformers-cli` need to load everything? It has nothing to do with the specific models.

Yeah I think it's a good idea as well!

cc https://github.com/huggingface/transformers/issues/12509

Yeah I don't think `transformers-cli` needs to load all optional packages either

I'll have a look as to why it's trying to load this model later today.

Ok I have dived into it a bit and here is the diagnostic. `transformers-cli env` in it self runs `transformers.commands.env` which doesn't import anything by itself (apart from the version and a few functions in `file_utils`).

*But* `transformers-cli` in itself imports all the commands (even if we only use env), in particular `run` which requires the pipeline. Now since #13023 this import does not import all the models anymore, so the bug in itself is resolved (if you could try again on your env with the problem @stas00 that would be great).

Hi - So the underlying problem in torchaudio is that torchaudio assumed that `soundfile` is either installed fine or not, but apparently there is a third state where `soundfile` is installed yet the underlying `libsndfile` is not available. On our end, we will make sure this third state is in consideration so that `import torchaudio` would not raise an error in this case.

> Ok I have dived into it a bit and here is the diagnostic. `transformers-cli env` in it self runs `transformers.commands.env` which doesn't import anything by itself (apart from the version and a few functions in `file_utils`).
> 
> _But_ `transformers-cli` in itself imports all the commands (even if we only use env), in particular `run` which requires the pipeline. Now since #13023 this import does not import all the models anymore, so the bug in itself is resolved (if you could try again on your env with the problem @stas00 that would be great).

I updated to master and confirm that this solved the problem. Thanks a lot, @sgugger!

> Hi - So the underlying problem in torchaudio is that torchaudio assumed that `soundfile` is either installed fine or not, but apparently there is a third state where `soundfile` is installed yet the underlying `libsndfile` is not available. On our end, we will make sure this third state is in consideration so that `import torchaudio` would not raise an error in this case.

That's really useful, thank you for implementing this, @mthrok!