起源
由于无法正常访问(2023-10)huggingface.co, 为了在Linux上下载预训练模型,尝试的种种办法
加载模型时报错如下:
'HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /facebook/bart-base/resolve/main/config.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f4d066bb160>, 'Connection to huggingface.co timed out. (connect timeout=10)'))' thrown while requesting HEAD huggingface.co/facebook/ba…
解决方案
模型下载+本地缓存加载
模型下载的四种方式
提前说明,我用的是第4种,从镜像网站找到模型,获取下载链接,然后在Linux中用wget下载文件。
- 进入镜像网站搜索自己想要的模型
- 点击 Files and versions
- 右键鼠标,选择复制链接
- 到Linux中用wget下载
wget https://xxxxxxx - 需要下载的文件:
config.jsontokenizer_config.json- 我的没有这个文件,下载了merges.txt,反正文件小下载很快
tokenizer.jsonvocab.txtpytorch_mode.binortf_model.h5- 按照错误提示若是.h5则需要加参数
from_tf=True - 亲测pytorch_mode.bin即可
- 按照错误提示若是.h5则需要加参数
下面的四种方式转自:
作者:江流儿的NLP
1、Hugging face.cn上手动下载模型及其文件,需科学上网
- 因为外网下载,所以速度依赖于你的外网网速
- huggingface.co/baichuan-in…
2、阿里的modelscope上下载,国内可访问,如果你的网速还行,建议这种方式
- 首先需要安装modelscope :
pip install modelscope
# 从modelscope上下载模型
from modelscope.hub.snapshot_download import snapshot_download
model_dir = snapshot_download('baichuan-inc/baichuan-7B', cache_dir='./model', revision='master')
如果你网速很好,下载就很快,如果是kb/s,那么大文件下载会失败。
3、huggingface的镜像网站aliendao网站下载,国内可访问,非开发者需手动下载
- 互链高科 (非开发者或者怕麻烦可以直接去网站手动下载)
- 开发者在aliendao的下载器上下载GitHub - git-cloner/aliendao: huggingface mirror download
# 开发者可以直接看这里,这里的操作是在你已经有了一个python3.7以上的环境下,可以直接下述操作
# 如果你没有的话,移步 https://github.com/git-cloner/aliendao
git clone https://github.com/git-cloner/aliendao
cd aliendao
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host=pypi.mirrors.ustc.edu.cn
# 带上mirror参数,优先从aliendao.cn镜像下载
python model_download.py --mirror --repo_id baichuan-inc/Baichuan2-13B-Chat-4bits
- 让公司的运维测了,aliendao的带宽很慢,即便你的网速很快,速度也非常一般,而且这个网站模型不全。
4、huggingface的镜像网站
- hf-mirror.com
- 网速很快,公司运维有测到4M/s,非常推荐用这个网站直接下载
本地加载
MODEL_PATH = '/xxx/xxx/models/bart-base'
tokenizer = BartTokenizerFast.from_pretrained(MODEL_PATH)
给服务器安装墙墙软件
试了,能够开启proxy,但仍旧无法下载,可自行尝试
我使用的是clash:github.com/wanhebin/cl…
参考:
discuss.huggingface.co/t/download-…
Load a pre-trained model from disk with Huggingface Transformers - Stack Overflow