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

相关文章
|
Python
python使用pip镜像源加速安装包(清华、阿里、中科大)
python使用pip镜像源加速安装包(清华、阿里、中科大)
10574 0
python使用pip镜像源加速安装包(清华、阿里、中科大)
后端返回base64格式数据转excel格式文件并下载
后端返回base64格式数据转excel格式文件并下载
943 0
|
6月前
|
JSON 人工智能 测试技术
我如何用Skills+Postman,让接口测试用例自动生成、自动维护,半年零手工更新
本文揭秘如何用Postman+大模型Skills实现接口测试用例“零手工维护”:通过自动感知OpenAPI变更、智能生成并应用Collection补丁、Git化管理+CI闭环验证,6个月未手动增删改用例。核心不是生成用例,而是让用例随代码自动同步。
|
Windows
windows错误0x80070057?各种安装软件时出现参数错误0x80070057解决办法
本文介绍了Windows系统中常见的错误代码0x80070057的解决方法。该错误通常出现在安装IIS、Edge浏览器、系统更新或文件复制等操作中。文章推荐使用FixWin 11这款修复工具,帮助用户快速解决此类问题。工具支持多种Windows系统问题修复,操作简单,适合普通用户使用。
1559 0
|
算法 测试技术 调度
【调度算法】DTLZ问题家族
【调度算法】DTLZ问题家族
882 1
|
缓存 监控 安全
80_离线环境搭建:无互联网LLM推理
在当今大语言模型(LLM)蓬勃发展的时代,许多组织和个人面临着一个共同的挑战:如何在无互联网连接的环境中高效部署和使用LLM?这一需求源于多方面的考量,包括数据安全、隐私保护、网络限制、极端环境作业等。2025年,随着企业对数据主权意识的增强和边缘计算的普及,离线LLM部署已成为AI应用落地的关键场景之一。
1186 0
|
自然语言处理 物联网 API
使用CAMEL和Unsloth进行数据生成与Qwen模型微调
本项目结合CAMEL和Unsloth,生成高质量训练数据并对Qwen 7B模型进行微调,提升其在特定内容上的理解和生成能力。我们使用CAMEL生成指令-输入-输出三元组数据,并通过Unsloth的LoRA技术加速微调过程。详细步骤包括环境准备、API密钥设置、模型加载与配置、数据生成与保存、模型训练及推理。最终,微调后的Qwen 7B模型能更好地处理CAMEL社区相关文本。 更多详情请参考: - [CAMEL GitHub](https://github.com/camel-ai/camel)
|
缓存 安全 Shell
Git - 新建分支及提交代码到分支到远程仓库
Git - 新建分支及提交代码到分支到远程仓库
2166 0
|
编解码 Ubuntu 前端开发
我的NVIDIA开发者之旅——作为一名初学者,我是如何开启 NVIDIA Jetson Nano 开发的
在本文中,我将展示如何从一个初学者角度,使用 NVIDIA Jetson Nano 。 你也可以参考官方教程。 如果你想跟着我一起做,那么接下来的内容比会比较花费时间,需要你耐心跟着往下做。😎
2371 103
我的NVIDIA开发者之旅——作为一名初学者,我是如何开启 NVIDIA Jetson Nano 开发的
|
网络安全 开发工具 git
Mac安装Git
Mac安装Git
1008 2