
self-llm 如何用 vLLM 部署 MiniCPM5-1B 服务并检查启动日志与接口可用性【免费下载链接】self-llm《开源大模型食用指南》针对中国宝宝量身打造的基于Linux环境快速微调全参数/Lora、部署国内外开源大模型LLM/多模态大模型MLLM教程项目地址: https://gitcode.com/GitHub_Trending/se/self-llm本文面向需要在 Linux NVIDIA GPU 环境本地部署MiniCPM5-1B的开发者。教程来自self-llm《开源大模型食用指南》仓库中的 01-MiniCPM5-1B-vLLM 部署调用 文档使用vLLM启动一个兼容 OpenAI API 的推理服务然后通过启动日志确认引擎初始化成功再用curl和 Python 客户端验证/v1/models与/v1/chat/completions接口可用。MiniCPM5-1B采用标准LlamaForCausalLM架构主流推理引擎可直接加载无需自定义算子。文档实测基础环境如下---------------- ubuntu 22.04 python 3.12 NVIDIA 驱动 580.105.08 GPU: RTX 4090 D (24G) torch 2.11.0cu128 vllm 0.23.0 ----------------文档默认你已配置好 PyTorch (CUDA) 环境如未配置请先自行安装。环境准备安装依赖文档给出的安装命令如下其中先切换 pip 到清华源再安装各依赖python -m pip install --upgrade pip pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip install modelscope pip install transformers5.6 pip install vllm0.21 pip install openaimodelscope用于下载模型transformers/vllm文档实测版本分别为transformers5.6、vllm 0.23.0安装要求0.21openai用于后续 Python 客户端请求示例。下载 MiniCPM5-1B 模型使用 modelscope 的snapshot_download函数下载模型。新建model_download.py# model_download.py from modelscope import snapshot_download model_dir snapshot_download(OpenBMB/MiniCPM5-1B, cache_dir/root/autodl-tmp) print(f模型下载完成保存路径为{model_dir})执行python model_download.py即可。cache_dir请改成你自己的模型下载路径后续启动命令中的模型路径要与它保持一致。启动 vLLM 服务MiniCPM5-1B兼容 OpenAI API 协议。文档中的启动命令及参数说明--host/--port服务地址与端口--model模型路径--served-model-name服务对外的模型名称--max-model-len最大上下文长度文档说明 1B 模型在 24G 显存上可设4096或更大--gpu-memory-utilization显存占用比例1B 模型很小0.6 即可--trust-remote-code信任远程代码vllm serve /root/autodl-tmp/OpenBMB/MiniCPM5-1B \ --served-model-name MiniCPM5-1B \ --max-model-len 4096 \ --gpu-memory-utilization 0.6 \ --trust-remote-code \ --host 0.0.0.0 --port 8000注意/root/autodl-tmp是文档示例中的 AutoDL 缓存目录。如果你把模型下载到了别的位置请把这条命令里的模型路径替换为你自己的实际路径--served-model-name保持不变它是后续接口请求里model字段要填的名称。检查启动日志如何判断服务启动成功启动过程的日志截图文档示例文档记录的实测启动日志示例输出(APIServer) INFO [model.py:611] Resolved architecture: LlamaForCausalLM (EngineCore) INFO [core.py:113] Initializing a V1 LLM engine (v0.23.0) ... (EngineCore) INFO [default_loader.py:397] Loading weights took 0.52 seconds (EngineCore) INFO [model_runner.py:319] Model loading took 2.09 GiB and 2.14 seconds (EngineCore) INFO [gpu_worker.py:480] Available KV cache memory: 11.54 GiB (EngineCore) INFO [kv_cache_utils.py:1744] GPU KV cache size: 504,192 tokens (EngineCore) INFO [core.py:306] init engine (profile, create kv cache, warmup model) took 39.05 s (compilation: 18.97 s) (APIServer) INFO: Application startup complete.判断启动是否成功看两点架构行Resolved architecture: LlamaForCausalLM确认 vLLM 识别了模型架构最后一行(APIServer) INFO: Application startup complete.出现即说明服务成功启动。文档还指出首次启动会触发torch.compile编译实测约 19s体现在init engine ... took 39.05 s (compilation: 18.97 s)这一行编译结果会缓存后续启动更快。所以上面的秒数是文档实测示例值你的环境会因硬件不同而变化不需要按这些数字做成功判定。检查接口可用性/v1/models服务启动后先确认模型已注册curl http://localhost:8000/v1/models文档实测返回示例输出{ object: list, data: [ { id: MiniCPM5-1B, object: model, owned_by: vllm, root: /root/autodl-tmp/OpenBMB/MiniCPM5-1B, max_model_len: 4096 } ] }返回中id与--served-model-name一致MiniCPM5-1B说明模型名配置生效max_model_len与启动参数--max-model-len 4096对应。调用 Chat Completions 验证推理能力curl 测试非思考模式MiniCPM5-1B内置thinkchat template可通过chat_template_kwargs.enable_thinking按请求级别控制模式模式推荐参数enable_thinking思考模式temperature0.9, top_p0.95True非思考模式temperature0.7, top_p0.95False文档的非思考模式 curl 测试命令curl http://localhost:8000/v1/chat/completions \ -H Content-Type: application/json \ -d { model: MiniCPM5-1B, messages: [ {role: user, content: 你是谁用一句话介绍自己。} ], temperature: 0.7, top_p: 0.95, max_tokens: 256, extra_body: {chat_template_kwargs: {enable_thinking: false}} }实测返回示例输出finish_reason为stop{ id: chatcmpl-9c66165de8661ff3, object: chat.completion, model: MiniCPM5-1B, choices: [ { index: 0, message: { role: assistant, content: ... }, finish_reason: stop } ], usage: { prompt_tokens: 15, completion_tokens: 57, total_tokens: 72 } }文档实测发现MiniCPM5-1B即便在非思考模式下也常在content开头先输出一段简短的think ... /think再给出回答这是该模型后训练形成的习惯。若需要纯粹的非思考输出可适当调大max_tokens。Python 脚本请求思考模式配合openai客户端的示例思考模式# vllm_openai_chat_completions.py from openai import OpenAI client OpenAI( api_keysk-xxx, # 随便填写只是为了通过接口参数校验 base_urlhttp://localhost:8000/v1, ) # 思考模式模型会先输出推理过程 chat_outputs client.chat.completions.create( modelMiniCPM5-1B, messages[{role: user, content: 5的阶乘是多少}], temperature0.9, top_p0.95, extra_body{chat_template_kwargs: {enable_thinking: True}}, ) print(chat_outputs.choices[0].message.content)文档记录的输出示例包含think ... /think推理过程与最终答案think 5 的阶乘记作 5!等于 5 × 4 × 3 × 2 × 1 ... /think 5 的阶乘5! 5 × 4 × 3 × 2 × 1 120。观察运行时日志请求处理过程中API 后端会持续打印日志与统计信息。文档记录的运行时日志示例输出(EngineCore) INFO [core.py:306] init engine (profile, create kv cache, warmup model) took 39.05 s (compilation: 18.97 s) (APIServer) INFO: Application startup complete. (APIServer) INFO: 127.0.0.1:34630 - POST /v1/chat/completions HTTP/1.1 200 OK (APIServer) INFO: 127.0.0.1:34660 - POST /v1/chat/completions HTTP/1.1 200 OK看到POST /v1/chat/completions ... 200 OK说明请求已被服务正常处理接口链路验证完成。启动报错处理flash_attn 命名空间冲突文档记录了启动时可能遇到的一类报错现象ModuleNotFoundError: No module named flash_attn.ops原因环境里装了flash-attn-4会留下一个空的flash_attn命名空间包而 vLLM 的 rotary 模块检测到flash_attn后会尝试导入其.ops子模块。解决pip uninstall flash-attn-4并删除残留的空目录rm -rf $(python -c import site;print(site.getsitepackages()[0]))/flash_attnvLLM 会自动回退到自带实现。注意上面第二条命令会强制删除 site-packages 下的flash_attn目录删除前请确认该目录确为空的残留命名空间包且环境没有其它依赖它的组件。边界说明本文所有日志与返回值均为文档在 ubuntu 22.04 RTX 4090 D (24G) vllm 0.23.0 环境下的实测示例输出硬件与版本不同时数值会有差异--max-model-len文档仅说明「1B 模型在 24G 显存上可设 4096 或更大」更大值请按自身显存调整模型除对话外还支持 XML 风格工具调用vLLM 较新版本可配合--tool-call-parser使用该能力与本文的部署和接口验证流程相互独立可另行参考 MiniCPM 官方 cookbook。【免费下载链接】self-llm《开源大模型食用指南》针对中国宝宝量身打造的基于Linux环境快速微调全参数/Lora、部署国内外开源大模型LLM/多模态大模型MLLM教程项目地址: https://gitcode.com/GitHub_Trending/se/self-llm创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考