






"""simple docstring"""



import gc
import unittest

import torch
from parameterized import parameterized

from diffusers import AutoencoderKL
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()







class    lowercase__			(							snake_case__,   snake_case__,   unittest.TestCase   ):
			_UpperCAmelCase		:List[Any]   			= AutoencoderKL
			_UpperCAmelCase		:str   			= '''sample'''
			_UpperCAmelCase		:int   			= 1e-2


			@property
			def 	UpperCAmelCase__						(      self     :  Any					):
										lowerCamelCase_       :							Optional[Any]									=4
										lowerCamelCase_       :							Optional[int]									=3
										lowerCamelCase_       :							Optional[Any]									=(32, 32)

										lowerCamelCase_       :							Union[str, Any]									=floats_tensor((batch_size, num_channels) + sizes					).to(SCREAMING_SNAKE_CASE__					)

										return {"sample": image}


			@property
			def 	UpperCAmelCase__						(      self     :  Any					):
										return (3, 32, 32)


			@property
			def 	UpperCAmelCase__						(      self     :  Dict					):
										return (3, 32, 32)


			def 	UpperCAmelCase__						(      self     :  List[Any]					):
										lowerCamelCase_       :							Any									={
										    '''block_out_channels''': [32, 64],
										    '''in_channels''': 3,
										    '''out_channels''': 3,
										    '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
										    '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
										    '''latent_channels''': 4,
										}
										lowerCamelCase_       :							Optional[int]									=self.dummy_input
										return init_dict, inputs_dict


			def 	UpperCAmelCase__						(      self     :  Any					):
										pass


			def 	UpperCAmelCase__						(      self     :  List[Any]					):
										pass


			@unittest.skipIf(torch_device == "mps"   ,       "Gradient checkpointing skipped on MPS"					)
			def 	UpperCAmelCase__						(      self     :  str					):
										lowerCamelCase_       :							Dict									=self.prepare_init_args_and_inputs_for_common()
										lowerCamelCase_       :							Optional[Any]									=self.model_class(**SCREAMING_SNAKE_CASE__					)
										model.to(SCREAMING_SNAKE_CASE__					)

										assert not model.is_gradient_checkpointing and model.training

										lowerCamelCase_       :							Union[str, Any]									=model(**SCREAMING_SNAKE_CASE__					).sample
										# run the backwards pass on the model. For backwards pass, for simplicity purpose,
										# we won't calculate the loss and rather backprop on out.sum()
										model.zero_grad()

										lowerCamelCase_       :							Union[str, Any]									=torch.randn_like(SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							Optional[Any]									=(out - labels).mean()
										loss.backward()

										# re-instantiate the model now enabling gradient checkpointing
										lowerCamelCase_       :							Optional[Any]									=self.model_class(**SCREAMING_SNAKE_CASE__					)
										# clone model
										model_a.load_state_dict(model.state_dict()					)
										model_a.to(SCREAMING_SNAKE_CASE__					)
										model_a.enable_gradient_checkpointing()

										assert model_a.is_gradient_checkpointing and model_a.training

										lowerCamelCase_       :							int									=model_a(**SCREAMING_SNAKE_CASE__					).sample
										# run the backwards pass on the model. For backwards pass, for simplicity purpose,
										# we won't calculate the loss and rather backprop on out.sum()
										model_a.zero_grad()
										lowerCamelCase_       :							Union[str, Any]									=(out_a - labels).mean()
										loss_a.backward()

										# compare the output and parameters gradients
										self.assertTrue((loss - loss_a).abs() < 1E-5					)
										lowerCamelCase_       :							Tuple									=dict(model.named_parameters()					)
										lowerCamelCase_       :							Any									=dict(model_a.named_parameters()					)
										for name, param in named_params.items():
																	self.assertTrue(torch_all_close(param.grad.data   ,       named_params_a[name].grad.data   ,       atol=5E-5					)					)


			def 	UpperCAmelCase__						(      self     :  int					):
										lowerCamelCase_       :							List[Any]									=AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy"   ,       output_loading_info=SCREAMING_SNAKE_CASE__					)
										self.assertIsNotNone(SCREAMING_SNAKE_CASE__					)
										self.assertEqual(len(loading_info["missing_keys"]					)   ,       0					)

										model.to(SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							List[str]									=model(**self.dummy_input					)

										assert image is not None, "Make sure output is not None"


			def 	UpperCAmelCase__						(      self     :  List[str]					):
										lowerCamelCase_       :							Optional[int]									=AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy"					)
										lowerCamelCase_       :							int									=model.to(SCREAMING_SNAKE_CASE__					)
										model.eval()

										if torch_device == "mps":
																	lowerCamelCase_       :							Optional[int]									=torch.manual_seed(0					)
										else:
																	lowerCamelCase_       :							int									=torch.Generator(device=SCREAMING_SNAKE_CASE__					).manual_seed(0					)

										lowerCamelCase_       :							Dict									=torch.randn(
										    1   ,       model.config.in_channels   ,       model.config.sample_size   ,       model.config.sample_size   ,       generator=torch.manual_seed(0					)   ,       )
										lowerCamelCase_       :							Tuple									=image.to(SCREAMING_SNAKE_CASE__					)
										with torch.no_grad():
																	lowerCamelCase_       :							int									=model(SCREAMING_SNAKE_CASE__   ,       sample_posterior=SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__					).sample

										lowerCamelCase_       :							Union[str, Any]									=output[0, -1, -3:, -3:].flatten().cpu()

										# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
										# the expected output slices are not the same for CPU and GPU.
										if torch_device == "mps":
																	lowerCamelCase_       :							Dict									=torch.tensor(
																	    [
																	        -4.0078E-01,
																	        -3.8323E-04,
																	        -1.2681E-01,
																	        -1.1462E-01,
																	        2.0095E-01,
																	        1.0893E-01,
																	        -8.8247E-02,
																	        -3.0361E-01,
																	        -9.8644E-03,
																	    ]					)
										elif torch_device == "cpu":
																	lowerCamelCase_       :							Optional[Any]									=torch.tensor(
																	    [-0.1_352, 0.0_878, 0.0_419, -0.0_818, -0.1_069, 0.0_688, -0.1_458, -0.4_446, -0.0_026]					)
										else:
																	lowerCamelCase_       :							int									=torch.tensor(
																	    [-0.2_421, 0.4_642, 0.2_507, -0.0_438, 0.0_682, 0.3_160, -0.2_018, -0.0_727, 0.2_485]					)

										self.assertTrue(torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       rtol=1E-2					)					)







@slow
class    lowercase__			(							unittest.TestCase   ):


			def 	UpperCAmelCase__						(      self     :  List[str]   ,       snake_case__     :  str   ,       snake_case__     :  Union[str, Any]					):
										return F"""gaussian_noise_s={seed}_shape={'_'.join([str(SCREAMING_SNAKE_CASE__					) for s in shape]					)}.npy"""


			def 	UpperCAmelCase__						(      self     :  str					):
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()


			def 	UpperCAmelCase__						(      self     :  Tuple   ,       snake_case__     :  str=0   ,       snake_case__     :  Tuple=(4, 3, 512, 512)   ,       snake_case__     :  Optional[Any]=False					):
										lowerCamelCase_       :							str									=torch.floataa if fpaa else torch.floataa
										lowerCamelCase_       :							Tuple									=torch.from_numpy(load_hf_numpy(self.get_file_format(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__					)					)					).to(SCREAMING_SNAKE_CASE__					).to(SCREAMING_SNAKE_CASE__					)
										return image


			def 	UpperCAmelCase__						(      self     :  Dict   ,       snake_case__     :  List[str]="CompVis/stable-diffusion-v1-4"   ,       snake_case__     :  Tuple=False					):
										lowerCamelCase_       :							List[Any]									='''fp16''' if fpaa else None
										lowerCamelCase_       :							List[Any]									=torch.floataa if fpaa else torch.floataa

										lowerCamelCase_       :							int									=AutoencoderKL.from_pretrained(
										    SCREAMING_SNAKE_CASE__   ,       subfolder="vae"   ,       torch_dtype=SCREAMING_SNAKE_CASE__   ,       revision=SCREAMING_SNAKE_CASE__   ,       )
										model.to(SCREAMING_SNAKE_CASE__					).eval()

										return model


			def 	UpperCAmelCase__						(      self     :  int   ,       snake_case__     :  Tuple=0					):






										if torch_device == "mps":
																	return torch.manual_seed(SCREAMING_SNAKE_CASE__					)
										return torch.Generator(device=SCREAMING_SNAKE_CASE__					).manual_seed(SCREAMING_SNAKE_CASE__					)


			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.1_603, 0.9_878, -0.0_495, -0.0_790, -0.2_709, 0.8_375, -0.2_060, -0.0_824], [-0.2_395, 0.0_098, 0.0_102, -0.0_709, -0.2_840, -0.0_274, -0.0_718, -0.1_824]],
			        [47, [-0.2_376, 0.1_168, 0.1_332, -0.4_840, -0.2_508, -0.0_791, -0.0_493, -0.4_089], [0.0_350, 0.0_847, 0.0_467, 0.0_344, -0.0_842, -0.0_547, -0.0_633, -0.1_131]],
			        # fmt: on
			    ]					)
			def 	UpperCAmelCase__						(      self     :  Tuple   ,       snake_case__     :  Any   ,       snake_case__     :  Union[str, Any]   ,       snake_case__     :  Dict					):
										lowerCamelCase_       :							Union[str, Any]									=self.get_sd_vae_model()
										lowerCamelCase_       :							Optional[Any]									=self.get_sd_image(SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							Optional[Any]									=self.get_generator(SCREAMING_SNAKE_CASE__					)

										with torch.no_grad():
																	lowerCamelCase_       :							Tuple									=model(SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       sample_posterior=SCREAMING_SNAKE_CASE__					).sample

										assert sample.shape == image.shape

										lowerCamelCase_       :							Union[str, Any]									=sample[-1, -2:, -2:, :2].flatten().float().cpu()
										lowerCamelCase_       :							str									=torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice					)

										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=3E-3					)


			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.0_513, 0.0_289, 1.3_799, 0.2_166, -0.2_573, -0.0_871, 0.5_103, -0.0_999]],
			        [47, [-0.4_128, -0.1_320, -0.3_704, 0.1_965, -0.4_116, -0.2_332, -0.3_340, 0.2_247]],
			        # fmt: on
			    ]					)
			@require_torch_gpu
			def 	UpperCAmelCase__						(      self     :  int   ,       snake_case__     :  Optional[Any]   ,       snake_case__     :  int					):
										lowerCamelCase_       :							str									=self.get_sd_vae_model(fpaa=SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							Optional[Any]									=self.get_sd_image(SCREAMING_SNAKE_CASE__   ,       fpaa=SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							Union[str, Any]									=self.get_generator(SCREAMING_SNAKE_CASE__					)

										with torch.no_grad():
																	lowerCamelCase_       :							Tuple									=model(SCREAMING_SNAKE_CASE__   ,       generator=SCREAMING_SNAKE_CASE__   ,       sample_posterior=SCREAMING_SNAKE_CASE__					).sample

										assert sample.shape == image.shape

										lowerCamelCase_       :							Any									=sample[-1, -2:, :2, -2:].flatten().float().cpu()
										lowerCamelCase_       :							Union[str, Any]									=torch.tensor(SCREAMING_SNAKE_CASE__					)

										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=1E-2					)


			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.1_609, 0.9_866, -0.0_487, -0.0_777, -0.2_716, 0.8_368, -0.2_055, -0.0_814], [-0.2_395, 0.0_098, 0.0_102, -0.0_709, -0.2_840, -0.0_274, -0.0_718, -0.1_824]],
			        [47, [-0.2_377, 0.1_147, 0.1_333, -0.4_841, -0.2_506, -0.0_805, -0.0_491, -0.4_085], [0.0_350, 0.0_847, 0.0_467, 0.0_344, -0.0_842, -0.0_547, -0.0_633, -0.1_131]],
			        # fmt: on
			    ]					)
			def 	UpperCAmelCase__						(      self     :  Dict   ,       snake_case__     :  List[str]   ,       snake_case__     :  List[str]   ,       snake_case__     :  int					):
										lowerCamelCase_       :							Union[str, Any]									=self.get_sd_vae_model()
										lowerCamelCase_       :							Dict									=self.get_sd_image(SCREAMING_SNAKE_CASE__					)

										with torch.no_grad():
																	lowerCamelCase_       :							List[Any]									=model(SCREAMING_SNAKE_CASE__					).sample

										assert sample.shape == image.shape

										lowerCamelCase_       :							int									=sample[-1, -2:, -2:, :2].flatten().float().cpu()
										lowerCamelCase_       :							Optional[Any]									=torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice					)

										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=3E-3					)


			@parameterized.expand(
			    [
			        # fmt: off
			        [13, [-0.2_051, -0.1_803, -0.2_311, -0.2_114, -0.3_292, -0.3_574, -0.2_953, -0.3_323]],
			        [37, [-0.2_632, -0.2_625, -0.2_199, -0.2_741, -0.4_539, -0.4_990, -0.3_720, -0.4_925]],
			        # fmt: on
			    ]					)
			@require_torch_gpu
			def 	UpperCAmelCase__						(      self     :  Tuple   ,       snake_case__     :  Union[str, Any]   ,       snake_case__     :  Any					):
										lowerCamelCase_       :							Any									=self.get_sd_vae_model()
										lowerCamelCase_       :							Optional[int]									=self.get_sd_image(SCREAMING_SNAKE_CASE__   ,       shape=(3, 4, 64, 64)					)

										with torch.no_grad():
																	lowerCamelCase_       :							List[str]									=model.decode(SCREAMING_SNAKE_CASE__					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										lowerCamelCase_       :							Optional[int]									=sample[-1, -2:, :2, -2:].flatten().cpu()
										lowerCamelCase_       :							Union[str, Any]									=torch.tensor(SCREAMING_SNAKE_CASE__					)

										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=1E-3					)


			@parameterized.expand(
			    [
			        # fmt: off
			        [27, [-0.0_369, 0.0_207, -0.0_776, -0.0_682, -0.1_747, -0.1_930, -0.1_465, -0.2_039]],
			        [16, [-0.1_628, -0.2_134, -0.2_747, -0.2_642, -0.3_774, -0.4_404, -0.3_687, -0.4_277]],
			        # fmt: on
			    ]					)
			@require_torch_gpu
			def 	UpperCAmelCase__						(      self     :  Tuple   ,       snake_case__     :  Optional[int]   ,       snake_case__     :  Dict					):
										lowerCamelCase_       :							List[str]									=self.get_sd_vae_model(fpaa=SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							int									=self.get_sd_image(SCREAMING_SNAKE_CASE__   ,       shape=(3, 4, 64, 64)   ,       fpaa=SCREAMING_SNAKE_CASE__					)

										with torch.no_grad():
																	lowerCamelCase_       :							List[Any]									=model.decode(SCREAMING_SNAKE_CASE__					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										lowerCamelCase_       :							Optional[Any]									=sample[-1, -2:, :2, -2:].flatten().float().cpu()
										lowerCamelCase_       :							Tuple									=torch.tensor(SCREAMING_SNAKE_CASE__					)

										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=5E-3					)


			@parameterized.expand([(13,), (16,), (27,)]					)
			@require_torch_gpu
			@unittest.skipIf(not is_xformers_available()   ,       reason="xformers is not required when using PyTorch 2.0."					)
			def 	UpperCAmelCase__						(      self     :  List[str]   ,       snake_case__     :  Union[str, Any]					):
										lowerCamelCase_       :							str									=self.get_sd_vae_model(fpaa=SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							Tuple									=self.get_sd_image(SCREAMING_SNAKE_CASE__   ,       shape=(3, 4, 64, 64)   ,       fpaa=SCREAMING_SNAKE_CASE__					)

										with torch.no_grad():
																	lowerCamelCase_       :							Optional[Any]									=model.decode(SCREAMING_SNAKE_CASE__					).sample

										model.enable_xformers_memory_efficient_attention()
										with torch.no_grad():
																	lowerCamelCase_       :							Tuple									=model.decode(SCREAMING_SNAKE_CASE__					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=1E-1					)


			@parameterized.expand([(13,), (16,), (37,)]					)
			@require_torch_gpu
			@unittest.skipIf(not is_xformers_available()   ,       reason="xformers is not required when using PyTorch 2.0."					)
			def 	UpperCAmelCase__						(      self     :  Any   ,       snake_case__     :  List[Any]					):
										lowerCamelCase_       :							List[Any]									=self.get_sd_vae_model()
										lowerCamelCase_       :							Optional[int]									=self.get_sd_image(SCREAMING_SNAKE_CASE__   ,       shape=(3, 4, 64, 64)					)

										with torch.no_grad():
																	lowerCamelCase_       :							List[str]									=model.decode(SCREAMING_SNAKE_CASE__					).sample

										model.enable_xformers_memory_efficient_attention()
										with torch.no_grad():
																	lowerCamelCase_       :							Dict									=model.decode(SCREAMING_SNAKE_CASE__					).sample

										assert list(sample.shape					) == [3, 3, 512, 512]

										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=1E-2					)


			@parameterized.expand(
			    [
			        # fmt: off
			        [33, [-0.3_001, 0.0_918, -2.6_984, -3.9_720, -3.2_099, -5.0_353, 1.7_338, -0.2_065, 3.4_267]],
			        [47, [-1.5_030, -4.3_871, -6.0_355, -9.1_157, -1.6_661, -2.7_853, 2.1_607, -5.0_823, 2.5_633]],
			        # fmt: on
			    ]					)
			def 	UpperCAmelCase__						(      self     :  Union[str, Any]   ,       snake_case__     :  int   ,       snake_case__     :  Optional[Any]					):
										lowerCamelCase_       :							List[Any]									=self.get_sd_vae_model()
										lowerCamelCase_       :							List[str]									=self.get_sd_image(SCREAMING_SNAKE_CASE__					)
										lowerCamelCase_       :							Tuple									=self.get_generator(SCREAMING_SNAKE_CASE__					)

										with torch.no_grad():
																	lowerCamelCase_       :							Optional[Any]									=model.encode(SCREAMING_SNAKE_CASE__					).latent_dist
																	lowerCamelCase_       :							str									=dist.sample(generator=SCREAMING_SNAKE_CASE__					)

										assert list(sample.shape					) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]]

										lowerCamelCase_       :							Any									=sample[0, -1, -3:, -3:].flatten().cpu()
										lowerCamelCase_       :							List[str]									=torch.tensor(SCREAMING_SNAKE_CASE__					)

										lowerCamelCase_       :							List[Any]									=3E-3 if torch_device != '''mps''' else 1E-2
										assert torch_all_close(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       atol=SCREAMING_SNAKE_CASE__					)










