【大模型】Vllm基础学习
前言:vllm是一个大语言模型高速推理框架,旨在提高大模型的服务效率。优势是内存管理,实现的核心是pageattetion算法。仅在gpu上加速,不在cpu加速。
目录
- 1. PageAttention
- 2. 实践
- 2.1 安装
- 2.2 离线推理
- 2.3 适配OpenAI的api
1. PageAttention
- 核心思想:将每个序列的KV cache(键值缓存)分块处理,每块包含固定数量的token。
- 灵感来源:操作系统中的虚拟内存和分页管理技术,旨在动态地为请求分配KV cache显存,提升显存利用率
- 评估结果:vLLM可以将常用的LLM吞吐量提高了2-4倍
2. 实践
2.1 安装
pip install vllm
2.2 离线推理
示例一
from vllm import llmllm = LLM("facebook/opt-13b", tensor_parallel_size=4)
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(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}")
- 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 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)