You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SentenceTransformer模型加载失败,报RuntimeError设备类型错误求助

问题描述

运行以下代码加载SentenceTransformer模型:

from sentence_transformers import SentenceTransformer, util

model = SentenceTransformer('all-MiniLM-L6-v2')

触发如下错误:

RuntimeError: Expected one of cpu, cuda, mkldnn, opengl, opencl, ideep, hip, msnpu, xla 
device type at start of device string: meta

已尝试两种方案但未解决:

  • 将transformers库降级至4.6.0版本
  • 显式指定运行设备:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = SentenceTransformer('all-MiniLM-L6-v2', device=device)
解决方案

1. 调整库版本至兼容组合

transformers 4.6.0版本过旧,与新版SentenceTransformer存在兼容性冲突。建议升级到经过验证的兼容版本组合:

pip install sentence-transformers==2.2.2 transformers==4.21.0

或直接升级到最新稳定版:

pip install --upgrade sentence-transformers transformers

2. 删除损坏的模型缓存

本地缓存的模型文件可能损坏或包含meta设备配置,删除缓存后重新下载:

  1. 定位缓存目录:通常为~/.cache/huggingface/hub
  2. 删除models--sentence-transformers--all-MiniLM-L6-v2文件夹
  3. 重新运行加载代码

3. 显式禁用meta设备加载逻辑

在初始化模型时添加参数,强制跳过meta设备相关逻辑:

from sentence_transformers import SentenceTransformer
import torch

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = SentenceTransformer(
    'all-MiniLM-L6-v2',
    device=device,
    load_in_8bit=False,
    device_map=None
)

4. 升级PyTorch至稳定版

旧版PyTorch可能存在设备识别bug,升级到对应CUDA/CPU的稳定版本:

# CUDA 11.8版本示例
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# CPU版本示例
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

内容的提问来源于stack exchange,提问作者lali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:08:29