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

这篇具有很好参考价值的文章主要介绍了测试大语言模型在嵌入式设备部署的可能性——模型TinyLlama-1.1B-Chat-v1.0。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

测试模型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://www.toymoban.com/news/detail-861458.html

'''
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 accelerate

import os
from datetime import datetime
import torch

os.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 pipe

def 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 - datetime1
    print("时间间隔", 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 - datetime2
        print("时间间隔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 - datetime3
        print("时间间隔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 - datetime4
        print("时间间隔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 - datetime5
        print("时间间隔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 - datetime6
        print("时间间隔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 outputs

if __name__ == "__main__":
    '''
    main function
    '''
    global pipe
    pipe = load_pipeline()

    # print('load pipe ok')

    while True:
        prompt = input("请输入一个提示(或输入'exit'退出):")
        if prompt.lower() == 'exit':
            break
        try:
            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'退出):

到了这里,关于测试大语言模型在嵌入式设备部署的可能性——模型TinyLlama-1.1B-Chat-v1.0的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【IoT】嵌入式Linux开发:网络设备开发(测试题)

    目录 网络开发 选择题 1、路由器工作在哪一层(B)

    2024年02月06日
    浏览(45)
  • 韦东山嵌入式Liunx入门驱动开发一(Hello 驱动编程、GPIO基础知识、LED驱动、总线设备驱动模型)

    本人学习完韦老师的视频,因此来复习巩固,写以笔记记之。 韦老师的课比较难,第一遍不知道在说什么,但是坚持看完一遍,再来复习,基本上就水到渠成了。 看完视频复习的同学观看最佳! 基于 IMX6ULL-PRO 参考视频 Linux快速入门到精通视频 参考资料 :01_嵌入式Linux应用

    2024年04月25日
    浏览(56)
  • C语言与嵌入式系统测试:单元测试、集成测试与硬件在环(HIL)测试方法(二)

    目录 二、C语言环境下的集成测试 集成测试定义与目标 C语言集成测试策略 C语言环境下的模块依赖管理 C语言集成测试实践 二、硬件在环(HIL)测试方法 HIL测试定义与原理 C语言环境下HIL测试实践 三、结论 重要角色与相互关系 发展趋势与建议 鼓励与展望 集成测试定义与目

    2024年04月28日
    浏览(27)
  • C语言与嵌入式系统测试:单元测试、集成测试与硬件在环(HIL)测试方法(一)

    目录 一、引言 二、C语言环境下的单元测试 单元测试定义与目标 C语言单元测试工具与框架 C语言单元测试实践 C语言作为一门历史悠久且广泛应用的编程语言,在嵌入式系统开发领域扮演着无可替代的角色。其简洁高效的语法、贴近硬件的特性、高度的可移植性以及丰富的编

    2024年04月26日
    浏览(34)
  • 【C C++开源库】适合单片机 嵌入式的C语言单元测试库_单片机 单元测试框架

    #define TEST_ASSERT_LESS_THAN_UINT64(threshold, actual) UNITY_TEST_ASSERT_SMALLER_THAN_UINT64((threshold), (actual), __LINE__, NULL) #define TEST_ASSERT_LESS_THAN_size_t(threshold, actual) UNITY_TEST_ASSERT_SMALLER_THAN_UINT((threshold), (actual), __LINE__, NULL) #define TEST_ASSERT_LESS_THAN_HEX8(threshold, actual) UNITY_TEST_ASSERT_SMALLER_THAN_HEX8((thres

    2024年04月25日
    浏览(35)
  • 可移动嵌入式设备

      可移动嵌入式设备是数据客户端的一种表现形式。软件的代码编写之后是运行在服务器之上,   服务器的数据为客户端提供服务的模式为服务器客户端模式,server2client 架构。服务器可以是大型的机器,也可以是小型机,主要看数据处理量和用户量的大小。一台计算机其实

    2024年02月08日
    浏览(34)
  • 【嵌入式AI部署神经网络】STM32CubeIDE上部署神经网络之指纹识别(Pytorch)——篇一|环境搭建与模型初步部署篇

    前言: 本篇主要讲解搭建所需环境,以及基于pytorch框架在stm32cubeide上部署神经网络,部署神经网络到STM32单片机,本篇实现初步部署模型,没有加入训练集与验证集,将在第二篇加入。篇二详细讲解STM32CubeIDE上部署神经网络之指纹识别(Pytorch)的数据准备和模型训练过程等

    2024年04月25日
    浏览(40)
  • 嵌入式开发,如何防止设备被抄袭?

    在国内做产品设计开发,很难避免被抄袭,被仿照。在没有形成技术壁垒之前,如何防止产品被抄袭是一个不可回避的问题。 常规设备主要的防护手段有: 专利保护 加密保护代码 授权校验 持续更新和改进 对于一些比较重要的技术发明或是创新,应该尽快申请专利。虽然目

    2024年02月08日
    浏览(47)
  • 【小黑嵌入式系统第二课】嵌入式系统的概述(二)——外围设备、处理器、ARM

    板级支持包(BSP) 是商用嵌入式操作系统实现可移植性所采用的一种方案,是硬件抽象层的一种实现。BSP是介于硬件和操作系统中驱动层程序之间的一层,有时也可认为属于操作系统一部分。BSP实现了对操作系统的支持,为上层的驱动程序提供访问硬件设备的函数包。 BSP隔离了

    2024年04月17日
    浏览(49)
  • 嵌入式Linux(8):字符设备驱动--注册字符类设备

    杂项设备 注册杂项设备: 注销杂项设备: 字符类设备 文件:include/linux/cdev.h 步骤流程: 定义一个cdev结构体。 使用cdev_init函数初始化cdev结构体成员变量。 参数: 第一个:要初始化的cdev结构体 第二个:文件操作集: cdev-ops = fops;//实际就是把文件操作集写ops 使用cdev_add函数

    2023年04月22日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包