LMDeploy量化部署LLM&VLM实践

一、前提知识:

大模型部署背景:

什么是模型部署:

部署面临的挑战:

受Transformer 架构影响:

常见GPU算力还能一战,但是显存带宽受限严重,时间花费在数据交换上居多

大模型部署方法:

模型参数以定点数或整数形式存储,实际计算时,反量化为浮点数去计算,再用定点数和整数去存储计算结果。量化会降低访存量。

LMDepoly简介:

二、项目实战

环境配置:

进入开发机,切换为终端(Terminal)模式。

创建conda环境,输入命令:

studio-conda -t lmdeploy -o pytorch-2.1.2

安装LMDeploy:

激活LMDeploy虚拟环境:

conda activate lmdeploy

安装0.3.0版本的lmdeploy:

pip install lmdeploy[all]==0.3.0

[all] 表示安装 lmdeploy 包的所有可选依赖项或功能

LMDeploy模型对话:

前提知识:

TurboMind是LMDeploy的一个推理引擎,仅支持推理TurboMind格式的模型,TurboMind在推理HF格式模型时,会自动将模型格式进行格式转换。

准备模型:

从开发机share文件夹创建模型软链接,输入命令:

cd ~
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b /root/
# cp -r /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b /root/

使用Transformer库运行模型:

(为后续对比做准备)

切换到VSCode,左边栏空白处右键,选择Open in Intergrated Terminal,打开终端

在终端中输入如下指令,新建pipeline_transformer.py

touch /root/pipeline_transformer.py

打开pipeline_transformer.py文件,填入代码:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("/root/internlm2-chat-1_8b", trust_remote_code=True)

# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and cause OOM Error.
model = AutoModelForCausalLM.from_pretrained("/root/internlm2-chat-1_8b", torch_dtype=torch.float16, trust_remote_code=True).cuda()
model = model.eval()

inp = "hello"
print("[INPUT]", inp)
response, history = model.chat(tokenizer, inp, history=[])
print("[OUTPUT]", response)

inp = "please provide three suggestions about time management"
print("[INPUT]", inp)
response, history = model.chat(tokenizer, inp, history=history)
print("[OUTPUT]", response)

Ctrl+S键保存。

回到终端,激活conda环境:

conda activate lmdeploy

运行刚刚创建的python代码:

python /root/pipeline_transformer.py

结果如下:

稍后与LMDeploy的推理速度对比

使用LMDeploy与模型对话:

使用LMDepoly命令:

#lmdeploy chat [HF格式模型路径/TurboMind格式模型路径]
lmdeploy chat /root/internlm2-chat-1_8b

至此,通过LMDepoly以命令行方式与 InternLM2-Chat-1.8B 模型对话跑通。

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以给你提供一些 VLM (Visual Light Communication) 和 LLM (Low Level Message) 的代码示例。以下是一个简单的VLM代码示例: ```python import time import RPi.GPIO as GPIO # 设置GPIO引脚 LED_PIN = 18 # 初始化GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) def transmit_message(message): for char in message: # 将字符转换为二进制 binary = bin(ord(char))[2:].zfill(8) # 发送二进制信号 for bit in binary: if bit == '1': GPIO.output(LED_PIN, GPIO.HIGH) else: GPIO.output(LED_PIN, GPIO.LOW) # 控制发送速度(根据实际情况调整) time.sleep(0.1) # 两个字符之间的间隔时间(根据实际情况调整) time.sleep(0.5) # 发送消息 transmit_message("Hello, World!") # 清理GPIO资源 GPIO.cleanup() ``` 这个示例使用树莓派的GPIO引脚来模拟光通信,通过控制LED的亮灭来传输二进制信号。 以下是一个简单的LLM代码示例,这里假设你是在C语言环境下进行开发: ```c #include <stdio.h> #include <string.h> // 定义消息缓冲区大小 #define BUFFER_SIZE 100 void process_message(char* message) { // 在这里实现对消息的处理逻辑 printf("Received message: %s\n", message); } int main() { char buffer[BUFFER_SIZE]; // 模拟接收消息 printf("Enter a message: "); fgets(buffer, BUFFER_SIZE, stdin); // 去除换行符 buffer[strcspn(buffer, "\n")] = '\0'; // 处理消息 process_message(buffer); return 0; } ``` 这个示例是一个简单的命令行程序,通过用户输入来模拟接收消息,并在控制台上输出收到的消息。 希望这些示例能对你有所帮助!如果你有任何其他问题,可以继续问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值