"""simple docstring"""
def      __lowerCamelCase   (							a_     :		list							)			->      list:

      if len(a_							) < 2:
            return collection

      def circle_sort_util(a_     :		list ,   a_     :		int ,   a_     :		int							) -> bool:
            __SCREAMING_SNAKE_CASE   :int						=  False

            if low == high:
                  return swapped

            __SCREAMING_SNAKE_CASE   :int						=  low
            __SCREAMING_SNAKE_CASE   :Tuple						=  high

            while left < right:
                  if collection[left] > collection[right]:
                        __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :str						=  (
                            collection[right],
                            collection[left],
                        )
                        __SCREAMING_SNAKE_CASE   :Optional[int]						=  True

                  left += 1
                  right -= 1

            if left == right and collection[left] > collection[right + 1]:
                  __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Tuple						=  (
                      collection[right + 1],
                      collection[left],
                  )

                  __SCREAMING_SNAKE_CASE   :Tuple						=  True

            __SCREAMING_SNAKE_CASE   :int						=  low + int((high - low) / 2							)
            __SCREAMING_SNAKE_CASE   :int						=  circle_sort_util(a_ ,   a_ ,   a_							)
            __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  circle_sort_util(a_ ,   mid + 1 ,   a_							)

            return swapped or left_swap or right_swap

      __SCREAMING_SNAKE_CASE   :Optional[Any]						=  True

      while is_not_sorted is True:
            __SCREAMING_SNAKE_CASE   :Tuple						=  circle_sort_util(a_ ,   0 ,   len(a_							) - 1							)

      return collection


if __name__ == "__main__":
  lowerCamelCase_								=		input("Enter numbers separated by a comma:\n").strip()
  lowerCamelCase_								=		[int(item) for item in user_input.split(",")]
  print(circle_sort(unsorted))