大模型的 N 种高效部署方法:以 LLaMA2 为例
通过部署 LLaMA2 示例,比较不同 开源推理服务框架的优缺点。
本文通过部署 LLaMA2 示例,对比了 vLLM、Text generation inference、CTranslate2、DeepSpeed-MII、OpenLLM、Ray Serve、MLC LLM 及 LightLLM 等八种开源推理服务框架的优缺点。文章涵盖了各框架的性能特点、支持功能(如连续批处理、量化、适配器支持)及适用场景,并提供了相应的代码示例与启动命令,旨在帮助开发者根据实际需求选择合适的大模型部署方案。

通过部署 LLaMA2 示例,比较不同 开源推理服务框架的优缺点。
LLM本文未介绍深度学习模型推理服务的传统库,如 TorchServe、KServe 或 Triton Inference Server。
它的吞吐量比 huggingface transformers(HF)高 14 倍到 24 倍,吞吐量比文本生成推理(TGI)高 2.2 倍。有连续批处理(Continuous batching)和 PagedAttention 功能,集成各种解码算法,包括并行采样、波束搜索等。但缺乏对适配器(LoRA、QLoRA 等)的支持。
后期功能迭代可以追踪官方库。
# 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}")
# Start the server:
python -m vllm.entrypoints.api_server --env MODEL_NAME=huggyllama/llama-13b
# Query the model in shell:
curl http://localhost:8000/generate \
-d '{
"prompt": "Funniest joke ever:",
"n": 1,
"temperature": 0.95,
"max_tokens": 200
}'
用于文本生成推理的 Rust、Python 和 gRPC 服务框架。在 HuggingFace 的生产中使用,为 LLM 的 API 推理小部件提供支持。内置 Prometheus metrics,可以监控服务器负载和性能,可以使用 Flashattention 和 PagedAttention。所有依赖项都安装在 Docker 中,支持 HuggingFace 模型,有很多选项来管理模型推理,包括精度调整、量化、张量并行性、重复惩罚等。适合了解 Rust 编程的人。
mkdir data
docker run --gpus all --shm-size 1g -p 8080:80 \
-v data:/data ghcr.io/huggingface/text-generation-inference:0.9 \
--model-id huggyllama/llama-13b \
--num-shard 1
# pip install text-generation
from text_generation import Client
client = Client("http://127.0.0.1:8080")
prompt = "Funniest joke ever:"
print(client.generate(prompt, max_new_tokens=17, temperature=0.95).generated_text)
CTranslate2 是一个 C++ 和 Python 库,用于使用 Transformer 模型进行高效推理。在 CPU 和 GPU 上快速高效地执行,支持多种 CPU 架构,一些优化技术:layer fusion, padding removal, batch reordering, in-place operations, caching mechanism。支持并行和异步执行。缺乏对适配器(LoRA、QLoRA 等)的支持。
pip install -qqq transformers ctranslate2
# The model should be first converted into the CTranslate2 model format:
ct2-transformers-converter --model huggyllama/llama-13b --output_dir llama-13b-ct2 --force
import ctranslate2
import transformers
generator = ctranslate2.Generator("llama-13b-ct2", device="cuda", compute_type="float16")
tokenizer = transformers.AutoTokenizer.from_pretrained("huggyllama/llama-13b")
prompt = "Funniest joke ever:"
tokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(prompt))
results = generator.generate_batch(
[tokens],
sampling_topk=1,
max_length=200,
)
tokens = results[0].sequences_ids[0]
output = tokenizer.decode(tokens)
print(output)
MII 使低延迟和高吞吐量推理成为可能,由 DeepSpeed 提供支持。跨多个副本的负载平衡,用于处理大量用户的非常有用的工具。原生和 Azure 集成、缺乏对适配器(LoRA、QLoRA 等)的支持。
# DON'T INSTALL USING pip install deepspeed-mii
# git clone https://github.com/microsoft/DeepSpeed-MII.git
# git reset --hard 60a85dc3da5bac3bcefa8824175f8646a0f12203
# cd DeepSpeed-MII && pip install .
# pip3 install -U deepspeed
# ... and make sure that you have same CUDA versions:
# python -c "import torch;print(torch.version.cuda)" == nvcc --version
import mii
mii_configs = {
"dtype": "fp16",
'max_tokens': 200,
'tensor_parallel': 1,
"enable_load_balancing": False
}
mii.deploy(task="text-generation",
model="huggyllama/llama-13b",
deployment_name="llama_13b_deployment",
mii_config=mii_configs)
import mii
generator = mii.mii_query_handle("llama_13b_deployment")
result = generator.query(
{"query": ["Funniest joke ever:"]},
do_sample=True,
max_new_tokens=200
)
print(result)
在生产中操作大型语言模型(LLM)的开放平台。OpenLLM 支持使用 bitsandbytes 和 gptQ 进行量化。LangChain 集成。缺乏批处理支持、缺乏内置的分布式推理。
pip install openllm scipy
openllm start llama --model-id huggyllama/llama-13b \
--max-new-tokens 200 \
--temperature 0.95 \
--api-workers 1 \
--workers-per-resource 1
import openllm
client = openllm.client.HTTPClient('http://localhost:3000')
print(client.query("Funniest joke ever:"))
Ray Serve 是一个可扩展的模型服务库,用于构建在线推理 API。Serve 与框架无关,因此您可以使用单个工具包来提供深度学习模型中的所有内容。可以使用 Ray 控制面板获取 Ray 集群和 Ray Serve 应用程序状态的高级概述。可以跨多个副本自动缩放和动态请求批处理。Ray Serve并不专注于LLM,它是一个更广泛的框架,用于部署任何ML模型。最适合可用性、可伸缩性和可观测性非常重要的企业。
# pip install ray[serve] accelerate>=0.16.0 transformers>=4.26.0 torch starlette pandas
# ray_serve.py
import pandas as pd
import ray
from ray import serve
from starlette.requests import Request
@serve.deployment(ray_actor_options={"num_gpus": 1})
class PredictDeployment:
def __init__(self, model_id: str):
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
self.model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16,
device_map="auto",
)
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
def generate(self, text: str) -> pd.DataFrame:
input_ids = self.tokenizer(text, return_tensors="pt").input_ids.to(
self.model.device
)
gen_tokens = self.model.generate(
input_ids,
temperature=0.9,
max_length=200,
)
return pd.DataFrame(
self.tokenizer.batch_decode(gen_tokens), columns=["responses"]
)
async def __call__(self, http_request: Request) -> str:
json_request: str = await http_request.json()
# Note: Prompt key might vary depending on request structure
prompt = json_request.get("text", "")
return self.generate(prompt)
deployment = PredictDeployment.bind(model_id="huggyllama/llama-13b")
# then run from CLI command:
# serve run ray_serve:deployment
import requests
sample_input = {"text": "Funniest joke ever:"}
output = requests.post("http://localhost:8000/", json=[sample_input]).json()
print(output)
机器学习编译 LLM(MLC LLM)是一种通用部署解决方案,使 LLM 能够利用本机硬件加速在消费者设备上高效运行。该库主要专注于为不同设备编译模型。支持分组量化。
# 1. Make sure that you have python >= 3.9
# 2. You have to run it using conda:
conda create -n mlc-chat-venv -c mlc-ai -c conda-forge mlc-chat-nightly
conda activate mlc-chat-venv
# 3. Then install package:
pip install --pre --force-reinstall mlc-ai-nightly-cu118 \
mlc-chat-nightly-cu118 \
-f https://mlc.ai/wheels
# 4. Download the model weights from HuggingFace and binary libraries:
git lfs install && mkdir -p dist/prebuilt && \
git clone https://github.com/mlc-ai/binary-mlc-llm-libs.git dist/prebuilt/lib && \
cd dist/prebuilt && \
git clone https://huggingface.co/huggyllama/llama-13b dist/ && \
cd ../..
python -m mlc_chat.rest --device-name cuda --artifact-path dist
import requests
payload = {
"model": "lama-30b",
"messages": [{"role": "user", "content": "Funniest joke ever:"}],
"stream": False
}
r = requests.post("http://127.0.0.1:8000/v1/chat/completions", json=payload)
print(r.json()['choices'][0]['message']['content'])
LightLLM 是一个基于 Python 的 LLM(大型语言模型)推理和服务框架,以其轻量级设计、易扩展性和高速性能而著称。LightLLM 利用了众多广受赞誉的开源实现,包括但不限于 FasterTransformer、TGI、vLLM 和 FlashAttention。
import time
import requests
import json
url = 'http://localhost:8000/generate'
headers = {'Content-Type': 'application/json'}
data = {
'inputs': 'What is AI?',
"parameters": {
'do_sample': False,
'ignore_eos': False,
'max_new_tokens': 1024,
}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
print(response.json())
else:
print('Error:', response.status_code, response.text)
python -m lightllm.server.api_server --model_dir /path/llama-7B --tp 1 --max_total_token_num 120000
curl 127.0.0.1:8000/generate \
-X POST \
-d '{"inputs":"What is AI?","parameters":{"max_new_tokens":17, "frequency_penalty":1}}' \
-H 'Content-Type: application/json'
为 LLM 推理配置必要的环境并使用单个命令启动:dstack run . -f vllm/serve.dstack.yml
type: task
env:
- MODEL=huggyllama/llama-13b
# (Optional) Specify your Hugging Face token
- HUGGING_FACE_HUB_TOKEN=
ports:
- 8000
commands:
- conda install cuda # Required since vLLM will rebuild the CUDA kernel
- pip install vllm
- python -m vllm.entrypoints.openai.api_server --model $MODEL --port 8000
在选择具体的部署框架时,除了考虑上述的性能和功能特性外,还应结合团队的技术栈储备、现有的基础设施以及未来的维护成本。例如,如果团队已经深度集成了 Ray 生态,那么 Ray Serve 可能是平滑过渡的最佳选择;若关注移动端或边缘端部署,MLC LLM 则是目前较为成熟的方案。此外,对于生产环境,务必关注各框架的版本稳定性及社区活跃度,确保长期可获得技术支持。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online