



'''simple docstring'''

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 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					A_						    =						42






class 		lowerCamelCase__      (						A	,   A     ):




					'''simple docstring'''



					A_						    =						True




					@register_to_config
					def __init__(  self				:	Dict     ,  UpperCamelCase_				:	int = 3     ,  UpperCamelCase_				:	int = 3     ,  UpperCamelCase_				:	Tuple[str] = ("DownEncoderBlock2D",)     ,  UpperCamelCase_				:	Tuple[str] = ("UpDecoderBlock2D",)     ,  UpperCamelCase_				:	Tuple[int] = (64,)     ,  UpperCamelCase_				:	int = 1     ,  UpperCamelCase_				:	str = "silu"     ,  UpperCamelCase_				:	int = 4     ,  UpperCamelCase_				:	int = 32     ,  UpperCamelCase_				:	int = 32     ,  UpperCamelCase_				:	float = 0.1_82_15     ,  )  ->       str:



						'''simple docstring'''





						super().__init__()

						# pass init params to Encoder
						_lowercase					:       str       =		Encoder(
						    in_channels=UpperCamelCase_     ,  out_channels=UpperCamelCase_     ,  down_block_types=UpperCamelCase_     ,  block_out_channels=UpperCamelCase_     ,  layers_per_block=UpperCamelCase_     ,  act_fn=UpperCamelCase_     ,  norm_num_groups=UpperCamelCase_     ,  double_z=UpperCamelCase_     ,  )

						# pass init params to Decoder
						_lowercase					:       Optional[int]       =		Decoder(
						    in_channels=UpperCamelCase_     ,  out_channels=UpperCamelCase_     ,  up_block_types=UpperCamelCase_     ,  block_out_channels=UpperCamelCase_     ,  layers_per_block=UpperCamelCase_     ,  norm_num_groups=UpperCamelCase_     ,  act_fn=UpperCamelCase_     ,  )

						_lowercase					:       Optional[int]       =		nn.Convad(2 * latent_channels     ,  2 * latent_channels     ,  1					)
						_lowercase					:       Any       =		nn.Convad(UpperCamelCase_     ,  UpperCamelCase_     ,  1					)

						_lowercase					:       int       =		False
						_lowercase					:       Union[str, Any]       =		False

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




					def 						__UpperCAmelCase     (  self				:	List[str]     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	str=False					)  ->       Union[str, Any]:



						'''simple docstring'''





						if isinstance(UpperCamelCase_     ,  (Encoder, Decoder)					):
							_lowercase					:       str       =		value




					def 						__UpperCAmelCase     (  self				:	Dict     ,  UpperCamelCase_				:	bool = True					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		use_tiling




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       str:



						'''simple docstring'''





						self.enable_tiling(UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       Union[str, Any]:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		True




					def 						__UpperCAmelCase     (  self				:	str					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		False




					@property
					# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Dict[str, AttentionProcessor]:



						'''simple docstring'''





						_lowercase					:       int       =		{}

						def fn_recursive_add_processors(UpperCamelCase_				:	str     ,  UpperCamelCase_				:	torch.nn.Module     ,  UpperCamelCase_				:	Dict[str, AttentionProcessor]					):
							if hasattr(UpperCamelCase_     ,  'set_processor'					):
								_lowercase					:       List[str]       =		module.processor

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

							return processors

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

						return processors




					def 						__UpperCAmelCase     (  self				:	Optional[Any]     ,  UpperCamelCase_				:	Union[AttentionProcessor, Dict[str, AttentionProcessor]]					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Tuple       =		len(self.attn_processors.keys()					)

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

						def fn_recursive_attn_processor(UpperCamelCase_				:	str     ,  UpperCamelCase_				:	torch.nn.Module     ,  UpperCamelCase_				:	List[Any]					):
							if hasattr(UpperCamelCase_     ,  'set_processor'					):
								if not isinstance(UpperCamelCase_     ,  UpperCamelCase_					):
									module.set_processor(UpperCamelCase_					)
								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}'''     ,  UpperCamelCase_     ,  UpperCamelCase_					)

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




					def 						__UpperCAmelCase     (  self				:	List[Any]					)  ->       int:



						'''simple docstring'''





						self.set_attn_processor(AttnProcessor()					)




					@apply_forward_hook
					def 						__UpperCAmelCase     (  self				:	List[str]     ,  UpperCamelCase_				:	torch.FloatTensor     ,  UpperCamelCase_				:	bool = True					)  ->       AutoencoderKLOutput:



						'''simple docstring'''





						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(UpperCamelCase_     ,  return_dict=UpperCamelCase_					)

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

						_lowercase					:       Tuple       =		self.quant_conv(UpperCamelCase_					)
						_lowercase					:       Any       =		DiagonalGaussianDistribution(UpperCamelCase_					)

						if not return_dict:
							return (posterior,)

						return AutoencoderKLOutput(latent_dist=UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]     ,  UpperCamelCase_				:	torch.FloatTensor     ,  UpperCamelCase_				:	bool = True					)  ->       Union[DecoderOutput, torch.FloatTensor]:



						'''simple docstring'''





						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(UpperCamelCase_     ,  return_dict=UpperCamelCase_					)

						_lowercase					:       Tuple       =		self.post_quant_conv(UpperCamelCase_					)
						_lowercase					:       int       =		self.decoder(UpperCamelCase_					)

						if not return_dict:
							return (dec,)

						return DecoderOutput(sample=UpperCamelCase_					)




					@apply_forward_hook
					def 						__UpperCAmelCase     (  self				:	Dict     ,  UpperCamelCase_				:	torch.FloatTensor     ,  UpperCamelCase_				:	bool = True					)  ->       Union[DecoderOutput, torch.FloatTensor]:



						'''simple docstring'''





						if self.use_slicing and z.shape[0] > 1:
							_lowercase					:       int       =		[self._decode(UpperCamelCase_					).sample for z_slice in z.split(1					)]
							_lowercase					:       Dict       =		torch.cat(UpperCamelCase_					)
						else:
							_lowercase					:       List[str]       =		self._decode(UpperCamelCase_					).sample

						if not return_dict:
							return (decoded,)

						return DecoderOutput(sample=UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Dict     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	Optional[Any]					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		min(a.shape[2]     ,  b.shape[2]     ,  UpperCamelCase_					)
						for y in range(UpperCamelCase_					):
							_lowercase					:       Optional[int]       =		a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
						return b




					def 						__UpperCAmelCase     (  self				:	Optional[int]     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Any					)  ->       str:



						'''simple docstring'''





						_lowercase					:       Tuple       =		min(a.shape[3]     ,  b.shape[3]     ,  UpperCamelCase_					)
						for x in range(UpperCamelCase_					):
							_lowercase					:       Optional[int]       =		a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
						return b




					def 						__UpperCAmelCase     (  self				:	str     ,  UpperCamelCase_				:	torch.FloatTensor     ,  UpperCamelCase_				:	bool = True					)  ->       AutoencoderKLOutput:



						'''simple docstring'''





						_lowercase					:       List[str]       =		int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)					)
						_lowercase					:       Dict       =		int(self.tile_latent_min_size * self.tile_overlap_factor					)
						_lowercase					:       List[Any]       =		self.tile_latent_min_size - blend_extent

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

						_lowercase					:       Tuple       =		torch.cat(UpperCamelCase_     ,  dim=2					)
						_lowercase					:       int       =		DiagonalGaussianDistribution(UpperCamelCase_					)

						if not return_dict:
							return (posterior,)

						return AutoencoderKLOutput(latent_dist=UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Optional[Any]     ,  UpperCamelCase_				:	torch.FloatTensor     ,  UpperCamelCase_				:	bool = True					)  ->       Union[DecoderOutput, torch.FloatTensor]:



						'''simple docstring'''





						_lowercase					:       str       =		int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)					)
						_lowercase					:       List[Any]       =		int(self.tile_sample_min_size * self.tile_overlap_factor					)
						_lowercase					:       Dict       =		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.
						_lowercase					:       str       =		[]
						for i in range(0     ,  z.shape[2]     ,  UpperCamelCase_					):
							_lowercase					:       Dict       =		[]
							for j in range(0     ,  z.shape[3]     ,  UpperCamelCase_					):
								_lowercase					:       Optional[int]       =		z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
								_lowercase					:       List[str]       =		self.post_quant_conv(UpperCamelCase_					)
								_lowercase					:       Union[str, Any]       =		self.decoder(UpperCamelCase_					)
								row.append(UpperCamelCase_					)
							rows.append(UpperCamelCase_					)
						_lowercase					:       int       =		[]
						for i, row in enumerate(UpperCamelCase_					):
							_lowercase					:       Optional[Any]       =		[]
							for j, tile in enumerate(UpperCamelCase_					):
								# blend the above tile and the left tile
								# to the current tile and add the current tile to the result row
								if i > 0:
									_lowercase					:       Tuple       =		self.blend_v(rows[i - 1][j]     ,  UpperCamelCase_     ,  UpperCamelCase_					)
								if j > 0:
									_lowercase					:       Optional[int]       =		self.blend_h(row[j - 1]     ,  UpperCamelCase_     ,  UpperCamelCase_					)
								result_row.append(tile[:, :, :row_limit, :row_limit]					)
							result_rows.append(torch.cat(UpperCamelCase_     ,  dim=3					)					)

						_lowercase					:       Optional[int]       =		torch.cat(UpperCamelCase_     ,  dim=2					)
						if not return_dict:
							return (dec,)

						return DecoderOutput(sample=UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]     ,  UpperCamelCase_				:	torch.FloatTensor     ,  UpperCamelCase_				:	bool = False     ,  UpperCamelCase_				:	bool = True     ,  UpperCamelCase_				:	Optional[torch.Generator] = None     ,  )  ->       Union[DecoderOutput, torch.FloatTensor]:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		sample
						_lowercase					:       List[Any]       =		self.encode(UpperCamelCase_					).latent_dist
						if sample_posterior:
							_lowercase					:       Optional[Any]       =		posterior.sample(generator=UpperCamelCase_					)
						else:
							_lowercase					:       str       =		posterior.mode()
						_lowercase					:       Union[str, Any]       =		self.decode(UpperCamelCase_					).sample

						if not return_dict:
							return (dec,)

						return DecoderOutput(sample=UpperCamelCase_					)





'''simple docstring'''

def 						__UpperCamelCase				(							_lowercase,				_lowercase					) ->			list:
	_lowercase					:       List[str]       =		word.split()

	def justify(_lowercase,				_lowercase,				_lowercase					) -> str:
		_lowercase					:       Dict       =		max_width - width
		_lowercase					:       Tuple       =		len(_lowercase					)
		if len(_lowercase					) == 1:
			# if there is only word in line
			# just insert overall_spaces_count for the remainder of line
			return line[0] + " " * overall_spaces_count
		else:
			_lowercase					:       Tuple       =		words_count - 1
			# num_spaces_between_words_list[i] : tells you to insert
			# num_spaces_between_words_list[i] spaces
			# after word on line[i]
			_lowercase					:       str       =		spaces_to_insert_between_words * [
			    overall_spaces_count // spaces_to_insert_between_words
			]
			_lowercase					:       Optional[int]       =		(
			    overall_spaces_count % spaces_to_insert_between_words
			)
			# distribute spaces via round robin to the left words
			for i in range(_lowercase					):
				num_spaces_between_words_list[i] += 1
			_lowercase					:       Union[str, Any]       =		[]
			for i in range(_lowercase					):
				# add the word
				aligned_words_list.append(line[i]					)
				# add the spaces to insert
				aligned_words_list.append(num_spaces_between_words_list[i] * ' '					)
			# just add the last word to the sentence
			aligned_words_list.append(line[-1]					)
			# join the aligned words list to form a justified line
			return "".join(_lowercase					)

	_lowercase					:       str       =		[]
	_lowercase					:       list[str]       =		[]
	_lowercase					:       Union[str, Any]       =		0
	for word in words:
		if width + len(_lowercase					) + len(_lowercase					) <= max_width:
			# keep adding words until we can fill out max_width
			# width = sum of length of all words (without overall_spaces_count)
			# len(word) = length of current word
			# len(line) = number of overall_spaces_count to insert between words
			line.append(_lowercase					)
			width += len(_lowercase					)
		else:
			# justify the line and add it to result
			answer.append(justify(_lowercase,				_lowercase,				_lowercase					)					)
			# reset new line and new width
			_lowercase  ,    _lowercase					:       Optional[Any]       =		[word], len(_lowercase					)
	_lowercase					:       Optional[int]       =		max_width - width - len(_lowercase					)
	answer.append(' '.join(_lowercase					) + (remaining_spaces + 1) * ' '					)
	return answer


if __name__ == "__main__":
		from doctest import testmod

		testmod()
