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

大模型框架:vLLM

目录

一、vLLM 介绍

二、安装 vLLM

2.1 使用 GPU 进行安装

2.2 使用CPU进行安装

2.3 相关配置

三、使用 vLLM

3.1 离线推理

3.2 适配OpenAI-API的API服务


一、vLLM 介绍

        vLLM是伯克利大学LMSYS组织开源的大语言模型高速推理框架。它利用了全新的注意力算法「PagedAttention」,提供易用、快速、便宜的LLM服务。

二、安装 vLLM

2.1 使用 GPU 进行安装

        vLLM 是一个Python库,同时也包含预编译的C++和CUDA(12.1版本)二进制文件。

       1. 安装条件:

  • OS: Linux
  • Python: 3.8 – 3.11
  • GPU: compute capability 7.0 or higher (e.g., V100, T4, RTX20xx, A100, L4, H100, etc.)

        2.使用 pip 安装:

# 使用conda创建python虚拟环境(可选)
conda create -n vllm python=3.11 -y
conda activate vllm

# Install vLLM with CUDA 12.1.
pip install vllm

2.2 使用CPU进行安装

        vLLM 也支持在 x86 CPU 平台上进行基本的模型推理和服务,支持的数据类型包括 FP32 和 BF16。

        1.安装要求:

  • OS: Linux
  • Compiler: gcc/g++>=12.3.0 (recommended)
  • Instruction set architecture (ISA) requirement: AVX512 is required.

        2.安装编译依赖:

yum install -y gcc  gcc-c++

        3.下载源码:

git clone https://github.com/vllm-project/vllm.git

        4.安装python依赖:

pip install wheel packaging ninja setuptools>=49.4.0 numpy psutil

# 需要进入源码目录
pip install -v -r requirements-cpu.txt --extra-index-url https://download.pytorch.org/whl/cpu

        5.执行安装:

VLLM_TARGET_DEVICE=cpu python setup.py install

2.3 相关配置

       1. vLLM默认从HuggingFace下载模型,如果想从ModelScope下载模型,需要配置环境变量:

export VLLM_USE_MODELSCOPE=True

三、使用 vLLM

3.1 离线推理

from transformers import AutoTokenizer
from vllm import LLM, SamplingParams# Initialize the tokenizer
tokenizer = AutoTokenizer.from_pretrained("/data/weisx/model/Qwen1.5-4B-Chat")# Pass the default decoding hyperparameters of Qwen1.5-4B-Chat
# max_tokens is for the maximum length for generation.
sampling_params = SamplingParams(temperature=0.7, top_p=0.8, repetition_penalty=1.05, max_tokens=512)# Input the model name or path. Can be GPTQ or AWQ models.
llm = LLM(model="Qwen/l/Qwen1.5-4B-Chat", trust_remote_code=True)# Prepare your prompts
prompt = "Tell me something about large language models."
messages = [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True
)# generate outputs
outputs = llm.generate([text], sampling_params)# Print the outputs.
for output in outputs:prompt = output.promptgenerated_text = output.outputs[0].textprint(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

3.2 适配OpenAI-API的API服务

        借助vLLM,构建一个与OpenAI API兼容的API服务十分简便,该服务可以作为实现OpenAI API协议的服务器进行部署。默认情况下,它将在 http://localhost:8000 启动服务器。您可以通过 --host 和 --port 参数来自定义地址。请按照以下所示运行命令:

python -m vllm.entrypoints.openai.api_server \
    --model Qwen/Qwen1.5-4B-Chat

        使用curl与Qwen对接:

curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
    "model": "Qwen/Qwen1.5-4B-Chat",
    "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Tell me something about large language models."}
    ]
    }'

        使用python客户端与Qwen对接:

from openai import OpenAI
# Set OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"client = OpenAI(api_key=openai_api_key,base_url=openai_api_base,
)chat_response = client.chat.completions.create(model="Qwen/Qwen1.5-4B-Chat",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Tell me something about large language models."},]
)
print("Chat response:", chat_response)

http://www.lryc.cn/news/352775.html

相关文章:

  • SQL 使用心得【持续更新】
  • 基于Spring Boot的高校图书馆管理系统
  • python(4) : pip安装使用国内源
  • 让写书人勇敢穿越纸海的迷雾
  • ROS2学习——节点话题通信(2)
  • 【Spring Boot】深度复盘在开发搜索引擎项目中重难点的整理,以及遇到的困难和总结
  • 配置docker阿里云镜像地址
  • ICML 2024 Mamba 论文总结
  • Sass详解
  • 如何实现一个高效的排序算法?
  • Linux--10---安装JDK、MySQL
  • 【大数据】MapReduce JAVA API编程实践及适用场景介绍
  • 图像分类和文本分类(传统机器学习和深度学习)
  • 基于SpringBoot和Hutool工具包实现的验证码案例
  • python-找出四位数中的玫瑰花数
  • Linux-命令上
  • 青鸟云报修系统:实现高效、便捷的维修申请处理
  • Python解析网页
  • IDEA连接MySQL后如何管理数据库
  • linux新机快速配置ssh
  • 使用elementUI的form表单校验时,错误提示位置异常解决方法
  • Android面试题之Kotlin常见集合操作技巧
  • 网络拓扑—DNS服务搭建
  • Mybatis-Plus笔记
  • “高考钉子户”唐尚珺决定再战2024年高考
  • Hive安装教程
  • 使用Python Tkinter创建GUI应用程序
  • 使用 RT 矩阵进行 3D 点云变换详解(基于 PCL 和 Eigen 库)
  • CTFHUB技能树——SSRF(二)
  • Vue3实现简单的瀑布流效果,可抽离成组件直接使用