






"""simple docstring"""

import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.bert.modeling_bert import (
    BERT_INPUTS_DOCSTRING,
    BERT_START_DOCSTRING,
    BertEmbeddings,
    BertLayer,
    BertPooler,
    BertPreTrainedModel,
)






def _lowerCAmelCase	(  __lowerCamelCase:Tuple   ):

						'''simple docstring'''






						__magic_name__            =     torch.exp(__lowerCamelCase   )
						__magic_name__            =     torch.sum(__lowerCamelCase ,						dim=1   )  # sum of exp(x_i)
						__magic_name__            =     torch.sum(x * exp_x ,						dim=1   )  # sum of x_i * exp(x_i)
						return torch.log(__lowerCamelCase   ) - B / A

class 						A_							(      nn.Module  ):







				def __init__(							self							:						List[Any]						,    __lowerCamelCase							:						int	)				->					Tuple:
										super().__init__()
										__magic_name__            =     config.output_attentions
										__magic_name__            =     config.output_hidden_states
										__magic_name__            =     nn.ModuleList([BertLayer(__lowerCamelCase	) for _ in range(config.num_hidden_layers	)]	)
										__magic_name__            =     nn.ModuleList([BertHighway(__lowerCamelCase	) for _ in range(config.num_hidden_layers	)]	)

										__magic_name__            =     [-1 for _ in range(config.num_hidden_layers	)]







				def     _snake_case					(							self							:						Optional[Any]						,    __lowerCamelCase							:						List[Any]	)				->					Dict:
										if (type(__lowerCamelCase	) is float) or (type(__lowerCamelCase	) is int):
																for i in range(len(self.early_exit_entropy	)	):
																						__magic_name__            =     x
										else:
																__magic_name__            =     x







				def     _snake_case					(							self							:						Optional[Any]						,    __lowerCamelCase							:						int	)				->					List[Any]:
										__magic_name__            =     pooler.state_dict()
										for highway in self.highway:
																for name, param in highway.pooler.state_dict().items():
																						param.copy_(loaded_model[name]	)







				def     _snake_case					(							self							:						str						,    __lowerCamelCase							:						Tuple						,    __lowerCamelCase							:						List[Any]=None						,    __lowerCamelCase							:						Tuple=None						,    __lowerCamelCase							:						Union[str, Any]=None						,    __lowerCamelCase							:						str=None						,    )				->					List[str]:
										__magic_name__            =     ()
										__magic_name__            =     ()
										__magic_name__            =     ()
										for i, layer_module in enumerate(self.layer	):
																if self.output_hidden_states:
																						__magic_name__            =     all_hidden_states + (hidden_states,)

																__magic_name__            =     layer_module(
																    __lowerCamelCase						,    __lowerCamelCase						,    head_mask[i]						,    __lowerCamelCase						,    __lowerCamelCase	)
																__magic_name__            =     layer_outputs[0]

																if self.output_attentions:
																						__magic_name__            =     all_attentions + (layer_outputs[1],)

																__magic_name__            =     (hidden_states,)
																if self.output_hidden_states:
																						__magic_name__            =     current_outputs + (all_hidden_states,)
																if self.output_attentions:
																						__magic_name__            =     current_outputs + (all_attentions,)

																__magic_name__            =     self.highway[i](__lowerCamelCase	)
																# logits, pooled_output

																if not self.training:
																						__magic_name__            =     highway_exit[0]
																						__magic_name__            =     entropy(__lowerCamelCase	)
																						__magic_name__            =     highway_exit + (highway_entropy,)  # logits, hidden_states(?), entropy
																						__magic_name__            =     all_highway_exits + (highway_exit,)

																						if highway_entropy < self.early_exit_entropy[i]:
																												__magic_name__            =     (highway_logits,) + current_outputs[1:] + (all_highway_exits,)
																												raise HighwayException(__lowerCamelCase						,    i + 1	)
																else:
																						__magic_name__            =     all_highway_exits + (highway_exit,)

        # Add last layer
										if self.output_hidden_states:
																__magic_name__            =     all_hidden_states + (hidden_states,)

										__magic_name__            =     (hidden_states,)
										if self.output_hidden_states:
																__magic_name__            =     outputs + (all_hidden_states,)
										if self.output_attentions:
																__magic_name__            =     outputs + (all_attentions,)

										__magic_name__            =     outputs + (all_highway_exits,)
										return outputs  # last-layer hidden state, (all hidden states), (all attentions), all highway exits





@add_start_docstrings(
    '''The Bert Model transformer with early exiting (DeeBERT). '''					,							snake_case_					,							)
