vLLM-Omni 文生图在线服务实战:基于 Qwen-Image 的部署、API 调用与 LoRA 扩展 vLLM-Omni 文生图在线服务实战基于 Qwen-Image 的部署、API 调用与 LoRA 扩展【免费下载链接】vllm-omniA framework for efficient model inference with omni-modality models项目地址: https://gitcode.com/GitHub_Trending/vl/vllm-omni导读本文以 vLLM-Omni 仓库中的文生图Text-to-Image在线服务示例为主线完整讲解如何基于 Qwen-Image 模型搭建 OpenAI 兼容的图像生成服务。你将掌握一条命令启动推理服务、通过 curl / OpenAI SDK / 自带 Python 客户端 / Gradio 四种方式调用生成接口、理解extra_body与/v1/images/generations两类请求格式的区别、熟悉 Tensor Parallelism、VAE Patch Parallelism、Ulysses-SP、Ring-Attention 等并行加速手段并学会为扩散模型挂载 PEFT 格式 LoRA 适配器。文中所有命令、参数与代码均可直接对照 示例目录 中的实际脚本运行。示例与底层能力概述本示例位于 examples/online_serving/text_to_image默认以Qwen-Image为目标模型但它并不局限于该模型——README 明确指出启动脚本与 Python 客户端默认指向 Qwen-ImageMODEL环境变量可以切换为任意受支持的文生图模型curl 辅助脚本则是一个固定的 Qwen-Image 请求样例。底层由 vLLM-Omni 的扩散diffusion能力支撑vllm serve model --omni会加载对应的扩散流水线并将图像生成结果以 OpenAI 兼容接口暴露给用户。仓库中 Qwen-Image 模型实现 包含 transformer、VAE 自编码器autoencoder_kl_qwenimage.py、CFG 并行cfg_parallel.py以及多条流水线文本生成、图像编辑等image_generation_api.md 对这套 DALL-E 兼容 API 做了完整定义。若想了解并行加速的细节可进一步查阅 并行加速指南位于示例文档的上一层目录此处给出仓库根路径。启动服务基础启动一条命令即可拉起文生图在线服务vllm serve Qwen/Qwen-Image --omni --port 8091注意OOM 提示如果遇到显存不足Out-of-Memory或 GPU 显存有限可以开启 VAE 切片与平铺以降低显存占用vllm serve Qwen/Qwen-Image --omni --port 8091 --vae-use-slicing --vae-use-tilingVAE 相关的这些开关在仓库中的配置层有正式定义例如 omni_config.py、stage_config.py 中均有对应字段且被 分布式 VAE 执行器 与 vae_patch_parallel.py 等实现消费说明该参数并非示例专用而是扩散服务通用能力。使用启动脚本示例仓库提供了封装好的启动脚本默认模型与端口通过环境变量控制bash run_server.sh脚本内容如下run_server.sh#!/bin/bash # Shared text-to-image online serving startup script MODEL${MODEL:-Qwen/Qwen-Image} PORT${PORT:-8091} echo Starting text-to-image server... echo Model: $MODEL echo Port: $PORT vllm serve $MODEL --omni \ --port $PORT因此你可以通过环境变量切换到其他文生图模型或端口例如MODELSomeOrg/SomeModel PORT9000 bash run_server.sh。并行加速启动vLLM-Omni 为扩散推理提供多种并行加速手段均以启动参数形式启用要求多卡环境# 张量并行Tensor Parallelism需要 2 张 GPU vllm serve Qwen/Qwen-Image --omni --port 8091 --tensor-parallel-size 2 # 张量并行 VAE Patch 并行需要 2 张 GPU vllm serve Qwen/Qwen-Image --omni --port 8091 --tensor-parallel-size 2 --vae-patch-parallel-size 2 --vae-use-tiling # 序列并行Ulysses-SP需要 2 张 GPU vllm serve Qwen/Qwen-Image --omni --port 8091 --usp 2 # Ring-Attention需要 2 张 GPU vllm serve Qwen/Qwen-Image --omni --port 8091 --ring 2 # 组合Ulysses Ring需要 4 张 GPU vllm serve Qwen/Qwen-Image --omni --port 8091 --usp 2 --ring 2Tensor ParallelismTP将 transformer 权重切分到多张 GPU 上并行计算是最常用的单模型加速手段VAE Patch Parallelism将 VAE 解码过程按图像 patch 切分并行配合--vae-use-tiling能进一步压低解码阶段显存峰值实现代码见 vae_patch_parallel.pyUlysses-SP / Ring-Attention针对超长序列注意力如高分辨率图像 token 序列的序列并行方案--usp 2 --ring 2组合时两者叠加需要至少 4 张 GPU。更完整的并行方案矩阵参见 并行加速指南。API 调用方式vLLM-Omni 同时提供两个 OpenAI 兼容入口/v1/chat/completions对话式生成参数放在extra_body中与/v1/images/generationsDALL-E 风格生成参数放在顶层字段。下面逐一演示。方法一curl直接执行仓库提供的脚本bash run_curl_text_to_image.sh该脚本run_curl_text_to_image.sh走的是/v1/images/generations端点#!/bin/bash # Qwen-Image text-to-image curl example curl -X POST http://localhost:8091/v1/images/generations \ -H Content-Type: application/json \ -d { prompt: a dragon laying over the spine of the Green Mountains of Vermont, size: 1024x1024, seed: 42 } | jq -r .data[0].b64_json | base64 -d dragon.png也可以直接通过/v1/chat/completions调用生成参数以extra_body包裹响应中的 base64 数据经cut与base64 -d解码落盘curl -s http://localhost:8091/v1/chat/completions \ -H Content-Type: application/json \ -d { messages: [ {role: user, content: A beautiful landscape painting} ], extra_body: { height: 1024, width: 1024, num_inference_steps: 50, true_cfg_scale: 4.0, seed: 42 } } | jq -r .choices[0].message.content[0].image_url.url | cut -d, -f2- | base64 -d output.png方法二OpenAI Python SDKfrom openai import OpenAI import base64 client OpenAI(base_urlhttp://localhost:8091/v1, api_keynone) response client.chat.completions.create( modelQwen/Qwen-Image, messages[{role: user, content: A beautiful landscape painting}], extra_body{ height: 1024, width: 1024, num_inference_steps: 50, true_cfg_scale: 4.0, seed: 42, }, ) img_url response.choices[0].message.content[0].image_url.url _, b64_data img_url.split(,, 1) with open(output.png, wb) as f: f.write(base64.b64decode(b64_data))关键约定使用 OpenAI SDK 时生成参数必须通过extra_body关键字传入SDK 会自动将其合并进请求体顶层而使用 curl 或 Pythonrequests直连时则要在 JSON 里显式写出extra_body这个键如方法一所示。两种方式不可混淆。方法三使用自带 Python 客户端示例提供了功能完整的命令行客户端 openai_chat_client.pypython openai_chat_client.py --prompt A beautiful landscape painting --output output.png该客户端基于requests直连/v1/images/generations常用参数如下python openai_chat_client.py \ --prompt A sunset \ --height 1024 --width 1024 \ --steps 50 --seed 42 \ --cfg-scale 4.0 \ --negative blurry, low quality \ --output output.png支持的完整参数集与脚本 openai_chat_client.py 中argparse定义一致参数说明默认值--prompt/-p图像内容描述a cup of coffee on the table--output/-o输出 PNG 文件路径text_to_image_output.png--server/-s服务地址http://localhost:8091--height/--width图像高/宽像素未指定时用模型默认无--steps扩散推理步数模型默认--cfg-scaleTrue CFG scale模型支持时生效无--seed随机种子用于复现无--negative负向提示词无--lora-path服务端本地 LoRA 适配器目录PEFT 格式无--lora-nameLoRA 名称可选无--lora-scaleLoRA 缩放系数1.0--lora-int-idLoRA 整数 ID缓存键省略时服务端由路径推导无--use-system-prompt系统提示词预设en_unified/en_vanilla/en_recaption/en_think_recaption/dynamic/None或直接传自定义文本仅 HunyuanImage-3.0 支持无--system-prompt自定义系统提示词文本仅在--use-system-prompt传自定义文本时使用无从客户端实现看图像尺寸通过size字段传递如1024x1024num_inference_steps、true_cfg_scale、negative_prompt、seed等扩展参数则作为顶层字段下发LoRA 通过lora字段携带local_path/name/scale/int_id并显式设置response_format: b64_json、n: num_outputs_per_prompt。方法四Gradio 交互式 Demopython gradio_demo.py # 访问 http://localhost:7860依赖提示Gradio Demo 属于可选依赖需要先安装[demo]extraspip install vllm-omni[demo]若从源码安装pip install -e .[demo]Demo 脚本gradio_demo.py走/v1/chat/completions接口把height、width、num_inference_steps、true_cfg_scale放进extra_bodyseed 0时才带上 seed非空负向提示词才带negative_prompt。界面提供 Prompt、Negative Prompt、Height/Width256–2048步长 64、Inference Steps10–100默认 100、True CFG Scale1.0–20.0默认 4.0、Seed-1 表示随机等控件并内置四个示例山水风景、窗台猫咪、赛博朋克城市、水墨竹林。也支持--server默认http://localhost:8091、--port默认 7860、--share生成公网链接三个启动参数。LoRA 适配器支持该示例支持两种 LoRA 使用形态适配器路径必须在服务端机器上可读通常为本地路径或挂载目录。启动时蒸馏 LoRA在服务启动阶段将蒸馏 LoRA 融合进模型对所有请求生效vllm serve Qwen/Qwen-Image-2512 \ --omni \ --port 8091 \ --lora-backend distill \ --lora-path /path/to/Qwen-Image-2512-Lightning-4steps.safetensors蒸馏 LoRA 在服务初始化时融合一次之后每次请求自动应用。请配合该 checkpoint 的少步采样设定使用例如num_inference_steps4、true_cfg_scale1.0。请求时 PEFT LoRA通过 Python 客户端python openai_chat_client.py \ --prompt A piece of cheesecake \ --lora-path /path/to/lora_adapter \ --lora-name my_lora \ --lora-scale 1.0 \ --output output.png通过 curlImages API/v1/images/generations端点请求体支持lora字段curl -X POST http://localhost:8091/v1/images/generations \ -H Content-Type: application/json \ -d { prompt: A piece of cheesecake, size: 1024x1024, seed: 42, lora: { name: my_lora, local_path: /path/to/lora_adapter, scale: 1.0 } } | jq -r .data[0].b64_json | base64 -d output.pngLoRA 参数参数类型说明namestrLoRA 适配器名称可选默认取路径末段 stemlocal_pathstr服务端本地 LoRA 适配器目录PEFT 格式必填scalefloatLoRA 缩放系数默认 1.0int_idintLoRA 整数 ID用于缓存可选未提供时由路径推导LoRA 适配器格式请求时 LoRA 必须为 PEFTParameter-Efficient Fine-Tuning格式典型目录结构lora_adapter/ ├── adapter_config.json └── adapter_model.safetensors请求格式详解简单文本生成{ messages: [ {role: user, content: A beautiful landscape painting} ] }携带生成参数通过/v1/chat/completions时生成参数包裹在extra_body中{ messages: [ {role: user, content: A beautiful landscape painting} ], extra_body: { height: 1024, width: 1024, num_inference_steps: 50, true_cfg_scale: 4.0, seed: 42 } }提示OpenAI SDK使用 OpenAI Python SDK 时通过extra_body关键字传参即可SDK 会自动合并进请求体顶层client.chat.completions.create( modelQwen/Qwen-Image, messages[...], extra_body{height: 1024, width: 1024, num_inference_steps: 50}, )关于生成参数在不同客户端中的处理细节可参阅 Chat Completions API 指南。多模态结构化输入content 也可使用结构化的多模态数组形式{ messages: [ { role: user, content: [ {type: text, text: A beautiful landscape painting} ] } ] }生成参数对照表两种接口的参数位置约定使用/v1/chat/completions时参数放在extra_bodycurl JSON 里的extra_body键或 OpenAI SDK 的extra_body关键字使用专门的 图像生成 API/v1/images/generations时受支持的生成控制参数作为顶层 JSON 字段直接传入图像尺寸与数量请使用size与n而不要使用height、width或num_outputs_per_prompt。参数类型默认值说明heightintNone图像高度像素widthintNone图像宽度像素sizestrNone图像尺寸如1024x1024num_inference_stepsint50chat模型默认images去噪步数true_cfg_scalefloat4.0chat模型默认imagesQwen-Image 的 True CFG 缩放系数seedintNone随机种子可复现negative_promptstrNone负向提示词num_outputs_per_promptint1生成图像数量use_system_promptstrNone系统提示词预设en_unified、en_vanilla、en_recaption、en_think_recaption、dynamic、None或自定义文本仅 HunyuanImage-3.0 支持system_promptstrNone自定义系统提示词文本仅当use_system_prompt设置为 custom 时使用仅 HunyuanImage-3.0 支持补充说明Qwen-Image 对应的去噪调度逻辑可在 pipeline_qwen_image.py 中看到如num_inference_steps会传递给 scheduler 的set_timesteps而在/v1/images/generations接口侧参数采取直通pass-through设计——API 层只做基本类型与范围校验直接把用户值透传给扩散引擎未指定的参数沿用模型自身默认值模型不支持的参数会被静默忽略不兼容的取值则由底层流水线报错。因此最佳实践是先从模型推荐参数起步再按需微调。响应格式与图像提取Chat Completions 响应结构{ id: chatcmpl-xxx, created: 1234567890, model: Qwen/Qwen-Image, choices: [{ index: 0, message: { role: assistant, content: [{ type: image_url, image_url: { url: data:image/png;base64,... } }] }, finish_reason: stop }], usage: {...} }Images API 响应结构/v1/images/generations返回 OpenAI 标准结构图像以 base64 形式放在data[0].b64_json{ created: 1701234567, data: [ { b64_json: base64-encoded PNG, url: null, revised_prompt: null } ] }提取并保存图像# 从响应 JSON 中提取 base64 并解码为图片 cat response.json | jq -r .choices[0].message.content[0].image_url.url | cut -d, -f2- | base64 -d output.png该命令先取data:image/png;base64,前缀之后的部分cut -d, -f2-再由base64 -d解码写入output.png。示例文件清单文件说明run_server.sh服务启动脚本MODEL/PORT环境变量可覆盖run_curl_text_to_image.shcurl 调用示例Images APIopenai_chat_client.py功能完整的 Python 命令行客户端gradio_demo.pyGradio 交互式界面 Demo常见问题与调优显存不足OOM优先开启--vae-use-slicing --vae-use-tiling降低 VAE 解码显存在 图像生成 API 文档 的 Troubleshooting 中还给出三步降载方案——缩小图像尺寸如size: 512x512、减少推理步数如num_inference_steps: 25、减少单次生成数量n: 1。多卡加速按上文并行启动方式组合 TP / VAE Patch / Ulysses-SP / Ring-Attention注意各方案的最低 GPU 数量要求。模型不支持某参数由于参数是直通透传的不支持的参数会被模型静默忽略或由底层流水线报错请以模型自身文档推荐的参数范围为准。LoRA 路径访问请求时 LoRA 的local_path是服务端路径必须保证服务进程可读客户端只是把路径字符串透传给服务端。调试日志可通过--uvicorn-log-level debug启动服务观察 prompts 与生成细节日志。回归验证仓库在 test_image_server.py 中覆盖了size解析含非法/负值/边界用例、base64 编码、健康检查、单图生成、并发参数校验等场景可作为接口行为的事实依据与自测参考。结语vLLM-Omni 的文生图在线服务把「Qwen-Image 等扩散模型的推理部署」收敛为一条vllm serve --omni命令并提供与 OpenAI 兼容的双端点 API/v1/chat/completions与/v1/images/generations同时支持多卡并行加速与 LoRA 扩展。建议的落地路径是先用 run_server.sh 起服务 → 用 run_curl_text_to_image.sh 验证连通 → 按需切换 openai_chat_client.py 或 gradio_demo.py 做交互与批量调用最终结合并行加速参数与 LoRA 适配器投入到实际生产场景。【免费下载链接】vllm-omniA framework for efficient model inference with omni-modality models项目地址: https://gitcode.com/GitHub_Trending/vl/vllm-omni创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考