Unsloth是否支持模型缓存目录?如何从本地硬盘加载模型?
Unsloth模型缓存与本地加载方法
Unsloth完全兼容Hugging Face的模型缓存机制,默认会复用HF Transformers的本地缓存目录(通常为~/.cache/huggingface/hub)。你遇到重复下载的问题,大概率是未明确指定缓存目录,或是缓存目录存在权限、路径异常。
一、指定自定义模型缓存目录
调用FastLanguageModel.from_pretrained时,通过cache_dir参数指定本地缓存路径,模型会下载并保存到该目录,后续调用将直接读取本地缓存:
from unsloth import FastLanguageModel max_seq_length = 16384 model, tokenizer = FastLanguageModel.from_pretrained( model_name="unsloth/Llama-3.2-1B-Instruct", max_seq_length=max_seq_length, load_in_4bit=False, cache_dir="/your/custom/cache/path" # 替换为你的目标缓存目录 )
二、从本地硬盘直接加载已下载的模型
若已手动将模型文件下载到本地目录(比如./local-llama-3.2-1b),只需将model_name参数替换为本地路径即可:
from unsloth import FastLanguageModel max_seq_length = 16384 model, tokenizer = FastLanguageModel.from_pretrained( model_name="./local-llama-3.2-1b", # 本地模型目录的绝对/相对路径 max_seq_length=max_seq_length, load_in_4bit=False, )
注意:本地目录需包含模型全部必要文件(如config.json、model.safetensors、tokenizer.json等),目录结构要与Hugging Face Hub上的模型保持一致。
内容的提问来源于stack exchange,提问作者Matt
相关产品推荐
相关产品推荐

