


'''simple docstring'''

from dataclasses import asdict, dataclass
from typing import Optional

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


a_  :				str						= logging.get_logger(__name__)

# TODO Update this
a_  :				List[str]						= {
    """facebook/esm-1b""": """https://huggingface.co/facebook/esm-1b/resolve/main/config.json""",
    # See all ESM models at https://huggingface.co/models?filter=esm
}





class      snake_case    (    lowercase			):





  """simple docstring"""




  _lowerCamelCase		    =  "esm"






  def __init__(    self			,  UpperCamelCase=None			,  UpperCamelCase=None			,  UpperCamelCase=None			,  UpperCamelCase=768			,  UpperCamelCase=12			,  UpperCamelCase=12			,  UpperCamelCase=3072			,  UpperCamelCase=0.1			,  UpperCamelCase=0.1			,  UpperCamelCase=1026			,  UpperCamelCase=0.02			,  UpperCamelCase=1e-12			,  UpperCamelCase="absolute"			,  UpperCamelCase=True			,  UpperCamelCase=None			,  UpperCamelCase=False			,  UpperCamelCase=False			,  UpperCamelCase=None			,  UpperCamelCase=None			,  **UpperCamelCase			,  ):







   """simple docstring"""
   super().__init__(pad_token_id=UpperCamelCase			,  mask_token_id=UpperCamelCase			,  **UpperCamelCase     )

   lowerCamelCase_      =  vocab_size
   lowerCamelCase_      =  hidden_size
   lowerCamelCase_      =  num_hidden_layers
   lowerCamelCase_      =  num_attention_heads
   lowerCamelCase_      =  intermediate_size
   lowerCamelCase_      =  hidden_dropout_prob
   lowerCamelCase_      =  attention_probs_dropout_prob
   lowerCamelCase_      =  max_position_embeddings
   lowerCamelCase_      =  initializer_range
   lowerCamelCase_      =  layer_norm_eps
   lowerCamelCase_      =  position_embedding_type
   lowerCamelCase_      =  use_cache
   lowerCamelCase_      =  emb_layer_norm_before
   lowerCamelCase_      =  token_dropout
   lowerCamelCase_      =  is_folding_model
   if is_folding_model:
    if esmfold_config is None:
     logger.info("No esmfold_config supplied for folding model, using default values."     )
     lowerCamelCase_      =  EsmFoldConfig()
    elif isinstance(UpperCamelCase			,  UpperCamelCase     ):
     lowerCamelCase_      =  EsmFoldConfig(**UpperCamelCase     )
    lowerCamelCase_      =  esmfold_config
    if vocab_list is None:
     logger.warning("No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!"     )
     lowerCamelCase_      =  get_default_vocab_list()
    else:
     lowerCamelCase_      =  vocab_list
   else:
    lowerCamelCase_      =  None
    lowerCamelCase_      =  None
   if self.esmfold_config is not None and getattr(self.esmfold_config			,  "use_esm_attn_map"			,  UpperCamelCase     ):
    raise ValueError("The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!"     )







  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  super().to_dict()
   if isinstance(self.esmfold_config			,  UpperCamelCase     ):
    lowerCamelCase_      =  self.esmfold_config.to_dict()
   return output





@dataclass
class      snake_case    :





  """simple docstring"""




  _lowerCamelCase		    =  None
  _lowerCamelCase		    =  True
  _lowerCamelCase		    =  False
  _lowerCamelCase		    =  False
  _lowerCamelCase		    =  False
  _lowerCamelCase		    =  0

  _lowerCamelCase		    =  True
  _lowerCamelCase		    =  False

  _lowerCamelCase		    =  1_28
  _lowerCamelCase		    =  None






  def   snake_case		(    self     ):







   """simple docstring"""
   if self.trunk is None:
    lowerCamelCase_      =  TrunkConfig()
   elif isinstance(self.trunk			,  UpperCamelCase     ):
    lowerCamelCase_      =  TrunkConfig(**self.trunk     )







  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  asdict(self     )
   lowerCamelCase_      =  self.trunk.to_dict()
   return output





