Skip to content
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

ComfyUI comes up with very different images than Diffusers #3507

Closed
komninoschatzipapas opened this issue May 17, 2024 · 2 comments
Closed

Comments

@komninoschatzipapas
Copy link

I'm using this Python script:

# Use Pytorch 2!
import torch
from diffusers import (
    StableDiffusionPipeline,
    DiffusionPipeline,
    AutoencoderKL,
    UNet2DConditionModel,
    DDPMScheduler,
)
from transformers import CLIPTextModel

# Any model currently on Huggingface Hub.
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipeline = DiffusionPipeline.from_pretrained(model_id)

# Optimize!
pipeline.unet = torch.compile(pipeline.unet)
scheduler = DDPMScheduler.from_pretrained(model_id, subfolder="scheduler")

# Remove this if you get an error.
torch.set_float32_matmul_precision("high")

pipeline.to("cuda")
prompts = {
    "car": "car",
}
for shortname, prompt in prompts.items():
    # old prompt: ''
    image = pipeline(
        prompt=prompt,
        negative_prompt="",
        num_inference_steps=35,
        generator=torch.Generator(device="cuda").manual_seed(42),
        width=832,
        height=1216,
        guidance_scale=5,
    ).images[0]
    image.save(f"./{shortname}_nobetas.png", format="PNG")

Which yields this image:
car_nobetas

Trying to replicate this in ComfyUI (719fb2c):
CleanShot 2024-05-17 at 21 25 06

I'm not sure what I'm missing here but although both setups seem to be using the same model and parameters, the results are quite different.

@RandomGitUser321
Copy link

RandomGitUser321 commented May 17, 2024

Two potential things it could be:

# Remove this if you get an error.
torch.set_float32_matmul_precision("high")

and

generator=torch.Generator(device="cuda").manual_seed(42),

ComfyUI is likely generating the seed on CPU, vs that code performing it on the GPU (in the comfy samplers dropdown, any sampler that says GPU in it is generating the seed on the GPU instead of the CPU). A seed of 42 on a GPU isn't the same as it is for a CPU. Also, when performed on GPU, they can be driver or architecture specific. That's why most people recommend using CPU based seeding for reproducibility.

@komninoschatzipapas
Copy link
Author

@RandomGitUser321, thank you! Generating the seed on the CPU matched the image I was getting from ComfyUI.

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

No branches or pull requests

2 participants