self-llm 如何用 vLLM 部署 Step-3.5-Flash 并基于 Docker 镜像准备运行环境
2026/9/13 2:58:21 网站建设 项目流程

self-llm 如何用 vLLM 部署 Step-3.5-Flash 并基于 Docker 镜像准备运行环境

【免费下载链接】self-llm《开源大模型食用指南》针对中国宝宝量身打造的基于Linux环境快速微调(全参数/Lora)、部署国内外开源大模型(LLM)/多模态大模型(MLLM)教程项目地址: https://gitcode.com/GitHub_Trending/se/self-llm

Step-3.5-Flash 是阶跃星辰推出的稀疏混合专家(Sparse MoE)模型,1960 亿参数、激活约 110 亿。要把它跑成可调用的推理服务,需要先准备好 vLLM 运行环境,再用vllm serve把模型暴露为兼容 OpenAI 的 API 服务。本文按 self-llm 仓库中的 01-Step-3.5-Flash-vLLM部署教程.md 完成这条路径:准备运行环境(可直接使用 AutoDL 平台提供的 Docker 镜像,也可用 pip 手动安装依赖)→ 用 modelscope 下载模型 → 用 Transformers 验证模型 → 启动 vLLM 服务 → 用 requests 和 OpenAI SDK 调用接口确认部署成功。

文档给出的基础环境是 ubuntu 22.04、python 3.12、cuda 12.4、pytorch 2.5.1,并默认学习者已配置好 PyTorch (CUDA) 环境,如未配置需先自行安装。

环境准备:两种搭建方式二选一

方式一:使用 AutoDL 的 Docker 镜像(可选分支)

文档在“环境准备”一节说明:考虑到部分同学配置环境可能会遇到一些问题,项目在 AutoDL 平台准备了 Step-3.5-Flash 的环境镜像,点击文档中给出的链接并直接创建 AutoDL 实例即可(链接位于 01-Step-3.5-Flash-vLLM部署教程.md 的“环境准备”小节)。走这条路径时,文档中手动安装依赖的步骤可以直接跳过,后续下载模型、启动服务的步骤与方式二完全一致。

方式二:手动安装依赖

在已配置好 PyTorch (CUDA) 的环境中执行:

python -m pip install --upgrade pip pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip install modelscope pip install vllm --pre --extra-index-url https://wheels.vllm.ai/nightly

注意pip config set global.index-url ...会把本机 pip 的默认索引源改为清华镜像,影响此后所有pip install,这是文档为加速国内下载给出的配置。两条安装命令中:modelscope用于后续下载模型;vllm使用--pre加 nightly 额外源安装预发布版本。

用 modelscope 下载 Step-3.5-Flash 模型

使用 modelscope 的snapshot_download函数下载模型:第一个参数为模型名称,参数cache_dir为模型的下载路径。新建model_download.py文件:

# model_download.py from modelscope import snapshot_download model_dir = snapshot_download('stepfun-ai/Step-3.5-Flash', cache_dir='/root/autodl-tmp', revision='master') print(f"模型下载完成,保存路径为:{model_dir}")
python model_download.py

需要等待一段时间直到下载完成。文档特别提示:cache_dir要修改为你的模型下载路径。示例中的/root/autodl-tmp是 AutoDL 数据盘路径,在其他机器上应换成自己可写的绝对路径。脚本结束时会打印“模型下载完成,保存路径为:{model_dir}”,记下这个路径,后续启动服务时会用到。

用 Transformers 预先验证模型

文档建议在正式用 vLLM 部署之前,先用 Transformers 验证模型能否正常推理。新建transformers_inference.py文件,其中MODEL_PATH注释已标明要修改为你的本地路径:

