05-09 周四 vLLM的部署和实践

news2024/10/5 12:43:25
05-09 周四 vLLM能够做什么事情
时间版本修改人描述
2024年5月9日14:33:05V0.1宋全恒新建文档

简介

 GitHub项目 vllm-project

image-20240509155104667

 官方网站上解释了Fast、和flexible and easy to use的原因。

image-20240509162446475

注,如果要使用ModelScope中的模型,请设置环境变量:

export VLLM_USE_MODELSCOPE=True

 参考链接

网页描述
 大模型推理加速工具:vLLM描述了安装,离线推理和在线服务的简单演示
 比HuggingFace快24倍!伯克利LLM推理系统开源碾压SOTA,GPU砍半描述了优点,并且有很多的图。
 【LLM】vLLM部署与int8量化-CSDN博客👍👍文章中讲述了关于Decoding methods的许多的内容,具体包括Greedy Search, Beam Search, Sampling, Top-K sampling, Tpo-P(nucleus) ,其对应于代码中的SamplingParams
 大模型推理部署:LLM 七种推理服务框架总结_大模型推理框架-CSDN博客👍👍👍文章主要是描述了七种推理框架,并且提供了一个vLLM离线推理的模型。vllm,TGI,CTranslate2, OpenLLM, Ray Server, MLC LLM, Deep Speed
基于vllm,探索产业级llm的部署 - jsxyhelu - 博客园使用了魔搭社区,并作为在线服务,同时使用Meta-Llama进行交互的实例。
Quickstart — vLLM👍👍👍👍👍官网介绍,详细的介绍了offline Batched Inference, OpenAI-Comatible Server、Using OpenAI Chat API with vLLM

 其优点:

  • PagedAttention
  • memory sharing
    • 当用单个 prompt 产出多个不同的序列时,可以共享计算量和显存
  • Continuous batching

 其优点如下所示:

  • vLLM通过PagedAttention机制,可以高效地管理大语言模型的keys和values,显著提高吞吐量,高吞吐量服务。
  • memory sharing机制可以实现不同样本间的显存共享,进一步提升吞吐量。
  • vLLM兼容主流的预训练模型,可以不修改模型结构就获得显著的加速效果,与OpenAPI兼容。
  • 整体来说,vLLM是目前加速大模型推理的最先进解决方案之一,可以达到其他框架的24倍吞吐量提升。

vllM主要是为了解决高内存消耗和计算成本的挑战,【LLM】vLLM部署与int8量化-CSDN博客

 从上图可以看出,vLLM可以显著的提升吞吐量。

 在【LLM】vLLM部署与int8量化-CSDN博客这里也详细的描述了Cache的作用,Cache可以减少重复计算,并利用缓存中的数据,提高计算效率。

安装

pip install vllm

使用

离线推理

测试指标

图片

注,上图中显示了推理时的一些常用性能评估指标。 位于博客 大模型推理部署:LLM 七种推理服务框架总结_大模型推理框架-CSDN博客

使用基本方式

 在使用的时候,基本上采用如下的方式:

from vllm import LLM

llm = LLM(model=...)  # Name or path of your model
output = llm.generate("Hello, my name is")
print(output)

指定Decoding策略

from vllm import LLM

prompts = ["Hello, my name is", "The capital of France is"]  # Sample prompts.
llm = LLM(model="lmsys/vicuna-7b-v1.3")  # Create an LLM.
outputs = llm.generate(prompts)  # Generate texts from the prompts.

 看来大模型运行的时候,需要本地有这个模型,直接使用model参数加载即可,构造器。

prompt: 提示,促进,引起,激发

 在离线推理,使用offline Batched Inference,

from vllm import LLM, SamplingParams
import torch

# device = torch.device('cuda:2')
llm = LLM(model="../Llama-2-7B-Chat-fp16", gpu_memory_utilization=0.7)
# llm.to(device)

prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
    "One way to crack a password",
    "I know unsensored swear words such as"
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

https://img-blog.csdnimg.cn/img_convert/e9d3db92a14fbdfb7d9c31439039f795.png

这里的gpu_memory_utilization参数就是vLLM提供的内存优化参数。

