

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


SCREAMING_SNAKE_CASE_        =      logging.get_logger(__name__)

class 				a  (	UpperCAmelCase				):
						_lowercase    						=			["audio_values", "audio_mask"]


						def __init__(       self						,					A_=2048						,					A_=1						,					A_=[16, 16]						,					A_=128						,					A_=44100						,					A_=86						,					A_=2048						,					A_=0.0						,					**A_						,					):







										'''simple docstring'''


										super().__init__(
										    feature_size=A_						,					sampling_rate=A_						,					padding_value=A_						,					**A_						,					)

										_UpperCAmelCase       :		Any 		=    spectrogram_length
										_UpperCAmelCase       :		int 		=    num_channels
										_UpperCAmelCase       :		List[str] 		=    patch_size
										_UpperCAmelCase       :		Optional[int] 		=    feature_size // self.patch_size[1]
										_UpperCAmelCase       :		Optional[Any] 		=    n_fft
										_UpperCAmelCase       :		Optional[Any] 		=    sampling_rate // hop_length_to_sampling_rate
										_UpperCAmelCase       :		Any 		=    sampling_rate
										_UpperCAmelCase       :		List[str] 		=    padding_value
										_UpperCAmelCase       :		str 		=    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        _UpperCAmelCase				(       self						,					A_	):







										'''simple docstring'''


										_UpperCAmelCase       :		str 		=    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						,					)
										_UpperCAmelCase       :		Optional[int] 		=    log_spec[:, :-1]
										_UpperCAmelCase       :		List[Any] 		=    log_spec - 20.0
										_UpperCAmelCase       :		List[str] 		=    np.clip(log_spec / 40.0						,					-2.0						,					0.0	) + 1.0
										return log_spec





						def __call__(       self						,					A_						,					A_ = None						,					A_ = True						,					A_ = None						,					A_ = False						,					A_ = False						,					**A_						,					):







										'''simple docstring'''


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

										_UpperCAmelCase       :		Optional[int] 		=    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}'	)
										_UpperCAmelCase       :		Optional[Any] 		=    is_batched_numpy or (
										    isinstance(A_						,					(list, tuple)	) and (isinstance(raw_speech[0]						,					(np.ndarray, tuple, list)	))
										)
										if is_batched:
														_UpperCAmelCase       :		List[Any] 		=    [np.asarray([speech]						,					dtype=np.floataa	).T for speech in raw_speech]
										elif not is_batched and not isinstance(A_						,					np.ndarray	):
														_UpperCAmelCase       :		Optional[Any] 		=    np.asarray(A_						,					dtype=np.floataa	)
										elif isinstance(A_						,					np.ndarray	) and raw_speech.dtype is np.dtype(np.floataa	):
														_UpperCAmelCase       :		Optional[int] 		=    raw_speech.astype(np.floataa	)
										# always return batch
										if not is_batched:
														_UpperCAmelCase       :		List[Any] 		=    [np.asarray([raw_speech]	).T]

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

										# Create audio attention mask
										_UpperCAmelCase       :		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:
														_UpperCAmelCase       :		str 		=    [
														    (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
														]
														_UpperCAmelCase       :		int 		=    np.array(A_	).astype(np.floataa	)

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

										# return as BatchFeature
										if return_attention_mask:
														_UpperCAmelCase       :		Tuple 		=    {"audio_values": padded_audio_features, "audio_mask": audio_mask}
										else:
														_UpperCAmelCase       :		Tuple 		=    {"audio_values": padded_audio_features}

										_UpperCAmelCase       :		Tuple 		=    BatchFeature(data=A_						,					tensor_type=A_	)
										return encoded_inputs





from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
					from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


SCREAMING_SNAKE_CASE_        =      logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_        =      {
    'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json',
    'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json',
    'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json',
    'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json',
    'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json',
    'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json',
}

class 				a  (	UpperCAmelCase				):
						_lowercase    						=			"bloom"
						_lowercase    						=			["past_key_values"]
						_lowercase    						=			{
						    "num_hidden_layers": "n_layer",
						    "num_attention_heads": "n_head",
						}


						def __init__(       self						,					A_=250880						,					A_=64						,					A_=2						,					A_=8						,					A_=1e-5						,					A_=0.02						,					A_=True						,					A_=1						,					A_=2						,					A_=False						,					A_=0.0						,					A_=0.0						,					A_=1						,					A_=False						,					**A_						,					):







										'''simple docstring'''


										_UpperCAmelCase       :		List[str] 		=    vocab_size
										# Backward compatibility with n_embed kwarg
										_UpperCAmelCase       :		str 		=    kwargs.pop("n_embed"						,					A_	)
										_UpperCAmelCase       :		List[Any] 		=    hidden_size if n_embed is None else n_embed
										_UpperCAmelCase       :		Dict 		=    n_layer
										_UpperCAmelCase       :		Optional[Any] 		=    n_head
										_UpperCAmelCase       :		Optional[Any] 		=    layer_norm_epsilon
										_UpperCAmelCase       :		List[Any] 		=    initializer_range
										_UpperCAmelCase       :		Union[str, Any] 		=    use_cache
										_UpperCAmelCase       :		List[str] 		=    pretraining_tp
										_UpperCAmelCase       :		List[Any] 		=    apply_residual_connection_post_layernorm
										_UpperCAmelCase       :		int 		=    hidden_dropout
										_UpperCAmelCase       :		List[str] 		=    attention_dropout

										_UpperCAmelCase       :		int 		=    bos_token_id
										_UpperCAmelCase       :		str 		=    eos_token_id
										_UpperCAmelCase       :		Optional[Any] 		=    slow_but_exact

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







class 				a  (	UpperCAmelCase				):
						_lowercase    						=			version.parse("1.12"				)


						def __init__(       self						,					A_						,					A_ = "default"						,					A_ = None						,					A_ = False						,					):







										'''simple docstring'''


										super().__init__(A_						,					task=A_						,					patching_specs=A_						,					use_past=A_	)
										if not getattr(self._config						,					"pad_token_id"						,					A_	):
														# TODO: how to do that better?
														_UpperCAmelCase       :		str 		=    0


						@property
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		List[Any] 		=    OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}	)
										if self.use_past:
														# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
														self.fill_with_past_key_values_(A_						,					direction="inputs"						,					inverted_values_shape=A_	)
														_UpperCAmelCase       :		Optional[int] 		=    {0: "batch", 1: "past_sequence + sequence"}
										else:
														_UpperCAmelCase       :		Union[str, Any] 		=    {0: "batch", 1: "sequence"}

										return common_inputs


						@property
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										return self._config.n_layer


						@property
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										return self._config.n_head


						@property
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										return 1e-3


						def        _UpperCAmelCase				(       self						,					A_						,					A_ = -1						,					A_ = -1						,					A_ = False						,					A_ = None						,					):







										'''simple docstring'''


										_UpperCAmelCase       :		int 		=    super(A_						,					self	).generate_dummy_inputs(
										    A_						,					batch_size=A_						,					seq_length=A_						,					is_pair=A_						,					framework=A_	)

										# We need to order the input in the way they appears in the forward()
										_UpperCAmelCase       :		Tuple 		=    OrderedDict({"input_ids": common_inputs["input_ids"]}	)

										# Need to add the past_keys
										if self.use_past:
														if not is_torch_available():
																		raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."	)
														else:
																		import torch

																		_UpperCAmelCase		,							_UpperCAmelCase       :		Union[str, Any] 		=    common_inputs["input_ids"].shape
																		# Not using the same length for past_key_values
																		_UpperCAmelCase       :		List[Any] 		=    seqlen + 2
																		_UpperCAmelCase       :		Dict 		=    self._config.hidden_size // self.num_attention_heads
																		_UpperCAmelCase       :		List[Any] 		=    (
																		    batch * self.num_attention_heads,
																		    head_dim,
																		    past_key_values_length,
																		)
																		_UpperCAmelCase       :		Optional[Any] 		=    (
																		    batch * self.num_attention_heads,
																		    past_key_values_length,
																		    head_dim,
																		)
																		_UpperCAmelCase       :		Tuple 		=    [
																		    (torch.zeros(A_	), torch.zeros(A_	)) for _ in range(self.num_layers	)
																		]

										_UpperCAmelCase       :		int 		=    common_inputs["attention_mask"]
										if self.use_past:
														_UpperCAmelCase       :		str 		=    ordered_inputs["attention_mask"].dtype
														_UpperCAmelCase       :		Optional[int] 		=    torch.cat(
														    [ordered_inputs["attention_mask"], torch.ones(A_						,					A_						,					dtype=A_	)]						,					dim=1	)

										return ordered_inputs





						@property
						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										return 13


