Huggingface 笔记:大模型(Gemma2B,Gemma 7B)部署+基本使用

news2024/10/5 20:28:22

1 部署

1.1 申请权限

在huggingface的gemma界面,点击“term”以申请gemma访问权限

https://huggingface.co/google/gemma-7b

然后接受条款

1.2 添加hugging对应的token

如果直接用gemma提供的代码,会出现如下问题:

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
model = AutoModelForCausalLM.from_pretrained("google/gemma-7b")

input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")

outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))

这时候就需要添加自己hugging的token了:

import os
os.environ["HF_TOKEN"] = '....'

token的位置在:

2 gemma 模型官方样例

2.0 gemma介绍

  • Gemma是Google推出的一系列轻量级、最先进的开放模型,基于创建Gemini模型的相同研究和技术构建。
  • 它们是文本到文本的、仅解码器的大型语言模型,提供英语版本,具有开放的权重、预训练的变体和指令调优的变体。
  • Gemma模型非常适合执行各种文本生成任务,包括问答、摘要和推理。它们相对较小的尺寸使得可以在资源有限的环境中部署,例如笔记本电脑、桌面电脑或您自己的云基础设施,使每个人都能获得最先进的AI模型,促进创新。

2.1 文本生成

2.1.1 CPU上执行

from transformers import AutoTokenizer, AutoModelForCausalLM
'''
AutoTokenizer用于加载预训练的分词器
AutoModelForCausalLM则用于加载预训练的因果语言模型(Causal Language Model),这种模型通常用于文本生成任务
'''

tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b",token='。。。')
#加载gemma-2b的预训练分词器
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b",token='。。。')
#加载gemma-2b的预训练语言生成模型
'''
使用其他几个进行文本续写,其他的地方是一样的,就这里加载的预训练模型不同:
"google/gemma-2b-it"
"google/gemma-7b"
"google/gemma-7b-it"
'''



input_text = "Write me a poem about Machine Learning."
#定义了要生成文本的初始输入
input_ids = tokenizer(input_text, return_tensors="pt")
#使用前面加载的分词器将input_text转换为模型可理解的数字表示【token id】
#return_tensors="pt"表明返回的是PyTorch张量格式。

outputs = model.generate(**input_ids)
#使用模型和转换后的输入input_ids来生成文本

print(tokenizer.decode(outputs[0]))
#将生成的文本令牌解码为人类可读的文本,并打印出来

 2.1.2 GPU上执行

多GPU

'''
前面的一样
'''
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto")

input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)

'''
后面的一样
'''

指定单GPU

'''
前面的一样
'''
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="cuda:0")

input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)

'''
后面的一样
'''

2.1.3 设置生成文本的长度

其他的不变(和2.1.1比),只修改outputs这一行

outputs = model.generate(**input_ids,max_length=100)

2.2 使用chat格式

目前gemma我没试出来同时放n个不同的chat怎么搞,目前只放了一个

2.2.1 模型部分

和文本生成相同,从预训练模型中导入一个分词器一个CausalLM

# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b-it", device_map="cuda:0")

2.2.2 获取prompt

chat=[
            {"role": "user", "content": "I am going to Paris, what should I see?"},
            {
                "role": "assistant",
                "content": """\
Paris, the capital of France, is known for its stunning architecture, art museums, historical landmarks, and romantic atmosphere. Here are some of the top attractions to see in Paris:
1. The Eiffel Tower: The iconic Eiffel Tower is one of the most recognizable landmarks in the world and offers breathtaking views of the city.
2. The Louvre Museum: The Louvre is one of the world's largest and most famous museums, housing an impressive collection of art and artifacts, including the Mona Lisa.
3. Notre-Dame Cathedral: This beautiful cathedral is one of the most famous landmarks in Paris and is known for its Gothic architecture and stunning stained glass windows.
These are just a few of the many attractions that Paris has to offer. With so much to see and do, it's no wonder that Paris is one of the most popular tourist destinations in the world.""",
            },
            {"role": "user", "content": "What is so great about #1?"},
        ]

prompt = tokenizer.apply_chat_template(chat, 
                                       tokenize=False,
                                       add_generation_prompt=True)
#tokenize=False:这个参数控制是否在应用模板之后对文本进行分词处理。False表示不进行分词处理

