快速结论:这个报错通常发生在 CPU-only 环境(无 CUDA、无 MPS)安装 transformers 5.19.0 后,导入模型或 AutoTokenizer 时因 torch 版本差异触发。优先确认 torch 版本是否为 2.5.x 或 2.6.x,并检查 get_device_type() 是否在导入路径上被调用。
适用环境:transformers 5.19.0(main 分支 2026-10-07 同样受影响);Python 3.12;Linux x86_64 与 aarch64 python:3.12-slim 容器;CPU-only torch wheels(无 CUDA、无 MPS);torch 2.5.1 与 2.6.0 失败,torch 2.7.1 正常。Apple Silicon(有 MPS)不复现。
最快修复方案:暂无确认的一步修复方案。Issue 中验证过的替代做法是在受影响环境中降级到 transformers 5.15.1(或 pin transformers<5.19),或升级 torch 到 2.7.1。
注意事项:降级 transformers 可能影响依赖新版本功能的项目;升级 torch 到 2.7.1 对只能使用 CPU-only wheel 的环境可能不适用。Issue 中的 get_device_type() guard 补丁是用户自行 patch 验证的,并非官方发布修复。
问题场景
在无 CUDA 设备、无 MPS 加速器的机器上,使用 CPU-only torch 安装 transformers 5.19.0 后,执行 from transformers import CLIPModel 或 from transformers import AutoTokenizer 即触发导入失败。后者由 opencompass==0.5.3 这类无上限声明 transformers>=4.29.1 的依赖在新 venv 中自动拉取最新版后触发。
报错原文
File ".../transformers/utils/import_utils.py", line 339, in get_device_type
device = torch.accelerator.current_accelerator() or torch.device("cpu")
File ".../torch/accelerator/__init__.py", line 74, in current_accelerator
return torch._C._accelerator_getAccelerator()
RuntimeError: Cannot access accelerator device when none is available.
The above exception was the direct cause of the following exception:
...
ModuleNotFoundError: Could not import module 'CLIPModel'. Are this object's requirements defined correctly? Set the logging verbosity to DEBUG for the original import error.
在 torch 2.5.1 上,同样导入会更早失败:
File ".../transformers/utils/import_utils.py", line 339, in get_device_type
device = torch.accelerator.current_accelerator() or torch.device("cpu")
AttributeError: module 'torch' has no attribute 'accelerator'
原因分析
调用链为 masking_utils.py:32(模块级 if is_torch_flex_attn_available():)→ import_utils.py:766(get_device_type() != "tpu")→ import_utils.py:339。两个 torch 版本失败原因不同:torch 2.5 中不存在 torch.accelerator 属性;torch 2.6 中 current_accelerator() 在无加速器时会主动抛出异常,而该函数从 2.7 起才改为返回 None,从而适配 ... or torch.device("cpu") 的写法。transformers 5.19.0 的 is_torch_available() 仍接受 torch >= 2.5.0,因此导入路径未提前拦截,报错信息也没有提示 torch 版本问题。
环境排查
- torch 版本:确认是 2.5.x、2.6.x 还是 2.7.x;2.5 与 2.6 均会触发,2.7.1 正常。
- transformers 版本:确认是否为 5.19.0 或 main 2026-10-07;5.15.1 导入正常。
- 加速器可用性:确认机器是否有 CUDA 设备或 MPS;Apple Silicon 不复现。
- Python 版本:Issue 中为 3.12。
- 依赖声明:检查上层包是否使用无上限的
transformers>=4.29.1之类声明,导致自动安装到 5.19.0。
解决步骤
- 在复现环境执行
python -c "import torch; print(torch.__version__)",确认 torch 落在 2.5.x 或 2.6.x。 - 直接定位根因(Issue 中已验证):执行
python -c "import torch; print(torch.accelerator.is_available())",再执行python -c "from transformers.utils.import_utils import get_device_type; get_device_type()",观察是否抛出同一 RuntimeError。 - 若项目允许,将 transformers 降级到 5.15.1(Issue 中验证该版本导入正常),或在依赖声明中 pin
transformers<5.19。 - 若环境允许升级,将 torch 升级到 2.7.1,Issue 中该版本在 CPU-only 环境导入正常。
- 可优先尝试:Issue 提交者验证过的 guard 写法,在
get_device_type()中先判断torch.accelerator是否存在及其is_available(),再回退到 CPU,可同时兼容 2.5.1、2.6.0、2.7.1;此补丁为就地 patch 验证,并非官方发布修复。
验证方法
在目标环境中重新执行 python -c "from transformers import CLIPModel" 或 python -c "from transformers import AutoTokenizer",不再出现 RuntimeError 或 ModuleNotFoundError 即表示问题已解决。也可单独调用 get_device_type() 与 is_torch_flex_attn_available() 确认不再抛异常。
参考来源
huggingface/transformers #49380
AI 工具推荐
想把多个 AI 模型放在一个入口?
GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。
推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。
这个方案解决了吗?
可以继续搜索完整报错,或查看同一工具的其他排查指南。
![HTTP Request node: array[object] variables are inserted as Python repr, corrupting the request body](https://www.chat-gpts.plus/wp-content/uploads/2026/10/43465-baa39943-768x403.jpg)