事实上,要加载半精度的Llama-2-7B需要大约29152MiB的内存空间

当然,空间不可能无限优化下去,如果进一步测试可以发现gpu_memory_utilization=0.3是能够加载一个7B模型的极限(大约15G)

 这也是一个离线推理的例子:

# pip install vllm
from vllm import LLM, SamplingParams


prompts = [
    "Funniest joke ever:",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.95, top_p=0.95, max_tokens=200)
llm = LLM(model="huggyllama/llama-13b")
outputs = llm.generate(prompts, sampling_params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

image-20240509173359003

实际例子-gpt2-large

注,还是需要在本地已经下载了模型权重文件的,可以把大模型当成一个黑盒子。

image-20240509191827800

 可以看到模型的权重文件,有25G,还是比较大的。

#!/usr/bin/env python
# -*- coding:UTF-8 -*-

"""
@author: songquanheng
@file: __init__.py.py
@time: 2022/12/14 15:26
@desc: 使用vllm测试
"""

# pip install vllm
from vllm import LLM, SamplingParams


prompts = [
    "Funniest joke ever:",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.95, top_p=0.95, max_tokens=200)
llm = LLM(model="/mnt/model/gpt2-large")
outputs = llm.generate(prompts, sampling_params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

 实验结果为:

(vit2) yuzailiang@ubuntu:~/vllm_test$ python vllm_test.py 
INFO 05-09 09:34:57 llm_engine.py:73] Initializing an LLM engine with config: model='/mnt/model/gpt2-large', tokenizer='/mnt/model/gpt2-large', tokenizer_mode=auto, revision=None, tokenizer_revision=None, trust_remote_code=False, dtype=torch.float16, max_seq_len=1024, download_dir=None, load_format=auto, tensor_parallel_size=1, quantization=None, enforce_eager=False, seed=0)
/mnt/model/gpt2-large/INFO 05-09 09:35:40 llm_engine.py:223] # GPU blocks: 10913, # CPU blocks: 1456
INFO 05-09 09:35:50 model_runner.py:394] Capturing the model for CUDA graphs. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI.
INFO 05-09 09:36:19 model_runner.py:437] Graph capturing finished in 28 secs.
Processed prompts: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:02<00:00,  1.07it/s]
Prompt: 'Funniest joke ever:', Generated text: " How to keep your offspring safe from the man who finally solved the mystery of Buggies\n\nIn the mean time, Kim Kardashian and Kanye West have pledged to help spread awareness and get the word out about the problem.\n\n\nBut what about the male victims?\n\n\nChristopher Parker, from London, has come out after his birthday.\n\n\nHe said: 'It was just a normal day... but then, not a second later, I was groped, cursed at, warned not to use the train again and not to go on any other public transport.\n\n\n'I went to the local library where I looked for people who have had similar experiences, but to no avail.'\n\n'Male victims? All it took was a look - and it was terrible for me to see.'\n\n\nTransport bosses say they are already planning to tighten up security on and off the underground to try and deter people from hijacking older, unattended kids.\n\n\nMike Gerl"
Prompt: 'The capital of France is', Generated text: " currently undergoing a major history shift, and one of the top natural history destinations in the world. Europe is now home to many more internationally famous museums and monuments. The Upper Plaza, founded in 1846, boasts more than 10,000 works of art—including the most famous painting in the world, the Sistine Chapel, and the Mona Lisa, as well as more than 13,000 statues of lesser known, but still unforgettable, artists. Luxury designer Louis Vuitton, whose brand was founded in Paris in 1857, is the largest clothing and jewelry designer in the world. In addition to doing business in many of the world's major cities, Louis Vuitton has offices in France, Russia, Argentina, and Mexico. This is just one of many inspirational celebrities that have made Paris their home.\n\nTake a stroll through the Louvre and the Arc de Triomphe by tube or your own private elevator and see the incredible feats of artists, scientists, and inventors all over"
Prompt: 'The future of AI is', Generated text: ' now way ahead of it with financial services, not just in other industries but also in a wide array of applications."\n\nThe key drivers in the evolution of AI will be commercial and geopolitical trends, according to Bivens. For example, in the future many of the payments transactions will be made between governments and corporations. "Commercialization will drive AI at a higher level. The money-transfer industry could be AI-targeted. In some cases, you\'re not going to know what the application is for until you\'re thinking about it," he said.'