# transformers_inference.py from transformers import AutoModelForCausalLM, AutoTokenizer # 模型路径:修改为你的本地路径 MODEL_PATH = "/root/autodl-tmp/stepfun-ai/Step-3.5-Flash" # 1) 加载 tokenizer tokenizer = AutoTokenizer.from_pretrained( MODEL_PATH, local_files_only=True, trust_remote_code=True, ) # 2) 加载模型 model = AutoModelForCausalLM.from_pretrained( MODEL_PATH, trust_remote_code=True, device_map="auto", local_files_only=True, ) # 3) 构造对话消息 messages = [{"role": "user", "content": "Explain the significance of the number 42."}] # 4) 应用 chat 模板 inputs = tokenizer.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt", ).to(model.device) # 5) 生成文本 generated_ids = model.generate( **inputs, max_new_tokens=128, do_sample=True, temperature=0.7, top_p=0.9, ) # 6) 解码输出 gen_ids = generated_ids[0][inputs.input_ids.shape[1]:] output_text = tokenizer.decode(gen_ids, skip_special_tokens=True) print(output_text)
python transformers_inference.py

文档给出的输出示例(示例结果,实际输出取决于模型采样):

Hmm, the user is asking about the significance of the number 42...

终端能针对该问题打印出模型的回复文本,说明下载下来的模型文件可以正常加载推理,可以继续下一步 vLLM 部署。

启动 vLLM 推理服务

文档建议使用 vLLM 的最新 nightly 版本以获得最佳性能,并在“vLLM 服务部署”一节给出了安装命令(与“环境准备”一节的pip install vllm --pre ...二选一执行即可):

# 通过 pip 安装(nightly 版本) pip install -U vllm --pre \ --index-url https://pypi.org/simple \ --extra-index-url https://wheels.vllm.ai/nightly

FP8 模型启动

vllm serve /root/autodl-tmp/stepfun-ai/Step-3.5-Flash \ --served-model-name step3p5-flash \ --tensor-parallel-size 8 \ --disable-cascade-attn \ --reasoning-parser step3p5 \ --enable-auto-tool-choice \ --tool-call-parser step3p5 \ --trust-remote-code \ --quantization fp8

BF16 模型启动

BF16 启动命令在 FP8 基础上额外启用了专家并行与推测解码配置:

vllm serve /root/autodl-tmp/stepfun-ai/Step-3.5-Flash \ --served-model-name step3p5-flash \ --tensor-parallel-size 8 \ --enable-expert-parallel \ --disable-cascade-attn \ --reasoning-parser step3p5 \ --enable-auto-tool-choice \ --tool-call-parser step3p5 \ --hf-overrides '{"num_nextn_predict_layers": 1}' \ --speculative_config '{"method": "step3p5_mtp", "num_speculative_tokens": 1}' \ --trust-remote-code

两条命令中的模型路径同样是 AutoDL 数据盘示例路径,需替换为你的实际模型目录。

参数说明

文档对各参数的说明如下:

参数文档说明
--served-model-name对外暴露的模型名称,客户端调用时需使用此名称
--tensor-parallel-size张量并行大小,通常设置为 GPU 数量
--enable-expert-parallel启用专家并行(适用于 MoE 模型)
--disable-cascade-attn禁用级联注意力机制
--reasoning-parser推理内容解析器,用于解析模型的思考过程
--enable-auto-tool-choice启用自动工具选择
--tool-call-parser工具调用解析器
--trust-remote-code允许加载自定义代码(必需)
--quantization量化方式(如 fp8)
--hf-overrides覆盖 HuggingFace 配置参数
--speculative_config推测解码配置

判断启动成功

  • 启动后日志中会先出现 vLLM 版本号、模型路径、解析到的模型架构等加载信息:

  • 服务启动成功后将监听http://0.0.0.0:8000/v1,日志中列出全部可用路由并等待请求:

  • 文档提示:由于模型较大,首次加载过程时间较长,可能在半个小时以上。
  • 参考显存占用:文档附有启动后的参考显存截图 01-03.jpg 与 01-04.jpg,可对照自己的环境。
  • 文档原文说明:vLLM 尚未完全支持 MTP3,官方正在开发 Pull Request 集成此功能,预计将显著提升解码性能。启动命令中的--reasoning-parser step3p5等参数已按文档保留,用于解析模型的思考过程。