class 						A_							(      snake_case_  ):







				def __init__(							self							:						str						,    __lowerCamelCase							:						Union[str, Any]	)				->					Tuple:
										super().__init__(__lowerCamelCase	)
										__magic_name__            =     config

										__magic_name__            =     BertEmbeddings(__lowerCamelCase	)
										__magic_name__            =     DeeBertEncoder(__lowerCamelCase	)
										__magic_name__            =     BertPooler(__lowerCamelCase	)

										self.init_weights()







				def     _snake_case					(							self							:						Optional[Any]	)				->					List[str]:
										self.encoder.init_highway_pooler(self.pooler	)







				def     _snake_case					(							self							:						Union[str, Any]	)				->					str:
										return self.embeddings.word_embeddings







				def     _snake_case					(							self							:						List[str]						,    __lowerCamelCase							:						Dict	)				->					Union[str, Any]:
										__magic_name__            =     value







				def     _snake_case					(							self							:						Union[str, Any]						,    __lowerCamelCase							:						Union[str, Any]	)				->					Any:
										for layer, heads in heads_to_prune.items():
																self.encoder.layer[layer].attention.prune_heads(__lowerCamelCase	)







				@add_start_docstrings_to_model_forward(__lowerCamelCase	)
				def     _snake_case					(							self							:						str						,    __lowerCamelCase							:						int=None						,    __lowerCamelCase							:						Optional[Any]=None						,    __lowerCamelCase							:						List[str]=None						,    __lowerCamelCase							:						int=None						,    __lowerCamelCase							:						List[str]=None						,    __lowerCamelCase							:						List[str]=None						,    __lowerCamelCase							:						List[str]=None						,    __lowerCamelCase							:						Dict=None						,    )				->					List[Any]:
										if input_ids is not None and inputs_embeds is not None:
																raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time"	)
										elif input_ids is not None:
																__magic_name__            =     input_ids.size()
										elif inputs_embeds is not None:
																__magic_name__            =     inputs_embeds.size()[:-1]
										else:
																raise ValueError("You have to specify either input_ids or inputs_embeds"	)

										__magic_name__            =     input_ids.device if input_ids is not None else inputs_embeds.device

										if attention_mask is None:
																__magic_name__            =     torch.ones(__lowerCamelCase						,    device=__lowerCamelCase	)
										if encoder_attention_mask is None:
																__magic_name__            =     torch.ones(__lowerCamelCase						,    device=__lowerCamelCase	)
										if token_type_ids is None:
																__magic_name__            =     torch.zeros(__lowerCamelCase						,    dtype=torch.long						,    device=__lowerCamelCase	)

										# We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length]
										# ourselves in which case we just need to make it broadcastable to all heads.
										__magic_name__            =     self.get_extended_attention_mask(__lowerCamelCase						,    __lowerCamelCase						,    __lowerCamelCase	)

										# If a 2D ou 3D attention mask is provided for the cross-attention
										# we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length]
										if encoder_attention_mask.dim() == 3:
																__magic_name__            =     encoder_attention_mask[:, None, :, :]
										if encoder_attention_mask.dim() == 2:
																__magic_name__            =     encoder_attention_mask[:, None, None, :]

										__magic_name__            =     encoder_extended_attention_mask.to(
										    dtype=next(self.parameters()	).dtype	)  # fp16 compatibility
										__magic_name__            =     (1.0 - encoder_extended_attention_mask) * -1_0000.0

										# Prepare head mask if needed
										# 1.0 in head_mask indicate we keep the head
										# attention_probs has shape bsz x n_heads x N x N
										# input head_mask has shape [num_heads] or [num_hidden_layers x num_heads]
										# and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length]
										__magic_name__            =     self.get_head_mask(__lowerCamelCase						,    self.config.num_hidden_layers	)

										__magic_name__            =     self.embeddings(
										    input_ids=__lowerCamelCase						,    position_ids=__lowerCamelCase						,    token_type_ids=__lowerCamelCase						,    inputs_embeds=__lowerCamelCase	)
										__magic_name__            =     self.encoder(
										    __lowerCamelCase						,    attention_mask=__lowerCamelCase						,    head_mask=__lowerCamelCase						,    encoder_hidden_states=__lowerCamelCase						,    encoder_attention_mask=__lowerCamelCase						,    )
										__magic_name__            =     encoder_outputs[0]
										__magic_name__            =     self.pooler(__lowerCamelCase	)

										__magic_name__            =     (
										    sequence_output,
										    pooled_output,
										) + encoder_outputs[
										    1:
										]  # add hidden_states and attentions if they are here
										return outputs  # sequence_output, pooled_output, (hidden_states), (attentions), highway exits





