深圳谷歌SEO公司推荐:大鱼营销——长效排名的实干家
2026/7/30 19:34:32
在使用 Hugging Face Transformers 库加载模型时,遇到以下错误:
OSError: Model file 'pytorch_model-00001-of-00003.bin' is corrupted or incomplete (unexpected EOF)这个错误通常由以下原因引起:
# 清理 Hugging Face 缓存rm-rf ~/.cache/huggingface/# 或只清理特定模型的缓存rm-rf ~/.cache/huggingface/hub/models--xxx--xxx-model/force_download=True强制重新下载fromtransformersimportAutoModelForCausalLM,AutoTokenizer# 强制重新下载模型tokenizer=AutoTokenizer.from_pretrained("xxx/xxx-model",force_download=True)model=AutoModelForCausalLM.from_pretrained("xxx/xxx-model",force_download=True)# 检查磁盘空间df-h# Windows 系统使用dirfromtransformersimportAutoModelForCausalLM,AutoTokenizer# 从本地路径加载模型model_path="./local_model_directory"tokenizer=AutoTokenizer.from_pretrained(model_path)model=AutoModelForCausalLM.from_pretrained(model_path)# 检查文件大小ls-lh pytorch_model-00001-of-00003.bin# 检查文件是否有意外的 EOF# 对于大型文件,可以使用以下命令检查head-c100pytorch_model-00001-of-00003.bin|xxd# 使用 git 克隆模型仓库gitclone https://huggingface.co/xxx/xxx-model# 或使用 lfsgitlfsinstallgitclone https://huggingface.co/xxx/xxx-model# 测试网络连接pinghuggingface.co# 检查下载速度curl-o /dev/null -s -w"%{speed_download}"https://huggingface.co/xxx/xxx-model/resolve/main/pytorch_model-00001-of-00003.binfromtransformersimportAutoModelForCausalLM,AutoTokenizerimportosimportshutildefdownload_model_with_retry(model_name,max_retries=3):"""带重试机制的模型下载"""retry_count=0whileretry_count<max_retries:try:print(f"Attempting to download model{model_name}(retry{retry_count+1}/{max_retries})...")# 清理可能的缓存cache_dir=os.path.expanduser("~/.cache/huggingface/hub")model_cache_dir=os.path.join(cache_dir,f"models--{model_name.replace('/','--')}")ifos.path.exists(model_cache_dir):print(f"Clearing existing cache for{model_name}...")shutil.rmtree(model_cache_dir)# 强制重新下载tokenizer=AutoTokenizer.from_pretrained(model_name,force_download=True)model=AutoModelForCausalLM.from_pretrained(model_name,force_download=True)print(f"Successfully downloaded and loaded model{model_name}!")returntokenizer,modelexceptExceptionase:print(f"Error downloading model:{e}")retry_count+=1ifretry_count<max_retries:print("Retrying...")else:print("Max retries reached. Exiting.")returnNone,Nonedefload_model_from_local(local_path):"""从本地路径加载模型"""try:print(f"Loading model from local path:{local_path}")tokenizer=AutoTokenizer.from_pretrained(local_path)model=AutoModelForCausalLM.from_pretrained(local_path)print("Successfully loaded model from local path!")returntokenizer,modelexceptExceptionase:print(f"Error loading model from local path:{e}")returnNone,None# 使用示例if__name__=="__main__":model_name="facebook/opt-1.3b"# 尝试下载模型tokenizer,model=download_model_with_retry(model_name)ifnottokenizerornotmodel:# 如果下载失败,尝试从本地加载print("\nAttempting to load from local path...")local_path="./opt-1.3b"tokenizer,model=load_model_from_local(local_path)iftokenizerandmodel:# 测试模型print("\nTesting model...")inputs=tokenizer("Hello, ",return_tensors="pt")outputs=model.generate(**inputs,max_new_tokens=20)print(f"Generated text:{tokenizer.decode(outputs[0],skip_special_tokens=True)}")else:print("Failed to load model.")A: 常见原因包括网络中断、磁盘空间不足、权限问题或服务器端问题。
A: 可以通过文件大小与 Hugging Face Hub 上显示的大小进行比较,或尝试加载模型看是否成功。
A: 清理整个 Hugging Face 缓存会删除所有已下载的模型,需要重新下载。如果只清理特定模型的缓存,则只影响该模型。
A: 需要下载所有必要的文件,包括配置文件、分词器文件和所有模型权重文件(pytorch_model-*.bin)。
A: 可以使用force_download=True参数,或使用 git lfs 进行下载,这些方法通常有更好的错误处理和重试机制。
遇到OSError: Model file 'pytorch_model-00001-of-00003.bin' is corrupted or incomplete (unexpected EOF)错误时,主要需要:
force_download=True强制重新下载通过以上解决方案,大部分情况下都能成功解决模型文件损坏或不完整的问题,顺利加载所需的模型。