




def   A		(__A  :       list[int]			)  -> list[int]:







     """simple docstring"""


     if len(__A			) > 1:
          UpperCAmelCase_		=				len(__A			) // 2  # Finds the middle of the array
          UpperCAmelCase_		=				arr[
              :middle_length
          ]  # Creates an array of the elements in the first half.
          UpperCAmelCase_		=				arr[
              middle_length:
          ]  # Creates an array of the elements in the second half.
          UpperCAmelCase_		=				len(__A			)
          UpperCAmelCase_		=				len(__A			)
          merge(__A			)  # Starts sorting the left.
          merge(__A			)  # Starts sorting the right
          UpperCAmelCase_		=				0  # Left Counter
          UpperCAmelCase_		=				0  # Right Counter
          UpperCAmelCase_		=				0  # Position Counter
          while (
              left_index < left_size and right_index < right_size
          ):  # Runs until the lowers size of the left and right are sorted.
               if left_array[left_index] < right_array[right_index]:
                    UpperCAmelCase_		=				left_array[left_index]
                    left_index += 1
               else:
                    UpperCAmelCase_		=				right_array[right_index]
                    right_index += 1
               index += 1
          while (
              left_index < left_size
          ):  # Adds the left over elements in the left half of the array
               UpperCAmelCase_		=				left_array[left_index]
               left_index += 1
               index += 1
          while (
              right_index < right_size
          ):  # Adds the left over elements in the right half of the array
               UpperCAmelCase_		=				right_array[right_index]
               right_index += 1
               index += 1
     return arr


if __name__ == "__main__":
    import doctest

    doctest.testmod()












import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig

