
import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import floats_tensor, load_image, load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference



class    _snake_case (						UpperCAmelCase_	,    unittest.TestCase				):
 __lowerCAmelCase	:  Union[str, Any]	   =      ShapEImgaImgPipeline
 __lowerCAmelCase	:  Optional[int]	   =      ['image']
 __lowerCAmelCase	:  str	   =      ['image']
 __lowerCAmelCase	:  List[str]	   =      [
     'num_images_per_prompt',
     'num_inference_steps',
     'generator',
     'latents',
     'guidance_scale',
     'frame_size',
     'output_type',
     'return_dict',
 ]
 __lowerCAmelCase	:  Union[str, Any]	   =      False


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   return 32


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   return 32


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   return self.time_input_dim * 4


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   return 8


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   torch.manual_seed(0)
   lowercase__	: List[str]     		=   CLIPVisionConfig(
       hidden_size=self.text_embedder_hidden_size       ,	image_size=64       ,	projection_dim=self.text_embedder_hidden_size       ,	intermediate_size=37       ,	num_attention_heads=4       ,	num_channels=3       ,	num_hidden_layers=5       ,	patch_size=1       ,	)

   lowercase__	: Any     		=   CLIPVisionModel(SCREAMING_SNAKE_CASE_)
   return model


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: str     		=   CLIPImageProcessor(
       crop_size=2_24       ,	do_center_crop=SCREAMING_SNAKE_CASE_       ,	do_normalize=SCREAMING_SNAKE_CASE_       ,	do_resize=SCREAMING_SNAKE_CASE_       ,	image_mean=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]       ,	image_std=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]       ,	resample=3       ,	size=2_24       ,	)

   return image_processor


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   torch.manual_seed(0)

   lowercase__	: int     		=   {
       """num_attention_heads""": 2,
       """attention_head_dim""": 16,
       """embedding_dim""": self.time_input_dim,
       """num_embeddings""": 32,
       """embedding_proj_dim""": self.text_embedder_hidden_size,
       """time_embed_dim""": self.time_embed_dim,
       """num_layers""": 1,
       """clip_embed_dim""": self.time_input_dim * 2,
       """additional_embeddings""": 0,
       """time_embed_act_fn""": """gelu""",
       """norm_in_type""": """layer""",
       """embedding_proj_norm_type""": """layer""",
       """encoder_hid_proj_type""": None,
       """added_emb_type""": None,
   }

   lowercase__	: List[Any]     		=   PriorTransformer(**SCREAMING_SNAKE_CASE_)
   return model


 @property
 def   lowercase__  (       self):


   '''simple docstring'''




   torch.manual_seed(0)

   lowercase__	: Dict     		=   {
       """param_shapes""": (
           (self.renderer_dim, 93),
           (self.renderer_dim, 8),
           (self.renderer_dim, 8),
           (self.renderer_dim, 8),
       ),
       """d_latent""": self.time_input_dim,
       """d_hidden""": self.renderer_dim,
       """n_output""": 12,
       """background""": (
           0.1,
           0.1,
           0.1,
       ),
   }
   lowercase__	: int     		=   ShapERenderer(**SCREAMING_SNAKE_CASE_)
   return model


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Dict     		=   self.dummy_prior
   lowercase__	: List[str]     		=   self.dummy_image_encoder
   lowercase__	: Union[str, Any]     		=   self.dummy_image_processor
   lowercase__	: List[Any]     		=   self.dummy_renderer

   lowercase__	: Optional[Any]     		=   HeunDiscreteScheduler(
       beta_schedule="""exp"""       ,	num_train_timesteps=10_24       ,	prediction_type="""sample"""       ,	use_karras_sigmas=SCREAMING_SNAKE_CASE_       ,	clip_sample=SCREAMING_SNAKE_CASE_       ,	clip_sample_range=1.0       ,	)
   lowercase__	: Tuple     		=   {
       """prior""": prior,
       """image_encoder""": image_encoder,
       """image_processor""": image_processor,
       """renderer""": renderer,
       """scheduler""": scheduler,
   }

   return components


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_=0):


   '''simple docstring'''




   lowercase__	: Optional[Any]     		=   floats_tensor((1, 3, 64, 64)       ,	rng=random.Random(SCREAMING_SNAKE_CASE_)).to(SCREAMING_SNAKE_CASE_)

   if str(SCREAMING_SNAKE_CASE_).startswith("""mps"""):
     lowercase__	: Union[str, Any]     		=   torch.manual_seed(SCREAMING_SNAKE_CASE_)
   else:
     lowercase__	: Union[str, Any]     		=   torch.Generator(device=SCREAMING_SNAKE_CASE_).manual_seed(SCREAMING_SNAKE_CASE_)
   lowercase__	: Union[str, Any]     		=   {
       """image""": input_image,
       """generator""": generator,
       """num_inference_steps""": 1,
       """frame_size""": 32,
       """output_type""": """np""",
   }
   return inputs


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Optional[Any]     		=   """cpu"""

   lowercase__	: Dict     		=   self.get_dummy_components()

   lowercase__	: int     		=   self.pipeline_class(**SCREAMING_SNAKE_CASE_)
   lowercase__	: Any     		=   pipe.to(SCREAMING_SNAKE_CASE_)

   pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

   lowercase__	: Any     		=   pipe(**self.get_dummy_inputs(SCREAMING_SNAKE_CASE_))
   lowercase__	: int     		=   output.images[0]
   lowercase__	: int     		=   image[0, -3:, -3:, -1]

   assert image.shape == (20, 32, 32, 3)

   lowercase__	: Optional[int]     		=   np.array(
       [
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
           0.0_0_0_3_9_2_1_6,
       ])

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


 def   lowercase__  (       self):


   '''simple docstring'''




   self._test_inference_batch_consistent(batch_sizes=[1, 2])


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Any     		=   torch_device == """cpu"""
   lowercase__	: Union[str, Any]     		=   True
   self._test_inference_batch_single_identical(
       batch_size=2       ,	test_max_difference=SCREAMING_SNAKE_CASE_       ,	relax_max_difference=SCREAMING_SNAKE_CASE_       ,	)



 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: List[str]     		=   self.get_dummy_components()
   lowercase__	: int     		=   self.pipeline_class(**SCREAMING_SNAKE_CASE_)
   lowercase__	: List[Any]     		=   pipe.to(SCREAMING_SNAKE_CASE_)
   pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

   lowercase__	: List[Any]     		=   1
   lowercase__	: Dict     		=   2

   lowercase__	: List[str]     		=   self.get_dummy_inputs(SCREAMING_SNAKE_CASE_)

   for key in inputs.keys():
     if key in self.batch_params:
       lowercase__	: Tuple     		=   batch_size * [inputs[key]]

   lowercase__	: Tuple     		=   pipe(**SCREAMING_SNAKE_CASE_       ,	num_images_per_prompt=SCREAMING_SNAKE_CASE_)[0]

   assert images.shape[0] == batch_size * num_images_per_prompt



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


 def   lowercase__  (       self):


   '''simple docstring'''




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



 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: List[Any]     		=   load_image(
       """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/corgi.png""")
   lowercase__	: Union[str, Any]     		=   load_numpy(
       """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
       """/shap_e/test_shap_e_img2img_out.npy""")
   lowercase__	: Optional[Any]     		=   ShapEImgaImgPipeline.from_pretrained("""openai/shap-e-img2img""")
   lowercase__	: List[str]     		=   pipe.to(SCREAMING_SNAKE_CASE_)
   pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

   lowercase__	: int     		=   torch.Generator(device=SCREAMING_SNAKE_CASE_).manual_seed(0)

   lowercase__	: Optional[int]     		=   pipe(
       SCREAMING_SNAKE_CASE_       ,	generator=SCREAMING_SNAKE_CASE_       ,	guidance_scale=3.0       ,	num_inference_steps=64       ,	frame_size=64       ,	output_type="""np"""       ,	).images[0]

   assert images.shape == (20, 64, 64, 3)

   assert_mean_pixel_difference(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_)








def 		UpperCamelCase						(   lowercase_ )		->						int:


  '''simple docstring'''






  if n == 1 or not isinstance(lowercase_			,			lowercase_ ):
    return 0
  elif n == 2:
    return 1
  else:
    lowercase__	: List[Any]     		=   [0, 1]
    for i in range(2			,			n + 1 ):
      sequence.append(sequence[i - 1] + sequence[i - 2] )

    return sequence[n]

def 		UpperCamelCase						(   lowercase_ )		->						int:


  '''simple docstring'''






  lowercase__	: Optional[Any]     		=   0
  lowercase__	: Dict     		=   2

  while digits < n:
    index += 1
    lowercase__	: str     		=   len(str(fibonacci(lowercase_ ) ) )

  return index

def 		UpperCamelCase						(   lowercase_ = 10_00 )		->						int:


  '''simple docstring'''






  return fibonacci_digits_index(lowercase_ )


if __name__ == "__main__":
     print(solution(int(str(input()).strip())))






