


"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase       :	Any  		=						logging.get_logger(__name__)

UpperCAmelCase       :	str  		=						{}





class 	lowerCamelCase__  (	__UpperCAmelCase							):







					"""simple docstring"""


					__a      			=				'llama'
					__a      			=				['past_key_values']






					def __init__(			self       :					Dict					,			UpperCamelCase       :					int=32_000					,			UpperCamelCase       :					Union[str, Any]=4_096					,			UpperCamelCase       :					int=11_008					,			UpperCamelCase       :					Optional[int]=32					,			UpperCamelCase       :					List[str]=32					,			UpperCamelCase       :					str=None					,			UpperCamelCase       :					int="silu"					,			UpperCamelCase       :					Dict=2_048					,			UpperCamelCase       :					Union[str, Any]=0.02					,			UpperCamelCase       :					Union[str, Any]=1e-6					,			UpperCamelCase       :					Dict=True					,			UpperCamelCase       :					List[str]=0					,			UpperCamelCase       :					List[str]=1					,			UpperCamelCase       :					Optional[int]=2					,			UpperCamelCase       :					str=1					,			UpperCamelCase       :					List[str]=False					,			UpperCamelCase       :					Dict=None					,			**UpperCamelCase       :					Union[str, Any]					,			):
												'''simple docstring'''






												__UpperCAmelCase  :		List[Any] 				=   vocab_size
												__UpperCAmelCase  :		Tuple 				=   max_position_embeddings
												__UpperCAmelCase  :		Tuple 				=   hidden_size
												__UpperCAmelCase  :		Dict 				=   intermediate_size
												__UpperCAmelCase  :		List[str] 				=   num_hidden_layers
												__UpperCAmelCase  :		int 				=   num_attention_heads

												# for backward compatibility
												if num_key_value_heads is None:
																			__UpperCAmelCase  :		str 				=   num_attention_heads

												__UpperCAmelCase  :		List[Any] 				=   num_key_value_heads
												__UpperCAmelCase  :		Optional[Any] 				=   hidden_act
												__UpperCAmelCase  :		int 				=   initializer_range
												__UpperCAmelCase  :		Tuple 				=   rms_norm_eps
												__UpperCAmelCase  :		str 				=   pretraining_tp
												__UpperCAmelCase  :		List[Any] 				=   use_cache
												__UpperCAmelCase  :		Union[str, Any] 				=   rope_scaling
												self._rope_scaling_validation()

												super().__init__(
												    pad_token_id=lowerCAmelCase_					,			bos_token_id=lowerCAmelCase_					,			eos_token_id=lowerCAmelCase_					,			tie_word_embeddings=lowerCAmelCase_					,			**lowerCAmelCase_					,			)






					def lowerCamelCase__				(			self       :					List[Any]    ):
												'''simple docstring'''






												if self.rope_scaling is None:
																			return

												if not isinstance(self.rope_scaling					,			lowerCAmelCase_    ) or len(self.rope_scaling    ) != 2:
																			raise ValueError(
																			    """`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, """
																			    f'''got {self.rope_scaling}'''    )
												__UpperCAmelCase  :		Any 				=   self.rope_scaling.get("""type"""					,			lowerCAmelCase_    )
												__UpperCAmelCase  :		Union[str, Any] 				=   self.rope_scaling.get("""factor"""					,			lowerCAmelCase_    )
												if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
																			raise ValueError(
																			    f'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}'''    )
												if rope_scaling_factor is None or not isinstance(lowerCAmelCase_					,			lowerCAmelCase_    ) or rope_scaling_factor <= 1.0:
																			raise ValueError(f'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}'''    )








"""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 _lowerCAmelCase						(    unittest.TestCase       ):







						def 			A      (    self							)		->							Any:
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()







						def 			A      (    self							)		->							int:
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    FlaxStableDiffusionPipeline.from_pretrained(
										    'stabilityai/stable-diffusion-2'   ,				revision='bf16'   ,				dtype=jnp.bfloataa   ,				)

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    'A painting of a squirrel eating a burger'
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    jax.device_count()
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    num_samples * [prompt]
										_SCREAMING_SNAKE_CASE   :		Any       		=    sd_pipe.prepare_inputs(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    replicate(lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    shard(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Any       		=    jax.random.PRNGKey(0							)
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    jax.random.split(lowerCAmelCase_   ,				jax.device_count()							)

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    sd_pipe(lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				num_inference_steps=2_5   ,				jit=lowerCAmelCase_							)[0]
										assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

										_SCREAMING_SNAKE_CASE   :		int       		=    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

										_SCREAMING_SNAKE_CASE   :		Tuple       		=    jnp.asarray(jax.device_get(image_slice.flatten()							)							)
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512]							)
										print(F"""output_slice: {output_slice}"""							)
										assert jnp.abs(output_slice - expected_slice							).max() < 1e-2







						def 			A      (    self							)		->							Optional[int]:
										_SCREAMING_SNAKE_CASE   :		str       		=    'stabilityai/stable-diffusion-2'
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		int       		=    FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCAmelCase_   ,				subfolder='scheduler'							)
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		List[str]       		=    FlaxStableDiffusionPipeline.from_pretrained(
										    lowerCAmelCase_   ,				scheduler=lowerCAmelCase_   ,				revision='bf16'   ,				dtype=jnp.bfloataa   ,				)
										_SCREAMING_SNAKE_CASE   :		Any       		=    scheduler_params

										_SCREAMING_SNAKE_CASE   :		int       		=    'A painting of a squirrel eating a burger'
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    jax.device_count()
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    num_samples * [prompt]
										_SCREAMING_SNAKE_CASE   :		Any       		=    sd_pipe.prepare_inputs(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Dict       		=    replicate(lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Any       		=    shard(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Any       		=    jax.random.PRNGKey(0							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    jax.random.split(lowerCAmelCase_   ,				jax.device_count()							)

										_SCREAMING_SNAKE_CASE   :		int       		=    sd_pipe(lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				num_inference_steps=2_5   ,				jit=lowerCAmelCase_							)[0]
										assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

										_SCREAMING_SNAKE_CASE   :		str       		=    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]							)
										_SCREAMING_SNAKE_CASE   :		str       		=    images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

										_SCREAMING_SNAKE_CASE   :		int       		=    jnp.asarray(jax.device_get(image_slice.flatten()							)							)
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297]							)
										print(F"""output_slice: {output_slice}"""							)
										assert jnp.abs(output_slice - expected_slice							).max() < 1e-2

