


'''simple docstring'''

import torch
from torch import nn

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





class      snake_case    (    lowercase			,						lowercase			):





  """simple docstring"""




  @register_to_config
  def __init__(    self			,  *,
      UpperCamelCase = 4			,  UpperCamelCase = 768			,  UpperCamelCase			,  UpperCamelCase			,  ):







   """simple docstring"""
   super().__init__()

   lowerCamelCase_      =  nn.Parameter(torch.zeros(UpperCamelCase     )     )

   # parameters for additional clip time embeddings
   lowerCamelCase_      =  nn.Linear(UpperCamelCase			,  UpperCamelCase     )
   lowerCamelCase_      =  nn.Linear(UpperCamelCase			,  UpperCamelCase     )

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







  def   snake_case		(    self			,  *, UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase     ):







   """simple docstring"""
   if do_classifier_free_guidance:
    # Add the classifier free guidance embeddings to the image embeddings
    lowerCamelCase_      =  image_embeddings.shape[0]
    lowerCamelCase_      =  self.learned_classifier_free_guidance_embeddings.unsqueeze(0     )
    lowerCamelCase_      =  classifier_free_guidance_embeddings.expand(
        UpperCamelCase			,  -1     )
    lowerCamelCase_      =  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]

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

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

   return text_encoder_hidden_states, additive_clip_time_embeddings







'''simple docstring'''

import os
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import torch
from torch import nn

from ...models.controlnet import ControlNetModel, ControlNetOutput
from ...models.modeling_utils import ModelMixin
from ...utils import logging


a_  :				Dict						= logging.get_logger(__name__)





class      snake_case    (    lowercase			):





  """simple docstring"""




  def __init__(    self			,  UpperCamelCase     ):







   """simple docstring"""
   super().__init__()
   lowerCamelCase_      =  nn.ModuleList(UpperCamelCase     )






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase = None			,  UpperCamelCase = None			,  UpperCamelCase = None			,  UpperCamelCase = None			,  UpperCamelCase = False			,  UpperCamelCase = True			,  ):







   """simple docstring"""
   for i, (image, scale, controlnet) in enumerate(zip(UpperCamelCase			,  UpperCamelCase			,  self.nets     )     ):
    lowerCamelCase_ ,lowerCamelCase_      =  controlnet(
        UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase			,  )

    # merge samples
    if i == 0:
     lowerCamelCase_ ,lowerCamelCase_      =  down_samples, mid_sample
    else:
     lowerCamelCase_      =  [
         samples_prev + samples_curr
         for samples_prev, samples_curr in zip(UpperCamelCase			,  UpperCamelCase     )
     ]
     mid_block_res_sample += mid_sample

   return down_block_res_samples, mid_block_res_sample






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase = True			,  UpperCamelCase = None			,  UpperCamelCase = False			,  UpperCamelCase = None			,  ):







   """simple docstring"""
   lowerCamelCase_      =  0
   lowerCamelCase_      =  save_directory
   for controlnet in self.nets:
    controlnet.save_pretrained(
        UpperCamelCase			,  is_main_process=UpperCamelCase			,  save_function=UpperCamelCase			,  safe_serialization=UpperCamelCase			,  variant=UpperCamelCase			,  )

    idx += 1
    lowerCamelCase_      =  model_path_to_save + f'''_{idx}'''







  @classmethod
  def   snake_case		(    cls			,  UpperCamelCase			,  **UpperCamelCase     ):







   """simple docstring"""
   lowerCamelCase_      =  0
   lowerCamelCase_      =  []

   # load controlnet and append to list until no controlnet directory exists anymore
   # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained`
   # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ...
   lowerCamelCase_      =  pretrained_model_path
   while os.path.isdir(UpperCamelCase     ):
    lowerCamelCase_      =  ControlNetModel.from_pretrained(UpperCamelCase			,  **UpperCamelCase     )
    controlnets.append(UpperCamelCase     )

    idx += 1
    lowerCamelCase_      =  pretrained_model_path + f'''_{idx}'''

   logger.info(f'''{len(UpperCamelCase     )} controlnets loaded from {pretrained_model_path}.'''     )

   if len(UpperCamelCase     ) == 0:
    raise ValueError(
        f'''No ControlNets found under {os.path.dirname(UpperCamelCase     )}. Expected at least {pretrained_model_path + "_0"}.'''     )

   return cls(UpperCamelCase     )