class 						A_							(      snake_case_  ):







				def __init__(							self							:						Any						,    __lowerCamelCase							:						Union[str, Any]						,    __lowerCamelCase							:						Optional[Any]	)				->					Union[str, Any]:
										__magic_name__            =     message
										__magic_name__            =     exit_layer  # start from 1!





class 						A_							(      nn.Module  ):







				def __init__(							self							:						Dict						,    __lowerCamelCase							:						Tuple	)				->					List[Any]:
										super().__init__()
										__magic_name__            =     BertPooler(__lowerCamelCase	)
										__magic_name__            =     nn.Dropout(config.hidden_dropout_prob	)
										__magic_name__            =     nn.Linear(config.hidden_size						,    config.num_labels	)







				def     _snake_case					(							self							:						Any						,    __lowerCamelCase							:						int	)				->					str:
										# Pooler
										__magic_name__            =     encoder_outputs[0]
										__magic_name__            =     self.pooler(__lowerCamelCase	)
										# "return" pooler_output

										# BertModel
										__magic_name__            =     (pooler_input, pooler_output) + encoder_outputs[1:]
										# "return" bmodel_output

										# Dropout and classification
										__magic_name__            =     bmodel_output[1]

										__magic_name__            =     self.dropout(__lowerCamelCase	)
										__magic_name__            =     self.classifier(__lowerCamelCase	)

										return logits, pooled_output





@add_start_docstrings(
    '''Bert Model (with early exiting - DeeBERT) with a classifier on top,
    also takes care of multi-layer training. '''					,							snake_case_					,							)
class 						A_							(      snake_case_  ):







				def __init__(							self							:						Optional[Any]						,    __lowerCamelCase							:						str	)				->					Optional[Any]:
										super().__init__(__lowerCamelCase	)
										__magic_name__            =     config.num_labels
										__magic_name__            =     config.num_hidden_layers

										__magic_name__            =     DeeBertModel(__lowerCamelCase	)
										__magic_name__            =     nn.Dropout(config.hidden_dropout_prob	)
										__magic_name__            =     nn.Linear(config.hidden_size						,    self.config.num_labels	)

										self.init_weights()







				@add_start_docstrings_to_model_forward(__lowerCamelCase	)
				def     _snake_case					(							self							:						List[str]						,    __lowerCamelCase							:						Tuple=None						,    __lowerCamelCase							:						Union[str, Any]=None						,    __lowerCamelCase							:						List[str]=None						,    __lowerCamelCase							:						int=None						,    __lowerCamelCase							:						Dict=None						,    __lowerCamelCase							:						Optional[int]=None						,    __lowerCamelCase							:						Union[str, Any]=None						,    __lowerCamelCase							:						Dict=-1						,    __lowerCamelCase							:						List[Any]=False						,    )				->					Dict:
										__magic_name__            =     self.num_layers
										try:
																__magic_name__            =     self.bert(
																    __lowerCamelCase						,    attention_mask=__lowerCamelCase						,    token_type_ids=__lowerCamelCase						,    position_ids=__lowerCamelCase						,    head_mask=__lowerCamelCase						,    inputs_embeds=__lowerCamelCase						,    )
																# sequence_output, pooled_output, (hidden_states), (attentions), highway exits

																__magic_name__            =     outputs[1]

																__magic_name__            =     self.dropout(__lowerCamelCase	)
																__magic_name__            =     self.classifier(__lowerCamelCase	)
																__magic_name__            =     (logits,) + outputs[2:]  # add hidden states and attention if they are here
										except HighwayException as e:
																__magic_name__            =     e.message
																__magic_name__            =     e.exit_layer
																__magic_name__            =     outputs[0]

										if not self.training:
																__magic_name__            =     entropy(__lowerCamelCase	)
																__magic_name__            =     []
																__magic_name__            =     []
										if labels is not None:
																if self.num_labels == 1:
																						#  We are doing regression
																						__magic_name__            =     MSELoss()
																						__magic_name__            =     loss_fct(logits.view(-1	)						,    labels.view(-1	)	)
																else:
																						__magic_name__            =     CrossEntropyLoss()
																						__magic_name__            =     loss_fct(logits.view(-1						,    self.num_labels	)						,    labels.view(-1	)	)

																# work with highway exits
																__magic_name__            =     []
																for highway_exit in outputs[-1]:
																						__magic_name__            =     highway_exit[0]
																						if not self.training:
																												highway_logits_all.append(__lowerCamelCase	)
																												highway_entropy.append(highway_exit[2]	)
																						if self.num_labels == 1:
																												#  We are doing regression
																												__magic_name__            =     MSELoss()
																												__magic_name__            =     loss_fct(highway_logits.view(-1	)						,    labels.view(-1	)	)
																						else:
																												__magic_name__            =     CrossEntropyLoss()
																												__magic_name__            =     loss_fct(highway_logits.view(-1						,    self.num_labels	)						,    labels.view(-1	)	)
																						highway_losses.append(__lowerCamelCase	)

																if train_highway:
																						__magic_name__            =     (sum(highway_losses[:-1]	),) + outputs
																						# exclude the final highway, of course
																else:
																						__magic_name__            =     (loss,) + outputs
										if not self.training:
																__magic_name__            =     outputs + ((original_entropy, highway_entropy), exit_layer)
																if output_layer >= 0:
																						__magic_name__            =     (
																						    (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:]
																						)  # use the highway of the last layer

										return outputs  # (loss), logits, (hidden_states), (attentions), (highway_exits)











