




'''simple docstring'''





import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class        _a    (						__lowerCAmelCase , __lowerCAmelCase							):






     @register_to_config
     def __init__(			self ,*,
         _SCREAMING_SNAKE_CASE = 4 ,_SCREAMING_SNAKE_CASE = 768 ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,)							->      Any:
          super().__init__()

          _snake_case							=				nn.Parameter(torch.zeros(_SCREAMING_SNAKE_CASE   )   )

          # parameters for additional clip time embeddings
          _snake_case							=				nn.Linear(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )
          _snake_case							=				nn.Linear(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )

          # parameters for encoder hidden states
          _snake_case							=				clip_extra_context_tokens
          _snake_case							=				nn.Linear(
              _SCREAMING_SNAKE_CASE ,self.clip_extra_context_tokens * cross_attention_dim   )
          _snake_case							=				nn.Linear(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )
          _snake_case							=				nn.LayerNorm(_SCREAMING_SNAKE_CASE   )






     def     _lowercase     (			self ,*, _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )							->      Union[str, Any]:
          if do_classifier_free_guidance:
               # Add the classifier free guidance embeddings to the image embeddings
               _snake_case							=				image_embeddings.shape[0]
               _snake_case							=				self.learned_classifier_free_guidance_embeddings.unsqueeze(0   )
               _snake_case							=				classifier_free_guidance_embeddings.expand(
                   _SCREAMING_SNAKE_CASE ,-1   )
               _snake_case							=				torch.cat([classifier_free_guidance_embeddings, image_embeddings] ,dim=0   )

          # The image embeddings batch size and the text embeddings batch size are equal
          assert image_embeddings.shape[0] == prompt_embeds.shape[0]

          _snake_case							=				prompt_embeds.shape[0]

          # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
          # adding CLIP embeddings to the existing timestep embedding, ...
          _snake_case							=				self.embedding_proj(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				self.clip_image_embeddings_project_to_time_embeddings(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				time_projected_image_embeddings + time_projected_prompt_embeds

          # ... and by projecting CLIP embeddings into four
          # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
          _snake_case							=				self.clip_extra_context_tokens_proj(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				clip_extra_context_tokens.reshape(_SCREAMING_SNAKE_CASE ,-1 ,self.clip_extra_context_tokens   )
          _snake_case							=				clip_extra_context_tokens.permute(0 ,2 ,1   )

          _snake_case							=				self.encoder_hidden_states_proj(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				self.text_encoder_hidden_states_norm(_SCREAMING_SNAKE_CASE   )
          _snake_case							=				torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] ,dim=1   )

          return text_encoder_hidden_states, additive_clip_time_embeddings








'''simple docstring'''





from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType

class        _a    (						__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:   Any       =						["""image_processor""", """tokenizer"""]
     SCREAMING_SNAKE_CASE_							:   List[Any]       =						"""BridgeTowerImageProcessor"""
     SCREAMING_SNAKE_CASE_							:   List[Any]       =						("""RobertaTokenizer""", """RobertaTokenizerFast""")






     def __init__(			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )							->      Tuple:
          super().__init__(_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE   )






     def __call__(			self ,_SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = True ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = 0 ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = None ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = False ,_SCREAMING_SNAKE_CASE = True ,_SCREAMING_SNAKE_CASE = None ,**_SCREAMING_SNAKE_CASE ,)							->      BatchEncoding:
          _snake_case							=				self.tokenizer(
              text=_SCREAMING_SNAKE_CASE ,add_special_tokens=_SCREAMING_SNAKE_CASE ,padding=_SCREAMING_SNAKE_CASE ,truncation=_SCREAMING_SNAKE_CASE ,max_length=_SCREAMING_SNAKE_CASE ,stride=_SCREAMING_SNAKE_CASE ,pad_to_multiple_of=_SCREAMING_SNAKE_CASE ,return_token_type_ids=_SCREAMING_SNAKE_CASE ,return_attention_mask=_SCREAMING_SNAKE_CASE ,return_overflowing_tokens=_SCREAMING_SNAKE_CASE ,return_special_tokens_mask=_SCREAMING_SNAKE_CASE ,return_offsets_mapping=_SCREAMING_SNAKE_CASE ,return_length=_SCREAMING_SNAKE_CASE ,verbose=_SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE ,)
          # add pixel_values + pixel_mask
          _snake_case							=				self.image_processor(
              _SCREAMING_SNAKE_CASE ,return_tensors=_SCREAMING_SNAKE_CASE ,do_normalize=_SCREAMING_SNAKE_CASE ,do_center_crop=_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )
          encoding.update(_SCREAMING_SNAKE_CASE   )

          return encoding






     def     _lowercase     (			self ,*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )							->      List[str]:
          return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )






     def     _lowercase     (			self ,*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )							->      List[str]:
          return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE ,**_SCREAMING_SNAKE_CASE   )






     @property
     def     _lowercase     (			self   )							->      Any:
          _snake_case							=				self.tokenizer.model_input_names
          _snake_case							=				self.image_processor.model_input_names
          return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )


