从本地克隆仓库加载Hugging Face Falcon-7B模型遇报错求助
本地加载Falcon-7B模型报错的解决方法
问题描述
我想使用Hugging Face的大语言模型(比如Falcon-7B),在线加载模型的代码如下:
from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-7b", trust_remote_code=True)
但我将模型仓库克隆/权重下载到本地后,使用以下代码加载时出现错误:
model = AutoModelForCausalLM.from_pretrained('./falcon-7b/')
错误信息:
945 has_remote_code = "auto_map" in config_dict and "AutoConfig" in config_dict["auto_map"] 946 has_local_code = "model_type" in config_dict and config_dict["model_type"] in CONFIG_MAPPING --> 947 trust_remote_code = resolve_trust_remote_code( 948 trust_remote_code, pretrained_model_name_or_path, has_local_code, has_remote_code 949 ) 951 if has_remote_code and trust_remote_code: ... --> 535 signal.signal(signal.SIGALRM, _raise_timeout_error) 536 signal.alarm(TIME_OUT_REMOTE_CODE) 537 while trust_remote_code is None: AttributeError: module 'signal' has no attribute 'SIGALRM'
错误原因与解决步骤
核心原因
- Falcon-7B属于自定义架构模型,依赖仓库中的自定义代码才能加载,本地加载时同样需要显式声明信任远程代码。
SIGALRM是类Unix系统专属信号,Windows系统不支持该信号,transformers在自动确认是否信任远程代码的超时逻辑中会触发此报错。
解决方法
必须添加
trust_remote_code=True参数
本地加载模型时,需和在线加载一样声明信任远程代码,正确代码如下:from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained('./falcon-7b/', trust_remote_code=True)Windows环境额外处理
若在Windows系统运行,可通过以下方式解决:- 升级transformers到4.30及以上版本:新版本已修复Windows下的信号兼容问题。
- 确保显式设置
trust_remote_code=True:直接跳过框架的自动确认提示,避免触发超时检查逻辑。
额外注意事项
确保本地仓库文件完整,包括config.json、所有模型权重文件,以及仓库中的自定义代码文件(如modeling_falcon.py、configuration_falcon.py等),缺失文件会导致加载失败。
内容的提问来源于stack exchange,提问作者Thanh Long Phan
相关产品推荐
相关产品推荐

