Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/pipelines/stable_diffusion_2/test_stable_diffusion_flax.py
1448 views
1
# coding=utf-8
2
# Copyright 2023 HuggingFace Inc.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import gc
17
import unittest
18
19
from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
20
from diffusers.utils import is_flax_available, slow
21
from diffusers.utils.testing_utils import require_flax
22
23
24
if is_flax_available():
25
import jax
26
import jax.numpy as jnp
27
from flax.jax_utils import replicate
28
from flax.training.common_utils import shard
29
30
31
@slow
32
@require_flax
33
class FlaxStableDiffusion2PipelineIntegrationTests(unittest.TestCase):
34
def tearDown(self):
35
# clean up the VRAM after each test
36
super().tearDown()
37
gc.collect()
38
39
def test_stable_diffusion_flax(self):
40
sd_pipe, params = FlaxStableDiffusionPipeline.from_pretrained(
41
"stabilityai/stable-diffusion-2",
42
revision="bf16",
43
dtype=jnp.bfloat16,
44
)
45
46
prompt = "A painting of a squirrel eating a burger"
47
num_samples = jax.device_count()
48
prompt = num_samples * [prompt]
49
prompt_ids = sd_pipe.prepare_inputs(prompt)
50
51
params = replicate(params)
52
prompt_ids = shard(prompt_ids)
53
54
prng_seed = jax.random.PRNGKey(0)
55
prng_seed = jax.random.split(prng_seed, jax.device_count())
56
57
images = sd_pipe(prompt_ids, params, prng_seed, num_inference_steps=25, jit=True)[0]
58
assert images.shape == (jax.device_count(), 1, 768, 768, 3)
59
60
images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])
61
image_slice = images[0, 253:256, 253:256, -1]
62
63
output_slice = jnp.asarray(jax.device_get(image_slice.flatten()))
64
expected_slice = jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.45508, 0.4512])
65
print(f"output_slice: {output_slice}")
66
assert jnp.abs(output_slice - expected_slice).max() < 1e-2
67
68
def test_stable_diffusion_dpm_flax(self):
69
model_id = "stabilityai/stable-diffusion-2"
70
scheduler, scheduler_params = FlaxDPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")
71
sd_pipe, params = FlaxStableDiffusionPipeline.from_pretrained(
72
model_id,
73
scheduler=scheduler,
74
revision="bf16",
75
dtype=jnp.bfloat16,
76
)
77
params["scheduler"] = scheduler_params
78
79
prompt = "A painting of a squirrel eating a burger"
80
num_samples = jax.device_count()
81
prompt = num_samples * [prompt]
82
prompt_ids = sd_pipe.prepare_inputs(prompt)
83
84
params = replicate(params)
85
prompt_ids = shard(prompt_ids)
86
87
prng_seed = jax.random.PRNGKey(0)
88
prng_seed = jax.random.split(prng_seed, jax.device_count())
89
90
images = sd_pipe(prompt_ids, params, prng_seed, num_inference_steps=25, jit=True)[0]
91
assert images.shape == (jax.device_count(), 1, 768, 768, 3)
92
93
images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])
94
image_slice = images[0, 253:256, 253:256, -1]
95
96
output_slice = jnp.asarray(jax.device_get(image_slice.flatten()))
97
expected_slice = jnp.array([0.4336, 0.42969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297])
98
print(f"output_slice: {output_slice}")
99
assert jnp.abs(output_slice - expected_slice).max() < 1e-2
100
101