code: 
  
  
  import collections
  import inspect
  import unittest
  
  from transformers import SwinConfig
  from transformers.testing_utils import require_torch, require_vision, slow, torch_device
  from transformers.utils import cached_property, is_torch_available, is_vision_available
  
  from ...test_backbone_common import BackboneTesterMixin
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
  from ...test_pipeline_mixin import PipelineTesterMixin
  
  
  if is_torch_available():
   import torch
   from torch import nn
  
   from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
   from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST
  
  
  if is_vision_available():
   from PIL import Image
  
   from transformers import AutoImageProcessor
  
  
  class 		_SCREAMING_SNAKE_CASE    :
  
  
       def __init__(     self       ,				A_       ,				A_=13       ,				A_=32       ,				A_=2       ,				A_=3       ,				A_=16       ,				A_=[1, 2, 1]       ,				A_=[2, 2, 4]       ,				A_=2       ,				A_=2.0       ,				A_=True       ,				A_=0.0       ,				A_=0.0       ,				A_=0.1       ,				A_="gelu"       ,				A_=False       ,				A_=True       ,				A_=0.0_2       ,				A_=1e-5       ,				A_=True       ,				A_=None       ,				A_=True       ,				A_=10       ,				A_=8       ,				A_=["stage1", "stage2"]       ,				A_=[1, 2]       ,				):
         _UpperCAmelCase		:       int       =    parent
         _UpperCAmelCase		:       Union[str, Any]       =    batch_size
         _UpperCAmelCase		:       Dict       =    image_size
         _UpperCAmelCase		:       Union[str, Any]       =    patch_size
         _UpperCAmelCase		:       Union[str, Any]       =    num_channels
         _UpperCAmelCase		:       Dict       =    embed_dim
         _UpperCAmelCase		:       Union[str, Any]       =    depths
         _UpperCAmelCase		:       str       =    num_heads
         _UpperCAmelCase		:       Any       =    window_size
         _UpperCAmelCase		:       Optional[Any]       =    mlp_ratio
         _UpperCAmelCase		:       Any       =    qkv_bias
         _UpperCAmelCase		:       str       =    hidden_dropout_prob
         _UpperCAmelCase		:       List[Any]       =    attention_probs_dropout_prob
         _UpperCAmelCase		:       Tuple       =    drop_path_rate
         _UpperCAmelCase		:       List[str]       =    hidden_act
         _UpperCAmelCase		:       Optional[int]       =    use_absolute_embeddings
         _UpperCAmelCase		:       int       =    patch_norm
         _UpperCAmelCase		:       Dict       =    layer_norm_eps
         _UpperCAmelCase		:       Any       =    initializer_range
         _UpperCAmelCase		:       List[Any]       =    is_training
         _UpperCAmelCase		:       str       =    scope
         _UpperCAmelCase		:       Any       =    use_labels
         _UpperCAmelCase		:       List[Any]       =    type_sequence_label_size
         _UpperCAmelCase		:       str       =    encoder_stride
         _UpperCAmelCase		:       Dict       =    out_features
         _UpperCAmelCase		:       Optional[Any]       =    out_indices
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       List[str]       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)
  
         _UpperCAmelCase		:       List[str]       =    None
         if self.use_labels:
           _UpperCAmelCase		:       str       =    ids_tensor([self.batch_size]       ,				self.type_sequence_label_size						)
  
         _UpperCAmelCase		:       Optional[Any]       =    self.get_config()
  
         return config, pixel_values, labels
  
  
       def 					__snake_case(     self						):
         return SwinConfig(
             image_size=self.image_size       ,				patch_size=self.patch_size       ,				num_channels=self.num_channels       ,				embed_dim=self.embed_dim       ,				depths=self.depths       ,				num_heads=self.num_heads       ,				window_size=self.window_size       ,				mlp_ratio=self.mlp_ratio       ,				qkv_bias=self.qkv_bias       ,				hidden_dropout_prob=self.hidden_dropout_prob       ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,				drop_path_rate=self.drop_path_rate       ,				hidden_act=self.hidden_act       ,				use_absolute_embeddings=self.use_absolute_embeddings       ,				path_norm=self.patch_norm       ,				layer_norm_eps=self.layer_norm_eps       ,				initializer_range=self.initializer_range       ,				encoder_stride=self.encoder_stride       ,				out_features=self.out_features       ,				out_indices=self.out_indices       ,				)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_       ,				A_						):
         _UpperCAmelCase		:       Optional[Any]       =    SwinModel(config=A_						)
         model.to(A_						)
         model.eval()
         _UpperCAmelCase		:       str       =    model(A_						)
  
         _UpperCAmelCase		:       Union[str, Any]       =    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
         _UpperCAmelCase		:       Any       =    int(config.embed_dim * 2 ** (len(config.depths						) - 1)						)
  
         self.parent.assertEqual(result.last_hidden_state.shape       ,				(self.batch_size, expected_seq_len, expected_dim)						)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_       ,				A_						):
         _UpperCAmelCase		:       str       =    SwinBackbone(config=A_						)
         model.to(A_						)
         model.eval()
         _UpperCAmelCase		:       Dict       =    model(A_						)
  
         # verify hidden states
         self.parent.assertEqual(len(result.feature_maps						)       ,				len(config.out_features						)						)
         self.parent.assertListEqual(list(result.feature_maps[0].shape						)       ,				[self.batch_size, model.channels[0], 16, 16]						)
  
         # verify channels
         self.parent.assertEqual(len(model.channels						)       ,				len(config.out_features						)						)
  
         # verify backbone works with out_features=None
         _UpperCAmelCase		:       List[str]       =    None
         _UpperCAmelCase		:       Tuple       =    SwinBackbone(config=A_						)
         model.to(A_						)
         model.eval()
         _UpperCAmelCase		:       int       =    model(A_						)
  
         # verify feature maps
         self.parent.assertEqual(len(result.feature_maps						)       ,				1						)
         self.parent.assertListEqual(list(result.feature_maps[0].shape						)       ,				[self.batch_size, model.channels[-1], 4, 4]						)
  
         # verify channels
         self.parent.assertEqual(len(model.channels						)       ,				1						)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_       ,				A_						):
         _UpperCAmelCase		:       Dict       =    SwinForMaskedImageModeling(config=A_						)
         model.to(A_						)
         model.eval()
         _UpperCAmelCase		:       Optional[Any]       =    model(A_						)
         self.parent.assertEqual(
             result.logits.shape       ,				(self.batch_size, self.num_channels, self.image_size, self.image_size)						)
  
         # test greyscale images
         _UpperCAmelCase		:       Optional[int]       =    1
         _UpperCAmelCase		:       Tuple       =    SwinForMaskedImageModeling(A_						)
         model.to(A_						)
         model.eval()
  
         _UpperCAmelCase		:       List[Any]       =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
         _UpperCAmelCase		:       Dict       =    model(A_						)
         self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, 1, self.image_size, self.image_size)						)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_       ,				A_						):
         _UpperCAmelCase		:       int       =    self.type_sequence_label_size
         _UpperCAmelCase		:       str       =    SwinForImageClassification(A_						)
         model.to(A_						)
         model.eval()
         _UpperCAmelCase		:       Union[str, Any]       =    model(A_       ,				labels=A_						)
         self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.type_sequence_label_size)						)
  
         # test greyscale images
         _UpperCAmelCase		:       Dict       =    1
         _UpperCAmelCase		:       Optional[int]       =    SwinForImageClassification(A_						)
         model.to(A_						)
         model.eval()
  
         _UpperCAmelCase		:       int       =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
         _UpperCAmelCase		:       Tuple       =    model(A_						)
         self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.type_sequence_label_size)						)
  
  
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       Optional[int]       =    self.prepare_config_and_inputs()
         (
             (
             _UpperCAmelCase
         ),(
             _UpperCAmelCase
         ),(
             _UpperCAmelCase
         ),
         )		:       str       =    config_and_inputs
         _UpperCAmelCase		:       Union[str, Any]       =    {"""pixel_values""": pixel_values}
         return config, inputs_dict
  
  
  @require_torch
  class 		_SCREAMING_SNAKE_CASE    (	A       ,  A       ,  unittest.TestCase    ):
       __SCREAMING_SNAKE_CASE    				=						(
           (
               SwinModel,
               SwinBackbone,
               SwinForImageClassification,
               SwinForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       __SCREAMING_SNAKE_CASE    				=						(
           {'''feature-extraction''': SwinModel, '''image-classification''': SwinForImageClassification}
           if is_torch_available()
           else {}
       )
       __SCREAMING_SNAKE_CASE    				=						True
  
       __SCREAMING_SNAKE_CASE    				=						False
       __SCREAMING_SNAKE_CASE    				=						False
       __SCREAMING_SNAKE_CASE    				=						False
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       Tuple       =    SwinModelTester(self						)
         _UpperCAmelCase		:       Union[str, Any]       =    ConfigTester(self       ,				config_class=A_       ,				embed_dim=37						)
  
  
       def 					__snake_case(     self						):
         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()
  
  
       def 					__snake_case(     self						):
         return
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       Any       =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A_						)
  
  
       @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""						)
       def 					__snake_case(     self						):
         pass
  
  
       def 					__snake_case(     self						):
         super().test_training_gradient_checkpointing()
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       Any       =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*A_						)
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       Union[str, Any]       =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*A_						)
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       List[Any]       =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*A_						)
  
  
       @unittest.skip(reason="""Swin does not use inputs_embeds"""						)
       def 					__snake_case(     self						):
         pass
  
  
       @unittest.skip(reason="""Swin Transformer does not use feedforward chunking"""						)
       def 					__snake_case(     self						):
         pass
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase,_UpperCAmelCase		:       List[str]       =    self.model_tester.prepare_config_and_inputs_for_common()
  
         for model_class in self.all_model_classes:
           _UpperCAmelCase		:       Tuple       =    model_class(A_						)
           self.assertIsInstance(model.get_input_embeddings()       ,				(nn.Module)						)
           _UpperCAmelCase		:       Optional[int]       =    model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(A_       ,				nn.Linear						)						)
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase,_UpperCAmelCase		:       Optional[int]       =    self.model_tester.prepare_config_and_inputs_for_common()
  
         for model_class in self.all_model_classes:
           _UpperCAmelCase		:       List[str]       =    model_class(A_						)
           _UpperCAmelCase		:       List[Any]       =    inspect.signature(model.forward						)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           _UpperCAmelCase		:       List[Any]       =    [*signature.parameters.keys()]
  
           _UpperCAmelCase		:       List[Any]       =    ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]       ,				A_						)
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase,_UpperCAmelCase		:       Any       =    self.model_tester.prepare_config_and_inputs_for_common()
         _UpperCAmelCase		:       Dict       =    True
  
         for model_class in self.all_model_classes:
           _UpperCAmelCase		:       Dict       =    True
           _UpperCAmelCase		:       Optional[Any]       =    False
           _UpperCAmelCase		:       Optional[Any]       =    True
           _UpperCAmelCase		:       Dict       =    model_class(A_						)
           model.to(A_						)
           model.eval()
           with torch.no_grad():
             _UpperCAmelCase		:       Optional[int]       =    model(**self._prepare_for_class(A_       ,				A_						)						)
           _UpperCAmelCase		:       Optional[Any]       =    outputs.attentions
           _UpperCAmelCase		:       Any       =    len(self.model_tester.depths						)
           self.assertEqual(len(A_						)       ,				A_						)
  
           # check that output_attentions also work using config
           del inputs_dict["output_attentions"]
           _UpperCAmelCase		:       str       =    True
           _UpperCAmelCase		:       Union[str, Any]       =    config.window_size**2
           _UpperCAmelCase		:       Optional[int]       =    model_class(A_						)
           model.to(A_						)
           model.eval()
           with torch.no_grad():
             _UpperCAmelCase		:       Optional[int]       =    model(**self._prepare_for_class(A_       ,				A_						)						)
           _UpperCAmelCase		:       Dict       =    outputs.attentions
           self.assertEqual(len(A_						)       ,				A_						)
  
           self.assertListEqual(
               list(attentions[0].shape[-3:]						)       ,				[self.model_tester.num_heads[0], window_size_squared, window_size_squared]       ,				)
           _UpperCAmelCase		:       str       =    len(A_						)
  
           # Check attention is always last and order is fine
           _UpperCAmelCase		:       List[Any]       =    True
           _UpperCAmelCase		:       Dict       =    True
           _UpperCAmelCase		:       Union[str, Any]       =    model_class(A_						)
           model.to(A_						)
           model.eval()
           with torch.no_grad():
             _UpperCAmelCase		:       List[str]       =    model(**self._prepare_for_class(A_       ,				A_						)						)
  
           # also another +1 for reshaped_hidden_states
           _UpperCAmelCase		:       Any       =    1 if model_class.__name__ == """SwinBackbone""" else 2
           self.assertEqual(out_len + added_hidden_states       ,				len(A_						)						)
  
           _UpperCAmelCase		:       str       =    outputs.attentions
  
           self.assertEqual(len(A_						)       ,				A_						)
  
           self.assertListEqual(
               list(self_attentions[0].shape[-3:]						)       ,				[self.model_tester.num_heads[0], window_size_squared, window_size_squared]       ,				)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_						):
         _UpperCAmelCase		:       Optional[Any]       =    model_class(A_						)
         model.to(A_						)
         model.eval()
  
         with torch.no_grad():
           _UpperCAmelCase		:       str       =    model(**self._prepare_for_class(A_       ,				A_						)						)
  
         _UpperCAmelCase		:       Optional[Any]       =    outputs.hidden_states
  
         _UpperCAmelCase		:       int       =    getattr(
             self.model_tester       ,				"""expected_num_hidden_layers"""       ,				len(self.model_tester.depths						) + 1						)
         self.assertEqual(len(A_						)       ,				A_						)
  
         # Swin has a different seq_length
         _UpperCAmelCase		:       List[str]       =    (
             config.patch_size
             if isinstance(config.patch_size       ,				collections.abc.Iterable						)
             else (config.patch_size, config.patch_size)
         )
  
         _UpperCAmelCase		:       Tuple       =    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
  
         self.assertListEqual(
             list(hidden_states[0].shape[-2:]						)       ,				[num_patches, self.model_tester.embed_dim]       ,				)
  
         if not model_class.__name__ == "SwinBackbone":
           _UpperCAmelCase		:       List[str]       =    outputs.reshaped_hidden_states
           self.assertEqual(len(A_						)       ,				A_						)
  
           _UpperCAmelCase,_UpperCAmelCase,_UpperCAmelCase,_UpperCAmelCase		:       Any       =    reshaped_hidden_states[0].shape
           _UpperCAmelCase		:       Union[str, Any]       =    (
               reshaped_hidden_states[0].view(A_       ,				A_       ,				height * width						).permute(0       ,				2       ,				1						)
           )
           self.assertListEqual(
               list(reshaped_hidden_states.shape[-2:]						)       ,				[num_patches, self.model_tester.embed_dim]       ,				)
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase,_UpperCAmelCase		:       Any       =    self.model_tester.prepare_config_and_inputs_for_common()
  
         _UpperCAmelCase		:       List[Any]       =    (
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size       ,				collections.abc.Iterable						)
             else (self.model_tester.image_size, self.model_tester.image_size)
         )
  
         for model_class in self.all_model_classes:
           _UpperCAmelCase		:       Any       =    True
           self.check_hidden_states_output(A_       ,				A_       ,				A_       ,				A_						)
  
           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           _UpperCAmelCase		:       Optional[int]       =    True
  
           self.check_hidden_states_output(A_       ,				A_       ,				A_       ,				A_						)
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase,_UpperCAmelCase		:       Optional[Any]       =    self.model_tester.prepare_config_and_inputs_for_common()
         _UpperCAmelCase		:       Optional[Any]       =    3
  
         _UpperCAmelCase		:       Dict       =    (
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size       ,				collections.abc.Iterable						)
             else (self.model_tester.image_size, self.model_tester.image_size)
         )
         _UpperCAmelCase		:       Optional[Any]       =    (
             config.patch_size
             if isinstance(config.patch_size       ,				collections.abc.Iterable						)
             else (config.patch_size, config.patch_size)
         )
  
         _UpperCAmelCase		:       Dict       =    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
         _UpperCAmelCase		:       Optional[int]       =    image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])
  
         for model_class in self.all_model_classes:
           _UpperCAmelCase		:       Optional[Any]       =    True
           self.check_hidden_states_output(A_       ,				A_       ,				A_       ,				(padded_height, padded_width)						)
  
           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           _UpperCAmelCase		:       List[Any]       =    True
           self.check_hidden_states_output(A_       ,				A_       ,				A_       ,				(padded_height, padded_width)						)
  
  
       @slow
       def 					__snake_case(     self						):
         for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _UpperCAmelCase		:       List[Any]       =    SwinModel.from_pretrained(A_						)
           self.assertIsNotNone(A_						)
  
  
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase,_UpperCAmelCase		:       List[str]       =    self.model_tester.prepare_config_and_inputs_for_common()
  
         _UpperCAmelCase		:       Union[str, Any]       =    _config_zero_init(A_						)
         for model_class in self.all_model_classes:
           _UpperCAmelCase		:       Union[str, Any]       =    model_class(config=A_						)
           for name, param in model.named_parameters():
             if "embeddings" not in name and param.requires_grad:
               self.assertIn(
                   ((param.data.mean() * 1e9).round() / 1e9).item()       ,				[0.0, 1.0]       ,				msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''       ,				)
  
  
  @require_vision
  @require_torch
  class 		_SCREAMING_SNAKE_CASE    (	unittest.TestCase    ):
  
  
       @cached_property
       def 					__snake_case(     self						):
         return (
             AutoImageProcessor.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""						)
             if is_vision_available()
             else None
         )
  
  
  
  
       @slow
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       Dict       =    SwinForImageClassification.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""						).to(A_						)
         _UpperCAmelCase		:       str       =    self.default_image_processor
  
         _UpperCAmelCase		:       Optional[int]       =    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
         _UpperCAmelCase		:       Optional[int]       =    image_processor(images=A_       ,				return_tensors="""pt"""						).to(A_						)
  
         # forward pass
         with torch.no_grad():
           _UpperCAmelCase		:       List[Any]       =    model(**A_						)
  
         # verify the logits
         _UpperCAmelCase		:       Optional[Any]       =    torch.Size((1, 10_00)						)
         self.assertEqual(outputs.logits.shape       ,				A_						)
         _UpperCAmelCase		:       int       =    torch.tensor([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]						).to(A_						)
         self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,				A_       ,				atol=1e-4						)						)
  
  
  @require_torch
  class 		_SCREAMING_SNAKE_CASE    (	unittest.TestCase       ,  A    ):
       __SCREAMING_SNAKE_CASE    				=						(SwinBackbone,) if is_torch_available() else ()
       __SCREAMING_SNAKE_CASE    				=						SwinConfig
  
  
       def 					__snake_case(     self						):
         _UpperCAmelCase		:       Tuple       =    SwinModelTester(self						)
  
  
  
  
  
  
