【AI实战】大模型 LLM 部署推理框架的 vLLM 应用

vLLM是一个快速且易用的库,用于LLM推理和服务。它支持多种HuggingFace模型,如BLOOM、GPT系列和LLaMA等。vLLM提供高吞吐量的推理,具备PagedAttention、连续批处理和优化的CUDA内核等功能。用户可以利用它进行离线批量推理、API服务器设置,甚至分布式推理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vLLM介绍

vLLM is a fast and easy-to-use library for LLM inference and serving.

vLLM 速度很快:

  • State-of-the-art serving throughput
  • Efficient management of attention key and value memory with PagedAttention
  • Continuous batching of incoming requests
  • Optimized CUDA kernels

vLLM灵活且易于使用:

  • Seamless integration with popular HuggingFace models
  • High-throughput serving with various decoding algorithms, including parallel sampling, beam search, and more
  • Tensor parallelism support for distributed inference
  • Streaming outputs
  • OpenAI-compatible API server

vLLM 无缝支持多数 Huggingface 模型,包括:

  • BLOOM (bigscience/bloom, bigscience/bloomz, etc.)
  • GPT-2 (gpt2, gpt2-xl, etc.)
  • GPT BigCode (bigcode/starcoder, bigcode/gpt_bigcode-santacoder, etc.)
  • GPT-J (EleutherAI/gpt-j-6b, nomic-ai/gpt4all-j, etc.)
  • GPT-NeoX (EleutherAI/gpt-neox-20b, databricks/dolly-v2-12b, stabilityai/stablelm-tuned-alpha-7b, etc.)
  • LLaMA (lmsys/vicuna-13b-v1.3, young-geng/koala, openlm-research/open_llama_13b, etc.)
  • MPT (mosaicml/mpt-7b, mosaicml/mpt-30b, etc.)
  • OPT (facebook/opt-66b, facebook/opt-iml-max-30b, etc.)

环境配置

环境要求

  • OS: Linux

  • Python: 3.8 or higher

  • CUDA: 11.0 – 11.8

  • GPU: compute capability 7.0 or higher (e.g., V100, T4, RTX20xx, A100, L4, etc.)

安装 vllm

  • pip安装
pip install vllm
  • 源码安装
git clone https://github.com/vllm-project/vllm.git
cd vllm
pip install -e .  # This may take 5-10 minutes.

算力要求

算力查询方法

  1. 打开bing查询地址:https://cn.bing.com/
  2. 查询方式选择 国际版
  3. 输入查询内容:
    t4   GPUs  compute capability
    
    我的 GPU 是 T4,修改 t4 为你的即可
  4. 查询结果如下:
    在这里插入图片描述

算力问题

vllm 对GPU 的 compute capability 要求必须大于等于 7.0,否则会报错,错误信息如下:

RuntimeError: GPUs with compute capability less than 7.0 are not supported.

Quickstart

离线批量推理

示例代码:

from vllm import LLM, SamplingParams
prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)

llm = LLM(model="facebook/opt-125m")
outputs = llm.generate(prompts, sampling_params)

# Print the outputs.
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

API Server

FastAPI server为例子, 服务使用 AsyncLLMEngine类来支持异步请求。

  • 开启服务:
python -m vllm.entrypoints.api_server

默认接口:http://localhost:8000
默认模型:OPT-125M model

  • 测试:
curl http://localhost:8000/generate \
    -d '{
        "prompt": "San Francisco is a",
        "use_beam_search": true,
        "n": 4,
        "temperature": 0
    }'

兼容 OpenAI Server

  • 开启服务:
python -m vllm.entrypoints.openai.api_server   --model facebook/opt-125m

可选参数:--host--port

  • 查询服务:
curl http://localhost:8000/v1/models
  • 测试:
curl http://localhost:8000/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "facebook/opt-125m",
        "prompt": "San Francisco is a",
        "max_tokens": 7,
        "temperature": 0
    }'

Serving

分布式推理和服务

安装依赖库:

pip install ray
  • 多GPU推理
    4块GPU推理:
from vllm import LLM
llm = LLM("facebook/opt-13b", tensor_parallel_size=4)
output = llm.generate("San Franciso is a")

使用 tensor_parallel_size 指定 GPU 数量

  • 多GPU服务
python -m vllm.entrypoints.api_server \
    --model facebook/opt-13b \
    --tensor-parallel-size 4
  • 扩展到多节点
    运行vllm之前开启Ray runtime
# On head node
ray start --head

# On worker nodes
ray start --address=<ray-head-address>

使用 SkyPilot 运行服务

安装 SkyPilot :

pip install skypilot
sky check

serving.yaml:

resources:
    accelerators: A100

envs:
    MODEL_NAME: decapoda-research/llama-13b-hf
    TOKENIZER: hf-internal-testing/llama-tokenizer

setup: |
    conda create -n vllm python=3.9 -y
    conda activate vllm
    git clone https://github.com/vllm-project/vllm.git
    cd vllm
    pip install .
    pip install gradio

