Skip to content

Commit 1d51224

Browse files
[Flax] Complete tests (#828)
1 parent 7c22626 commit 1d51224

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

tests/test_pipelines_flax.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
if is_flax_available():
2525
import jax
2626
import jax.numpy as jnp
27-
from diffusers import FlaxStableDiffusionPipeline
27+
from diffusers import FlaxDDIMScheduler, FlaxStableDiffusionPipeline
2828
from flax.jax_utils import replicate
2929
from flax.training.common_utils import shard
3030
from jax import pmap
@@ -61,7 +61,7 @@ def test_dummy_all_tpus(self):
6161

6262
assert images.shape == (8, 1, 64, 64, 3)
6363
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
6565

6666
images_pil = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
6767

@@ -93,13 +93,9 @@ def test_stable_diffusion_v1_4(self):
9393

9494
images = p_sample(prompt_ids, params, prng_seed, num_inference_steps).images
9595

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-
10096
assert images.shape == (8, 1, 512, 512, 3)
10197
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
10399

104100
def test_stable_diffusion_v1_4_bfloat_16(self):
105101
pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
@@ -129,7 +125,7 @@ def test_stable_diffusion_v1_4_bfloat_16(self):
129125

130126
assert images.shape == (8, 1, 512, 512, 3)
131127
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
133129

134130
def test_stable_diffusion_v1_4_bfloat_16_with_safety(self):
135131
pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
@@ -157,4 +153,49 @@ def test_stable_diffusion_v1_4_bfloat_16_with_safety(self):
157153

158154
assert images.shape == (8, 1, 512, 512, 3)
159155
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

Comments
 (0)