style_context: 
  
  
  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__				:    Optional[int]         =    logging.get_logger(__name__)  # pylint: disable=invalid-name
  
  SCREAMING_SNAKE_CASE__				:    Tuple         =    '\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 		_SCREAMING_SNAKE_CASE    (	A    ):
       __SCREAMING_SNAKE_CASE    				=						42
  
  
  class 		_SCREAMING_SNAKE_CASE    (	A    ):
  
  
       def __init__(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				):
         super().__init__()
  
         self.register_modules(
             prior=A_       ,				text_encoder=A_       ,				tokenizer=A_       ,				scheduler=A_       ,				renderer=A_       ,				)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_						):
         if latents is None:
           _UpperCAmelCase		:       List[Any]       =    randn_tensor(A_       ,				generator=A_       ,				device=A_       ,				dtype=A_						)
         else:
           if latents.shape != shape:
             raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}'''						)
           _UpperCAmelCase		:       Optional[int]       =    latents.to(A_						)
  
         _UpperCAmelCase		:       str       =    latents * scheduler.init_noise_sigma
         return latents
  
  
       def 					__snake_case(     self       ,				A_=0						):
         if is_accelerate_available():
           from accelerate import cpu_offload
         else:
           raise ImportError("""Please install accelerate via `pip install accelerate`"""						)
  
         _UpperCAmelCase		:       Any       =    torch.device(F'''cuda:{gpu_id}'''						)
  
         _UpperCAmelCase		:       Dict       =    [self.text_encoder, self.prior]
         for cpu_offloaded_model in models:
           if cpu_offloaded_model is not None:
             cpu_offload(A_       ,				A_						)
  
  
       def 					__snake_case(     self       ,				A_=0						):
         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."""						)
  
         _UpperCAmelCase		:       int       =    torch.device(F'''cuda:{gpu_id}'''						)
  
         if self.device.type != "cpu":
           self.to("""cpu"""       ,				silence_dtype_warnings=A_						)
           torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)
  
         _UpperCAmelCase		:       List[str]       =    None
         for cpu_offloaded_model in [self.text_encoder, self.prior, self.renderer]:
           _UpperCAmelCase,_UpperCAmelCase		:       List[Any]       =    cpu_offload_with_hook(A_       ,				A_       ,				prev_module_hook=A_						)
  
         if self.safety_checker is not None:
           _UpperCAmelCase,_UpperCAmelCase		:       int       =    cpu_offload_with_hook(self.safety_checker       ,				A_       ,				prev_module_hook=A_						)
  
         # We'll offload the last model manually.
         _UpperCAmelCase		:       Optional[Any]       =    hook
  
  
       @property
       def 					__snake_case(     self						):
         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(A_       ,				"""_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 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				):
         len(A_						) if isinstance(A_       ,				A_						) else 1
  
         # YiYi Notes: set pad_token_id to be 0, not sure why I can't set in the config file
         _UpperCAmelCase		:       str       =    0
         # get prompt text embeddings
         _UpperCAmelCase		:       List[str]       =    self.tokenizer(
             A_       ,				padding="""max_length"""       ,				max_length=self.tokenizer.model_max_length       ,				truncation=A_       ,				return_tensors="""pt"""       ,				)
         _UpperCAmelCase		:       Any       =    text_inputs.input_ids
         _UpperCAmelCase		:       Dict       =    self.tokenizer(A_       ,				padding="""longest"""       ,				return_tensors="""pt"""						).input_ids
  
         if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(A_       ,				A_						):
           _UpperCAmelCase		:       Union[str, 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}'''						)
  
         _UpperCAmelCase		:       List[Any]       =    self.text_encoder(text_input_ids.to(A_						)						)
         _UpperCAmelCase		:       Union[str, Any]       =    text_encoder_output.text_embeds
  
         _UpperCAmelCase		:       Tuple       =    prompt_embeds.repeat_interleave(A_       ,				dim=0						)
         # in Shap-E it normalize the prompt_embeds and then later rescale it
         _UpperCAmelCase		:       Any       =    prompt_embeds / torch.linalg.norm(A_       ,				dim=-1       ,				keepdim=A_						)
  
         if do_classifier_free_guidance:
           _UpperCAmelCase		:       Union[str, Any]       =    torch.zeros_like(A_						)
  
           # 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
           _UpperCAmelCase		:       Optional[Any]       =    torch.cat([negative_prompt_embeds, prompt_embeds]						)
  
         # Rescale the features to have unit variance
         _UpperCAmelCase		:       List[Any]       =    math.sqrt(prompt_embeds.shape[1]						) * prompt_embeds
  
         return prompt_embeds
  
  
  
  
       @torch.no_grad()
       @replace_example_docstring(A_						)
       def __call__(     self       ,				A_       ,				A_ = 1       ,				A_ = 25       ,				A_ = None       ,				A_ = None       ,				A_ = 4.0       ,				A_ = 64       ,				A_ = "pil"       ,				A_ = True       ,				):
  
         if isinstance(A_       ,				A_						):
           _UpperCAmelCase		:       Optional[int]       =    1
         elif isinstance(A_       ,				A_						):
           _UpperCAmelCase		:       Union[str, Any]       =    len(A_						)
         else:
           raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(A_						)}'''						)
  
         _UpperCAmelCase		:       Tuple       =    self._execution_device
  
         _UpperCAmelCase		:       List[str]       =    batch_size * num_images_per_prompt
  
         _UpperCAmelCase		:       str       =    guidance_scale > 1.0
         _UpperCAmelCase		:       Tuple       =    self._encode_prompt(A_       ,				A_       ,				A_       ,				A_						)
  
         # prior
  
         self.scheduler.set_timesteps(A_       ,				device=A_						)
         _UpperCAmelCase		:       Any       =    self.scheduler.timesteps
  
         _UpperCAmelCase		:       List[str]       =    self.prior.config.num_embeddings
         _UpperCAmelCase		:       Union[str, Any]       =    self.prior.config.embedding_dim
  
         _UpperCAmelCase		:       Optional[Any]       =    self.prepare_latents(
             (batch_size, num_embeddings * embedding_dim)       ,				prompt_embeds.dtype       ,				A_       ,				A_       ,				A_       ,				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
         _UpperCAmelCase		:       Optional[Any]       =    latents.reshape(latents.shape[0]       ,				A_       ,				A_						)
  
         for i, t in enumerate(self.progress_bar(A_						)						):
           # expand the latents if we are doing classifier free guidance
           _UpperCAmelCase		:       Optional[int]       =    torch.cat([latents] * 2						) if do_classifier_free_guidance else latents
           _UpperCAmelCase		:       Union[str, Any]       =    self.scheduler.scale_model_input(A_       ,				A_						)
  
           _UpperCAmelCase		:       Any       =    self.prior(
               A_       ,				timestep=A_       ,				proj_embedding=A_       ,				).predicted_image_embedding
  
           # remove the variance
           _UpperCAmelCase,_UpperCAmelCase		:       Dict       =    noise_pred.split(
               scaled_model_input.shape[2]       ,				dim=2						)  # batch_size, num_embeddings, embedding_dim
  
           if do_classifier_free_guidance is not None:
             _UpperCAmelCase,_UpperCAmelCase		:       Union[str, Any]       =    noise_pred.chunk(2						)
             _UpperCAmelCase		:       List[Any]       =    noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)
  
           _UpperCAmelCase		:       Tuple       =    self.scheduler.step(
               A_       ,				timestep=A_       ,				sample=A_       ,				).prev_sample
  
         if output_type == "latent":
           return ShapEPipelineOutput(images=A_						)
  
         _UpperCAmelCase		:       Dict       =    []
         for i, latent in enumerate(A_						):
           _UpperCAmelCase		:       int       =    self.renderer.decode(
               latent[None, :]       ,				A_       ,				size=A_       ,				ray_batch_size=40_96       ,				n_coarse_samples=64       ,				n_fine_samples=1_28       ,				)
           images.append(A_						)
  
         _UpperCAmelCase		:       List[Any]       =    torch.stack(A_						)
  
         if output_type not in ["np", "pil"]:
           raise ValueError(F'''Only the output types `pil` and `np` are supported not output_type={output_type}'''						)
  
         _UpperCAmelCase		:       str       =    images.cpu().numpy()
  
         if output_type == "pil":
           _UpperCAmelCase		:       List[str]       =    [self.numpy_to_pil(A_						) 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=A_						)
  
  
  
  
  
  