#add_generation_prompt=True:这个参数控制是否在处理后的文本中添加生成提示。
#True意味着会添加一个提示,这个提示通常用于指导模型进行下一步的文本生成
#添加的提示是:<start_of_turn>model

print(prompt)
'''
<bos><start_of_turn>user
I am going to Paris, what should I see?<end_of_turn>
<start_of_turn>model
Paris, the capital of France, is known for its stunning architecture, art museums, historical landmarks, and romantic atmosphere. Here are some of the top attractions to see in Paris:
1. The Eiffel Tower: The iconic Eiffel Tower is one of the most recognizable landmarks in the world and offers breathtaking views of the city.
2. The Louvre Museum: The Louvre is one of the world's largest and most famous museums, housing an impressive collection of art and artifacts, including the Mona Lisa.
3. Notre-Dame Cathedral: This beautiful cathedral is one of the most famous landmarks in Paris and is known for its Gothic architecture and stunning stained glass windows.
These are just a few of the many attractions that Paris has to offer. With so much to see and do, it's no wonder that Paris is one of the most popular tourist destinations in the world.<end_of_turn>
<start_of_turn>user
What is so great about #1?<end_of_turn>
<start_of_turn>model
'''

2.2.3 分词

inputs = tokenizer.encode(prompt, 
                          add_special_tokens=False, 
                          return_tensors="pt")
inputs
'''
tensor([[     2,    106,   1645,    108, 235285,   1144,   2319,    577,   7127,
         235269,   1212,   1412,    590,   1443, 235336,    107,    108,    106,
           2516,    108,  29437, 235269,    573,   6037,    576,   6081, 235269,
            603,   3836,    604,   1277,  24912,  16333, 235269,   3096,  52054,
         235269,  13457,  82625, 235269,    578,  23939,  13795, 235265,   5698,
            708,   1009,    576,    573,   2267,  39664,    577,   1443,    575,
           7127, 235292,    108, 235274, 235265,    714, 125957,  22643, 235292,
            714,  34829, 125957,  22643,    603,    974,    576,    573,   1546,
          93720,  82625,    575,    573,   2134,    578,   6952,  79202,   7651,
            576,    573,   3413, 235265,    108, 235284, 235265,    714,  91182,
           9850, 235292,    714,  91182,    603,    974,    576,    573,   2134,
         235303, 235256,  10155,    578,   1546,  10964,  52054, 235269,  12986,
            671,  20110,   5488,    576,   3096,    578,  51728, 235269,   3359,
            573,  37417,  25380, 235265,    108, 235304, 235265,  32370, 235290,
          76463,  41998, 235292,   1417,   4964,  57046,    603,    974,    576,
            573,   1546,  10964,  82625,    575,   7127,    578,    603,   3836,
            604,   1277,  60151,  16333,    578,  24912,  44835,   5570,  11273,
         235265,    108,   8652,    708,   1317,    476,   2619,    576,    573,
           1767,  39664,    674,   7127,    919,    577,   3255, 235265,   3279,
            712,   1683,    577,   1443,    578,    749, 235269,    665, 235303,
         235256,    793,   5144,    674,   7127,    603,    974,    576,    573,
           1546,   5876,  18408,  42333,    575,    573,   2134, 235265,    107,
            108,    106,   1645,    108,   1841,    603,    712,   1775,   1105,
           1700, 235274, 235336,    107,    108,    106,   2516,    108]])
'''

2.2.4 生成结果

和文本生成一样,也是model.generate

outputs = model.generate(input_ids=inputs.to(model.device), 
                         max_new_tokens=500)
print(tokenizer.decode(outputs[0]))
'''
<bos><start_of_turn>user
I am going to Paris, what should I see?<end_of_turn>
<start_of_turn>model
Paris, the capital of France, is known for its stunning architecture, art museums, historical landmarks, and romantic atmosphere. Here are some of the top attractions to see in Paris:
1. The Eiffel Tower: The iconic Eiffel Tower is one of the most recognizable landmarks in the world and offers breathtaking views of the city.
2. The Louvre Museum: The Louvre is one of the world's largest and most famous museums, housing an impressive collection of art and artifacts, including the Mona Lisa.
3. Notre-Dame Cathedral: This beautiful cathedral is one of the most famous landmarks in Paris and is known for its Gothic architecture and stunning stained glass windows.
These are just a few of the many attractions that Paris has to offer. With so much to see and do, it's no wonder that Paris is one of the most popular tourist destinations in the world.<end_of_turn>
<start_of_turn>user
What is so great about #1?<end_of_turn>
<start_of_turn>model
The Eiffel Tower is one of the most iconic landmarks in the world and offers breathtaking views of the city. It is a symbol of French engineering and architecture and is a must-see for any visitor to Paris.<eos>
'''

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1528344.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

