





'''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      (lowerCamelCase     ):
							__a					:   "DiagonalGaussianDistribution"





class 					__a      (lowerCamelCase       ,		lowerCamelCase     ):
							__a					:   str									=       True

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







													"""simple docstring"""
													super().__init__()

													# pass init params to Encoder
													UpperCAmelCase_			:      Union[str, Any]     						=  Encoder(
													    in_channels=__magic_name__       ,     out_channels=__magic_name__       ,     down_block_types=__magic_name__       ,     block_out_channels=__magic_name__       ,     layers_per_block=__magic_name__       ,     act_fn=__magic_name__       ,     norm_num_groups=__magic_name__       ,     double_z=__magic_name__       ,     )

													# pass init params to Decoder
													UpperCAmelCase_			:      Dict     						=  Decoder(
													    in_channels=__magic_name__       ,     out_channels=__magic_name__       ,     up_block_types=__magic_name__       ,     block_out_channels=__magic_name__       ,     layers_per_block=__magic_name__       ,     norm_num_groups=__magic_name__       ,     act_fn=__magic_name__       ,     )

													UpperCAmelCase_			:      str     						=  nn.Convad(2 * latent_channels       ,     2 * latent_channels       ,     1   )
													UpperCAmelCase_			:      Optional[Any]     						=  nn.Convad(__magic_name__       ,     __magic_name__       ,     1   )

													UpperCAmelCase_			:      str     						=  False
													UpperCAmelCase_			:      List[str]     						=  False

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

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Union[str, Any]=False   )					->       Dict:







													"""simple docstring"""
													if isinstance(__magic_name__       ,     (Encoder, Decoder)   ):
																			UpperCAmelCase_			:      List[str]     						=  value

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     bool = True   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  use_tiling

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       List[str]:







													"""simple docstring"""
													self.enable_tiling(__magic_name__   )

							def     UpperCAmelCase__	(							self							:     str   )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  True

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  False

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







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  {}

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

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

																			return processors

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

													return processors

							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Union[AttentionProcessor, Dict[str, AttentionProcessor]]   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  len(self.attn_processors.keys()   )

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

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

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

							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       Optional[int]:







													"""simple docstring"""
													self.set_attn_processor(AttnProcessor()   )

							@apply_forward_hook
							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     torch.FloatTensor       ,     __magic_name__							:     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(__magic_name__       ,     return_dict=__magic_name__   )

													if self.use_slicing and x.shape[0] > 1:
																			UpperCAmelCase_			:      Optional[int]     						=  [self.encoder(__magic_name__   ) for x_slice in x.split(1   )]
																			UpperCAmelCase_			:      Union[str, Any]     						=  torch.cat(__magic_name__   )
													else:
																			UpperCAmelCase_			:      List[str]     						=  self.encoder(__magic_name__   )

													UpperCAmelCase_			:      Dict     						=  self.quant_conv(__magic_name__   )
													UpperCAmelCase_			:      Any     						=  DiagonalGaussianDistribution(__magic_name__   )

													if not return_dict:
																			return (posterior,)

													return AutoencoderKLOutput(latent_dist=__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     torch.FloatTensor       ,     __magic_name__							:     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(__magic_name__       ,     return_dict=__magic_name__   )

													UpperCAmelCase_			:      Tuple     						=  self.post_quant_conv(__magic_name__   )
													UpperCAmelCase_			:      Optional[Any]     						=  self.decoder(__magic_name__   )

													if not return_dict:
																			return (dec,)

													return DecoderOutput(sample=__magic_name__   )

							@apply_forward_hook
							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     torch.FloatTensor       ,     __magic_name__							:     bool = True   )					->       Union[DecoderOutput, torch.FloatTensor]:







													"""simple docstring"""
													if self.use_slicing and z.shape[0] > 1:
																			UpperCAmelCase_			:      Union[str, Any]     						=  [self._decode(__magic_name__   ).sample for z_slice in z.split(1   )]
																			UpperCAmelCase_			:      Any     						=  torch.cat(__magic_name__   )
													else:
																			UpperCAmelCase_			:      Dict     						=  self._decode(__magic_name__   ).sample

													if not return_dict:
																			return (decoded,)

													return DecoderOutput(sample=__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     str       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     List[str]   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  min(a.shape[2]       ,     b.shape[2]       ,     __magic_name__   )
													for y in range(__magic_name__   ):
																			UpperCAmelCase_			:      Optional[int]     						=  a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
													return b

							def     UpperCAmelCase__	(							self							:     str       ,     __magic_name__							:     int       ,     __magic_name__							:     Any       ,     __magic_name__							:     List[Any]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  min(a.shape[3]       ,     b.shape[3]       ,     __magic_name__   )
													for x in range(__magic_name__   ):
																			UpperCAmelCase_			:      Optional[Any]     						=  a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
													return b

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







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)   )
													UpperCAmelCase_			:      Union[str, Any]     						=  int(self.tile_latent_min_size * self.tile_overlap_factor   )
													UpperCAmelCase_			:      List[Any]     						=  self.tile_latent_min_size - blend_extent

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

													UpperCAmelCase_			:      List[str]     						=  torch.cat(__magic_name__       ,     dim=2   )
													UpperCAmelCase_			:      List[str]     						=  DiagonalGaussianDistribution(__magic_name__   )

													if not return_dict:
																			return (posterior,)

													return AutoencoderKLOutput(latent_dist=__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     torch.FloatTensor       ,     __magic_name__							:     bool = True   )					->       Union[DecoderOutput, torch.FloatTensor]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)   )
													UpperCAmelCase_			:      Union[str, Any]     						=  int(self.tile_sample_min_size * self.tile_overlap_factor   )
													UpperCAmelCase_			:      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.
													UpperCAmelCase_			:      Union[str, Any]     						=  []
													for i in range(0       ,     z.shape[2]       ,     __magic_name__   ):
																			UpperCAmelCase_			:      int     						=  []
																			for j in range(0       ,     z.shape[3]       ,     __magic_name__   ):
																									UpperCAmelCase_			:      Optional[int]     						=  z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
																									UpperCAmelCase_			:      Union[str, Any]     						=  self.post_quant_conv(__magic_name__   )
																									UpperCAmelCase_			:      Any     						=  self.decoder(__magic_name__   )
																									row.append(__magic_name__   )
																			rows.append(__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  []
													for i, row in enumerate(__magic_name__   ):
																			UpperCAmelCase_			:      int     						=  []
																			for j, tile in enumerate(__magic_name__   ):
																									# 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_			:      Tuple     						=  self.blend_v(rows[i - 1][j]       ,     __magic_name__       ,     __magic_name__   )
																									if j > 0:
																															UpperCAmelCase_			:      str     						=  self.blend_h(row[j - 1]       ,     __magic_name__       ,     __magic_name__   )
																									result_row.append(tile[:, :, :row_limit, :row_limit]   )
																			result_rows.append(torch.cat(__magic_name__       ,     dim=3   )   )

													UpperCAmelCase_			:      List[Any]     						=  torch.cat(__magic_name__       ,     dim=2   )
													if not return_dict:
																			return (dec,)

													return DecoderOutput(sample=__magic_name__   )



							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     torch.FloatTensor       ,     __magic_name__							:     bool = False       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     )					->       Union[DecoderOutput, torch.FloatTensor]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  sample
													UpperCAmelCase_			:      Any     						=  self.encode(__magic_name__   ).latent_dist
													if sample_posterior:
																			UpperCAmelCase_			:      str     						=  posterior.sample(generator=__magic_name__   )
													else:
																			UpperCAmelCase_			:      Tuple     						=  posterior.mode()
													UpperCAmelCase_			:      str     						=  self.decode(__magic_name__   ).sample

													if not return_dict:
																			return (dec,)

													return DecoderOutput(sample=__magic_name__   )












'''simple docstring'''







import json
import os
import unittest

from transformers.models.blenderbot_small.tokenization_blenderbot_small import (
    VOCAB_FILES_NAMES,
    BlenderbotSmallTokenizer,
)

from ...test_tokenization_common import TokenizerTesterMixin


class 					__a      (lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   List[str]									=       BlenderbotSmallTokenizer
							__a					:   List[Any]									=       False

							def     UpperCAmelCase__	(							self							:     str   )					->       str:







													"""simple docstring"""
													super().setUp()

													UpperCAmelCase_			:      Tuple     						=  ['''__start__''', '''adapt''', '''act''', '''ap@@''', '''te''', '''__end__''', '''__unk__''']
													UpperCAmelCase_			:      Optional[Any]     						=  dict(zip(__magic_name__       ,     range(len(__magic_name__   )   )   )   )

													UpperCAmelCase_			:      int     						=  ['''#version: 0.2''', '''a p''', '''t e</w>''', '''ap t</w>''', '''a d''', '''ad apt</w>''', '''a c''', '''ac t</w>''', '''''']
													UpperCAmelCase_			:      Optional[Any]     						=  {'''unk_token''': '''__unk__''', '''bos_token''': '''__start__''', '''eos_token''': '''__end__'''}

													UpperCAmelCase_			:      Tuple     						=  os.path.join(self.tmpdirname       ,     VOCAB_FILES_NAMES['''vocab_file''']   )
													UpperCAmelCase_			:      Dict     						=  os.path.join(self.tmpdirname       ,     VOCAB_FILES_NAMES['''merges_file''']   )
													with open(self.vocab_file       ,     '''w'''       ,     encoding='''utf-8'''   ) as fp:
																			fp.write(json.dumps(__magic_name__   ) + '''\n'''   )
													with open(self.merges_file       ,     '''w'''       ,     encoding='''utf-8'''   ) as fp:
																			fp.write('''\n'''.join(__magic_name__   )   )

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     **__magic_name__							:     Dict   )					->       Tuple:







													"""simple docstring"""
													kwargs.update(self.special_tokens_map   )
													return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     List[str]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  '''adapt act apte'''
													UpperCAmelCase_			:      Tuple     						=  '''adapt act apte'''
													return input_text, output_text

							def     UpperCAmelCase__	(							self							:     str   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  BlenderbotSmallTokenizer(self.vocab_file       ,     self.merges_file       ,     **self.special_tokens_map   )
													UpperCAmelCase_			:      List[Any]     						=  '''adapt act apte'''
													UpperCAmelCase_			:      Dict     						=  ['''adapt''', '''act''', '''ap@@''', '''te''']
													UpperCAmelCase_			:      Dict     						=  tokenizer.tokenize(__magic_name__   )
													self.assertListEqual(__magic_name__       ,     __magic_name__   )

													UpperCAmelCase_			:      Tuple     						=  [tokenizer.bos_token] + tokens + [tokenizer.eos_token]

													UpperCAmelCase_			:      Dict     						=  [0, 1, 2, 3, 4, 5]
													self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__   )       ,     __magic_name__   )

							def     UpperCAmelCase__	(							self							:     int   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M'''   )
													assert tok('''sam'''   ).input_ids == [13_84]
													UpperCAmelCase_			:      Optional[int]     						=  '''I am a small frog.'''
													UpperCAmelCase_			:      List[str]     						=  tok([src_text]       ,     padding=__magic_name__       ,     truncation=__magic_name__   )['''input_ids''']
													UpperCAmelCase_			:      Dict     						=  tok.batch_decode(__magic_name__       ,     skip_special_tokens=__magic_name__       ,     clean_up_tokenization_spaces=__magic_name__   )[0]
													assert src_text != decoded  # I wish it did!
													assert decoded == "i am a small frog ."



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







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M'''   )
													UpperCAmelCase_			:      List[Any]     						=  '''I am a small frog .'''
													UpperCAmelCase_			:      Any     						=  '''.'''
													UpperCAmelCase_			:      List[Any]     						=  tok(__magic_name__   )['''input_ids''']
													UpperCAmelCase_			:      Optional[int]     						=  tok(__magic_name__   )['''input_ids''']

													assert encoded[-1] == encoded_dot[0]





