
"""simple docstring"""






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

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




class 				lowercase__(				_UpperCAmelCase   ):
	'''simple docstring'''

	UpperCamelCase				=  """vision-encoder-decoder"""
	UpperCamelCase				=  True





	def __init__(						self    :List[Any]		, **lowerCamelCase_    :Tuple					)							->			Union[str, Any]:







			'''simple docstring'''




			super().__init__(**lowerCamelCase_					)
			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}"					)

			SCREAMING_SNAKE_CASE       :     List[str]				= kwargs.pop('''encoder'''					)
			SCREAMING_SNAKE_CASE       :     Optional[int]				= encoder_config.pop('''model_type'''					)
			SCREAMING_SNAKE_CASE       :     Any				= kwargs.pop('''decoder'''					)
			SCREAMING_SNAKE_CASE       :     List[Any]				= decoder_config.pop('''model_type'''					)

			SCREAMING_SNAKE_CASE       :     Dict				= AutoConfig.for_model(lowerCamelCase_		, **lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Optional[int]				= AutoConfig.for_model(lowerCamelCase_		, **lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Tuple				= True





	@classmethod
	def   __lowerCAmelCase (						cls    :int		, lowerCamelCase_    :PretrainedConfig		, lowerCamelCase_    :PretrainedConfig		, **lowerCamelCase_    :List[str]					)							->			PretrainedConfig:







			'''simple docstring'''




			logger.info('''Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''					)
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= True
			SCREAMING_SNAKE_CASE       :     Dict				= True

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







	def   __lowerCAmelCase (						self    :Any					)							->			str:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     str				= copy.deepcopy(self.__dict__					)
			SCREAMING_SNAKE_CASE       :     int				= self.encoder.to_dict()
			SCREAMING_SNAKE_CASE       :     Tuple				= self.decoder.to_dict()
			SCREAMING_SNAKE_CASE       :     List[Any]				= self.__class__.model_type
			return output





class 				lowercase__(				_UpperCAmelCase   ):
	'''simple docstring'''

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





	@property
	def   __lowerCAmelCase (						self    :Union[str, Any]					)							->			Mapping[str, Mapping[int, str]]:







			'''simple docstring'''




			return OrderedDict(
			    [
			        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
			    ]					)





	@property
	def   __lowerCAmelCase (						self    :Optional[Any]					)							->			float:







			'''simple docstring'''




			return 1E-4







	@property
	def   __lowerCAmelCase (						self    :str					)							->			Mapping[str, Mapping[int, str]]:







			'''simple docstring'''




			return OrderedDict({'''last_hidden_state''': {0: '''batch''', 1: '''encoder_sequence'''}}					)





class 				lowercase__(				_UpperCAmelCase   ):
	'''simple docstring'''

	@property
	def   __lowerCAmelCase (						self    :Dict					)							->			Mapping[str, Mapping[int, str]]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[Any]				= OrderedDict()
			SCREAMING_SNAKE_CASE       :     Any				= {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
			SCREAMING_SNAKE_CASE       :     List[Any]				= {0: '''batch''', 1: '''encoder_sequence'''}

			return common_inputs







	def   __lowerCAmelCase (						self    :List[str]		, lowerCamelCase_    :"PreTrainedTokenizerBase"		, lowerCamelCase_    :int = -1		, lowerCamelCase_    :int = -1		, lowerCamelCase_    :bool = False		, lowerCamelCase_    :Optional["TensorType"] = None		, )							->			Mapping[str, Any]:







			'''simple docstring'''




			import torch

			SCREAMING_SNAKE_CASE       :     Dict				= OrderedDict()

			SCREAMING_SNAKE_CASE       :     Any				= super().generate_dummy_inputs(
			    lowerCamelCase_		, batch_size=lowerCamelCase_		, seq_length=lowerCamelCase_		, is_pair=lowerCamelCase_		, framework=lowerCamelCase_					)

			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     List[str]				= dummy_input['''input_ids'''].shape
			SCREAMING_SNAKE_CASE       :     str				= (batch, encoder_sequence, self._config.encoder_hidden_size)
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= dummy_input.pop('''input_ids'''					)
			SCREAMING_SNAKE_CASE       :     str				= dummy_input.pop('''attention_mask'''					)
			SCREAMING_SNAKE_CASE       :     List[str]				= torch.zeros(lowerCamelCase_					)

			return common_inputs





class 				lowercase__(				_UpperCAmelCase   ):
	'''simple docstring'''

	@property
	def   __lowerCAmelCase (						self    :Tuple					)							->			None:







			'''simple docstring'''




			pass





	def   __lowerCAmelCase (						self    :List[str]		, lowerCamelCase_    :PretrainedConfig					)							->			OnnxConfig:







			'''simple docstring'''




			return VisionEncoderDecoderEncoderOnnxConfig(lowerCamelCase_					)







	def   __lowerCAmelCase (						self    :Union[str, Any]		, lowerCamelCase_    :PretrainedConfig		, lowerCamelCase_    :PretrainedConfig		, lowerCamelCase_    :str = "default"					)							->			OnnxConfig:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= encoder_config.hidden_size
			return VisionEncoderDecoderDecoderOnnxConfig(lowerCamelCase_		, lowerCamelCase_					)







"""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


lowerCamelCase__ :       List[Any]						     =			logging.get_logger(__name__)




class 				lowercase__(				_UpperCAmelCase   ):
	'''simple docstring'''

	def __init__(						self    :List[str]		, lowerCamelCase_    :Union[List[ControlNetModel], Tuple[ControlNetModel]]					)							->			Union[str, Any]:







			'''simple docstring'''




			super().__init__()
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= nn.ModuleList(lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :Any		, lowerCamelCase_    :torch.FloatTensor		, lowerCamelCase_    :Union[torch.Tensor, float, int]		, lowerCamelCase_    :torch.Tensor		, lowerCamelCase_    :List[torch.tensor]		, lowerCamelCase_    :List[float]		, lowerCamelCase_    :Optional[torch.Tensor] = None		, lowerCamelCase_    :Optional[torch.Tensor] = None		, lowerCamelCase_    :Optional[torch.Tensor] = None		, lowerCamelCase_    :Optional[Dict[str, Any]] = None		, lowerCamelCase_    :bool = False		, lowerCamelCase_    :bool = True		, )							->			Union[ControlNetOutput, Tuple]:







			'''simple docstring'''




			for i, (image, scale, controlnet) in enumerate(zip(lowerCamelCase_		, lowerCamelCase_		, self.nets					)					):
					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     List[Any]				= controlnet(
					    lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, lowerCamelCase_		, )

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

			return down_block_res_samples, mid_block_res_sample





	def   __lowerCAmelCase (						self    :Any		, lowerCamelCase_    :Union[str, os.PathLike]		, lowerCamelCase_    :bool = True		, lowerCamelCase_    :Callable = None		, lowerCamelCase_    :bool = False		, lowerCamelCase_    :Optional[str] = None		, )							->			Optional[int]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[Any]				= 0
			SCREAMING_SNAKE_CASE       :     Any				= save_directory
			for controlnet in self.nets:
					controlnet.save_pretrained(
					    lowerCamelCase_		, is_main_process=lowerCamelCase_		, save_function=lowerCamelCase_		, safe_serialization=lowerCamelCase_		, variant=lowerCamelCase_		, )

					idx += 1
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= model_path_to_save + f"_{idx}"







	@classmethod
	def   __lowerCAmelCase (						cls    :Dict		, lowerCamelCase_    :Optional[Union[str, os.PathLike]]		, **lowerCamelCase_    :Tuple					)							->			List[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     str				= 0
			SCREAMING_SNAKE_CASE       :     Optional[int]				= []

			# 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`, ...
			SCREAMING_SNAKE_CASE       :     Dict				= pretrained_model_path
			while os.path.isdir(lowerCamelCase_					):
					SCREAMING_SNAKE_CASE       :     Tuple				= ControlNetModel.from_pretrained(lowerCamelCase_		, **lowerCamelCase_					)
					controlnets.append(lowerCamelCase_					)

					idx += 1
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= pretrained_model_path + f"_{idx}"

			logger.info(f"{len(lowerCamelCase_					)} controlnets loaded from {pretrained_model_path}."					)

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

			return cls(lowerCamelCase_					)





