


import torch
from torch import nn

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






class      UpperCamelCase					(		lowercase__						,      lowercase__     ):


   '''simple docstring'''







   @register_to_config
   def __init__(     self   ,							*,
       UpperCamelCase_ = 4   ,							UpperCamelCase_ = 768   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							):
        super().__init__()

        lowercase_       :Union[str, Any]     =       nn.Parameter(torch.zeros(UpperCamelCase_		)		)

        # parameters for additional clip time embeddings
        lowercase_       :List[Any]     =       nn.Linear(UpperCamelCase_   ,							UpperCamelCase_		)
        lowercase_       :Optional[Any]     =       nn.Linear(UpperCamelCase_   ,							UpperCamelCase_		)

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




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

        lowercase_       :Union[str, 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, ...
        lowercase_       :Union[str, Any]     =       self.embedding_proj(UpperCamelCase_		)
        lowercase_       :Tuple     =       self.clip_image_embeddings_project_to_time_embeddings(UpperCamelCase_		)
        lowercase_       :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"
        lowercase_       :int     =       self.clip_extra_context_tokens_proj(UpperCamelCase_		)
        lowercase_       :Dict     =       clip_extra_context_tokens.reshape(UpperCamelCase_   ,							-1   ,							self.clip_extra_context_tokens		)
        lowercase_       :List[Any]     =       clip_extra_context_tokens.permute(0   ,							2   ,							1		)

        lowercase_       :Tuple     =       self.encoder_hidden_states_proj(UpperCamelCase_		)
        lowercase_       :List[Any]     =       self.text_encoder_hidden_states_norm(UpperCamelCase_		)
        lowercase_       :Optional[int]     =       torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]   ,							dim=1		)

        return text_encoder_hidden_states, additive_clip_time_embeddings





def     UpperCamelCase    (      _a       ,		_a		)					->							int:
     '''simple docstring'''





     while a != 0:
          lowercase_    ,      lowercase_       :Union[str, Any]     =       b % a, a
     return b




def     UpperCamelCase    (      _a       ,		_a		)					->							int:
     '''simple docstring'''





     if gcd(_a       ,		_a		) != 1:
          lowercase_       :Union[str, Any]     =       f"mod inverse of {a!r} and {m!r} does not exist"
          raise ValueError(_a		)
     lowercase_    ,      lowercase_    ,      lowercase_       :Any     =       1, 0, a
     lowercase_    ,      lowercase_    ,      lowercase_       :List[str]     =       0, 1, m
     while va != 0:
          lowercase_       :Tuple     =       ua // va
          lowercase_    ,      lowercase_    ,      lowercase_    ,      lowercase_    ,      lowercase_    ,      lowercase_       :Optional[int]     =       (ua - q * va), (ua - q * va), (ua - q * va), va, va, va
     return ua % m

