Skip to content

Commit a6ddd02

Browse files
committed
Make motion_encode_batch_size configurable in pipeline __call__
1 parent e96f638 commit a6ddd02

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/diffusers/models/transformers/transformer_wan_animate.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ def __init__(
10491049
face_encoder_hidden_dim: int = 1024,
10501050
face_encoder_num_heads: int = 4,
10511051
inject_face_latents_blocks: int = 5,
1052-
motion_encoder_batch_size: Optional[int] = 8,
1052+
motion_encoder_batch_size: int = 8,
10531053
) -> None:
10541054
super().__init__()
10551055

@@ -1135,8 +1135,6 @@ def __init__(
11351135

11361136
self.gradient_checkpointing = False
11371137

1138-
self.motion_encoder_batch_size = motion_encoder_batch_size
1139-
11401138
def forward(
11411139
self,
11421140
hidden_states: torch.Tensor,
@@ -1168,8 +1166,9 @@ def forward(
11681166
face_pixel_values (`torch.Tensor` of shape `(B, C', S, H', W')`):
11691167
Face video in pixel space (not latent space). Typically C' = 3 and H' and W' are the height/width of
11701168
the face video in pixels. Here S is the inference segment length, usually set to 77.
1171-
motion_encode_batch_size (`int`, *optional*, defaults to `8`):
1172-
The batch size for batched encoding of the face video via the motion encoder.
1169+
motion_encode_batch_size (`int`, *optional*):
1170+
The batch size for batched encoding of the face video via the motion encoder. Will default to
1171+
`self.config.motion_encoder_batch_size` if not set.
11731172
return_dict (`bool`, *optional*, defaults to `True`):
11741173
Whether to return the output as a dict or tuple.
11751174
"""
@@ -1233,7 +1232,7 @@ def forward(
12331232

12341233
# Extract motion features using motion encoder
12351234
# Perform batched motion encoder inference to allow trading off inference speed for memory usage
1236-
motion_encode_batch_size = motion_encode_batch_size or self.motion_encoder_batch_size
1235+
motion_encode_batch_size = motion_encode_batch_size or self.config.motion_encoder_batch_size
12371236
face_batches = torch.split(face_pixel_values, motion_encode_batch_size)
12381237
motion_vec_batches = []
12391238
for face_batch in face_batches:

src/diffusers/pipelines/wan/pipeline_wan_animate.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ def __call__(
787787
num_inference_steps: int = 20,
788788
mode: str = "animate",
789789
prev_segment_conditioning_frames: int = 1,
790+
motion_encode_batch_size: Optional[int] = None,
790791
guidance_scale: float = 1.0,
791792
num_videos_per_prompt: Optional[int] = 1,
792793
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
@@ -832,6 +833,10 @@ def __call__(
832833
prev_segment_conditioning_frames (`int`, defaults to `1`):
833834
The number of frames from the previous video segment to be used for temporal guidance. Recommended to
834835
be 1 or 5. In general, should be 4N + 1, where N is a non-negative integer.
836+
motion_encode_batch_size (`int`, *optional*):
837+
The batch size for batched encoding of the face video via the motion encoder. This allows trading off
838+
inference speed for lower memory usage by setting a smaller batch size. Will default to
839+
`self.transformer.config.motion_encoder_batch_size` if not set.
835840
height (`int`, defaults to `720`):
836841
The height of the generated video.
837842
width (`int`, defaults to `1280`):
@@ -1127,6 +1132,7 @@ def __call__(
11271132
encoder_hidden_states_image=image_embeds,
11281133
pose_hidden_states=pose_latents,
11291134
face_pixel_values=face_video_segment,
1135+
motion_encode_batch_size=motion_encode_batch_size,
11301136
attention_kwargs=attention_kwargs,
11311137
return_dict=False,
11321138
)[0]
@@ -1142,6 +1148,7 @@ def __call__(
11421148
encoder_hidden_states_image=image_embeds,
11431149
pose_hidden_states=pose_latents,
11441150
face_pixel_values=face_pixel_values_uncond,
1151+
motion_encode_batch_size=motion_encode_batch_size,
11451152
attention_kwargs=attention_kwargs,
11461153
return_dict=False,
11471154
)[0]

0 commit comments

Comments
 (0)