



'''simple docstring'''





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		):



	'''simple docstring'''







	def       a__						(self )					->  int:


						"""simple docstring"""



						with tempfile.TemporaryDirectory() as tmpdirname:
											# pipeline has Flax weights
											_a											=						FlaxDiffusionPipeline.from_pretrained(
											    '''hf-internal-testing/tiny-stable-diffusion-pipe'''				,     safety_checker=lowerCamelCase__				,     cache_dir=lowerCamelCase__ )

											_a											=						[t[-1] for t in os.walk(os.path.join(lowerCamelCase__				,     os.listdir(lowerCamelCase__ )[0]				,     '''snapshots''' ) )]
											_a											=						[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		):



	'''simple docstring'''







	def       a__						(self )					->  Optional[int]:


						"""simple docstring"""



						_a											=						FlaxStableDiffusionPipeline.from_pretrained(
						    '''hf-internal-testing/tiny-stable-diffusion-pipe'''				,     safety_checker=lowerCamelCase__ )

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

						_a											=						jax.random.PRNGKey(0 )
						_a											=						4

						_a											=						jax.device_count()
						_a											=						num_samples * [prompt]
						_a											=						pipeline.prepare_inputs(lowerCamelCase__ )

						# shard inputs and rng
						_a											=						replicate(lowerCamelCase__ )
						_a											=						jax.random.split(lowerCamelCase__				,     lowerCamelCase__ )
						_a											=						shard(lowerCamelCase__ )

						_a											=						pipeline(lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     jit=lowerCamelCase__ ).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(lowerCamelCase__				,     dtype=np.floataa ).sum() - 49_947.875 ) < 5E-1

						_a											=						pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:] ) ) )
						assert len(lowerCamelCase__ ) == num_samples



	def       a__						(self )					->  Tuple:


						"""simple docstring"""



						_a											=						FlaxStableDiffusionPipeline.from_pretrained(
						    '''CompVis/stable-diffusion-v1-4'''				,     revision='''flax'''				,     safety_checker=lowerCamelCase__ )

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

						_a											=						jax.random.PRNGKey(0 )
						_a											=						50

						_a											=						jax.device_count()
						_a											=						num_samples * [prompt]
						_a											=						pipeline.prepare_inputs(lowerCamelCase__ )

						# shard inputs and rng
						_a											=						replicate(lowerCamelCase__ )
						_a											=						jax.random.split(lowerCamelCase__				,     lowerCamelCase__ )
						_a											=						shard(lowerCamelCase__ )

						_a											=						pipeline(lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     jit=lowerCamelCase__ ).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(lowerCamelCase__				,     dtype=np.floataa ).sum() - 2_383_808.2) ) < 5E-1



	def       a__						(self )					->  Union[str, Any]:


						"""simple docstring"""



						_a											=						FlaxStableDiffusionPipeline.from_pretrained(
						    '''CompVis/stable-diffusion-v1-4'''				,     revision='''bf16'''				,     dtype=jnp.bfloataa				,     safety_checker=lowerCamelCase__ )

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

						_a											=						jax.random.PRNGKey(0 )
						_a											=						50

						_a											=						jax.device_count()
						_a											=						num_samples * [prompt]
						_a											=						pipeline.prepare_inputs(lowerCamelCase__ )

						# shard inputs and rng
						_a											=						replicate(lowerCamelCase__ )
						_a											=						jax.random.split(lowerCamelCase__				,     lowerCamelCase__ )
						_a											=						shard(lowerCamelCase__ )

						_a											=						pipeline(lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     jit=lowerCamelCase__ ).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(lowerCamelCase__				,     dtype=np.floataa ).sum() - 2_373_516.75) ) < 5E-1



	def       a__						(self )					->  int:


						"""simple docstring"""



						_a											=						FlaxStableDiffusionPipeline.from_pretrained(
						    '''CompVis/stable-diffusion-v1-4'''				,     revision='''bf16'''				,     dtype=jnp.bfloataa )

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

						_a											=						jax.random.PRNGKey(0 )
						_a											=						50

						_a											=						jax.device_count()
						_a											=						num_samples * [prompt]
						_a											=						pipeline.prepare_inputs(lowerCamelCase__ )

						# shard inputs and rng
						_a											=						replicate(lowerCamelCase__ )
						_a											=						jax.random.split(lowerCamelCase__				,     lowerCamelCase__ )
						_a											=						shard(lowerCamelCase__ )

						_a											=						pipeline(lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     jit=lowerCamelCase__ ).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(lowerCamelCase__				,     dtype=np.floataa ).sum() - 2_373_516.75) ) < 5E-1



	def       a__						(self )					->  List[Any]:


						"""simple docstring"""



						_a											=						FlaxDDIMScheduler(
						    beta_start=0.00085				,     beta_end=0.012				,     beta_schedule='''scaled_linear'''				,     set_alpha_to_one=lowerCamelCase__				,     steps_offset=1				,     )

						_a											=						FlaxStableDiffusionPipeline.from_pretrained(
						    '''CompVis/stable-diffusion-v1-4'''				,     revision='''bf16'''				,     dtype=jnp.bfloataa				,     scheduler=lowerCamelCase__				,     safety_checker=lowerCamelCase__				,     )
						_a											=						scheduler.create_state()

						_a											=						scheduler_state

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

						_a											=						jax.random.PRNGKey(0 )
						_a											=						50

						_a											=						jax.device_count()
						_a											=						num_samples * [prompt]
						_a											=						pipeline.prepare_inputs(lowerCamelCase__ )

						# shard inputs and rng
						_a											=						replicate(lowerCamelCase__ )
						_a											=						jax.random.split(lowerCamelCase__				,     lowerCamelCase__ )
						_a											=						shard(lowerCamelCase__ )

						_a											=						pipeline(lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     jit=lowerCamelCase__ ).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(lowerCamelCase__				,     dtype=np.floataa ).sum() - 2_347_693.5) ) < 5E-1



	def       a__						(self )					->  Union[str, Any]:


						"""simple docstring"""



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

						_a											=						jax.device_count()
						_a											=						num_samples * [prompt]
						_a											=						jax.random.split(jax.random.PRNGKey(0 )				,     lowerCamelCase__ )

						_a											=						FlaxStableDiffusionPipeline.from_pretrained(
						    '''CompVis/stable-diffusion-v1-4'''				,     revision='''bf16'''				,     dtype=jnp.bfloataa				,     safety_checker=lowerCamelCase__				,     )

						_a											=						replicate(lowerCamelCase__ )
						_a											=						pipeline.prepare_inputs(lowerCamelCase__ )
						_a											=						shard(lowerCamelCase__ )
						_a											=						pipeline(lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     jit=lowerCamelCase__ ).images
						assert images.shape == (num_samples, 1, 512, 512, 3)
						_a											=						images[2, 0, 256, 10:17, 1]

						# With memory efficient attention
						_a											=						FlaxStableDiffusionPipeline.from_pretrained(
						    '''CompVis/stable-diffusion-v1-4'''				,     revision='''bf16'''				,     dtype=jnp.bfloataa				,     safety_checker=lowerCamelCase__				,     use_memory_efficient_attention=lowerCamelCase__				,     )

						_a											=						replicate(lowerCamelCase__ )
						_a											=						pipeline.prepare_inputs(lowerCamelCase__ )
						_a											=						shard(lowerCamelCase__ )
						_a											=						pipeline(lowerCamelCase__				,     lowerCamelCase__				,     lowerCamelCase__				,     jit=lowerCamelCase__ ).images
						assert images_eff.shape == (num_samples, 1, 512, 512, 3)
						_a											=						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








'''simple docstring'''





def lowerCAmelCase						(__A = 1_000):



     """simple docstring"""



     _a											=						2**power
     _a											=						str(__A)
     _a											=						list(__A)
     _a											=						0

     for i in list_num:
          sum_of_num += int(__A)

     return sum_of_num


if __name__ == "__main__":
  lowercase_	 =						int(input("Enter the power of 2: ").strip())
  print("2 ^ ", power, " = ", 2**power)
  lowercase_	 =						solution(power)
  print("Sum of the digits is: ", result)