用 API 调用验证部署成功

使用 requests 调用

新建test_requests.py文件,model字段需与启动时的--served-model-name一致:

# test_requests.py import requests url = "http://0.0.0.0:8000/v1/chat/completions" headers = {"Content-Type": "application/json"} data = { "messages": [ {"role": "user", "content": "Explain the significance of the number 42."} ], "model": "step3p5-flash" } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: result = response.json() print("Response:", result['choices'][0]['message']['content']) else: print("Error:", response.status_code, response.text)
python test_requests.py

HTTP 状态码为 200 时脚本打印Response:及模型回复内容,说明服务可用。文档给出的响应示例(部分截取):

{ "id": "chatcmpl-9f51bb9294e6712a", "object": "chat.completion", "created": 1770390777, "model": "step3p5-flash", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The number **42** is most famously known as the **\"Answer to the Ultimate Question of Life, the Universe, and Everything\"** in Douglas Adams' beloved sci-fi series *The Hitchhiker's Guide to the Galaxy*. Its significance is both a hilarious absurdist joke and a cultural phenomenon...", "reasoning_content": "Hmm, the user is asking about the significance of the number 42. This is a classic pop culture reference, so they're likely expecting an explanation tied to Douglas Adams' *The Hitchhiker's Guide to the Galaxy*..." }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 21, "total_tokens": 982, "completion_tokens": 961 } }

注意:响应中包含reasoning_content字段,展示了模型的思考过程。

使用 OpenAI SDK 调用(Chat Completions)

新建test_chat.py文件:

# test_chat.py from openai import OpenAI client = OpenAI( api_key="EMPTY", base_url="http://127.0.0.1:8000/v1", ) response = client.chat.completions.create( model="step3p5-flash", messages=[ {"role": "user", "content": "Explain the significance of the number 42."} ] ) print("Response:", response.choices[0].message.content) print("\nReasoning:", response.choices[0].message.reasoning_content)
python test_chat.py

文档的输出示例(示例结果):

Response: The number **42** is most famously known as the **"Answer to the Ultimate Question of Life, the Universe, and Everything"** in Douglas Adams' beloved sci-fi series *The Hitchhiker's Guide to the Galaxy*... Reasoning: Hmm, the user is asking about the significance of the number 42. This is a classic pop culture reference, so they're likely expecting an explanation tied to Douglas Adams' *The Hitchhiker's Guide to the Galaxy*...

在以上所有请求处理过程中,API 后端都会打印相对应的日志和统计信息,可据此观察服务运行状态。

可选:Completions 接口

文档还提供了一个 Completions 接口示例,新建test_completion.py

# test_completion.py from openai import OpenAI client = OpenAI( api_key="EMPTY", base_url="http://127.0.0.1:8000/v1", ) response = client.completions.create( model="step3p5-flash", prompt="简要介绍一下 Step-3.5-Flash 模型。", max_tokens=500, top_p=0.95, temperature=0.2, ) response_text = response.choices[0].text print(response_text)

边界说明与后续入口

  • 启动命令中已包含--enable-auto-tool-choice--tool-call-parser step3p5,服务支持工具调用;文档附有天气查询的工具调用示例(test_tool_calling.py),其中 WeatherAPI 的api_key需替换为你自己的 key,可回到 01-Step-3.5-Flash-vLLM部署教程.md 的“工具调用”小节查看完整代码。
  • 如果后续想改用 SGLang 框架部署同一模型,仓库提供了 02-Step-3.5-Flash-SGLang.md,环境要求与 vLLM 路线不同,按该文档单独操作。

【免费下载链接】self-llm《开源大模型食用指南》针对中国宝宝量身打造的基于Linux环境快速微调(全参数/Lora)、部署国内外开源大模型(LLM)/多模态大模型(MLLM)教程项目地址: https://gitcode.com/GitHub_Trending/se/self-llm

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询