



import math
from dataclasses import dataclass
from typing import List, Optional, Union

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

from ...models import PriorTransformer
from ...pipelines import DiffusionPipeline
from ...schedulers import HeunDiscreteScheduler
from ...utils import (
    BaseOutput,
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .renderer import ShapERenderer


__SCREAMING_SNAKE_CASE     =	logging.get_logger(__name__)  # pylint: disable=invalid-name

__SCREAMING_SNAKE_CASE     =	"""\n    Examples:\n        ```py\n        >>> import torch\n        >>> from diffusers import DiffusionPipeline\n        >>> from diffusers.utils import export_to_gif\n\n        >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n        >>> repo = \"openai/shap-e\"\n        >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n        >>> pipe = pipe.to(device)\n\n        >>> guidance_scale = 15.0\n        >>> prompt = \"a shark\"\n\n        >>> images = pipe(\n        ...     prompt,\n        ...     guidance_scale=guidance_scale,\n        ...     num_inference_steps=64,\n        ...     frame_size=256,\n        ... ).images\n\n        >>> gif_path = export_to_gif(images[0], \"shark_3d.gif\")\n        ```\n"""

@dataclass
class 						lowerCamelCase_     (			__a    ):






						'''simple docstring'''
						a__ =	42







class 						lowerCamelCase_     (			__a    ):






						'''simple docstring'''







						def __init__(						self   :      str						,						__lowerCamelCase   :      PriorTransformer						,						__lowerCamelCase   :      CLIPTextModelWithProjection						,						__lowerCamelCase   :      CLIPTokenizer						,						__lowerCamelCase   :      HeunDiscreteScheduler						,						__lowerCamelCase   :      ShapERenderer						,						)   ->					str:
									super().__init__()

									self.register_modules(
									    prior=__lowerCamelCase						,						text_encoder=__lowerCamelCase						,						tokenizer=__lowerCamelCase						,						scheduler=__lowerCamelCase						,						renderer=__lowerCamelCase						,						)







						def       SCREAMING_SNAKE_CASE__		(						self   :      Optional[Any]						,						__lowerCamelCase   :      Optional[Any]						,						__lowerCamelCase   :      List[str]						,						__lowerCamelCase   :      Dict						,						__lowerCamelCase   :      List[str]						,						__lowerCamelCase   :      int						,						__lowerCamelCase   :      Tuple			)   ->					Any:

									if latents is None:
												A				:       Tuple														=    randn_tensor(__lowerCamelCase						,						generator=__lowerCamelCase						,						device=__lowerCamelCase						,						dtype=__lowerCamelCase			)
									else:
												if latents.shape != shape:
															raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}"""			)
												A				:       Tuple														=    latents.to(__lowerCamelCase			)

									A				:       Union[str, Any]														=    latents * scheduler.init_noise_sigma
									return latents







						def       SCREAMING_SNAKE_CASE__		(						self   :      Union[str, Any]						,						__lowerCamelCase   :      List[Any]=0			)   ->					int:

									if is_accelerate_available():
												from accelerate import cpu_offload
									else:
												raise ImportError("Please install accelerate via `pip install accelerate`"			)

									A				:       Optional[Any]														=    torch.device(F"""cuda:{gpu_id}"""			)

									A				:       int														=    [self.text_encoder, self.prior]
									for cpu_offloaded_model in models:
												if cpu_offloaded_model is not None:
															cpu_offload(__lowerCamelCase						,						__lowerCamelCase			)







						def       SCREAMING_SNAKE_CASE__		(						self   :      Any						,						__lowerCamelCase   :      List[Any]=0			)   ->					List[str]:

									if is_accelerate_available() and is_accelerate_version(">="						,						"0.17.0.dev0"			):
												from accelerate import cpu_offload_with_hook
									else:
												raise ImportError("`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."			)

									A				:       Union[str, Any]														=    torch.device(F"""cuda:{gpu_id}"""			)

									if self.device.type != "cpu":
												self.to("cpu"						,						silence_dtype_warnings=__lowerCamelCase			)
												torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

									A				:       Union[str, Any]														=    None
									for cpu_offloaded_model in [self.text_encoder, self.prior, self.renderer]:
												A							,			A				:       Tuple														=    cpu_offload_with_hook(__lowerCamelCase						,						__lowerCamelCase						,						prev_module_hook=__lowerCamelCase			)

									if self.safety_checker is not None:
												A							,			A				:       Union[str, Any]														=    cpu_offload_with_hook(self.safety_checker						,						__lowerCamelCase						,						prev_module_hook=__lowerCamelCase			)

									# We'll offload the last model manually.
									A				:       Union[str, Any]														=    hook







						@property
						def       SCREAMING_SNAKE_CASE__		(						self   :      Dict			)   ->					Union[str, Any]:

									if self.device != torch.device("meta"			) or not hasattr(self.text_encoder						,						"_hf_hook"			):
												return self.device
									for module in self.text_encoder.modules():
												if (
												    hasattr(__lowerCamelCase						,						"_hf_hook"			)
												    and hasattr(module._hf_hook						,						"execution_device"			)
												    and module._hf_hook.execution_device is not None
												):
															return torch.device(module._hf_hook.execution_device			)
									return self.device







						def       SCREAMING_SNAKE_CASE__		(						self   :      Union[str, Any]						,						__lowerCamelCase   :      Any						,						__lowerCamelCase   :      Tuple						,						__lowerCamelCase   :      List[str]						,						__lowerCamelCase   :      Any						,						)   ->					List[Any]:
									len(__lowerCamelCase			) if isinstance(__lowerCamelCase						,						__lowerCamelCase			) else 1

									# YiYi Notes: set pad_token_id to be 0, not sure why I can't set in the config file
									A				:       Tuple														=    0
									# get prompt text embeddings
									A				:       int														=    self.tokenizer(
									    __lowerCamelCase						,						padding="max_length"						,						max_length=self.tokenizer.model_max_length						,						truncation=__lowerCamelCase						,						return_tensors="pt"						,						)
									A				:       Union[str, Any]														=    text_inputs.input_ids
									A				:       Optional[int]														=    self.tokenizer(__lowerCamelCase						,						padding="longest"						,						return_tensors="pt"			).input_ids

									if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(__lowerCamelCase						,						__lowerCamelCase			):
												A				:       Any														=    self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]			)
												logger.warning(
												    "The following part of your input was truncated because CLIP can only handle sequences up to"
												    F""" {self.tokenizer.model_max_length} tokens: {removed_text}"""			)

									A				:       List[Any]														=    self.text_encoder(text_input_ids.to(__lowerCamelCase			)			)
									A				:       List[str]														=    text_encoder_output.text_embeds

									A				:       Union[str, Any]														=    prompt_embeds.repeat_interleave(__lowerCamelCase						,						dim=0			)
									# in Shap-E it normalize the prompt_embeds and then later rescale it
									A				:       str														=    prompt_embeds / torch.linalg.norm(__lowerCamelCase						,						dim=-1						,						keepdim=__lowerCamelCase			)

									if do_classifier_free_guidance:
												A				:       List[str]														=    torch.zeros_like(__lowerCamelCase			)

												# For classifier free guidance, we need to do two forward passes.
												# Here we concatenate the unconditional and text embeddings into a single batch
												# to avoid doing two forward passes
												A				:       List[Any]														=    torch.cat([negative_prompt_embeds, prompt_embeds]			)

									# Rescale the features to have unit variance
									A				:       Optional[Any]														=    math.sqrt(prompt_embeds.shape[1]			) * prompt_embeds

									return prompt_embeds







						@torch.no_grad()
						@replace_example_docstring(__lowerCamelCase			)
						def __call__(						self   :      Union[str, Any]						,						__lowerCamelCase   :      str						,						__lowerCamelCase   :      int = 1						,						__lowerCamelCase   :      int = 25						,						__lowerCamelCase   :      Optional[Union[torch.Generator, List[torch.Generator]]] = None						,						__lowerCamelCase   :      Optional[torch.FloatTensor] = None						,						__lowerCamelCase   :      float = 4.0						,						__lowerCamelCase   :      int = 64						,						__lowerCamelCase   :      Optional[str] = "pil"						,						__lowerCamelCase   :      bool = True						,						)   ->					Any:

									if isinstance(__lowerCamelCase						,						__lowerCamelCase			):
												A				:       Dict														=    1
									elif isinstance(__lowerCamelCase						,						__lowerCamelCase			):
												A				:       int														=    len(__lowerCamelCase			)
									else:
												raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(__lowerCamelCase			)}"""			)

									A				:       Optional[Any]														=    self._execution_device

									A				:       List[Any]														=    batch_size * num_images_per_prompt

									A				:       Optional[int]														=    guidance_scale > 1.0
									A				:       Union[str, Any]														=    self._encode_prompt(__lowerCamelCase						,						__lowerCamelCase						,						__lowerCamelCase						,						__lowerCamelCase			)

									# prior

									self.scheduler.set_timesteps(__lowerCamelCase						,						device=__lowerCamelCase			)
									A				:       Tuple														=    self.scheduler.timesteps

									A				:       Tuple														=    self.prior.config.num_embeddings
									A				:       int														=    self.prior.config.embedding_dim

									A				:       Union[str, Any]														=    self.prepare_latents(
									    (batch_size, num_embeddings * embedding_dim)						,						prompt_embeds.dtype						,						__lowerCamelCase						,						__lowerCamelCase						,						__lowerCamelCase						,						self.scheduler						,						)

									# YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim
									A				:       str														=    latents.reshape(latents.shape[0]						,						__lowerCamelCase						,						__lowerCamelCase			)

									for i, t in enumerate(self.progress_bar(__lowerCamelCase			)			):
												# expand the latents if we are doing classifier free guidance
												A				:       Union[str, Any]														=    torch.cat([latents] * 2			) if do_classifier_free_guidance else latents
												A				:       Optional[Any]														=    self.scheduler.scale_model_input(__lowerCamelCase						,						__lowerCamelCase			)

												A				:       Tuple														=    self.prior(
												    __lowerCamelCase						,						timestep=__lowerCamelCase						,						proj_embedding=__lowerCamelCase						,						).predicted_image_embedding

												# remove the variance
												A							,			A				:       Tuple														=    noise_pred.split(
												    scaled_model_input.shape[2]						,						dim=2			)  # batch_size, num_embeddings, embedding_dim

												if do_classifier_free_guidance is not None:
															A							,			A				:       Dict														=    noise_pred.chunk(2			)
															A				:       Tuple														=    noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)

												A				:       List[str]														=    self.scheduler.step(
												    __lowerCamelCase						,						timestep=__lowerCamelCase						,						sample=__lowerCamelCase						,						).prev_sample

									if output_type == "latent":
												return ShapEPipelineOutput(images=__lowerCamelCase			)

									A				:       Union[str, Any]														=    []
									for i, latent in enumerate(__lowerCamelCase			):
												A				:       List[Any]														=    self.renderer.decode(
												    latent[None, :]						,						__lowerCamelCase						,						size=__lowerCamelCase						,						ray_batch_size=40_96						,						n_coarse_samples=64						,						n_fine_samples=1_28						,						)
												images.append(__lowerCamelCase			)

									A				:       Union[str, Any]														=    torch.stack(__lowerCamelCase			)

									if output_type not in ["np", "pil"]:
												raise ValueError(F"""Only the output types `pil` and `np` are supported not output_type={output_type}"""			)

									A				:       str														=    images.cpu().numpy()

									if output_type == "pil":
												A				:       List[str]														=    [self.numpy_to_pil(__lowerCamelCase			) for image in images]

									# Offload last model to CPU
									if hasattr(self						,						"final_offload_hook"			) and self.final_offload_hook is not None:
												self.final_offload_hook.offload()

									if not return_dict:
												return (images,)

									return ShapEPipelineOutput(images=__lowerCamelCase			)





