


"""simple docstring"""





import tempfile

import numpy as np
import torch
from transformers import AutoTokenizer, TaEncoderModel

from diffusers import DDPMScheduler, UNetaDConditionModel
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.pipelines.deepfloyd_if import IFWatermarker
from diffusers.utils.testing_utils import torch_device

from ..test_pipelines_common import to_np



class 		snake_case_:






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           torch.manual_seed(0				)
           lowerCAmelCase					:	Optional[int]       =   TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5'''				)

           torch.manual_seed(0				)
           lowerCAmelCase					:	int       =   AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5'''				)

           torch.manual_seed(0				)
           lowerCAmelCase					:	Any       =   UNetaDConditionModel(
               sample_size=3_2					,				layers_per_block=1					,				block_out_channels=[3_2, 6_4]					,				down_block_types=[
                   '''ResnetDownsampleBlock2D''',
                   '''SimpleCrossAttnDownBlock2D''',
               ]					,				mid_block_type='''UNetMidBlock2DSimpleCrossAttn'''					,				up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D''']					,				in_channels=3					,				out_channels=6					,				cross_attention_dim=3_2					,				encoder_hid_dim=3_2					,				attention_head_dim=8					,				addition_embed_type='''text'''					,				addition_embed_type_num_heads=2					,				cross_attention_norm='''group_norm'''					,				resnet_time_scale_shift='''scale_shift'''					,				act_fn='''gelu'''					,				)
           unet.set_attn_processor(AttnAddedKVProcessor()				)  # For reproducibility tests

           torch.manual_seed(0				)
           lowerCAmelCase					:	Dict       =   DDPMScheduler(
               num_train_timesteps=1_0_0_0					,				beta_schedule='''squaredcos_cap_v2'''					,				beta_start=0.0_001					,				beta_end=0.02					,				thresholding=lowerCamelCase__					,				dynamic_thresholding_ratio=0.95					,				sample_max_value=1.0					,				prediction_type='''epsilon'''					,				variance_type='''learned_range'''					,				)

           torch.manual_seed(0				)
           lowerCAmelCase					:	List[Any]       =   IFWatermarker()

           return {
               "text_encoder": text_encoder,
               "tokenizer": tokenizer,
               "unet": unet,
               "scheduler": scheduler,
               "watermarker": watermarker,
               "safety_checker": None,
               "feature_extractor": None,
           }






     def      lowerCamelCase__     (	self  :					List[str]				):
           torch.manual_seed(0				)
           lowerCAmelCase					:	str       =   TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5'''				)

           torch.manual_seed(0				)
           lowerCAmelCase					:	Any       =   AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5'''				)

           torch.manual_seed(0				)
           lowerCAmelCase					:	Optional[int]       =   UNetaDConditionModel(
               sample_size=3_2					,				layers_per_block=[1, 2]					,				block_out_channels=[3_2, 6_4]					,				down_block_types=[
                   '''ResnetDownsampleBlock2D''',
                   '''SimpleCrossAttnDownBlock2D''',
               ]					,				mid_block_type='''UNetMidBlock2DSimpleCrossAttn'''					,				up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D''']					,				in_channels=6					,				out_channels=6					,				cross_attention_dim=3_2					,				encoder_hid_dim=3_2					,				attention_head_dim=8					,				addition_embed_type='''text'''					,				addition_embed_type_num_heads=2					,				cross_attention_norm='''group_norm'''					,				resnet_time_scale_shift='''scale_shift'''					,				act_fn='''gelu'''					,				class_embed_type='''timestep'''					,				mid_block_scale_factor=1.414					,				time_embedding_act_fn='''gelu'''					,				time_embedding_dim=3_2					,				)
           unet.set_attn_processor(AttnAddedKVProcessor()				)  # For reproducibility tests

           torch.manual_seed(0				)
           lowerCAmelCase					:	Optional[Any]       =   DDPMScheduler(
               num_train_timesteps=1_0_0_0					,				beta_schedule='''squaredcos_cap_v2'''					,				beta_start=0.0_001					,				beta_end=0.02					,				thresholding=lowerCamelCase__					,				dynamic_thresholding_ratio=0.95					,				sample_max_value=1.0					,				prediction_type='''epsilon'''					,				variance_type='''learned_range'''					,				)

           torch.manual_seed(0				)
           lowerCAmelCase					:	Dict       =   DDPMScheduler(
               num_train_timesteps=1_0_0_0					,				beta_schedule='''squaredcos_cap_v2'''					,				beta_start=0.0_001					,				beta_end=0.02					,				)

           torch.manual_seed(0				)
           lowerCAmelCase					:	List[str]       =   IFWatermarker()

           return {
               "text_encoder": text_encoder,
               "tokenizer": tokenizer,
               "unet": unet,
               "scheduler": scheduler,
               "image_noising_scheduler": image_noising_scheduler,
               "watermarker": watermarker,
               "safety_checker": None,
               "feature_extractor": None,
           }






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           lowerCAmelCase					:	List[str]       =   self.get_dummy_components()
           lowerCAmelCase					:	List[str]       =   self.pipeline_class(**lowerCamelCase__				)
           pipe.to(lowerCamelCase__				)
           pipe.set_progress_bar_config(disable=lowerCamelCase__				)

           lowerCAmelCase					:	Any       =   self.get_dummy_inputs(lowerCamelCase__				)

           lowerCAmelCase					:	List[Any]       =   inputs['''prompt''']
           lowerCAmelCase					:	List[Any]       =   inputs['''generator''']
           lowerCAmelCase					:	str       =   inputs['''num_inference_steps''']
           lowerCAmelCase					:	Union[str, Any]       =   inputs['''output_type''']

           if "image" in inputs:
                 lowerCAmelCase					:	Optional[Any]       =   inputs['''image''']
           else:
                 lowerCAmelCase					:	int       =   None

           if "mask_image" in inputs:
                 lowerCAmelCase					:	int       =   inputs['''mask_image''']
           else:
                 lowerCAmelCase					:	Dict       =   None

           if "original_image" in inputs:
                 lowerCAmelCase					:	int       =   inputs['''original_image''']
           else:
                 lowerCAmelCase					:	Any       =   None

           lowerCAmelCase					:	Optional[Any]       =   pipe.encode_prompt(lowerCamelCase__				)

           # inputs with prompt converted to embeddings
           lowerCAmelCase					:	Optional[int]       =   {
               '''prompt_embeds''': prompt_embeds,
               '''negative_prompt_embeds''': negative_prompt_embeds,
               '''generator''': generator,
               '''num_inference_steps''': num_inference_steps,
               '''output_type''': output_type,
           }

           if image is not None:
                 lowerCAmelCase					:	Tuple       =   image

           if mask_image is not None:
                 lowerCAmelCase					:	Optional[Any]       =   mask_image

           if original_image is not None:
                 lowerCAmelCase					:	Dict       =   original_image

           # set all optional components to None
           for optional_component in pipe._optional_components:
                 setattr(lowerCamelCase__					,				lowerCamelCase__					,				lowerCamelCase__				)

           lowerCAmelCase					:	Optional[int]       =   pipe(**lowerCamelCase__				)[0]

           with tempfile.TemporaryDirectory() as tmpdir:
                 pipe.save_pretrained(lowerCamelCase__				)
                 lowerCAmelCase					:	Optional[Any]       =   self.pipeline_class.from_pretrained(lowerCamelCase__				)
                 pipe_loaded.to(lowerCamelCase__				)
                 pipe_loaded.set_progress_bar_config(disable=lowerCamelCase__				)

           pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()				)  # For reproducibility tests

           for optional_component in pipe._optional_components:
                 self.assertTrue(
                     getattr(lowerCamelCase__					,				lowerCamelCase__				) is None					,				F'''`{optional_component}` did not stay set to None after loading.'''					,				)

           lowerCAmelCase					:	Optional[Any]       =   self.get_dummy_inputs(lowerCamelCase__				)

           lowerCAmelCase					:	Optional[Any]       =   inputs['''generator''']
           lowerCAmelCase					:	int       =   inputs['''num_inference_steps''']
           lowerCAmelCase					:	Optional[int]       =   inputs['''output_type''']

           # inputs with prompt converted to embeddings
           lowerCAmelCase					:	List[str]       =   {
               '''prompt_embeds''': prompt_embeds,
               '''negative_prompt_embeds''': negative_prompt_embeds,
               '''generator''': generator,
               '''num_inference_steps''': num_inference_steps,
               '''output_type''': output_type,
           }

           if image is not None:
                 lowerCAmelCase					:	Any       =   image

           if mask_image is not None:
                 lowerCAmelCase					:	List[str]       =   mask_image

           if original_image is not None:
                 lowerCAmelCase					:	str       =   original_image

           lowerCAmelCase					:	str       =   pipe_loaded(**lowerCamelCase__				)[0]

           lowerCAmelCase					:	Any       =   np.abs(to_np(lowerCamelCase__				) - to_np(lowerCamelCase__				)				).max()
           self.assertLess(lowerCamelCase__					,				1E-4				)






     def      lowerCamelCase__     (	self  :					str				):
           lowerCAmelCase					:	Optional[int]       =   self.get_dummy_components()
           lowerCAmelCase					:	List[str]       =   self.pipeline_class(**lowerCamelCase__				)
           pipe.to(lowerCamelCase__				)
           pipe.set_progress_bar_config(disable=lowerCamelCase__				)

           lowerCAmelCase					:	Dict       =   self.get_dummy_inputs(lowerCamelCase__				)
           lowerCAmelCase					:	Dict       =   pipe(**lowerCamelCase__				)[0]

           with tempfile.TemporaryDirectory() as tmpdir:
                 pipe.save_pretrained(lowerCamelCase__				)
                 lowerCAmelCase					:	Union[str, Any]       =   self.pipeline_class.from_pretrained(lowerCamelCase__				)
                 pipe_loaded.to(lowerCamelCase__				)
                 pipe_loaded.set_progress_bar_config(disable=lowerCamelCase__				)

           pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()				)  # For reproducibility tests

           lowerCAmelCase					:	Tuple       =   self.get_dummy_inputs(lowerCamelCase__				)
           lowerCAmelCase					:	Union[str, Any]       =   pipe_loaded(**lowerCamelCase__				)[0]

           lowerCAmelCase					:	List[Any]       =   np.abs(to_np(lowerCamelCase__				) - to_np(lowerCamelCase__				)				).max()
           self.assertLess(lowerCamelCase__					,				1E-4				)






"""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}""")


