Path: blob/main/tests/pipelines/stable_diffusion_2/test_stable_diffusion_flax.py
1448 views
# coding=utf-81# Copyright 2023 HuggingFace Inc.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.1415import gc16import unittest1718from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline19from diffusers.utils import is_flax_available, slow20from diffusers.utils.testing_utils import require_flax212223if is_flax_available():24import jax25import jax.numpy as jnp26from flax.jax_utils import replicate27from flax.training.common_utils import shard282930@slow31@require_flax32class FlaxStableDiffusion2PipelineIntegrationTests(unittest.TestCase):33def tearDown(self):34# clean up the VRAM after each test35super().tearDown()36gc.collect()3738def test_stable_diffusion_flax(self):39sd_pipe, params = FlaxStableDiffusionPipeline.from_pretrained(40"stabilityai/stable-diffusion-2",41revision="bf16",42dtype=jnp.bfloat16,43)4445prompt = "A painting of a squirrel eating a burger"46num_samples = jax.device_count()47prompt = num_samples * [prompt]48prompt_ids = sd_pipe.prepare_inputs(prompt)4950params = replicate(params)51prompt_ids = shard(prompt_ids)5253prng_seed = jax.random.PRNGKey(0)54prng_seed = jax.random.split(prng_seed, jax.device_count())5556images = sd_pipe(prompt_ids, params, prng_seed, num_inference_steps=25, jit=True)[0]57assert images.shape == (jax.device_count(), 1, 768, 768, 3)5859images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])60image_slice = images[0, 253:256, 253:256, -1]6162output_slice = jnp.asarray(jax.device_get(image_slice.flatten()))63expected_slice = jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.45508, 0.4512])64print(f"output_slice: {output_slice}")65assert jnp.abs(output_slice - expected_slice).max() < 1e-26667def test_stable_diffusion_dpm_flax(self):68model_id = "stabilityai/stable-diffusion-2"69scheduler, scheduler_params = FlaxDPMSolverMultistepScheduler.from_pretrained(model_id, subfolder="scheduler")70sd_pipe, params = FlaxStableDiffusionPipeline.from_pretrained(71model_id,72scheduler=scheduler,73revision="bf16",74dtype=jnp.bfloat16,75)76params["scheduler"] = scheduler_params7778prompt = "A painting of a squirrel eating a burger"79num_samples = jax.device_count()80prompt = num_samples * [prompt]81prompt_ids = sd_pipe.prepare_inputs(prompt)8283params = replicate(params)84prompt_ids = shard(prompt_ids)8586prng_seed = jax.random.PRNGKey(0)87prng_seed = jax.random.split(prng_seed, jax.device_count())8889images = sd_pipe(prompt_ids, params, prng_seed, num_inference_steps=25, jit=True)[0]90assert images.shape == (jax.device_count(), 1, 768, 768, 3)9192images = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:])93image_slice = images[0, 253:256, 253:256, -1]9495output_slice = jnp.asarray(jax.device_get(image_slice.flatten()))96expected_slice = jnp.array([0.4336, 0.42969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297])97print(f"output_slice: {output_slice}")98assert jnp.abs(output_slice - expected_slice).max() < 1e-299100101