cannot import name 'is_timm_config_dict' from 'transformers.utils.generic'
Author: xiezhipeng-git
Pull request: no
Labels: name: bug

### System Info

Transformers version 4.48.2
platform  kaggle L4*4 or P40
timm version 1.0.12 or1.0.14 or None
Python version 3.10.12 (main, Nov  6 2024, 20:22:13) [GCC 11.4.0]

### Who can help?

_No response_

### Information

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

### Tasks

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

### Reproduction

from vllm.platforms import current_platform
then get
```
ImportError                               Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   1792             missing_backends = self._object_missing_backend[name]
-> 1793 
   1794             class Placeholder(metaclass=DummyObject):

/usr/lib/python3.10/importlib/__init__.py in import_module(name, package)
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 

/usr/lib/python3.10/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/usr/lib/python3.10/importlib/_bootstrap.py in _load_unlocked(spec)

/usr/lib/python3.10/importlib/_bootstrap_external.py in exec_module(self, module)

/usr/lib/python3.10/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

/usr/local/lib/python3.10/dist-packages/transformers/configuration_utils.py in <module>
     42 )
---> 43 from .utils.generic import is_timm_config_dict
     44 

ImportError: cannot import name 'is_timm_config_dict' from 'transformers.utils.generic' (/usr/local/lib/python3.10/dist-packages/transformers/utils/generic.py)

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-13-46a92ab71489> in <cell line: 1>()
----> 1 from vllm.platforms import current_platform
      2 device_name = current_platform.get_device_name().lower()
      3 print(device_name)

/usr/local/lib/python3.10/dist-packages/vllm/__init__.py in <module>
      4 import torch
      5 
----> 6 from vllm.engine.arg_utils import AsyncEngineArgs, EngineArgs
      7 from vllm.engine.async_llm_engine import AsyncLLMEngine
      8 from vllm.engine.llm_engine import LLMEngine

/usr/local/lib/python3.10/dist-packages/vllm/engine/arg_utils.py in <module>
      9 
     10 import vllm.envs as envs
---> 11 from vllm.config import (CacheConfig, CompilationConfig, ConfigFormat,
     12                          DecodingConfig, DeviceConfig, HfOverrides,
     13                          KVTransferConfig, LoadConfig, LoadFormat, LoRAConfig,

/usr/local/lib/python3.10/dist-packages/vllm/config.py in <module>
     15 import torch
     16 from pydantic import BaseModel, Field, PrivateAttr
---> 17 from transformers import PretrainedConfig
     18 
     19 import vllm.envs as envs

/usr/lib/python3.10/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)

/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py in __getattr__(self, name)
   1779     def __dir__(self):
   1780         result = super().__dir__()
-> 1781         # The elements of self.__all__ that are submodules may or may not be in the dir already, depending on whether
   1782         # they have been accessed or not. So we only add the elements of self.__all__ that are not already in the dir.
   1783         for attr in self.__all__:

/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   1793 
   1794             class Placeholder(metaclass=DummyObject):
-> 1795                 _backends = missing_backends
   1796 
   1797                 def __init__(self, *args, **kwargs):

RuntimeError: Failed to import transformers.configuration_utils because of the following error (look up to see its traceback):
cannot import name 'is_timm_config_dict' from 'transformers.utils.generic' (/usr/local/lib/python3.10/dist-packages/transformers/utils/generic.py)
```

### Expected behavior

No error

@zucchini-nlp 

Comments:
cc @qubvel who added TIMM

Hey @xiezhipeng-git, thanks for opening an issue! It's a bit hard to debug without a local reproducing example, but I made a guess. Can you try to install it from my branch to check if it fixes the issue?

```
pip install -U git+https://github.com/qubvel/transformers@fix-import-is-timm-config
```

> https://github.com/qubvel/transformers@fix-import-is-timm-config

