湖州企業(yè)做網(wǎng)站手機(jī)關(guān)鍵詞seo排名優(yōu)化
目錄
一、vLLM 介紹
二、安裝 vLLM
2.1 使用 GPU 進(jìn)行安裝
2.2 使用CPU進(jìn)行安裝
2.3 相關(guān)配置
三、使用 vLLM
3.1 離線推理
3.2?適配OpenAI-API的API服務(wù)
一、vLLM 介紹
????????vLLM是伯克利大學(xué)LMSYS組織開源的大語言模型高速推理框架。它利用了全新的注意力算法「PagedAttention」,提供易用、快速、便宜的LLM服務(wù)。
二、安裝 vLLM
2.1 使用 GPU 進(jìn)行安裝
????????vLLM 是一個(gè)Python庫,同時(shí)也包含預(yù)編譯的C++和CUDA(12.1版本)二進(jìn)制文件。
? ? ? ?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創(chuàng)建python虛擬環(huán)境(可選)
conda create -n vllm python=3.11 -y
conda activate vllm# Install vLLM with CUDA 12.1.
pip install vllm
2.2 使用CPU進(jìn)行安裝
????????vLLM 也支持在 x86 CPU 平臺上進(jìn)行基本的模型推理和服務(wù),支持的數(shù)據(jù)類型包括 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
# 需要進(jìn)入源碼目錄
pip install -v -r requirements-cpu.txt --extra-index-url https://download.pytorch.org/whl/cpu
? ? ? ? 5.執(zhí)行安裝:
VLLM_TARGET_DEVICE=cpu python setup.py install
2.3 相關(guān)配置
? ? ? ?1. vLLM默認(rèn)從HuggingFace下載模型,如果想從ModelScope下載模型,需要配置環(huán)境變量:
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服務(wù)
????????借助vLLM,構(gòu)建一個(gè)與OpenAI API兼容的API服務(wù)十分簡便,該服務(wù)可以作為實(shí)現(xiàn)OpenAI API協(xié)議的服務(wù)器進(jìn)行部署。默認(rèn)情況下,它將在?http://localhost:8000?啟動服務(wù)器。您可以通過?--host?和?--port?參數(shù)來自定義地址。請按照以下所示運(yùn)行命令:
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)