当前位置: 首页 > news >正文

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

1 部署

1.1 申请权限

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

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

然后接受条款

1.2 添加hugging对应的token

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

from transformers import AutoTokenizer, AutoModelForCausalLMtokenizer = 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, AutoModelForCausalLMtokenizer = 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>modelprint(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>
'''

相关文章:

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

1 部署 1.1 申请权限 在huggingface的gemma界面&#xff0c;点击“term”以申请gemma访问权限 https://huggingface.co/google/gemma-7b 然后接受条款 1.2 添加hugging对应的token 如果直接用gemma提供的代码&#xff0c;会出现如下问题&#xff1a; from transformers i…...

WebGL 理论基础 01 WebGL 基础概念

WebGL 理论基础 基础概念 WebGL 基础概念 顶点着色器的作用是计算顶点的位置。根据计算出的一系列顶点位置&#xff0c;WebGL可以对点&#xff0c; 线和三角形在内的一些图元进行光栅化处理。当对这些图元进行光栅化处理时需要使用片段着色器方法。 片段着色器的作用是计算…...

Leetcode 28:找出字符串中第一个匹配项的下标

给你两个字符串 haystack 和 needle &#xff0c;请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标&#xff08;下标从 0 开始&#xff09;。如果 needle 不是 haystack 的一部分&#xff0c;则返回 -1 。 示例 1&#xff1a; 输入&#xff1a;haystack &q…...

docker opensearch arm64 运行失败解决方案

opensearch版本 2.1.0 docker日志错误信息&#xff1a; Disabling execution of install_demo_configuration.sh for OpenSearch Security Plugin Enabling OpenSearch Security Plugin Killing opensearch process 10 OpenSearch exited with code 143 Performance analyze…...

C#、ASP、ASP.NET、.NET、ASP.NET CORE区别、ASP.NET Core其概念和特点、ASP.NET Core个人心得体会

C#是一种面向对象的编程语言&#xff0c;主要用于开发跨平台的应用程序。它是.NET框架的一部分&#xff0c;并且可以在.NET平台上运行。 ASP&#xff08;Active Server Pages&#xff09;是一种用于构建动态Web页面的技术&#xff0c;使用VBScript或JScript作为服务器端脚本语…...

SpringMVC 简介及入门级的快速搭建详细步骤

MVC 回顾 MVC&#xff0c;即Model-View-Controller&#xff08;模型-视图-控制器&#xff09;设计模式&#xff0c;是一种广泛应用于软件工程中&#xff0c;特别是Web应用开发中的架构模式。它将应用程序分为三个核心组件&#xff1a; Model&#xff08;模型&#xff09;&#…...

Flutter编译卡在Running Gradle task ‘assembleDebug

1、翻墙 2、修改国内镜像源&#xff08;以下以Flutter 3.19.3版本为例&#xff09; 找到Flutter SDK目录下的Flutter配置文件resolve_dependencies.gradle 路径&#xff1a;flutter/packages/flutter_tools/gradle/resolve_dependencies.gradle 1)、第一处修改&#xff1a; g…...

基于springboot的牙科就诊管理系统

技术&#xff1a;springbootmysqlvue 一、系统背景 当前社会各行业领域竞争压力非常大&#xff0c;随着当前时代的信息化&#xff0c;科学化发展&#xff0c;让社会各行业领域都争相使用新的信息技术&#xff0c;对行业内的各种相关数据进行科学化&#xff0c;规范化管理。这样…...

C语言 指针练习

一、 a、b是两个浮点型变量&#xff0c;给a、b赋值&#xff0c;建立两个指针分别指向a的地址和b的地址&#xff0c;输出两个指针的值。 #include<stdio.h> int main() {float a,b,*p1,*p2;a10.2;b2.3;p1&a;p2&b;printf("a%f,b%f\n",a,b);printf("…...

【力扣 TOP100】 无重复字符的最长子串

题目描述&#xff1a; 思路&#xff1a; 使用left和right表示子串的端点。每次判断新的right是否在之前的子串里&#xff0c;如果在&#xff0c;则将left更新为新字符在子串里的位置&#xff08;因为在此之间&#xff0c;没有更长的子串了&#xff09;。如果不在则right1&…...

K8S node磁盘清理

K8S磁盘清理 K8S的部署形式相比传统非容器部署&#xff0c;会消耗更多的磁盘&#xff0c;在运行时可能会把磁盘占满。 这里以使用containerd运行时的K8S node为例&#xff0c;说明磁盘会用到那里了和如何清理磁盘 通用处理 磁盘清理: du -h --max-depth6 / 2>/dev/nul…...

2024年上半年软考,现在开始学真的来得及吗?

24上软考报名进行时&#xff0c;如果从现在开始学习来得及吗&#xff1f;只为拿证&#xff0c;还没报名的选哪科通过率高一点呢&#xff1f; 01、现在开始学来得及吗&#xff1f; 还没开始备考的考生&#xff0c;现在开始抓紧时间学还来得及&#xff0c;但是要正视软考的试题…...

SfM——八点法计算F矩阵(基础矩阵)与三角测量

1 八点法计算F矩阵&#xff08;基础矩阵&#xff09; 基础矩阵用于描述两个视图之间的几何关系 基础矩阵&#xff1a;基础矩阵 F F F 是描述两个视图之间相机投影关系的矩阵。对于两个对应的图像坐标点 ( x , y , 1 ) (x, y, 1) (x,y,1) 和 ( u , v , 1 ) (u, v, 1) (u,v,1…...

分布式事务的解决方案--Seata架构

一、Seata的XA模式 二、AT模式原理 三、TCC模式原理 四、MQ分布式事务 异步&#xff0c;非实时&#xff0c;实现最终的一致性。 四、分布式事务的解决方案...

【 React 】React JSX 转换成真实DOM的过程?

1. 是什么 react通过将组件编写的JSX映射到屏幕&#xff0c;以及组件中的状态发生了变化之后React会将这些「变化」更新到屏幕上 在前面文章了解中&#xff0c;JSX通过babel最终转化成React.createElement这种形式&#xff0c;例如&#xff1a; <div>< img src"…...

[Open3d]: 知识记录

python api 官方手册&#xff1a;http://www.open3d.org/docs/release/ 可视化&#xff1a;http://www.open3d.org/docs/release/tutorial/visualization/visualization.html python-vis 参考代码&#xff1a;https://github.com/isl-org/Open3D/tree/master/examples/python/v…...

css面试题

1、css盒模型 a、标准盒模型---在标准盒模型中&#xff0c;width的宽度指的是content的宽度 b、怪异盒模型---在怪异盒模型中&#xff0c;width的宽度等于contentborderpadding 切换盒子模型的话&#xff0c;使用box-sizing。 2、link和import的区别 a、link是html标签&#x…...

vscode调试launch.json常用格式

1、简单的模版 定义一个简单的模版如下&#xff1a; {// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息&#xff0c;请访问: https://go.microsoft.com/fwlink/?linkid830387"version": "0.2.0","configuration…...

巨细!Python爬虫详解

爬虫&#xff08;又称为网页蜘蛛&#xff0c;网络机器人&#xff0c;在 FOAF 社区中间&#xff0c;更经常的称为网页追逐者&#xff09;&#xff1b;它是一种按照一定的规则&#xff0c;自动地抓取网络信息的程序或者脚本。 如果我们把互联网比作一张大的蜘蛛网&#xff0c;那…...

项目中如何进行限流(限流的算法、实现方法详解)

❤ 作者主页&#xff1a;李奕赫揍小邰的博客 ❀ 个人介绍&#xff1a;大家好&#xff0c;我是李奕赫&#xff01;(&#xffe3;▽&#xffe3;)~* &#x1f34a; 记得点赞、收藏、评论⭐️⭐️⭐️ &#x1f4e3; 认真学习!!!&#x1f389;&#x1f389; 文章目录 限流的算法漏…...

SD卡接口PCB设计实战:从引脚定义到高速信号完整性布局布线

1. SD卡接口基础&#xff1a;从物理结构到引脚定义 第一次接触SD卡接口设计时&#xff0c;我对着那排密密麻麻的引脚直发懵。后来才发现&#xff0c;理解SD卡物理结构是PCB设计的第一步。常见的SD卡有标准SD、microSD&#xff08;TF卡&#xff09;和miniSD三种规格&#xff0c;…...

OpenClaw人人养虾:仪表盘(Dashboard)

Gateway 仪表盘是默认在 / 路径提供的浏览器 Control UI&#xff08;可通过 gateway.controlUi.basePath 覆盖&#xff09;。 快速打开&#xff08;本地 Gateway&#xff09;&#xff1a; http://127.0.0.1:18789/&#xff08;或 http://localhost:18789/&#xff09; 关键参…...

MGeo地址相似度模型快速入门:3步完成部署,实测效果展示

MGeo地址相似度模型快速入门&#xff1a;3步完成部署&#xff0c;实测效果展示 1. 为什么选择MGeo地址相似度模型&#xff1f; 地址匹配一直是数据处理中的痛点问题。传统方法如编辑距离、Jaccard相似度在面对中文地址特有的缩写、层级错位和口语化表达时&#xff0c;往往表现…...

别让AI代码,变成明天的技术债残

如果有多个供应商&#xff0c;你也可以使用 [[CC-Switch]] 来可视化管理这些API key&#xff0c;以及claude code 的skills。 # 多平台安装指令 curl -fsSL https://claude.ai/install.sh | bash ## Claude Code 配置 GLM Coding Plan curl -O "https://cdn.bigmodel.cn/i…...

EtherLab IGH1.6.5新版本发布:7年等待后的全面升级

1. 七年磨一剑&#xff1a;EtherLab IGH1.6.5的诞生背景 2017年10月发布的EtherLab IGH1.5.2版本&#xff0c;曾经是工业自动化领域的一个重要里程碑。这个开源EtherCAT主站解决方案&#xff0c;凭借其稳定性和灵活性&#xff0c;在机器人控制、数控机床、自动化生产线等场景中…...

5分钟快速上手OHIF-Viewers:零基础搭建医学影像DICOMweb阅片环境

5分钟快速上手OHIF-Viewers&#xff1a;零基础搭建医学影像DICOMweb阅片环境 医学影像数字化阅片已成为现代医疗信息化的核心需求。对于刚接触医疗IT的临床转技术人员或医疗信息化初学者而言&#xff0c;如何快速搭建一个符合DICOMweb标准的阅片环境常常令人望而生畏。本文将带…...

2026最权威的五大AI辅助写作工具实测分析

Ai论文网站排名&#xff08;开题报告、文献综述、降aigc率、降重综合对比&#xff09; TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 利用自然语言处理跟知识图谱技术的AI开题报告工具&#xff0c;能够快速剖析研究领域的动态变…...

SITS2026现场演示:1台边缘设备+3毫秒延迟完成千亿参数模型本地微调——联邦大模型轻量化推理的5个硬核实现细节

第一章&#xff1a;SITS2026现场演示&#xff1a;1台边缘设备3毫秒延迟完成千亿参数模型本地微调——联邦大模型轻量化推理的5个硬核实现细节 2026奇点智能技术大会(https://ml-summit.org) 在SITS2026主会场边缘计算展区&#xff0c;一台搭载NVIDIA Jetson AGX Orin&#xf…...

Ollama镜像免配置优势解析:ChatGLM3-6B-128K无需conda/pip手动依赖

Ollama镜像免配置优势解析&#xff1a;ChatGLM3-6B-128K无需conda/pip手动依赖 1. 开篇&#xff1a;告别繁琐配置的AI部署新时代 如果你曾经尝试过在本地部署AI大模型&#xff0c;一定经历过这样的痛苦&#xff1a;安装Python环境、配置CUDA驱动、解决依赖冲突、处理版本兼容…...

FinalBurn Neo:开启你的街机复古游戏宝库之旅

FinalBurn Neo&#xff1a;开启你的街机复古游戏宝库之旅 【免费下载链接】FBNeo FinalBurn Neo - We are Team FBNeo. 项目地址: https://gitcode.com/gh_mirrors/fb/FBNeo 你是否曾怀念那些在街机厅度过的美好时光&#xff1f;那些投币、摇杆、按键的清脆声响&#xf…...