





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


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

_A :   Optional[Any]  			=  {
    's-JoL/Open-Llama-V1': 'https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json',
}


class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_ ):
       _UpperCAmelCase				:  Dict			   =   'open-llama'







       def __init__(				self					:     Tuple		, A					:     str=1_0_0_0_0_0		, A					:     List[Any]=4_0_9_6		, A					:     str=1_1_0_0_8		, A					:     Optional[Any]=3_2		, A					:     List[str]=3_2		, A					:     List[Any]="silu"		, A					:     List[Any]=2_0_4_8		, A					:     Dict=0.02		, A					:     Union[str, Any]=1e-6		, A					:     Any=True		, A					:     List[Any]=0		, A					:     Optional[Any]=1		, A					:     List[str]=2		, A					:     Union[str, Any]=False		, A					:     str=True		, A					:     Any=0.1		, A					:     Union[str, Any]=0.1		, A					:     Dict=True		, A					:     Optional[Any]=True		, A					:     Optional[Any]=None		, **A					:     str		, )       ->Optional[int]:
         lowerCamelCase__       :						Dict        =							vocab_size
         lowerCamelCase__       :						str        =							max_position_embeddings
         lowerCamelCase__       :						Optional[int]        =							hidden_size
         lowerCamelCase__       :						Optional[int]        =							intermediate_size
         lowerCamelCase__       :						Dict        =							num_hidden_layers
         lowerCamelCase__       :						List[Any]        =							num_attention_heads
         lowerCamelCase__       :						List[str]        =							hidden_act
         lowerCamelCase__       :						List[str]        =							initializer_range
         lowerCamelCase__       :						Optional[int]        =							rms_norm_eps
         lowerCamelCase__       :						Optional[Any]        =							use_cache
         lowerCamelCase__       :						Tuple        =							kwargs.pop(
             '''use_memorry_efficient_attention'''		, __lowerCAmelCase      )
         lowerCamelCase__       :						Tuple        =							hidden_dropout_prob
         lowerCamelCase__       :						List[str]        =							attention_dropout_prob
         lowerCamelCase__       :						List[str]        =							use_stable_embedding
         lowerCamelCase__       :						Tuple        =							shared_input_output_embedding
         lowerCamelCase__       :						Any        =							rope_scaling
         self._rope_scaling_validation()

         super().__init__(
             pad_token_id=__lowerCAmelCase		, bos_token_id=__lowerCAmelCase		, eos_token_id=__lowerCAmelCase		, tie_word_embeddings=__lowerCAmelCase		, **__lowerCAmelCase		, )







       def 						__lowerCamelCase   (				self					:     Optional[Any]      )       ->str:
         if self.rope_scaling is None:
           return

         if not isinstance(self.rope_scaling		, __lowerCAmelCase      ) or len(self.rope_scaling      ) != 2:
           raise ValueError(
               '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
               F"got {self.rope_scaling}"      )
         lowerCamelCase__       :						Optional[int]        =							self.rope_scaling.get('''type'''		, __lowerCAmelCase      )
         lowerCamelCase__       :						Union[str, Any]        =							self.rope_scaling.get('''factor'''		, __lowerCAmelCase      )
         if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
           raise ValueError(
               F"`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}"      )
         if rope_scaling_factor is None or not isinstance(__lowerCAmelCase		, __lowerCAmelCase      ) or rope_scaling_factor <= 1.0:
           raise ValueError(F"`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}"      )










from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


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


