



'''simple docstring'''
import gc
import unittest

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


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






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





	def 	UpperCamelCase_  ( self					:				Union[str, Any] ):







							'''simple docstring'''
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()





	def 	UpperCamelCase_  ( self					:				List[str] ):







							'''simple docstring'''
							_UpperCamelCase				,			_UpperCamelCase					:		Union[str, Any] 				=   FlaxStableDiffusionPipeline.from_pretrained(
							    'stabilityai/stable-diffusion-2' ,revision='bf16' ,dtype=jnp.bfloataa ,)

							_UpperCamelCase					:		Dict 				=   'A painting of a squirrel eating a burger'
							_UpperCamelCase					:		Any 				=   jax.device_count()
							_UpperCamelCase					:		Any 				=   num_samples * [prompt]
							_UpperCamelCase					:		Tuple 				=   sd_pipe.prepare_inputs(lowerCamelCase__ )

							_UpperCamelCase					:		List[Any] 				=   replicate(lowerCamelCase__ )
							_UpperCamelCase					:		Optional[int] 				=   shard(lowerCamelCase__ )

							_UpperCamelCase					:		str 				=   jax.random.PRNGKey(0 )
							_UpperCamelCase					:		int 				=   jax.random.split(lowerCamelCase__ ,jax.device_count() )

							_UpperCamelCase					:		List[Any] 				=   sd_pipe(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,num_inference_steps=25 ,jit=lowerCamelCase__ )[0]
							assert images.shape == (jax.device_count(), 1, 768, 768, 3)

							_UpperCamelCase					:		int 				=   images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
							_UpperCamelCase					:		List[str] 				=   images[0, 253:256, 253:256, -1]

							_UpperCamelCase					:		Optional[Any] 				=   jnp.asarray(jax.device_get(image_slice.flatten() ) )
							_UpperCamelCase					:		Dict 				=   jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2] )
							print(F'output_slice: {output_slice}' )
							assert jnp.abs(output_slice - expected_slice ).max() < 1E-2





	def 	UpperCamelCase_  ( self					:				List[str] ):







							'''simple docstring'''
							_UpperCamelCase					:		Optional[int] 				=   'stabilityai/stable-diffusion-2'
							_UpperCamelCase				,			_UpperCamelCase					:		Tuple 				=   FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCamelCase__ ,subfolder='scheduler' )
							_UpperCamelCase				,			_UpperCamelCase					:		Optional[int] 				=   FlaxStableDiffusionPipeline.from_pretrained(
							    lowerCamelCase__ ,scheduler=lowerCamelCase__ ,revision='bf16' ,dtype=jnp.bfloataa ,)
							_UpperCamelCase					:		Tuple 				=   scheduler_params

							_UpperCamelCase					:		str 				=   'A painting of a squirrel eating a burger'
							_UpperCamelCase					:		Optional[int] 				=   jax.device_count()
							_UpperCamelCase					:		Optional[int] 				=   num_samples * [prompt]
							_UpperCamelCase					:		Dict 				=   sd_pipe.prepare_inputs(lowerCamelCase__ )

							_UpperCamelCase					:		Dict 				=   replicate(lowerCamelCase__ )
							_UpperCamelCase					:		Dict 				=   shard(lowerCamelCase__ )

							_UpperCamelCase					:		Tuple 				=   jax.random.PRNGKey(0 )
							_UpperCamelCase					:		int 				=   jax.random.split(lowerCamelCase__ ,jax.device_count() )

							_UpperCamelCase					:		Optional[Any] 				=   sd_pipe(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,num_inference_steps=25 ,jit=lowerCamelCase__ )[0]
							assert images.shape == (jax.device_count(), 1, 768, 768, 3)

							_UpperCamelCase					:		List[Any] 				=   images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
							_UpperCamelCase					:		Any 				=   images[0, 253:256, 253:256, -1]

							_UpperCamelCase					:		Dict 				=   jnp.asarray(jax.device_get(image_slice.flatten() ) )
							_UpperCamelCase					:		List[str] 				=   jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7] )
							print(F'output_slice: {output_slice}' )
							assert jnp.abs(output_slice - expected_slice ).max() < 1E-2










"""simple docstring"""
def 				snake_case      (   A__	,A__	,A__	,A__	,A__						):
			if index == number_of_items:
						return 0
			UpperCAmelCase_     : str			     =					0
			UpperCAmelCase_     : Dict			     =					0
			UpperCAmelCase_     : Tuple			     =					knapsack(A__	,A__	,A__	,A__	,index + 1						)
			if weights[index] <= max_weight:
						UpperCAmelCase_     : Union[str, Any]			     =					values[index] + knapsack(
						    A__	,A__	,A__	,max_weight - weights[index]	,index + 1						)
			return max(A__	,A__						)


if __name__ == "__main__":
			import doctest

			doctest.testmod()