'''simple docstring'''







import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
   from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import PoolFormerImageProcessor


class      _a    (      __a					):
       def        A (      self		:      Tuple							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.config_class(**self.inputs_dict							)
           self.parent.assertTrue(hasattr(lowercase  ,							'''hidden_sizes'''							)							)
           self.parent.assertTrue(hasattr(lowercase  ,							'''num_encoder_blocks'''							)							)






class      _a    :
       def __init__(      self		:      Any  ,							lowercase		:      Any  ,							lowercase		:      Optional[Any]=13  ,							lowercase		:      List[str]=64  ,							lowercase		:      Optional[Any]=3  ,							lowercase		:      List[str]=4  ,							lowercase		:      Optional[int]=[2, 2, 2, 2]  ,							lowercase		:      Tuple=[8, 4, 2, 1]  ,							lowercase		:      Optional[int]=[16, 32, 64, 128]  ,							lowercase		:      Tuple=[1, 4, 8, 16]  ,							lowercase		:      List[Any]=False  ,							lowercase		:      List[Any]=True  ,							lowercase		:      List[str]="gelu"  ,							lowercase		:      Any=0.1  ,							lowercase		:      Dict=0.02  ,							lowercase		:      str=3  ,							lowercase		:      Optional[Any]=None  ,							):



           '''simple docstring'''

           UpperCAmelCase     			=						parent
           UpperCAmelCase     			=						batch_size
           UpperCAmelCase     			=						image_size
           UpperCAmelCase     			=						num_channels
           UpperCAmelCase     			=						num_encoder_blocks
           UpperCAmelCase     			=						sr_ratios
           UpperCAmelCase     			=						depths
           UpperCAmelCase     			=						hidden_sizes
           UpperCAmelCase     			=						downsampling_rates
           UpperCAmelCase     			=						is_training
           UpperCAmelCase     			=						use_labels
           UpperCAmelCase     			=						hidden_act
           UpperCAmelCase     			=						hidden_dropout_prob
           UpperCAmelCase     			=						initializer_range
           UpperCAmelCase     			=						num_labels
           UpperCAmelCase     			=						scope
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

           UpperCAmelCase     			=						None
           if self.use_labels:
               UpperCAmelCase     			=						ids_tensor([self.batch_size, self.image_size, self.image_size]  ,							self.num_labels							)

           UpperCAmelCase     			=						PoolFormerConfig(
               image_size=self.image_size  ,							num_channels=self.num_channels  ,							num_encoder_blocks=self.num_encoder_blocks  ,							depths=self.depths  ,							hidden_sizes=self.hidden_sizes  ,							hidden_act=self.hidden_act  ,							hidden_dropout_prob=self.hidden_dropout_prob  ,							initializer_range=self.initializer_range  ,							)

           return config, pixel_values, labels
       def        A (      self		:      Optional[Any]  ,							lowercase		:      List[Any]  ,							lowercase		:      Dict  ,							lowercase		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerModel(config=lowercase							)
           model.to(lowercase							)
           model.eval()
           UpperCAmelCase     			=						model(lowercase							)
           UpperCAmelCase     			=						UpperCAmelCase     			=						self.image_size // 32.0
           self.parent.assertEqual(
               result.last_hidden_state.shape  ,							(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)							)







       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.prepare_config_and_inputs()
           UpperCAmelCase			,	UpperCAmelCase			,	UpperCAmelCase     			=						config_and_inputs
           UpperCAmelCase     			=						{'''pixel_values''': pixel_values}
           return config, inputs_dict






@require_torch
class      _a    (      __a				,				__a				,				unittest.TestCase					):
       __a							:	Optional[int]  							=   (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
       __a							:	Dict  							=   (
           {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       __a							:	str  							=   False
       __a							:	Optional[int]  							=   False
       __a							:	Tuple  							=   False
       __a							:	Union[str, Any]  							=   False
       __a							:	Dict  							=   False
       def        A (      self		:      Any							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerModelTester(self							)
           UpperCAmelCase     			=						PoolFormerConfigTester(self  ,							config_class=lowercase							)
       def        A (      self		:      Any							):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def        A (      self		:      Optional[int]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase							)
       @unittest.skip('''PoolFormer does not use inputs_embeds'''							)
       def        A (      self		:      Dict							):



           '''simple docstring'''

           pass
       @unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''							)
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           pass
       def        A (      self		:      Dict							):



           '''simple docstring'''

           def check_hidden_states_output(lowercase		:      int  ,							lowercase		:      Any  ,							lowercase		:      Union[str, Any]							):
               UpperCAmelCase     			=						model_class(lowercase							)
               model.to(lowercase							)
               model.eval()

               with torch.no_grad():
                   UpperCAmelCase     			=						model(**self._prepare_for_class(lowercase  ,							lowercase							)							)

               UpperCAmelCase     			=						outputs.hidden_states

               UpperCAmelCase     			=						self.model_tester.num_encoder_blocks
               self.assertEqual(len(lowercase							)  ,							lowercase							)

               # verify the first hidden states (first block)
               self.assertListEqual(
                   list(hidden_states[0].shape[-3:]							)  ,							[
                       self.model_tester.hidden_sizes[0],
                       self.model_tester.image_size // 4,
                       self.model_tester.image_size // 4,
                   ]  ,							)

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						True
               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               UpperCAmelCase     			=						True

               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)
       def        A (      self		:      Optional[Any]							):



           '''simple docstring'''

           if not self.model_tester.is_training:
               return

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()
           UpperCAmelCase     			=						True

           for model_class in self.all_model_classes:
               if model_class in get_values(lowercase							):
                   continue
               UpperCAmelCase     			=						model_class(lowercase							)
               model.to(lowercase							)
               model.train()
               UpperCAmelCase     			=						self._prepare_for_class(lowercase  ,							lowercase  ,							return_labels=lowercase							)
               UpperCAmelCase     			=						model(**lowercase							).loss
               loss.backward()
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						model_class(lowercase							)
               UpperCAmelCase     			=						inspect.signature(model.forward							)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               UpperCAmelCase     			=						[*signature.parameters.keys()]

               UpperCAmelCase     			=						['''pixel_values''']
               self.assertListEqual(arg_names[:1]  ,							lowercase							)







       @slow
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase     			=						PoolFormerModel.from_pretrained(lowercase							)
               self.assertIsNotNone(lowercase							)






def 		snake_case_				():
    UpperCAmelCase     			=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
    return image







@require_torch
class      _a    (      unittest.TestCase					):
       @slow
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerImageProcessor()
           UpperCAmelCase     			=						PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''							).to(lowercase							)

           UpperCAmelCase     			=						image_processor(images=prepare_img()  ,							return_tensors='''pt'''							).to(lowercase							)

           # forward pass
           with torch.no_grad():
               UpperCAmelCase     			=						model(**lowercase							)

           # verify the logits
           UpperCAmelCase     			=						torch.Size((1, 1_000)							)
           self.assertEqual(outputs.logits.shape  ,							lowercase							)

           UpperCAmelCase     			=						torch.tensor([-0.6113, 0.1685, -0.0492]							).to(lowercase							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,							lowercase  ,							atol=1E-4							)							)




