


from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .attention_processor import AttentionProcessor, AttnProcessor
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder






@dataclass
class       _UpperCamelCase    (					UpperCamelCase_				):

				"""simple docstring"""







				lowerCAmelCase 		= 4_2






class       _UpperCamelCase    (					UpperCamelCase_    ,		UpperCamelCase_				):

				"""simple docstring"""







				lowerCAmelCase 		= True

				@register_to_config
				def __init__(						self				,   a__ = 3				,   a__ = 3				,   a__ = ("DownEncoderBlock2D",)				,   a__ = ("UpDecoderBlock2D",)				,   a__ = (64,)				,   a__ = 1				,   a__ = "silu"				,   a__ = 4				,   a__ = 32				,   a__ = 32				,   a__ = 0.1_82_15				,   )	->				str:
					super().__init__()

					# pass init params to Encoder
					A								=				Encoder(
					    in_channels=__A				,   out_channels=__A				,   down_block_types=__A				,   block_out_channels=__A				,   layers_per_block=__A				,   act_fn=__A				,   norm_num_groups=__A				,   double_z=__A				,   )

					# pass init params to Decoder
					A								=				Decoder(
					    in_channels=__A				,   out_channels=__A				,   up_block_types=__A				,   block_out_channels=__A				,   layers_per_block=__A				,   norm_num_groups=__A				,   act_fn=__A				,   )

					A								=				nn.Convad(2 * latent_channels				,   2 * latent_channels				,   1   )
					A								=				nn.Convad(__A				,   __A				,   1   )

					A								=				False
					A								=				False

					# only relevant if vae tiling is enabled
					A								=				self.config.sample_size
					A								=				(
					    self.config.sample_size[0]
					    if isinstance(self.config.sample_size				,   (list, tuple)   )
					    else self.config.sample_size
					)
					A								=				int(sample_size / (2 ** (len(self.config.block_out_channels   ) - 1))   )
					A								=				0.25

				def      _UpperCAmelCase					(						self				,   a__				,   a__=False   )	->				Dict:
					if isinstance(__A				,   (Encoder, Decoder)   ):
						A								=				value

				def      _UpperCAmelCase					(						self				,   a__ = True   )	->				int:
					A								=				use_tiling

				def      _UpperCAmelCase					(						self   )	->				Any:
					self.enable_tiling(__A   )

				def      _UpperCAmelCase					(						self   )	->				Any:
					A								=				True

				def      _UpperCAmelCase					(						self   )	->				List[str]:
					A								=				False

				@property
				# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
				def      _UpperCAmelCase					(						self   )	->				Optional[int]:
					A								=				{}

					def fn_recursive_add_processors(a__				,   a__				,   a__   ):
						if hasattr(__A				,   """set_processor"""   ):
							A								=				module.processor

						for sub_name, child in module.named_children():
							fn_recursive_add_processors(f'{name}.{sub_name}'				,   __A				,   __A   )

						return processors

					for name, module in self.named_children():
						fn_recursive_add_processors(__A				,   __A				,   __A   )

					return processors

				def      _UpperCAmelCase					(						self				,   a__   )	->				Optional[int]:
					A								=				len(self.attn_processors.keys()   )

					if isinstance(__A				,   __A   ) and len(__A   ) != count:
						raise ValueError(
						    f'A dict of processors was passed, but the number of processors {len(__A   )} does not match the'
						    f' number of attention layers: {count}. Please make sure to pass {count} processor classes.'   )

					def fn_recursive_attn_processor(a__				,   a__				,   a__   ):
						if hasattr(__A				,   """set_processor"""   ):
							if not isinstance(__A				,   __A   ):
								module.set_processor(__A   )
							else:
								module.set_processor(processor.pop(f'{name}.processor'   )   )

						for sub_name, child in module.named_children():
							fn_recursive_attn_processor(f'{name}.{sub_name}'				,   __A				,   __A   )

					for name, module in self.named_children():
						fn_recursive_attn_processor(__A				,   __A				,   __A   )

				def      _UpperCAmelCase					(						self   )	->				Union[str, Any]:
					self.set_attn_processor(AttnProcessor()   )

				@apply_forward_hook
				def      _UpperCAmelCase					(						self				,   a__				,   a__ = True   )	->				Tuple:
					if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size):
						return self.tiled_encode(__A				,   return_dict=__A   )

					if self.use_slicing and x.shape[0] > 1:
						A								=				[self.encoder(__A   ) for x_slice in x.split(1   )]
						A								=				torch.cat(__A   )
					else:
						A								=				self.encoder(__A   )

					A								=				self.quant_conv(__A   )
					A								=				DiagonalGaussianDistribution(__A   )

					if not return_dict:
						return (posterior,)

					return AutoencoderKLOutput(latent_dist=__A   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__ = True   )	->				str:
					if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size):
						return self.tiled_decode(__A				,   return_dict=__A   )

					A								=				self.post_quant_conv(__A   )
					A								=				self.decoder(__A   )

					if not return_dict:
						return (dec,)

					return DecoderOutput(sample=__A   )

				@apply_forward_hook
				def      _UpperCAmelCase					(						self				,   a__				,   a__ = True   )	->				Optional[Any]:
					if self.use_slicing and z.shape[0] > 1:
						A								=				[self._decode(__A   ).sample for z_slice in z.split(1   )]
						A								=				torch.cat(__A   )
					else:
						A								=				self._decode(__A   ).sample

					if not return_dict:
						return (decoded,)

					return DecoderOutput(sample=__A   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__   )	->				Union[str, Any]:
					A								=				min(a.shape[2]				,   b.shape[2]				,   __A   )
					for y in range(__A   ):
						A								=				a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
					return b

				def      _UpperCAmelCase					(						self				,   a__				,   a__				,   a__   )	->				Optional[int]:
					A								=				min(a.shape[3]				,   b.shape[3]				,   __A   )
					for x in range(__A   ):
						A								=				a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
					return b

				def      _UpperCAmelCase					(						self				,   a__				,   a__ = True   )	->				Union[str, Any]:
					A								=				int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)   )
					A								=				int(self.tile_latent_min_size * self.tile_overlap_factor   )
					A								=				self.tile_latent_min_size - blend_extent

					# Split the image into 512x512 tiles and encode them separately.
					A								=				[]
					for i in range(0				,   x.shape[2]				,   __A   ):
						A								=				[]
						for j in range(0				,   x.shape[3]				,   __A   ):
							A								=				x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
							A								=				self.encoder(__A   )
							A								=				self.quant_conv(__A   )
							row.append(__A   )
						rows.append(__A   )
					A								=				[]
					for i, row in enumerate(__A   ):
						A								=				[]
						for j, tile in enumerate(__A   ):
							# blend the above tile and the left tile
							# to the current tile and add the current tile to the result row
							if i > 0:
								A								=				self.blend_v(rows[i - 1][j]				,   __A				,   __A   )
							if j > 0:
								A								=				self.blend_h(row[j - 1]				,   __A				,   __A   )
							result_row.append(tile[:, :, :row_limit, :row_limit]   )
						result_rows.append(torch.cat(__A				,   dim=3   )   )

					A								=				torch.cat(__A				,   dim=2   )
					A								=				DiagonalGaussianDistribution(__A   )

					if not return_dict:
						return (posterior,)

					return AutoencoderKLOutput(latent_dist=__A   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__ = True   )	->				Any:
					A								=				int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)   )
					A								=				int(self.tile_sample_min_size * self.tile_overlap_factor   )
					A								=				self.tile_sample_min_size - blend_extent

					# Split z into overlapping 64x64 tiles and decode them separately.
					# The tiles have an overlap to avoid seams between tiles.
					A								=				[]
					for i in range(0				,   z.shape[2]				,   __A   ):
						A								=				[]
						for j in range(0				,   z.shape[3]				,   __A   ):
							A								=				z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
							A								=				self.post_quant_conv(__A   )
							A								=				self.decoder(__A   )
							row.append(__A   )
						rows.append(__A   )
					A								=				[]
					for i, row in enumerate(__A   ):
						A								=				[]
						for j, tile in enumerate(__A   ):
							# blend the above tile and the left tile
							# to the current tile and add the current tile to the result row
							if i > 0:
								A								=				self.blend_v(rows[i - 1][j]				,   __A				,   __A   )
							if j > 0:
								A								=				self.blend_h(row[j - 1]				,   __A				,   __A   )
							result_row.append(tile[:, :, :row_limit, :row_limit]   )
						result_rows.append(torch.cat(__A				,   dim=3   )   )

					A								=				torch.cat(__A				,   dim=2   )
					if not return_dict:
						return (dec,)

					return DecoderOutput(sample=__A   )

				def      _UpperCAmelCase					(						self				,   a__				,   a__ = False				,   a__ = True				,   a__ = None				,   )	->				List[str]:
					A								=				sample
					A								=				self.encode(__A   ).latent_dist
					if sample_posterior:
						A								=				posterior.sample(generator=__A   )
					else:
						A								=				posterior.mode()
					A								=				self.decode(__A   ).sample

					if not return_dict:
						return (dec,)

					return DecoderOutput(sample=__A   )



def   _lowerCAmelCase				(   UpperCamelCase__:     int						) ->   bool:





	"""simple docstring"""







	return str(UpperCamelCase__						) == str(UpperCamelCase__						)[::-1]





def   _lowerCAmelCase				(   UpperCamelCase__:     int						) ->   int:





	"""simple docstring"""







	return int(UpperCamelCase__						) + int(str(UpperCamelCase__						)[::-1]						)





def   _lowerCAmelCase				(   UpperCamelCase__:     int = 1_00_00						) ->   int:





	"""simple docstring"""







	A								=				[]
	for num in range(1							,					UpperCamelCase__						):
		A								=				0
		A								=				num
		while iterations < 50:
			A								=				sum_reverse(UpperCamelCase__						)
			iterations += 1
			if is_palindrome(UpperCamelCase__						):
				break
		else:
			lychrel_nums.append(UpperCamelCase__						)
	return len(UpperCamelCase__						)


if __name__ == "__main__":
				print(f'''{solution() = }''')



