from typing import Dict, Optional, Tuple

import torch


def SCREAMING_SNAKE_CASE			(       lowercase_:		torch.Tensor			):
							_UpperCamelCase       :    int					  =					boundaries[1] - boundaries[0]
							_UpperCamelCase       :    List[Any]					  =					boundaries + step / 2
							_UpperCamelCase       :    str					  =					torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1			)]						,  dim=0			)
							return bin_centers


def SCREAMING_SNAKE_CASE			(       lowercase_:		torch.Tensor						,  lowercase_:		torch.Tensor						,  ):
							_UpperCamelCase       :    Optional[Any]					  =					_calculate_bin_centers(lowercase_			)
							return (
							    torch.sum(aligned_distance_error_probs * bin_centers						,  dim=-1			),
							    bin_centers[-1],
							)


def SCREAMING_SNAKE_CASE			(       lowercase_:		torch.Tensor						,  lowercase_:		int = 31						,  lowercase_:		int = 64						,  **lowercase_:		Union[str, Any]						,  ):
							_UpperCamelCase       :    Optional[Any]					  =					torch.linspace(0						,  lowercase_						,  steps=(no_bins - 1)						,  device=logits.device			)

							_UpperCamelCase       :    Union[str, Any]					  =					torch.nn.functional.softmax(lowercase_						,  dim=-1			)
							_UpperCamelCase       :    str					  =					_calculate_expected_aligned_error(
							    alignment_confidence_breaks=lowercase_						,  aligned_distance_error_probs=lowercase_						,  )

							return {
							    "aligned_confidence_probs": aligned_confidence_probs,
							    "predicted_aligned_error": predicted_aligned_error,
							    "max_predicted_aligned_error": max_predicted_aligned_error,
							}





def SCREAMING_SNAKE_CASE			(       lowercase_:		torch.Tensor						,  lowercase_:		Optional[torch.Tensor] = None						,  lowercase_:		int = 31						,  lowercase_:		int = 64						,  lowercase_:		float = 1e-8						,  **lowercase_:		Tuple						,  ):
							if residue_weights is None:
														_UpperCamelCase       :    Dict					  =					logits.new_ones(logits.shape[-2]			)

							_UpperCamelCase       :    Optional[int]					  =					torch.linspace(0						,  lowercase_						,  steps=(no_bins - 1)						,  device=logits.device			)

							_UpperCamelCase       :    str					  =					_calculate_bin_centers(lowercase_			)
							torch.sum(lowercase_			)
							_UpperCamelCase       :    List[Any]					  =					logits.shape[-2]
							_UpperCamelCase       :    Optional[int]					  =					max(lowercase_						,  19			)

							_UpperCamelCase       :    Tuple					  =					1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8

							_UpperCamelCase       :    Optional[int]					  =					torch.nn.functional.softmax(lowercase_						,  dim=-1			)

							_UpperCamelCase       :    Any					  =					1.0 / (1 + (bin_centers**2) / (da**2))
							_UpperCamelCase       :    Union[str, Any]					  =					torch.sum(probs * tm_per_bin						,  dim=-1			)

							_UpperCamelCase       :    Union[str, Any]					  =					residue_weights / (eps + residue_weights.sum())
							_UpperCamelCase       :    Union[str, Any]					  =					torch.sum(predicted_tm_term * normed_residue_mask						,  dim=-1			)

							_UpperCamelCase       :    Dict					  =					per_alignment * residue_weights

							_UpperCamelCase       :    int					  =					(weighted == torch.max(lowercase_			)).nonzero()[0]
							return per_alignment[tuple(lowercase_			)]




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


UpperCAmelCase     						=			{
    '''configuration_speech_to_text_2''': ['''SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2Text2Config'''],
    '''processing_speech_to_text_2''': ['''Speech2Text2Processor'''],
    '''tokenization_speech_to_text_2''': ['''Speech2Text2Tokenizer'''],
}


try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase     						=			[
     '''SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''Speech2Text2ForCausalLM''',
     '''Speech2Text2PreTrainedModel''',
 ]


if TYPE_CHECKING:
 from .configuration_speech_to_text_a import SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextaConfig
 from .processing_speech_to_text_a import SpeechaTextaProcessor
 from .tokenization_speech_to_text_a import SpeechaTextaTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_speech_to_text_a import (
      SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST,
      SpeechaTextaForCausalLM,
      SpeechaTextaPreTrainedModel,
  )

else:
 import sys

 UpperCAmelCase     						=			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



