[Feature] HunyuanImage-3.0 IT2I: multi-image input + prompt API cleanup (#3444)
Signed-off-by: TaffyOfficial <2324465096@qq.com> Signed-off-by: TaffyOfficial <wu15922848573@outlook.com> Signed-off-by: skf1999 <13234016272@163.com> Signed-off-by: zuiho <2324465096@qq.com> Signed-off-by: Claude Code <noreply@anthropic.com> Signed-off-by: zuiho <wu15922848573@outlook.com> Signed-off-by: TaffyOfficial <2587297563@qq.com> Co-authored-by: TaffyOfficial <2324465096@qq.com> Co-authored-by: TaffyOfficial <wu15922848573@outlook.com> Co-authored-by: skf1999 <13234016272@163.com>
This commit is contained in:
17 files changed
+1559
-534
No files matched your search
@@ -1700,7 +1700,10 @@ async def edit_images(
|
||||
# vllm-omni extension for layered models (e.g., Qwen-Image-Layered)
|
||||
layers: int | None = Form(None),
|
||||
resolution: int | None = Form(None), # See SUPPORTED_LAYERED_RESOLUTIONS
|
||||
# /v1/images/edits is always IT2I; only the prompting knobs are exposed.
|
||||
bot_task: str | None = Form(None),
|
||||
sys_type: str | None = Form(None),
|
||||
system_prompt: str | None = Form(None),
|
||||
) -> ImageGenerationResponse:
|
||||
"""
|
||||
OpenAI-compatible image edit endpoint.
|
||||
@@ -1751,16 +1754,20 @@ async def edit_images(
|
||||
status_code=HTTPStatus.BAD_REQUEST.value,
|
||||
detail=detail,
|
||||
)
|
||||
pil_images = await _load_input_images(input_images_list)
|
||||
# Match the offline path: RGB normalize when the caller opts into
|
||||
# Hunyuan-aware behavior. RGBA/P uploads otherwise diverge from offline.
|
||||
normalize_edit_images_rgb = bot_task is not None or sys_type is not None
|
||||
pil_images = await _load_input_images(input_images_list, normalize_rgb=normalize_edit_images_rgb)
|
||||
prompt["multi_modal_data"] = {}
|
||||
prompt["multi_modal_data"]["image"] = pil_images
|
||||
|
||||
if mask_image is not None:
|
||||
loaded = await _load_input_images([mask_image])
|
||||
# Mask role is different (alpha channel matters); never normalize.
|
||||
loaded = await _load_input_images([mask_image], normalize_rgb=False)
|
||||
prompt["multi_modal_data"]["mask_image"] = loaded[0]
|
||||
|
||||
if reference_image is not None:
|
||||
loaded = await _load_input_images([reference_image])
|
||||
loaded = await _load_input_images([reference_image], normalize_rgb=normalize_edit_images_rgb)
|
||||
prompt["multi_modal_data"]["reference_image"] = loaded[0]
|
||||
|
||||
# 3 Build sample params
|
||||
@@ -1811,7 +1818,8 @@ async def edit_images(
|
||||
|
||||
# 3.3 Parse and add size if provided
|
||||
width, height = None, None
|
||||
if size.lower() == "auto":
|
||||
size_was_auto = size.lower() == "auto"
|
||||
if size_was_auto:
|
||||
if resolution is None:
|
||||
# No resolution specified, use input image size
|
||||
width, height = pil_images[0].size
|
||||
@@ -1882,10 +1890,13 @@ async def edit_images(
|
||||
"seed": effective_seed,
|
||||
"num_outputs_per_prompt": n,
|
||||
}
|
||||
if width is not None:
|
||||
extra_body["width"] = width
|
||||
if height is not None:
|
||||
extra_body["height"] = height
|
||||
# size="auto" resolves width/height from input image; forwarding
|
||||
# those would override AR-driven `<img_ratio_*>` token selection.
|
||||
if not size_was_auto:
|
||||
if width is not None:
|
||||
extra_body["width"] = width
|
||||
if height is not None:
|
||||
extra_body["height"] = height
|
||||
if negative_prompt is not None:
|
||||
extra_body["negative_prompt"] = negative_prompt
|
||||
if num_inference_steps is not None:
|
||||
@@ -1907,6 +1918,10 @@ async def edit_images(
|
||||
extra_body["lora"] = lora_dict
|
||||
if bot_task is not None:
|
||||
extra_body["bot_task"] = bot_task
|
||||
if sys_type is not None:
|
||||
extra_body["sys_type"] = sys_type
|
||||
if system_prompt is not None:
|
||||
extra_body["system_prompt"] = system_prompt
|
||||
|
||||
prompt_text = prompt.get("prompt", "")
|
||||
generation_result = await chat_handler.generate_diffusion_images(
|
||||
@@ -2187,6 +2202,8 @@ def _extract_images_from_result(result: Any) -> list[Any]:
|
||||
|
||||
async def _load_input_images(
|
||||
inputs: list[str],
|
||||
*,
|
||||
normalize_rgb: bool = True,
|
||||
) -> list[Image.Image]:
|
||||
"""
|
||||
convert to PIL.Image.Image list
|
||||
@@ -2233,7 +2250,18 @@ async def _load_input_images(
|
||||
if not images:
|
||||
raise ValueError("No valid input images found")
|
||||
|
||||
return images
|
||||
if not normalize_rgb:
|
||||
return images
|
||||
|
||||
# Match the offline HunyuanImage3 image-edit example path, which eagerly
|
||||
# normalizes input files with ``Image.open(...).convert("RGB")`` before
|
||||
# they reach the AR stage. Keeping uploads as RGBA/P PIL objects makes
|
||||
# online IT2I observe a different visual input than offline (for example
|
||||
# transparent-logo uploads alpha-composited over white instead of black),
|
||||
# which is enough for HunyuanImage3 AR recaption to diverge before DiT
|
||||
# sees the request -- root cause of the "online 3 magnets vs offline 1
|
||||
# magnet" systematic semantic mismatch.
|
||||
return [img.convert("RGB") for img in images]
|
||||
|
||||
|
||||
def _choose_output_format(output_format: str | None, background: str | None) -> str:
|
||||
|
||||
@@ -419,7 +419,10 @@ class OmniOpenAIServingChat(OpenAIServingChat, AudioMixin):
|
||||
# consistency. After the multimodal processor consumes
|
||||
# the image data, the uuids remain as a stable reference.
|
||||
tprompt["multi_modal_uuids"] = {
|
||||
k: [f"{request_id}-{k}-{i}"] for i, k in enumerate(engine_prompt_image)
|
||||
k: [f"{request_id}-{k}-{i}" for i in range(len(v))]
|
||||
if isinstance(v, list)
|
||||
else [f"{request_id}-{k}-0"]
|
||||
for k, v in engine_prompt_image.items()
|
||||
}
|
||||
|
||||
engine_prompts = [tprompt]
|
||||
@@ -2245,6 +2248,8 @@ class OmniOpenAIServingChat(OpenAIServingChat, AudioMixin):
|
||||
layers = extra_body.get("layers")
|
||||
resolution = extra_body.get("resolution")
|
||||
bot_task = extra_body.get("bot_task")
|
||||
sys_type = extra_body.get("sys_type")
|
||||
custom_system_prompt = extra_body.get("system_prompt")
|
||||
|
||||
engine_prompt_data: dict[str, Any] | None = None
|
||||
modalities = ["image"]
|
||||
@@ -2255,31 +2260,48 @@ class OmniOpenAIServingChat(OpenAIServingChat, AudioMixin):
|
||||
else:
|
||||
engine_prompt_data = {"image": reference_images}
|
||||
|
||||
engine_prompt: OmniTextPrompt = {"prompt": prompt}
|
||||
if bot_task:
|
||||
prompt_token_ids: list[int] | None = None
|
||||
system_prompt_type: str | None = None
|
||||
if bot_task is not None or sys_type is not None or custom_system_prompt is not None:
|
||||
from vllm_omni.diffusion.models.hunyuan_image3.prompt_utils import (
|
||||
build_prompt,
|
||||
build_prompt_tokens,
|
||||
)
|
||||
|
||||
prompt_token_ids: list[int] | None = None
|
||||
system_prompt_type: str | None = None
|
||||
build_kwargs: dict[str, Any] = {
|
||||
"task": "it2i" if reference_images else "t2i",
|
||||
"sys_type": sys_type,
|
||||
"custom_system_prompt": custom_system_prompt,
|
||||
"num_images": len(reference_images) if reference_images else 1,
|
||||
}
|
||||
if bot_task is not None:
|
||||
build_kwargs["bot_task"] = bot_task
|
||||
elif "bot_task" in extra_body:
|
||||
# Explicit None from the caller is plain-mode; omitted lets
|
||||
# each task fall back to its default trigger.
|
||||
build_kwargs["bot_task"] = None
|
||||
if tokenizer is not None:
|
||||
result = build_prompt_tokens(prompt, tokenizer, task=bot_task)
|
||||
# Feed segment-tokenized prompt_token_ids so AR matches HF
|
||||
# apply_chat_template byte-for-byte (engine BPE would merge
|
||||
# across template boundaries, e.g. "。\n\n" -> single id).
|
||||
result = build_prompt_tokens(prompt, tokenizer, **build_kwargs)
|
||||
prompt_token_ids = result.token_ids
|
||||
system_prompt_type = result.system_prompt_type
|
||||
else:
|
||||
prompt = build_prompt(prompt, task=bot_task)
|
||||
engine_prompt["prompt"] = prompt
|
||||
|
||||
prompt = build_prompt(prompt, **build_kwargs)
|
||||
if reference_images and len(reference_images) == 1:
|
||||
engine_prompt_data = {"image": reference_images[0]}
|
||||
modalities = ["image"]
|
||||
if prompt_token_ids is not None:
|
||||
engine_prompt["prompt_token_ids"] = prompt_token_ids
|
||||
if system_prompt_type is not None:
|
||||
engine_prompt["use_system_prompt"] = system_prompt_type
|
||||
|
||||
engine_prompt: OmniTextPrompt = {"prompt": prompt}
|
||||
if prompt_token_ids is not None:
|
||||
engine_prompt["prompt_token_ids"] = prompt_token_ids
|
||||
if system_prompt_type is not None:
|
||||
engine_prompt["use_system_prompt"] = system_prompt_type
|
||||
# DiT's get_system_prompt(use_system_prompt, "image", system_prompt) reads
|
||||
# this; omitting it makes sys_type=custom yield an empty DiT prefix.
|
||||
if custom_system_prompt is not None:
|
||||
engine_prompt["system_prompt"] = custom_system_prompt
|
||||
engine_prompt["modalities"] = modalities
|
||||
if negative_prompt is not None:
|
||||
engine_prompt["negative_prompt"] = negative_prompt
|
||||
@@ -2295,7 +2317,11 @@ class OmniOpenAIServingChat(OpenAIServingChat, AudioMixin):
|
||||
engine_prompt["multi_modal_data"] = engine_prompt_data
|
||||
# Provide multi_modal_uuids so that newer vLLM versions can
|
||||
# validate multi_modal_data / multi_modal_uuids consistency.
|
||||
engine_prompt["multi_modal_uuids"] = {k: [f"img-{k}-{i}"] for i, k in enumerate(engine_prompt_data)}
|
||||
# Generate one uuid per image when the value is a list (multi-image inputs).
|
||||
engine_prompt["multi_modal_uuids"] = {
|
||||
k: [f"img-{k}-{i}" for i in range(len(v))] if isinstance(v, list) else [f"img-{k}-0"]
|
||||
for k, v in engine_prompt_data.items()
|
||||
}
|
||||
|
||||
comprehension_idx = None
|
||||
for idx, stage in enumerate(stage_configs):
|
||||
@@ -2320,10 +2346,8 @@ class OmniOpenAIServingChat(OpenAIServingChat, AudioMixin):
|
||||
):
|
||||
default_stage_params.seed = seed
|
||||
|
||||
# Inject target_h/w into comprehension (AR) stage sampling params
|
||||
# for models that need M-RoPE position pre-computation (e.g.
|
||||
# GLM-Image). max_tokens is handled via the deploy YAML default
|
||||
# (upper-bound ceiling) rather than computed dynamically here.
|
||||
# Inject target_h/w into AR stage for M-RoPE position pre-computation
|
||||
# (e.g. GLM-Image). max_tokens comes from deploy YAML.
|
||||
if comprehension_idx is not None and idx == comprehension_idx and height is not None and width is not None:
|
||||
extra_args = getattr(default_stage_params, "extra_args", None)
|
||||
if extra_args is None:
|
||||
@@ -2449,13 +2473,17 @@ class OmniOpenAIServingChat(OpenAIServingChat, AudioMixin):
|
||||
diffusion_engine = cast(AsyncOmni, engine)
|
||||
stage_configs = getattr(diffusion_engine, "stage_configs", None) or []
|
||||
if len(stage_configs) > 1:
|
||||
# Pull tokenizer from the comprehension (AR) stage so we can
|
||||
# build HF byte-for-byte prompt_token_ids in the helper. If
|
||||
# the engine doesn"t expose one, fall back to the legacy
|
||||
# string-prompt path (engine re-tokenizes).
|
||||
tokenizer = None
|
||||
get_tok = getattr(diffusion_engine, "get_tokenizer", None)
|
||||
if get_tok is not None:
|
||||
try:
|
||||
tokenizer = await get_tok()
|
||||
except Exception as exc:
|
||||
logger.warning("get_tokenizer failed: %s", exc)
|
||||
logger.warning("get_tokenizer failed; falling back to string prompt path: %s", exc)
|
||||
engine_prompt, sampling_params_list = self._build_multistage_generation_inputs(
|
||||
engine=diffusion_engine,
|
||||
prompt=prompt,
|
||||
|
||||
Reference in new issue
Block a user