大模型之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并对外提供服务。

相关文章
|
人工智能 开发者
解决HuggingFace模型下载难题:AI快站的高效加速服务
在AI研发领域,获取优质模型资源是关键。国内开发者常因海外服务器导致的下载困难而苦恼,尤其是大型模型下载更是耗时且充满挑战。AI快站(aifasthub.com)作为huggingface镜像网站,提供了高效的加速下载服务。它不仅解决了huggingface大模型下载的速度问题,还支持断点续传,保证下载连续性。此外,AI快站还提供全面及时更新的模型资源,是国内AI开发者的理想选择。
1445 0
|
9月前
您可以使用验证集来评估微调后的模型效果
【1月更文挑战第16天】【1月更文挑战第78篇】您可以使用验证集来评估微调后的模型效果
204 6
|
机器学习/深度学习 人工智能 搜索推荐
LangChain入门指南
LangChain入门指南
2614 0
|
9月前
|
PyTorch 算法框架/工具 Python
pytorch安装教程
pytorch安装教程
171 0
|
4月前
|
存储 人工智能 安全
LangChain-23 Vector stores 向量化存储 并附带一个实际案例 通过Loader加载 Embedding后持久化 LangChain ChatOpenAI ChatGLM3对话
LangChain-23 Vector stores 向量化存储 并附带一个实际案例 通过Loader加载 Embedding后持久化 LangChain ChatOpenAI ChatGLM3对话
156 0
|
机器学习/深度学习 开发工具 git
开发专题 | 1 :下载 huggingface 上模型的正确姿势
本文主要介绍如何以正确的方式下载 huggingface 上的模型
|
9月前
|
人工智能 Python
huggingface_hub加速
huggingface_hub加速
230 0
|
Linux 开发工具 git
阿里云dsw实例git clone Hugging Face
因为网络及python包版本的原因,dsw实例在使用git指令下载hugging face资源的时候,总是会出现这样或那样的问题,本文基于实际测试遇到的情况,给出对应的解决方案。
4072 1
|
9月前
|
机器学习/深度学习 人工智能 自然语言处理
使用Huggingface创建大语言模型RLHF训练流程的完整教程
ChatGPT已经成为家喻户晓的名字,而大语言模型在ChatGPT刺激下也得到了快速发展,这使得我们可以基于这些技术来改进我们的业务。
396 2
|
9月前
|
开发工具 git
阿里云部署 ChatGLM2-6B 与 langchain+chatGLM
阿里云部署 ChatGLM2-6B 与 langchain+chatGLM
516 1

热门文章

最新文章