Migrate import checks not need accelerate, and be more clear on min versions
Author: muellerzr
Pull request: yes

# What does this PR do?

This PR moves all import checks of `is_xxx_available()` from accelerate to their `transformers` counterparts. Avoids headaches when a new backend is added to accelerate (such as on main or a newer version) and as a result not part of the `utils.__init__.py`. 

Also clarifies min accelerate versions for features which raise `ImportErrors`

Fixes https://github.com/huggingface/transformers/issues/31583
Fixes another issue I found relating to this, but sadly buried across everything


## 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#create-a-pull-request),
      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/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/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.

@LysandreJik @SunMarc 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_32292). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SunMarc correct; which is why if not, we simply return False safely. (Since we’ve delegated everything wrt torch backends to Accelerate with the Trainer anyways)

@SunMarc thinking on it more post my walk, I'm going to do the reverse. This leaves people who use `Pipeline` etc to not get issues about accelerate being installed. Will push shortly

Hi @muellerzr, sadly I am still getting the "cannot import name 'is_mlu_available' from 'accelerate.utils'". Do you know how to solve this problem? My transformer version is 4.44.2 which should already have the above features integrated, and my accelerate version is 0.25.0

Can you share your traceback @nbasyl ? This will help us figure out the issue. That's strange because in the latest transformers, there is no records of `is_mlu_available`. 

I have the same issue right now with accelerate 0.21.0  and transformers 4.44.2   with the following Traceback (How do you share a Traceback message neatly in a comment like this?):

File /usr/lib/python3.12/importlib/__init__.py:90, in import_module(name, package)
     89         level += 1
---> 90 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1387, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1360, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:1331, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:935, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:995, in exec_module(self, module)

File <frozen importlib._bootstrap>:488, in _call_with_frames_removed(f, *args, **kwds)

File ~/.cache/pypoetry/virtualenvs/modelplayground-Bo7suU4M-py3.12/lib/python3.12/site-packages/transformers/trainer.py:221
    220 from accelerate import __version__ as accelerate_version
--> 221 from accelerate.utils import (
    222     DistributedDataParallelKwargs,
    223     DistributedType,
    224     GradientAccumulationPlugin,
    225     is_mlu_available,
    226     is_mps_available,
    227     is_npu_available,
    228     is_torch_version,
    229     is_xpu_available,
    230     load_fsdp_model,
    231     load_fsdp_optimizer,
    232     save_fsdp_model,
    233     save_fsdp_optimizer,
    234 )
    236 DATA_SAMPLERS = [RandomSampler]

ImportError: cannot import name 'is_mlu_available' from 'accelerate.utils' 



The above exception was the direct cause of the following exception:
RuntimeError                              Traceback (most recent call last)
Cell In[4], line 2
      1 # assemble Trainer
----> 2 from transformers import Trainer
      4 trainer = Trainer(
      5     model=model, 
      6     args=args,  # TrainingArguments
   (...)
     11     tokenizer=tokenizer,
     12 )
File <frozen importlib._bootstrap>:1412, in _handle_fromlist(module, fromlist, import_, recursive)
File ~/.cache/pypoetry/virtualenvs/modelplayground-Bo7suU4M-py3.12/lib/python3.12/site-packages/transformers/utils/import_utils.py:1593, in _LazyModule.__getattr__(self, name)
   1591     value = self._get_module(name)
   1592 elif name in self._class_to_module.keys():
-> 1593     module = self._get_module(self._class_to_module[name])
   1594     value = getattr(module, name)
   1595 else:
File ~/.cache/pypoetry/virtualenvs/modelplayground-Bo7suU4M-py3.12/lib/python3.12/site-packages/transformers/utils/import_utils.py:1605, in _LazyModule._get_module(self, module_name)
   1603     return importlib.import_module("." + module_name, self.__name__)
   1604 except Exception as e:
-> 1605     raise RuntimeError(
   1606         f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
   1607         f" traceback):\n{e}"
   1608     ) from e
RuntimeError: Failed to import transformers.trainer because of the following error (look up to see its traceback):
cannot import name 'is_mlu_available' from 'accelerate.utils' (/home//.cache/pypoetry/virtualenvs/modelplayground-Bo7suU4M-py3.12/lib/python3.12/site-packages/accelerate/utils/__init__.py)

Thx for the traceback @AylinW ! Sorry for the inconvenience. You need to either upgrade your version of accelerate or install the main branch of transformers. Also it will be fixed in the next release of transformers that will come out in ~ 1 week . 

Thank you for your quick answer! Took some time but updating on accelerate 0.33.0 did the trick. Restarting my IDE was the missing step after trying the version update yesterday apparently.