Linux服务器下载Huggingface模型

4,394 阅读2分钟

起源

由于无法正常访问(2023-10)huggingface.co, 为了在Linux上下载预训练模型,尝试的种种办法

如何看待huggingface.co已无法访问? - 知乎

加载模型时报错如下:

'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下载文件。

  1. 进入镜像网站搜索自己想要的模型
  2. 点击 Files and versions
  3. 右键鼠标,选择复制链接
  4. 到Linux中用wget下载 wget https://xxxxxxx
  5. 需要下载的文件:
  • config.json
  • tokenizer_config.json
    • 我的没有这个文件,下载了merges.txt,反正文件小下载很快
  • tokenizer.json
  • vocab.txt
  • pytorch_mode.bin or tf_model.h5
    • 按照错误提示若是.h5则需要加参数from_tf=True
    • 亲测pytorch_mode.bin即可
image.png

下面的四种方式转自:

作者:江流儿的NLP

文章地址:zhuanlan.zhihu.com/p/662017962

1、Hugging face.cn上手动下载模型及其文件,需科学上网
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网站下载,国内可访问,非开发者需手动下载
# 开发者可以直接看这里,这里的操作是在你已经有了一个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

大模型下载的四种方式