模型下载加速(Hugging Face 镜像)
在不同智算中心,设置 Hugging Face 镜像站点可显著加速模型下载。本页给出按区域配置 HF_ENDPOINT 的环境变量与三种下载方式。
注意
智算中心地址可能随智算中心建设发生变化,请以实际获取的智算中心信息为准。
设置HF_ENDPOINT环境变量
根据所在智算中心选择对应镜像站点。镜像站缓存了主流开源模型。
| 智算中心 | Linux(bash) | Windows(PowerShell) |
|---|---|---|
| 北京一区 | export HF_ENDPOINT=http://hfmirror.mas.zetyun.cn:8082 | $env:HF_ENDPOINT = "http://hfmirror.mas.zetyun.cn:8082" |
| 北京二区 | export HF_ENDPOINT=http://hfmirrora01.hd-02.zetyun.cn:8082 | $env:HF_ENDPOINT = "http://hfmirrora01.hd-02.zetyun.cn:8082" |
| 北京三区 | export HF_ENDPOINT=http://hfmirror-1.hd-03.zetyun.cn:8082 | $env:HF_ENDPOINT = "http://hfmirror-1.hd-03.zetyun.cn:8082" |
| 北京五区 | export HF_ENDPOINT=http://hfmirror.hd-04.zetyun.cn:8082 | $env:HF_ENDPOINT = "http://hfmirror.hd-04.zetyun.cn:8082" |
各区域镜像地址仅域名前缀不同,端口统一为8082,HF_ENDPOINT填写含http://与端口的完整地址。请选择与实例所在智算中心一致的地址,不要跨区使用。
查看缓存模型列表
通过curl查看各区域已缓存的模型清单:
# 北京一区
curl http://hfmirror.mas.zetyun.cn:8082/repos
# 北京二区
curl http://hfmirrora01.hd-02.zetyun.cn:8082/repos
# 北京三区
curl http://hfmirror-1.hd-03.zetyun.cn:8082/repos
# 北京五区
curl http://hfmirror.hd-04.zetyun.cn:8082/repos说明
如果模型列表中没有所需模型,可通过官网在线咨询申请加入缓存。
下载模型
先安装最新的huggingface_hub:
pip install -U huggingface_hub使用snapshot_download下载模型
snapshot_download()可将指定模型仓库的文件下载到本地目录:
from huggingface_hub import snapshot_download
import os
os.environ["HF_ENDPOINT"] = "http://hfmirror-1.hd-03.zetyun.cn:8082"
snapshot_download(
repo_id="Qwen/Qwen-7B",
repo_type="model",
local_dir="./model_dir",
resume_download=True,
max_workers=8,
)使用huggingface cli下载模型
以下示例在Windows系统下载Qwen2.5系列模型:
hf download --% Qwen/Qwen2.5-1.5B-Instruct --local-dir Qwen/Qwen2.5-1.5B-Instruct
hf download --% Qwen/Qwen2.5-14B-Instruct --local-dir Qwen/Qwen2.5-14B-Instruct使用huggingface示例代码下载模型
使用示例代码时,需要将模型缓存路径修改为持久化的PVC路径。例如:
export HF_HOME=/mnt/models配置完成后,可使用如下代码加载模型并直接运行,模型会在首次运行时自动下载:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto",
proxies={"http://": "hfmirror-1.hd-03.zetyun.cn:8082"},
)
tokenizer = AutoTokenizer.from_pretrained(
model_name,
proxies={"http://": "hfmirror-1.hd-03.zetyun.cn:8082"},
)
prompt = "write a quick sort algorithm."
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt},
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=512,
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)最后更新于
这篇文档对你有帮助吗?