I can not update on kaggle
```
Collecting git+https://github.com/qubvel/transformers@fix-import-is-timm-config
  Cloning https://github.com/qubvel/transformers (to revision fix-import-is-timm-config) to /tmp/pip-req-build-3gfpawmg
  Running command git clone --filter=blob:none --quiet https://github.com/qubvel/transformers /tmp/pip-req-build-3gfpawmg
  fatal: unable to access 'https://github.com/qubvel/transformers/': Could not resolve host: github.com
  error: subprocess-exited-with-error
  
  × git clone --filter=blob:none --quiet https://github.com/qubvel/transformers /tmp/pip-req-build-3gfpawmg did not run successfully.
  │ exit code: 128
  ╰─> See above for output.
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× git clone --filter=blob:none --quiet https://github.com/qubvel/transformers /tmp/pip-req-build-3gfpawmg did not run successfully.
│ exit code: 128
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
Note: you may need to restart the kernel to use updated packages.
```

It seems Kaggle is blocking installation from github, here is another way

1. **Download the repo as a ZIP file**  
   On your local machine, run:
   ```bash
   git clone --branch fix-import-is-timm-config https://github.com/qubvel/transformers.git
   zip -r transformers.zip transformers
   ```
   
2. **Upload the ZIP file to Kaggle**  
   - Go to your Kaggle notebook.
   - Click on `+ Add Data` (on the right panel).
   - Upload the `transformers.zip` file.

3. **Extract and Install**  
   Inside the Kaggle notebook, run:
   ```bash
   !unzip -q /kaggle/input/transformers.zip -d /kaggle/working/
   !pip install /kaggle/working/transformers
   ```

> It seems Kaggle is blocking installation from github, here is another way似乎 Kaggle 正在阻止从 GitHub 安装，这里还有另一种方法
> 
> 1. **Download the repo as a ZIP file下载该存储库的 ZIP 文件**
>    On your local machine, run:在您的本地计算机上，运行：
>    git clone --branch fix-import-is-timm-config https://github.com/qubvel/transformers.git
>    zip -r transformers.zip transformers
> 2. **Upload the ZIP file to Kaggle上传 ZIP 文件到 Kaggle**
>    
>    * Go to your Kaggle notebook.前往您的 Kaggle 笔记本。
>    * Click on `+ Add Data` (on the right panel).点击右侧面板上的 `+ Add Data` 。
>    * Upload the `transformers.zip` file.  上传 `transformers.zip` 文件。
> 3. **Extract and Install  提取和安装**
>    Inside the Kaggle notebook, run:在 Kaggle 笔记本中运行：
>    !unzip -q /kaggle/input/transformers.zip -d /kaggle/working/
>    !pip install /kaggle/working/transformers

Can you directly upload a resource on Kaggle as public tranfomers4.48.2fix? Or upload a zip of your branch here?
It's also difficult to download locally. Always get interrupted halfway through the download.
```
PS D:\my\work\LLM\tranformers> git clone --branch fix-import-is-timm-config https://github.com/qubvel/transformers.git
Cloning into 'transformers'...
remote: Enumerating objects: 211587, done.
remote: Counting objects: 100% (397/397), done.
remote: Compressing objects: 100% (305/305), done.
error: RPC failed; curl 92 HTTP/2 stream 5 was not closed cleanly: CANCEL (err 8)
error: 7843 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
```
Already tried several times

Here it is

https://www.kaggle.com/datasets/pavel92/transformers-import-fix

> Here is it
> 
> https://www.kaggle.com/datasets/pavel92/transformers-import-fix

