
"""simple docstring"""






from collections.abc import Generator
from math import sin





def __A						(   a_ :							bytes     )->							bytes:




		'''simple docstring'''






		if len(a_     ) != 32:
				raise ValueError('''Input must be of length 32'''     )

		SCREAMING_SNAKE_CASE       :     str				= B''''''
		for i in [3, 2, 1, 0]:
				little_endian += string_aa[8 * i : 8 * i + 8]
		return little_endian





def __A						(   a_ :							int     )->							bytes:




		'''simple docstring'''






		if i < 0:
				raise ValueError('''Input must be non-negative'''     )

		SCREAMING_SNAKE_CASE       :     Tuple				= format(a_		,		'''08x'''     )[-8:]
		SCREAMING_SNAKE_CASE       :     int				= B''''''
		for i in [3, 2, 1, 0]:
				little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('''utf-8'''     )
		return little_endian_hex





def __A						(   a_ :							bytes     )->							bytes:




		'''simple docstring'''






		SCREAMING_SNAKE_CASE       :     Dict				= B''''''
		for char in message:
				bit_string += format(a_		,		'''08b'''     ).encode('''utf-8'''     )
		SCREAMING_SNAKE_CASE       :     int				= format(len(a_     )		,		'''064b'''     ).encode('''utf-8'''     )

		# Pad bit_string to a multiple of 512 chars
		bit_string += b"1"
		while len(a_     ) % 5_12 != 4_48:
				bit_string += b"0"
		bit_string += to_little_endian(start_len[32:]     ) + to_little_endian(start_len[:32]     )

		return bit_string





def __A						(   a_ :							bytes     )->							Generator[list[int], None, None]:




		'''simple docstring'''






		if len(a_     ) % 5_12 != 0:
				raise ValueError('''Input must have length that\'s a multiple of 512'''     )

		for pos in range(0		,		len(a_     )		,		5_12     ):
				SCREAMING_SNAKE_CASE       :     List[str]				= bit_string[pos : pos + 5_12]
				SCREAMING_SNAKE_CASE       :     Optional[int]				= []
				for i in range(0		,		5_12		,		32     ):
						block_words.append(int(to_little_endian(block[i : i + 32]     )		,		2     )     )
				yield block_words





def __A						(   a_ :							int     )->							int:




		'''simple docstring'''






		if i < 0:
				raise ValueError('''Input must be non-negative'''     )

		SCREAMING_SNAKE_CASE       :     Optional[int]				= format(a_		,		'''032b'''     )
		SCREAMING_SNAKE_CASE       :     Optional[int]				= ''''''
		for c in i_str:
				new_str += "1" if c == "0" else "0"
		return int(a_		,		2     )





def __A						(   a_ :							int		,		a_ :							int     )->							int:




		'''simple docstring'''






		return (a + b) % 2**32





def __A						(   a_ :							int		,		a_ :							int     )->							int:




		'''simple docstring'''






		if i < 0:
				raise ValueError('''Input must be non-negative'''     )
		if shift < 0:
				raise ValueError('''Shift must be non-negative'''     )
		return ((i << shift) ^ (i >> (32 - shift))) % 2**32





def __A						(   a_ :							bytes     )->							bytes:




		'''simple docstring'''






		SCREAMING_SNAKE_CASE       :     Dict				= preprocess(a_     )

		SCREAMING_SNAKE_CASE       :     List[Any]				= [int(2**32 * abs(sin(i + 1     )     )     ) for i in range(64     )]

		# Starting states
		SCREAMING_SNAKE_CASE       :     Dict				= 0x67_45_23_01
		SCREAMING_SNAKE_CASE       :     Tuple				= 0xEF_CD_AB_89
		SCREAMING_SNAKE_CASE       :     str				= 0x98_BA_DC_FE
		SCREAMING_SNAKE_CASE       :     List[Any]				= 0x10_32_54_76

		SCREAMING_SNAKE_CASE       :     Any				= [
		    7,
		    12,
		    17,
		    22,
		    7,
		    12,
		    17,
		    22,
		    7,
		    12,
		    17,
		    22,
		    7,
		    12,
		    17,
		    22,
		    5,
		    9,
		    14,
		    20,
		    5,
		    9,
		    14,
		    20,
		    5,
		    9,
		    14,
		    20,
		    5,
		    9,
		    14,
		    20,
		    4,
		    11,
		    16,
		    23,
		    4,
		    11,
		    16,
		    23,
		    4,
		    11,
		    16,
		    23,
		    4,
		    11,
		    16,
		    23,
		    6,
		    10,
		    15,
		    21,
		    6,
		    10,
		    15,
		    21,
		    6,
		    10,
		    15,
		    21,
		    6,
		    10,
		    15,
		    21,
		]

		# Process bit string in chunks, each with 16 32-char words
		for block_words in get_block_words(a_     ):
				SCREAMING_SNAKE_CASE       :     Dict				= aa
				SCREAMING_SNAKE_CASE       :     Optional[Any]				= ba
				SCREAMING_SNAKE_CASE       :     Tuple				= ca
				SCREAMING_SNAKE_CASE       :     Any				= da

				# Hash current chunk
				for i in range(64     ):
						if i <= 15:
								# f = (b & c) | (not_32(b) & d)     # Alternate definition for f
								SCREAMING_SNAKE_CASE       :     str				= d ^ (b & (c ^ d))
								SCREAMING_SNAKE_CASE       :     Optional[Any]				= i
						elif i <= 31:
								# f = (d & b) | (not_32(d) & c)     # Alternate definition for f
								SCREAMING_SNAKE_CASE       :     List[Any]				= c ^ (d & (b ^ c))
								SCREAMING_SNAKE_CASE       :     Optional[int]				= (5 * i + 1) % 16
						elif i <= 47:
								SCREAMING_SNAKE_CASE       :     List[Any]				= b ^ c ^ d
								SCREAMING_SNAKE_CASE       :     Union[str, Any]				= (3 * i + 5) % 16
						else:
								SCREAMING_SNAKE_CASE       :     Union[str, Any]				= c ^ (b | not_aa(a_     ))
								SCREAMING_SNAKE_CASE       :     Optional[Any]				= (7 * i) % 16
						SCREAMING_SNAKE_CASE       :     Dict				= (f + a + added_consts[i] + block_words[g]) % 2**32
						SCREAMING_SNAKE_CASE       :     int				= d
						SCREAMING_SNAKE_CASE       :     Union[str, Any]				= c
						SCREAMING_SNAKE_CASE       :     Any				= b
						SCREAMING_SNAKE_CASE       :     List[Any]				= sum_aa(a_		,		left_rotate_aa(a_		,		shift_amounts[i]     )     )

				# Add hashed chunk to running total
				SCREAMING_SNAKE_CASE       :     str				= sum_aa(a_		,		a_     )
				SCREAMING_SNAKE_CASE       :     str				= sum_aa(a_		,		a_     )
				SCREAMING_SNAKE_CASE       :     Dict				= sum_aa(a_		,		a_     )
				SCREAMING_SNAKE_CASE       :     Dict				= sum_aa(a_		,		a_     )

		SCREAMING_SNAKE_CASE       :     int				= reformat_hex(a_     ) + reformat_hex(a_     ) + reformat_hex(a_     ) + reformat_hex(a_     )
		return digest


if __name__ == "__main__":
					import doctest

					doctest.testmod()







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





