



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




      """simple docstring"""




      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[int]	):
        super().tearDown()
        gc.collect()



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        lowerCAmelCase_       :    int								=			FlaxStableDiffusionPipeline.from_pretrained(
            'stabilityai/stable-diffusion-2'   ,      revision='bf16'   ,      dtype=jnp.bfloataa   ,      )

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

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

        lowerCAmelCase_       :    int								=			jax.random.PRNGKey(0	)
        lowerCAmelCase_       :    Tuple								=			jax.random.split(_SCREAMING_SNAKE_CASE   ,      jax.device_count()	)

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

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

        lowerCAmelCase_       :    Any								=			jnp.asarray(jax.device_get(image_slice.flatten()	)	)
        lowerCAmelCase_       :    str								=			jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12]	)
        print(F"output_slice: {output_slice}"	)
        assert jnp.abs(output_slice - expected_slice	).max() < 1E-2





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

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

        lowerCAmelCase_       :    Optional[int]								=			replicate(_SCREAMING_SNAKE_CASE	)
        lowerCAmelCase_       :    Dict								=			shard(_SCREAMING_SNAKE_CASE	)

        lowerCAmelCase_       :    int								=			jax.random.PRNGKey(0	)
        lowerCAmelCase_       :    Tuple								=			jax.random.split(_SCREAMING_SNAKE_CASE   ,      jax.device_count()	)

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

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

        lowerCAmelCase_       :    Any								=			jnp.asarray(jax.device_get(image_slice.flatten()	)	)
        lowerCAmelCase_       :    List[Any]								=			jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97]	)
        print(F"output_slice: {output_slice}"	)
        assert jnp.abs(output_slice - expected_slice	).max() < 1E-2





"""simple docstring"""







def     UpperCamelCase_   (   lowerCAmelCase__				:    Optional[Any]   ,       lowerCAmelCase__				:    str						)						->       List[Any]:

  """simple docstring"""




  return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2

def     UpperCamelCase_   (   lowerCAmelCase__				:    List[Any]   ,       lowerCAmelCase__				:    str=0						)						->       Union[str, Any]:

  """simple docstring"""




  return sorted(lowerCAmelCase__   ,       key=lambda lowerCAmelCase__						: x[column]						)

def     UpperCamelCase_   (   lowerCAmelCase__				:    Dict   ,       lowerCAmelCase__				:    Optional[int]   ,       lowerCAmelCase__				:    Optional[Any]=float('inf'						)						)						->       Optional[int]:

  """simple docstring"""




  for i in range(points_counts - 1						):
    for j in range(i + 1   ,       lowerCAmelCase__						):
      lowerCAmelCase_       :    Union[str, Any]								=			euclidean_distance_sqr(points[i]   ,       points[j]						)
      if current_dis < min_dis:
        lowerCAmelCase_       :    Optional[int]								=			current_dis
  return min_dis

def     UpperCamelCase_   (   lowerCAmelCase__				:    List[Any]   ,       lowerCAmelCase__				:    Dict   ,       lowerCAmelCase__				:    Union[str, Any]=float('inf'						)						)						->       Dict:

  """simple docstring"""




  for i in range(min(6   ,       points_counts - 1						)   ,       lowerCAmelCase__						):
    for j in range(max(0   ,       i - 6						)   ,       lowerCAmelCase__						):
      lowerCAmelCase_       :    Any								=			euclidean_distance_sqr(points[i]   ,       points[j]						)
      if current_dis < min_dis:
        lowerCAmelCase_       :    Union[str, Any]								=			current_dis
  return min_dis

def     UpperCamelCase_   (   lowerCAmelCase__				:    Dict   ,       lowerCAmelCase__				:    int   ,       lowerCAmelCase__				:    Optional[int]						)						->       Dict:

  """simple docstring"""




  if points_counts <= 3:
    return dis_between_closest_pair(lowerCAmelCase__   ,       lowerCAmelCase__						)

  # recursion
  lowerCAmelCase_       :    int								=			points_counts // 2
  lowerCAmelCase_       :    Optional[Any]								=			closest_pair_of_points_sqr(
      lowerCAmelCase__   ,       points_sorted_on_y[:mid]   ,       lowerCAmelCase__						)
  lowerCAmelCase_       :    Optional[Any]								=			closest_pair_of_points_sqr(
      lowerCAmelCase__   ,       points_sorted_on_y[mid:]   ,       points_counts - mid						)
  lowerCAmelCase_       :    Any								=			min(lowerCAmelCase__   ,       lowerCAmelCase__						)
  lowerCAmelCase_       :    str								=			[]
  for point in points_sorted_on_x:
    if abs(point[0] - points_sorted_on_x[mid][0]						) < closest_pair_dis:
      cross_strip.append(lowerCAmelCase__						)

  lowerCAmelCase_       :    List[Any]								=			dis_between_closest_in_strip(
      lowerCAmelCase__   ,       len(lowerCAmelCase__						)   ,       lowerCAmelCase__						)
  return min(lowerCAmelCase__   ,       lowerCAmelCase__						)

def     UpperCamelCase_   (   lowerCAmelCase__				:    str   ,       lowerCAmelCase__				:    Tuple						)						->       List[Any]:

  """simple docstring"""




  lowerCAmelCase_       :    List[str]								=			column_based_sort(lowerCAmelCase__   ,       column=0						)
  lowerCAmelCase_       :    Dict								=			column_based_sort(lowerCAmelCase__   ,       column=1						)
  return (
      closest_pair_of_points_sqr(
          lowerCAmelCase__   ,       lowerCAmelCase__   ,       lowerCAmelCase__						)
  ) ** 0.5


if __name__ == "__main__":
     lowercase__		:    List[str]				   =     [(2, 3), (1_2, 3_0), (4_0, 5_0), (5, 1), (1_2, 1_0), (3, 4)]
     print("""Distance:""", closest_pair_of_points(points, len(points)))