"""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 						A_							(      snake_case_  ):
				UpperCAmelCase__        =				42





class 						A_							(      snake_case_					,							snake_case_  ):
				UpperCAmelCase__        =				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						,    )				->					Any:
										super().__init__()

										# pass init params to Encoder
										__magic_name__            =     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
										__magic_name__            =     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						,    )

										__magic_name__            =     nn.Convad(2 * latent_channels						,    2 * latent_channels						,    1	)
										__magic_name__            =     nn.Convad(__lowerCamelCase						,    __lowerCamelCase						,    1	)

										__magic_name__            =     False
										__magic_name__            =     False

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







				def     _snake_case					(							self							:						List[str]						,    __lowerCamelCase							:						Tuple						,    __lowerCamelCase							:						Dict=False	)				->					Optional[int]:
										if isinstance(__lowerCamelCase						,    (Encoder, Decoder)	):
																__magic_name__            =     value







				def     _snake_case					(							self							:						Dict						,    __lowerCamelCase							:						bool = True	)				->					int:
										__magic_name__            =     use_tiling







				def     _snake_case					(							self							:						Dict	)				->					Optional[int]:
										self.enable_tiling(__lowerCamelCase	)







				def     _snake_case					(							self							:						int	)				->					str:
										__magic_name__            =     True







				def     _snake_case					(							self							:						Optional[Any]	)				->					Tuple:
										__magic_name__            =     False







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

										def fn_recursive_add_processors(__lowerCamelCase							:						str						,    __lowerCamelCase							:						torch.nn.Module						,    __lowerCamelCase							:						Dict[str, AttentionProcessor]	):
																if hasattr(__lowerCamelCase						,    "set_processor"	):
																						__magic_name__            =     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     _snake_case					(							self							:						Dict						,    __lowerCamelCase							:						Union[AttentionProcessor, Dict[str, AttentionProcessor]]	)				->					int:
										__magic_name__            =     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							:						List[str]	):
																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     _snake_case					(							self							:						List[Any]	)				->					Optional[Any]:
										self.set_attn_processor(AttnProcessor()	)







				@apply_forward_hook
				def     _snake_case					(							self							:						str						,    __lowerCamelCase							:						torch.FloatTensor						,    __lowerCamelCase							:						bool = True	)				->					AutoencoderKLOutput:
										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:
																__magic_name__            =     [self.encoder(__lowerCamelCase	) for x_slice in x.split(1	)]
																__magic_name__            =     torch.cat(__lowerCamelCase	)
										else:
																__magic_name__            =     self.encoder(__lowerCamelCase	)

										__magic_name__            =     self.quant_conv(__lowerCamelCase	)
										__magic_name__            =     DiagonalGaussianDistribution(__lowerCamelCase	)

										if not return_dict:
																return (posterior,)

										return AutoencoderKLOutput(latent_dist=__lowerCamelCase	)







				def     _snake_case					(							self							:						int						,    __lowerCamelCase							:						torch.FloatTensor						,    __lowerCamelCase							:						bool = True	)				->					Union[DecoderOutput, torch.FloatTensor]:
										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	)

										__magic_name__            =     self.post_quant_conv(__lowerCamelCase	)
										__magic_name__            =     self.decoder(__lowerCamelCase	)

										if not return_dict:
																return (dec,)

										return DecoderOutput(sample=__lowerCamelCase	)







				@apply_forward_hook
				def     _snake_case					(							self							:						Optional[Any]						,    __lowerCamelCase							:						torch.FloatTensor						,    __lowerCamelCase							:						bool = True	)				->					Union[DecoderOutput, torch.FloatTensor]:
										if self.use_slicing and z.shape[0] > 1:
																__magic_name__            =     [self._decode(__lowerCamelCase	).sample for z_slice in z.split(1	)]
																__magic_name__            =     torch.cat(__lowerCamelCase	)
										else:
																__magic_name__            =     self._decode(__lowerCamelCase	).sample

										if not return_dict:
																return (decoded,)

										return DecoderOutput(sample=__lowerCamelCase	)







				def     _snake_case					(							self							:						Optional[int]						,    __lowerCamelCase							:						str						,    __lowerCamelCase							:						List[str]						,    __lowerCamelCase							:						Dict	)				->					Optional[int]:
										__magic_name__            =     min(a.shape[2]						,    b.shape[2]						,    __lowerCamelCase	)
										for y in range(__lowerCamelCase	):
																__magic_name__            =     a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
										return b







				def     _snake_case					(							self							:						List[str]						,    __lowerCamelCase							:						Any						,    __lowerCamelCase							:						Tuple						,    __lowerCamelCase							:						Tuple	)				->					Optional[int]:
										__magic_name__            =     min(a.shape[3]						,    b.shape[3]						,    __lowerCamelCase	)
										for x in range(__lowerCamelCase	):
																__magic_name__            =     a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
										return b







				def     _snake_case					(							self							:						Optional[int]						,    __lowerCamelCase							:						torch.FloatTensor						,    __lowerCamelCase							:						bool = True	)				->					AutoencoderKLOutput:
										__magic_name__            =     int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)	)
										__magic_name__            =     int(self.tile_latent_min_size * self.tile_overlap_factor	)
										__magic_name__            =     self.tile_latent_min_size - blend_extent

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

										__magic_name__            =     torch.cat(__lowerCamelCase						,    dim=2	)
										__magic_name__            =     DiagonalGaussianDistribution(__lowerCamelCase	)

										if not return_dict:
																return (posterior,)

										return AutoencoderKLOutput(latent_dist=__lowerCamelCase	)







				def     _snake_case					(							self							:						str						,    __lowerCamelCase							:						torch.FloatTensor						,    __lowerCamelCase							:						bool = True	)				->					Union[DecoderOutput, torch.FloatTensor]:
										__magic_name__            =     int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)	)
										__magic_name__            =     int(self.tile_sample_min_size * self.tile_overlap_factor	)
										__magic_name__            =     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.
										__magic_name__            =     []
										for i in range(0						,    z.shape[2]						,    __lowerCamelCase	):
																__magic_name__            =     []
																for j in range(0						,    z.shape[3]						,    __lowerCamelCase	):
																						__magic_name__            =     z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
																						__magic_name__            =     self.post_quant_conv(__lowerCamelCase	)
																						__magic_name__            =     self.decoder(__lowerCamelCase	)
																						row.append(__lowerCamelCase	)
																rows.append(__lowerCamelCase	)
										__magic_name__            =     []
										for i, row in enumerate(__lowerCamelCase	):
																__magic_name__            =     []
																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:
																												__magic_name__            =     self.blend_v(rows[i - 1][j]						,    __lowerCamelCase						,    __lowerCamelCase	)
																						if j > 0:
																												__magic_name__            =     self.blend_h(row[j - 1]						,    __lowerCamelCase						,    __lowerCamelCase	)
																						result_row.append(tile[:, :, :row_limit, :row_limit]	)
																result_rows.append(torch.cat(__lowerCamelCase						,    dim=3	)	)

										__magic_name__            =     torch.cat(__lowerCamelCase						,    dim=2	)
										if not return_dict:
																return (dec,)

										return DecoderOutput(sample=__lowerCamelCase	)







				def     _snake_case					(							self							:						List[str]						,    __lowerCamelCase							:						torch.FloatTensor						,    __lowerCamelCase							:						bool = False						,    __lowerCamelCase							:						bool = True						,    __lowerCamelCase							:						Optional[torch.Generator] = None						,    )				->					Union[DecoderOutput, torch.FloatTensor]:
										__magic_name__            =     sample
										__magic_name__            =     self.encode(__lowerCamelCase	).latent_dist
										if sample_posterior:
																__magic_name__            =     posterior.sample(generator=__lowerCamelCase	)
										else:
																__magic_name__            =     posterior.mode()
										__magic_name__            =     self.decode(__lowerCamelCase	).sample

										if not return_dict:
																return (dec,)

										return DecoderOutput(sample=__lowerCamelCase	)



