[Bugfix]Fix HunyuanImage3 conditional image and prompt kwargs alignment (#4145)
Signed-off-by: reandeng <reandeng@tencent.com> Signed-off-by: Yaegaki1Erika <70182590+Yaegaki1Erika@users.noreply.github.com> Co-authored-by: reandeng <reandeng@tencent.com>
This commit is contained in:
1 parent
1e43656ab1
commit
84569280b1
5 files changed
+56
-17
No files matched your search
Regular → Executable
+16
-6
@@ -1043,6 +1043,9 @@ class HunyuanImage3MultiModalProcessor(BaseMultiModalProcessor[HunyuanImage3Proc
|
||||
images = mm_data.get("images", [])
|
||||
logger.debug(f"process image count: {len(images)}")
|
||||
batch_feature = image_processor(prompt, images, **tok_kwargs)
|
||||
vae_generator_seed = mm_kwargs.get("vae_generator_seed")
|
||||
if vae_generator_seed is not None and images:
|
||||
batch_feature["vae_generator_seed"] = torch.full((len(images),), int(vae_generator_seed), dtype=torch.long)
|
||||
return batch_feature
|
||||
|
||||
def _hf_processor_applies_updates(
|
||||
@@ -1080,6 +1083,8 @@ class HunyuanImage3MultiModalProcessor(BaseMultiModalProcessor[HunyuanImage3Proc
|
||||
config["base_size"] = MultiModalFieldConfig.batched("image")
|
||||
if "ratio_index" in hf_inputs:
|
||||
config["ratio_index"] = MultiModalFieldConfig.batched("image")
|
||||
if "vae_generator_seed" in hf_inputs:
|
||||
config["vae_generator_seed"] = MultiModalFieldConfig.batched("image")
|
||||
return config
|
||||
|
||||
def _get_prompt_updates(
|
||||
@@ -1732,6 +1737,7 @@ class HunyuanImage3ForConditionalGeneration(nn.Module, SupportsMultiModal, Suppo
|
||||
# we reconstruct per-image shapes from vae_token_grid_hw below.
|
||||
kwargs.pop("vae_pixel_size", None)
|
||||
vae_token_grid_hw = kwargs.pop("vae_token_grid_hw", None)
|
||||
vae_generator_seed = kwargs.pop("vae_generator_seed", None)
|
||||
|
||||
if vit_pixel_values is None or vae_pixel_values is None:
|
||||
return None
|
||||
@@ -1771,6 +1777,7 @@ class HunyuanImage3ForConditionalGeneration(nn.Module, SupportsMultiModal, Suppo
|
||||
"vit_spatial_shapes": vit_spatial_shapes,
|
||||
"vae_pixel_values": vae_image_list,
|
||||
"vae_token_grid_hw": vae_token_grid_hw,
|
||||
"vae_generator_seed": vae_generator_seed,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -1778,6 +1785,7 @@ class HunyuanImage3ForConditionalGeneration(nn.Module, SupportsMultiModal, Suppo
|
||||
self,
|
||||
images: torch.Tensor,
|
||||
cfg_factor: int = 1,
|
||||
generator: torch.Generator | None = None,
|
||||
) -> tuple[tuple[torch.Tensor, torch.Tensor], ...]:
|
||||
"""
|
||||
Encode images through VAE encoder.
|
||||
@@ -1797,11 +1805,7 @@ class HunyuanImage3ForConditionalGeneration(nn.Module, SupportsMultiModal, Suppo
|
||||
images = images.to(dtype=self.vae.dtype)
|
||||
|
||||
vae_encode_result = self.vae.encode(images)
|
||||
# Match HunyuanImage-3's cond encode path: sample the posterior, but
|
||||
# use a fixed generator so online requests do not consume the global
|
||||
# RNG and drift across a long-running server.
|
||||
_cond_vae_gen = torch.Generator(device=images.device).manual_seed(0)
|
||||
latents = vae_encode_result.latent_dist.sample(_cond_vae_gen)
|
||||
latents = vae_encode_result.latent_dist.sample(generator)
|
||||
|
||||
# Apply shift and scaling factors if present
|
||||
if hasattr(config, "shift_factor") and config.shift_factor:
|
||||
@@ -1933,6 +1937,12 @@ class HunyuanImage3ForConditionalGeneration(nn.Module, SupportsMultiModal, Suppo
|
||||
vit_pixel_attention_mask = pixel_values["vit_pixel_attention_mask"]
|
||||
vit_spatial_shapes = pixel_values["vit_spatial_shapes"]
|
||||
vae_pixel_values = pixel_values["vae_pixel_values"]
|
||||
vae_generator_seed = pixel_values.get("vae_generator_seed")
|
||||
vae_generator = None
|
||||
if vae_generator_seed is not None and vae_generator_seed.numel() > 0:
|
||||
vae_generator = torch.Generator(device=vae_pixel_values[0].device).manual_seed(
|
||||
int(vae_generator_seed.reshape(-1)[0].item())
|
||||
)
|
||||
|
||||
# Perform ViT encoding
|
||||
vit_embeddings = self._vit_encode(vit_pixel_values, vit_pixel_attention_mask, vit_spatial_shapes)
|
||||
@@ -1941,7 +1951,7 @@ class HunyuanImage3ForConditionalGeneration(nn.Module, SupportsMultiModal, Suppo
|
||||
# `reso_group` bucket so shapes are ragged across the image-batch dim.
|
||||
vae_token_embeddings = []
|
||||
for vae_image_i in vae_pixel_values:
|
||||
t_i, latents_i = self._vae_encode(vae_image_i.unsqueeze(0), vae_cfg_factor)
|
||||
t_i, latents_i = self._vae_encode(vae_image_i.unsqueeze(0), vae_cfg_factor, generator=vae_generator)
|
||||
t_emb = self.time_embed(t_i[0])
|
||||
vae_tokens, _, _ = self.patch_embed(latents_i, t_emb)
|
||||
vae_token_embeddings.append(vae_tokens)
|
||||
|
||||
Reference in new issue
Block a user