邮箱验证码api接口申请流程?有哪些条件?

邮箱验证码API服务如何选择&#xff1f;怎么正确设置邮箱验证码&#xff1f; 邮箱验证码API接口在保障用户账号安全、提高用户体验方面发挥着至关重要的作用。AokSend将详细介绍邮箱验证码API接口的申请流程&#xff0c;帮助您顺利集成这一功能&#xff0c;增强应用的安全性。…

【ADF4351】使用FPGA进行SPI寄存器配置、使用FPGA计算各个频率的频点,ADF4351配置程序

简介 特性 输出频率范围&#xff1a;35 MHz至4,400 MHz 小数N分频频率合成器和整数N分频频率合成器 具有低相位噪声的VCO 可编程的1/2/4/8/16/32/64分频输出 典型抖动&#xff1a;0.3 ps rms EVM(典型值&#xff0c;2.1 GHz)&#xff1a; 0.4% 电源&#xff1a;3.0 V至3.6 V …

基于spring boot框架的发艺美发店管理系统

摘 要 系统根据现有的管理模块进行开发和扩展&#xff0c;采用面向对象的开发的思想和结构化的开发方法对发艺美发店管理的现状进行系统调查。采用结构化的分析设计&#xff0c;该方法要求结合一定的图表&#xff0c;在模块化的基础上进行系统的开发工作。在设计中采用“自下而…

算法详解——选择排序和冒泡排序

一、选择排序 选择排序算法的执行过程是这样的&#xff1a;首先&#xff0c;算法遍历整个列表以确定最小的元素&#xff0c;接着&#xff0c;这个最小的元素被置换到列表的开头&#xff0c;确保它被放置在其应有的有序位置上。接下来&#xff0c;从列表的第二个元素开始&#x…

Java进阶 Maven基础

资料格式 配置文件 com.itheima Java代码 Statement stat con.createStatement(); 示例 com.itheima 命令 mvn test - Maven简介 传统项目管理状态分析 Maven 是什么 Maven的本质是一个项目管理工具&#xff0c;将项目开发过程抽象成一个项目对象模型&#xff08;POM&…

如何使用phpStudy在Windows系统部署静态站点并实现无公网IP远程访问

文章目录 使用工具1. 本地搭建web网站1.1 下载phpstudy后解压并安装1.2 打开默认站点&#xff0c;测试1.3 下载静态演示站点1.4 打开站点根目录1.5 复制演示站点到站网根目录1.6 在浏览器中&#xff0c;查看演示效果。 2. 将本地web网站发布到公网2.1 安装cpolar内网穿透2.2 映…

文献速递:深度学习乳腺癌诊断---使用深度学习改善乳腺癌组织学分级

Title 题目 Improved breast cancer histological grading using deep learning 使用深度学习改善乳腺癌组织学分级 01 文献速递介绍 乳腺癌组织学分级是乳腺癌中一个确立的临床变量&#xff0c;它包括来自三个方面的信息&#xff0c;即小管形成程度、核多态性和有丝分裂计…

java数据结构与算法刷题-----LeetCode1005. K 次取反后最大化的数组和(这就不是简单题)

java数据结构与算法刷题目录&#xff08;剑指Offer、LeetCode、ACM&#xff09;-----主目录-----持续更新(进不去说明我没写完)&#xff1a;https://blog.csdn.net/grd_java/article/details/123063846 卷来卷去&#xff0c;把简单题都卷成中等题了 文章目录 1. 排序后从小到大…

免费录屏软件无水印推荐,录制视频更轻松(3款)

随着互联网技术的快速发展&#xff0c;录制屏幕成为人们日常生活中日益重要的需求。无论是制作教学视频、直播分享&#xff0c;还是录制游戏过程&#xff0c;一款好用且免费的录屏软件都是不可或缺的。然而&#xff0c;许多录屏软件在录制过程中会添加水印&#xff0c;影响了录…