It seems to have no effect. I deleted the .github folder and the .git folder. Then I used python setup.py bdist_wheel to package it into a wheel, and then uploaded the dist/transformers-4.49.0.dev0-py3-none-any.whl file. File size changed from 289M to 10M
Installation successful
```
%pip show timm
%pip show transformers
```
```
Name: timm
Version: 1.0.12
Summary: PyTorch Image Models
Home-page: https://github.com/huggingface/pytorch-image-models
Author: 
Author-email: Ross Wightman <ross@huggingface.co>
License: Apache-2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: huggingface_hub, pyyaml, safetensors, torch, torchvision
Required-by: 
Note: you may need to restart the kernel to use updated packages.
Name: transformers
Version: 4.49.0.dev0
Summary: State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow
Home-page: https://github.com/huggingface/transformers
Author: The Hugging Face team (past and future) with the help of all our contributors (https://github.com/huggingface/transformers/graphs/contributors)
Author-email: transformers@huggingface.co
License: Apache 2.0 License
Location: /usr/local/lib/python3.10/dist-packages
Requires: filelock, huggingface-hub, numpy, packaging, pyyaml, regex, requests, safetensors, tokenizers, tqdm
Required-by: compressed-tensors, kaggle-environments, peft, sentence-transformers, vllm, xgrammar
Note: you may need to restart the kernel to use updated packages.
```
However, it still reports an error
```
ImportError                               Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   1792             self.__path__ = [os.path.dirname(module_file)]
-> 1793             self._objects = {} if extra_objects is None else extra_objects
   1794             self._name = name

/usr/lib/python3.10/importlib/__init__.py in import_module(name, package)
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 

/usr/lib/python3.10/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/usr/lib/python3.10/importlib/_bootstrap.py in _load_unlocked(spec)

/usr/lib/python3.10/importlib/_bootstrap_external.py in exec_module(self, module)

/usr/lib/python3.10/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

/usr/local/lib/python3.10/dist-packages/transformers/configuration_utils.py in <module>
     29 from .modeling_gguf_pytorch_utils import load_gguf_checkpoint
---> 30 from .utils import (
     31     CONFIG_NAME,

ImportError: cannot import name 'is_timm_config_dict' from 'transformers.utils' (/usr/local/lib/python3.10/dist-packages/transformers/utils/__init__.py)

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-18-39a0592b9e7a> in <cell line: 1>()
----> 1 from vllm import LLM, SamplingParams
      2 
      3 warnings.simplefilter('ignore')
      4 
      5 # os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"

/usr/local/lib/python3.10/dist-packages/vllm/__init__.py in <module>
      4 import torch
      5 
----> 6 from vllm.engine.arg_utils import AsyncEngineArgs, EngineArgs
      7 from vllm.engine.async_llm_engine import AsyncLLMEngine
      8 from vllm.engine.llm_engine import LLMEngine

/usr/local/lib/python3.10/dist-packages/vllm/engine/arg_utils.py in <module>
      9 
     10 import vllm.envs as envs
---> 11 from vllm.config import (CacheConfig, CompilationConfig, ConfigFormat,
     12                          DecodingConfig, DeviceConfig, HfOverrides,
     13                          KVTransferConfig, LoadConfig, LoadFormat, LoRAConfig,

/usr/local/lib/python3.10/dist-packages/vllm/config.py in <module>
     15 import torch
     16 from pydantic import BaseModel, Field, PrivateAttr
---> 17 from transformers import PretrainedConfig
     18 
     19 import vllm.envs as envs

/usr/lib/python3.10/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)

/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py in __getattr__(self, name)
   1779                         self._object_missing_backend[key] = missing_backends
   1780 
-> 1781                     for value in values:
   1782                         self._class_to_module[value] = key
   1783                         if len(missing_backends):

/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
   1793             self._objects = {} if extra_objects is None else extra_objects
   1794             self._name = name
-> 1795             self._import_structure = _import_structure
   1796 
   1797         # This can be removed once every exportable object has a `export()` export.

RuntimeError: Failed to import transformers.configuration_utils because of the following error (look up to see its traceback):
cannot import name 'is_timm_config_dict' from 'transformers.utils' (/usr/local/lib/python3.10/dist-packages/transformers/utils/__init__.py)
```
the error changed

