|
24 | 24 | if is_flax_available():
|
25 | 25 | import jax
|
26 | 26 | import jax.numpy as jnp
|
27 |
| - from diffusers import FlaxStableDiffusionPipeline |
| 27 | + from diffusers import FlaxDDIMScheduler, FlaxStableDiffusionPipeline |
28 | 28 | from flax.jax_utils import replicate
|
29 | 29 | from flax.training.common_utils import shard
|
30 | 30 | from jax import pmap
|
@@ -61,7 +61,7 @@ def test_dummy_all_tpus(self):
|
61 | 61 |
|
62 | 62 | assert images.shape == (8, 1, 64, 64, 3)
|
63 | 63 | assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.float32).sum() - 4.151474)) < 1e-3
|
64 |
| - assert np.abs((np.abs(images, dtype=np.float32).sum() - 49947.875)) < 1e-2 |
| 64 | + assert np.abs((np.abs(images, dtype=np.float32).sum() - 49947.875)) < 5e-1 |
65 | 65 |
|
66 | 66 | images_pil = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
|
67 | 67 |
|
@@ -93,13 +93,9 @@ def test_stable_diffusion_v1_4(self):
|
93 | 93 |
|
94 | 94 | images = p_sample(prompt_ids, params, prng_seed, num_inference_steps).images
|
95 | 95 |
|
96 |
| - images_pil = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]))) |
97 |
| - for i, image in enumerate(images_pil): |
98 |
| - image.save(f"/home/patrick/images/flax-test-{i}_fp32.png") |
99 |
| - |
100 | 96 | assert images.shape == (8, 1, 512, 512, 3)
|
101 | 97 | assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.float32).sum() - 0.05652401)) < 1e-3
|
102 |
| - assert np.abs((np.abs(images, dtype=np.float32).sum() - 2383808.2)) < 1e-2 |
| 98 | + assert np.abs((np.abs(images, dtype=np.float32).sum() - 2383808.2)) < 5e-1 |
103 | 99 |
|
104 | 100 | def test_stable_diffusion_v1_4_bfloat_16(self):
|
105 | 101 | pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
|
@@ -129,7 +125,7 @@ def test_stable_diffusion_v1_4_bfloat_16(self):
|
129 | 125 |
|
130 | 126 | assert images.shape == (8, 1, 512, 512, 3)
|
131 | 127 | assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.float32).sum() - 0.06652832)) < 1e-3
|
132 |
| - assert np.abs((np.abs(images, dtype=np.float32).sum() - 2384849.8)) < 1e-2 |
| 128 | + assert np.abs((np.abs(images, dtype=np.float32).sum() - 2384849.8)) < 5e-1 |
133 | 129 |
|
134 | 130 | def test_stable_diffusion_v1_4_bfloat_16_with_safety(self):
|
135 | 131 | pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
|
@@ -157,4 +153,49 @@ def test_stable_diffusion_v1_4_bfloat_16_with_safety(self):
|
157 | 153 |
|
158 | 154 | assert images.shape == (8, 1, 512, 512, 3)
|
159 | 155 | assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.float32).sum() - 0.06652832)) < 1e-3
|
160 |
| - assert np.abs((np.abs(images, dtype=np.float32).sum() - 2384849.8)) < 1e-2 |
| 156 | + assert np.abs((np.abs(images, dtype=np.float32).sum() - 2384849.8)) < 5e-1 |
| 157 | + |
| 158 | + def test_stable_diffusion_v1_4_bfloat_16_ddim(self): |
| 159 | + scheduler = FlaxDDIMScheduler( |
| 160 | + beta_start=0.00085, |
| 161 | + beta_end=0.012, |
| 162 | + beta_schedule="scaled_linear", |
| 163 | + set_alpha_to_one=False, |
| 164 | + steps_offset=1, |
| 165 | + ) |
| 166 | + |
| 167 | + pipeline, params = FlaxStableDiffusionPipeline.from_pretrained( |
| 168 | + "CompVis/stable-diffusion-v1-4", |
| 169 | + revision="bf16", |
| 170 | + dtype=jnp.bfloat16, |
| 171 | + scheduler=scheduler, |
| 172 | + safety_checker=None, |
| 173 | + ) |
| 174 | + scheduler_state = scheduler.create_state() |
| 175 | + |
| 176 | + params["scheduler"] = scheduler_state |
| 177 | + |
| 178 | + prompt = ( |
| 179 | + "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of" |
| 180 | + " field, close up, split lighting, cinematic" |
| 181 | + ) |
| 182 | + |
| 183 | + prng_seed = jax.random.PRNGKey(0) |
| 184 | + num_inference_steps = 50 |
| 185 | + |
| 186 | + num_samples = jax.device_count() |
| 187 | + prompt = num_samples * [prompt] |
| 188 | + prompt_ids = pipeline.prepare_inputs(prompt) |
| 189 | + |
| 190 | + p_sample = pmap(pipeline.__call__, static_broadcasted_argnums=(3,)) |
| 191 | + |
| 192 | + # shard inputs and rng |
| 193 | + params = replicate(params) |
| 194 | + prng_seed = jax.random.split(prng_seed, 8) |
| 195 | + prompt_ids = shard(prompt_ids) |
| 196 | + |
| 197 | + images = p_sample(prompt_ids, params, prng_seed, num_inference_steps).images |
| 198 | + |
| 199 | + assert images.shape == (8, 1, 512, 512, 3) |
| 200 | + assert np.abs((np.abs(images[0, 0, :2, :2, -2:], dtype=np.float32).sum() - 0.045043945)) < 1e-3 |
| 201 | + assert np.abs((np.abs(images, dtype=np.float32).sum() - 2347693.5)) < 5e-1 |
0 commit comments