






"""simple docstring"""







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


lowerCamelCase_       :       Any      						=  logging.get_logger(__name__)

lowerCamelCase_       :       Optional[int]      						=  {
    """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      ):






 """simple docstring"""




 __lowerCAmelCase			 =  "bloom"
 __lowerCAmelCase			 =  ["past_key_values"]
 __lowerCAmelCase			 =  {
     "num_hidden_layers": "n_layer",
     "num_attention_heads": "n_head",
 }


 def __init__(		self			,   __A=25_0880			,   __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			,   ) ->					Any:
     a					=vocab_size
     # Backward compatibility with n_embed kwarg
     a					=kwargs.pop('''n_embed'''			,   _UpperCAmelCase						)
     a					=hidden_size if n_embed is None else n_embed
     a					=n_layer
     a					=n_head
     a					=layer_norm_epsilon
     a					=initializer_range
     a					=use_cache
     a					=pretraining_tp
     a					=apply_residual_connection_post_layernorm
     a					=hidden_dropout
     a					=attention_dropout

     a					=bos_token_id
     a					=eos_token_id
     a					=slow_but_exact

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






class 				__A    (	_UpperCAmelCase      ):






 """simple docstring"""




 __lowerCAmelCase			 =  version.parse("1.12"      )


 def __init__(		self			,   __A			,   __A = "default"			,   __A = None			,   __A = False			,   ) ->					Dict:
     super().__init__(_UpperCAmelCase			,   task=_UpperCAmelCase			,   patching_specs=_UpperCAmelCase			,   use_past=_UpperCAmelCase						)
     if not getattr(self._config			,   '''pad_token_id'''			,   _UpperCAmelCase						):
         # TODO: how to do that better?
         a					=0


 @property
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Any:
     a					=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_(_UpperCAmelCase			,   direction='''inputs'''			,   inverted_values_shape=_UpperCAmelCase						)
         a					={0: '''batch''', 1: '''past_sequence + sequence'''}
     else:
         a					={0: '''batch''', 1: '''sequence'''}

     return common_inputs


 @property
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Dict:
     return self._config.n_layer


 @property
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[Any]:
     return self._config.n_head


 @property
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     return 1E-3


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A = -1			,   __A = -1			,   __A = False			,   __A = None			,   ) ->					Union[str, Any]:
     a					=super(_UpperCAmelCase			,   self						).generate_dummy_inputs(
         _UpperCAmelCase			,   batch_size=_UpperCAmelCase			,   seq_length=_UpperCAmelCase			,   is_pair=_UpperCAmelCase			,   framework=_UpperCAmelCase						)

     # We need to order the input in the way they appears in the forward()
     a					=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

             a					=common_inputs['''input_ids'''].shape
             # Not using the same length for past_key_values
             a					=seqlen + 2
             a					=self._config.hidden_size // self.num_attention_heads
             a					=(
                 batch * self.num_attention_heads,
                 head_dim,
                 past_key_values_length,
             )
             a					=(
                 batch * self.num_attention_heads,
                 past_key_values_length,
                 head_dim,
             )
             a					=[
                 (torch.zeros(_UpperCAmelCase						), torch.zeros(_UpperCAmelCase						)) for _ in range(self.num_layers						)
             ]

     a					=common_inputs['''attention_mask''']
     if self.use_past:
         a					=ordered_inputs['''attention_mask'''].dtype
         a					=torch.cat(
             [ordered_inputs['''attention_mask'''], torch.ones(_UpperCAmelCase			,   _UpperCAmelCase			,   dtype=_UpperCAmelCase						)]			,   dim=1						)

     return ordered_inputs





 @property
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     return 13



"""simple docstring"""







import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
       from transformers import WhisperFeatureExtractor

if is_torch_available():
       import torch

lowerCamelCase_       :       Any      						=  random.Random()
def _A						(						lowercase	,   lowercase=1.0	,   lowercase=None	,   lowercase=None		):







    """simple docstring"""
    if rng is None:
        a					=global_rng

    a					=[]
    for batch_idx in range(shape[0]		):
        values.append([]		)
        for _ in range(shape[1]		):
            values[-1].append(rng.random() * scale		)

    return values






@require_torch
@require_torchaudio
class 				__A    (	unittest.TestCase      ):






 """simple docstring"""




 def __init__(		self			,   __A			,   __A=7			,   __A=400			,   __A=2000			,   __A=10			,   __A=160			,   __A=8			,   __A=0.0			,   __A=4000			,   __A=False			,   __A=True			,   ) ->					Optional[Any]:
     a					=parent
     a					=batch_size
     a					=min_seq_length
     a					=max_seq_length
     a					=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
     a					=padding_value
     a					=sampling_rate
     a					=return_attention_mask
     a					=do_normalize
     a					=feature_size
     a					=chunk_length
     a					=hop_length


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     return {
         "feature_size": self.feature_size,
         "hop_length": self.hop_length,
         "chunk_length": self.chunk_length,
         "padding_value": self.padding_value,
         "sampling_rate": self.sampling_rate,
         "return_attention_mask": self.return_attention_mask,
         "do_normalize": self.do_normalize,
     }





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A=False			,   __A=False						) ->					str:
     def _flatten(__A						):
         return list(itertools.chain(*__A						)						)

     if equal_length:
         a					=[floats_list((self.max_seq_length, self.feature_size)						) for _ in range(self.batch_size						)]
     else:
         # make sure that inputs increase in size
         a					=[
             floats_list((x, self.feature_size)						)
             for x in range(self.min_seq_length			,   self.max_seq_length			,   self.seq_length_diff						)
         ]
     if numpify:
         a					=[np.asarray(__A						) for x in speech_inputs]
     return speech_inputs






@require_torch
@require_torchaudio
class 				__A    (	_SCREAMING_SNAKE_CASE,   unittest.TestCase      ):






 """simple docstring"""




 __lowerCAmelCase			 =  WhisperFeatureExtractor if is_speech_available() else None


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Dict:
     a					=WhisperFeatureExtractionTester(self						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a					=self.feature_extraction_class(**self.feat_extract_dict						)

     with tempfile.TemporaryDirectory() as tmpdirname:
         a					=feat_extract_first.save_pretrained(__A						)[0]
         check_json_file_has_correct_format(__A						)
         a					=self.feature_extraction_class.from_pretrained(__A						)

     a					=feat_extract_first.to_dict()
     a					=feat_extract_second.to_dict()
     a					=feat_extract_first.mel_filters
     a					=feat_extract_second.mel_filters
     self.assertTrue(np.allclose(__A			,   __A						)						)
     self.assertEqual(__A			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     a					=self.feature_extraction_class(**self.feat_extract_dict						)

     with tempfile.TemporaryDirectory() as tmpdirname:
         a					=os.path.join(__A			,   '''feat_extract.json'''						)
         feat_extract_first.to_json_file(__A						)
         a					=self.feature_extraction_class.from_json_file(__A						)

     a					=feat_extract_first.to_dict()
     a					=feat_extract_second.to_dict()
     a					=feat_extract_first.mel_filters
     a					=feat_extract_second.mel_filters
     self.assertTrue(np.allclose(__A			,   __A						)						)
     self.assertEqual(__A			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[int]:
     # Tests that all call wrap to encode_plus and batch_encode_plus
     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     # create three inputs of length 800, 1000, and 1200
     a					=[floats_list((1, x)						)[0] for x in range(800			,   1400			,   200						)]
     a					=[np.asarray(__A						) for speech_input in speech_inputs]

     # Test feature size
     a					=feature_extractor(__A			,   padding='''max_length'''			,   return_tensors='''np'''						).input_features
     self.assertTrue(input_features.ndim == 3						)
     self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames						)
     self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size						)

     # Test not batched input
     a					=feature_extractor(speech_inputs[0]			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(np_speech_inputs[0]			,   return_tensors='''np'''						).input_features
     self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test batched
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test 2-D numpy arrays are batched.
     a					=[floats_list((1, x)						)[0] for x in (800, 800, 800)]
     a					=np.asarray(__A						)
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test truncation required
     a					=[floats_list((1, x)						)[0] for x in range(200			,   (feature_extractor.n_samples + 500)			,   200						)]
     a					=[np.asarray(__A						) for speech_input in speech_inputs]

     a					=[x[: feature_extractor.n_samples] for x in speech_inputs]
     a					=[np.asarray(__A						) for speech_input in speech_inputs_truncated]

     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     import torch

     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     a					=np.random.rand(100			,   32						).astype(np.floataa						)
     a					=np_speech_inputs.tolist()

     for inputs in [py_speech_inputs, np_speech_inputs]:
         a					=feature_extractor.pad([{'''input_features''': inputs}]			,   return_tensors='''np'''						)
         self.assertTrue(np_processed.input_features.dtype == np.floataa						)
         a					=feature_extractor.pad([{'''input_features''': inputs}]			,   return_tensors='''pt'''						)
         self.assertTrue(pt_processed.input_features.dtype == torch.floataa						)


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					Dict:
     a					=load_dataset('''hf-internal-testing/librispeech_asr_dummy'''			,   '''clean'''			,   split='''validation'''						)
     # automatic decoding with librispeech
     a					=ds.sort('''id'''						).select(range(__A						)						)[:num_samples]['''audio''']

     return [x["array"] for x in speech_samples]


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Any:
     # fmt: off
     a					=torch.tensor(
         [
             0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
             0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
             0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
             -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
         ]						)
     # fmt: on

     a					=self._load_datasamples(1						)
     a					=WhisperFeatureExtractor()
     a					=feature_extractor(__A			,   return_tensors='''pt'''						).input_features
     self.assertEqual(input_features.shape			,   (1, 80, 3000)						)
     self.assertTrue(torch.allclose(input_features[0, 0, :30]			,   __A			,   atol=1E-4						)						)





 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     a					=self._load_datasamples(1						)[0]
     a					=((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
     a					=feat_extract.zero_mean_unit_var_norm([audio]			,   attention_mask=__A						)[0]

     self.assertTrue(np.all(np.mean(__A						) < 1E-3						)						)
     self.assertTrue(np.all(np.abs(np.var(__A						) - 1						) < 1E-3						)						)