自主式响应网站,用html做网页,品牌建设与质量培训,手机网站建设服务哪家好前言#xff1a;vllm是一个大语言模型高速推理框架#xff0c;旨在提高大模型的服务效率。优势是内存管理#xff0c;实现的核心是pageattetion算法。仅在gpu上加速#xff0c;不在cpu加速。 目录 1. PageAttention2. 实践2.1 安装2.2 离线推理2.3 适配OpenAI的api 1. Page… 前言vllm是一个大语言模型高速推理框架旨在提高大模型的服务效率。优势是内存管理实现的核心是pageattetion算法。仅在gpu上加速不在cpu加速。 目录 1. PageAttention2. 实践2.1 安装2.2 离线推理2.3 适配OpenAI的api 1. PageAttention
核心思想将每个序列的KV cache键值缓存分块处理每块包含固定数量的token。灵感来源操作系统中的虚拟内存和分页管理技术旨在动态地为请求分配KV cache显存提升显存利用率评估结果vLLM可以将常用的LLM吞吐量提高了2-4倍
2. 实践
2.1 安装 pip install vllm2.2 离线推理
示例一
from vllm import llmllm LLM(facebook/opt-13b, tensor_parallel_size4)
output llm.generate(San Franciso is a)示例二
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(temperature0.7, top_p0.8, repetition_penalty1.05, max_tokens512)# Input the model name or path. Can be GPTQ or AWQ models.
llm LLM(modelQwen/l/Qwen1.5-4B-Chat, trust_remote_codeTrue)# 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,tokenizeFalse,add_generation_promptTrue
)# generate outputs
outputs llm.generate([text], sampling_params)# Print the outputs.
for output in outputs:prompt output.promptgenerated_text output.outputs[0].textprint(fPrompt: {prompt!r}, Generated text: {generated_text!r})SamplingParams在VLLM模型中主要负责调整采样过程。采样是在模型生成文本或其他类型输出时的一个关键步骤它决定了模型如何从可能的输出中选择一个。LLM的参数model是模型名还可以输入其他大语言模型但要注意不是所有的llm都被vllm支持。message中定义了系统的角色内容以及用户的角色内容
2.3 适配OpenAI的api
a. 命令行输入
python -m vllm.entrypoints.openai.api_server --model your_model_path --trust-remote-code默认监听 8000 端口–host 和–port 参数可以指定主机和端口。 b. 使用curl与Qwen对接(命令行)
curl http://localhost:8000/generate \-d {prompt: San Francisco is a,use_beam_search: true,n: 4,temperature: 0}http://localhost:8000/generate是访问的http地址也就是客户端地址-d后面跟的是参数可以根据需求配置不同的参数
c. 使用python和Qwen对接
from openai import OpenAI
# Set OpenAIs API key and API base to use vLLMs API server.
openai_api_key EMPTY
openai_api_base http://localhost:8000/v1client OpenAI(api_keyopenai_api_key,base_urlopenai_api_base,
)chat_response client.chat.completions.create(modelQwen/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)