from diffusers import AutoencoderKL
vae = AutoencoderKL.from_pretrained(
"your local model cache",
torch_dtype=torch.float16,
local_files_only=True # 使用这个来强制读取本地模型
)
使用上述代码读取模型后,模型加载时间异常,使用ctrl+c中断后,发现终止在socket连接处。由于中国ip访问huggingface有问题,想试下能不能使用镜像网站。
将下述代码添加后仍然无效,逐行分析代码寻找原因:
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
发现问题出在读取模型时,在diffuser包中的model_utils.py中使用了send_telemetry函数来验证模型结构,对huggingface发送了请求。具体调用如下:
send_telemetry(
{"model_class": cls.__name__, "model_path": "local", "framework": "pytorch"},
name="diffusers_from_pretrained",
)
send_telemetry的具体实现在hub_utils.py中,具体如下:
def send_telemetry(data: Dict, name: str):
"""
Sends logs to the Hub telemetry endpoint.
Args:
data: the fields to track, e.g. {"example_name": "dreambooth"}
name: a unique name to differentiate the telemetry logs, e.g. "diffusers_examples" or "diffusers_notebooks"
"""
if DISABLE_TELEMETRY or HF_HUB_OFFLINE:
pass
headers = {"user-agent": http_user_agent(data)}
endpoint = HUGGINGFACE_CO_TELEMETRY + name
try:
r = requests.head(endpoint, headers=headers)
r.raise_for_status()
except Exception:
# We don't want to error in case of connection errors of any kind.
pass
可以发现放松请求的地址来自于endpoint这个变量,点击HUGGINGFACE_CO_TELEMETRY溯源,发现最终来自diffuser包中的utils中的__init__.py中的HUGGINGFACE_CO_RESOLVE_ENDPOINT
将其修改为
# HUGGINGFACE_CO_RESOLVE_ENDPOINT = "https://huggingface.co"
HUGGINGFACE_CO_RESOLVE_ENDPOINT="https://hf-mirror.com"
即可直接访问国内的镜像网站,问题解决。
1万+

被折叠的 条评论
为什么被折叠?



