Skip to content

clip-guided stable diffusion correctness #596

Closed

Description

@keturn

Describe the bug

the clip-guided pipeline uses these text-embeddings:

# perform clip guidance
if clip_guidance_scale > 0:
text_embeddings_for_guidance = (
text_embeddings.chunk(2)[0] if do_classifier_free_guidance else text_embeddings

defined earlier as:

# For classifier free guidance, we need to do two forward passes.
# Here we concatenate the unconditional and text embeddings into a single batch
# to avoid doing two forward passes
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])

which I read as using the unconditioned (i.e. null prompt) embeddings.

Is that the way it's supposed to work? That doesn't feel like how it's supposed to work. Like, if the normal classifier-free guidance function is turned off, it would be using the embeddings from the text prompt, not the nulls.

But this pipeline was added without any tests or samples or other reference material, so I really don't know.

Reproduction

No response

Logs

No response

System Info

👀

Activity

  1. patrickvonplaten commented on Sep 22, 2022

    @patrickvonplaten
    Contributor

    cc @patil-suraj might be good to take a look soon since we just released it :-)

  2. keturn commented on Sep 23, 2022

    @keturn
    ContributorAuthor

    Someone posted another concern on discord:

    I think there's a bug in the community CLIP guidance example -

    pred_original_sample = (latents - beta_prod_t ** (0.5) * noise_pred) / alpha_prod_t ** (0.5)
    fac = torch.sqrt(beta_prod_t)
    sample = pred_original_sample * (fac) + latents * (1 - fac)

    IIUC the correct behavior would be to use pred_original_sample to do the CLIP comparison - i.e. it should be the sample, not pred_original_sample * (fac) + latents * (1 - fac)

  3. patil-suraj commented on Sep 28, 2022

    @patil-suraj
    Contributor

    That's a great catch @keturn ! Sorry to come back to this only now, will send a fix right away.

  4. ajundo commented on Sep 29, 2022

    @ajundo

    Someone posted another concern on discord:

    I think there's a bug in the community CLIP guidance example -

    pred_original_sample = (latents - beta_prod_t ** (0.5) * noise_pred) / alpha_prod_t ** (0.5)
    fac = torch.sqrt(beta_prod_t)
    sample = pred_original_sample * (fac) + latents * (1 - fac)

    IIUC the correct behavior would be to use pred_original_sample to do the CLIP comparison - i.e. it should be the sample, not pred_original_sample * (fac) + latents * (1 - fac)

    Afaik, this is correct because the conditional diffusion is conditional on predicted x_t rather than predicted x_0。

  5. zzhang2816 commented on Apr 11, 2023

    @zzhang2816

    I think the correct format is "sample = pred_original_sample * (fac) + noise_pred * (1 - fac)"
    Screenshot 2023-04-11 at 15 33 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions