Skip to content

Commit 5a523cd

Browse files
committed
add pipeline
1 parent 6bafea7 commit 5a523cd

3 files changed

Lines changed: 976 additions & 57 deletions

File tree

src/diffusers/models/transformers/transformer_hunyuan_video_framepack.py

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,22 @@ def __init__(self, inner_dim: int):
9898

9999
def forward(
100100
self,
101-
clean_latents: Optional[torch.Tensor] = None,
102-
clean_latents_2x: Optional[torch.Tensor] = None,
103-
clean_latents_4x: Optional[torch.Tensor] = None,
101+
latents_clean: Optional[torch.Tensor] = None,
102+
latents_clean_2x: Optional[torch.Tensor] = None,
103+
latents_clean_4x: Optional[torch.Tensor] = None,
104104
):
105-
if clean_latents is not None:
106-
clean_latents = self.proj(clean_latents)
107-
clean_latents = clean_latents.flatten(2).transpose(1, 2)
108-
if clean_latents_2x is not None:
109-
clean_latents_2x = _pad_for_3d_conv(clean_latents_2x, (2, 4, 4))
110-
clean_latents_2x = self.proj_2x(clean_latents_2x)
111-
clean_latents_2x = clean_latents_2x.flatten(2).transpose(1, 2)
112-
if clean_latents_4x is not None:
113-
clean_latents_4x = _pad_for_3d_conv(clean_latents_4x, (4, 8, 8))
114-
clean_latents_4x = self.proj_4x(clean_latents_4x)
115-
clean_latents_4x = clean_latents_4x.flatten(2).transpose(1, 2)
116-
return clean_latents, clean_latents_2x, clean_latents_4x
105+
if latents_clean is not None:
106+
latents_clean = self.proj(latents_clean)
107+
latents_clean = latents_clean.flatten(2).transpose(1, 2)
108+
if latents_clean_2x is not None:
109+
latents_clean_2x = _pad_for_3d_conv(latents_clean_2x, (2, 4, 4))
110+
latents_clean_2x = self.proj_2x(latents_clean_2x)
111+
latents_clean_2x = latents_clean_2x.flatten(2).transpose(1, 2)
112+
if latents_clean_4x is not None:
113+
latents_clean_4x = _pad_for_3d_conv(latents_clean_4x, (4, 8, 8))
114+
latents_clean_4x = self.proj_4x(latents_clean_4x)
115+
latents_clean_4x = latents_clean_4x.flatten(2).transpose(1, 2)
116+
return latents_clean, latents_clean_2x, latents_clean_4x
117117

118118

