[SDXL Flax] fix SDXL flax init#5187
Merged
Merged
Conversation
| is_refiner = 5 * self.config.addition_time_embed_dim + self.config.cross_attention_dim == self.config.projection_class_embeddings_input_dim | ||
| num_micro_conditions = 5 if is_refiner else 6 | ||
|
|
||
| text_embeds_dim = self.config.projection_class_embeddings_input_dim - (num_micro_conditions * self.config.addition_time_embed_dim) |
Contributor
Author
There was a problem hiding this comment.
2816 - 6 * 256 = 1280 for base and 2560 - 5 * 256 = 1280 for refiner
| ) | ||
|
|
||
| # scale the initial noise by the standard deviation required by the scheduler | ||
| latents = latents * scheduler_state.init_noise_sigma |
Contributor
Author
There was a problem hiding this comment.
We need to set the init_noise_sigma atfter scaling
Contributor
Author
|
@pcuenca I'm now getting almost identical results on CPU with PyTorch vs. Flax for dummy inputs: Flax from diffusers import FlaxStableDiffusionXLPipeline
import numpy as np
import jax.numpy as jnp
import jax
path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe"
pipe, params = FlaxStableDiffusionXLPipeline.from_pretrained(path)
prompt = "An astronaut riding a green horse on Mars"
negative_prompt = "ugly"
steps = 3
batch_size, height, width, ch = 1, 32, 32, 4
num_elems = batch_size * height * width * ch
rng = jax.random.PRNGKey(0)
latents = (jnp.arange(num_elems) / num_elems)[:, None, None, None].reshape(batch_size, ch, width, height)
print("latents", np.abs(np.asarray(latents)).sum())
prompt_embeds = pipe.prepare_inputs(prompt)
neg_prompt_ids = pipe.prepare_inputs(negative_prompt)
image = pipe(prompt_embeds, params, rng, neg_prompt_ids=neg_prompt_ids, latents=latents, num_inference_steps=3, output_type="np").images[0]
print(np.abs(np.asarray(image)).sum())PT import torch
import numpy as np
from diffusers import StableDiffusionXLPipeline
path = "hf-internal-testing/tiny-stable-diffusion-xl-pipe"
pipe = StableDiffusionXLPipeline.from_pretrained(path)
pipe.unet.set_default_attn_processor()
prompt = "An astronaut riding a green horse on Mars"
neg_prompt = "ugly"
steps = 3
batch_size, height, width, ch = 1, 32, 32, 4
num_elems = batch_size * height * width * ch
latents = (torch.arange(num_elems) / num_elems)[:, None, None, None].reshape(batch_size, ch, width, height)
print("latents", latents.abs().sum())
image = pipe(prompt, negative_prompt=neg_prompt, latents=latents, num_inference_steps=3, output_type="np", guidance_scale=7.5).images[0]
print(np.abs(image).sum())Getting: |
yoonseokjin
pushed a commit
to yoonseokjin/diffusers
that referenced
this pull request
Dec 25, 2023
* fix SDXL flax init * finish * Fix
AmericanPresidentJimmyCarter
pushed a commit
to AmericanPresidentJimmyCarter/diffusers
that referenced
this pull request
Apr 26, 2024
* fix SDXL flax init * finish * Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make sure Flax init is correct for different model sizes