测试大语言模型在嵌入式设备部署的可能性——模型TinyLlama-1.1B-Chat-v1.0

embedded/2024/10/15 18:31:09/

测试模型TinyLlama-1.1B-Chat-v1.0修改推理参数,观察参数变化与推理时间变化之间的关系。
本地环境:

处理器 Intel® Core™ i5-8400 CPU @ 2.80GHz 2.80 GHz
机带 RAM 16.0 GB (15.9 GB 可用)
集显 Intel® UHD Graphics 630
独显 NVIDIA GeForce GTX 1050

主要测试修改:

outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)

源代码来源(镜像):https://hf-mirror.com/TinyLlama/TinyLlama-1.1B-Chat-v1.0

'''
https://hf-mirror.com/TinyLlama/TinyLlama-1.1B-Chat-v1.0
测试tinyLlama 1.1B效果不错,比Qwen1.8B经过量化的都好很多
'''# Install transformers from source - only needed for versions <= v4.34
# pip install git+https://github.com/huggingface/transformers.git
# pip install accelerateimport os
from datetime import datetime
import torchos.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
from transformers import pipeline'''
pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="auto")# We use the tokenizer's chat template to format each message - see https://hf-mirror.com/docs/transformers/main/en/chat_templating
messages = [{"role": "system","content": "You are a friendly chatbot who always responds in the style of a pirate",},# {"role": "user", "content": "How many helicopters can a human eat in one sitting?"},{"role": "user", "content": "你叫什么名字?"},
]
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
print(outputs[0]["generated_text"])
'''# <|system|>
# You are a friendly chatbot who always responds in the style of a pirate.</s>
# <|user|>
# How many helicopters can a human eat in one sitting?</s>
# <|assistant|>
# ...
def load_pipeline():pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16,device_map="auto")return pipedef generate_text(content, length=20):"""根据给定的prompt生成文本"""messages = [{"role": "提示","content": "这是个友好的聊天机器人...",},# {"role": "user", "content": "How many helicopters can a human eat in one sitting?"},{"role": "user", "content": content},]prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)datetime1 = datetime.now()outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)print(outputs[0]["generated_text"])datetime2 = datetime.now()time12_interval = datetime2 - datetime1print("时间间隔", time12_interval)if False:outputs = pipe(prompt, max_new_tokens=32, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)print(outputs[0]["generated_text"])datetime3 = datetime.now()time23_interval = datetime3 - datetime2print("时间间隔2", time23_interval)outputs = pipe(prompt, max_new_tokens=32, do_sample=False, top_k=50)print(outputs[0]["generated_text"])datetime4 = datetime.now()time34_interval = datetime4 - datetime3print("时间间隔3", time34_interval)outputs = pipe(prompt, max_new_tokens=32, do_sample=True, temperature=0.7, top_k=30, top_p=0.95)print(outputs[0]["generated_text"])datetime5 = datetime.now()time45_interval = datetime5 - datetime4print("时间间隔4", time45_interval)outputs = pipe(prompt, max_new_tokens=32, do_sample=False, top_k=30)print(outputs[0]["generated_text"])datetime6 = datetime.now()time56_interval = datetime6 - datetime5print("时间间隔5", time56_interval)outputs = pipe(prompt, max_new_tokens=12, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)print(outputs[0]["generated_text"])datetime7 = datetime.now()time67_interval = datetime7 - datetime6print("时间间隔6", time67_interval)'''结论:修改top_p不会显著降低推理时间,并且中英文相同的问题,中文问题推理时间是英文的两倍do_sample修改成False基本不会降低推理时间只有max_new_tokens才能显著降低推理时间,但是max_new_tokens与推理时间不是呈线性关系比如max_new_tokens=256,推理时间2分钟当max_new_tokens=32的时候,推理时间才会变成约1分钟因此,不如将max_new_tokens设置大些用于获取比较完整的答案'''return outputsif __name__ == "__main__":'''main function'''global pipepipe = load_pipeline()# print('load pipe ok')while True:prompt = input("请输入一个提示(或输入'exit'退出):")if prompt.lower() == 'exit':breaktry:generated_text = generate_text(prompt)print("生成的文本:")print(generated_text[0]["generated_text"])except Exception as e:print("发生错误:", e)
请输入一个提示(或输入'exit'退出):如何开门?
<|user|>
如何开门?</s>
<|assistant|>
Certainly! Opening a door is a simple process that involves several steps. Here are the general steps to follow to open a door:1. Turn off the lock: Turn off the lock with the key by pressing the "lock" button.2. Press the handle: Use the handle to push the door open. If the door is mechanical, you may need to turn a knob or pull the door handle to activate the door.3. Release the latch: Once the door is open, release the latch by pulling it backward.4. Slide the door: Slide the door forward by pushing it against the wall with your feet or using a push bar.5. Close the door: Once the door is open, close it by pressing the lock button or pulling the handle backward.6. Use a second key: If the lock has a second key, make sure it is properly inserted and then turn it to the correct position to unlock the door.Remember to always double-check the locks before opening a door, as some locks can be tricky to open. If you're unsure about the correct procedure for opening a door,
时间间隔 0:04:23.561065
生成的文本:
<|user|>
如何开门?</s>
<|assistant|>
Certainly! Opening a door is a simple process that involves several steps. Here are the general steps to follow to open a door:1. Turn off the lock: Turn off the lock with the key by pressing the "lock" button.2. Press the handle: Use the handle to push the door open. If the door is mechanical, you may need to turn a knob or pull the door handle to activate the door.3. Release the latch: Once the door is open, release the latch by pulling it backward.4. Slide the door: Slide the door forward by pushing it against the wall with your feet or using a push bar.5. Close the door: Once the door is open, close it by pressing the lock button or pulling the handle backward.6. Use a second key: If the lock has a second key, make sure it is properly inserted and then turn it to the correct position to unlock the door.Remember to always double-check the locks before opening a door, as some locks can be tricky to open. If you're unsure about the correct procedure for opening a door,
请输入一个提示(或输入'exit'退出):

http://www.ppmy.cn/embedded/8706.html

相关文章

大数据平台搭建2024(二)

二&#xff1a;Hive安装 只在node01上操作 1 安装MySQL 8.0 最小化安装需要安装这个 yum install -y wget1-1 下载MySQL的yum源 wget http://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm检查是否安装成功 rpm -qpl mysql80-community-release-el7-7.n…

【NLP练习】调用Gensim库训练Word2Vec模型

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 一、准备工作 1.安装Gensim库 使用pip安装&#xff1a; !pip install gensim2. 对原始语料分词 选择《人民的民义》的小说原文作为语料&#xff0c;先采用…

【网络通信基础】网络中的常见基本概念

目录 一、IP地址 二、端口号 三、协议 四、五元组 五、协议分层 1. OSI 模型 2. TCP/CP五层&#xff08;或四层&#xff09;模型 3. 网络设备所在分层 六、封装和分用 封装&#xff08;Encapsulation&#xff09; 分用&#xff08;Multiplexing&#xff09; 一、IP…

hbase基础(三)

HBase第三天 HBase的读流程 客户端拿到一个rowkey&#xff08;首先得要知道这个rowkey存在哪个region中&#xff09;根据zk获取hbase:meta表&#xff0c;这个表中存放了region的信息&#xff0c;根据namespace、表名&#xff0c;就可以根据rowkey查看是否匹配某个region的sta…

大数据------额外插件及技术------Git(完整知识点汇总)

Git 定义 它是分布式版本控制工具&#xff0c;主要用于管理开发过程中的源代码文件&#xff08;如&#xff1a;Java类、xml文件、html页面等&#xff09;&#xff0c;在软件开发过程中被广泛应用 作用 代码回溯&#xff1a;快速回到某一代码历史版本版本切换&#xff1a;同一个…

云计算: OVN 集群 部署分布式交换机

目录 一、实验 1.环境 2.OVN 集群 部署云主机 3.中心端添加DVS分布式大二层交换机 二、问题 1.南向控制器查看主机名只显示localhost 2.中心端如何添加DVR分布式⼤三层路由器 一、实验 1.环境 (1) 主机 表1 宿主机 主机架构软件主要服务IP备注ovn_central中心端 ovn…

24届数字IC验证——SV+UVM基础知识汇总(九)

文章目录 前言57、module和program区别58、仿真调度区域59、fork-join、fork-join_none和fork-join_any的区别(经常问)60、D触发器输入高阻,输出是什么61、recovery time和removal time62、设置仿真退出时间62、$ time和$ realtime区别63、选择验证的原因(常问)64、验证工…

4.3雷达建图与导航(冰达机器人实现)

4.3雷达建图与导航 4.3.1启动激光雷达和数据查看 本节内容介绍激光雷达的启动和雷达数据的查看 机器人端启动激光雷达roslaunch robot_navigation lidar.launch 此时查看话题列表就会发现多了一个scan话题&#xff0c;这个就是雷达发布的。看到这个话题说明雷达已经正常启动。…