@dataclass
class      snake_case    :





  """simple docstring"""




  _lowerCamelCase		    =  48
  _lowerCamelCase		    =  10_24
  _lowerCamelCase		    =  1_28
  _lowerCamelCase		    =  32
  _lowerCamelCase		    =  32
  _lowerCamelCase		    =  32
  _lowerCamelCase		    =  0
  _lowerCamelCase		    =  0
  _lowerCamelCase		    =  False
  _lowerCamelCase		    =  4
  _lowerCamelCase		    =  1_28
  _lowerCamelCase		    =  None






  def   snake_case		(    self     ):







   """simple docstring"""
   if self.structure_module is None:
    lowerCamelCase_      =  StructureModuleConfig()
   elif isinstance(self.structure_module			,  UpperCamelCase     ):
    lowerCamelCase_      =  StructureModuleConfig(**self.structure_module     )

   if self.max_recycles <= 0:
    raise ValueError(f'''`max_recycles` should be positive, got {self.max_recycles}.'''     )
   if self.sequence_state_dim % self.sequence_state_dim != 0:
    raise ValueError(
        "`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got"
        f''' {self.sequence_state_dim} and {self.sequence_state_dim}.'''     )
   if self.pairwise_state_dim % self.pairwise_state_dim != 0:
    raise ValueError(
        "`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got"
        f''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.'''     )

   lowerCamelCase_      =  self.sequence_state_dim // self.sequence_head_width
   lowerCamelCase_      =  self.pairwise_state_dim // self.pairwise_head_width

   if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width:
    raise ValueError(
        "`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got"
        f''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.'''     )
   if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width:
    raise ValueError(
        "`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got"
        f''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.'''     )
   if self.pairwise_state_dim % 2 != 0:
    raise ValueError(f'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.'''     )

   if self.dropout >= 0.4:
    raise ValueError(f'''`dropout` should not be greater than 0.4, got {self.dropout}.'''     )







  def   snake_case		(    self     ):







   """simple docstring"""
   lowerCamelCase_      =  asdict(self     )
   lowerCamelCase_      =  self.structure_module.to_dict()
   return output





@dataclass
class      snake_case    :





  """simple docstring"""




  _lowerCamelCase		    =  3_84
  _lowerCamelCase		    =  1_28
  _lowerCamelCase		    =  16
  _lowerCamelCase		    =  1_28
  _lowerCamelCase		    =  12
  _lowerCamelCase		    =  4
  _lowerCamelCase		    =  8
  _lowerCamelCase		    =  0.1
  _lowerCamelCase		    =  8
  _lowerCamelCase		    =  1
  _lowerCamelCase		    =  2
  _lowerCamelCase		    =  7
  _lowerCamelCase		    =  10
  _lowerCamelCase		    =  1e-8
  _lowerCamelCase		    =  1e5






  def   snake_case		(    self     ):







   """simple docstring"""
   return asdict(self     )





def __snake_case					(      ):
 return (
     "<cls>",
     "<pad>",
     "<eos>",
     "<unk>",
     "L",
     "A",
     "G",
     "V",
     "S",
     "E",
     "R",
     "T",
     "I",
     "D",
     "P",
     "K",
     "Q",
     "N",
     "F",
     "Y",
     "M",
     "H",
     "W",
     "C",
     "X",
     "B",
     "U",
     "Z",
     "O",
     ".",
     "-",
     "<null_1>",
     "<mask>",
 )







'''simple docstring'''

from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


a_  :				int						= logging.get_logger(__name__)





