import os
import tempfile
import unittest

import numpy as np

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax, slow


if is_flax_available():
		import jax
		import jax.numpy as jnp
		from flax.jax_utils import replicate
		from flax.training.common_utils import shard

		from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline






@require_flax
class 		a__						(					unittest.TestCase					):

					def 				__UpperCamelCase   (    self      :       Dict    ):







							"""simple docstring"""






							with tempfile.TemporaryDirectory() as tmpdirname:
									# pipeline has Flax weights
									SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    FlaxDiffusionPipeline.from_pretrained(
									    "hf-internal-testing/tiny-stable-diffusion-pipe",safety_checker=_A,cache_dir=_A    )

									SCREAMING_SNAKE_CASE_  :		int				      =    [t[-1] for t in os.walk(os.path.join(_A,os.listdir(_A    )[0],"snapshots"    )    )]
									SCREAMING_SNAKE_CASE_  :		Any				      =    [item for sublist in all_root_files for item in sublist]

									# None of the downloaded files should be a PyTorch file even if we have some here:
									# https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
									assert not any(f.endswith(".bin"    ) for f in files    )







@slow
@require_flax
class 		a__						(					unittest.TestCase					):

					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Any				      =    FlaxStableDiffusionPipeline.from_pretrained(
							    "hf-internal-testing/tiny-stable-diffusion-pipe",safety_checker=_A    )

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    (
							    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
							    " field, close up, split lighting, cinematic"
							)

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    jax.random.PRNGKey(0    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    4

							SCREAMING_SNAKE_CASE_  :		int				      =    jax.device_count()
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    num_samples * [prompt]
							SCREAMING_SNAKE_CASE_  :		int				      =    pipeline.prepare_inputs(_A    )

							# shard inputs and rng
							SCREAMING_SNAKE_CASE_  :		Any				      =    replicate(_A    )
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    jax.random.split(_A,_A    )
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    shard(_A    )

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    pipeline(_A,_A,_A,_A,jit=_A    ).images

							assert images.shape == (num_samples, 1, 64, 64, 3)
							if jax.device_count() == 8:
									assert np.abs(np.abs(images[0, 0, :2, :2, -2:],dtype=np.floataa    ).sum() - 4.1514745    ) < 1E-3
									assert np.abs(np.abs(_A,dtype=np.floataa    ).sum() - 49947.875    ) < 5E-1

							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]    )    )    )
							assert len(_A    ) == num_samples

					def 				__UpperCamelCase   (    self      :       Optional[Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    FlaxStableDiffusionPipeline.from_pretrained(
							    "CompVis/stable-diffusion-v1-4",revision="flax",safety_checker=_A    )

							SCREAMING_SNAKE_CASE_  :		Dict				      =    (
							    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
							    " field, close up, split lighting, cinematic"
							)

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    jax.random.PRNGKey(0    )
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    50

							SCREAMING_SNAKE_CASE_  :		int				      =    jax.device_count()
							SCREAMING_SNAKE_CASE_  :		str				      =    num_samples * [prompt]
							SCREAMING_SNAKE_CASE_  :		str				      =    pipeline.prepare_inputs(_A    )

							# shard inputs and rng
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    replicate(_A    )
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    jax.random.split(_A,_A    )
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    shard(_A    )

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    pipeline(_A,_A,_A,_A,jit=_A    ).images

							assert images.shape == (num_samples, 1, 512, 512, 3)
							if jax.device_count() == 8:
									assert np.abs((np.abs(images[0, 0, :2, :2, -2:],dtype=np.floataa    ).sum() - 0.05652401)    ) < 1E-3
									assert np.abs((np.abs(_A,dtype=np.floataa    ).sum() - 2383808.2)    ) < 5E-1

					def 				__UpperCamelCase   (    self      :       List[Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Dict				      =    FlaxStableDiffusionPipeline.from_pretrained(
							    "CompVis/stable-diffusion-v1-4",revision="bf16",dtype=jnp.bfloataa,safety_checker=_A    )

							SCREAMING_SNAKE_CASE_  :		str				      =    (
							    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
							    " field, close up, split lighting, cinematic"
							)

							SCREAMING_SNAKE_CASE_  :		Dict				      =    jax.random.PRNGKey(0    )
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    50

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    jax.device_count()
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    num_samples * [prompt]
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    pipeline.prepare_inputs(_A    )

							# shard inputs and rng
							SCREAMING_SNAKE_CASE_  :		int				      =    replicate(_A    )
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    jax.random.split(_A,_A    )
							SCREAMING_SNAKE_CASE_  :		str				      =    shard(_A    )

							SCREAMING_SNAKE_CASE_  :		Tuple				      =    pipeline(_A,_A,_A,_A,jit=_A    ).images

							assert images.shape == (num_samples, 1, 512, 512, 3)
							if jax.device_count() == 8:
									assert np.abs((np.abs(images[0, 0, :2, :2, -2:],dtype=np.floataa    ).sum() - 0.04003906)    ) < 1E-3
									assert np.abs((np.abs(_A,dtype=np.floataa    ).sum() - 2373516.75)    ) < 5E-1

					def 				__UpperCamelCase   (    self      :       Optional[int]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		str				      =    FlaxStableDiffusionPipeline.from_pretrained(
							    "CompVis/stable-diffusion-v1-4",revision="bf16",dtype=jnp.bfloataa    )

							SCREAMING_SNAKE_CASE_  :		Any				      =    (
							    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
							    " field, close up, split lighting, cinematic"
							)

							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    jax.random.PRNGKey(0    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    50

							SCREAMING_SNAKE_CASE_  :		int				      =    jax.device_count()
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    num_samples * [prompt]
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    pipeline.prepare_inputs(_A    )

							# shard inputs and rng
							SCREAMING_SNAKE_CASE_  :		Dict				      =    replicate(_A    )
							SCREAMING_SNAKE_CASE_  :		Dict				      =    jax.random.split(_A,_A    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    shard(_A    )

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    pipeline(_A,_A,_A,_A,jit=_A    ).images

							assert images.shape == (num_samples, 1, 512, 512, 3)
							if jax.device_count() == 8:
									assert np.abs((np.abs(images[0, 0, :2, :2, -2:],dtype=np.floataa    ).sum() - 0.04003906)    ) < 1E-3
									assert np.abs((np.abs(_A,dtype=np.floataa    ).sum() - 2373516.75)    ) < 5E-1

					def 				__UpperCamelCase   (    self      :       Any    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Dict				      =    FlaxDDIMScheduler(
							    beta_start=0.00085,beta_end=0.012,beta_schedule="scaled_linear",set_alpha_to_one=_A,steps_offset=1,)

							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    FlaxStableDiffusionPipeline.from_pretrained(
							    "CompVis/stable-diffusion-v1-4",revision="bf16",dtype=jnp.bfloataa,scheduler=_A,safety_checker=_A,)
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    scheduler.create_state()

							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    scheduler_state

							SCREAMING_SNAKE_CASE_  :		str				      =    (
							    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
							    " field, close up, split lighting, cinematic"
							)

							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    jax.random.PRNGKey(0    )
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    50

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    jax.device_count()
							SCREAMING_SNAKE_CASE_  :		Dict				      =    num_samples * [prompt]
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    pipeline.prepare_inputs(_A    )

							# shard inputs and rng
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    replicate(_A    )
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    jax.random.split(_A,_A    )
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    shard(_A    )

							SCREAMING_SNAKE_CASE_  :		str				      =    pipeline(_A,_A,_A,_A,jit=_A    ).images

							assert images.shape == (num_samples, 1, 512, 512, 3)
							if jax.device_count() == 8:
									assert np.abs((np.abs(images[0, 0, :2, :2, -2:],dtype=np.floataa    ).sum() - 0.045043945)    ) < 1E-3
									assert np.abs((np.abs(_A,dtype=np.floataa    ).sum() - 2347693.5)    ) < 5E-1






					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int				      =    (
							    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
							    " field, close up, split lighting, cinematic"
							)

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    jax.device_count()
							SCREAMING_SNAKE_CASE_  :		Any				      =    num_samples * [prompt]
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    jax.random.split(jax.random.PRNGKey(0    ),_A    )

							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Tuple				      =    FlaxStableDiffusionPipeline.from_pretrained(
							    "CompVis/stable-diffusion-v1-4",revision="bf16",dtype=jnp.bfloataa,safety_checker=_A,)

							SCREAMING_SNAKE_CASE_  :		str				      =    replicate(_A    )
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    pipeline.prepare_inputs(_A    )
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    shard(_A    )
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    pipeline(_A,_A,_A,jit=_A    ).images
							assert images.shape == (num_samples, 1, 512, 512, 3)
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    images[2, 0, 256, 10:17, 1]

							# With memory efficient attention
							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Tuple				      =    FlaxStableDiffusionPipeline.from_pretrained(
							    "CompVis/stable-diffusion-v1-4",revision="bf16",dtype=jnp.bfloataa,safety_checker=_A,use_memory_efficient_attention=_A,)

							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    replicate(_A    )
							SCREAMING_SNAKE_CASE_  :		Any				      =    pipeline.prepare_inputs(_A    )
							SCREAMING_SNAKE_CASE_  :		int				      =    shard(_A    )
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    pipeline(_A,_A,_A,jit=_A    ).images
							assert images_eff.shape == (num_samples, 1, 512, 512, 3)
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    images[2, 0, 256, 10:17, 1]

							# I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
							# over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
							assert abs(slice_eff - slice    ).max() < 1E-2

# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from argparse import ArgumentParser

from accelerate.commands.config import get_config_parser
from accelerate.commands.env import env_command_parser
from accelerate.commands.launch import launch_command_parser
from accelerate.commands.test import test_command_parser
from accelerate.commands.tpu import tpu_command_parser




def       _snake_case	(   ):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		int				      =    ArgumentParser("Accelerate CLI tool"   , usage="accelerate <command> [<args>]"   , allow_abbrev=lowerCAmelCase	)
		SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    parser.add_subparsers(help="accelerate command helpers"	)

		# Register commands
		get_config_parser(subparsers=lowerCAmelCase	)
		env_command_parser(subparsers=lowerCAmelCase	)
		launch_command_parser(subparsers=lowerCAmelCase	)
		tpu_command_parser(subparsers=lowerCAmelCase	)
		test_command_parser(subparsers=lowerCAmelCase	)

		# Let's go
		SCREAMING_SNAKE_CASE_  :		Dict				      =    parser.parse_args()

		if not hasattr(lowerCAmelCase   , "func"	):
				parser.print_help()
				exit(1	)

		# Run
		args.func(lowerCAmelCase	)


if __name__ == "__main__":
		main()
