

import torch
from torch import nn

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






class 						__magic_name__    (       _snake_case	,						_snake_case    ):


 @register_to_config
 def __init__(						self		: Optional[Any]			,						*,
     lowerCAmelCase__		: int = 4			,						lowerCAmelCase__		: int = 7_6_8			,						lowerCAmelCase__		: int			,						lowerCAmelCase__		: Optional[int]			,						)    ->      Optional[int]:
   super().__init__()

   UpperCAmelCase					      =  nn.Parameter(torch.zeros(lowerCAmelCase__    )    )

   # parameters for additional clip time embeddings
   UpperCAmelCase					      =  nn.Linear(lowerCAmelCase__			,						lowerCAmelCase__    )
   UpperCAmelCase					      =  nn.Linear(lowerCAmelCase__			,						lowerCAmelCase__    )

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


 def _UpperCamelCase	(						self		: str			,						*, lowerCAmelCase__		: Dict			,						lowerCAmelCase__		: Any			,						lowerCAmelCase__		: Dict			,						lowerCAmelCase__		: Optional[int]    )    ->      List[Any]:
   if do_classifier_free_guidance:
     # Add the classifier free guidance embeddings to the image embeddings
     UpperCAmelCase					      =  image_embeddings.shape[0]
     UpperCAmelCase					      =  self.learned_classifier_free_guidance_embeddings.unsqueeze(0    )
     UpperCAmelCase					      =  classifier_free_guidance_embeddings.expand(
         lowerCAmelCase__			,						-1    )
     UpperCAmelCase					      =  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]

   UpperCAmelCase					      =  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, ...
   UpperCAmelCase					      =  self.embedding_proj(lowerCAmelCase__    )
   UpperCAmelCase					      =  self.clip_image_embeddings_project_to_time_embeddings(lowerCAmelCase__    )
   UpperCAmelCase					      =  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"
   UpperCAmelCase					      =  self.clip_extra_context_tokens_proj(lowerCAmelCase__    )
   UpperCAmelCase					      =  clip_extra_context_tokens.reshape(lowerCAmelCase__			,						-1			,						self.clip_extra_context_tokens    )
   UpperCAmelCase					      =  clip_extra_context_tokens.permute(0			,						2			,						1    )

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

   return text_encoder_hidden_states, additive_clip_time_embeddings






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""lxmert"""
 UpperCAmelCase							    =						{}


 def __init__(						self		: int			,						lowerCAmelCase__		: Any=3_0_5_2_2			,						lowerCAmelCase__		: List[str]=7_6_8			,						lowerCAmelCase__		: Union[str, Any]=1_2			,						lowerCAmelCase__		: List[Any]=9_5_0_0			,						lowerCAmelCase__		: Any=1_6_0_0			,						lowerCAmelCase__		: Union[str, Any]=4_0_0			,						lowerCAmelCase__		: Tuple=3_0_7_2			,						lowerCAmelCase__		: Dict="gelu"			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: int=5_1_2			,						lowerCAmelCase__		: List[str]=2			,						lowerCAmelCase__		: List[str]=0.02			,						lowerCAmelCase__		: str=1e-1_2			,						lowerCAmelCase__		: str=9			,						lowerCAmelCase__		: int=5			,						lowerCAmelCase__		: Optional[int]=5			,						lowerCAmelCase__		: List[Any]=2_0_4_8			,						lowerCAmelCase__		: Any=4			,						lowerCAmelCase__		: Dict=6.67			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Union[str, Any]=True			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Tuple=True			,						lowerCAmelCase__		: Optional[Any]=True			,						lowerCAmelCase__		: Optional[int]=True			,						lowerCAmelCase__		: Tuple=True			,						**lowerCAmelCase__		: List[Any]			,						)    ->      Dict:
   UpperCAmelCase					      =  vocab_size
   UpperCAmelCase					      =  hidden_size
   UpperCAmelCase					      =  num_attention_heads
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  intermediate_size
   UpperCAmelCase					      =  hidden_dropout_prob
   UpperCAmelCase					      =  attention_probs_dropout_prob
   UpperCAmelCase					      =  max_position_embeddings
   UpperCAmelCase					      =  type_vocab_size
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  num_qa_labels
   UpperCAmelCase					      =  num_object_labels
   UpperCAmelCase					      =  num_attr_labels
   UpperCAmelCase					      =  l_layers
   UpperCAmelCase					      =  x_layers
   UpperCAmelCase					      =  r_layers
   UpperCAmelCase					      =  visual_feat_dim
   UpperCAmelCase					      =  visual_pos_dim
   UpperCAmelCase					      =  visual_loss_normalizer
   UpperCAmelCase					      =  task_matched
   UpperCAmelCase					      =  task_mask_lm
   UpperCAmelCase					      =  task_obj_predict
   UpperCAmelCase					      =  task_qa
   UpperCAmelCase					      =  visual_obj_loss
   UpperCAmelCase					      =  visual_attr_loss
   UpperCAmelCase					      =  visual_feat_loss
   UpperCAmelCase					      =  {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers}
   super().__init__(**lowerCAmelCase__    )



