


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 						__a (							UpperCAmelCase     ):
		_a      :    "DiagonalGaussianDistribution"



class 						__a (							UpperCAmelCase       , UpperCAmelCase     ):
		_a      :    Optional[Any]           =      True
		@register_to_config
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE = 3     ,       _SCREAMING_SNAKE_CASE = 3     ,       _SCREAMING_SNAKE_CASE = ("DownEncoderBlock2D",)     ,       _SCREAMING_SNAKE_CASE = ("UpDecoderBlock2D",)     ,       _SCREAMING_SNAKE_CASE = (64,)     ,       _SCREAMING_SNAKE_CASE = 1     ,       _SCREAMING_SNAKE_CASE = "silu"     ,       _SCREAMING_SNAKE_CASE = 4     ,       _SCREAMING_SNAKE_CASE = 32     ,       _SCREAMING_SNAKE_CASE = 32     ,       _SCREAMING_SNAKE_CASE = 0.18215     ,       )					->			Dict:




									"""simple docstring"""





									super().__init__()

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

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

									_UpperCAmelCase											=     nn.Convad(2 * latent_channels     ,       2 * latent_channels     ,       1		)
									_UpperCAmelCase											=     nn.Convad(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       1		)

									_UpperCAmelCase											=     False
									_UpperCAmelCase											=     False

									# only relevant if vae tiling is enabled
									_UpperCAmelCase											=     self.config.sample_size
									_UpperCAmelCase											=     (
									    self.config.sample_size[0]
									    if isinstance(self.config.sample_size     ,       (list, tuple)		)
									    else self.config.sample_size
									)
									_UpperCAmelCase											=     int(sample_size / (2 ** (len(self.config.block_out_channels		) - 1))		)
									_UpperCAmelCase											=     0.25
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE=False		)					->			List[Any]:




									"""simple docstring"""





									if isinstance(_SCREAMING_SNAKE_CASE     ,       (Encoder, Decoder)		):
																_UpperCAmelCase											=     value
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE = True		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     use_tiling
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									self.enable_tiling(_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     True
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									_UpperCAmelCase											=     False
		@property
		# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
		def       UpperCAmelCase__							(     self		)					->			Dict[str, AttentionProcessor]:




									"""simple docstring"""





									_UpperCAmelCase											=     {}

									def fn_recursive_add_processors(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		):
																if hasattr(_SCREAMING_SNAKE_CASE     ,       'set_processor'		):
																							_UpperCAmelCase											=     module.processor

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

																return processors

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

									return processors
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE		)					->			Union[str, Any]:




									"""simple docstring"""





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

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

									def fn_recursive_attn_processor(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		):
																if hasattr(_SCREAMING_SNAKE_CASE     ,       'set_processor'		):
																							if not isinstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		):
																														module.set_processor(_SCREAMING_SNAKE_CASE		)
																							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}'''     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)

									for name, module in self.named_children():
																fn_recursive_attn_processor(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Any:




									"""simple docstring"""





									self.set_attn_processor(AttnProcessor()		)
		@apply_forward_hook
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = 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(_SCREAMING_SNAKE_CASE     ,       return_dict=_SCREAMING_SNAKE_CASE		)

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

									_UpperCAmelCase											=     self.quant_conv(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     DiagonalGaussianDistribution(_SCREAMING_SNAKE_CASE		)

									if not return_dict:
																return (posterior,)

									return AutoencoderKLOutput(latent_dist=_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = 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(_SCREAMING_SNAKE_CASE     ,       return_dict=_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     self.post_quant_conv(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     self.decoder(_SCREAMING_SNAKE_CASE		)

									if not return_dict:
																return (dec,)

									return DecoderOutput(sample=_SCREAMING_SNAKE_CASE		)
		@apply_forward_hook
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = True		)					->			Union[DecoderOutput, torch.FloatTensor]:




									"""simple docstring"""





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

									if not return_dict:
																return (decoded,)

									return DecoderOutput(sample=_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Optional[Any]:




									"""simple docstring"""





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




									"""simple docstring"""





									_UpperCAmelCase											=     min(a.shape[3]     ,       b.shape[3]     ,       _SCREAMING_SNAKE_CASE		)
									for x in range(_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase											=     a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
									return b
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = True		)					->			AutoencoderKLOutput:




									"""simple docstring"""





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

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

									_UpperCAmelCase											=     torch.cat(_SCREAMING_SNAKE_CASE     ,       dim=2		)
									_UpperCAmelCase											=     DiagonalGaussianDistribution(_SCREAMING_SNAKE_CASE		)

									if not return_dict:
																return (posterior,)

									return AutoencoderKLOutput(latent_dist=_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = True		)					->			Union[DecoderOutput, torch.FloatTensor]:




									"""simple docstring"""





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

									_UpperCAmelCase											=     torch.cat(_SCREAMING_SNAKE_CASE     ,       dim=2		)
									if not return_dict:
																return (dec,)

									return DecoderOutput(sample=_SCREAMING_SNAKE_CASE		)






		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = False     ,       _SCREAMING_SNAKE_CASE = True     ,       _SCREAMING_SNAKE_CASE = None     ,       )					->			Union[DecoderOutput, torch.FloatTensor]:




									"""simple docstring"""





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

									if not return_dict:
																return (dec,)

									return DecoderOutput(sample=_SCREAMING_SNAKE_CASE		)




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase__    :Any       =				logging.get_logger(__name__)

lowerCAmelCase__    :Union[str, Any]       =				{
    '''facebook/xmod-base''': '''https://huggingface.co/facebook/xmod-base/resolve/main/config.json''',
    '''facebook/xmod-large-prenorm''': '''https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json''',
    '''facebook/xmod-base-13-125k''': '''https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-125k''': '''https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-195k''': '''https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json''',
    '''facebook/xmod-base-60-125k''': '''https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json''',
    '''facebook/xmod-base-60-265k''': '''https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json''',
    '''facebook/xmod-base-75-125k''': '''https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json''',
    '''facebook/xmod-base-75-269k''': '''https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json''',
}

