Skip to content

Fix Hunyuan I2V for transformers>4.47.1 #11293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged

Fix Hunyuan I2V for transformers>4.47.1 #11293

merged 2 commits into from
Apr 16, 2025

Conversation

DN6
Copy link
Collaborator

@DN6 DN6 commented Apr 11, 2025

What does this PR do?

We need to manually expand the text_input_ids to include the token ids for the image. The function responsible for it in transformers has been deprecated, and the logic in the current LLaVAProcessor isn't compatible with the way max_sequence_length should be applied in this model.

This PR

  1. Adds an _expand_input_ids_with_image_tokens function to replicate some of the behaviour in the LLava model in transformers<4.47.1
    https://github.com/huggingface/transformers/blob/241c04d36867259cdf11dbb4e9d9a60f9cb65ebc/src/transformers/models/llava/modeling_llava.py#L302

This should keep backwards compatibility and work with the newer transformers versions, provided the prompt template and token indices for the image token do not change (seems unlikely?)

Fixes # (issue)
#11118

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@DN6 DN6 requested a review from a-r-r-o-w April 11, 2025 12:30
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@a-r-r-o-w a-r-r-o-w left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if the outputs match before/after these changes! Prompt template and token indices are very unlikely to change by end users, but even if they do, they need to provide the correct indices to work, so I would assume it should be okay on our end

@@ -100,6 +100,50 @@
}


def _expand_input_ids_with_image_tokens(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe could point to a specific commit by adding a comment about where this was adapted from (assuming if it was)

@DN6
Copy link
Collaborator Author

DN6 commented Apr 14, 2025

LGTM if the outputs match before/after these changes!

Hmm so prompt embeds do change, because the way the input embeds are created in LLava <=4.47.1 is different from the current version. Old version, the input embeds ended up being zero padded because of this line
https://github.com/huggingface/transformers/blob/241c04d36867259cdf11dbb4e9d9a60f9cb65ebc/src/transformers/models/llava/modeling_llava.py#L325

With the current changes, I think the padding token embedding is used to pad to the end of the sequence. Since the attention mask is passed in both cases, all the differences in the embeddings appear to be where the pad tokens are supposed to be, visually the pipeline outputs look pretty much the same, but numerically the prompt embeds are different.

Old:
https://github.com/user-attachments/assets/77917cc9-a52e-41bf-9ec5-4831fd2f61a4

Current Fix:
https://github.com/user-attachments/assets/2af19aa0-23c4-410e-9638-40d100cd6700

@DN6
Copy link
Collaborator Author

DN6 commented Apr 15, 2025

@vladmandic Do you have some time to check if this fixes the issue you were facing?

@vladmandic
Copy link
Contributor

vladmandic commented Apr 15, 2025

quick test HunyuanVideoImageToVideoPipeline with

  • diffusers==hunyuan-i2v-update
  • transformers==4.51.1

lgtm!

@DN6 DN6 merged commit b316104 into main Apr 16, 2025
14 of 15 checks passed
@DN6 DN6 mentioned this pull request Apr 16, 2025
6 tasks
@yiyixuxu yiyixuxu deleted the hunyuan-i2v-update branch April 17, 2025 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants