


"""simple docstring"""





import gc
import inspect
import unittest

import torch
from parameterized import parameterized

from diffusers import PriorTransformer
from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


enable_full_determinism()



class 		snake_case_(       a__							,					unittest.TestCase			):
     __UpperCamelCase  =					PriorTransformer
     __UpperCamelCase  =					'''hidden_states'''






     @property
     def      lowerCamelCase__     (	self  :					List[str]				):
           lowerCAmelCase					:	Optional[Any]       =   4
           lowerCAmelCase					:	str       =   8
           lowerCAmelCase					:	Optional[Any]       =   7

           lowerCAmelCase					:	List[Any]       =   floats_tensor((batch_size, embedding_dim)				).to(__A				)

           lowerCAmelCase					:	List[str]       =   floats_tensor((batch_size, embedding_dim)				).to(__A				)
           lowerCAmelCase					:	Tuple       =   floats_tensor((batch_size, num_embeddings, embedding_dim)				).to(__A				)

           return {
               "hidden_states": hidden_states,
               "timestep": 2,
               "proj_embedding": proj_embedding,
               "encoder_hidden_states": encoder_hidden_states,
           }






     def      lowerCamelCase__     (	self  :					Union[str, Any]					,				UpperCamelCase_  :					List[Any]=0				):
           torch.manual_seed(__A				)
           lowerCAmelCase					:	List[str]       =   4
           lowerCAmelCase					:	int       =   8
           lowerCAmelCase					:	List[str]       =   7

           lowerCAmelCase					:	str       =   torch.randn((batch_size, embedding_dim)				).to(__A				)

           lowerCAmelCase					:	Union[str, Any]       =   torch.randn((batch_size, embedding_dim)				).to(__A				)
           lowerCAmelCase					:	int       =   torch.randn((batch_size, num_embeddings, embedding_dim)				).to(__A				)

           return {
               "hidden_states": hidden_states,
               "timestep": 2,
               "proj_embedding": proj_embedding,
               "encoder_hidden_states": encoder_hidden_states,
           }






     @property
     def      lowerCamelCase__     (	self  :					Dict				):
           return (4, 8)






     @property
     def      lowerCamelCase__     (	self  :					Any				):
           return (4, 8)






     def      lowerCamelCase__     (	self  :					List[Any]				):
           lowerCAmelCase					:	str       =   {
               '''num_attention_heads''': 2,
               '''attention_head_dim''': 4,
               '''num_layers''': 2,
               '''embedding_dim''': 8,
               '''num_embeddings''': 7,
               '''additional_embeddings''': 4,
           }
           lowerCAmelCase					:	Optional[int]       =   self.dummy_input
           return init_dict, inputs_dict






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           lowerCAmelCase,      lowerCAmelCase					:	Union[str, Any]       =   PriorTransformer.from_pretrained(
               '''hf-internal-testing/prior-dummy'''					,				output_loading_info=__A				)
           self.assertIsNotNone(__A				)
           self.assertEqual(len(loading_info['''missing_keys''']				)					,				0				)

           model.to(__A				)
           lowerCAmelCase					:	Dict       =   model(**self.dummy_input				)[0]

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






     def      lowerCamelCase__     (	self  :					Tuple				):
           lowerCAmelCase,      lowerCAmelCase					:	Optional[Any]       =   self.prepare_init_args_and_inputs_for_common()

           lowerCAmelCase					:	Tuple       =   self.model_class(**__A				)
           lowerCAmelCase					:	Tuple       =   inspect.signature(model.forward				)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowerCAmelCase					:	str       =   [*signature.parameters.keys()]

           lowerCAmelCase					:	List[Any]       =   ['''hidden_states''', '''timestep''']
           self.assertListEqual(arg_names[:2]					,				__A				)






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           lowerCAmelCase					:	List[str]       =   PriorTransformer.from_pretrained('''hf-internal-testing/prior-dummy'''				)
           lowerCAmelCase					:	Dict       =   model.to(__A				)

           if hasattr(__A					,				'''set_default_attn_processor'''				):
                 model.set_default_attn_processor()

           lowerCAmelCase					:	str       =   self.get_dummy_seed_input()

           with torch.no_grad():
                 lowerCAmelCase					:	Optional[int]       =   model(**__A				)[0]

           lowerCAmelCase					:	Tuple       =   output[0, :5].flatten().cpu()
           print(__A				)

           # 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.
           lowerCAmelCase					:	Optional[Any]       =   torch.tensor([-1.3_436, -0.2_870, 0.7_538, 0.4_368, -0.0_239]				)
           self.assertTrue(torch_all_close(__A					,				__A					,				rtol=1E-2				)				)



