

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


    '''simple docstring'''






    def    lowerCAmelCase_  (self     )   ->					List[Any]:
        with tempfile.TemporaryDirectory() as tmpdirname:
            # pipeline has Flax weights
            __UpperCAmelCase     =	FlaxDiffusionPipeline.from_pretrained(
                '''hf-internal-testing/tiny-stable-diffusion-pipe'''   ,					safety_checker=UpperCamelCase_   ,					cache_dir=UpperCamelCase_     )

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


    '''simple docstring'''






    def    lowerCAmelCase_  (self     )   ->					Union[str, Any]:
        __UpperCAmelCase     =	FlaxStableDiffusionPipeline.from_pretrained(
            '''hf-internal-testing/tiny-stable-diffusion-pipe'''   ,					safety_checker=UpperCamelCase_     )

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

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

        __UpperCAmelCase     =	jax.device_count()
        __UpperCAmelCase     =	num_samples * [prompt]
        __UpperCAmelCase     =	pipeline.prepare_inputs(UpperCamelCase_     )

        # shard inputs and rng
        __UpperCAmelCase     =	replicate(UpperCamelCase_     )
        __UpperCAmelCase     =	jax.random.split(UpperCamelCase_   ,					UpperCamelCase_     )
        __UpperCAmelCase     =	shard(UpperCamelCase_     )

        __UpperCAmelCase     =	pipeline(UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					jit=UpperCamelCase_     ).images

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

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


    def    lowerCAmelCase_  (self     )   ->					Optional[int]:
        __UpperCAmelCase     =	FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,					revision='''flax'''   ,					safety_checker=UpperCamelCase_     )

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

        __UpperCAmelCase     =	jax.random.PRNGKey(0     )
        __UpperCAmelCase     =	50

        __UpperCAmelCase     =	jax.device_count()
        __UpperCAmelCase     =	num_samples * [prompt]
        __UpperCAmelCase     =	pipeline.prepare_inputs(UpperCamelCase_     )

        # shard inputs and rng
        __UpperCAmelCase     =	replicate(UpperCamelCase_     )
        __UpperCAmelCase     =	jax.random.split(UpperCamelCase_   ,					UpperCamelCase_     )
        __UpperCAmelCase     =	shard(UpperCamelCase_     )

        __UpperCAmelCase     =	pipeline(UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					jit=UpperCamelCase_     ).images

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


    def    lowerCAmelCase_  (self     )   ->					Any:
        __UpperCAmelCase     =	FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,					revision='''bf16'''   ,					dtype=jnp.bfloataa   ,					safety_checker=UpperCamelCase_     )

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

        __UpperCAmelCase     =	jax.random.PRNGKey(0     )
        __UpperCAmelCase     =	50

        __UpperCAmelCase     =	jax.device_count()
        __UpperCAmelCase     =	num_samples * [prompt]
        __UpperCAmelCase     =	pipeline.prepare_inputs(UpperCamelCase_     )

        # shard inputs and rng
        __UpperCAmelCase     =	replicate(UpperCamelCase_     )
        __UpperCAmelCase     =	jax.random.split(UpperCamelCase_   ,					UpperCamelCase_     )
        __UpperCAmelCase     =	shard(UpperCamelCase_     )

        __UpperCAmelCase     =	pipeline(UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					jit=UpperCamelCase_     ).images

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


    def    lowerCAmelCase_  (self     )   ->					Dict:
        __UpperCAmelCase     =	FlaxStableDiffusionPipeline.from_pretrained(
            '''CompVis/stable-diffusion-v1-4'''   ,					revision='''bf16'''   ,					dtype=jnp.bfloataa     )

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

        __UpperCAmelCase     =	jax.random.PRNGKey(0     )
        __UpperCAmelCase     =	50

        __UpperCAmelCase     =	jax.device_count()
        __UpperCAmelCase     =	num_samples * [prompt]
        __UpperCAmelCase     =	pipeline.prepare_inputs(UpperCamelCase_     )

        # shard inputs and rng
        __UpperCAmelCase     =	replicate(UpperCamelCase_     )
        __UpperCAmelCase     =	jax.random.split(UpperCamelCase_   ,					UpperCamelCase_     )
        __UpperCAmelCase     =	shard(UpperCamelCase_     )

        __UpperCAmelCase     =	pipeline(UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					jit=UpperCamelCase_     ).images

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


    def    lowerCAmelCase_  (self     )   ->					List[str]:
        __UpperCAmelCase     =	FlaxDDIMScheduler(
            beta_start=0.00085   ,					beta_end=0.012   ,					beta_schedule='''scaled_linear'''   ,					set_alpha_to_one=UpperCamelCase_   ,					steps_offset=1   ,					)

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

        __UpperCAmelCase     =	scheduler_state

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

        __UpperCAmelCase     =	jax.random.PRNGKey(0     )
        __UpperCAmelCase     =	50

        __UpperCAmelCase     =	jax.device_count()
        __UpperCAmelCase     =	num_samples * [prompt]
        __UpperCAmelCase     =	pipeline.prepare_inputs(UpperCamelCase_     )

        # shard inputs and rng
        __UpperCAmelCase     =	replicate(UpperCamelCase_     )
        __UpperCAmelCase     =	jax.random.split(UpperCamelCase_   ,					UpperCamelCase_     )
        __UpperCAmelCase     =	shard(UpperCamelCase_     )

        __UpperCAmelCase     =	pipeline(UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					jit=UpperCamelCase_     ).images

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






    def    lowerCAmelCase_  (self     )   ->					str:
        __UpperCAmelCase     =	(
            '''A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'''
            ''' field, close up, split lighting, cinematic'''
        )

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

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

        __UpperCAmelCase     =	replicate(UpperCamelCase_     )
        __UpperCAmelCase     =	pipeline.prepare_inputs(UpperCamelCase_     )
        __UpperCAmelCase     =	shard(UpperCamelCase_     )
        __UpperCAmelCase     =	pipeline(UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					jit=UpperCamelCase_     ).images
        assert images.shape == (num_samples, 1, 512, 512, 3)
        __UpperCAmelCase     =	images[2, 0, 256, 10:17, 1]

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

        __UpperCAmelCase     =	replicate(UpperCamelCase_     )
        __UpperCAmelCase     =	pipeline.prepare_inputs(UpperCamelCase_     )
        __UpperCAmelCase     =	shard(UpperCamelCase_     )
        __UpperCAmelCase     =	pipeline(UpperCamelCase_   ,					UpperCamelCase_   ,					UpperCamelCase_   ,					jit=UpperCamelCase_     ).images
        assert images_eff.shape == (num_samples, 1, 512, 512, 3)
        __UpperCAmelCase     =	images[2, 0, 256, 10:17, 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








"""simple docstring"""





import unittest
from typing import Dict, List, Optional, Union

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import BridgeTowerImageProcessor



class 						snake_case_(				unittest.TestCase	):






     def __init__(		self     :    Union[str, Any]    , UpperCamelCase_     :    Optional[Any]    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Dict[str, int] = None    , UpperCamelCase_     :    int = 3_2    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Union[int, float] = 1 / 2_5_5    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Optional[Union[float, List[float]]] = [0.48_145_466, 0.4_578_275, 0.40_821_073]    , UpperCamelCase_     :    Optional[Union[float, List[float]]] = [0.26_862_954, 0.26_130_258, 0.27_577_711]    , UpperCamelCase_     :    bool = True    , UpperCamelCase_     :    Optional[int]=7    , UpperCamelCase_     :    int=3_0    , UpperCamelCase_     :    str=4_0_0    , UpperCamelCase_     :    List[Any]=3    , ):
           lowerCAmelCase    :		Union[str, Any]				=      parent
           lowerCAmelCase    :		Union[str, Any]				=      do_resize
           lowerCAmelCase    :		List[str]				=      size if size is not None else {'''shortest_edge''': 2_8_8}
           lowerCAmelCase    :		int				=      size_divisor
           lowerCAmelCase    :		List[str]				=      do_rescale
           lowerCAmelCase    :		Optional[Any]				=      rescale_factor
           lowerCAmelCase    :		Dict				=      do_normalize
           lowerCAmelCase    :		Any				=      do_center_crop
           lowerCAmelCase    :		Union[str, Any]				=      image_mean
           lowerCAmelCase    :		Optional[Any]				=      image_std
           lowerCAmelCase    :		Union[str, Any]				=      do_pad
           lowerCAmelCase    :		Union[str, Any]				=      batch_size
           lowerCAmelCase    :		Any				=      num_channels
           lowerCAmelCase    :		Union[str, Any]				=      min_resolution
           lowerCAmelCase    :		int				=      max_resolution






     def    lowerCamelCase__   (		self     :    Dict ):
           return {
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_normalize": self.do_normalize,
               "do_resize": self.do_resize,
               "size": self.size,
               "size_divisor": self.size_divisor,
           }






     def    lowerCamelCase__   (		self     :    Any    , UpperCamelCase_     :    int    , UpperCamelCase_     :    List[str]=False ):
           if not batched:
                 lowerCAmelCase    :		Dict				=      self.size['''shortest_edge''']
                 lowerCAmelCase    :		Dict				=      image_inputs[0]
                 if isinstance(UpperCamelCase_    , Image.Image ):
                       lowerCAmelCase,					lowerCAmelCase    :		Optional[int]				=      image.size
                 else:
                       lowerCAmelCase,					lowerCAmelCase    :		List[Any]				=      image.shape[1], image.shape[2]
                 lowerCAmelCase    :		Union[str, Any]				=      size / min(UpperCamelCase_    , UpperCamelCase_ )
                 if h < w:
                       lowerCAmelCase,					lowerCAmelCase    :		Dict				=      size, scale * w
                 else:
                       lowerCAmelCase,					lowerCAmelCase    :		Optional[int]				=      scale * h, size

                 lowerCAmelCase    :		List[Any]				=      int((1_3_3_3 / 8_0_0) * size )
                 if max(UpperCamelCase_    , UpperCamelCase_ ) > max_size:
                       lowerCAmelCase    :		int				=      max_size / max(UpperCamelCase_    , UpperCamelCase_ )
                       lowerCAmelCase    :		str				=      newh * scale
                       lowerCAmelCase    :		Tuple				=      neww * scale

                 lowerCAmelCase,					lowerCAmelCase    :		List[str]				=      int(newh + 0.5 ), int(neww + 0.5 )
                 lowerCAmelCase,					lowerCAmelCase    :		Tuple				=      (
                     newh // self.size_divisor * self.size_divisor,
                     neww // self.size_divisor * self.size_divisor,
                 )

           else:
                 lowerCAmelCase    :		Optional[int]				=      []
                 for image in image_inputs:
                       lowerCAmelCase,					lowerCAmelCase    :		List[str]				=      self.get_expected_values([image] )
                       expected_values.append((expected_height, expected_width) )
                 lowerCAmelCase    :		Union[str, Any]				=      max(UpperCamelCase_    , key=lambda UpperCamelCase_ : item[0] )[0]
                 lowerCAmelCase    :		Union[str, Any]				=      max(UpperCamelCase_    , key=lambda UpperCamelCase_ : item[1] )[1]

           return expected_height, expected_width



@require_torch
@require_vision
class 						snake_case_(				a__							,    unittest.TestCase	):
     __UpperCamelCase     =    BridgeTowerImageProcessor if is_vision_available() else None






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           lowerCAmelCase    :		Optional[int]				=      BridgeTowerImageProcessingTester(self )






     @property
     def    lowerCamelCase__   (		self     :    List[str] ):
           return self.image_processor_tester.prepare_image_processor_dict()






     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		Optional[Any]				=      self.image_processing_class(**self.image_processor_dict )
           self.assertTrue(hasattr(UpperCamelCase_    , '''image_mean''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''image_std''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''do_normalize''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''do_resize''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''size''' ) )
           self.assertTrue(hasattr(UpperCamelCase_    , '''size_divisor''' ) )






     def    lowerCamelCase__   (		self     :    int ):
           pass






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           # Initialize image processor
           lowerCAmelCase    :		str				=      self.image_processing_class(**self.image_processor_dict )
           # create random PIL images
           lowerCAmelCase    :		Optional[int]				=      prepare_image_inputs(self.image_processor_tester    , equal_resolution=UpperCamelCase_ )
           for image in image_inputs:
                 self.assertIsInstance(UpperCamelCase_    , Image.Image )

           # Test not batched input
           lowerCAmelCase    :		Optional[int]				=      image_processing(image_inputs[0]    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		List[Any]				=      self.image_processor_tester.get_expected_values(UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

           # Test batched
           lowerCAmelCase    :		Dict				=      image_processing(UpperCamelCase_    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		int				=      self.image_processor_tester.get_expected_values(UpperCamelCase_    , batched=UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    , )






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           # Initialize image processor
           lowerCAmelCase    :		Tuple				=      self.image_processing_class(**self.image_processor_dict )
           # create random numpy tensors
           lowerCAmelCase    :		List[Any]				=      prepare_image_inputs(self.image_processor_tester    , equal_resolution=UpperCamelCase_    , numpify=UpperCamelCase_ )
           for image in image_inputs:
                 self.assertIsInstance(UpperCamelCase_    , np.ndarray )

           # Test not batched input
           lowerCAmelCase    :		Any				=      image_processing(image_inputs[0]    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		Optional[Any]				=      self.image_processor_tester.get_expected_values(UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

           # Test batched
           lowerCAmelCase    :		Tuple				=      image_processing(UpperCamelCase_    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		str				=      self.image_processor_tester.get_expected_values(UpperCamelCase_    , batched=UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    , )






     def    lowerCamelCase__   (		self     :    Optional[int] ):
           # Initialize image processor
           lowerCAmelCase    :		Union[str, Any]				=      self.image_processing_class(**self.image_processor_dict )
           # create random PyTorch tensors
           lowerCAmelCase    :		List[str]				=      prepare_image_inputs(self.image_processor_tester    , equal_resolution=UpperCamelCase_    , torchify=UpperCamelCase_ )
           for image in image_inputs:
                 self.assertIsInstance(UpperCamelCase_    , torch.Tensor )

           # Test not batched input
           lowerCAmelCase    :		Any				=      image_processing(image_inputs[0]    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		Tuple				=      self.image_processor_tester.get_expected_values(UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

           # Test batched
           lowerCAmelCase    :		str				=      image_processing(UpperCamelCase_    , return_tensors='''pt''' ).pixel_values

           lowerCAmelCase,					lowerCAmelCase    :		str				=      self.image_processor_tester.get_expected_values(UpperCamelCase_    , batched=UpperCamelCase_ )
           self.assertEqual(
               encoded_images.shape    , (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    , )


