

"""simple docstring"""



import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import 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 					a						(       _lowerCamelCase,	unittest.TestCase				):



    """simple docstring"""



    UpperCAmelCase						 =					ShapEPipeline
    UpperCAmelCase						 =					["prompt"]
    UpperCAmelCase						 =					["prompt"]
    UpperCAmelCase						 =					[
        "num_images_per_prompt",
        "num_inference_steps",
        "generator",
        "latents",
        "guidance_scale",
        "frame_size",
        "output_type",
        "return_dict",
    ]
    UpperCAmelCase						 =					False

    @property
    def 				UpperCamelCase	(     self: Optional[Any]       ):






         """simple docstring"""


         return 32

    @property
    def 				UpperCamelCase	(     self: Union[str, Any]       ):






         """simple docstring"""


         return 32

    @property
    def 				UpperCamelCase	(     self: Tuple       ):






         """simple docstring"""


         return self.time_input_dim * 4

    @property
    def 				UpperCamelCase	(     self: Tuple       ):






         """simple docstring"""


         return 8

    @property
    def 				UpperCamelCase	(     self: int       ):






         """simple docstring"""


         A__										=  CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""       )
         return tokenizer

    @property
    def 				UpperCamelCase	(     self: Union[str, Any]       ):






         """simple docstring"""


         torch.manual_seed(0       )
         A__										=  CLIPTextConfig(
             bos_token_id=0    ,						eos_token_id=2    ,						hidden_size=self.text_embedder_hidden_size    ,						projection_dim=self.text_embedder_hidden_size    ,						intermediate_size=37    ,						layer_norm_eps=1e-0_5    ,						num_attention_heads=4    ,						num_hidden_layers=5    ,						pad_token_id=1    ,						vocab_size=10_00    ,						)
         return CLIPTextModelWithProjection(UpperCamelCase       )

    @property
    def 				UpperCamelCase	(     self: List[Any]       ):






         """simple docstring"""


         torch.manual_seed(0       )

         A__										=  {
             """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""",
             """encoder_hid_proj_type""": None,
             """added_emb_type""": None,
         }

         A__										=  PriorTransformer(**UpperCamelCase       )
         return model

    @property
    def 				UpperCamelCase	(     self: str       ):






         """simple docstring"""


         torch.manual_seed(0       )

         A__										=  {
             """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,
             ),
         }
         A__										=  ShapERenderer(**UpperCamelCase       )
         return model

    def 				UpperCamelCase	(     self: Dict       ):






         """simple docstring"""


         A__										=  self.dummy_prior
         A__										=  self.dummy_text_encoder
         A__										=  self.dummy_tokenizer
         A__										=  self.dummy_renderer

         A__										=  HeunDiscreteScheduler(
             beta_schedule="""exp"""    ,						num_train_timesteps=10_24    ,						prediction_type="""sample"""    ,						use_karras_sigmas=UpperCamelCase    ,						clip_sample=UpperCamelCase    ,						clip_sample_range=1.0    ,						)
         A__										=  {
             """prior""": prior,
             """text_encoder""": text_encoder,
             """tokenizer""": tokenizer,
             """renderer""": renderer,
             """scheduler""": scheduler,
         }

         return components

    def 				UpperCamelCase	(     self: int    ,						UpperCamelCase: Dict    ,						UpperCamelCase: List[Any]=0       ):






         """simple docstring"""


         if str(UpperCamelCase       ).startswith("""mps"""       ):
              A__										=  torch.manual_seed(UpperCamelCase       )
         else:
              A__										=  torch.Generator(device=UpperCamelCase       ).manual_seed(UpperCamelCase       )
         A__										=  {
             """prompt""": """horse""",
             """generator""": generator,
             """num_inference_steps""": 1,
             """frame_size""": 32,
             """output_type""": """np""",
         }
         return inputs

    def 				UpperCamelCase	(     self: List[Any]       ):






         """simple docstring"""


         A__										=  """cpu"""

         A__										=  self.get_dummy_components()

         A__										=  self.pipeline_class(**UpperCamelCase       )
         A__										=  pipe.to(UpperCamelCase       )

         pipe.set_progress_bar_config(disable=UpperCamelCase       )

         A__										=  pipe(**self.get_dummy_inputs(UpperCamelCase       )       )
         A__										=  output.images[0]
         A__										=  image[0, -3:, -3:, -1]

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

         A__										=  np.array(
             [
                 0.00_039_216,
                 0.00_039_216,
                 0.00_039_216,
                 0.00_039_216,
                 0.00_039_216,
                 0.00_039_216,
                 0.00_039_216,
                 0.00_039_216,
                 0.00_039_216,
             ]       )

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

    def 				UpperCamelCase	(     self: List[str]       ):






         """simple docstring"""


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

    def 				UpperCamelCase	(     self: int       ):






         """simple docstring"""


         A__										=  torch_device == """cpu"""
         A__										=  True

         self._test_inference_batch_single_identical(
             batch_size=2    ,						test_max_difference=UpperCamelCase    ,						relax_max_difference=UpperCamelCase    ,						)





    def 				UpperCamelCase	(     self: Optional[Any]       ):






         """simple docstring"""


         A__										=  self.get_dummy_components()
         A__										=  self.pipeline_class(**UpperCamelCase       )
         A__										=  pipe.to(UpperCamelCase       )
         pipe.set_progress_bar_config(disable=UpperCamelCase       )

         A__										=  1
         A__										=  2

         A__										=  self.get_dummy_inputs(UpperCamelCase       )

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

         A__										=  pipe(**UpperCamelCase    ,						num_images_per_prompt=UpperCamelCase       )[0]

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



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



    """simple docstring"""



    def 				UpperCamelCase	(     self: Any       ):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: int       ):






         """simple docstring"""


         A__										=  load_numpy(
             """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
             """/shap_e/test_shap_e_np_out.npy"""       )
         A__										=  ShapEPipeline.from_pretrained("""openai/shap-e"""       )
         A__										=  pipe.to(UpperCamelCase       )
         pipe.set_progress_bar_config(disable=UpperCamelCase       )

         A__										=  torch.Generator(device=UpperCamelCase       ).manual_seed(0       )

         A__										=  pipe(
             """a shark"""    ,						generator=UpperCamelCase    ,						guidance_scale=15.0    ,						num_inference_steps=64    ,						frame_size=64    ,						output_type="""np"""    ,						).images[0]

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

         assert_mean_pixel_difference(UpperCamelCase    ,						UpperCamelCase       )









"""simple docstring"""



def  _snake_case     (    UpperCAmelCase_  :					list			):
     if len(UpperCAmelCase_			) <= 1:
          return lst

     A__										=  1

     while i < len(UpperCAmelCase_			):
          if lst[i - 1] <= lst[i]:
               i += 1
          else:
               A__			,				A__										=  lst[i], lst[i - 1]
               i -= 1
               if i == 0:
                    A__										=  1

     return lst


if __name__ == "__main__":
 SCREAMING_SNAKE_CASE_					:	Union[str, Any]							  =						input('Enter numbers separated by a comma:\n').strip()
 SCREAMING_SNAKE_CASE_					:	Union[str, Any]							  =						[int(item) for item in user_input.split(',')]
 print(gnome_sort(unsorted))






