




'''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


UpperCamelCase_		:     Optional[int]          =		logging.get_logger(__name__)

class   _a							(		__lowerCAmelCase				):
     SCREAMING_SNAKE_CASE_				:       int       							=		["""input_features""", """attention_mask"""]






     def __init__(					self   ,_SCREAMING_SNAKE_CASE=80   ,_SCREAMING_SNAKE_CASE=16_000   ,_SCREAMING_SNAKE_CASE=80   ,_SCREAMING_SNAKE_CASE=0.0   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=True   ,**_SCREAMING_SNAKE_CASE   ,)				->     str:
          super().__init__(feature_size=_SCREAMING_SNAKE_CASE   ,sampling_rate=_SCREAMING_SNAKE_CASE   ,padding_value=_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )
          _snake_case				= num_mel_bins
          _snake_case				= do_ceptral_normalize
          _snake_case				= normalize_means
          _snake_case				= normalize_vars
          _snake_case				= True






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,)				->     np.ndarray:
          _snake_case				= waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
          _snake_case				= torch.from_numpy(_SCREAMING_SNAKE_CASE       ).unsqueeze(0       )
          _snake_case				= ta_kaldi.fbank(_SCREAMING_SNAKE_CASE   ,num_mel_bins=self.num_mel_bins   ,sample_frequency=self.sampling_rate       )
          return features.numpy()






     @staticmethod
     def 							_lowercase      (					_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE = True   ,_SCREAMING_SNAKE_CASE = True   ,_SCREAMING_SNAKE_CASE = 0.0   ,)				->     np.ndarray:
          # make sure we normalize float32 arrays
          if normalize_means:
               _snake_case				= x[:input_length].mean(axis=0       )
               _snake_case				= np.subtract(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )
          if normalize_vars:
               _snake_case				= x[:input_length].std(axis=0       )
               _snake_case				= np.divide(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )

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

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

          return x






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE = None       )				->     List[np.ndarray]:
          _snake_case				= attention_mask.sum(-1       ) if attention_mask is not None else [x.shape[0] for x in input_features]
          return [
              self.utterance_cmvn(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,self.normalize_means   ,self.normalize_vars   ,self.padding_value       )
              for x, n in zip(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )
          ]






     def __call__(					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE = False   ,_SCREAMING_SNAKE_CASE = None   ,_SCREAMING_SNAKE_CASE = False   ,_SCREAMING_SNAKE_CASE = None   ,_SCREAMING_SNAKE_CASE = None   ,_SCREAMING_SNAKE_CASE = None   ,_SCREAMING_SNAKE_CASE = None   ,**_SCREAMING_SNAKE_CASE   ,)				->     BatchFeature:

          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."       )

          _snake_case				= isinstance(_SCREAMING_SNAKE_CASE   ,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}"""       )
          _snake_case				= is_batched_numpy or (
              isinstance(_SCREAMING_SNAKE_CASE   ,(list, tuple)       ) and (isinstance(raw_speech[0]   ,(np.ndarray, tuple, list)       ))
          )

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

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

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

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

          _snake_case				= self.pad(
              _SCREAMING_SNAKE_CASE   ,padding=_SCREAMING_SNAKE_CASE   ,max_length=_SCREAMING_SNAKE_CASE   ,truncation=_SCREAMING_SNAKE_CASE   ,pad_to_multiple_of=_SCREAMING_SNAKE_CASE   ,return_attention_mask=_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE   ,)

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

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

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

          if return_tensors is not None:
               _snake_case				= padded_inputs.convert_to_tensors(_SCREAMING_SNAKE_CASE       )

          return padded_inputs








'''simple docstring'''





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


UpperCamelCase_		:     int          =		logging.get_logger(__name__)

UpperCamelCase_		:     Any          =		{
    '''tiiuae/falcon-40b''': '''https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json''',
    '''tiiuae/falcon-7b''': '''https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json''',
}

class   _a							(		__lowerCAmelCase				):
     SCREAMING_SNAKE_CASE_				:       List[str]       							=		"""falcon"""
     SCREAMING_SNAKE_CASE_				:       Optional[int]       							=		["""past_key_values"""]






     def __init__(					self   ,_SCREAMING_SNAKE_CASE=65_024   ,_SCREAMING_SNAKE_CASE=4_544   ,_SCREAMING_SNAKE_CASE=32   ,_SCREAMING_SNAKE_CASE=71   ,_SCREAMING_SNAKE_CASE=1e-5   ,_SCREAMING_SNAKE_CASE=0.0_2   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=0.0   ,_SCREAMING_SNAKE_CASE=0.0   ,_SCREAMING_SNAKE_CASE=None   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=11   ,_SCREAMING_SNAKE_CASE=11   ,**_SCREAMING_SNAKE_CASE   ,)				->     Dict:
          _snake_case				= vocab_size
          # Backward compatibility with n_embed kwarg
          _snake_case				= kwargs.pop("n_embed"   ,_SCREAMING_SNAKE_CASE       )
          _snake_case				= hidden_size if n_embed is None else n_embed
          _snake_case				= num_hidden_layers
          _snake_case				= num_attention_heads
          _snake_case				= layer_norm_epsilon
          _snake_case				= initializer_range
          _snake_case				= use_cache
          _snake_case				= hidden_dropout
          _snake_case				= attention_dropout

          _snake_case				= bos_token_id
          _snake_case				= eos_token_id
          _snake_case				= num_attention_heads if num_kv_heads is None else num_kv_heads
          _snake_case				= alibi
          _snake_case				= new_decoder_architecture
          _snake_case				= multi_query  # Ignored when new_decoder_architecture is True
          _snake_case				= parallel_attn
          _snake_case				= bias

          super().__init__(bos_token_id=_SCREAMING_SNAKE_CASE   ,eos_token_id=_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )






     @property
     def 							_lowercase      (					self       )				->     Union[str, Any]:
          return self.hidden_size // self.num_attention_heads






     @property
     def 							_lowercase      (					self       )				->     Optional[int]:
          return not self.alibi


