

"""simple docstring"""







import torch
from torch import nn

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





class   UpperCamelCase_ (       a_   ,					a_  ):



  @register_to_config
  def __init__(      self ,		*,
      snake_case__ = 4 ,		snake_case__ = 7_68 ,		snake_case__ ,		snake_case__ ,		)			->			Optional[Any]:



         """simple docstring"""





         super().__init__()

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

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

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



  def 		UpperCamelCase_				(      self ,		*, snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			Optional[Any]:



         """simple docstring"""





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

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

         return text_encoder_hidden_states, additive_clip_time_embeddings








"""simple docstring"""







def       _lowerCAmelCase       (      lowerCAmelCase		):







       '''simple docstring'''
       UpperCAmelCase	    =			0
       UpperCAmelCase	    =			len(lowerCAmelCase		)

       for i in range(n - 1		):
              for j in range(i + 1	,      lowerCAmelCase		):
                     if arr[i] > arr[j]:
                            num_inversions += 1

       return num_inversions






def       _lowerCAmelCase       (      lowerCAmelCase		):







       '''simple docstring'''
       if len(lowerCAmelCase		) <= 1:
              return arr, 0
       UpperCAmelCase	    =			len(lowerCAmelCase		) // 2
       UpperCAmelCase	    =			arr[0:mid]
       UpperCAmelCase	    =			arr[mid:]

       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			_count_cross_inversions(lowerCAmelCase	,      lowerCAmelCase		)

       UpperCAmelCase	    =			inversion_p + inversions_q + cross_inversions
       return c, num_inversions






def       _lowerCAmelCase       (      lowerCAmelCase	,      lowerCAmelCase		):







       '''simple docstring'''
       UpperCAmelCase	    =			[]
       UpperCAmelCase	    =			UpperCAmelCase	    =			UpperCAmelCase	    =			0
       while i < len(lowerCAmelCase		) and j < len(lowerCAmelCase		):
              if p[i] > q[j]:
                     # if P[1] > Q[j], then P[k] > Q[k] for all  i < k <= len(P)
                     # These are all inversions. The claim emerges from the
                     # property that P is sorted.
                     num_inversion += len(lowerCAmelCase		) - i
                     r.append(q[j]		)
                     j += 1
              else:
                     r.append(p[i]		)
                     i += 1

       if i < len(lowerCAmelCase		):
              r.extend(p[i:]		)
       else:
              r.extend(q[j:]		)

       return r, num_inversion






def       _lowerCAmelCase       (      ):







       '''simple docstring'''
       UpperCAmelCase	    =			[10, 2, 1, 5, 5, 2, 11]

       # this arr has 8 inversions:
       # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2)

       UpperCAmelCase	    =			count_inversions_bf(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)

       assert num_inversions_bf == num_inversions_recursive == 8

       print("""number of inversions = """	,      lowerCAmelCase		)

       # testing an array with zero inversion (a sorted arr_1)

       arr_a.sort()
       UpperCAmelCase	    =			count_inversions_bf(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)

       assert num_inversions_bf == num_inversions_recursive == 0
       print("""number of inversions = """	,      lowerCAmelCase		)

       # an empty list should also have zero inversions
       UpperCAmelCase	    =			[]
       UpperCAmelCase	    =			count_inversions_bf(lowerCAmelCase		)
       UpperCAmelCase    ,       UpperCAmelCase	    =			count_inversions_recursive(lowerCAmelCase		)

       assert num_inversions_bf == num_inversions_recursive == 0
       print("""number of inversions = """	,      lowerCAmelCase		)


if __name__ == "__main__":
 main()
