





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							(						__lowercase    ):
      _snake_case				  =42

class   A							(						__lowercase		,   __lowercase    ):
      _snake_case				  =True



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

         super().__init__()

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

         UpperCAmelCase_           =nn.Convad(2 * latent_channels		,	2 * latent_channels		,	1							)
         UpperCAmelCase_           =nn.Convad(_lowerCAmelCase		,	_lowerCAmelCase		,	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 						lowerCAmelCase__     (						self:    Optional[Any]		,	_lowerCAmelCase:    Any		,	_lowerCAmelCase:    Optional[int]=False							)    ->     Tuple:
         '''simple docstring'''

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



      def 						lowerCAmelCase__     (						self:    int		,	_lowerCAmelCase:    bool = True							)    ->     Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_           =use_tiling



      def 						lowerCAmelCase__     (						self:    int							)    ->     Optional[int]:
         '''simple docstring'''

         self.enable_tiling(_lowerCAmelCase							)



      def 						lowerCAmelCase__     (						self:    List[Any]							)    ->     Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_           =True



      def 						lowerCAmelCase__     (						self:    Any							)    ->     List[str]:
         '''simple docstring'''

         UpperCAmelCase_           =False



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

         UpperCAmelCase_           ={}

         def fn_recursive_add_processors(_lowerCAmelCase:    str		,	_lowerCAmelCase:    torch.nn.Module		,	_lowerCAmelCase:    Dict[str, AttentionProcessor]							):
            if hasattr(_lowerCAmelCase		,	"set_processor"							):
               UpperCAmelCase_           =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:    Union[str, Any]		,	_lowerCAmelCase:    Union[AttentionProcessor, Dict[str, AttentionProcessor]]							)    ->     Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_           =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:    Dict							)    ->     int:
         '''simple docstring'''

         self.set_attn_processor(AttnProcessor()							)



      @apply_forward_hook
      def 						lowerCAmelCase__     (						self:    List[str]		,	_lowerCAmelCase:    torch.FloatTensor		,	_lowerCAmelCase:    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(_lowerCAmelCase		,	return_dict=_lowerCAmelCase							)

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

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

         if not return_dict:
            return (posterior,)

         return AutoencoderKLOutput(latent_dist=_lowerCAmelCase							)



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

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

         if not return_dict:
            return (dec,)

         return DecoderOutput(sample=_lowerCAmelCase							)



      @apply_forward_hook
      def 						lowerCAmelCase__     (						self:    Dict		,	_lowerCAmelCase:    torch.FloatTensor		,	_lowerCAmelCase:    bool = True							)    ->     Union[DecoderOutput, torch.FloatTensor]:
         '''simple docstring'''

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

         if not return_dict:
            return (decoded,)

         return DecoderOutput(sample=_lowerCAmelCase							)



      def 						lowerCAmelCase__     (						self:    str		,	_lowerCAmelCase:    Union[str, Any]		,	_lowerCAmelCase:    str		,	_lowerCAmelCase:    Dict							)    ->     Optional[int]:
         '''simple docstring'''

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



      def 						lowerCAmelCase__     (						self:    List[Any]		,	_lowerCAmelCase:    Optional[int]		,	_lowerCAmelCase:    List[Any]		,	_lowerCAmelCase:    Optional[Any]							)    ->     Optional[Any]:
         '''simple docstring'''

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



      def 						lowerCAmelCase__     (						self:    str		,	_lowerCAmelCase:    torch.FloatTensor		,	_lowerCAmelCase:    bool = 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]		,	_lowerCAmelCase							):
            UpperCAmelCase_           =[]
            for j in range(0		,	x.shape[3]		,	_lowerCAmelCase							):
               UpperCAmelCase_           =x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
               UpperCAmelCase_           =self.encoder(_lowerCAmelCase							)
               UpperCAmelCase_           =self.quant_conv(_lowerCAmelCase							)
               row.append(_lowerCAmelCase							)
            rows.append(_lowerCAmelCase							)
         UpperCAmelCase_           =[]
         for i, row in enumerate(_lowerCAmelCase							):
            UpperCAmelCase_           =[]
            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:
                  UpperCAmelCase_           =self.blend_v(rows[i - 1][j]		,	_lowerCAmelCase		,	_lowerCAmelCase							)
               if j > 0:
                  UpperCAmelCase_           =self.blend_h(row[j - 1]		,	_lowerCAmelCase		,	_lowerCAmelCase							)
               result_row.append(tile[:, :, :row_limit, :row_limit]							)
            result_rows.append(torch.cat(_lowerCAmelCase		,	dim=3							)							)

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

         if not return_dict:
            return (posterior,)

         return AutoencoderKLOutput(latent_dist=_lowerCAmelCase							)



      def 						lowerCAmelCase__     (						self:    str		,	_lowerCAmelCase:    torch.FloatTensor		,	_lowerCAmelCase:    bool = 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]		,	_lowerCAmelCase							):
            UpperCAmelCase_           =[]
            for j in range(0		,	z.shape[3]		,	_lowerCAmelCase							):
               UpperCAmelCase_           =z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
               UpperCAmelCase_           =self.post_quant_conv(_lowerCAmelCase							)
               UpperCAmelCase_           =self.decoder(_lowerCAmelCase							)
               row.append(_lowerCAmelCase							)
            rows.append(_lowerCAmelCase							)
         UpperCAmelCase_           =[]
         for i, row in enumerate(_lowerCAmelCase							):
            UpperCAmelCase_           =[]
            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:
                  UpperCAmelCase_           =self.blend_v(rows[i - 1][j]		,	_lowerCAmelCase		,	_lowerCAmelCase							)
               if j > 0:
                  UpperCAmelCase_           =self.blend_h(row[j - 1]		,	_lowerCAmelCase		,	_lowerCAmelCase							)
               result_row.append(tile[:, :, :row_limit, :row_limit]							)
            result_rows.append(torch.cat(_lowerCAmelCase		,	dim=3							)							)

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

         return DecoderOutput(sample=_lowerCAmelCase							)






      def 						lowerCAmelCase__     (						self:    Dict		,	_lowerCAmelCase:    torch.FloatTensor		,	_lowerCAmelCase:    bool = False		,	_lowerCAmelCase:    bool = True		,	_lowerCAmelCase:    Optional[torch.Generator] = None		,	)    ->     Union[DecoderOutput, torch.FloatTensor]:
         '''simple docstring'''

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

         if not return_dict:
            return (dec,)

         return DecoderOutput(sample=_lowerCAmelCase							)












'''simple docstring'''



from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

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


if TYPE_CHECKING:
 from ...feature_extraction_utils import FeatureExtractionMixin
 from ...tokenization_utils_base import PreTrainedTokenizerBase
 from ...utils import TensorType

__UpperCamelCase   :							List[str]         =   logging.get_logger(__name__)

__UpperCamelCase   :							Optional[int]         =   {
    """openai/whisper-base""": """https://huggingface.co/openai/whisper-base/resolve/main/config.json""",
}

# fmt: off
__UpperCamelCase   :							Optional[Any]         =   [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377,
    1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211,
    4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 10563, 10786,
    11420, 11709, 11907, 13163, 13697, 13700, 14808, 15306, 16410, 16791,
    17992, 19203, 19510, 20724, 22305, 22935, 27007, 30109, 30420, 33409,
    34949, 40283, 40493, 40549, 47282, 49146, 50257, 50359, 50360, 50361
]
__UpperCamelCase   :							List[str]         =   [
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627,
    3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647,
    7273, 9061, 9383, 10428, 10929, 11938, 12033, 12331, 12562, 13793,
    14157, 14635, 15265, 15618, 16553, 16604, 18362, 18956, 20075, 21675,
    22520, 26130, 26161, 26435, 28279, 29464, 31650, 32302, 32470, 36865,
    42863, 47425, 49870, 50254, 50258, 50360, 50361, 50362
]
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):
      __a	       ="whisper"
      __a	       =["past_key_values"]
      __a	       ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}




      def __init__(	self			, lowerCamelCase=5_1865			, lowerCamelCase=80			, lowerCamelCase=6			, lowerCamelCase=4			, lowerCamelCase=6			, lowerCamelCase=4			, lowerCamelCase=1536			, lowerCamelCase=1536			, lowerCamelCase=0.0			, lowerCamelCase=0.0			, lowerCamelCase=5_0257			, lowerCamelCase=True			, lowerCamelCase=True			, lowerCamelCase="gelu"			, lowerCamelCase=256			, lowerCamelCase=0.0			, lowerCamelCase=0.0			, lowerCamelCase=0.0			, lowerCamelCase=0.02			, lowerCamelCase=False			, lowerCamelCase=1500			, lowerCamelCase=448			, lowerCamelCase=5_0256			, lowerCamelCase=5_0256			, lowerCamelCase=5_0256			, lowerCamelCase=None			, lowerCamelCase=[220, 5_0256]			, lowerCamelCase=False			, lowerCamelCase=256			, lowerCamelCase=False			, lowerCamelCase=0.05			, lowerCamelCase=10			, lowerCamelCase=2			, lowerCamelCase=0.0			, lowerCamelCase=10			, lowerCamelCase=0			, lowerCamelCase=7			, **lowerCamelCase			, )				->Any:



         '''simple docstring'''
         __a					  =     vocab_size
         __a					  =     num_mel_bins
         __a					  =     d_model
         __a					  =     encoder_layers
         __a					  =     encoder_attention_heads
         __a					  =     decoder_layers
         __a					  =     decoder_attention_heads
         __a					  =     decoder_ffn_dim
         __a					  =     encoder_ffn_dim
         __a					  =     dropout
         __a					  =     attention_dropout
         __a					  =     activation_dropout
         __a					  =     activation_function
         __a					  =     init_std
         __a					  =     encoder_layerdrop
         __a					  =     decoder_layerdrop
         __a					  =     use_cache
         __a					  =     encoder_layers
         __a					  =     scale_embedding  # scale factor will be sqrt(d_model) if True
         __a					  =     max_source_positions
         __a					  =     max_target_positions

         # Audio Classification-specific parameters. Feel free to ignore for other classes.
         __a					  =     classifier_proj_size
         __a					  =     use_weighted_layer_sum

         # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
         __a					  =     apply_spec_augment
         __a					  =     mask_time_prob
         __a					  =     mask_time_length
         __a					  =     mask_time_min_masks
         __a					  =     mask_feature_prob
         __a					  =     mask_feature_length
         __a					  =     mask_feature_min_masks

         __a					  =     median_filter_width

         super().__init__(
             pad_token_id=lowerCamelCase			, bos_token_id=lowerCamelCase			, eos_token_id=lowerCamelCase			, is_encoder_decoder=lowerCamelCase			, decoder_start_token_id=lowerCamelCase			, suppress_tokens=lowerCamelCase			, begin_suppress_tokens=lowerCamelCase			, **lowerCamelCase			, )







class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):




      @property
      def 			__UpperCamelCase				(	self   )				->Mapping[str, Mapping[int, str]]:



         '''simple docstring'''
         __a					  =     OrderedDict(
             [
                 ('input_features', {0: 'batch', 1: 'feature_size', 2: 'encoder_sequence'}),
             ]   )
         if self.use_past:
            __a					  =     {0: 'batch'}
         else:
            __a					  =     {0: 'batch', 1: 'decoder_sequence'}

         if self.use_past:
            self.fill_with_past_key_values_(lowerCamelCase			, direction='inputs'   )

         return common_inputs




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = -1			, lowerCamelCase = -1			, lowerCamelCase = False			, lowerCamelCase = None			, lowerCamelCase = 2_2050			, lowerCamelCase = 5.0			, lowerCamelCase = 220			, )				->Mapping[str, Any]:



         '''simple docstring'''
         __a					  =     OrderedDict()
         __a					  =     OnnxConfig.generate_dummy_inputs(
             self			, preprocessor=preprocessor.feature_extractor			, batch_size=lowerCamelCase			, framework=lowerCamelCase			, sampling_rate=lowerCamelCase			, time_duration=lowerCamelCase			, frequency=lowerCamelCase			, )
         __a					  =     encoder_inputs['input_features'].shape[2]
         __a					  =     encoder_sequence_length // 2 if self.use_past else seq_length

         __a					  =     super().generate_dummy_inputs(
             preprocessor.tokenizer			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )

         __a					  =     encoder_inputs.pop('input_features'   )
         __a					  =     decoder_inputs.pop('decoder_input_ids'   )

         if "past_key_values" in decoder_inputs:
            __a					  =     decoder_inputs.pop('past_key_values'   )

         return dummy_inputs






      @property
      def 			__UpperCamelCase				(	self   )				->float:



         '''simple docstring'''
         return 1e-3