在线服务

例子

python -m vllm.entrypoints.openai.api_server --model lmsys/vicuna-7b-v1.3

 启动了在线服务之后,可以使用Rest调用服务

curl http://localhost:8000/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "lmsys/vicuna-7b-v1.3",
        "prompt": "San Francisco is a",
        "max_tokens": 7,
        "temperature": 0
    }'

 看起来,–model可以指定各种各样的大模型,然后通过-d来指定提示词以及生成的配置。

 在官网上Quickstart — vLLM,可以更加具体的看到一些使用的例子

image-20240509194544518

使用gpt2-large实验

 首先在服务器端启动api_server服务。

(vit2) yuzailiang@ubuntu:/mnt/model/gpt2-large$ python -m vllm.entrypoints.openai.api_server --model /mnt/model/gpt2-large/
INFO 05-09 11:21:04 api_server.py:719] args: Namespace(allow_credentials=False, allowed_headers=['*'], allowed_methods=['*'], allowed_origins=['*'], block_size=16, chat_template=None, disable_log_requests=False, disable_log_stats=False, download_dir=None, dtype='auto', enforce_eager=False, engine_use_ray=False, gpu_memory_utilization=0.9, host=None, load_format='auto', max_context_len_to_capture=8192, max_log_len=None, max_model_len=None, max_num_batched_tokens=None, max_num_seqs=256, max_paddings=256, max_parallel_loading_workers=None, model='/mnt/model/gpt2-large/', pipeline_parallel_size=1, port=8000, quantization=None, response_role='assistant', revision=None, seed=0, served_model_name=None, swap_space=4, tensor_parallel_size=1, tokenizer=None, tokenizer_mode='auto', tokenizer_revision=None, trust_remote_code=False, worker_use_ray=False)
INFO 05-09 11:21:04 llm_engine.py:73] Initializing an LLM engine with config: model='/mnt/model/gpt2-large/', tokenizer='/mnt/model/gpt2-large/', tokenizer_mode=auto, revision=None, tokenizer_revision=None, trust_remote_code=False, dtype=torch.float16, max_seq_len=1024, download_dir=None, load_format=auto, tensor_parallel_size=1, quantization=None, enforce_eager=False, seed=0)
INFO 05-09 11:21:16 llm_engine.py:223] # GPU blocks: 10913, # CPU blocks: 1456
INFO 05-09 11:21:27 model_runner.py:394] Capturing the model for CUDA graphs. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI.
INFO 05-09 11:21:58 model_runner.py:437] Graph capturing finished in 31 secs.
WARNING 05-09 11:21:58 api_server.py:115] No chat template provided. Chat API will not work.
INFO:     Started server process [17663]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

 可以看到,在8000端口已经启动了服务。这样可以通过Rest的客户端与服务器进行交互了。

image-20240509192641133

 使用类似Postman的客户端,如下输入,可以看到通过接口的输出:

POST http://10.5.30.42:8000/v1/completions 
application/json


{
        "model":"/mnt/model/gpt2-large/",
        "prompt": "San Francisco is a",
        "temperature": 0
}
响应为:
-------------------------------------------------
{
    "id": "cmpl-3dc95c24b94846a19de837deebc856af",
    "object": "text_completion",
    "created": 1917432,
    "model": "/mnt/model/gpt2-large/",
    "choices": [
        {
                "index": 0,
                "text": " city of many different cultures, and the diversity of the people who live here is",
                "logprobs": null,
                "finish_reason": "length"
        }
    ],
    "usage": {
        "prompt_tokens": 4,
        "total_tokens": 20,
        "completion_tokens": 16
    }
}

总结

 本文整理了vLLM的优点,主要是Paged Attention,并具有Continuous batch的特点,文章主要是包含了vLLM安装,并包含离线推理和在线服务的描述。

 通过实践离线推理和在线服务,

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

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

相关文章

深度学习——前馈全连接神经网络(鸢尾花)

前馈全连接神经网络对鸢尾花数据集进行分类 1.导入所需要的包2.打印训练集和测试集二维数组3.定义模型4.打印模型信息5.权重和偏执6.编译网络和训练网络7.打印二维数据表格8.绘制图像9.查看准确率 1.鸢尾花数据集可以用 from sklearn.datasets import load_iris 方式获取&#…

汽车行业芯片 车规级芯片 单车芯片( soc mcu)数量

链接&#xff1a;https://xueqiu.com/3000217281/272114755 10大车规级MCU芯片10大车规级MCU芯片 汽车芯片是什么&#xff1f; 汽车芯片即车规级芯片&#xff0c;标准要高于工业级和民用级芯片&#xff0c;仅次于军工级芯片。芯片大概有以下四种级别&#xff0c;分别是军工级…

基于OceanBase+Flink CDC,云粒智慧实时数仓演进之路

摘要&#xff1a;本文整理自云粒智慧高级技术专家付大伟在 4 月 20 日的 2024 OceanBase 开发者大会上的分享&#xff0c;讲述了其数据中台在传统数仓技术框架下做的一系列努力后&#xff0c;跨进 FlinkCDC 结合 OceanBase 的实时数仓演进过程。 内容主要分为以下几个部分: 业务…

2023年ICPC亚洲济南地区赛 G. Gifts from Knowledge

题目 思路&#xff1a; #include <bits/stdc.h> using namespace std; #define int long long #define pb push_back #define fi first #define se second #define lson p << 1 #define rson p << 1 | 1 const int maxn 1e6 5, inf 1e18, maxm 4e4 5, b…

一步一脚印式学习Linux第二课(简单明了)

继续昨天的基础命令&#xff0c;正文如下&#xff1a; 六、基础命令 8.history命令 作用&#xff1a;查看和控制历史记录 历史命令保存位置&#xff1a;~/.bash_history 启动终端时&#xff0c;会自动从历史命令保存位置的文件中读取历史命令加载到内存中。终端中执行命令时&…

视频降噪算法 hqdn3d 原理分析

视频降噪 视频降噪是一种处理技术&#xff0c;旨在减少视频中的噪声&#xff0c;提高画面质量。噪声可能来自多种源头&#xff0c;包括摄像机的传感器、压缩算法、传输过程中的干扰等。降噪处理对于视频监控、视频会议、电影后期制作以及任何需要高画质输出的应用场景都非常重…

msvcp110.dll是什么东西?关于msvcp110.dll丢失修复的问题分享

msvcp110.dll是什么东西&#xff1f;如果有人问出这个问题&#xff0c;那么百分之一百就是他遇到了丢失msvcp110.dll文件的问题了&#xff0c;不然别人是不可能问出这个问题的&#xff01;这个文件名字一般只有在你的电脑系统出现问题的时候&#xff0c;它才会弹出提示。今天来…

Baidu Comate智能编码助手

Baidu Comate智能编码助手 &#x1f388;1.Baidu Comate的简介&#x1f388;2.安装Baidu Comate&#x1f388;3.Baidu Comate实现功能&#x1f388;4.使用注释进行智能代码提示&#x1f388;5.结束语 &#x1f388;1.Baidu Comate的简介 根据官网的介绍&#xff0c;我们了解到B…

Verilog复习(一)| 模块的定义

模块&#xff08;module&#xff09;是Verilog的基本描述单位&#xff0c;用于描述某个设计的功能或结构&#xff0c;及其与其他模块通信&#xff08;连接&#xff09;的外部端口。 Verilog程序由关键词module和endmodule进行定义。 定义模块的步骤&#xff1a; 定义模块的端…

通用型产品发布解决方案(基础环境搭建)

文章目录 1.项目技术栈和前置技术2.创建Linux平台1.需求分析2.安装Virtual Box1.BIOS里修改设置开启虚拟化设备支持&#xff08;f2 或f10&#xff09;2.任务管理器 -> cpu 查看虚拟化是否开启3.卸载方式4.安装6.1.265.管理员身份运行&#xff0c;选择安装位置6.一直下一步&a…

