



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





				'''simple docstring'''





				lowerCAmelCase__					    =    42





class _UpperCamelCase		(     A      ,       A		):





				'''simple docstring'''





				lowerCAmelCase__					    =    True

				@register_to_config
				def __init__(							self  :  Any  ,       _lowerCAmelCase  :  int = 3  ,       _lowerCAmelCase  :  int = 3  ,       _lowerCAmelCase  :  Tuple[str] = ("DownEncoderBlock2D",)  ,       _lowerCAmelCase  :  Tuple[str] = ("UpDecoderBlock2D",)  ,       _lowerCAmelCase  :  Tuple[int] = (6_4,)  ,       _lowerCAmelCase  :  int = 1  ,       _lowerCAmelCase  :  str = "silu"  ,       _lowerCAmelCase  :  int = 4  ,       _lowerCAmelCase  :  int = 3_2  ,       _lowerCAmelCase  :  int = 3_2  ,       _lowerCAmelCase  :  float = 0.1_8215  ,       ):







											'''simple docstring'''

											super().__init__()

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

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

											__lowercase       	=nn.Convad(2 * latent_channels  ,       2 * latent_channels  ,       1)
											__lowercase       	=nn.Convad(_lowerCAmelCase  ,       _lowerCAmelCase  ,       1)

											__lowercase       	=False
											__lowercase       	=False

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

				def 				__lowerCamelCase     (							self  :  Union[str, Any]  ,       _lowerCAmelCase  :  Any  ,       _lowerCAmelCase  :  Optional[Any]=False):







											'''simple docstring'''

											if isinstance(_lowerCAmelCase  ,       (Encoder, Decoder)):
																		__lowercase       	=value

				def 				__lowerCamelCase     (							self  :  Optional[Any]  ,       _lowerCAmelCase  :  bool = True):







											'''simple docstring'''

											__lowercase       	=use_tiling

				def 				__lowerCamelCase     (							self  :  Union[str, Any]):







											'''simple docstring'''

											self.enable_tiling(_lowerCAmelCase)

				def 				__lowerCamelCase     (							self  :  List[str]):







											'''simple docstring'''

											__lowercase       	=True

				def 				__lowerCamelCase     (							self  :  List[Any]):







											'''simple docstring'''

											__lowercase       	=False

				@property
				# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
				def 				__lowerCamelCase     (							self  :  List[str]):







											'''simple docstring'''

											__lowercase       	={}

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

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

																		return processors

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

											return processors

				def 				__lowerCamelCase     (							self  :  str  ,       _lowerCAmelCase  :  Union[AttentionProcessor, Dict[str, AttentionProcessor]]):







											'''simple docstring'''

											__lowercase       	=len(self.attn_processors.keys())

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

											def fn_recursive_attn_processor(_lowerCAmelCase  :  str  ,       _lowerCAmelCase  :  torch.nn.Module  ,       _lowerCAmelCase  :  Tuple):
																		if hasattr(_lowerCAmelCase  ,       'set_processor'):
																									if not isinstance(_lowerCAmelCase  ,       _lowerCAmelCase):
																																module.set_processor(_lowerCAmelCase)
																									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}"""  ,       _lowerCAmelCase  ,       _lowerCAmelCase)

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

				def 				__lowerCamelCase     (							self  :  List[Any]):







											'''simple docstring'''

											self.set_attn_processor(AttnProcessor())

				@apply_forward_hook
				def 				__lowerCamelCase     (							self  :  Union[str, Any]  ,       _lowerCAmelCase  :  torch.FloatTensor  ,       _lowerCAmelCase  :  bool = True):







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

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

											__lowercase       	=self.quant_conv(_lowerCAmelCase)
											__lowercase       	=DiagonalGaussianDistribution(_lowerCAmelCase)

											if not return_dict:
																		return (posterior,)

											return AutoencoderKLOutput(latent_dist=_lowerCAmelCase)

				def 				__lowerCamelCase     (							self  :  Optional[Any]  ,       _lowerCAmelCase  :  torch.FloatTensor  ,       _lowerCAmelCase  :  bool = True):







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

											__lowercase       	=self.post_quant_conv(_lowerCAmelCase)
											__lowercase       	=self.decoder(_lowerCAmelCase)

											if not return_dict:
																		return (dec,)

											return DecoderOutput(sample=_lowerCAmelCase)

				@apply_forward_hook
				def 				__lowerCamelCase     (							self  :  Tuple  ,       _lowerCAmelCase  :  torch.FloatTensor  ,       _lowerCAmelCase  :  bool = True):







											'''simple docstring'''

											if self.use_slicing and z.shape[0] > 1:
																		__lowercase       	=[self._decode(_lowerCAmelCase).sample for z_slice in z.split(1)]
																		__lowercase       	=torch.cat(_lowerCAmelCase)
											else:
																		__lowercase       	=self._decode(_lowerCAmelCase).sample

											if not return_dict:
																		return (decoded,)

											return DecoderOutput(sample=_lowerCAmelCase)

				def 				__lowerCamelCase     (							self  :  Optional[int]  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  int  ,       _lowerCAmelCase  :  List[str]):







											'''simple docstring'''

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

				def 				__lowerCamelCase     (							self  :  Tuple  ,       _lowerCAmelCase  :  Optional[Any]  ,       _lowerCAmelCase  :  Union[str, Any]  ,       _lowerCAmelCase  :  str):







											'''simple docstring'''

											__lowercase       	=min(a.shape[3]  ,       b.shape[3]  ,       _lowerCAmelCase)
											for x in range(_lowerCAmelCase):
																		__lowercase       	=a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
											return b

				def 				__lowerCamelCase     (							self  :  Any  ,       _lowerCAmelCase  :  torch.FloatTensor  ,       _lowerCAmelCase  :  bool = True):







											'''simple docstring'''

											__lowercase       	=int(self.tile_sample_min_size * (1 - self.tile_overlap_factor))
											__lowercase       	=int(self.tile_latent_min_size * self.tile_overlap_factor)
											__lowercase       	=self.tile_latent_min_size - blend_extent

											# Split the image into 512x512 tiles and encode them separately.
											__lowercase       	=[]
											for i in range(0  ,       x.shape[2]  ,       _lowerCAmelCase):
																		__lowercase       	=[]
																		for j in range(0  ,       x.shape[3]  ,       _lowerCAmelCase):
																									__lowercase       	=x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
																									__lowercase       	=self.encoder(_lowerCAmelCase)
																									__lowercase       	=self.quant_conv(_lowerCAmelCase)
																									row.append(_lowerCAmelCase)
																		rows.append(_lowerCAmelCase)
											__lowercase       	=[]
											for i, row in enumerate(_lowerCAmelCase):
																		__lowercase       	=[]
																		for j, tile in enumerate(_lowerCAmelCase):
																									# 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       	=self.blend_v(rows[i - 1][j]  ,       _lowerCAmelCase  ,       _lowerCAmelCase)
																									if j > 0:
																																__lowercase       	=self.blend_h(row[j - 1]  ,       _lowerCAmelCase  ,       _lowerCAmelCase)
																									result_row.append(tile[:, :, :row_limit, :row_limit])
																		result_rows.append(torch.cat(_lowerCAmelCase  ,       dim=3))

											__lowercase       	=torch.cat(_lowerCAmelCase  ,       dim=2)
											__lowercase       	=DiagonalGaussianDistribution(_lowerCAmelCase)

											if not return_dict:
																		return (posterior,)

											return AutoencoderKLOutput(latent_dist=_lowerCAmelCase)

				def 				__lowerCamelCase     (							self  :  List[Any]  ,       _lowerCAmelCase  :  torch.FloatTensor  ,       _lowerCAmelCase  :  bool = True):







											'''simple docstring'''

											__lowercase       	=int(self.tile_latent_min_size * (1 - self.tile_overlap_factor))
											__lowercase       	=int(self.tile_sample_min_size * self.tile_overlap_factor)
											__lowercase       	=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       	=[]
											for i in range(0  ,       z.shape[2]  ,       _lowerCAmelCase):
																		__lowercase       	=[]
																		for j in range(0  ,       z.shape[3]  ,       _lowerCAmelCase):
																									__lowercase       	=z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
																									__lowercase       	=self.post_quant_conv(_lowerCAmelCase)
																									__lowercase       	=self.decoder(_lowerCAmelCase)
																									row.append(_lowerCAmelCase)
																		rows.append(_lowerCAmelCase)
											__lowercase       	=[]
											for i, row in enumerate(_lowerCAmelCase):
																		__lowercase       	=[]
																		for j, tile in enumerate(_lowerCAmelCase):
																									# 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       	=self.blend_v(rows[i - 1][j]  ,       _lowerCAmelCase  ,       _lowerCAmelCase)
																									if j > 0:
																																__lowercase       	=self.blend_h(row[j - 1]  ,       _lowerCAmelCase  ,       _lowerCAmelCase)
																									result_row.append(tile[:, :, :row_limit, :row_limit])
																		result_rows.append(torch.cat(_lowerCAmelCase  ,       dim=3))

											__lowercase       	=torch.cat(_lowerCAmelCase  ,       dim=2)
											if not return_dict:
																		return (dec,)

											return DecoderOutput(sample=_lowerCAmelCase)

				def 				__lowerCamelCase     (							self  :  Any  ,       _lowerCAmelCase  :  torch.FloatTensor  ,       _lowerCAmelCase  :  bool = False  ,       _lowerCAmelCase  :  bool = True  ,       _lowerCAmelCase  :  Optional[torch.Generator] = None  ,       ):







											'''simple docstring'''

											__lowercase       	=sample
											__lowercase       	=self.encode(_lowerCAmelCase).latent_dist
											if sample_posterior:
																		__lowercase       	=posterior.sample(generator=_lowerCAmelCase)
											else:
																		__lowercase       	=posterior.mode()
											__lowercase       	=self.decode(_lowerCAmelCase).sample

											if not return_dict:
																		return (dec,)

											return DecoderOutput(sample=_lowerCAmelCase)








'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import _LazyModule


lowerCamelCase	 =       {"""tokenization_bertweet""": ["""BertweetTokenizer"""]}


if TYPE_CHECKING:
		from .tokenization_bertweet import BertweetTokenizer

else:
		import sys

		lowerCamelCase	 =       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