119119
class HunyuanVideoFramepackTransformer3DModel(
@@ -206,15 +206,15 @@ def forward(
206206
encoder_hidden_states: torch.Tensor,
207207
encoder_attention_mask: torch.Tensor,
208208
pooled_projections: torch.Tensor,
209-
image_embeddings: torch.Tensor,
209+
image_embeds: torch.Tensor,
210210
latent_indices: torch.Tensor,
211211
guidance: Optional[torch.Tensor] = None,
212-
clean_latents: Optional[torch.Tensor] = None,
213-
clean_latent_indices: Optional[torch.Tensor] = None,
214-
clean_latents_2x: Optional[torch.Tensor] = None,
215-
clean_latent_2x_indices: Optional[torch.Tensor] = None,
216-
clean_latents_4x: Optional[torch.Tensor] = None,
217-
clean_latent_4x_indices: Optional[torch.Tensor] = None,
212+
latents_clean: Optional[torch.Tensor] = None,
213+
indices_latents_clean: Optional[torch.Tensor] = None,
214+
latents_history_2x: Optional[torch.Tensor] = None,
215+
indices_latents_history_2x: Optional[torch.Tensor] = None,
216+
latents_history_4x: Optional[torch.Tensor] = None,
217+
indices_latents_history_4x: Optional[torch.Tensor] = None,
218218
attention_kwargs: Optional[Dict[str, Any]] = None,
219219
return_dict: bool = True,
220220
):
@@ -242,18 +242,18 @@ def forward(
242242
hidden_states, image_rotary_emb = self._pack_history_states(
243243
hidden_states,
244244
latent_indices,
245-
clean_latents,
246-
clean_latent_indices,
247-
clean_latents_2x,
248-
clean_latent_2x_indices,
249-
clean_latents_4x,
250-
clean_latent_4x_indices,
245+
latents_clean,
246+
indices_latents_clean,
247+
latents_history_2x,
248+
indices_latents_history_2x,
249+
latents_history_4x,
250+
indices_latents_history_4x,
251251
)
252252

253253
temb, token_replace_emb = self.time_text_embed(timestep, pooled_projections, guidance)
254254
encoder_hidden_states = self.context_embedder(encoder_hidden_states, timestep, encoder_attention_mask)
255255

256-
encoder_hidden_states_image = self.image_projection(image_embeddings)
256+
encoder_hidden_states_image = self.image_projection(image_embeds)
257257
attention_mask_image = encoder_attention_mask.new_ones((batch_size, encoder_hidden_states_image.shape[1]))
258258

259259
# must cat before (not after) encoder_hidden_states, due to attn masking
@@ -319,12 +319,12 @@ def _pack_history_states(
319319
self,
320320
hidden_states: torch.Tensor,
321321
latent_indices: torch.Tensor,
322-
clean_latents: Optional[torch.Tensor] = None,
323-
clean_latents_2x: Optional[torch.Tensor] = None,
324-
clean_latents_4x: Optional[torch.Tensor] = None,
325-
clean_latent_indices: Optional[torch.Tensor] = None,
326-
clean_latent_2x_indices: Optional[torch.Tensor] = None,
327-
clean_latent_4x_indices: Optional[torch.Tensor] = None,
322+
latents_clean: Optional[torch.Tensor] = None,
323+
latents_history_2x: Optional[torch.Tensor] = None,
324+
latents_history_4x: Optional[torch.Tensor] = None,
325+
indices_latents_clean: Optional[torch.Tensor] = None,
326+
indices_latents_history_2x: Optional[torch.Tensor] = None,
327+
indices_latents_history_4x: Optional[torch.Tensor] = None,
328328
):
329329
batch_size, num_channels, num_frames, height, width = hidden_states.shape
330330
if latent_indices is None:
@@ -336,34 +336,34 @@ def _pack_history_states(
336336
frame_indices=latent_indices, height=height, width=width, device=hidden_states.device
337337
)
338338

339-
clean_latents, clean_latents_2x, clean_latents_4x = self.clean_x_embedder(
340-
clean_latents, clean_latents_2x, clean_latents_4x
339+
latents_clean, latents_history_2x, latents_history_4x = self.clean_x_embedder(
340+
latents_clean, latents_history_2x, latents_history_4x
341341
)
342342

343-
if clean_latents is not None:
344-
clean_rotary_emb_1x = self.rope(
345-
frame_indices=clean_latent_indices, height=height, width=width, device=clean_latents.device
343+
if latents_clean is not None:
344+
image_rotary_emb_clean = self.rope(
345+
frame_indices=indices_latents_clean, height=height, width=width, device=latents_clean.device
346346
)
347-
hidden_states = torch.cat([clean_latents, hidden_states], dim=1)
348-
image_rotary_emb = torch.cat([clean_rotary_emb_1x, image_rotary_emb], dim=1)
347+
hidden_states = torch.cat([latents_clean, hidden_states], dim=1)
348+
image_rotary_emb = torch.cat([image_rotary_emb_clean, image_rotary_emb], dim=1)
349349

350-
if clean_latents_2x is not None and clean_latent_2x_indices is not None:
351-
clean_rotary_emb_2x = self.rope(
352-
frame_indices=clean_latent_2x_indices, height=height, width=width, device=clean_latents_2x.device
350+
if latents_history_2x is not None and indices_latents_history_2x is not None:
351+
image_rotary_emb_history_2x = self.rope(
352+
frame_indices=indices_latents_history_2x, height=height, width=width, device=latents_history_2x.device
353353
)
354-
clean_rotary_emb_2x = _pad_for_3d_conv(clean_rotary_emb_2x, (2, 2, 2))
355-
clean_rotary_emb_2x = _center_down_sample_3d(clean_rotary_emb_2x, (2, 2, 2))
356-
hidden_states = torch.cat([clean_latents_2x, hidden_states], dim=1)
357-
image_rotary_emb = torch.cat([clean_rotary_emb_2x, image_rotary_emb], dim=1)
358-
359-
if clean_latents_4x is not None and clean_latent_4x_indices is not None:
360-
clean_rotary_emb_4x = self.rope(
361-
frame_indices=clean_latent_4x_indices, height=height, width=width, device=clean_latents_4x.device
354+
image_rotary_emb_history_2x = _pad_for_3d_conv(image_rotary_emb_history_2x, (2, 2, 2))
355+
image_rotary_emb_history_2x = _center_down_sample_3d(image_rotary_emb_history_2x, (2, 2, 2))
356+
hidden_states = torch.cat([latents_history_2x, hidden_states], dim=1)
357+
image_rotary_emb = torch.cat([image_rotary_emb_history_2x, image_rotary_emb], dim=1)
358+
359+
if latents_history_4x is not None and indices_latents_history_4x is not None:
360+
image_rotary_emb_history_4x = self.rope(
361+
frame_indices=indices_latents_history_4x, height=height, width=width, device=latents_history_4x.device
362362
)
363-
clean_rotary_emb_4x = _pad_for_3d_conv(clean_rotary_emb_4x, (4, 4, 4))
364-
clean_rotary_emb_4x = _center_down_sample_3d(clean_rotary_emb_4x, (4, 4, 4))
365-
hidden_states = torch.cat([clean_latents_4x, hidden_states], dim=1)
366-
image_rotary_emb = torch.cat([clean_rotary_emb_4x, image_rotary_emb], dim=1)
363+
image_rotary_emb_history_4x = _pad_for_3d_conv(image_rotary_emb_history_4x, (4, 4, 4))
364+
image_rotary_emb_history_4x = _center_down_sample_3d(image_rotary_emb_history_4x, (4, 4, 4))
365+
hidden_states = torch.cat([latents_history_4x, hidden_states], dim=1)
366+
image_rotary_emb = torch.cat([image_rotary_emb_history_4x, image_rotary_emb], dim=1)
367367

368368
return hidden_states, image_rotary_emb
369369

0 commit comments

Comments
 (0)