[Feature] HunyuanImage-3.0 IT2I (image editing) support (#3107)
Signed-off-by: TaffyOfficial <2324465096@qq.com> Signed-off-by: zuiho <2324465096@qq.com> Signed-off-by: skf1999 <13234016272@163.com> Co-authored-by: TaffyOfficial <2324465096@qq.com> Co-authored-by: dengyunyang <584797741@qq.com> Co-authored-by: John Liu BUAA <liukecheng97@gmail.com>
This commit is contained in:
8 files changed
+573
-28
No files matched your search
@@ -746,6 +746,9 @@ class LightProjector(nn.Module):
|
||||
|
||||
self.layers = modules
|
||||
|
||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
||||
return self.layers(x)
|
||||
|
||||
|
||||
class HunYuanRotary2DEmbedder:
|
||||
r"""
|
||||
|
||||
@@ -6,9 +6,13 @@ import os
|
||||
from collections.abc import Iterable
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler
|
||||
from diffusers.schedulers.scheduling_flow_match_euler_discrete import (
|
||||
FlowMatchEulerDiscreteScheduler,
|
||||
)
|
||||
from PIL import Image as PILImage
|
||||
from transformers.generation.configuration_utils import GenerationConfig
|
||||
from transformers.generation.utils import ALL_CACHE_NAMES, GenerationMixin
|
||||
from transformers.utils.generic import ModelOutput
|
||||
@@ -19,8 +23,12 @@ from vllm.transformers_utils.config import get_config
|
||||
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
|
||||
from vllm_omni.diffusion.distributed.utils import get_local_device
|
||||
from vllm_omni.diffusion.model_loader.diffusers_loader import DiffusersPipelineLoader
|
||||
from vllm_omni.diffusion.profiler.diffusion_pipeline_profiler import DiffusionPipelineProfilerMixin
|
||||
from vllm_omni.diffusion.models.interface import SupportImageInput
|
||||
from vllm_omni.diffusion.profiler.diffusion_pipeline_profiler import (
|
||||
DiffusionPipelineProfilerMixin,
|
||||
)
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
from vllm_omni.inputs.data import OmniTextPrompt
|
||||
from vllm_omni.model_executor.models.hunyuan_image3.siglip2 import Siglip2VisionTransformer
|
||||
|
||||
from .autoencoder import AutoencoderKLConv3D
|
||||
@@ -63,7 +71,239 @@ def to_device(data, device):
|
||||
return data
|
||||
|
||||
|
||||
class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, DiffusionPipelineProfilerMixin):
|
||||
def _to_pil_image(image: Any) -> PILImage.Image:
|
||||
if isinstance(image, PILImage.Image):
|
||||
return image
|
||||
if isinstance(image, str):
|
||||
return PILImage.open(image)
|
||||
if isinstance(image, np.ndarray):
|
||||
array = image
|
||||
if array.dtype != np.uint8:
|
||||
if np.issubdtype(array.dtype, np.floating):
|
||||
if float(np.min(array)) < 0.0:
|
||||
array = (np.clip(array, -1.0, 1.0) + 1.0) / 2.0
|
||||
if float(np.max(array)) <= 1.0:
|
||||
array = array * 255.0
|
||||
array = np.clip(array, 0, 255).astype(np.uint8)
|
||||
if array.ndim == 3 and array.shape[0] in (1, 3, 4):
|
||||
array = np.transpose(array, (1, 2, 0))
|
||||
return PILImage.fromarray(array)
|
||||
if isinstance(image, torch.Tensor):
|
||||
tensor = image.detach().cpu()
|
||||
if tensor.ndim == 4:
|
||||
if tensor.shape[0] != 1:
|
||||
raise ValueError(f"Only a single image tensor is supported, but got shape {tuple(tensor.shape)}.")
|
||||
tensor = tensor.squeeze(0)
|
||||
if tensor.ndim == 3 and tensor.shape[0] in (1, 3, 4):
|
||||
tensor = tensor.permute(1, 2, 0)
|
||||
if tensor.dtype.is_floating_point:
|
||||
if float(tensor.min()) < 0.0:
|
||||
tensor = (tensor.clamp(-1.0, 1.0) + 1.0) / 2.0
|
||||
if float(tensor.max()) > 1.0:
|
||||
tensor = tensor / 255.0
|
||||
tensor = (tensor.clamp(0.0, 1.0) * 255.0).to(torch.uint8)
|
||||
else:
|
||||
tensor = tensor.to(torch.uint8)
|
||||
return PILImage.fromarray(tensor.numpy())
|
||||
raise TypeError(f"Unsupported image input type: {type(image)}")
|
||||
|
||||
|
||||
def _resize_and_crop_center(image: PILImage.Image, target_width: int, target_height: int) -> PILImage.Image:
|
||||
# Mirrors HunyuanImage3Processor._resize_and_crop in
|
||||
# vllm_omni.model_executor.models.hunyuan_image3.hunyuan_image3 so the AR
|
||||
# and DiT stages preprocess condition images identically.
|
||||
tw, th = target_width, target_height
|
||||
w, h = image.size
|
||||
tr = th / tw
|
||||
r = h / w
|
||||
if r < tr:
|
||||
resize_height = th
|
||||
resize_width = int(round(th / h * w))
|
||||
else:
|
||||
resize_width = tw
|
||||
resize_height = int(round(tw / w * h))
|
||||
resized = image.resize((resize_width, resize_height), PILImage.Resampling.LANCZOS)
|
||||
crop_top = int(round((resize_height - th) / 2.0))
|
||||
crop_left = int(round((resize_width - tw) / 2.0))
|
||||
return resized.crop((crop_left, crop_top, crop_left + tw, crop_top + th))
|
||||
|
||||
|
||||
def _to_python_scalar(value: Any) -> Any:
|
||||
if isinstance(value, np.generic):
|
||||
return value.item()
|
||||
return value
|
||||
|
||||
|
||||
def _image_info_to_payload(image_info: ImageInfo) -> dict[str, Any]:
|
||||
return {
|
||||
"image_type": image_info.image_type,
|
||||
"image_tensor": image_info.image_tensor,
|
||||
"image_width": _to_python_scalar(image_info.image_width),
|
||||
"image_height": _to_python_scalar(image_info.image_height),
|
||||
"token_width": _to_python_scalar(image_info.token_width),
|
||||
"token_height": _to_python_scalar(image_info.token_height),
|
||||
"image_token_length": _to_python_scalar(image_info.image_token_length),
|
||||
"base_size": _to_python_scalar(image_info.base_size),
|
||||
"ratio_index": _to_python_scalar(image_info.ratio_index),
|
||||
"add_timestep_token": image_info.add_timestep_token,
|
||||
"add_guidance_token": image_info.add_guidance_token,
|
||||
"use_front_boi_token": image_info.use_front_boi_token,
|
||||
"add_image_shape_token": image_info.add_image_shape_token,
|
||||
}
|
||||
|
||||
|
||||
def _to_tensor_if_needed(value: Any) -> Any:
|
||||
if isinstance(value, np.generic):
|
||||
return value.item()
|
||||
if isinstance(value, list):
|
||||
return torch.tensor(value)
|
||||
return value
|
||||
|
||||
|
||||
def _image_info_from_payload(payload: dict[str, Any]) -> ImageInfo:
|
||||
return ImageInfo(
|
||||
image_type=payload.get("image_type"),
|
||||
image_tensor=_to_tensor_if_needed(payload.get("image_tensor")),
|
||||
image_width=payload.get("image_width"),
|
||||
image_height=payload.get("image_height"),
|
||||
token_width=payload.get("token_width"),
|
||||
token_height=payload.get("token_height"),
|
||||
image_token_length=payload.get("image_token_length"),
|
||||
base_size=payload.get("base_size"),
|
||||
ratio_index=payload.get("ratio_index"),
|
||||
add_timestep_token=payload.get("add_timestep_token", True),
|
||||
add_guidance_token=payload.get("add_guidance_token", False),
|
||||
use_front_boi_token=payload.get("use_front_boi_token", True),
|
||||
add_image_shape_token=payload.get("add_image_shape_token", True),
|
||||
)
|
||||
|
||||
|
||||
def _joint_image_info_to_payload(joint_image_info: JointImageInfo) -> dict[str, Any]:
|
||||
return {
|
||||
"type": "joint_image_info",
|
||||
"vae_image_info": _image_info_to_payload(joint_image_info.vae_image_info),
|
||||
"vision_image_info": _image_info_to_payload(joint_image_info.vision_image_info),
|
||||
"vision_encoder_kwargs": joint_image_info.vision_encoder_kwargs,
|
||||
}
|
||||
|
||||
|
||||
def _joint_image_info_from_payload(payload: Any) -> JointImageInfo:
|
||||
if isinstance(payload, JointImageInfo):
|
||||
return payload
|
||||
if not isinstance(payload, dict):
|
||||
raise TypeError(f"Expected dict or JointImageInfo for conditional image payload, got {type(payload)}.")
|
||||
|
||||
vae_image_info = _image_info_from_payload(payload["vae_image_info"])
|
||||
vision_image_info = _image_info_from_payload(payload["vision_image_info"])
|
||||
vision_encoder_kwargs = payload.get("vision_encoder_kwargs") or {}
|
||||
if isinstance(vision_encoder_kwargs, dict):
|
||||
vision_encoder_kwargs = {key: _to_tensor_if_needed(value) for key, value in vision_encoder_kwargs.items()}
|
||||
return JointImageInfo(
|
||||
vae_image_info=vae_image_info,
|
||||
vision_image_info=vision_image_info,
|
||||
vision_encoder_kwargs=vision_encoder_kwargs,
|
||||
)
|
||||
|
||||
|
||||
def get_hunyuan_image_3_pre_process_func(od_config: OmniDiffusionConfig):
|
||||
hf_config = get_config(od_config.model, trust_remote_code=True)
|
||||
image_processor = HunyuanImage3ImageProcessor(hf_config)
|
||||
vae_h_factor = hf_config.vae_downsample_factor[0] * hf_config.patch_size
|
||||
vae_w_factor = hf_config.vae_downsample_factor[1] * hf_config.patch_size
|
||||
vit_patch_size = getattr(image_processor.vision_encoder_processor, "patch_size", 1)
|
||||
if isinstance(vit_patch_size, tuple | list):
|
||||
vit_patch_size = int(vit_patch_size[0])
|
||||
|
||||
def _build_cond_joint_image(raw_image: Any) -> dict[str, Any]:
|
||||
pil_image = _to_pil_image(raw_image).convert("RGB")
|
||||
orig_width, orig_height = pil_image.size
|
||||
|
||||
target_width, target_height = image_processor.reso_group.get_target_size(orig_width, orig_height)
|
||||
target_width = int(target_width)
|
||||
target_height = int(target_height)
|
||||
vae_input = _resize_and_crop_center(pil_image, target_width, target_height)
|
||||
vae_tensor = image_processor.vae_processor(vae_input)
|
||||
base_size, ratio_idx = image_processor.reso_group.get_base_size_and_ratio_index(orig_width, orig_height)
|
||||
base_size = int(base_size)
|
||||
ratio_idx = int(ratio_idx)
|
||||
|
||||
vae_info = ImageInfo(
|
||||
image_type="vae",
|
||||
image_tensor=vae_tensor,
|
||||
image_width=target_width,
|
||||
image_height=target_height,
|
||||
token_width=target_width // vae_w_factor,
|
||||
token_height=target_height // vae_h_factor,
|
||||
base_size=base_size,
|
||||
ratio_index=ratio_idx,
|
||||
)
|
||||
|
||||
vit_inputs = image_processor.vision_encoder_processor(pil_image, return_tensors="pt")
|
||||
vit_tensor = vit_inputs["pixel_values"]
|
||||
spatial_shapes = vit_inputs["spatial_shapes"].squeeze(0)
|
||||
pixel_attention_mask = vit_inputs["pixel_attention_mask"].squeeze(0)
|
||||
vit_token_h = int(spatial_shapes[0].item())
|
||||
vit_token_w = int(spatial_shapes[1].item())
|
||||
|
||||
vit_info = ImageInfo(
|
||||
image_type="siglip2",
|
||||
image_tensor=vit_tensor,
|
||||
image_width=vit_token_w * vit_patch_size,
|
||||
image_height=vit_token_h * vit_patch_size,
|
||||
token_width=vit_token_w,
|
||||
token_height=vit_token_h,
|
||||
image_token_length=int(vit_tensor.shape[1]),
|
||||
)
|
||||
|
||||
return _joint_image_info_to_payload(
|
||||
JointImageInfo(
|
||||
vae_image_info=vae_info,
|
||||
vision_image_info=vit_info,
|
||||
vision_encoder_kwargs={
|
||||
"spatial_shapes": spatial_shapes,
|
||||
"pixel_attention_mask": pixel_attention_mask,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
def pre_process_func(request: OmniDiffusionRequest):
|
||||
for i, prompt in enumerate(request.prompts):
|
||||
if isinstance(prompt, str):
|
||||
prompt = OmniTextPrompt(prompt=prompt)
|
||||
|
||||
if "additional_information" not in prompt:
|
||||
prompt["additional_information"] = {}
|
||||
|
||||
multi_modal_data = prompt.get("multi_modal_data") or {}
|
||||
raw_images = multi_modal_data.get("image")
|
||||
if raw_images is None:
|
||||
raw_images = prompt.get("pil_image")
|
||||
has_images = raw_images is not None and (not isinstance(raw_images, list) or len(raw_images) > 0)
|
||||
if has_images:
|
||||
image_list = raw_images if isinstance(raw_images, list) else [raw_images]
|
||||
cond_image_infos = [_build_cond_joint_image(image) for image in image_list]
|
||||
prompt["additional_information"]["batch_cond_image_info"] = cond_image_infos
|
||||
|
||||
first_image_w, first_image_h = _to_pil_image(image_list[0]).size
|
||||
if request.sampling_params.width is None:
|
||||
request.sampling_params.width = int(first_image_w)
|
||||
if request.sampling_params.height is None:
|
||||
request.sampling_params.height = int(first_image_h)
|
||||
|
||||
request.prompts[i] = prompt
|
||||
|
||||
return request
|
||||
|
||||
return pre_process_func
|
||||
|
||||
|
||||
class HunyuanImage3Pipeline(
|
||||
HunyuanImage3PreTrainedModel,
|
||||
GenerationMixin,
|
||||
SupportImageInput,
|
||||
DiffusionPipelineProfilerMixin,
|
||||
):
|
||||
support_image_input = True
|
||||
hf_to_vllm_mapper = WeightsMapper(
|
||||
orig_to_new_prefix={
|
||||
"model.": "",
|
||||
@@ -108,6 +348,7 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
self.model = HunyuanImage3Model(self.hf_config, quant_config=quant_config)
|
||||
self.transformer = self.model
|
||||
self.vae = AutoencoderKLConv3D.from_config(self.hf_config.vae)
|
||||
self.vae.use_spatial_tiling = self.od_config.vae_use_tiling
|
||||
self._pipeline = None
|
||||
self._tkwrapper = TokenizerWrapper(od_config.model)
|
||||
self.image_processor = HunyuanImage3ImageProcessor(self.hf_config)
|
||||
@@ -380,6 +621,13 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
def vae_encode(self, image, cfg_factor=1):
|
||||
config = self.vae.config
|
||||
|
||||
if image.ndim == 3:
|
||||
image = image.unsqueeze(0)
|
||||
if image.ndim == 4:
|
||||
image = image.unsqueeze(2)
|
||||
if image.ndim != 5:
|
||||
raise ValueError(f"Expected image tensor with 3/4/5 dims, got shape {tuple(image.shape)}.")
|
||||
|
||||
with torch.autocast(device_type=self.model.device.type, dtype=torch.float16, enabled=True):
|
||||
vae_encode_result = self.vae.encode(image)
|
||||
if isinstance(vae_encode_result, torch.Tensor):
|
||||
@@ -499,8 +747,7 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
batch_cot_text = cot_text
|
||||
batch_system_prompt = system_prompt
|
||||
batch_gen_image_info = None
|
||||
# TODO: construct with user input images
|
||||
batch_cond_image_info = None
|
||||
batch_cond_image_info = kwargs.pop("batch_cond_image_info", None)
|
||||
|
||||
# -- 2.1 message_list
|
||||
if batch_message_list is not None:
|
||||
@@ -545,6 +792,12 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
if mode == "gen_image":
|
||||
batch_gen_image_info = [self.image_processor.build_image_info(image_size) for _ in range(batch_size)]
|
||||
|
||||
if batch_cond_image_info is not None:
|
||||
assert isinstance(batch_cond_image_info, list) and len(batch_cond_image_info) == batch_size, (
|
||||
"`batch_cond_image_info` should be a list with the same batch size as `prompt`."
|
||||
)
|
||||
batch_cond_image_info = [cond if isinstance(cond, list) else [cond] for cond in batch_cond_image_info]
|
||||
|
||||
# -- 2.3 seed
|
||||
generator = kwargs.get("generator", None)
|
||||
if generator is None:
|
||||
@@ -556,6 +809,11 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
bot_task = kwargs.pop("bot_task", "auto")
|
||||
# If `drop_think` enabled, always drop <think> parts in the context.
|
||||
drop_think = kwargs.get("drop_think", self.generation_config.drop_think)
|
||||
# Pull sequence_template from the model's generation_config so the DiT
|
||||
# text prefix matches how the model was trained (Instruct for the
|
||||
# HunyuanImage-3.0-Instruct checkpoint). Falling back to "pretrain"
|
||||
# only if the config does not specify it.
|
||||
sequence_template = getattr(self.generation_config, "sequence_template", "pretrain")
|
||||
# Apply batched prompt or batched message_list to build input sequence with associated info.
|
||||
out = self._tkwrapper.apply_chat_template(
|
||||
batch_prompt=batch_prompt,
|
||||
@@ -568,7 +826,7 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
max_length=kwargs.get("max_length"),
|
||||
bot_task=bot_task,
|
||||
image_base_size=self.config.image_base_size,
|
||||
sequence_template="pretrain",
|
||||
sequence_template=sequence_template,
|
||||
cfg_factor=cfg_factor[mode],
|
||||
drop_think=drop_think,
|
||||
)
|
||||
@@ -579,18 +837,21 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
cond_vae_images, cond_timestep, cond_vit_images = self._encode_cond_image(
|
||||
batch_cond_image_info, cfg_factor[mode]
|
||||
)
|
||||
# Pack vit kwargs. Siglip2-so requires spatial_shapes and attention_mask for inference.
|
||||
vit_kwargs = {"spatial_shapes": [], "attention_mask": []}
|
||||
# Pack vit kwargs. Siglip2 requires spatial_shapes and pixel_attention_mask
|
||||
# at the forward boundary. transformers >=5.54 renamed the kwarg from
|
||||
# `attention_mask` to `pixel_attention_mask` so the dict key must match
|
||||
# the expected forward signature.
|
||||
vit_kwargs = {"spatial_shapes": [], "pixel_attention_mask": []}
|
||||
for cond_image_info in batch_cond_image_info:
|
||||
vit_kwargs["spatial_shapes"].append(
|
||||
torch.stack([item.vision_encoder_kwargs["spatial_shapes"] for item in cond_image_info])
|
||||
)
|
||||
vit_kwargs["attention_mask"].append(
|
||||
vit_kwargs["pixel_attention_mask"].append(
|
||||
torch.stack([item.vision_encoder_kwargs["pixel_attention_mask"] for item in cond_image_info])
|
||||
)
|
||||
if cfg_factor[mode] > 1:
|
||||
vit_kwargs["spatial_shapes"] = vit_kwargs["spatial_shapes"] * cfg_factor[mode]
|
||||
vit_kwargs["attention_mask"] = vit_kwargs["attention_mask"] * cfg_factor[mode]
|
||||
vit_kwargs["pixel_attention_mask"] = vit_kwargs["pixel_attention_mask"] * cfg_factor[mode]
|
||||
else:
|
||||
cond_vae_images, cond_timestep, cond_vit_images = None, None, None
|
||||
vit_kwargs = None
|
||||
@@ -627,8 +888,16 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
stop_token_id = dict(
|
||||
auto=[tkw.eos_token_id] + extra_auto_stops,
|
||||
image=[tkw.eos_token_id],
|
||||
recaption=[tkw.end_recaption_token_id, tkw.end_answer_token_id, tkw.eos_token_id],
|
||||
think=[tkw.end_recaption_token_id, tkw.end_answer_token_id, tkw.eos_token_id],
|
||||
recaption=[
|
||||
tkw.end_recaption_token_id,
|
||||
tkw.end_answer_token_id,
|
||||
tkw.eos_token_id,
|
||||
],
|
||||
think=[
|
||||
tkw.end_recaption_token_id,
|
||||
tkw.end_answer_token_id,
|
||||
tkw.eos_token_id,
|
||||
],
|
||||
img_ratio=extra_auto_stops,
|
||||
)
|
||||
model_input_kwargs = dict(
|
||||
@@ -818,7 +1087,10 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
# 50 and 5.0 hard code
|
||||
results = self.pipeline(
|
||||
batch_size=len(batch_gen_image_info),
|
||||
image_size=[batch_gen_image_info[0].image_height, batch_gen_image_info[0].image_width],
|
||||
image_size=[
|
||||
batch_gen_image_info[0].image_height,
|
||||
batch_gen_image_info[0].image_width,
|
||||
],
|
||||
num_inference_steps=kwargs.get("num_inference_steps", 50),
|
||||
guidance_scale=kwargs.get("guidance_scale", 5.0),
|
||||
generator=generator,
|
||||
@@ -1006,10 +1278,51 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
extra_args = getattr(getattr(req, "sampling_params", None), "extra_args", {}) or {}
|
||||
use_system_prompt = extra_args.get("use_system_prompt")
|
||||
system_prompt = extra_args.get("system_prompt")
|
||||
# Fall back to per-prompt use_system_prompt forwarded by ar2diffusion
|
||||
if use_system_prompt is None and req.prompts:
|
||||
first_prompt = req.prompts[0]
|
||||
if isinstance(first_prompt, dict):
|
||||
use_system_prompt = first_prompt.get("use_system_prompt")
|
||||
if use_system_prompt is not None:
|
||||
system_prompt = get_system_prompt(use_system_prompt, "image", system_prompt)
|
||||
system_prompt = system_prompt.strip() if system_prompt is not None else ""
|
||||
prompt = [p if isinstance(p, str) else (p.get("prompt") or "") for p in req.prompts] or prompt
|
||||
|
||||
# Extract AR-generated CoT/recaption text from each prompt's extra dict.
|
||||
# The AR-side stage input processor (``ar2diffusion``) already prepends
|
||||
# the trigger tag (e.g. ``<think>``) when the AR used the KV-reuse
|
||||
# pretrain format, so ``ar_generated_text`` is a self-contained string
|
||||
# and ``get_cot_sections()`` can parse the think/recaption structure
|
||||
# directly.
|
||||
cot_text_list = []
|
||||
for p in req.prompts:
|
||||
extra = p.get("extra", {}) if isinstance(p, dict) else {}
|
||||
cot_text_list.append(extra.get("ar_generated_text") or None)
|
||||
cot_text = cot_text_list if any(t is not None for t in cot_text_list) else None
|
||||
|
||||
batch_cond_image_info: list[list[JointImageInfo]] | None = None
|
||||
if any(not isinstance(p, str) for p in req.prompts):
|
||||
batch_cond_image_info = []
|
||||
for prompt_item in req.prompts:
|
||||
if isinstance(prompt_item, str):
|
||||
batch_cond_image_info.append([])
|
||||
continue
|
||||
prompt_additional_information = prompt_item.get("additional_information") or {}
|
||||
prompt_cond_infos = prompt_additional_information.get("batch_cond_image_info", [])
|
||||
if isinstance(prompt_cond_infos, JointImageInfo | dict):
|
||||
prompt_cond_infos = [prompt_cond_infos]
|
||||
if prompt_cond_infos is None:
|
||||
prompt_cond_infos = []
|
||||
batch_cond_image_info.append([_joint_image_info_from_payload(item) for item in prompt_cond_infos])
|
||||
|
||||
has_cond_image = [len(cond_infos) > 0 for cond_infos in batch_cond_image_info]
|
||||
if any(has_cond_image) and not all(has_cond_image):
|
||||
raise ValueError(
|
||||
"When batching Hunyuan image editing requests, every prompt must include input image(s)."
|
||||
)
|
||||
if not any(has_cond_image):
|
||||
batch_cond_image_info = None
|
||||
|
||||
generator = req.sampling_params.generator or generator
|
||||
height = req.sampling_params.height or height
|
||||
width = req.sampling_params.width or width
|
||||
@@ -1019,17 +1332,20 @@ class HunyuanImage3Pipeline(HunyuanImage3PreTrainedModel, GenerationMixin, Diffu
|
||||
if guidance_scale <= 1.0:
|
||||
logger.info("HunyuanImage3.0 runs without classifier-free guidance when guidance_scale <= 1.0.")
|
||||
image_size = (height, width)
|
||||
|
||||
model_inputs = self.prepare_model_inputs(
|
||||
prompt=prompt,
|
||||
cot_text=None,
|
||||
cot_text=cot_text,
|
||||
system_prompt=system_prompt,
|
||||
mode="gen_image",
|
||||
generator=generator,
|
||||
image_size=image_size,
|
||||
num_inference_steps=num_inference_steps,
|
||||
guidance_scale=guidance_scale,
|
||||
batch_cond_image_info=batch_cond_image_info,
|
||||
)
|
||||
outputs = self._generate(**model_inputs, **kwargs)
|
||||
return DiffusionOutput(
|
||||
output=outputs[0], stage_durations=self.stage_durations if hasattr(self, "stage_durations") else None
|
||||
output=outputs[0],
|
||||
stage_durations=self.stage_durations if hasattr(self, "stage_durations") else None,
|
||||
)
|
||||
Reference in new issue
Block a user