Java基于Spring Boot框架毕业生实习与就业管理系统的设计与实现(附源码,说明文档)

博主介绍&#xff1a;✌IT徐师兄、7年大厂程序员经历。全网粉丝15W、csdn博客专家、掘金/华为云//InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;&#x1f3…

Qt---项目的创建及运行

一、创建第一个Qt程序 1. 点击创建项目后&#xff0c;选择项目路径以及给项目起名称 名称&#xff1a;不能有中文、不能有空格 路径&#xff1a;不能有中文路径 2. 默认创建有窗口类myWidget&#xff0c;基类有三种选择&#xff1a;QWidget、QMainWindow、QDialog 3. m…

智慧手术室手麻系统源码,C#手术麻醉临床信息系统源码,符合三级甲等医院评审要求

手麻系统全套源码&#xff0c;C#手术麻醉系统源码&#xff0c;支持二次开发&#xff0c;授权后可商用。 手术麻醉临床信息系统功能符合三级甲等医院评审要求&#xff0c;实现与医院现有信息系统如HIS、LIS、PACS、EMR等系统全面对接&#xff0c;全面覆盖从患者入院&#xff0c;…

深度学习之视觉特征提取器——GoogleNet/Inception

GoogleNet GoogleNet在2014年中的ImageNet夺冠&#xff0c;将Inception这一结构推向了热潮。从另外一个角度来看&#xff0c;CV魔改网络结构也从中得到启发或者说推动&#xff0c;拓宽了各种魔改的方式。GoogleNet其实只是Inception这一结构大规模集成后得到的模型&#xff0c…

堆的应用1——堆排序

一&#xff0c;堆排序 堆排序是一种基于比较的排序算法&#xff0c;它利用堆这种数据结构所设计。 堆是一个近似完全二叉树的结构&#xff0c;并同时满足堆积的性质&#xff1a;即子结点的键值或索引总是小于&#xff08;或者大于&#xff09;它的父结点。 堆排序可以分为两…

【python】模拟巴特沃斯滤波器

巴特沃斯滤波器&#xff08;Butterworth Filter&#xff09;&#xff0c;以其设计者斯蒂芬巴特沃斯&#xff08;Stephen Butterworth&#xff09;的名字命名&#xff0c;是一种具有平滑频率响应的滤波器。这种滤波器在频域中具有非常平坦的无波纹响应&#xff0c;直到它达到截止…

Linux(openEuler、CentOS8)企业内网DHCP服务器搭建(固定Mac获取指定IP)

----本实验环境为openEuler系统<以server方式安装>&#xff08;CentOS8基本一致&#xff0c;可参考本文&#xff09;---- 目录 一、知识点二、实验&#xff08;一&#xff09;为服务器配置网卡和IP&#xff08;二&#xff09;为服务器安装DHCP服务软件&#xff08;三&a…

iOS 17 / iPad OS 17屏蔽更新

iOS 17 / iPad OS 17屏蔽更新 1&#xff0c;进入屏蔽iOS更新的描述文件下载链接 下载链接 wx 搜索 Geek 前端发送屏蔽更新进行获取 2&#xff0c;复制这段链接&#xff0c;在Safari浏览器中打开&#xff0c;注意打开后别点击下载&#xff01;要先改时间&#xff01; 3&#…

实例分割——Mask R-CNN、YOLOV8、RTMDET、DeepLab四种实例分割算法比对

1.概述 1.1 语义分割与实例分割 实例分割和语义分割都是计算机视觉领域中图像分割的任务&#xff0c;它们在目标和方法上有一些区别&#xff1a; 语义分割&#xff1a; 语义分割的目标是对图像中的每个像素打上类别标签&#xff0c;即识别出图像中每个像素属于哪个预定义的…

Redis简介和数据结构

目录 简介 进入之后身份认证才能使用 优点 用途&#xff1a; 数据结构 string string自动扩容 Redis中的简单动态字符串&#xff08;SDS&#xff09;具有以下优点&#xff1a; SDS数据的编码格式 比较&#xff1a; string 常用操作 分布式锁 使用情况&#xff0c;…