电脑如何直接压缩图片?这几个方法帮你解决

在许多社交媒体平台上&#xff0c;上传照片时经常需要进行大小调整&#xff0c;这是因为较大的照片文件可能会占用更多的存储空间&#xff0c;并且在传输过程中需要更长的时间。通过图片压缩可以减小文件大小&#xff0c;提高上传速度&#xff0c;并节省存储空间&#xff0c;那…

大学老师不会告诉你的网安证书?

前言 在大学中&#xff0c;有很多安全专业的师傅们&#xff0c;一直有问&#xff1a; “计算机xxx级有用吗&#xff1f;” “软考初级有用吗&#xff1f;” “xxx资格证有用吗&#xff1f;” 甚至有一些来讲这些整数的&#xff0c;以“我们这个专业以后就业需要的证书....…

SpringCloud-注册中心

一、注册中心简介 1、服务治理 服务治理中的三个角色&#xff1a; 服务提供者&#xff1a;负责提供服务的实现和运行。服务提供者将服务注册到服务注册中心&#xff0c;并根据需要处理来自消费者的请求。&#xff08;暴露服务接口&#xff0c;供其他服务调用&#xff09;。 …

【Python】flask框架请求体数据,文件上传,请求头信息获取方式案例汇总

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…

新媒体代运营是什么意思?CloudNEO:新媒体代运营的领先之选

新媒体代运营是什么意思&#xff1f; 随着互联网的迅速发展和普及&#xff0c;新媒体已经成为了企业推广和营销的重要工具。在这个背景下&#xff0c;新媒体代运营应运而生&#xff0c;成为了企业实现品牌曝光、粉丝增长和内容传播的重要方式。那么&#xff0c;新媒体代运营到…

经纬恒润推出新一代快速控制原型产品 ControlBase_S

近年来&#xff0c;软件定义汽车的发展趋势在行业内已经达成共识&#xff0c;与此同时&#xff0c;产品越来越复杂&#xff0c;开发周期越来越短&#xff0c;给软件开发带来了极大的挑战。在软硬件解耦的背景下&#xff0c;如何提前进行软件架构、算法开发及验证&#xff0c;成…

LeetCode 热题 100 | 回溯(三)

目录 1 131. 分割回文串 2 51. N 皇后 菜鸟做题&#xff0c;语言是 C&#xff0c;感冒好了 ver. 1 131. 分割回文串 题眼&#xff1a;给你一个字符串 s&#xff0c;请你将 s 分割 成一些子串。 根据题眼可知&#xff0c;我们需要做的是将字符串 s 连续分割 为几段&#…

深度观察2024中国系统架构师大会(SACC)

今年的中国系统架构师大会&#xff08;SACC&#xff09;在我所在的城市广州举办&#xff0c;很荣幸受邀参加。这次能接触到国内最优秀的架构师&#xff0c;学习他们的架构思想和行业经验。对我而言非常有意义。 大会分为上下午共4场&#xff0c;我参加了上午的多云多活架构设计…

Altair® Activate® 多学科系统仿真

Altair Activate 多学科系统仿真 Altair Activate 是一个开放且灵活的集成平台&#xff0c;用于系统仿真综合系统。Altair Activate 基于针对信号块、面向对象的物理组件、电气和电子系统的混合框图建模环境&#xff0c;支持在整个开发周期中进行多物理场分析。 全面支持 数学…

目标检测——PP-YOLOE算法解读

PP-YOLO系列&#xff0c;均是基于百度自研PaddlePaddle深度学习框架发布的算法&#xff0c;2020年基于YOLOv3改进发布PP-YOLO&#xff0c;2021年发布PP-YOLOv2和移动端检测算法PP-PicoDet&#xff0c;2022年发布PP-YOLOE和PP-YOLOE-R。由于均是一个系列&#xff0c;所以放一起解…

针对元宇宙概念的兴起,普通人能不能参与?

元宇宙是一种虚拟现实空间&#xff0c;由数字技术构建&#xff0c;让人们可以在其中进行交互和沟通。通俗点说&#xff0c;元宇宙就像是一个类似于《黑镜》剧集中的“星空游乐园”的虚拟世界&#xff0c;我们可以在其中自由探索、玩乐、交互、创造。 在元宇宙中&#xff0c;我们…