


'''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   UpperCAmelCase_    (_UpperCAmelCase    ):





    """simple docstring"""






    def __init__(				self     ,	SCREAMING_SNAKE_CASE_      )							->	List[str]:
         super().__init__()
         __lowerCamelCase  :      str														=						nn.ModuleList(SCREAMING_SNAKE_CASE_      )







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = False     ,	SCREAMING_SNAKE_CASE_ = True     ,	)							->	Union[ControlNetOutput, Tuple]:
         for i, (image, scale, controlnet) in enumerate(zip(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	self.nets      )      ):
              __lowerCamelCase							,			__lowerCamelCase  :      str														=						controlnet(
                  SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	)

              # merge samples
              if i == 0:
                   __lowerCamelCase							,			__lowerCamelCase  :      Tuple														=						down_samples, mid_sample
              else:
                   __lowerCamelCase  :      str														=						[
                       samples_prev + samples_curr
                       for samples_prev, samples_curr in zip(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_      )
                   ]
                   mid_block_res_sample += mid_sample

         return down_block_res_samples, mid_block_res_sample







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = True     ,	SCREAMING_SNAKE_CASE_ = None     ,	SCREAMING_SNAKE_CASE_ = False     ,	SCREAMING_SNAKE_CASE_ = None     ,	)							->	List[Any]:
         __lowerCamelCase  :      Any														=						0
         __lowerCamelCase  :      Union[str, Any]														=						save_directory
         for controlnet in self.nets:
              controlnet.save_pretrained(
                  SCREAMING_SNAKE_CASE_     ,	is_main_process=SCREAMING_SNAKE_CASE_     ,	save_function=SCREAMING_SNAKE_CASE_     ,	safe_serialization=SCREAMING_SNAKE_CASE_     ,	variant=SCREAMING_SNAKE_CASE_     ,	)

              idx += 1
              __lowerCamelCase  :      List[str]														=						model_path_to_save + f'_{idx}'







    @classmethod
    def  lowercase_							(				cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )							->	Optional[Any]:
         __lowerCamelCase  :      str														=						0
         __lowerCamelCase  :      Any														=						[]

         # 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  :      str														=						pretrained_model_path
         while os.path.isdir(SCREAMING_SNAKE_CASE_      ):
              __lowerCamelCase  :      Optional[int]														=						ControlNetModel.from_pretrained(SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_      )
              controlnets.append(SCREAMING_SNAKE_CASE_      )

              idx += 1
              __lowerCamelCase  :      Dict														=						pretrained_model_path + f'_{idx}'

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

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

         return cls(SCREAMING_SNAKE_CASE_      )













'''simple docstring'''






lowerCAmelCase									=   [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)]






def   __A    (				a_    :	int						):
		lowerCAmelCase  :     Optional[int]       						= 0
		while number:
				# Increased Speed Slightly by checking every 5 digits together.
				sum_of_digits_squared += DIGITS_SQUARED[number % 1_0_0_0_0_0]
				number //= 1_0_0_0_0_0

		return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
lowerCAmelCase									=   [None] * 10_00_00_00
lowerCAmelCase									=   True
lowerCAmelCase									=   False






def   __A    (				a_    :	int						):

		if CHAINS[number - 1] is not None:
				return CHAINS[number - 1]  # type: ignore

		lowerCAmelCase  :     Dict       						= chain(next_number(a_						)						)
		lowerCAmelCase  :     Union[str, Any]       						= number_chain

		while number < 1_0_0_0_0_0_0_0:
				lowerCAmelCase  :     Any       						= number_chain
				number *= 1_0

		return number_chain






def   __A    (				a_    :	int = 1_0_0_0_0_0_0_0						):
		for i in range(1   ,a_						):
				if CHAINS[i] is None:
						chain(i + 1						)

		return CHAINS[:number].count(a_						)


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					print(F'''{solution() = }''')



