code: 
  
  
  import os
  from typing import List, Union
  
  import tensorflow as tf
  from tensorflow_text import BertTokenizer as BertTokenizerLayer
  from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs
  
  from .tokenization_bert import BertTokenizer
  
  
  class 		_SCREAMING_SNAKE_CASE    (	tf.keras.layers.Layer    ):
  
  
       def __init__(     self       ,				A_       ,				A_       ,				A_ = None       ,				A_ = None       ,				A_ = None       ,				A_ = "longest"       ,				A_ = True       ,				A_ = 5_12       ,				A_ = None       ,				A_ = True       ,				A_ = True       ,				A_ = True       ,				**A_       ,				):
         super().__init__()
         if use_fast_bert_tokenizer:
           _UpperCAmelCase		:       Any       =    FastBertTokenizer(
               _lowerCAmelCase       ,				token_out_type=tf.intaa       ,				lower_case_nfd_strip_accents=_lowerCAmelCase       ,				**_lowerCAmelCase						)
         else:
           _UpperCAmelCase		:       str       =    tf.lookup.StaticVocabularyTable(
               tf.lookup.KeyValueTensorInitializer(
                   keys=_lowerCAmelCase       ,				key_dtype=tf.string       ,				values=tf.range(tf.size(_lowerCAmelCase       ,				out_type=tf.intaa						)       ,				dtype=tf.intaa						)       ,				value_dtype=tf.intaa       ,				)       ,				num_oov_buckets=1       ,				)
           _UpperCAmelCase		:       Tuple       =    BertTokenizerLayer(
               _lowerCAmelCase       ,				token_out_type=tf.intaa       ,				lower_case=_lowerCAmelCase       ,				**_lowerCAmelCase						)
  
         _UpperCAmelCase		:       List[Any]       =    vocab_list
         _UpperCAmelCase		:       Any       =    do_lower_case
         _UpperCAmelCase		:       Optional[int]       =    cls_token_id or vocab_list.index("""[CLS]"""						)
         _UpperCAmelCase		:       Any       =    sep_token_id or vocab_list.index("""[SEP]"""						)
         _UpperCAmelCase		:       Dict       =    pad_token_id or vocab_list.index("""[PAD]"""						)
         _UpperCAmelCase		:       List[Any]       =    ShrinkLongestTrimmer(max_length - 3       ,				axis=1						)  # Allow room for special tokens
         _UpperCAmelCase		:       List[Any]       =    max_length
         _UpperCAmelCase		:       Tuple       =    padding
         _UpperCAmelCase		:       Optional[int]       =    truncation
         _UpperCAmelCase		:       Dict       =    pad_to_multiple_of
         _UpperCAmelCase		:       List[Any]       =    return_token_type_ids
         _UpperCAmelCase		:       Union[str, Any]       =    return_attention_mask
  
  
       @classmethod
       def 					__snake_case(     cls       ,				A_       ,				**A_						):  # noqa: F821
         _UpperCAmelCase		:       Optional[int]       =    kwargs.pop("""do_lower_case"""       ,				_lowerCAmelCase						)
         _UpperCAmelCase		:       Any       =    tokenizer.do_lower_case if do_lower_case is None else do_lower_case
         _UpperCAmelCase		:       Union[str, Any]       =    kwargs.pop("""cls_token_id"""       ,				_lowerCAmelCase						)
         _UpperCAmelCase		:       Optional[int]       =    tokenizer.cls_token_id if cls_token_id is None else cls_token_id
         _UpperCAmelCase		:       Optional[Any]       =    kwargs.pop("""sep_token_id"""       ,				_lowerCAmelCase						)
         _UpperCAmelCase		:       Tuple       =    tokenizer.sep_token_id if sep_token_id is None else sep_token_id
         _UpperCAmelCase		:       Any       =    kwargs.pop("""pad_token_id"""       ,				_lowerCAmelCase						)
         _UpperCAmelCase		:       Optional[Any]       =    tokenizer.pad_token_id if pad_token_id is None else pad_token_id
  
         _UpperCAmelCase		:       Any       =    tokenizer.get_vocab()
         _UpperCAmelCase		:       Tuple       =    sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]       ,				key=lambda A_						: x[1]						)
         _UpperCAmelCase		:       List[Any]       =    [entry[0] for entry in vocab]
         return cls(
             vocab_list=_lowerCAmelCase       ,				do_lower_case=_lowerCAmelCase       ,				cls_token_id=_lowerCAmelCase       ,				sep_token_id=_lowerCAmelCase       ,				pad_token_id=_lowerCAmelCase       ,				**_lowerCAmelCase       ,				)
  
  
       @classmethod
       def 					__snake_case(     cls       ,				A_       ,				*A_       ,				**A_						):
         try:
           _UpperCAmelCase		:       int       =    BertTokenizer.from_pretrained(_lowerCAmelCase       ,				*_lowerCAmelCase       ,				**_lowerCAmelCase						)
         except:  # noqa: E722
           from .tokenization_bert_fast import BertTokenizerFast
  
           _UpperCAmelCase		:       Union[str, Any]       =    BertTokenizerFast.from_pretrained(_lowerCAmelCase       ,				*_lowerCAmelCase       ,				**_lowerCAmelCase						)
         return cls.from_tokenizer(_lowerCAmelCase       ,				**_lowerCAmelCase						)
  
  
       def 					__snake_case(     self       ,				A_						):
         if self.do_lower_case:
           _UpperCAmelCase		:       Tuple       =    case_fold_utfa(_lowerCAmelCase						)
         _UpperCAmelCase		:       Optional[int]       =    self.tf_tokenizer.tokenize(_lowerCAmelCase						)
         return tokens.merge_dims(1       ,				-1						)
  
  
       def 					__snake_case(     self       ,				A_       ,				A_=None       ,				A_=None       ,				A_=None       ,				A_=None       ,				A_=None       ,				A_=None       ,				A_=None       ,				):
         if padding is None:
           _UpperCAmelCase		:       Union[str, Any]       =    self.padding
         if padding not in ("longest", "max_length"):
           raise ValueError("""Padding must be either 'longest' or 'max_length'!"""						)
         if max_length is not None and text_pair is not None:
           # Because we have to instantiate a Trimmer to do it properly
           raise ValueError("""max_length cannot be overridden at call time when truncating paired texts!"""						)
         if max_length is None:
           _UpperCAmelCase		:       Optional[Any]       =    self.max_length
         if truncation is None:
           _UpperCAmelCase		:       Optional[Any]       =    self.truncation
         if pad_to_multiple_of is None:
           _UpperCAmelCase		:       str       =    self.pad_to_multiple_of
         if return_token_type_ids is None:
           _UpperCAmelCase		:       Optional[Any]       =    self.return_token_type_ids
         if return_attention_mask is None:
           _UpperCAmelCase		:       Optional[int]       =    self.return_attention_mask
         if not isinstance(_lowerCAmelCase       ,				tf.Tensor						):
           _UpperCAmelCase		:       str       =    tf.convert_to_tensor(_lowerCAmelCase						)
         if text_pair is not None and not isinstance(_lowerCAmelCase       ,				tf.Tensor						):
           _UpperCAmelCase		:       int       =    tf.convert_to_tensor(_lowerCAmelCase						)
         if text_pair is not None:
           if text.shape.rank > 1:
             raise ValueError("""text argument should not be multidimensional when a text pair is supplied!"""						)
           if text_pair.shape.rank > 1:
             raise ValueError("""text_pair should not be multidimensional!"""						)
         if text.shape.rank == 2:
           _UpperCAmelCase,_UpperCAmelCase		:       List[Any]       =    text[:, 0], text[:, 1]
         _UpperCAmelCase		:       Optional[Any]       =    self.unpaired_tokenize(_lowerCAmelCase						)
         if text_pair is None:  # Unpaired text
           if truncation:
             _UpperCAmelCase		:       str       =    text[:, : max_length - 2]  # Allow room for special tokens
           _UpperCAmelCase,_UpperCAmelCase		:       Optional[Any]       =    combine_segments(
               (text,)       ,				start_of_sequence_id=self.cls_token_id       ,				end_of_segment_id=self.sep_token_id						)
         else:  # Paired text
           _UpperCAmelCase		:       Union[str, Any]       =    self.unpaired_tokenize(_lowerCAmelCase						)
           if truncation:
             _UpperCAmelCase,_UpperCAmelCase		:       List[str]       =    self.paired_trimmer.trim([text, text_pair]						)
           _UpperCAmelCase,_UpperCAmelCase		:       Tuple       =    combine_segments(
               (text, text_pair)       ,				start_of_sequence_id=self.cls_token_id       ,				end_of_segment_id=self.sep_token_id						)
         if padding == "longest":
           _UpperCAmelCase		:       Dict       =    input_ids.bounding_shape(axis=1						)
           if pad_to_multiple_of is not None:
             # No ceiling division in tensorflow, so we negate floordiv instead
             _UpperCAmelCase		:       Optional[Any]       =    pad_to_multiple_of * (-tf.math.floordiv(-pad_length       ,				_lowerCAmelCase						))
         else:
           _UpperCAmelCase		:       Optional[int]       =    max_length
  
         _UpperCAmelCase,_UpperCAmelCase		:       List[Any]       =    pad_model_inputs(_lowerCAmelCase       ,				max_seq_length=_lowerCAmelCase       ,				pad_value=self.pad_token_id						)
         _UpperCAmelCase		:       List[Any]       =    {"""input_ids""": input_ids}
         if return_attention_mask:
           _UpperCAmelCase		:       Optional[int]       =    attention_mask
         if return_token_type_ids:
           _UpperCAmelCase,_UpperCAmelCase		:       Tuple       =    pad_model_inputs(
               _lowerCAmelCase       ,				max_seq_length=_lowerCAmelCase       ,				pad_value=self.pad_token_id						)
           _UpperCAmelCase		:       int       =    token_type_ids
         return output
  
  
  
  
       def 					__snake_case(     self						):
         return {
             "vocab_list": self.vocab_list,
             "do_lower_case": self.do_lower_case,
             "cls_token_id": self.cls_token_id,
             "sep_token_id": self.sep_token_id,
             "pad_token_id": self.pad_token_id,
         }
  
  
  
  
  
  
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_						)
  
  
  
  
  
  