run: |
    conda activate vllm
    echo 'Starting vllm api server...'
    python -u -m vllm.entrypoints.api_server \
                    --model $MODEL_NAME \
                    --tensor-parallel-size $SKYPILOT_NUM_GPUS_PER_NODE \
                    --tokenizer $TOKENIZER 2>&1 | tee api_server.log &
    echo 'Waiting for vllm api server to start...'
    while ! `cat api_server.log | grep -q 'Uvicorn running on'`; do sleep 1; done
    echo 'Starting gradio server...'
    python vllm/examples/gradio_webserver.py

开启服务:

sky launch serving.yaml

其他可选参数:

sky launch -c vllm-serve-new -s serve.yaml --gpus A100:8 --env MODEL_NAME=decapoda-research/llama-65b-hf

测试:
浏览器打开:https://<gradio-hash>.gradio.live

模型

vLLM支持的模型

在这里插入图片描述

https://vllm.readthedocs.io/en/latest/models/supported_models.html#supported-models

添加自己的模型

本文档提供了将HuggingFace Transformers模型集成到vLLM中的高级指南。
https://vllm.readthedocs.io/en/latest/models/adding_model.html

参考

1.https://vllm.readthedocs.io/en/latest/
2.https://github.com/vllm-project/vllm
3.https://vllm.ai/
4.https://github.com/vllm-project/vllm/discussions
5.https://github.com/skypilot-org/skypilot/blob/master/llm/vllm

<think>好的,我需要帮助用户了解如何在本地使用VLLM部署通义千问。首先,我需要查阅用户提供的引用内容,看看是否有相关的部署指南或参考资料。 用户提到了引用[2]和引用[3],其中引用[2]是关于在昇腾910B2服务器上部署Qwen2.5,而引用[3]是关于Qwen2-VL的本地部署实战。虽然VLLM未被直接提及,但VLLM通常用于加速大语言模型的推理,可能和LightLLM有关联,因为引用[1]提到了LightLLM的高效调度和性能优势。不过用户明确询问的是VLLM,所以可能需要结合这些信息。 首先,我需要确认通义千问是否支持VLLM框架VLLM是支持HuggingFace模型的,所以如果通义千问的模型权重是HuggingFace格式的,应该可以适配。然后,部署步骤通常包括环境准备、模型下载、配置调整、启动服务等环节。需要检查是否有依赖库需要安装,比如vllm、transformers等。此外,硬件方面可能需要NVIDIA GPU和CUDA支持,但用户提到的是昇腾910B2,这里可能存在兼容性问题,因为VLLM主要针对CUDA优化,昇腾芯片可能需要特定的适配,但用户的问题可能假设使用NVIDIA环境。 需要提醒用户注意硬件兼容性,如果使用昇腾芯片可能需要其他框架如LightLLM,如引用[1]所提。但用户明确问的是VLLM,所以还是以常规NVIDIA环境为例。接下来,步骤可能包括安装VLLM,下载模型,编写启动脚本。需要参考通义千问的官方文档是否有VLLM的示例,或者社区中的实践案例。另外,可能需要调整参数如tensor并行度、最大token数等。 另外,用户提供的引用[3]提到Qwen2-VL在文档理解方面的优势,如果用户需要视觉语言模型,可能需要额外的步骤,但问题主要聚焦于VLLM部署,可能以Qwen2.5为例。最后,生成相关问题时要考虑用户可能的后续疑问,比如性能优化、多GPU部署、兼容性问题等。</think>以下是在本地使用VLLM部署通义千问的详细指南: ### 一、部署准备 1. **硬件要求** - NVIDIA GPU(推荐RTX 3090/A100以上) - CUDA 11.8+环境(需与VLLM版本匹配) 2. **环境依赖** ```bash conda create -n qwen_vllm python=3.9 conda activate qwen_vllm pip install vllm>=0.3.0 transformers>=4.37.0 ``` ### 二、模型获取 1. 从官方渠道下载模型权重: ```bash git clone https://www.modelscope.cn/qwen/Qwen2.5-7B-Instruct.git ``` 或通过HuggingFace下载: ```python from huggingface_hub import snapshot_download snapshot_download(repo_id="Qwen/Qwen2.5-7B-Instruct") ``` ### 三、VLLM启动配置 ```python # launch_vllm.py from vllm import LLM, SamplingParams llm = LLM( model="/path/to/Qwen2.5-7B-Instruct", tensor_parallel_size=2, # GPU数量 max_num_batched_tokens=4096, gpu_memory_utilization=0.9 ) sampling_params = SamplingParams( temperature=0.7, top_p=0.9, max_tokens=512 ) ``` ### 四、启动API服务 ```bash python -m vllm.entrypoints.openai.api_server \ --model /path/to/Qwen2.5-7B-Instruct \ --served-model-name Qwen2.5-7B \ --port 8000 \ --tensor-parallel-size 2 ``` ### 五、调用验证 ```python import openai client = openai.OpenAI(base_url="http://localhost:8000/v1") response = client.chat.completions.create( model="Qwen2.5-7B", messages=[{"role": "user", "content": "解释量子纠缠"}] ) print(response.choices[0].message.content) ``` ### 六、昇腾芯片注意事项 若使用昇腾910B2芯片(如引用[2]所述),需注意: 1. VLLM原生仅支持CUDA,昇腾部署需通过CANN工具链适配 2. 可考虑采用华为MindSpore框架或LightLLM(引用[1])替代方案
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值