class 		a__						:

     def __init__(   self				:	List[str]   ):







       """simple docstring"""






       SCREAMING_SNAKE_CASE_  :		dict[int, list[int]]    				=   {}
       # indexmap keys are an index and it's values are the number at that index
       SCREAMING_SNAKE_CASE_  :		dict[int, int]    				=   {}

     def     __UpperCamelCase       (   self				:	str,_A				:	Union[str, Any],_A				:	List[Any]   ):







       """simple docstring"""






       if isinstance(lowercase__,(range, str)   ):
         SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   list(lowercase__   )
       elif not isinstance(lowercase__,lowercase__   ):
         raise TypeError(
             "binary_search_delete() only accepts either a list, range or str"   )

       SCREAMING_SNAKE_CASE_  :		Any    				=   0
       SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   len(lowercase__   ) - 1

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

     def     __UpperCamelCase       (   self				:	Dict,_A				:	str,_A				:	str   ):







       """simple docstring"""






       if isinstance(lowercase__,(range, str)   ):
         SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   list(lowercase__   )
       elif not isinstance(lowercase__,lowercase__   ):
         raise TypeError(
             "binary_search_insert() only accepts either a list, range or str"   )

       SCREAMING_SNAKE_CASE_  :		Dict    				=   0
       SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   len(lowercase__   ) - 1

       while low <= high:
         SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   (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,lowercase__   )
           return array
         elif array[mid] < index:
           SCREAMING_SNAKE_CASE_  :		List[str]    				=   mid + 1
         else:
           SCREAMING_SNAKE_CASE_  :		Any    				=   mid - 1

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

     def     __UpperCamelCase       (   self				:	Union[str, Any],_A				:	Union[str, Any],_A				:	Union[str, Any]   ):







       """simple docstring"""






       if index in self.indexmap:
         SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   self.indexmap[index]
         if len(self.numbermap[n]   ) == 1:
           del self.numbermap[n]
         else:
           SCREAMING_SNAKE_CASE_  :		Any    				=   self.binary_search_delete(self.numbermap[n],lowercase__   )

        # Set new index
       SCREAMING_SNAKE_CASE_  :		Dict    				=   number

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

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






     def     __UpperCamelCase       (   self				:	Any,_A				:	int   ):







       """simple docstring"""






       return self.numbermap.get(lowercase__,[-1]   )[0]


if __name__ == "__main__":
  import doctest

  doctest.testmod()

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


__lowerCamelCase :     List[Any]          =  False






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

					def     __UpperCamelCase       (   self				:	Optional[int]   ):







							"""simple docstring"""






							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()

					def     __UpperCamelCase       (   self				:	Optional[Any]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int    				=   VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"   )
							# remove text_unet
							pipe.remove_unused_weights()
							pipe.to(_A   )
							pipe.set_progress_bar_config(disable=_A   )

							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"   )

							SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   torch.manual_seed(0   )
							SCREAMING_SNAKE_CASE_  :		int    				=   pipe(
							    prompt="first prompt",image=_A,text_to_image_strength=0.75,generator=_A,guidance_scale=7.5,num_inference_steps=2,output_type="numpy",).images

							with tempfile.TemporaryDirectory() as tmpdirname:
									pipe.save_pretrained(_A   )
									SCREAMING_SNAKE_CASE_  :		Dict    				=   VersatileDiffusionDualGuidedPipeline.from_pretrained(_A   )

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

							SCREAMING_SNAKE_CASE_  :		List[str]    				=   generator.manual_seed(0   )
							SCREAMING_SNAKE_CASE_  :		str    				=   pipe(
							    prompt="first prompt",image=_A,text_to_image_strength=0.75,generator=_A,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				:	Any   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any    				=   VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"   )
							pipe.remove_unused_weights()
							pipe.to(_A   )
							pipe.set_progress_bar_config(disable=_A   )

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   "cyberpunk 2077"
							SCREAMING_SNAKE_CASE_  :		Dict    				=   load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"   )
							SCREAMING_SNAKE_CASE_  :		Tuple    				=   torch.manual_seed(0   )
							SCREAMING_SNAKE_CASE_  :		Dict    				=   pipe(
							    prompt=_A,image=_A,text_to_image_strength=0.75,generator=_A,guidance_scale=7.5,num_inference_steps=50,output_type="numpy",).images

							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   image[0, 253:256, 253:256, -1]

							assert image.shape == (1, 512, 512, 3)
							SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]   )

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