大模型之Huggingface初体验

简介: huggingface相关环境的安装和问题处理本篇暂不涉及,后续补充。这里以一个模型为例,完成从模型介绍到加载、运行的完整过程,作为我们熟悉huggingface的一个示例。

一 背景

   huggingface相关环境的安装和问题处理本篇暂不涉及,后续补充。这里以一个模型为例,完成从模型介绍到加载、运行的完整过程,作为我们熟悉huggingface的一个示例。

二 模型

   这里选择google/pegasus-newsroom模型作为示例。

2.1 介绍

   模型介绍参见https://huggingface.co/docs/transformers/main/model_doc/pegasus,模型是在论文《PEGASUS: Pre-training with Extracted Gap-sentences forAbstractive Summarization》中提出的,作者:Jingqing Zhang。基本思想是,PEGASUS在预训练阶段,将输入的文档的重要句子remove/mask,通过其它的句子预测生成,类似于摘要生成的做法。

2.2 使用示例

https://huggingface.co/google/pegasus-newsroom/tree/main

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("google/pegasus-newsroom")
model = AutoModelForSeq2SeqLM.from_pretrained("google/pegasus-newsroom")

2.3 遇到问题

   按理说应该可以顺利执行,但实际上不出意外地遇到了意外。在执行时还是报错提示无法加载模型,信息如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/onnx/tutorial-env/lib/python3.10/site-packages/transformers/models/auto/tokenization_auto.py", line 709, in from_pretrained
    return tokenizer_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
  File "/root/onnx/tutorial-env/lib/python3.10/site-packages/transformers/tokenization_utils_base.py", line 1809, in from_pretrained
    raise EnvironmentError(
OSError: Can't load tokenizer for 'google/pegasus-newsroom'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'google/pegasus-newsroom' is the correct path to a directory containing all relevant files for a PegasusTokenizerFast tokenizer.

在最后一行,OSError这段,给出了两种错误可能的提示:

(1)确保本地没有同名目录

这一点显然,从来都没有创建过这个目录;

(2)确认'google/pegasus-newsroom'是一个包含所有相关文件的正确目录

这是从huggingface官网上复制过来的代码,不可能会出错。那问题出在哪里了?

三 问题排查

3.1 SSH拉取模型文件

通过资料搜搜,和huggingface官网的模型页面查看,发现如下:

可以通过git拉取模型文件

不过执行后有如下报错:

所以改为使用SSH方式:

报了权限错误,不过还好,看到publickey的提示,应该是设置一下访问授权就可以了。

git clone git@hf.co:google/pegasus-newsroom
正克隆到 'pegasus-newsroom'...
Warning: Permanently added the ECDSA host key for IP address '3.210.66.237' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

3.2 ssh key生成与添加

   在https://huggingface.co/docs/hub/security-git-ssh 中有相关的操作描述(当然在实际操作中发现也有坑。。。),简单整理如下:

1、检查是否存在SSH key,由于是linux系统,所以默认是在~/.ssh目录下。由于我们之前没有生成过,所以没有(有也没关系,直接覆盖生成就好)

  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

2、如果没有,那么先生成,使用ssh-keygen命令,引号内是你注册huggingface时使用的邮箱:

ssh-keygen -t ed25519 -C "your.email@example.co"

3、生成完毕后,使用ssh-add命令加入到你的SSH agent中:

ssh-add ~/.ssh/id_ed25519

在第三步可能会遇到报错,例如我本地执行时错误如下:

Could not open a connection to your authentication agent.

无法正常添加,这种情况需要先执行ssh-agent bash,然后再次执行ssh-add 添加即可。

接下来就可以拉模型文件了:

git clone git@hf.co:google/pegasus-newsroom
正克隆到 'pegasus-newsroom'...
remote: Enumerating objects: 33, done.
remote: Total 33 (delta 0), reused 0 (delta 0), pack-reused 33
接收对象中: 100% (33/33), 931.72 KiB | 604.00 KiB/s, done.
处理 delta 中: 100% (12/12), done.
Downloading pytorch_model.bin (2.3 GB)

下载成功。

   不过跟huggingface的描述相比,还有有个地方有些问题。按照huggingface的文档描述,ssh-add 添加id_ed25519成功后,在终端执行ssh -T git@hf.co 命令,应该能看到包含你用户名的提示信息。但如上所述,我已经成功添加,并且可以拉取模型文件了,在终端执行命令后还是只有: “Hi anonymous, welcome to Hugging Face.”,按照文档描述这应该是失败的状态。这里暂时没有解决,留待后续继续排查。

四 继续运行模型

4.1 网络问题

   回过头来,我们继续尝试对google/pegasus-newsroom的尝试。依次执行命令如下:

from transformers import AutoTokenizer, PegasusModel
tokenizer = AutoTokenizer.from_pretrained("google/pegasus-large")
model = PegasusModel.from_pretrained("google/pegasus-large")
inputs = tokenizer("Studies have been shown that owning a dog is good for you", return_tensors="pt")
decoder_inputs = tokenizer("Studies show that", return_tensors="pt")
outputs = model(input_ids=inputs.input_ids, decoder_input_ids=decoder_inputs.input_ids)
last_hidden_states = outputs.last_hidden_state
list(last_hidden_states.shape)

执行成功。

   不过我们重复执行时,发现这里还有个问题,执行:model = PegasusModel.from_pretrained("google/pegasus-large") 时,依然会报连接失败的错误,而且失败的概率还比较大,所以依然需要继续解决。不过这个稍微分析一下,就知道是国内众所周知的“网络环境”问题,如果可以“科学上网”,那么就可以解决。不过相信也有很多小伙伴不具备这样的环境,或者风险较大,所以需要考虑采用其他更合法的方式。

4.2 离线模式

   官网和其他可搜到的资料,基本都推荐采用离线模式。也就是把模型通过git或者手工下载再上传到服务器的指定目录,然后修改执行脚本从本地加载的方式。

   由于上面我们已经完成了ssh的配置,并且可以git clone拉取模型文件,所以就直接加载已经拉下来的模型,脚本如下:

>>> from transformers import AutoTokenizer, AutoModelForMaskedLM
>>> tokenizer = AutoTokenizer.from_pretrained("/root/onnx/model/huggingface/pegasus-newsroom")
>>> 
>>> from transformers import AutoTokenizer, AutoModel
>>> model = AutoModel.from_pretrained("/root/onnx/model/huggingface/pegasus-newsroom")
Some weights of the model checkpoint at /root/onnx/model/huggingface/pegasus-newsroom were not used when initializing PegasusModel: ['final_logits_bias']
- This IS expected if you are initializing PegasusModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing PegasusModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of PegasusModel were not initialized from the model checkpoint at /root/onnx/model/huggingface/pegasus-newsroom and are newly initialized: ['model.encoder.embed_positions.weight', 'model.decoder.embed_positions.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
>>> 
>>> model = model.eval()

   到这里,算是跑通了整个运行流程。

五 后续

   接下来,将继续验证huggingface转onnx,和加载onnx并对外提供服务。

相关文章
|
16天前
|
自然语言处理 前端开发 Swift
Llama3 中文通用Agent微调模型来啦!(附手把手微调实战教程)
Llama3模型在4月18日公布后,国内开发者对Llama3模型进行了很多训练和适配,除了中文纯文本模型外,多模态版本也陆续在发布中。
|
11月前
|
存储 机器学习/深度学习 人工智能
本地部署开源大模型的完整教程:LangChain + Streamlit+ Llama
在过去的几个月里,大型语言模型(llm)获得了极大的关注,这些模型创造了令人兴奋的前景,特别是对于从事聊天机器人、个人助理和内容创作的开发人员。
6600 1
|
11月前
|
机器学习/深度学习 开发工具 git
开发专题 | 1 :下载 huggingface 上模型的正确姿势
本文主要介绍如何以正确的方式下载 huggingface 上的模型
|
12月前
|
机器学习/深度学习 Shell API
Gradio机器学习模型快速部署工具【quickstart】翻译1
Gradio机器学习模型快速部署工具【quickstart】翻译1
260 0
|
12月前
|
机器学习/深度学习 API Python
Gradio机器学习模型快速部署工具【quickstart】翻译2
Gradio机器学习模型快速部署工具【quickstart】翻译2
225 0
|
12月前
|
机器学习/深度学习 Web App开发 缓存
Gradio机器学习模型快速部署工具【应用分享】翻译4
Gradio机器学习模型快速部署工具【应用分享】翻译4
737 0
|
12月前
|
机器学习/深度学习 存储 前端开发
Gradio机器学习模型快速部署工具【应用分享】翻译3
Gradio机器学习模型快速部署工具【应用分享】翻译3
518 0
|
12月前
|
机器学习/深度学习 达摩院 Shell
如何使用魔搭ModelScope快速定制一款对长文本进行理解的模型?
‍本文详细介绍PoNet模型的原理以及其在ModelScope上的体验用法,包括PoNet简介原理、PoNet在ModelScope上怎么用、PoNet在各种下游任务上的效果等。
385 0
|
12月前
|
人工智能 达摩院 Linux
如何使用ModelScope魔搭开源代码训练一款语音合成模型
如何使用ModelScope魔搭开源代码训练一款语音合成模型
882 0
|
人工智能 开发者
ModelScope开源模型
ModelScope开源模型