Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/pipelines/stable_diffusion/test_stable_diffusion_flax_controlnet.py
1451 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 FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
20
from diffusers.utils import is_flax_available, load_image, 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 FlaxStableDiffusionControlNetPipelineIntegrationTests(unittest.TestCase):
34
def tearDown(self):
35
# clean up the VRAM after each test
36
super().tearDown()
37
gc.collect()
38
39
def test_canny(self):
40
controlnet, controlnet_params = FlaxControlNetModel.from_pretrained(
41
"lllyasviel/sd-controlnet-canny", from_pt=True, dtype=jnp.bfloat16
42
)
43
pipe, params = FlaxStableDiffusionControlNetPipeline.from_pretrained(
44
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, from_pt=True, dtype=jnp.bfloat16
45
)
46
params["controlnet"] = controlnet_params
47
48
prompts = "bird"
49
num_samples = jax.device_count()
50
prompt_ids = pipe.prepare_text_inputs([prompts] * num_samples)
51
52
canny_image = load_image(
53
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png"
54
)
55
processed_image = pipe.prepare_image_inputs([canny_image] * num_samples)
56
57
rng = jax.random.PRNGKey(0)
58
rng = jax.random.split(rng, jax.device_count())
59
60
p_params = replicate(params)
61
prompt_ids = shard(prompt_ids)
62
processed_image = shard(processed_image)
63
64
images = pipe(
65
prompt_ids=prompt_ids,
66
image=processed_image,
67
params=p_params,
68
prng_seed=rng,
69
num_inference_steps=50,
70
jit=True,
71
).images
72
assert images.shape == (jax.device_count(), 1, 768, 512, 3)
73
74
images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])
75
image_slice = images[0, 253:256, 253:256, -1]
76
77
output_slice = jnp.asarray(jax.device_get(image_slice.flatten()))
78
expected_slice = jnp.array(
79
[0.167969, 0.116699, 0.081543, 0.154297, 0.132812, 0.108887, 0.169922, 0.169922, 0.205078]
80
)
81
print(f"output_slice: {output_slice}")
82
assert jnp.abs(output_slice - expected_slice).max() < 1e-2
83
84
def test_pose(self):
85
controlnet, controlnet_params = FlaxControlNetModel.from_pretrained(
86
"lllyasviel/sd-controlnet-openpose", from_pt=True, dtype=jnp.bfloat16
87
)
88
pipe, params = FlaxStableDiffusionControlNetPipeline.from_pretrained(
89
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, from_pt=True, dtype=jnp.bfloat16
90
)
91
params["controlnet"] = controlnet_params
92
93
prompts = "Chef in the kitchen"
94
num_samples = jax.device_count()
95
prompt_ids = pipe.prepare_text_inputs([prompts] * num_samples)
96
97
pose_image = load_image(
98
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png"
99
)
100
processed_image = pipe.prepare_image_inputs([pose_image] * num_samples)
101
102
rng = jax.random.PRNGKey(0)
103
rng = jax.random.split(rng, jax.device_count())
104
105
p_params = replicate(params)
106
prompt_ids = shard(prompt_ids)
107
processed_image = shard(processed_image)
108
109
images = pipe(
110
prompt_ids=prompt_ids,
111
image=processed_image,
112
params=p_params,
113
prng_seed=rng,
114
num_inference_steps=50,
115
jit=True,
116
).images
117
assert images.shape == (jax.device_count(), 1, 768, 512, 3)
118
119
images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])
120
image_slice = images[0, 253:256, 253:256, -1]
121
122
output_slice = jnp.asarray(jax.device_get(image_slice.flatten()))
123
expected_slice = jnp.array(
124
[[0.271484, 0.261719, 0.275391, 0.277344, 0.279297, 0.291016, 0.294922, 0.302734, 0.302734]]
125
)
126
print(f"output_slice: {output_slice}")
127
assert jnp.abs(output_slice - expected_slice).max() < 1e-2
128
129