


'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_  :				Tuple						= logging.get_logger(__name__)

a_  :				Dict						= {
    """alibaba-damo/mgp-str-base""": """https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json""",
}





class      snake_case    (    lowercase			):





  """simple docstring"""




  _lowerCamelCase		    =  "mgp-str"






  def __init__(    self			,  UpperCamelCase=[32, 128]			,  UpperCamelCase=4			,  UpperCamelCase=3			,  UpperCamelCase=27			,  UpperCamelCase=38			,  UpperCamelCase=5_0257			,  UpperCamelCase=3_0522			,  UpperCamelCase=768			,  UpperCamelCase=12			,  UpperCamelCase=12			,  UpperCamelCase=4.0			,  UpperCamelCase=True			,  UpperCamelCase=False			,  UpperCamelCase=1e-5			,  UpperCamelCase=0.0			,  UpperCamelCase=0.0			,  UpperCamelCase=0.0			,  UpperCamelCase=False			,  UpperCamelCase=0.02			,  **UpperCamelCase			,  ):







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

   lowerCamelCase_      =  image_size
   lowerCamelCase_      =  patch_size
   lowerCamelCase_      =  num_channels
   lowerCamelCase_      =  max_token_length
   lowerCamelCase_      =  num_character_labels
   lowerCamelCase_      =  num_bpe_labels
   lowerCamelCase_      =  num_wordpiece_labels
   lowerCamelCase_      =  hidden_size
   lowerCamelCase_      =  num_hidden_layers
   lowerCamelCase_      =  num_attention_heads
   lowerCamelCase_      =  mlp_ratio
   lowerCamelCase_      =  distilled
   lowerCamelCase_      =  layer_norm_eps
   lowerCamelCase_      =  drop_rate
   lowerCamelCase_      =  qkv_bias
   lowerCamelCase_      =  attn_drop_rate
   lowerCamelCase_      =  drop_path_rate
   lowerCamelCase_      =  output_aa_attentions
   lowerCamelCase_      =  initializer_range







'''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     )