@qubvel 
I took a look at the location of this erroneous file. I found that the reason for the error is a path error caused by the same file name. The path that reported an error is.
/usr/local/lib/python3.10/dist-packages/transformers/utils/
But there is no generic.py file under this path. The actual path is.
/usr/local/lib/python3.10/dist-packages/transformers/src/transformers/utils/generic.py

Not sure I got it, were you able to solve the issue?

> Not sure I got it, were you able to solve the issue?

I haven't tried it yet. Do I need to move all the code under /usr/local/lib/python3.10/dist-packages/transformers/src/transformers/utils/ to /usr/local/lib/python3.10/dist-packages/transformers/utils/?
This may have an impact on searching other files. The previous transformers did not have this issue. From which version was /usr/local/lib/python3.10/dist-packages/transformers/src/transformers added?
2-level Transformers path? or 2-level utils path?
If there was a level 2 transformers path a long time ago. Does that mean the new referencing method is incorrect?
The impact of this problem is quite serious. It directly leads to the inability to use vllm0.7.1 for inference on Kaggle. However, my local 4090 does not have this problem.

@qubvel
The strangest point is that clearly there is no code related to devices involved. Why is it that only on Kaggle does this error occur? I am completely out of ideas now. At present, apart from the possibility that the same second-level path may lead to files not being found, I can't think of any other possibilities. But the code seems to be correct. Do you have any ideas?
When I create a new kaggle notebook,this issue disappear.
then I get this
https://github.com/vllm-project/vllm/issues/12904

Sorry, don't have any ideas, I'm not using Kaggle env at the moment. I hope you will figure out the solution, and thanks for linking the issue.
Feel free to close this issue if it's no longer relevant.

> Sorry, don't have any ideas, I'm not using Kaggle env at the moment. I hope you will figure out the solution, and thanks for linking the issue. Feel free to close this issue if it's no longer relevant.

Today it appeared again. And after it appeared, this problem exists regardless of how it is handled. I also don't know what the specific conditions for its appearance are.

I think trying with conda can resolve the issue 🤔 

> I think trying with conda can resolve the issue 🤔

Can you be more detailed? How can I use conda to reinstall a local transformers package in the Kaggle environment?

`pip install vllm==0.7.1 triton==3.1.0`

@qubvel 
transformers 4.49.0
Kaggle  This problem still exists. And uninstalling timm is also not useful.

> > https://github.com/qubvel/transformers@fix-import-is-timm-config
> 
> I can not update on kaggle
> 
> ```
> Collecting git+https://github.com/qubvel/transformers@fix-import-is-timm-config
>   Cloning https://github.com/qubvel/transformers (to revision fix-import-is-timm-config) to /tmp/pip-req-build-3gfpawmg
>   Running command git clone --filter=blob:none --quiet https://github.com/qubvel/transformers /tmp/pip-req-build-3gfpawmg
>   fatal: unable to access 'https://github.com/qubvel/transformers/': Could not resolve host: github.com
>   error: subprocess-exited-with-error
>   
>   × git clone --filter=blob:none --quiet https://github.com/qubvel/transformers /tmp/pip-req-build-3gfpawmg did not run successfully.
>   │ exit code: 128
>   ╰─> See above for output.
>   
>   note: This error originates from a subprocess, and is likely not a problem with pip.
> error: subprocess-exited-with-error
> 
> × git clone --filter=blob:none --quiet https://github.com/qubvel/transformers /tmp/pip-req-build-3gfpawmg did not run successfully.
> │ exit code: 128
> ╰─> See above for output.
> 
> note: This error originates from a subprocess, and is likely not a problem with pip.
> Note: you may need to restart the kernel to use updated packages.
> ```

You need to turn the internet settings on to be able to access and resolve any link on the internet.

If kaggle Utility Scripts are not used. There seems to be no such problem. It may be that there is a problem with the version of transformers in kaggle Utility Scripts. And it is caused by overwriting the current kaggle environment. That is, the latest version of transfomers should solve this problem.

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.