class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_ ):
       _UpperCAmelCase				:  Any			   =   ["audio_values", "audio_mask"]







       def __init__(				self					:     Any		, A					:     Union[str, Any]=2_0_4_8		, A					:     Any=1		, A					:     int=[1_6, 1_6]		, A					:     Any=1_2_8		, A					:     List[Any]=4_4_1_0_0		, A					:     Dict=8_6		, A					:     Dict=2_0_4_8		, A					:     str=0.0		, **A					:     Union[str, Any]		, )       ->List[Any]:
          super().__init__(
              feature_size=A		, sampling_rate=A		, padding_value=A		, **A		, )

          lowerCamelCase__       :						Dict        =							spectrogram_length
          lowerCamelCase__       :						Optional[int]        =							num_channels
          lowerCamelCase__       :						str        =							patch_size
          lowerCamelCase__       :						Any        =							feature_size // self.patch_size[1]
          lowerCamelCase__       :						Union[str, Any]        =							n_fft
          lowerCamelCase__       :						Union[str, Any]        =							sampling_rate // hop_length_to_sampling_rate
          lowerCamelCase__       :						Optional[Any]        =							sampling_rate
          lowerCamelCase__       :						Optional[Any]        =							padding_value
          lowerCamelCase__       :						Union[str, Any]        =							mel_filter_bank(
              num_frequency_bins=1 + n_fft // 2		, num_mel_filters=A		, min_frequency=0.0		, max_frequency=2_20_50.0		, sampling_rate=A		, norm='''slaney'''		, mel_scale='''slaney'''		, ).T







       def 						__lowerCamelCase   (				self					:     List[Any]		, A					:     np.array      )       ->np.ndarray:
          lowerCamelCase__       :						Any        =							spectrogram(
              A		, window_function(self.n_fft		, '''hann'''      )		, frame_length=self.n_fft		, hop_length=self.hop_length		, power=2.0		, mel_filters=self.mel_filters.T		, log_mel='''dB'''		, db_range=80.0		, )
          lowerCamelCase__       :						Any        =							log_spec[:, :-1]
          lowerCamelCase__       :						int        =							log_spec - 20.0
          lowerCamelCase__       :						int        =							np.clip(log_spec / 40.0		, -2.0		, 0.0      ) + 1.0
          return log_spec







       def __call__(				self					:     Union[str, Any]		, A					:     Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]		, A					:     Optional[Union[str, TensorType]] = None		, A					:     Optional[bool] = True		, A					:     Optional[int] = None		, A					:     bool = False		, A					:     bool = False		, **A					:     str		, )       ->BatchFeature:

          if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                raise ValueError(
                    '''This feature extractor is set to support sampling rate'''
                    F" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled"
                    F" with {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__       :						List[str]        =							isinstance(A		, 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__       :						Any        =							is_batched_numpy or (
              isinstance(A		, (list, tuple)      ) and (isinstance(raw_speech[0]		, (np.ndarray, tuple, list)      ))
          )
          if is_batched:
             lowerCamelCase__       :						Optional[int]        =							[np.asarray([speech]		, dtype=np.floataa      ).T for speech in raw_speech]
          elif not is_batched and not isinstance(A		, np.ndarray      ):
             lowerCamelCase__       :						Optional[int]        =							np.asarray(A		, dtype=np.floataa      )
          elif isinstance(A		, np.ndarray      ) and raw_speech.dtype is np.dtype(np.floataa      ):
             lowerCamelCase__       :						Optional[Any]        =							raw_speech.astype(np.floataa      )
          # always return batch
          if not is_batched:
             lowerCamelCase__       :						int        =							[np.asarray([raw_speech]      ).T]

          # Convert audio signals to log mel spectrograms, truncate by time axis
          lowerCamelCase__       :						List[str]        =							[
              self._np_extract_fbank_features(waveform.squeeze()      ).T[: self.spectrogram_length] for waveform in raw_speech
          ]
          if isinstance(audio_features[0]		, A      ):
             lowerCamelCase__       :						Dict        =							[np.asarray(A		, dtype=np.floataa      ) for feature in audio_features]

          # Create audio attention mask
          lowerCamelCase__       :						Optional[Any]        =							max(
              [ceil(feature.shape[0] / self.patch_size[0]      ) * self.freq_len for feature in audio_features]      )  # The maximum number of audio patches in a batch
          if return_attention_mask:
             lowerCamelCase__       :						List[Any]        =							[
                 (ceil(feature.shape[0] / self.patch_size[0]      ) * self.freq_len) * [1]
                 + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]      ) * self.freq_len) * [0]
                 for feature in audio_features
             ]
             lowerCamelCase__       :						List[str]        =							np.array(A      ).astype(np.floataa      )

          # convert into correct format for padding
          lowerCamelCase__       :						str        =							max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
          lowerCamelCase__       :						Optional[Any]        =							np.ones([len(A      ), 1, max_time_len, self.feature_size]      ).astype(np.floataa      )
          lowerCamelCase__       :						Tuple        =							padded_audio_features * self.padding_value
          for i in range(len(A      )      ):
             lowerCamelCase__       :						int        =							audio_features[i]
             lowerCamelCase__       :						Optional[int]        =							feature

          # return as BatchFeature
          if return_attention_mask:
             lowerCamelCase__       :						Optional[int]        =							{'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask}
          else:
             lowerCamelCase__       :						Tuple        =							{'''audio_values''': padded_audio_features}

          lowerCamelCase__       :						Union[str, Any]        =							BatchFeature(data=A		, tensor_type=A      )
          return encoded_inputs



