




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



				def       SCREAMING_SNAKE_CASE       (      self      :		Tuple      )  ->    Tuple:





								'''simple docstring'''




								super().tearDown()
								gc.collect()



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase					,    _lowerCAmelCase    :    Dict										=      FlaxStableDiffusionPipeline.from_pretrained(
								    """stabilityai/stable-diffusion-2"""		,	revision="""bf16"""		,	dtype=jnp.bfloataa		,	)

								_lowerCAmelCase    :    List[str]										=      """A painting of a squirrel eating a burger"""
								_lowerCAmelCase    :    List[str]										=      jax.device_count()
								_lowerCAmelCase    :    Dict										=      num_samples * [prompt]
								_lowerCAmelCase    :    List[Any]										=      sd_pipe.prepare_inputs(_UpperCAmelCase      )

								_lowerCAmelCase    :    int										=      replicate(_UpperCAmelCase      )
								_lowerCAmelCase    :    List[str]										=      shard(_UpperCAmelCase      )

								_lowerCAmelCase    :    str										=      jax.random.PRNGKey(0      )
								_lowerCAmelCase    :    List[Any]										=      jax.random.split(_UpperCAmelCase		,	jax.device_count()      )

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

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

								_lowerCAmelCase    :    int										=      jnp.asarray(jax.device_get(image_slice.flatten()      )      )
								_lowerCAmelCase    :    Optional[int]										=      jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.45508, 0.4512]      )
								print(f"output_slice: {output_slice}"      )
								assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2



				def       SCREAMING_SNAKE_CASE       (      self      :		List[Any]      )  ->    Optional[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      """stabilityai/stable-diffusion-2"""
								_lowerCAmelCase					,    _lowerCAmelCase    :    List[Any]										=      FlaxDPMSolverMultistepScheduler.from_pretrained(_UpperCAmelCase		,	subfolder="""scheduler"""      )
								_lowerCAmelCase					,    _lowerCAmelCase    :    Tuple										=      FlaxStableDiffusionPipeline.from_pretrained(
								    _UpperCAmelCase		,	scheduler=_UpperCAmelCase		,	revision="""bf16"""		,	dtype=jnp.bfloataa		,	)
								_lowerCAmelCase    :    Tuple										=      scheduler_params

								_lowerCAmelCase    :    Tuple										=      """A painting of a squirrel eating a burger"""
								_lowerCAmelCase    :    Optional[int]										=      jax.device_count()
								_lowerCAmelCase    :    int										=      num_samples * [prompt]
								_lowerCAmelCase    :    Any										=      sd_pipe.prepare_inputs(_UpperCAmelCase      )

								_lowerCAmelCase    :    List[str]										=      replicate(_UpperCAmelCase      )
								_lowerCAmelCase    :    Tuple										=      shard(_UpperCAmelCase      )

								_lowerCAmelCase    :    Union[str, Any]										=      jax.random.PRNGKey(0      )
								_lowerCAmelCase    :    int										=      jax.random.split(_UpperCAmelCase		,	jax.device_count()      )

								_lowerCAmelCase    :    Tuple										=      sd_pipe(_UpperCAmelCase		,	_UpperCAmelCase		,	_UpperCAmelCase		,	num_inference_steps=25		,	jit=_UpperCAmelCase      )[0]
								assert images.shape == (jax.device_count(), 1, 768, 768, 3)

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

								_lowerCAmelCase    :    Optional[Any]										=      jnp.asarray(jax.device_get(image_slice.flatten()      )      )
								_lowerCAmelCase    :    Union[str, Any]										=      jnp.array([0.4336, 0.42969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297]      )
								print(f"output_slice: {output_slice}"      )
								assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2







from __future__ import annotations

from collections.abc import Iterator
from typing import Any
class        __snake_case  :



				def __init__(      self      :		List[Any]		,	_UpperCAmelCase      :		Any      )  ->    Dict:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      data
								_lowerCAmelCase    :    Node | None										=      None






class        __snake_case  :



				def __init__(      self      :		Tuple      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase    :    str										=      None
								_lowerCAmelCase    :    List[Any]										=      None



				def __iter__(      self      :		str      )  ->    Iterator[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[int]										=      self.head
								while self.head:
												yield node.data
												_lowerCAmelCase    :    List[str]										=      node.next
												if node == self.head:
																break



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





								'''simple docstring'''




								return sum(1 for _ in self      )



				def __repr__(      self      :		Optional[int]      )  ->    str:





								'''simple docstring'''




								return "->".join(str(_UpperCAmelCase      ) for item in iter(self      )      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Any		,	_UpperCAmelCase      :		Any      )  ->    None:





								'''simple docstring'''




								self.insert_nth(len(self      )		,	_UpperCAmelCase      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict		,	_UpperCAmelCase      :		Any      )  ->    None:





								'''simple docstring'''




								self.insert_nth(0		,	_UpperCAmelCase      )



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		Any      )  ->    None:





								'''simple docstring'''




								if index < 0 or index > len(self      ):
												raise IndexError("""list index out of range."""      )
								_lowerCAmelCase    :    Optional[int]										=      Node(_UpperCAmelCase      )
								if self.head is None:
												_lowerCAmelCase    :    Tuple										=      new_node  # first node points itself
												_lowerCAmelCase    :    int										=      new_node
								elif index == 0:  # insert at head
												_lowerCAmelCase    :    Tuple										=      self.head
												_lowerCAmelCase    :    int										=      new_node
								else:
												_lowerCAmelCase    :    Union[str, Any]										=      self.head
												for _ in range(index - 1      ):
																_lowerCAmelCase    :    Optional[Any]										=      temp.next
												_lowerCAmelCase    :    Optional[int]										=      temp.next
												_lowerCAmelCase    :    Optional[int]										=      new_node
												if index == len(self      ) - 1:  # insert at tail
																_lowerCAmelCase    :    Optional[Any]										=      new_node



				def       SCREAMING_SNAKE_CASE       (      self      :		Dict      )  ->    str:





								'''simple docstring'''




								return self.delete_nth(0      )



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    Any:





								'''simple docstring'''




								return self.delete_nth(len(self      ) - 1      )



				def       SCREAMING_SNAKE_CASE       (      self      :		str		,	_UpperCAmelCase      :		int = 0      )  ->    Any:





								'''simple docstring'''




								if not 0 <= index < len(self      ):
												raise IndexError("""list index out of range."""      )
								_lowerCAmelCase    :    Optional[Any]										=      self.head
								if self.head == self.tail:  # just one node
												_lowerCAmelCase    :    Optional[Any]										=      None
								elif index == 0:  # delete head node
												_lowerCAmelCase    :    str										=      self.tail.next.next
												_lowerCAmelCase    :    List[str]										=      self.head.next
								else:
												_lowerCAmelCase    :    List[str]										=      self.head
												for _ in range(index - 1      ):
																_lowerCAmelCase    :    List[Any]										=      temp.next
												_lowerCAmelCase    :    List[str]										=      temp.next
												_lowerCAmelCase    :    Dict										=      temp.next.next
												if index == len(self      ) - 1:  # delete at tail
																_lowerCAmelCase    :    Optional[int]										=      temp
								return delete_node.data



				def       SCREAMING_SNAKE_CASE       (      self      :		int      )  ->    bool:





								'''simple docstring'''




								return len(self      ) == 0






def 							_UpperCAmelCase  ():
				'''simple docstring'''
				_lowerCAmelCase    :    Dict										=      CircularLinkedList()
				assert len(UpperCamelCase_       ) == 0
				assert circular_linked_list.is_empty() is True
				assert str(UpperCamelCase_       ) == ""

				try:
								circular_linked_list.delete_front()
								raise AssertionError  # This should not happen
				except IndexError:
								assert True  # This should happen

				try:
								circular_linked_list.delete_tail()
								raise AssertionError  # This should not happen
				except IndexError:
								assert True  # This should happen

				try:
								circular_linked_list.delete_nth(-1       )
								raise AssertionError
				except IndexError:
								assert True

				try:
								circular_linked_list.delete_nth(0       )
								raise AssertionError
				except IndexError:
								assert True

				assert circular_linked_list.is_empty() is True
				for i in range(5       ):
								assert len(UpperCamelCase_       ) == i
								circular_linked_list.insert_nth(UpperCamelCase_      ,    i + 1       )
				assert str(UpperCamelCase_       ) == "->".join(str(UpperCamelCase_       ) for i in range(1      ,    6       )       )

				circular_linked_list.insert_tail(6       )
				assert str(UpperCamelCase_       ) == "->".join(str(UpperCamelCase_       ) for i in range(1      ,    7       )       )
				circular_linked_list.insert_head(0       )
				assert str(UpperCamelCase_       ) == "->".join(str(UpperCamelCase_       ) for i in range(0      ,    7       )       )

				assert circular_linked_list.delete_front() == 0
				assert circular_linked_list.delete_tail() == 6
				assert str(UpperCamelCase_       ) == "->".join(str(UpperCamelCase_       ) for i in range(1      ,    6       )       )
				assert circular_linked_list.delete_nth(2       ) == 3

				circular_linked_list.insert_nth(2      ,    3       )
				assert str(UpperCamelCase_       ) == "->".join(str(UpperCamelCase_       ) for i in range(1      ,    6       )       )

				assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
	import doctest

	doctest.testmod()

