【个人开发】cuda12.6安装vllm安装实践【内含踩坑经验】

1. 背景

vLLM是一个快速且易于使用的LLM推理和服务库。企业级应用比较普遍,尝试安装相关环境,尝试使用。

2. 环境

模块 版本
python 3.10
CUDA 12.6
torch 2.5.1
xformers 0.0.28.post3
flash_attn 2.7.4
vllm 0.6.4.post1

2.1 安装flash_attn

具体选择什么版本,可参考:flash-attention保姆级安装教程

基于cuda跟torch的版本考虑,笔者选择如下版本

复制代码
flash_attn-2.7.4.post1+cu12torch2.5cxx11abiFALSE-cp310-cp310-linux_x86_64.whl

安装命令

shell 复制代码
wget https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.4.post1/flash_attn-2.7.4.post1+cu12torch2.5cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
MAX_JOBS=4 
pip install flash_attn-2.7.4.post1+cu12torch2.5cxx11abiFALSE-cp310-cp310-linux_x86_64.whl

2.2 安装vllm

参考:vLLM环境安装与运行实例【最新版(0.6.4.post1)】

shell 复制代码
pip3 install vllm==0.6.4.post1 --extra-index-url https://download.pytorch.org/whl/cu121 -i https://pypi.tuna.tsinghua.edu.cn/simple
2.2.1 坑1: 报错:libcudart.so.11.0: cannot open shared object file: No such file or directory

这个文件应该指向的是cuda的文件。到cuda的路径看看【笔者cuda的路径为:/usr/local/cuda/lib64】

发现确实只有libcudart.so.12。没有libcudart.so.11。

那么这里只有两种解决方案,要么重装cuda,要么重装vllm。

借鉴楼上大佬的经验参考,将vllm 版本降至0.6.4.post1即可解决。

3. 启动服务

3.1 vllm启动服务

使用vllm启动模型/root/Qwen2.5-7B-Instruct。

参考文章:使用 vllm 搭建推理加速大模型服务

shell 复制代码
python -m vllm.entrypoints.openai.api_server --model /root/Qwen2.5-7B-Instruct  --served-model-name Qwen2.5-7B-Instruct --max-model-len=2048

3.2 vllm一次性调用

python 复制代码
# vllm_model.py
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
import os
import json

# 自动下载模型时,指定使用modelscope。不设置的话,会从 huggingface 下载
os.environ['VLLM_USE_MODELSCOPE']='True'

def get_completion(prompts, model, tokenizer=None, max_tokens=512, temperature=0.8, top_p=0.95, max_model_len=2048):
    stop_token_ids = [151329, 151336, 151338]
    # 创建采样参数。temperature 控制生成文本的多样性,top_p 控制核心采样的概率
    sampling_params = SamplingParams(temperature=temperature, top_p=top_p, max_tokens=max_tokens, stop_token_ids=stop_token_ids)
    # 初始化 vLLM 推理引擎
    llm = LLM(model=model, tokenizer=tokenizer, max_model_len=max_model_len,trust_remote_code=True)
    outputs = llm.generate(prompts, sampling_params)
    return outputs


if __name__ == "__main__":    
    # 初始化 vLLM 推理引擎
    model='/root/Qwen2.5-7B-Instruct' # 指定模型路径
    # model="qwen/Qwen2-7B-Instruct" # 指定模型名称,自动下载模型
    tokenizer = None
    # 加载分词器后传入vLLM 模型,但不是必要的。
    # tokenizer = AutoTokenizer.from_pretrained(model, use_fast=False) 
    
    text = ["你好,帮我介绍一下什么是大语言模型。",
            "可以给我将一个有趣的童话故事吗?"]
    # messages = [
    #     {"role": "system", "content": "你是一个有用的助手。"},
    #     {"role": "user", "content": prompt}
    # ]
    # 作为聊天模板的消息,不是必要的。
    # text = tokenizer.apply_chat_template(
    #     messages,
    #     tokenize=False,
    #     add_generation_prompt=True
    # )

    outputs = get_completion(text, model, tokenizer=tokenizer, max_tokens=512, temperature=1, top_p=1, max_model_len=2048)

    # 输出是一个包含 prompt、生成文本和其他信息的 RequestOutput 对象列表。
    # 打印输出。
    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text
        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

效果如下:

4. 模型调用

shell 复制代码
curl http://localhost:8000/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "Qwen2.5-7B-Instruct",
        "prompt": "请基于如下的知识点,帮忙总结一下该病例的关键信息点。

        ",
        "max_tokens": 50,
        "temperature": 0
    }'

参考文章:
1.flash-attention保姆级安装教程
2.vLLM环境安装与运行实例【最新版(0.6.4.post1)】
3.使用 vllm 搭建推理加速大模型服务
4.[大模型]Qwen2-7B-Instruct vLLM 部署调用

以上,结束。

相关推荐
您^_^1 天前
专家(一):Claude Code 微服务实战——6 个服务从拆分到 K8s 部署,$0.45 全套 YAML 照抄
人工智能·windows·微服务·架构·kubernetes·个人开发·claude code
aaaffaewrerewrwer1 天前
免费在线图片压缩工具推荐|批量压缩 + 无需上传 + 浏览器本地处理
安全·个人开发
YuanDaima20481 天前
云计算基础与容器技术演进
java·服务器·人工智能·python·深度学习·云计算·个人开发
500佰1 天前
我唯一的一个变现产品,说说它的逻辑
网络·职场和发展·idea·个人开发·软件需求
00后程序员张1 天前
iOS开发中Xcode安装不完整问题解决方案与配置指南
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
aaaffaewrerewrwer2 天前
免费在线 2048 游戏推荐|经典数字合并玩法 + 流畅浏览器体验
安全·游戏·个人开发
PM老周2 天前
如何打造高效研发团队文化?组织建设的管理实践与落地指南
团队开发·个人开发·研发管理·组织建设·团队文化
笨笨饿3 天前
80_聊聊SPI以及它们的变体
linux·c语言·网络·stm32·单片机·算法·个人开发
Derrick__13 天前
LangChain基础实战手记:如何给大模型装上“大脑(记忆)”和“双手(工具)”?
人工智能·python·langchain·个人开发
2501_916008893 天前
Xcode功能、下载、反馈与版本支持详细解析
ide·vscode·macos·ios·个人开发·xcode·敏捷流程