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

加载Hugging Face量化Llama2模型时遇SafeTensors参数错误求助

问题描述

此前一直使用Hugging Face Transformers的量化Llama2模型(TheBloke/Llama-2-7B-Chat-AWQ),代码完全来自官方文档:

from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer

model_name_or_path = "TheBloke/Llama-2-7b-Chat-AWQ"

# Load model
model = AutoAWQForCausalLM.from_quantized(
    model_name_or_path,
    fuse_layers = True,
    trust_remote_code = False,
    safetensors = True
)

如今加载模型时突然报错:

TypeError: SafeTensorsInfo.__init__() got an unexpected keyword argument 'sharded'

今日早些时候同一机器上运行该代码完全正常,未更换模型,也未安装或更新任何Python包。当前环境为Ubuntu 20.04,相关包版本如下:

accelerate==0.28.0
autoawq==0.2.4
autoawq_kernels==0.0.6
bitsandbytes==0.43.0
huggingface-hub==0.22.2
torch==2.2.2+cu121
torchaudio==2.2.2+cu121
torchvision==0.17.2+cu121
transformers==4.38.2

怀疑模型缓存失效后下载了新版本,但模型页面未显示更新,求恢复至今早代码可正常运行状态的方法。

解决方案

按以下优先级尝试恢复:

  • 清理并重新加载缓存
    删除默认的Hugging Face模型缓存目录~/.cache/huggingface/hub/models--TheBloke--Llama-2-7b-Chat-AWQ,然后在代码中指定新缓存目录强制重新下载模型:

    model = AutoAWQForCausalLM.from_quantized(
        model_name_or_path,
        fuse_layers=True,
        trust_remote_code=False,
        safetensors=True,
        cache_dir="./temp_cache"
    )
    

    运行正常后可保留该缓存或替换原缓存目录。

  • 锁定safetensors版本
    该报错大概率是safetensors版本兼容问题,即使未手动更新,也可能被其他依赖间接更新。回退至匹配当前环境的稳定版本:

    pip install safetensors==0.4.2
    

    之后重新运行代码测试。

  • 禁用safetensors加载
    若前两种方法无效,尝试关闭safetensors参数改用常规加载方式:

    model = AutoAWQForCausalLM.from_quantized(
        model_name_or_path,
        fuse_layers=True,
        trust_remote_code=False,
        safetensors=False
    )
    
  • 回退huggingface-hub版本
    SafeTensorsInfo来自huggingface-hub包,可能是隐性更新导致兼容性问题,回退至旧版本:

    pip install huggingface-hub==0.21.4
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 19:04:51