@slow
class 		snake_case_(       unittest.TestCase			):






     def      lowerCamelCase__     (	self  :					int					,				UpperCamelCase_  :					Optional[Any]=1					,				UpperCamelCase_  :					List[Any]=7_6_8					,				UpperCamelCase_  :					List[str]=7_7					,				UpperCamelCase_  :					List[Any]=0				):
           torch.manual_seed(__A				)
           lowerCAmelCase					:	int       =   batch_size
           lowerCAmelCase					:	Tuple       =   embedding_dim
           lowerCAmelCase					:	Any       =   num_embeddings

           lowerCAmelCase					:	Tuple       =   torch.randn((batch_size, embedding_dim)				).to(__A				)

           lowerCAmelCase					:	Any       =   torch.randn((batch_size, embedding_dim)				).to(__A				)
           lowerCAmelCase					:	Any       =   torch.randn((batch_size, num_embeddings, embedding_dim)				).to(__A				)

           return {
               "hidden_states": hidden_states,
               "timestep": 2,
               "proj_embedding": proj_embedding,
               "encoder_hidden_states": encoder_hidden_states,
           }






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()






     @parameterized.expand(
         [
             # fmt: off
             [1_3, [-0.5_861,  0.1_283, -0.0_931,  0.0_882,  0.4_476,  0.1_329, -0.0_498,  0.0_640]],
             [3_7, [-0.4_913,  0.0_110, -0.0_483,  0.0_541,  0.4_954, -0.0_170,  0.0_354,  0.1_651]],
             # fmt: on
         ]				)
     def      lowerCamelCase__     (	self  :					Any					,				UpperCamelCase_  :					Tuple					,				UpperCamelCase_  :					int				):
           lowerCAmelCase					:	Union[str, Any]       =   PriorTransformer.from_pretrained('''kandinsky-community/kandinsky-2-1-prior'''					,				subfolder='''prior'''				)
           model.to(__A				)
           lowerCAmelCase					:	Tuple       =   self.get_dummy_seed_input(seed=__A				)

           with torch.no_grad():
                 lowerCAmelCase					:	Optional[int]       =   model(**__A				)[0]

           assert list(sample.shape				) == [1, 7_6_8]

           lowerCAmelCase					:	Dict       =   sample[0, :8].flatten().cpu()
           print(__A				)
           lowerCAmelCase					:	List[str]       =   torch.tensor(__A				)

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






"""simple docstring"""





from __future__ import annotations


def      _snake_case						(						_snake_case				:			list[int] ,   _snake_case				:			int			):
      if len(_snake_case			) == 0:
            return False
      lowerCAmelCase					:	List[Any]       =   len(_snake_case			) // 2
      if a_list[midpoint] == item:
            return True
      if item < a_list[midpoint]:
            return binary_search(a_list[:midpoint] ,   _snake_case			)
      else:
            return binary_search(a_list[midpoint + 1 :] ,   _snake_case			)


if __name__ == "__main__":
 snake_case__		:     List[str]    				=				input('''Enter numbers separated by comma:\n''').strip()
 snake_case__		:     Optional[int]    				=				[int(item.strip()) for item in user_input.split(''',''')]
 snake_case__		:     Dict    				=				int(input('''Enter the number to be found in the list:\n''').strip())
 snake_case__		:     str    				=				'''''' if binary_search(sequence, target) else '''not '''
 print(f"""{target} was {not_str}found in {sequence}""")