class      snake_case    (    lowercase			):





  """simple docstring"""




  _lowerCamelCase		    =  ["input_features", "attention_mask"]






  def __init__(    self			,  UpperCamelCase=80			,  UpperCamelCase=1_6000			,  UpperCamelCase=80			,  UpperCamelCase=0.0			,  UpperCamelCase=True			,  UpperCamelCase=True			,  UpperCamelCase=True			,  **UpperCamelCase			,  ):







   """simple docstring"""
   super().__init__(feature_size=UpperCamelCase			,  sampling_rate=UpperCamelCase			,  padding_value=UpperCamelCase			,  **UpperCamelCase     )
   lowerCamelCase_      =  num_mel_bins
   lowerCamelCase_      =  do_ceptral_normalize
   lowerCamelCase_      =  normalize_means
   lowerCamelCase_      =  normalize_vars
   lowerCamelCase_      =  True






  def   snake_case		(    self			,  UpperCamelCase			,  ):







   """simple docstring"""
   lowerCamelCase_      =  waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
   lowerCamelCase_      =  torch.from_numpy(UpperCamelCase     ).unsqueeze(0     )
   lowerCamelCase_      =  ta_kaldi.fbank(UpperCamelCase			,  num_mel_bins=self.num_mel_bins			,  sample_frequency=self.sampling_rate     )
   return features.numpy()






  @staticmethod
  def   snake_case		(    UpperCamelCase			,  UpperCamelCase			,  UpperCamelCase = True			,  UpperCamelCase = True			,  UpperCamelCase = 0.0			,  ):







   """simple docstring"""
   # make sure we normalize float32 arrays
   if normalize_means:
    lowerCamelCase_      =  x[:input_length].mean(axis=0     )
    lowerCamelCase_      =  np.subtract(UpperCamelCase			,  UpperCamelCase     )
   if normalize_vars:
    lowerCamelCase_      =  x[:input_length].std(axis=0     )
    lowerCamelCase_      =  np.divide(UpperCamelCase			,  UpperCamelCase     )

   if input_length < x.shape[0]:
    lowerCamelCase_      =  padding_value

   # make sure array is in float32
   lowerCamelCase_      =  x.astype(np.floataa     )

   return x






  def   snake_case		(    self			,  UpperCamelCase			,  UpperCamelCase = None     ):







   """simple docstring"""
   lowerCamelCase_      =  attention_mask.sum(-1     ) if attention_mask is not None else [x.shape[0] for x in input_features]
   return [
       self.utterance_cmvn(UpperCamelCase			,  UpperCamelCase			,  self.normalize_means			,  self.normalize_vars			,  self.padding_value     )
       for x, n in zip(UpperCamelCase			,  UpperCamelCase     )
   ]







  def __call__(    self			,  UpperCamelCase			,  UpperCamelCase = False			,  UpperCamelCase = None			,  UpperCamelCase = False			,  UpperCamelCase = None			,  UpperCamelCase = None			,  UpperCamelCase = None			,  UpperCamelCase = None			,  **UpperCamelCase			,  ):







   """simple docstring"""

   if sampling_rate is not None:
    if sampling_rate != self.sampling_rate:
     raise ValueError(
         f'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of'''
         f''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'''
         f''' {self.sampling_rate} and not {sampling_rate}.'''     )
   else:
    logger.warning(
        "It is strongly recommended to pass the `sampling_rate` argument to this function. "
        "Failing to do so can result in silent errors that might be hard to debug."     )

   lowerCamelCase_      =  isinstance(UpperCamelCase			,  np.ndarray     ) and len(raw_speech.shape     ) > 1
   if is_batched_numpy and len(raw_speech.shape     ) > 2:
    raise ValueError(f'''Only mono-channel audio is supported for input to {self}'''     )
   lowerCamelCase_      =  is_batched_numpy or (
       isinstance(UpperCamelCase			,  (list, tuple)     ) and (isinstance(raw_speech[0]			,  (np.ndarray, tuple, list)     ))
   )

   if is_batched:
    lowerCamelCase_      =  [np.asarray(UpperCamelCase			,  dtype=np.floataa     ) for speech in raw_speech]
   elif not is_batched and not isinstance(UpperCamelCase			,  np.ndarray     ):
    lowerCamelCase_      =  np.asarray(UpperCamelCase			,  dtype=np.floataa     )
   elif isinstance(UpperCamelCase			,  np.ndarray     ) and raw_speech.dtype is np.dtype(np.floataa     ):
    lowerCamelCase_      =  raw_speech.astype(np.floataa     )

   # always return batch
   if not is_batched:
    lowerCamelCase_      =  [raw_speech]

   # extract fbank features
   lowerCamelCase_      =  [self._extract_fbank_features(UpperCamelCase     ) for waveform in raw_speech]

   # convert into correct format for padding
   lowerCamelCase_      =  BatchFeature({"input_features": features}     )

   lowerCamelCase_      =  self.pad(
       UpperCamelCase			,  padding=UpperCamelCase			,  max_length=UpperCamelCase			,  truncation=UpperCamelCase			,  pad_to_multiple_of=UpperCamelCase			,  return_attention_mask=UpperCamelCase			,  **UpperCamelCase			,  )

   # make sure list is in array format
   lowerCamelCase_      =  padded_inputs.get("input_features"     )
   if isinstance(input_features[0]			,  UpperCamelCase     ):
    lowerCamelCase_      =  [np.asarray(UpperCamelCase			,  dtype=np.floataa     ) for feature in input_features]

   lowerCamelCase_      =  padded_inputs.get("attention_mask"     )
   if attention_mask is not None:
    lowerCamelCase_      =  [np.asarray(UpperCamelCase			,  dtype=np.intaa     ) for array in attention_mask]

   # Utterance-level cepstral mean and variance normalization
   if self.do_ceptral_normalize:
    lowerCamelCase_      =  (
        np.array(UpperCamelCase			,  dtype=np.intaa     )
        if self._get_padding_strategies(UpperCamelCase			,  max_length=UpperCamelCase     ) is not PaddingStrategy.DO_NOT_PAD
        else None
    )
    lowerCamelCase_      =  self.normalize(
        padded_inputs["input_features"]			,  attention_mask=UpperCamelCase     )

   if return_tensors is not None:
    lowerCamelCase_      =  padded_inputs.convert_to_tensors(UpperCamelCase     )

   return padded_inputs



