Skip to content

[Single File] Allow loading T5 encoder in mixed precision #8778

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 5 commits into from
Jul 5, 2024
Merged

[Single File] Allow loading T5 encoder in mixed precision #8778

merged 5 commits into from
Jul 5, 2024

Conversation

DN6
Copy link
Collaborator

@DN6 DN6 commented Jul 3, 2024

What does this PR do?

When loading the T5 encoder in SD3 with from_pretrained certain layers are are kept in fp32. Currently this is not the case with single file loading as all modules are cast to torch_dtype when loading the pipeline. This is leading to NaNs being produced by the T5 encoder when loading from single file, leading to a black image.

This PR

  1. Adds a check to T5 loading to convert the required modules to fp32
  2. Removes casting the entire pipeline to torch_dtype. This is now only handled at the model level.
  3. Adds additional tests to the single file suite to check that dtypes are set correctly in the pipeline

Single file tests for SD3 need to be added. I'll handle that in a follow up PR.

Fixes # (issue)
#8759

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.

@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.

Comment on lines -558 to -559
if torch_dtype is not None:
pipe.to(dtype=torch_dtype)
Copy link
Member

Choose a reason for hiding this comment

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

How is this handled then?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At the model level by passing in dtype to their respective loading methods

Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

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

thanks!

@@ -1808,4 +1808,17 @@ def create_diffusers_t5_model_from_checkpoint(

else:
model.load_state_dict(diffusers_format_checkpoint)

Copy link
Collaborator

Choose a reason for hiding this comment

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

ohh is this related to this?
#8604 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah exactly. When you cast the entire model to fp16 the images will be corrupted. You have to use the logic in from_pretrained.

@DN6 DN6 merged commit 0bab9d6 into main Jul 5, 2024
18 checks passed
sayakpaul pushed a commit that referenced this pull request Dec 23, 2024
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