'''simple docstring'''





import torch
from torch import nn

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




class 		SCREAMING_SNAKE_CASE  (       UpperCAmelCase__ ,			UpperCAmelCase__						):







				'''simple docstring'''





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


										'''simple docstring'''




										super().__init__()

										_UpperCamelCase      :      Any       =    nn.Parameter(torch.zeros(_UpperCAmelCase       )       )

										# parameters for additional clip time embeddings
										_UpperCamelCase      :      List[Any]       =    nn.Linear(_UpperCAmelCase							,		_UpperCAmelCase       )
										_UpperCamelCase      :      Tuple       =    nn.Linear(_UpperCAmelCase							,		_UpperCAmelCase       )

										# parameters for encoder hidden states
										_UpperCamelCase      :      str       =    clip_extra_context_tokens
										_UpperCamelCase      :      Union[str, Any]       =    nn.Linear(
										    _UpperCAmelCase							,		self.clip_extra_context_tokens * cross_attention_dim       )
										_UpperCamelCase      :      int       =    nn.Linear(_UpperCAmelCase							,		_UpperCAmelCase       )
										_UpperCamelCase      :      int       =    nn.LayerNorm(_UpperCAmelCase       )







				def 			snake_case__					(    self       :    Optional[int]							,		*, lowercase__       :    Dict							,		lowercase__       :    Any							,		lowercase__       :    Optional[Any]							,		lowercase__       :    Optional[Any]       )				->Dict:


										'''simple docstring'''




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

										_UpperCamelCase      :      Any       =    self.encoder_hidden_states_proj(_UpperCAmelCase       )
										_UpperCamelCase      :      Tuple       =    self.text_encoder_hidden_states_norm(_UpperCAmelCase       )
										_UpperCamelCase      :      str       =    torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]							,		dim=1       )

										return text_encoder_hidden_states, additive_clip_time_embeddings










import copy
from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


if TYPE_CHECKING:
 from ... import PreTrainedTokenizerBase, TensorType

snake_case__   :						List[str]													=   logging.get_logger(__name__)


class     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  A_						       = """vision-encoder-decoder"""
  A_						       = True




  def __init__(  self		,		**_UpperCAmelCase			)      ->			Dict:
     super().__init__(**_UpperCAmelCase			)
     if "encoder" not in kwargs or "decoder" not in kwargs:
        raise ValueError(
            f"""A configuraton of type {self.model_type} cannot be instantiated because """
            f"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}"""			)

     UpperCamelCase_						=  kwargs.pop('encoder'			)
     UpperCamelCase_						=  encoder_config.pop('model_type'			)
     UpperCamelCase_						=  kwargs.pop('decoder'			)
     UpperCamelCase_						=  decoder_config.pop('model_type'			)

     UpperCamelCase_						=  AutoConfig.for_model(_UpperCAmelCase		,		**_UpperCAmelCase			)
     UpperCamelCase_						=  AutoConfig.for_model(_UpperCAmelCase		,		**_UpperCAmelCase			)
     UpperCamelCase_						=  True




  @classmethod
  def  _UpperCAmelCase  (  cls		,		_UpperCAmelCase		,		_UpperCAmelCase		,		**_UpperCAmelCase			)      ->			PretrainedConfig:
     logger.info('Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'			)
     UpperCamelCase_						=  True
     UpperCamelCase_						=  True

     return cls(encoder=encoder_config.to_dict()		,		decoder=decoder_config.to_dict()		,		**_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self			)      ->			int:
     UpperCamelCase_						=  copy.deepcopy(self.__dict__			)
     UpperCamelCase_						=  self.encoder.to_dict()
     UpperCamelCase_						=  self.decoder.to_dict()
     UpperCamelCase_						=  self.__class__.model_type
     return output




class     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  A_						       = version.parse("""1.11"""     )




  @property
  def  _UpperCAmelCase  (  self			)      ->			Mapping[str, Mapping[int, str]]:
     return OrderedDict(
         [
             ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
         ]			)




  @property
  def  _UpperCAmelCase  (  self			)      ->			float:
     return 1e-4




  @property
  def  _UpperCAmelCase  (  self			)      ->			Mapping[str, Mapping[int, str]]:
     return OrderedDict({'last_hidden_state': {0: 'batch', 1: 'encoder_sequence'}}			)




class     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  @property
  def  _UpperCAmelCase  (  self			)      ->			Mapping[str, Mapping[int, str]]:
     UpperCamelCase_						=  OrderedDict()
     UpperCamelCase_						=  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
     UpperCamelCase_						=  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
     UpperCamelCase_						=  {0: 'batch', 1: 'encoder_sequence'}

     return common_inputs




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase = -1		,		_UpperCAmelCase = -1		,		_UpperCAmelCase = False		,		_UpperCAmelCase = None		,		)      ->			Mapping[str, Any]:
     import torch

     UpperCamelCase_						=  OrderedDict()

     UpperCamelCase_						=  super().generate_dummy_inputs(
         _UpperCAmelCase		,		batch_size=_UpperCAmelCase		,		seq_length=_UpperCAmelCase		,		is_pair=_UpperCAmelCase		,		framework=_UpperCAmelCase			)

     UpperCamelCase_		,   UpperCamelCase_						=  dummy_input['input_ids'].shape
     UpperCamelCase_						=  (batch, encoder_sequence, self._config.encoder_hidden_size)
     UpperCamelCase_						=  dummy_input.pop('input_ids'			)
     UpperCamelCase_						=  dummy_input.pop('attention_mask'			)
     UpperCamelCase_						=  torch.zeros(_UpperCAmelCase			)

     return common_inputs




class     _a							(	UpperCAmelCase__     ):






  """simple docstring"""





  @property
  def  _UpperCAmelCase  (  self			)      ->			None:
     pass




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase			)      ->			OnnxConfig:
     return VisionEncoderDecoderEncoderOnnxConfig(_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase = "default"			)      ->			OnnxConfig:
     UpperCamelCase_						=  encoder_config.hidden_size
     return VisionEncoderDecoderDecoderOnnxConfig(_UpperCAmelCase		,		_UpperCAmelCase			)





