

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


a_     :    List[str]     		=   logging.get_logger(__name__)




class   UpperCamelCase			(  SCREAMING_SNAKE_CASE ):
    __UpperCamelCase    =["pixel_values"]

    def __init__(      self			:   List[str]				,     snake_case__			:   bool = True				,     snake_case__			:   Optional[Dict[str, int]] = None				,     snake_case__			:   PILImageResampling = PILImageResampling.BILINEAR				,     snake_case__			:   bool = True				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   bool = True				,     snake_case__			:   Union[int, float] = 1 / 2_5_5				,     snake_case__			:   bool = True				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     **snake_case__			:   Optional[Any]				,     ):

     """simple docstring"""






     super().__init__(**snake_case__     )
     SCREAMING_SNAKE_CASE		    =      size if size is not None else {'shortest_edge': 2_5_6}
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     default_to_square=snake_case__     )
     SCREAMING_SNAKE_CASE		    =      crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      do_resize
     SCREAMING_SNAKE_CASE		    =      size
     SCREAMING_SNAKE_CASE		    =      resample
     SCREAMING_SNAKE_CASE		    =      do_center_crop
     SCREAMING_SNAKE_CASE		    =      crop_size
     SCREAMING_SNAKE_CASE		    =      do_rescale
     SCREAMING_SNAKE_CASE		    =      rescale_factor
     SCREAMING_SNAKE_CASE		    =      do_normalize
     SCREAMING_SNAKE_CASE		    =      image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
     SCREAMING_SNAKE_CASE		    =      image_std if image_std is not None else IMAGENET_STANDARD_STD

    def UpperCamelCase      (      self			:   Optional[int]				,     snake_case__			:   np.ndarray				,     snake_case__			:   Dict[str, int]				,     snake_case__			:   PILImageResampling = PILImageResampling.BICUBIC				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   Dict				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     default_to_square=snake_case__     )
     if "shortest_edge" not in size:
      raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""     )
     SCREAMING_SNAKE_CASE		    =      get_resize_output_image_size(snake_case__				,     size=size['shortest_edge']				,     default_to_square=snake_case__     )
     return resize(snake_case__				,     size=snake_case__				,     resample=snake_case__				,     data_format=snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   Any				,     snake_case__			:   np.ndarray				,     snake_case__			:   Dict[str, int]				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   Dict				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__     )
     return center_crop(snake_case__				,     size=(size['height'], size['width'])				,     data_format=snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   int				,     snake_case__			:   np.ndarray				,     snake_case__			:   float				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   List[Any]     ):

     """simple docstring"""






     return rescale(snake_case__				,     scale=snake_case__				,     data_format=snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   List[str]				,     snake_case__			:   np.ndarray				,     snake_case__			:   Union[float, List[float]]				,     snake_case__			:   Union[float, List[float]]				,     snake_case__			:   Optional[Union[str, ChannelDimension]] = None				,     **snake_case__			:   Tuple				,     ):

     """simple docstring"""






     return normalize(snake_case__				,     mean=snake_case__				,     std=snake_case__				,     data_format=snake_case__				,     **snake_case__     )




    def UpperCamelCase      (      self			:   Dict				,     snake_case__			:   ImageInput				,     snake_case__			:   Optional[bool] = None				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   PILImageResampling = None				,     snake_case__			:   bool = None				,     snake_case__			:   Dict[str, int] = None				,     snake_case__			:   Optional[bool] = None				,     snake_case__			:   Optional[float] = None				,     snake_case__			:   Optional[bool] = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[Union[float, List[float]]] = None				,     snake_case__			:   Optional[Union[str, TensorType]] = None				,     snake_case__			:   Union[str, ChannelDimension] = ChannelDimension.FIRST				,     **snake_case__			:   List[str]				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      do_resize if do_resize is not None else self.do_resize
     SCREAMING_SNAKE_CASE		    =      size if size is not None else self.size
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__				,     default_to_square=snake_case__     )
     SCREAMING_SNAKE_CASE		    =      resample if resample is not None else self.resample
     SCREAMING_SNAKE_CASE		    =      do_center_crop if do_center_crop is not None else self.do_center_crop
     SCREAMING_SNAKE_CASE		    =      crop_size if crop_size is not None else self.crop_size
     SCREAMING_SNAKE_CASE		    =      get_size_dict(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      do_rescale if do_rescale is not None else self.do_rescale
     SCREAMING_SNAKE_CASE		    =      rescale_factor if rescale_factor is not None else self.rescale_factor
     SCREAMING_SNAKE_CASE		    =      do_normalize if do_normalize is not None else self.do_normalize
     SCREAMING_SNAKE_CASE		    =      image_mean if image_mean is not None else self.image_mean
     SCREAMING_SNAKE_CASE		    =      image_std if image_std is not None else self.image_std

     SCREAMING_SNAKE_CASE		    =      make_list_of_images(snake_case__     )

     if not valid_images(snake_case__     ):
      raise ValueError(
          'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
          'torch.Tensor, tf.Tensor or jax.ndarray.'     )

     if do_resize and size is None:
      raise ValueError('Size must be specified if do_resize is True.'     )

     if do_center_crop and crop_size is None:
      raise ValueError('Crop size must be specified if do_center_crop is True.'     )

     if do_rescale and rescale_factor is None:
      raise ValueError('Rescale factor must be specified if do_rescale is True.'     )

     if do_normalize and (image_mean is None or image_std is None):
      raise ValueError('Image mean and std must be specified if do_normalize is True.'     )

     # All transformations expect numpy arrays.
     SCREAMING_SNAKE_CASE		    =      [to_numpy_array(snake_case__     ) for image in images]

     if do_resize:
      SCREAMING_SNAKE_CASE		    =      [self.resize(image=snake_case__				,     size=snake_case__				,     resample=snake_case__     ) for image in images]

     if do_center_crop:
      SCREAMING_SNAKE_CASE		    =      [self.center_crop(image=snake_case__				,     size=snake_case__     ) for image in images]

     if do_rescale:
      SCREAMING_SNAKE_CASE		    =      [self.rescale(image=snake_case__				,     scale=snake_case__     ) for image in images]

     if do_normalize:
      SCREAMING_SNAKE_CASE		    =      [self.normalize(image=snake_case__				,     mean=snake_case__				,     std=snake_case__     ) for image in images]

     SCREAMING_SNAKE_CASE		    =      [to_channel_dimension_format(snake_case__				,     snake_case__     ) for image in images]

     SCREAMING_SNAKE_CASE		    =      {'pixel_values': images}
     return BatchFeature(data=snake_case__				,     tensor_type=snake_case__     )




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

    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






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

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

    def UpperCamelCase      (      self			:   Optional[Any]     ):

     """simple docstring"""






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

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

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

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 6_4, 6_4, 3)
     if jax.device_count() == 8:
      assert np.abs(np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 4.1_514_745     ) < 1E-3
      assert np.abs(np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 49_947.875     ) < 5E-1

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

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

     """simple docstring"""






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

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

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.05_652_401)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_383_808.2)     ) < 5E-1

    def UpperCamelCase      (      self			:   Optional[Any]     ):

     """simple docstring"""






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

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

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.04_003_906)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_373_516.75)     ) < 5E-1

    def UpperCamelCase      (      self			:   List[Any]     ):

     """simple docstring"""






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

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

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.04_003_906)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_373_516.75)     ) < 5E-1

    def UpperCamelCase      (      self			:   Dict     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      FlaxDDIMScheduler(
         beta_start=0.00_085				,     beta_end=0.012				,     beta_schedule='scaled_linear'				,     set_alpha_to_one=snake_case__				,     steps_offset=1				,     )

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

     SCREAMING_SNAKE_CASE		    =      scheduler_state

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

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.045_043_945)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_347_693.5)     ) < 5E-1




    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






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

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

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

     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images
     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     SCREAMING_SNAKE_CASE		    =      images[2, 0, 2_5_6, 1_0:1_7, 1]

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

     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images
     assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     SCREAMING_SNAKE_CASE		    =      images[2, 0, 2_5_6, 1_0:1_7, 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

