Packages auto-gptq or optimum mistakenly reported as not available
Author: stwykd
Pull request: no

### System Info

I'm running on a Kaggle notebook using GPU T4 x2

### Who can help?

@younesbelkada @SunMarc 

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

https://github.com/huggingface/transformers/blob/main/src/transformers/utils/import_utils.py#L41 this function _is_package_available() might be misreporting optimum or auto-gptq as not available:

```
model = AutoModelForCausalLM.from_pretrained(mn, device_map=0, torch_dtype=torch.float16)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[14], line 1
----> 1 model = AutoModelForCausalLM.from_pretrained(mn, device_map=0, torch_dtype=torch.float16)

File /opt/conda/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py:563, in _BaseAutoModelClass.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    561 elif type(config) in cls._model_mapping.keys():
    562     model_class = _get_model_class(config, cls._model_mapping)
--> 563     return model_class.from_pretrained(
    564         pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs
    565     )
    566 raise ValueError(
    567     f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
    568     f"Model type should be one of {', '.join(c.__name__ for c in cls._model_mapping.keys())}."
    569 )

File /opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py:2572, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, *model_args, **kwargs)
   2570     raise RuntimeError("GPU is required to quantize or run quantize model.")
   2571 elif not (is_optimum_available() and is_auto_gptq_available()):
-> 2572     raise ImportError(
   2573         "Loading GPTQ quantized model requires optimum library : `pip install optimum` and auto-gptq library 'pip install auto-gptq'"
   2574     )
   2575 else:
   2576     # Need to protect the import
   2577     from optimum.gptq import GPTQQuantizer

ImportError: Loading GPTQ quantized model requires optimum library : `pip install optimum` and auto-gptq library 'pip install auto-gptq'`
```

Here's some a filtered list of packages installed:
```
!pip list
Package                                  Version               Editable project location
---------------------------------------- --------------------- -------------------------
accelerate                               0.22.0
auto-gptq                                0.4.2
optimum                                  1.13.2
scipy                                    1.11.3
tensorflow                               2.12.0
tokenizers                               0.13.3
torch                                    2.0.0
```

### Expected behavior

I'd expect to be able to load the model

Comments:
hi @stwykd 
Hmmm with these notebooks I used to face similar issues with `bitsandbytes` and I resolved them by simply deleting the kernel and restarting it. Make sure to put all packages at the first cell. Let me know how that goes

Hey. I did already tried restarting the kernel, but the error persisted.
What also led me to think it might be `transformers`, is that I can write a cell to import those libraries and it works

I've tried on a colab as well (though I understand it's a similar runtime) and I get the error

OK thanks, will have a look

seeing the same error myself, notebook was running a few weeks ago

Hi @ainatersol, that would be very helpful if you could share a snippet or a colab that reproduce the error.

I had the same error using colab and followed younesbelkada advice and it resolved. 
> I resolved them by simply deleting the kernel and restarting it. Make sure to put all packages at the first cell. Let me know how that goes

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/main/CONTRIBUTING.md) are likely to be ignored.

I'm running the notebook again. This time on WSL rather than Kaggle from a fresh conda environment (therefore with new installation of these packages) using Python 3.10.
I get the same exact error:
```
ImportError: Loading a GPTQ quantized model requires optimum (`pip install optimum`) and auto-gptq library (`pip install auto-gptq`)
```

Could it be the way the presence of packages is checked: https://github.com/huggingface/transformers/blob/main/src/transformers/utils/import_utils.py#L41 ?

I resolved by commenting out this `elif` scope throwing the error https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_utils.py#L3017
The notebook is ran fine afterwards. 

Hi @stwykd, thanks for investigating. Can you tell me what the following snippet returns: 

```python
from transformers.utils import is_auto_gptq_available,   is_optimum_available
print(is_auto_gptq_available())
print(is_optimum_available())
```

This is strange that this is working when one of the following libraries are not available. Maybe there is an issue on how we check them. 

They both return True now.
I've uncommented the lines I had removed. I also restarted the kernel and cleared any cache. The notebook seems good now

Thanks @SunMarc @stwykd ! Issue seems to be resolved, is it ok if we close it?

Yes, it can be closed now

In google colab, i reversed these lines and it started to work:

from:
```
!pip install -U accelerate bitsandbytes datasets peft transformers
!pip install auto_gptq
!pip install optimum
```

to: 
```
!pip install auto_gptq
!pip install optimum
!pip install -U accelerate bitsandbytes datasets peft transformers
```

> hi @stwykd Hmmm with these notebooks I used to face similar issues with `bitsandbytes` and I resolved them by simply deleting the kernel and restarting it. Make sure to put all packages at the first cell. Let me know how that goes

It work for me!

> !pip install auto_gptq
> !pip install optimum
> !pip install -U accelerate bitsandbytes datasets peft transformers


Thank You. It worked for me.

> In google colab, i reversed these lines and it started to work:
> 
> from:
> 
> ```
> !pip install -U accelerate bitsandbytes datasets peft transformers
> !pip install auto_gptq
> !pip install optimum
> ```
> 
> to:
> 
> ```
> !pip install auto_gptq
> !pip install optimum
> !pip install -U accelerate bitsandbytes datasets peft transformers
> ```

I'd advise against mixing `pip install` with `pip install -U`