


'''simple docstring'''



import torch
from torch import nn

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



class      a    (		_a     ,  _a	):


     """simple docstring"""






     @register_to_config
     def __init__(    self					:		List[Any]							,			*,
         snake_case					:		int = 4							,			snake_case					:		int = 768							,			snake_case					:		int							,			snake_case					:		List[str]							,			)						->							Union[str, Any]:
          super().__init__()

          __UpperCAmelCase     :			Any  							=      nn.Parameter(torch.zeros(_lowerCAmelCase      )      )

          # parameters for additional clip time embeddings
          __UpperCAmelCase     :			List[Any]  							=      nn.Linear(_lowerCAmelCase							,			_lowerCAmelCase      )
          __UpperCAmelCase     :			List[Any]  							=      nn.Linear(_lowerCAmelCase							,			_lowerCAmelCase      )

          # parameters for encoder hidden states
          __UpperCAmelCase     :			Optional[Any]  							=      clip_extra_context_tokens
          __UpperCAmelCase     :			Optional[Any]  							=      nn.Linear(
              _lowerCAmelCase							,			self.clip_extra_context_tokens * cross_attention_dim      )
          __UpperCAmelCase     :			str  							=      nn.Linear(_lowerCAmelCase							,			_lowerCAmelCase      )
          __UpperCAmelCase     :			List[str]  							=      nn.LayerNorm(_lowerCAmelCase      )






     def 			lowerCamelCase__			(    self					:		Optional[int]							,			*, snake_case					:		Union[str, Any]							,			snake_case					:		Dict							,			snake_case					:		List[str]							,			snake_case					:		Optional[Any]      )						->							Union[str, Any]:

          if do_classifier_free_guidance:
               # Add the classifier free guidance embeddings to the image embeddings
               __UpperCAmelCase     :			List[Any]  							=      image_embeddings.shape[0]
               __UpperCAmelCase     :			str  							=      self.learned_classifier_free_guidance_embeddings.unsqueeze(0      )
               __UpperCAmelCase     :			List[Any]  							=      classifier_free_guidance_embeddings.expand(
                   _lowerCAmelCase							,			-1      )
               __UpperCAmelCase     :			List[Any]  							=      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     :			str  							=      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     :			List[Any]  							=      self.embedding_proj(_lowerCAmelCase      )
          __UpperCAmelCase     :			Optional[Any]  							=      self.clip_image_embeddings_project_to_time_embeddings(_lowerCAmelCase      )
          __UpperCAmelCase     :			List[Any]  							=      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     :			str  							=      self.clip_extra_context_tokens_proj(_lowerCAmelCase      )
          __UpperCAmelCase     :			List[Any]  							=      clip_extra_context_tokens.reshape(_lowerCAmelCase							,			-1							,			self.clip_extra_context_tokens      )
          __UpperCAmelCase     :			Optional[Any]  							=      clip_extra_context_tokens.permute(0							,			2							,			1      )

          __UpperCAmelCase     :			Any  							=      self.encoder_hidden_states_proj(_lowerCAmelCase      )
          __UpperCAmelCase     :			int  							=      self.text_encoder_hidden_states_norm(_lowerCAmelCase      )
          __UpperCAmelCase     :			List[Any]  							=      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 TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase      :List[Any]			    =       {
    "configuration_lxmert": ["LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LxmertConfig"],
    "tokenization_lxmert": ["LxmertTokenizer"],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[Any]			    =       ["LxmertTokenizerFast"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[Any]			    =       [
          "LxmertEncoder",
          "LxmertForPreTraining",
          "LxmertForQuestionAnswering",
          "LxmertModel",
          "LxmertPreTrainedModel",
          "LxmertVisualFeatureEncoder",
          "LxmertXLayer",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[str]			    =       [
          "TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "TFLxmertForPreTraining",
          "TFLxmertMainLayer",
          "TFLxmertModel",
          "TFLxmertPreTrainedModel",
          "TFLxmertVisualFeatureEncoder",
      ]


if TYPE_CHECKING:
      from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig
      from .tokenization_lxmert import LxmertTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_lxmert_fast import LxmertTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_lxmert import (
                LxmertEncoder,
                LxmertForPreTraining,
                LxmertForQuestionAnswering,
                LxmertModel,
                LxmertPreTrainedModel,
                LxmertVisualFeatureEncoder,
                LxmertXLayer,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_lxmert import (
                TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFLxmertForPreTraining,
                TFLxmertMainLayer,
                TFLxmertModel,
                TFLxmertPreTrainedModel,
                TFLxmertVisualFeatureEncoder,
            )

else:
      import sys

      __UpperCAmelCase      :int			    =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)