from diffusers import AutoencoderKL, PaintByExamplePipeline, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.paint_by_example import PaintByExampleImageEncoder
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class __snake_case     (     a					,    unittest.TestCase						):
   UpperCAmelCase__    :       str		=    PaintByExamplePipeline
   UpperCAmelCase__    :       Tuple		=    IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
   UpperCAmelCase__    :       Any		=    IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
   UpperCAmelCase__    :       Union[str, Any]		=    frozenset([]						)  # TO_DO: update the image_prams once refactored VaeImageProcessor.preprocess







   def lowerCamelCase		( self						:		Optional[Any]):

        """simple docstring"""


        torch.manual_seed(0)
        UpperCAmelCase_		=				UNetaDConditionModel(
            block_out_channels=(32, 64)   ,		layers_per_block=2   ,		sample_size=32   ,		in_channels=9   ,		out_channels=4   ,		down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   ,		up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   ,		cross_attention_dim=32   ,		)
        UpperCAmelCase_		=				PNDMScheduler(skip_prk_steps=_snake_case)
        torch.manual_seed(0)
        UpperCAmelCase_		=				AutoencoderKL(
            block_out_channels=[32, 64]   ,		in_channels=3   ,		out_channels=3   ,		down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,		up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,		latent_channels=4   ,		)
        torch.manual_seed(0)
        UpperCAmelCase_		=				CLIPVisionConfig(
            hidden_size=32   ,		projection_dim=32   ,		intermediate_size=37   ,		layer_norm_eps=1e-05   ,		num_attention_heads=4   ,		num_hidden_layers=5   ,		image_size=32   ,		patch_size=4   ,		)
        UpperCAmelCase_		=				PaintByExampleImageEncoder(_snake_case   ,		proj_size=32)
        UpperCAmelCase_		=				CLIPImageProcessor(crop_size=32   ,		size=32)

        UpperCAmelCase_		=				{
            '''unet''': unet,
            '''scheduler''': scheduler,
            '''vae''': vae,
            '''image_encoder''': image_encoder,
            '''safety_checker''': None,
            '''feature_extractor''': feature_extractor,
        }
        return components







   def lowerCamelCase		( self						:		Optional[Any]   ,		_snake_case						:		List[str]):

        """simple docstring"""


        UpperCAmelCase_		=				np.array(image.convert('''RGB'''))
        UpperCAmelCase_		=				image[None].transpose(0   ,		3   ,		1   ,		2)
        UpperCAmelCase_		=				torch.from_numpy(_snake_case).to(dtype=torch.floataa) / 1_2_7.5 - 1.0
        return image







   def lowerCamelCase		( self						:		Tuple   ,		_snake_case						:		Dict="cpu"   ,		_snake_case						:		int=0):

        """simple docstring"""


        UpperCAmelCase_		=				floats_tensor((1, 3, 32, 32)   ,		rng=random.Random(_snake_case)).to(_snake_case)
        UpperCAmelCase_		=				image.cpu().permute(0   ,		2   ,		3   ,		1)[0]
        UpperCAmelCase_		=				Image.fromarray(np.uinta(_snake_case)).convert('''RGB''').resize((64, 64))
        UpperCAmelCase_		=				Image.fromarray(np.uinta(image + 4)).convert('''RGB''').resize((64, 64))
        UpperCAmelCase_		=				Image.fromarray(np.uinta(_snake_case)).convert('''RGB''').resize((32, 32))

        if str(_snake_case).startswith('''mps'''):
             UpperCAmelCase_		=				torch.manual_seed(_snake_case)
        else:
             UpperCAmelCase_		=				torch.Generator(device=_snake_case).manual_seed(_snake_case)
        UpperCAmelCase_		=				{
            '''example_image''': example_image,
            '''image''': init_image,
            '''mask_image''': mask_image,
            '''generator''': generator,
            '''num_inference_steps''': 2,
            '''guidance_scale''': 6.0,
            '''output_type''': '''numpy''',
        }
        return inputs







   def lowerCamelCase		( self						:		Any):

        """simple docstring"""


        UpperCAmelCase_		=				self.get_dummy_components()

        # make sure here that pndm scheduler skips prk
        UpperCAmelCase_		=				PaintByExamplePipeline(**_snake_case)
        UpperCAmelCase_		=				pipe.to('''cpu''')
        pipe.set_progress_bar_config(disable=_snake_case)

        UpperCAmelCase_		=				self.get_dummy_inputs()
        UpperCAmelCase_		=				pipe(**_snake_case)
        UpperCAmelCase_		=				output.images

        UpperCAmelCase_		=				image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)
        UpperCAmelCase_		=				np.array([0.4_6_8_6, 0.5_6_8_7, 0.4_0_0_7, 0.5_2_1_8, 0.5_7_4_1, 0.4_4_8_2, 0.4_9_4_0, 0.4_6_2_9, 0.4_5_0_3])

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







   def lowerCamelCase		( self						:		Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_		=				'''cpu'''
        UpperCAmelCase_		=				self.get_dummy_inputs()
        inputs.pop('''mask_image''')
        UpperCAmelCase_		=				self.convert_to_pt(inputs.pop('''image'''))
        UpperCAmelCase_		=				image.clamp(0   ,		1) / 2

        # make sure here that pndm scheduler skips prk
        UpperCAmelCase_		=				PaintByExamplePipeline(**self.get_dummy_components())
        UpperCAmelCase_		=				pipe.to(_snake_case)
        pipe.set_progress_bar_config(disable=_snake_case)

        UpperCAmelCase_		=				pipe(image=_snake_case   ,		mask_image=mask_image[:, 0]   ,		**_snake_case)
        UpperCAmelCase_		=				output.images

        UpperCAmelCase_		=				image.cpu().permute(0   ,		2   ,		3   ,		1)[0]
        UpperCAmelCase_		=				mask_image.cpu().permute(0   ,		2   ,		3   ,		1)[0]

        UpperCAmelCase_		=				Image.fromarray(np.uinta(_snake_case)).convert('''RGB''')
        UpperCAmelCase_		=				Image.fromarray(np.uinta(_snake_case)).convert('''RGB''')

        UpperCAmelCase_		=				pipe(**self.get_dummy_inputs())
        UpperCAmelCase_		=				output.images

        assert out_a.shape == (1, 64, 64, 3)
        assert np.abs(out_a.flatten() - out_a.flatten()).max() < 5e-2







   def lowerCamelCase		( self						:		Any):

        """simple docstring"""


        super().test_inference_batch_single_identical(expected_max_diff=3e-3)







@slow
@require_torch_gpu
class __snake_case     (     unittest.TestCase						):







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

        """simple docstring"""


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







   def lowerCamelCase		( self						:		Tuple):

        """simple docstring"""


        UpperCAmelCase_		=				load_image(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
            '''/paint_by_example/dog_in_bucket.png''')
        UpperCAmelCase_		=				load_image(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
            '''/paint_by_example/mask.png''')
        UpperCAmelCase_		=				load_image(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
            '''/paint_by_example/panda.jpg''')

        UpperCAmelCase_		=				PaintByExamplePipeline.from_pretrained('''Fantasy-Studio/Paint-by-Example''')
        UpperCAmelCase_		=				pipe.to(_snake_case)
        pipe.set_progress_bar_config(disable=_snake_case)

        UpperCAmelCase_		=				torch.manual_seed(321)
        UpperCAmelCase_		=				pipe(
            image=_snake_case   ,		mask_image=_snake_case   ,		example_image=_snake_case   ,		generator=_snake_case   ,		guidance_scale=5.0   ,		num_inference_steps=50   ,		output_type='''np'''   ,		)

        UpperCAmelCase_		=				output.images

        UpperCAmelCase_		=				image[0, -3:, -3:, -1]

        assert image.shape == (1, 512, 512, 3)
        UpperCAmelCase_		=				np.array([0.4_8_3_4, 0.4_8_1_1, 0.4_8_7_4, 0.5_1_2_2, 0.5_0_8_1, 0.5_1_4_4, 0.5_2_9_1, 0.5_2_9_0, 0.5_3_7_4])

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






