





'''simple docstring'''







import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


snake_case_  :							Dict            =	False


@nightly
@require_torch_gpu
class        __a   (unittest.TestCase  ):

							def      UpperCAmelCase__						(				self				:  Dict )       ->							int:







													"""simple docstring"""
													# clean up the VRAM after each test
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()

							def      UpperCAmelCase__						(				self				:  Tuple )       ->							List[Any]:







													"""simple docstring"""
													UpperCAmelCase_      :   Union[str, Any]      =	VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion''' )
													# remove text_unet
													pipe.remove_unused_weights()
													pipe.to(__magic_name__ )
													pipe.set_progress_bar_config(disable=__magic_name__ )

													UpperCAmelCase_      :   str      =	load_image(
													    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' )

													UpperCAmelCase_      :   List[str]      =	torch.manual_seed(0 )
													UpperCAmelCase_      :   Dict      =	pipe(
													    prompt='''first prompt'''							,  image=__magic_name__							,  text_to_image_strength=0.7_5							,  generator=__magic_name__							,  guidance_scale=7.5							,  num_inference_steps=2							,  output_type='''numpy'''							,  ).images

													with tempfile.TemporaryDirectory() as tmpdirname:
																			pipe.save_pretrained(__magic_name__ )
																			UpperCAmelCase_      :   List[str]      =	VersatileDiffusionDualGuidedPipeline.from_pretrained(__magic_name__ )

													pipe.to(__magic_name__ )
													pipe.set_progress_bar_config(disable=__magic_name__ )

													UpperCAmelCase_      :   str      =	generator.manual_seed(0 )
													UpperCAmelCase_      :   str      =	pipe(
													    prompt='''first prompt'''							,  image=__magic_name__							,  text_to_image_strength=0.7_5							,  generator=__magic_name__							,  guidance_scale=7.5							,  num_inference_steps=2							,  output_type='''numpy'''							,  ).images

													assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass"



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







													"""simple docstring"""
													UpperCAmelCase_      :   Any      =	VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion''' )
													pipe.remove_unused_weights()
													pipe.to(__magic_name__ )
													pipe.set_progress_bar_config(disable=__magic_name__ )

													UpperCAmelCase_      :   Optional[int]      =	'''cyberpunk 2077'''
													UpperCAmelCase_      :   List[Any]      =	load_image(
													    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' )
													UpperCAmelCase_      :   List[str]      =	torch.manual_seed(0 )
													UpperCAmelCase_      :   str      =	pipe(
													    prompt=__magic_name__							,  image=__magic_name__							,  text_to_image_strength=0.7_5							,  generator=__magic_name__							,  guidance_scale=7.5							,  num_inference_steps=50							,  output_type='''numpy'''							,  ).images

													UpperCAmelCase_      :   Any      =	image[0, 2_53:2_56, 2_53:2_56, -1]

													assert image.shape == (1, 5_12, 5_12, 3)
													UpperCAmelCase_      :   Optional[Any]      =	np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9] )

													assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2












class 		_lowerCAmelCase:

  """simple docstring"""







  def __init__(   self      )->   None:
     # numbermap keys are the number and its values are lists of indexes sorted
     # in ascending order
     __A    =  {}
     # indexmap keys are an index and it's values are the number at that index
     __A    =  {}

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   list[int]:
     if isinstance(UpperCAmelCase    ,							(range, str)      ):
        __A    =  list(UpperCAmelCase      )
     elif not isinstance(UpperCAmelCase    ,							UpperCAmelCase      ):
        raise TypeError(
            '''binary_search_delete() only accepts either a list, range or str'''      )

     __A    =  0
     __A    =  len(UpperCAmelCase      ) - 1

     while low <= high:
        __A    =  (low + high) // 2
        if array[mid] == item:
           array.pop(UpperCAmelCase      )
           return array
        elif array[mid] < item:
           __A    =  mid + 1
        else:
           __A    =  mid - 1
     raise ValueError(
         '''Either the item is not in the array or the array was unsorted'''      )

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   list[int]:
     if isinstance(UpperCAmelCase    ,							(range, str)      ):
        __A    =  list(UpperCAmelCase      )
     elif not isinstance(UpperCAmelCase    ,							UpperCAmelCase      ):
        raise TypeError(
            '''binary_search_insert() only accepts either a list, range or str'''      )

     __A    =  0
     __A    =  len(UpperCAmelCase      ) - 1

     while low <= high:
        __A    =  (low + high) // 2
        if array[mid] == index:
           # If the item already exists in the array,
           # insert it after the existing item
           array.insert(mid + 1    ,							UpperCAmelCase      )
           return array
        elif array[mid] < index:
           __A    =  mid + 1
        else:
           __A    =  mid - 1

        # If the item doesn't exist in the array, insert it at the appropriate position
     array.insert(UpperCAmelCase    ,							UpperCAmelCase      )
     return array

  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   None:
     # Remove previous index
     if index in self.indexmap:
        __A    =  self.indexmap[index]
        if len(self.numbermap[n]      ) == 1:
           del self.numbermap[n]
        else:
           __A    =  self.binary_search_delete(self.numbermap[n]    ,							UpperCAmelCase      )

        # Set new index
     __A    =  number

     # Number not seen before or empty so insert number value
     if number not in self.numbermap:
        __A    =  [index]

     # Here we need to perform a binary search insertion in order to insert
     # The item in the correct place
     else:
        __A    =  self.binary_search_insert(
            self.numbermap[number]    ,							UpperCAmelCase      )





  def 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase      )->   int:
     return self.numbermap.get(UpperCAmelCase    ,							[-1]      )[0]


if __name__ == "__main__":
    import doctest

    doctest.testmod()

