[Frontend][Model] Support batch request with refined OmniDiffusionReq… (#797)

Signed-off-by: Huang, Zeyu <11222265+fhfuih@users.noreply.github.com>
This commit is contained in:
Zeyu Huang | 黃澤宇 authored and GitHub committed 2026-01-28 16:07:42 +08:00
1 parent 9ece725fc5
commit d64bbde561
64 files changed
+2995 -1414

No files matched your search

@@ -662,21 +662,28 @@ class QwenImagePipeline(
callback_on_step_end_tensor_inputs: list[str] = ["latents"],
max_sequence_length: int = 512,
) -> DiffusionOutput:
# # TODO: only support single prompt now
# if req.prompt is not None:
# prompt = req.prompt[0] if isinstance(req.prompt, list) else req.prompt
prompt = req.prompt
negative_prompt = req.negative_prompt
height = req.height or self.default_sample_size * self.vae_scale_factor
width = req.width or self.default_sample_size * self.vae_scale_factor
num_inference_steps = req.num_inference_steps or num_inference_steps
generator = req.generator or generator
true_cfg_scale = req.true_cfg_scale or true_cfg_scale
if req.guidance_scale_provided:
guidance_scale = req.guidance_scale
req_num_outputs = getattr(req, "num_outputs_per_prompt", None)
if req_num_outputs and req_num_outputs > 0:
num_images_per_prompt = req_num_outputs
# TODO: In online mode, sometimes it receives [{"negative_prompt": None}, {...}], so cannot use .get("...", "")
# TODO: May be some data formatting operations on the API side. Hack for now.
prompt = [p if isinstance(p, str) else (p.get("prompt") or "") for p in req.prompts] or prompt
if all(isinstance(p, str) or p.get("negative_prompt") is None for p in req.prompts):
negative_prompt = None
elif req.prompts:
negative_prompt = ["" if isinstance(p, str) else (p.get("negative_prompt") or "") for p in req.prompts]
height = req.sampling_params.height or self.default_sample_size * self.vae_scale_factor
width = req.sampling_params.width or self.default_sample_size * self.vae_scale_factor
num_inference_steps = req.sampling_params.num_inference_steps or num_inference_steps
sigmas = req.sampling_params.sigmas or sigmas
max_sequence_length = req.sampling_params.max_sequence_length or max_sequence_length
generator = req.sampling_params.generator or generator
true_cfg_scale = req.sampling_params.true_cfg_scale or true_cfg_scale
if req.sampling_params.guidance_scale_provided:
guidance_scale = req.sampling_params.guidance_scale
num_images_per_prompt = (
req.sampling_params.num_outputs_per_prompt
if req.sampling_params.num_outputs_per_prompt > 0
else num_images_per_prompt
)
# 1. check inputs
# 2. encode prompts
# 3. prepare latents and timesteps
@@ -7,7 +7,7 @@ import logging
import math
import os
from collections.abc import Iterable
from typing import Any
from typing import Any, cast
import numpy as np
import PIL.Image
@@ -39,6 +39,7 @@ from vllm_omni.diffusion.models.qwen_image.qwen_image_transformer import (
)
from vllm_omni.diffusion.request import OmniDiffusionRequest
from vllm_omni.diffusion.utils.tf_utils import get_transformer_config_kwargs
from vllm_omni.inputs.data import OmniTextPrompt
from vllm_omni.model_executor.model_loader.weight_utils import (
download_weights_from_hf_specific,
)
@@ -64,16 +65,33 @@ def get_qwen_image_edit_pre_process_func(
latent_channels = vae_config.get("z_dim", 16)
def pre_process_func(
requests: list[OmniDiffusionRequest],
request: OmniDiffusionRequest,
):
"""Pre-process requests for QwenImageEditPipeline."""
for req in requests:
image = req.pil_image
for i, prompt in enumerate(request.prompts):
multi_modal_data = prompt.get("multi_modal_data", {}) if not isinstance(prompt, str) else None
raw_image = multi_modal_data.get("image", None) if multi_modal_data is not None else None
if isinstance(prompt, str):
prompt = OmniTextPrompt(prompt=prompt)
if "additional_information" not in prompt:
prompt["additional_information"] = {}
image_size = image[0].size if isinstance(image, list) else image.size
# Only handles single image
if raw_image is None or isinstance(raw_image, list):
raise ValueError(
"""Received no image or a list of image. Only a single image is supported by this model."""
"""Please correctly set `"multi_modal_data": {"image": <an image object or file path>, …}`"""
)
if isinstance(raw_image, str):
image = PIL.Image.open(raw_image)
else:
image = cast(PIL.Image.Image | torch.Tensor | np.ndarray, raw_image)
image_size = image.size
calculated_width, calculated_height = calculate_dimensions(1024 * 1024, image_size[0] / image_size[1])
height = req.height or calculated_height
width = req.width or calculated_width
height = request.sampling_params.height or calculated_height
width = request.sampling_params.width or calculated_width
# Ensure dimensions are multiples of vae_scale_factor * 2
multiple_of = vae_scale_factor * 2
@@ -81,10 +99,10 @@ def get_qwen_image_edit_pre_process_func(
width = width // multiple_of * multiple_of
# Store calculated dimensions in request
req.calculated_height = calculated_height
req.calculated_width = calculated_width
req.height = height
req.width = width
prompt["additional_information"]["calculated_height"] = calculated_height
prompt["additional_information"]["calculated_width"] = calculated_width
request.sampling_params.height = height
request.sampling_params.width = width
# Preprocess image
if image is not None and not (
@@ -96,10 +114,10 @@ def get_qwen_image_edit_pre_process_func(
image = image.unsqueeze(2)
# Store preprocessed image and prompt image in request
req.preprocessed_image = image
req.prompt_image = prompt_image
return requests
prompt["additional_information"]["preprocessed_image"] = image
prompt["additional_information"]["prompt_image"] = prompt_image
request.prompts[i] = prompt
return request
return pre_process_func
@@ -717,17 +735,27 @@ class QwenImageEditPipeline(
max_sequence_length: int = 512,
) -> DiffusionOutput:
"""Forward pass for image editing."""
prompt = req.prompt
negative_prompt = req.negative_prompt
# TODO: In online mode, sometimes it receives [{"negative_prompt": None}, {...}], so cannot use .get("...", "")
# TODO: May be some data formatting operations on the API side. Hack for now.
if len(req.prompts) > 1:
logger.warning(
"""This model only supports a single prompt, not a batched request.""",
"""Taking only the first image for now.""",
)
first_prompt = req.prompts[0]
prompt = first_prompt if isinstance(first_prompt, str) else (first_prompt.get("prompt") or "")
negative_prompt = None if isinstance(first_prompt, str) else first_prompt.get("negative_prompt")
# Get preprocessed image from request (pre-processing is done in DiffusionEngine)
if hasattr(req, "preprocessed_image"):
prompt_image = req.prompt_image
image = req.preprocessed_image
calculated_height = req.calculated_height
calculated_width = req.calculated_width
height = req.height
width = req.width
if not isinstance(first_prompt, str) and "preprocessed_image" in (
additional_information := first_prompt.get("additional_information", {})
):
prompt_image = additional_information.get("prompt_image")
image = additional_information.get("preprocessed_image")
calculated_height = additional_information.get("calculated_height")
calculated_width = additional_information.get("calculated_width")
height = req.sampling_params.height
width = req.sampling_params.width
else:
# fallback to run pre-processing in pipeline (debug only)
image_size = image[0].size if isinstance(image, list) else image.size
@@ -745,14 +773,18 @@ class QwenImageEditPipeline(
image = self.image_processor.preprocess(image, calculated_height, calculated_width)
image = image.unsqueeze(2)
num_inference_steps = req.num_inference_steps or num_inference_steps
generator = req.generator or generator
true_cfg_scale = req.true_cfg_scale or true_cfg_scale
if req.guidance_scale_provided:
guidance_scale = req.guidance_scale
req_num_outputs = getattr(req, "num_outputs_per_prompt", None)
if req_num_outputs and req_num_outputs > 0:
num_images_per_prompt = req_num_outputs
num_inference_steps = req.sampling_params.num_inference_steps or num_inference_steps
sigmas = req.sampling_params.sigmas or sigmas
max_sequence_length = req.sampling_params.max_sequence_length or max_sequence_length
generator = req.sampling_params.generator or generator
true_cfg_scale = req.sampling_params.true_cfg_scale or true_cfg_scale
if req.sampling_params.guidance_scale_provided:
guidance_scale = req.sampling_params.guidance_scale
num_images_per_prompt = (
req.sampling_params.num_outputs_per_prompt
if req.sampling_params.num_outputs_per_prompt > 0
else num_images_per_prompt
)
# 1. check inputs
# 2. encode prompts
@@ -5,7 +5,7 @@ import json
import logging
import os
from collections.abc import Iterable
from typing import Any
from typing import Any, cast
import numpy as np
import PIL.Image
@@ -42,6 +42,7 @@ from vllm_omni.diffusion.models.qwen_image.qwen_image_transformer import (
)
from vllm_omni.diffusion.request import OmniDiffusionRequest
from vllm_omni.diffusion.utils.tf_utils import get_transformer_config_kwargs
from vllm_omni.inputs.data import OmniTextPrompt
from vllm_omni.model_executor.model_loader.weight_utils import (
download_weights_from_hf_specific,
)
@@ -70,24 +71,33 @@ def get_qwen_image_edit_plus_pre_process_func(
latent_channels = vae_config.get("z_dim", 16)
def pre_process_func(
requests: list[OmniDiffusionRequest],
request: OmniDiffusionRequest,
):
"""Pre-process requests for QwenImageEditPlusPipeline."""
for req in requests:
image = req.pil_image
for i, prompt in enumerate(request.prompts):
multi_modal_data = prompt.get("multi_modal_data", {}) if not isinstance(prompt, str) else None
raw_image = multi_modal_data.get("image", None) if multi_modal_data is not None else None
if isinstance(prompt, str):
prompt = OmniTextPrompt(prompt=prompt)
if "additional_information" not in prompt:
prompt["additional_information"] = {}
# Handle single image or list of images
if image is None:
if raw_image is None:
continue
if not isinstance(image, list):
image = [image]
if not isinstance(raw_image, list):
raw_image = [raw_image]
image = [
PIL.Image.open(im) if isinstance(im, str) else cast(PIL.Image.Image | np.ndarray | torch.Tensor, im)
for im in raw_image
]
# Calculate dimensions based on first image
image_size = image[0].size
calculated_width, calculated_height = calculate_dimensions(VAE_IMAGE_SIZE, image_size[0] / image_size[1])
height = req.height or calculated_height
width = req.width or calculated_width
height = request.sampling_params.height or calculated_height
width = request.sampling_params.width or calculated_width
# Ensure dimensions are multiples of vae_scale_factor * 2
multiple_of = vae_scale_factor * 2
@@ -95,10 +105,10 @@ def get_qwen_image_edit_plus_pre_process_func(
width = width // multiple_of * multiple_of
# Store calculated dimensions in request
req.calculated_height = calculated_height
req.calculated_width = calculated_width
req.height = height
req.width = width
prompt["additional_information"]["calculated_height"] = calculated_height
prompt["additional_information"]["calculated_width"] = calculated_width
request.sampling_params.height = height
request.sampling_params.width = width
# Preprocess images into condition_images (for prompt encoding) and vae_images (for VAE encoding)
condition_images = []
@@ -124,12 +134,12 @@ def get_qwen_image_edit_plus_pre_process_func(
vae_images.append(image_processor.preprocess(img, vae_height, vae_width).unsqueeze(2))
# Store preprocessed images in request
req.condition_images = condition_images
req.vae_images = vae_images
req.condition_image_sizes = condition_image_sizes
req.vae_image_sizes = vae_image_sizes
return requests
prompt["additional_information"]["condition_images"] = condition_images
prompt["additional_information"]["vae_images"] = vae_images
prompt["additional_information"]["condition_image_sizes"] = condition_image_sizes
prompt["additional_information"]["vae_image_sizes"] = vae_image_sizes
request.prompts[i] = prompt
return request
return pre_process_func
@@ -281,7 +291,7 @@ class QwenImageEditPlusPipeline(nn.Module, SupportImageInput):
def _get_qwen_prompt_embeds(
self,
prompt: str | list[str] = None,
prompt: str | list[str],
image: list[torch.Tensor] | torch.Tensor | None = None,
dtype: torch.dtype | None = None,
):
@@ -655,19 +665,31 @@ class QwenImageEditPlusPipeline(nn.Module, SupportImageInput):
max_sequence_length: int = 512,
) -> DiffusionOutput:
"""Forward pass for image editing with support for multiple images."""
prompt = req.prompt
negative_prompt = req.negative_prompt
# TODO: In online mode, sometimes it receives [{"negative_prompt": None}, {...}], so cannot use .get("...", "")
# TODO: May be some data formatting operations on the API side. Hack for now.
if len(req.prompts) > 1:
logger.warning(
"""This model only supports a single prompt, not a batched request.""",
"""Taking only the first image for now.""",
)
first_prompt = req.prompts[0]
prompt = first_prompt if isinstance(first_prompt, str) else (first_prompt.get("prompt") or "")
negative_prompt = None if isinstance(first_prompt, str) else first_prompt.get("negative_prompt")
# Get preprocessed images from request (pre-processing is done in DiffusionEngine)
if hasattr(req, "vae_images") and hasattr(req, "condition_images"):
condition_images = req.condition_images
vae_images = req.vae_images
condition_image_sizes = req.condition_image_sizes
vae_image_sizes = req.vae_image_sizes
calculated_height = req.calculated_height
calculated_width = req.calculated_width
height = req.height
width = req.width
if (
not isinstance(first_prompt, str)
and "vae_images" in (additional_information := first_prompt.get("additional_information", {}))
and "condition_images" in additional_information
):
condition_images = additional_information.get("condition_images")
vae_images = additional_information.get("vae_images")
condition_image_sizes = additional_information.get("condition_image_sizes")
vae_image_sizes = additional_information.get("vae_image_sizes")
calculated_height = additional_information.get("calculated_height")
calculated_width = additional_information.get("calculated_width")
height = req.sampling_params.height
width = req.sampling_params.width
else:
# fallback to run pre-processing in pipeline (debug only)
if image is None:
@@ -701,14 +723,18 @@ class QwenImageEditPlusPipeline(nn.Module, SupportImageInput):
condition_images.append(self.image_processor.resize(img, condition_height, condition_width))
vae_images.append(self.image_processor.preprocess(img, vae_height, vae_width).unsqueeze(2))
num_inference_steps = req.num_inference_steps or num_inference_steps
generator = req.generator or generator
true_cfg_scale = req.true_cfg_scale or true_cfg_scale
if req.guidance_scale_provided:
guidance_scale = req.guidance_scale
req_num_outputs = getattr(req, "num_outputs_per_prompt", None)
if req_num_outputs and req_num_outputs > 0:
num_images_per_prompt = req_num_outputs
num_inference_steps = req.sampling_params.num_inference_steps or num_inference_steps
sigmas = req.sampling_params.sigmas or sigmas
max_sequence_length = req.sampling_params.max_sequence_length or max_sequence_length
generator = req.sampling_params.generator or generator
true_cfg_scale = req.sampling_params.true_cfg_scale or true_cfg_scale
if req.sampling_params.guidance_scale_provided:
guidance_scale = req.sampling_params.guidance_scale
num_images_per_prompt = (
req.sampling_params.num_outputs_per_prompt
if req.sampling_params.num_outputs_per_prompt > 0
else num_images_per_prompt
)
# 1. check inputs
# 2. encode prompts
@@ -7,7 +7,7 @@ import logging
import math
import os
from collections.abc import Iterable
from typing import Any
from typing import Any, cast
import numpy as np
import PIL.Image
@@ -38,6 +38,7 @@ from vllm_omni.diffusion.models.qwen_image.qwen_image_transformer import (
)
from vllm_omni.diffusion.request import OmniDiffusionRequest
from vllm_omni.diffusion.utils.tf_utils import get_transformer_config_kwargs
from vllm_omni.inputs.data import OmniTextPrompt
from vllm_omni.model_executor.model_loader.weight_utils import (
download_weights_from_hf_specific,
)
@@ -69,17 +70,35 @@ def get_qwen_image_layered_pre_process_func(
image_processor = VaeImageProcessor(vae_scale_factor=vae_scale_factor * 2)
def pre_process_func(
requests: list[OmniDiffusionRequest],
request: OmniDiffusionRequest,
):
"""Pre-process requests for QwenImageLayeredPipeline."""
for req in requests:
image = req.pil_image
for i, prompt in enumerate(request.prompts):
multi_modal_data = prompt.get("multi_modal_data", {}) if not isinstance(prompt, str) else None
raw_image = multi_modal_data.get("image", None) if multi_modal_data is not None else None
if isinstance(prompt, str):
prompt = OmniTextPrompt(prompt=prompt)
if "additional_information" not in prompt:
prompt["additional_information"] = {}
if raw_image is None or isinstance(raw_image, list):
raise ValueError(
"""Received no image or a list of image. Only a single image is supported by this model."""
"""Please correctly set `"multi_modal_data": {"image": <an image object or file path>, …}`"""
)
if isinstance(raw_image, str):
image = PIL.Image.open(raw_image)
else:
image = cast(PIL.Image.Image | torch.Tensor | np.ndarray, raw_image)
# 1. calculate dimensions
image_size = image[0].size if isinstance(image, list) else image.size
assert req.resolution in [640, 1024], f"resolution must be either 640 or 1024, but got {req.resolution}"
image_size = image.size
assert request.sampling_params.resolution in [640, 1024], (
f"resolution must be either 640 or 1024, but got {request.sampling_params.resolution}"
)
calculated_width, calculated_height = calculate_dimensions(
req.resolution * req.resolution, image_size[0] / image_size[1]
request.sampling_params.resolution * request.sampling_params.resolution, image_size[0] / image_size[1]
)
height = calculated_height
width = calculated_width
@@ -89,10 +108,10 @@ def get_qwen_image_layered_pre_process_func(
height = height // multiple_of * multiple_of
# Store calculated dimensions in request
req.calculated_height = calculated_height
req.calculated_width = calculated_width
req.height = height
req.width = width
prompt["additional_information"]["calculated_height"] = calculated_height
prompt["additional_information"]["calculated_width"] = calculated_width
request.sampling_params.height = height
request.sampling_params.width = width
# 2. Preprocess image
if image is not None and not (isinstance(image, torch.Tensor) and image.size(1) == latent_channels):
@@ -103,10 +122,10 @@ def get_qwen_image_layered_pre_process_func(
# image = image.to(dtype=self.text_encoder.dtype) # do it later
# Store preprocessed image and prompt image in request
req.preprocessed_image = image
req.prompt_image = prompt_image
return requests
prompt["additional_information"]["preprocessed_image"] = image
prompt["additional_information"]["prompt_image"] = prompt_image
request.prompts[i] = prompt
return request
return pre_process_func
@@ -689,7 +708,7 @@ the image\n<|vision_start|><|image_pad|><|vision_end|><|im_end|>\n<|im_start|>as
req: OmniDiffusionRequest,
image: PIL.Image.Image | torch.Tensor | None = None,
prompt: str | list[str] | None = None,
negative_prompt: str | list[str] = None,
negative_prompt: str | list[str] | None = None,
true_cfg_scale: float = 4.0,
layers: int | None = 4,
num_inference_steps: int = 50,
@@ -713,29 +732,48 @@ the image\n<|vision_start|><|image_pad|><|vision_end|><|im_end|>\n<|im_start|>as
# 1. Get preprocessed image from request (pre-processing is done in DiffusionEngine)
# Override parameters from request if provided
prompt = req.prompt if req.prompt is not None else prompt
layers = req.layers if req.layers is not None else layers
resolution = req.resolution if req.resolution is not None else resolution
cfg_normalize = req.cfg_normalize if req.cfg_normalize is not None else cfg_normalize
use_en_prompt = req.use_en_prompt if req.use_en_prompt is not None else use_en_prompt
negative_prompt = req.negative_prompt if req.negative_prompt is not None else negative_prompt
num_inference_steps = req.num_inference_steps or num_inference_steps
generator = req.generator or generator
true_cfg_scale = req.true_cfg_scale or true_cfg_scale
if req.guidance_scale_provided:
guidance_scale = req.guidance_scale
req_num_outputs = getattr(req, "num_outputs_per_prompt", None)
if req_num_outputs and req_num_outputs > 0:
num_images_per_prompt = req_num_outputs
# TODO: In online mode, sometimes it receives [{"negative_prompt": None}, {...}], so cannot use .get("...", "")
# TODO: May be some data formatting operations on the API side. Hack for now.
if len(req.prompts) > 1:
logger.warning(
"""This model only supports a single prompt, not a batched request.""",
"""Taking only the first image for now.""",
)
first_prompt = req.prompts[0]
prompt = first_prompt if isinstance(first_prompt, str) else (first_prompt.get("prompt") or "")
negative_prompt = None if isinstance(first_prompt, str) else first_prompt.get("negative_prompt")
if hasattr(req, "preprocessed_image"):
prompt_image = req.prompt_image
image = req.preprocessed_image
layers = req.sampling_params.layers if req.sampling_params.layers is not None else layers
resolution = req.sampling_params.resolution if req.sampling_params.resolution is not None else resolution
max_sequence_length = req.sampling_params.max_sequence_length or max_sequence_length
cfg_normalize = (
req.sampling_params.cfg_normalize if req.sampling_params.cfg_normalize is not None else cfg_normalize
)
use_en_prompt = (
req.sampling_params.use_en_prompt if req.sampling_params.use_en_prompt is not None else use_en_prompt
)
num_inference_steps = req.sampling_params.num_inference_steps or num_inference_steps
sigmas = req.sampling_params.sigmas or sigmas
generator = req.sampling_params.generator or generator
true_cfg_scale = req.sampling_params.true_cfg_scale or true_cfg_scale
if req.sampling_params.guidance_scale_provided:
guidance_scale = req.sampling_params.guidance_scale
num_images_per_prompt = (
req.sampling_params.num_outputs_per_prompt
if req.sampling_params.num_outputs_per_prompt > 0
else num_images_per_prompt
)
if not isinstance(first_prompt, str) and "preprocessed_image" in (
additional_information := first_prompt.get("additional_information", {})
):
prompt_image = additional_information.get("prompt_image")
image = additional_information.get("preprocessed_image")
image = image.to(dtype=self.text_encoder.dtype) # Now we get the type
calculated_height = req.calculated_height
calculated_width = req.calculated_width
height = req.height
width = req.width
calculated_height = additional_information.get("calculated_height")
calculated_width = additional_information.get("calculated_width")
height = req.sampling_params.height
width = req.sampling_params.width
else:
# fallback to run pre-processing in pipeline (debug only)
image_size = image[0].size if isinstance(image, list) else image.size