[Perf] Trim HunyuanVideo encoder padding tokens (#3844)

Signed-off-by: david6666666 <530634352@qq.com>
Co-authored-by: SYLAR <125541396+lishunyang12@users.noreply.github.com>
This commit is contained in:
WeiQing ChenandSYLAR authored and GitHub committed 2026-05-27 14:17:10 +08:00
1 parent 753508fe02
commit 41cb6378a2
1 file changed
+7
@@ -764,6 +764,13 @@ class HunyuanVideo15Transformer3DModel(nn.Module):
# Create explicit attn_mask for image tokens when SP auto_pad is active.
ctx = get_forward_context()
if not ctx.sp_active:
max_valid_encoder_tokens = int(encoder_attention_mask.sum(dim=1).max().item())
if max_valid_encoder_tokens < encoder_attention_mask.shape[1]:
encoder_hidden_states = encoder_hidden_states[:, :max_valid_encoder_tokens]
encoder_attention_mask = encoder_attention_mask[:, :max_valid_encoder_tokens]
if encoder_attention_mask.all():
encoder_attention_mask = None
hidden_states_mask = None
if ctx.sp_original_seq_len is not None and ctx.sp_padding_size > 0:
padded_seq_len = ctx.sp_original_seq_len + ctx.sp_padding_size