class 						__a (							UpperCAmelCase     ):
		_a      :    Any           =      'xmod'
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=30522     ,       _SCREAMING_SNAKE_CASE=768     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=3072     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=512     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=1e-1_2     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=0     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE="absolute"     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE=False     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=False     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=("en_XX",)     ,       _SCREAMING_SNAKE_CASE=None     ,       **_SCREAMING_SNAKE_CASE     ,       )					->			Dict:




									"""simple docstring"""





									super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE     ,       bos_token_id=_SCREAMING_SNAKE_CASE     ,       eos_token_id=_SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     vocab_size
									_UpperCAmelCase											=     hidden_size
									_UpperCAmelCase											=     num_hidden_layers
									_UpperCAmelCase											=     num_attention_heads
									_UpperCAmelCase											=     hidden_act
									_UpperCAmelCase											=     intermediate_size
									_UpperCAmelCase											=     hidden_dropout_prob
									_UpperCAmelCase											=     attention_probs_dropout_prob
									_UpperCAmelCase											=     max_position_embeddings
									_UpperCAmelCase											=     type_vocab_size
									_UpperCAmelCase											=     initializer_range
									_UpperCAmelCase											=     layer_norm_eps
									_UpperCAmelCase											=     position_embedding_type
									_UpperCAmelCase											=     use_cache
									_UpperCAmelCase											=     classifier_dropout
									_UpperCAmelCase											=     pre_norm
									_UpperCAmelCase											=     adapter_reduction_factor
									_UpperCAmelCase											=     adapter_layer_norm
									_UpperCAmelCase											=     adapter_reuse_layer_norm
									_UpperCAmelCase											=     ln_before_adapter
									_UpperCAmelCase											=     list(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     default_language



class 						__a (							UpperCAmelCase     ):
		@property
		def       UpperCAmelCase__							(     self		)					->			Mapping[str, Mapping[int, str]]:




									"""simple docstring"""





									if self.task == "multiple-choice":
																_UpperCAmelCase											=     {0: 'batch', 1: 'choice', 2: 'sequence'}
									else:
																_UpperCAmelCase											=     {0: 'batch', 1: 'sequence'}
									return OrderedDict(
									    [
									        ('input_ids', dynamic_axis),
									        ('attention_mask', dynamic_axis),
									    ]		)
