Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix Flux CLIP prompt embeds repeat for num_images_per_prompt > 1 #9280

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

DN6
Copy link
Collaborator

@DN6 DN6 commented Aug 26, 2024

What does this PR do?

Flux uses the pooled CLIP prompt embeds, so it produces a 2D tensor (batch_size, dim) rather than a 3D tensor (batch_size, seq_len, dim). The current way of repeating the prompt embeds for num_images_per_prompt is incorrect. This PR corrects the behaviour

Fixes # (issue)
#9215

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 yiyixuxu August 26, 2024 15:18
@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
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 for the fix!
but this didn't cause different results, no?

@DN6
Copy link
Collaborator Author

DN6 commented Aug 27, 2024

So the effect can be subtle for num_images_per_prompt>1 . e.g In the current implementation with this snippet

prompt = ["a cat", "a dog"]

height = 1024
width = 768

images = pipe(
    prompt=prompt,
    guidance_scale=3.5,
    num_inference_steps=20,
    num_images_per_prompt=2,
    generator=torch.Generator("cpu").manual_seed(1),
    height=height,
    width=width
).images

The CLIP prompt embeds alternate between cat/dog embeddings

tensor([[-0.5312,  0.2305,  0.1787,  ..., -0.3242,  0.5781, -0.7578],
        [-0.4375,  0.5430, -0.2891,  ..., -0.1133,  0.0938, -0.3477],
        [-0.5312,  0.2305,  0.1787,  ..., -0.3242,  0.5781, -0.7578],
        [-0.4375,  0.5430, -0.2891,  ..., -0.1133,  0.0938, -0.3477]],
       device='cuda:0', dtype=torch.bfloat16)

but it should be (2 cat embeddings, 2 dog embeddings)

tensor([[-0.5312,  0.2305,  0.1787,  ..., -0.3242,  0.5781, -0.7578],
        [-0.5312,  0.2305,  0.1787,  ..., -0.3242,  0.5781, -0.7578],
        [-0.4375,  0.5430, -0.2891,  ..., -0.1133,  0.0938, -0.3477],
        [-0.4375,  0.5430, -0.2891,  ..., -0.1133,  0.0938, -0.3477]],
       device='cuda:0', dtype=torch.bfloat16)

Since the T5 embeddings are in the right order, what happens in this case is that

1st image (should be cat) - uses CLIP cat embedding + T5 cat embedding
2nd image (should be cat) - uses CLIP dog embedding + T5 cat embedding
3rd image (should be dog) - uses CLIP cat embedding + T5 dog embedding
4th image (should be dog) - uses CLIP dog embedding + T5 dog embedding  

@yiyixuxu
Copy link
Collaborator

@DN6 got it!
let's do a patch for this then!

@yiyixuxu yiyixuxu merged commit 05b38c3 into main Aug 27, 2024
18 checks passed
@yiyixuxu yiyixuxu deleted the flux-image-per-prompt branch August 27, 2024 17:41
Gothos added a commit to Gothos/diffusers that referenced this pull request Aug 31, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants