



from typing import Callable, Dict, Optional, Tuple

import torch
from torch import nn
from torch.distributions import (
    AffineTransform,
    Distribution,
    Independent,
    NegativeBinomial,
    Normal,
    StudentT,
    TransformedDistribution,
)



class 				lowerCAmelCase_		(  a__							):
       def __init__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=None,			SCREAMING_SNAKE_CASE_=None,			SCREAMING_SNAKE_CASE_=0       )     ->		Optional[int]:
          UpperCamelCase    :  List[str]							=	1.0 if scale is None else scale
          UpperCamelCase    :  Optional[Any]							=	0.0 if loc is None else loc

          super().__init__(SCREAMING_SNAKE_CASE_,			[AffineTransform(loc=self.loc,			scale=self.scale,			event_dim=SCREAMING_SNAKE_CASE_       )]       )
       @property
       def 					snake_case_       (			self       )     ->		List[Any]:
          return self.base_dist.mean * self.scale + self.loc
       @property
       def 					snake_case_       (			self       )     ->		int:
          return self.base_dist.variance * self.scale**2






       @property
       def 					snake_case_       (			self       )     ->		Any:
          return self.variance.sqrt()






class 				lowerCAmelCase_		(  nn.Module							):
       def __init__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_       )     ->		None:
          super().__init__(**SCREAMING_SNAKE_CASE_       )
          UpperCamelCase    :  str							=	args_dim
          UpperCamelCase    :  Dict							=	nn.ModuleList([nn.Linear(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       ) for dim in args_dim.values()]       )
          UpperCamelCase    :  Optional[int]							=	domain_map






       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_       )     ->		Tuple[torch.Tensor]:
          UpperCamelCase    :  str							=	[proj(SCREAMING_SNAKE_CASE_       ) for proj in self.proj]

          return self.domain_map(*SCREAMING_SNAKE_CASE_       )






class 				lowerCAmelCase_		(  nn.Module							):
       def __init__(			self,			SCREAMING_SNAKE_CASE_       )     ->		Union[str, Any]:
          super().__init__()
          UpperCamelCase    :  int							=	function






       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			*SCREAMING_SNAKE_CASE_       )     ->		Any:
          return self.function(SCREAMING_SNAKE_CASE_,			*SCREAMING_SNAKE_CASE_       )






class 				lowerCAmelCase_		:
       UpperCAmelCase__				:					type
       UpperCAmelCase__				:					int
       UpperCAmelCase__				:					Dict[str, int]
       def __init__(			self,			SCREAMING_SNAKE_CASE_ = 1       )     ->		None:
          UpperCamelCase    :  Optional[Any]							=	dim
          UpperCamelCase    :  List[Any]							=	{k: dim * self.args_dim[k] for k in self.args_dim}
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_       )     ->		Tuple:
          if self.dim == 1:
             return self.distribution_class(*SCREAMING_SNAKE_CASE_       )
          else:
             return Independent(self.distribution_class(*SCREAMING_SNAKE_CASE_       ),			1       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			)     ->		Distribution:
          UpperCamelCase    :  Dict							=	self._base_distribution(SCREAMING_SNAKE_CASE_       )
          if loc is None and scale is None:
             return distr
          else:
             return AffineTransformed(SCREAMING_SNAKE_CASE_,			loc=SCREAMING_SNAKE_CASE_,			scale=SCREAMING_SNAKE_CASE_,			event_dim=self.event_dim       )
       @property
       def 					snake_case_       (			self       )     ->		Tuple:
          return () if self.dim == 1 else (self.dim,)
       @property
       def 					snake_case_       (			self       )     ->		int:
          return len(self.event_shape       )
       @property
       def 					snake_case_       (			self       )     ->		float:
          return 0.0
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_       )     ->		nn.Module:
          return ParameterProjection(
              in_features=SCREAMING_SNAKE_CASE_,			args_dim=self.args_dim,			domain_map=LambdaLayer(self.domain_map       ),			)
       def 					snake_case_       (			self,			*SCREAMING_SNAKE_CASE_       )     ->		int:
          raise NotImplementedError()






       @staticmethod
       def 					snake_case_       (			SCREAMING_SNAKE_CASE_       )     ->		torch.Tensor:
          return (x + torch.sqrt(torch.square(SCREAMING_SNAKE_CASE_       ) + 4.0       )) / 2.0






class 				lowerCAmelCase_		(  a__							):
       UpperCAmelCase__				:					Dict[str, int]        =  {"df": 1, "loc": 1, "scale": 1}
       UpperCAmelCase__				:					type        =  StudentT
       @classmethod
       def 					snake_case_       (			cls,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		List[Any]:
          UpperCamelCase    :  int							=	cls.squareplus(SCREAMING_SNAKE_CASE_       ).clamp_min(torch.finfo(scale.dtype       ).eps       )
          UpperCamelCase    :  List[str]							=	2.0 + cls.squareplus(SCREAMING_SNAKE_CASE_       )
          return df.squeeze(-1       ), loc.squeeze(-1       ), scale.squeeze(-1       )






class 				lowerCAmelCase_		(  a__							):
       UpperCAmelCase__				:					Dict[str, int]        =  {"loc": 1, "scale": 1}
       UpperCAmelCase__				:					type        =  Normal
       @classmethod
       def 					snake_case_       (			cls,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Any:
          UpperCamelCase    :  List[Any]							=	cls.squareplus(SCREAMING_SNAKE_CASE_       ).clamp_min(torch.finfo(scale.dtype       ).eps       )
          return loc.squeeze(-1       ), scale.squeeze(-1       )






class 				lowerCAmelCase_		(  a__							):
       UpperCAmelCase__				:					Dict[str, int]        =  {"total_count": 1, "logits": 1}
       UpperCAmelCase__				:					type        =  NegativeBinomial
       @classmethod
       def 					snake_case_       (			cls,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		str:
          UpperCamelCase    :  Tuple							=	cls.squareplus(SCREAMING_SNAKE_CASE_       )
          return total_count.squeeze(-1       ), logits.squeeze(-1       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_       )     ->		Distribution:
          UpperCamelCase	,     UpperCamelCase    :  Union[str, Any]							=	distr_args
          if self.dim == 1:
             return self.distribution_class(total_count=SCREAMING_SNAKE_CASE_,			logits=SCREAMING_SNAKE_CASE_       )
          else:
             return Independent(self.distribution_class(total_count=SCREAMING_SNAKE_CASE_,			logits=SCREAMING_SNAKE_CASE_       ),			1       )






       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None       )     ->		Distribution:
          UpperCamelCase	,     UpperCamelCase    :  List[Any]							=	distr_args

          if scale is not None:
             # See scaling property of Gamma.
             logits += scale.log()

          return self._base_distribution((total_count, logits)       )








from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase          =       logging.get_logger(__name__)

__UpperCAmelCase          =       {
    '''facebook/s2t-wav2vec2-large-en-de''': (
        '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json'''
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}



class 				lowerCAmelCase_		(  a__							):
       UpperCAmelCase__				:					Optional[Any]        =  "speech_to_text_2"
       UpperCAmelCase__				:					List[Any]        =  ["past_key_values"]
       UpperCAmelCase__				:					Any        =  {"num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model"}
       def __init__(			self,			SCREAMING_SNAKE_CASE_=1_0000,			SCREAMING_SNAKE_CASE_=6,			SCREAMING_SNAKE_CASE_=2048,			SCREAMING_SNAKE_CASE_=4,			SCREAMING_SNAKE_CASE_=0.0,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_="relu",			SCREAMING_SNAKE_CASE_=256,			SCREAMING_SNAKE_CASE_=0.1,			SCREAMING_SNAKE_CASE_=0.0,			SCREAMING_SNAKE_CASE_=0.0,			SCREAMING_SNAKE_CASE_=0.02,			SCREAMING_SNAKE_CASE_=2,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=1,			SCREAMING_SNAKE_CASE_=0,			SCREAMING_SNAKE_CASE_=2,			SCREAMING_SNAKE_CASE_=1024,			**SCREAMING_SNAKE_CASE_,			)     ->		int:
          UpperCamelCase    :  Optional[int]							=	vocab_size
          UpperCamelCase    :  List[str]							=	d_model
          UpperCamelCase    :  List[str]							=	decoder_ffn_dim
          UpperCamelCase    :  Optional[Any]							=	decoder_layers
          UpperCamelCase    :  Any							=	decoder_attention_heads
          UpperCamelCase    :  Tuple							=	dropout
          UpperCamelCase    :  str							=	attention_dropout
          UpperCamelCase    :  str							=	activation_dropout
          UpperCamelCase    :  Union[str, Any]							=	activation_function
          UpperCamelCase    :  Optional[int]							=	init_std
          UpperCamelCase    :  Tuple							=	decoder_layerdrop
          UpperCamelCase    :  Dict							=	use_cache
          UpperCamelCase    :  Any							=	decoder_layers
          UpperCamelCase    :  Union[str, Any]							=	scale_embedding  # scale factor will be sqrt(d_model) if True
          UpperCamelCase    :  Union[str, Any]							=	max_target_positions

          super().__init__(
              pad_token_id=SCREAMING_SNAKE_CASE_,			bos_token_id=SCREAMING_SNAKE_CASE_,			eos_token_id=SCREAMING_SNAKE_CASE_,			decoder_start_token_id=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_,			)



