



import itertools
import random
import unittest

import numpy as np

from transformers import BatchFeature, SpeechTaFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
     import torch


SCREAMING_SNAKE_CASE_:int					=						random.Random()




def      __UpperCamelCase     (  _lowerCAmelCase				,					_lowerCAmelCase=1.0				,					_lowerCAmelCase=None				,					_lowerCAmelCase=None		)	->  Union[str, Any]:



       """simple docstring"""






       if rng is None:
              A			:	Tuple										=       global_rng

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

       return values






@require_torch
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    def __init__(	self,		lowerCamelCase__,		lowerCamelCase__=7,		lowerCamelCase__=400,		lowerCamelCase__=2000,		lowerCamelCase__=1,		lowerCamelCase__=0.0,		lowerCamelCase__=1_6000,		lowerCamelCase__=True,		lowerCamelCase__=80,		lowerCamelCase__=16,		lowerCamelCase__=64,		lowerCamelCase__="hann_window",		lowerCamelCase__=80,		lowerCamelCase__=7600,		lowerCamelCase__=1e-10,		lowerCamelCase__=True,		):
           A			:	Optional[int]										=       parent
           A			:	str										=       batch_size
           A			:	List[str]										=       min_seq_length
           A			:	Optional[Any]										=       max_seq_length
           A			:	str										=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           A			:	Tuple										=       feature_size
           A			:	List[str]										=       padding_value
           A			:	List[str]										=       sampling_rate
           A			:	Union[str, Any]										=       do_normalize
           A			:	Optional[Any]										=       num_mel_bins
           A			:	int										=       hop_length
           A			:	Optional[Any]										=       win_length
           A			:	Tuple										=       win_function
           A			:	Any										=       fmin
           A			:	str										=       fmax
           A			:	str										=       mel_floor
           A			:	Any										=       return_attention_mask






    def       _lowerCAmelCase (	self		):
           return {
               "feature_size": self.feature_size,
               "padding_value": self.padding_value,
               "sampling_rate": self.sampling_rate,
               "do_normalize": self.do_normalize,
               "num_mel_bins": self.num_mel_bins,
               "hop_length": self.hop_length,
               "win_length": self.win_length,
               "win_function": self.win_function,
               "fmin": self.fmin,
               "fmax": self.fmax,
               "mel_floor": self.mel_floor,
               "return_attention_mask": self.return_attention_mask,
           }






    def       _lowerCAmelCase (	self,		lowerCamelCase__=False,		lowerCamelCase__=False		):
           def _flatten(lowerCamelCase__		):
                  return list(itertools.chain(*lowercase_		)		)

           if equal_length:
                  A			:	List[Any]										=       floats_list((self.batch_size, self.max_seq_length)		)
           else:
                  # make sure that inputs increase in size
                  A			:	Optional[int]										=       [
                      _flatten(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			:	Optional[Any]										=       [np.asarray(lowercase_		) for x in speech_inputs]

           return speech_inputs






    def       _lowerCAmelCase (	self,		lowerCamelCase__=False,		lowerCamelCase__=False		):
           if equal_length:
                  A			:	Optional[Any]										=       [floats_list((self.max_seq_length, self.num_mel_bins)		) for _ in range(self.batch_size		)]
           else:
                  # make sure that inputs increase in size
                  A			:	Union[str, Any]										=       [
                      floats_list((x, self.num_mel_bins)		)
                      for x in range(self.min_seq_length,		self.max_seq_length,		self.seq_length_diff		)
                  ]

           if numpify:
                  A			:	Tuple										=       [np.asarray(lowercase_		) for x in speech_inputs]

           return speech_inputs






@require_torch
class SCREAMING_SNAKE_CASE__					(		_UpperCAmelCase			,						unittest.TestCase   ):




    '''simple docstring'''






    __lowerCamelCase      :			Any =				SpeechTaFeatureExtractor






    def       _lowerCAmelCase (	self		):
           A			:	Optional[Any]										=       SpeechTaFeatureExtractionTester(self		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
           self.assertTrue(np.all(np.mean(lowercase_,		axis=0		) < 1e-3		)		)
           self.assertTrue(np.all(np.abs(np.var(lowercase_,		axis=0		) - 1		) < 1e-3		)		)






    def       _lowerCAmelCase (	self		):
           # Tests that all call wrap to encode_plus and batch_encode_plus
           A			:	Union[str, Any]										=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
           # create three inputs of length 800, 1000, and 1200
           A			:	int										=       [floats_list((1, x)		)[0] for x in range(800,		1400,		200		)]
           A			:	Any										=       [np.asarray(lowercase_		) for speech_input in speech_inputs]

           # Test not batched input
           A			:	Optional[int]										=       feat_extract(speech_inputs[0],		return_tensors="""np"""		).input_values
           A			:	Any										=       feat_extract(np_speech_inputs[0],		return_tensors="""np"""		).input_values
           self.assertTrue(np.allclose(lowercase_,		lowercase_,		atol=1e-3		)		)

           # Test batched
           A			:	Dict										=       feat_extract(lowercase_,		return_tensors="""np"""		).input_values
           A			:	Optional[Any]										=       feat_extract(lowercase_,		return_tensors="""np"""		).input_values
           for enc_seq_a, enc_seq_a in zip(lowercase_,		lowercase_		):
                  self.assertTrue(np.allclose(lowercase_,		lowercase_,		atol=1e-3		)		)






    def       _lowerCAmelCase (	self		):
           A			:	Any										=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
           A			:	int										=       [floats_list((1, x)		)[0] for x in range(800,		1400,		200		)]

           A			:	List[Any]										=       ["""longest""", """max_length""", """do_not_pad"""]
           A			:	int										=       [None, 1600, None]
           for max_length, padding in zip(lowercase_,		lowercase_		):
                  A			:	Any										=       feat_extract(lowercase_,		padding=lowercase_,		max_length=lowercase_,		return_tensors="""np"""		)
                  A			:	Optional[Any]										=       processed.input_values

                  self._check_zero_mean_unit_variance(input_values[0][:800]		)
                  self.assertTrue(input_values[0][800:].sum() < 1e-6		)
                  self._check_zero_mean_unit_variance(input_values[1][:1000]		)
                  self.assertTrue(input_values[0][1000:].sum() < 1e-6		)
                  self._check_zero_mean_unit_variance(input_values[2][:1200]		)






    def       _lowerCAmelCase (	self		):
           A			:	Union[str, Any]										=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
           A			:	Any										=       range(800,		1400,		200		)
           A			:	int										=       [floats_list((1, x)		)[0] for x in lengths]

           A			:	int										=       ["""longest""", """max_length""", """do_not_pad"""]
           A			:	str										=       [None, 1600, None]

           for max_length, padding in zip(lowercase_,		lowercase_		):
                  A			:	Tuple										=       feat_extract(lowercase_,		max_length=lowercase_,		padding=lowercase_		)
                  A			:	Optional[int]										=       processed.input_values

                  self._check_zero_mean_unit_variance(input_values[0][:800]		)
                  self._check_zero_mean_unit_variance(input_values[1][:1000]		)
                  self._check_zero_mean_unit_variance(input_values[2][:1200]		)






    def       _lowerCAmelCase (	self		):
           A			:	Optional[Any]										=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
           A			:	List[Any]										=       [floats_list((1, x)		)[0] for x in range(800,		1400,		200		)]
           A			:	int										=       feat_extract(
               lowercase_,		truncation=lowercase_,		max_length=1000,		padding="""max_length""",		return_tensors="""np"""		)
           A			:	int										=       processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800]		)
           self._check_zero_mean_unit_variance(input_values[1]		)
           self._check_zero_mean_unit_variance(input_values[2]		)






    def       _lowerCAmelCase (	self		):
           A			:	Dict										=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
           A			:	Optional[Any]										=       [floats_list((1, x)		)[0] for x in range(800,		1400,		200		)]
           A			:	str										=       feat_extract(
               lowercase_,		truncation=lowercase_,		max_length=1000,		padding="""longest""",		return_tensors="""np"""		)
           A			:	List[str]										=       processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800]		)
           self._check_zero_mean_unit_variance(input_values[1, :1000]		)
           self._check_zero_mean_unit_variance(input_values[2]		)

           # make sure that if max_length < longest -> then pad to max_length
           self.assertTrue(input_values.shape == (3, 1000)		)

           A			:	List[str]										=       [floats_list((1, x)		)[0] for x in range(800,		1400,		200		)]
           A			:	Optional[int]										=       feat_extract(
               lowercase_,		truncation=lowercase_,		max_length=2000,		padding="""longest""",		return_tensors="""np"""		)
           A			:	int										=       processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800]		)
           self._check_zero_mean_unit_variance(input_values[1, :1000]		)
           self._check_zero_mean_unit_variance(input_values[2]		)

           # make sure that if max_length > longest -> then pad to longest
           self.assertTrue(input_values.shape == (3, 1200)		)






    def       _lowerCAmelCase (	self		):
           A			:	str										=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
           A			:	Tuple										=       np.random.rand(100		).astype(np.floataa		)
           A			:	Any										=       np_speech_inputs.tolist()

           for inputs in [py_speech_inputs, np_speech_inputs]:
                  A			:	List[Any]										=       feature_extractor.pad([{"""input_values""": inputs}],		return_tensors="""np"""		)
                  self.assertTrue(np_processed.input_values.dtype == np.floataa		)
                  A			:	Any										=       feature_extractor.pad([{"""input_values""": inputs}],		return_tensors="""pt"""		)
                  self.assertTrue(pt_processed.input_values.dtype == torch.floataa		)






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

           # Test feature size
           A			:	Dict										=       feature_extractor(audio_target=lowercase_,		padding=lowercase_,		return_tensors="""np"""		).input_values
           self.assertTrue(input_values.ndim == 3		)
           self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins		)

           # Test not batched input
           A			:	Optional[int]										=       feature_extractor(speech_inputs[0],		return_tensors="""np"""		).input_values
           A			:	Any										=       feature_extractor(np_speech_inputs[0],		return_tensors="""np"""		).input_values
           self.assertTrue(np.allclose(lowercase_,		lowercase_,		atol=1e-3		)		)

           # Test batched
           A			:	Dict										=       feature_extractor(lowercase_,		return_tensors="""np"""		).input_values
           A			:	int										=       feature_extractor(lowercase_,		return_tensors="""np"""		).input_values
           for enc_seq_a, enc_seq_a in zip(lowercase_,		lowercase_		):
                  self.assertTrue(np.allclose(lowercase_,		lowercase_,		atol=1e-3		)		)

           # Test 2-D numpy arrays are batched.
           A			:	str										=       [floats_list((1, x)		)[0] for x in (800, 800, 800)]
           A			:	str										=       np.asarray(lowercase_		)
           A			:	Any										=       feature_extractor(lowercase_,		return_tensors="""np"""		).input_values
           A			:	Dict										=       feature_extractor(lowercase_,		return_tensors="""np"""		).input_values
           for enc_seq_a, enc_seq_a in zip(lowercase_,		lowercase_		):
                  self.assertTrue(np.allclose(lowercase_,		lowercase_,		atol=1e-3		)		)






    def       _lowerCAmelCase (	self		):
           A			:	Tuple										=       self.feat_extract_tester.prepare_inputs_for_target()
           A			:	List[Any]										=       self.feature_extraction_class(**self.feat_extract_dict		)
           A			:	List[str]										=       feat_extract.model_input_names[0]

           A			:	Dict										=       BatchFeature({input_name: speech_inputs}		)

           self.assertTrue(all(len(lowercase_		) == len(lowercase_		) for x, y in zip(lowercase_,		processed_features[input_name]		)		)		)

           A			:	List[Any]										=       self.feat_extract_tester.prepare_inputs_for_target(equal_length=lowercase_		)
           A			:	List[str]										=       BatchFeature({input_name: speech_inputs},		tensor_type="""np"""		)

           A			:	List[Any]										=       processed_features[input_name]

           if len(batch_features_input.shape		) < 3:
                  A			:	List[Any]										=       batch_features_input[:, :, None]

           self.assertTrue(
               batch_features_input.shape
               == (self.feat_extract_tester.batch_size, len(speech_inputs[0]		), self.feat_extract_tester.num_mel_bins)		)






    @require_torch
    def       _lowerCAmelCase (	self		):
           A			:	List[Any]										=       self.feat_extract_tester.prepare_inputs_for_target(equal_length=lowercase_		)
           A			:	Union[str, Any]										=       self.feature_extraction_class(**self.feat_extract_dict		)
           A			:	Union[str, Any]										=       feat_extract.model_input_names[0]

           A			:	List[Any]										=       BatchFeature({input_name: speech_inputs},		tensor_type="""pt"""		)

           A			:	Union[str, Any]										=       processed_features[input_name]

           if len(batch_features_input.shape		) < 3:
                  A			:	List[Any]										=       batch_features_input[:, :, None]

           self.assertTrue(
               batch_features_input.shape
               == (self.feat_extract_tester.batch_size, len(speech_inputs[0]		), self.feat_extract_tester.num_mel_bins)		)






    @require_torch
    def       _lowerCAmelCase (	self		):
           A			:	Dict										=       self.feature_extraction_class(**self.feat_extract_dict		)
           A			:	List[str]										=       self.feat_extract_tester.prepare_inputs_for_target()
           A			:	List[str]										=       feat_extract.model_input_names[0]

           A			:	Tuple										=       BatchFeature({input_name: speech_inputs}		)

           A			:	str										=       feat_extract.num_mel_bins  # hack!

           A			:	Optional[int]										=       feat_extract.pad(lowercase_,		padding="""longest""",		return_tensors="""np"""		)[input_name]
           A			:	Dict										=       feat_extract.pad(lowercase_,		padding="""longest""",		return_tensors="""pt"""		)[input_name]

           self.assertTrue(abs(input_np.astype(np.floataa		).sum() - input_pt.numpy().astype(np.floataa		).sum()		) < 1e-2		)






    def       _lowerCAmelCase (	self		):
           A			:	Optional[Any]										=       self.feat_extract_dict
           A			:	str										=       True
           A			:	Optional[Any]										=       self.feature_extraction_class(**lowercase_		)
           A			:	str										=       self.feat_extract_tester.prepare_inputs_for_target()
           A			:	Any										=       [len(lowercase_		) for x in speech_inputs]
           A			:	List[str]										=       feat_extract.model_input_names[0]

           A			:	Any										=       BatchFeature({input_name: speech_inputs}		)

           A			:	Union[str, Any]										=       feat_extract.num_mel_bins  # hack!

           A			:	Dict										=       feat_extract.pad(lowercase_,		padding="""longest""",		return_tensors="""np"""		)
           self.assertIn("""attention_mask""",		lowercase_		)
           self.assertListEqual(list(processed.attention_mask.shape		),		list(processed[input_name].shape[:2]		)		)
           self.assertListEqual(processed.attention_mask.sum(-1		).tolist(),		lowercase_		)






    def       _lowerCAmelCase (	self		):
           A			:	Dict										=       self.feat_extract_dict
           A			:	Tuple										=       True
           A			:	int										=       self.feature_extraction_class(**lowercase_		)
           A			:	int										=       self.feat_extract_tester.prepare_inputs_for_target()
           A			:	List[Any]										=       [len(lowercase_		) for x in speech_inputs]
           A			:	Dict										=       feat_extract.model_input_names[0]

           A			:	List[str]										=       BatchFeature({input_name: speech_inputs}		)
           A			:	List[Any]										=       min(lowercase_		)

           A			:	str										=       feat_extract.num_mel_bins  # hack!

           A			:	Union[str, Any]										=       feat_extract.pad(
               lowercase_,		padding="""max_length""",		max_length=lowercase_,		truncation=lowercase_,		return_tensors="""np"""		)
           self.assertIn("""attention_mask""",		lowercase_		)
           self.assertListEqual(
               list(processed_pad.attention_mask.shape		),		[processed_pad[input_name].shape[0], max_length]		)
           self.assertListEqual(
               processed_pad.attention_mask[:, :max_length].sum(-1		).tolist(),		[max_length for x in speech_inputs]		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
           from datasets import load_dataset

           A			:	Tuple										=       load_dataset("""hf-internal-testing/librispeech_asr_dummy""",		"""clean""",		split="""validation"""		)
           # automatic decoding with librispeech
           A			:	Optional[Any]										=       ds.sort("""id"""		).select(range(lowercase_		)		)[:num_samples]["""audio"""]

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






    def       _lowerCAmelCase (	self		):
           # fmt: off
           A			:	int										=       torch.tensor(
               [2.3804e-03, 2.0752e-03, 1.9836e-03, 2.1057e-03, 1.6174e-03,
                3.0518e-04, 9.1553e-05, 3.3569e-04, 9.7656e-04, 1.8311e-03,
                2.0142e-03, 2.1057e-03, 1.7395e-03, 4.5776e-04, -3.9673e-04,
                4.5776e-04, 1.0071e-03, 9.1553e-05, 4.8828e-04, 1.1597e-03,
                7.3242e-04, 9.4604e-04, 1.8005e-03, 1.8311e-03, 8.8501e-04,
                4.2725e-04, 4.8828e-04, 7.3242e-04, 1.0986e-03, 2.1057e-03]		)
           # fmt: on

           A			:	List[str]										=       self._load_datasamples(1		)
           A			:	str										=       SpeechTaFeatureExtractor()
           A			:	int										=       feature_extractor(lowercase_,		return_tensors="""pt"""		).input_values
           self.assertEquals(input_values.shape,		(1, 9_3680)		)
           self.assertTrue(torch.allclose(input_values[0, :30],		lowercase_,		atol=1e-6		)		)






    def       _lowerCAmelCase (	self		):
           # fmt: off
           A			:	int										=       torch.tensor(
               [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777,
                -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386,
                -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571,
                -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998]		)
           # fmt: on

           A			:	str										=       self._load_datasamples(1		)
           A			:	str										=       SpeechTaFeatureExtractor()
           A			:	int										=       feature_extractor(audio_target=lowercase_,		return_tensors="""pt"""		).input_values
           self.assertEquals(input_values.shape,		(1, 366, 80)		)
           self.assertTrue(torch.allclose(input_values[0, 0, :30],		lowercase_,		atol=1e-4		)		)







from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_:int					=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:Any					=						{
    """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""",
    """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""",
    """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""",
    """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""",
    """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""",
    """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""",
}






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Any =				"roberta"






    def __init__(	self,		lowerCamelCase__=5_0265,		lowerCamelCase__=768,		lowerCamelCase__=12,		lowerCamelCase__=12,		lowerCamelCase__=3072,		lowerCamelCase__="gelu",		lowerCamelCase__=0.1,		lowerCamelCase__=0.1,		lowerCamelCase__=512,		lowerCamelCase__=2,		lowerCamelCase__=0.02,		lowerCamelCase__=1e-12,		lowerCamelCase__=1,		lowerCamelCase__=0,		lowerCamelCase__=2,		lowerCamelCase__="absolute",		lowerCamelCase__=True,		lowerCamelCase__=None,		**lowerCamelCase__,		):
        super().__init__(pad_token_id=lowerCamelCase__,		bos_token_id=lowerCamelCase__,		eos_token_id=lowerCamelCase__,		**lowerCamelCase__		)

        A			:	List[str]										=       vocab_size
        A			:	List[Any]										=       hidden_size
        A			:	List[Any]										=       num_hidden_layers
        A			:	Optional[Any]										=       num_attention_heads
        A			:	Union[str, Any]										=       hidden_act
        A			:	Union[str, Any]										=       intermediate_size
        A			:	Optional[int]										=       hidden_dropout_prob
        A			:	List[str]										=       attention_probs_dropout_prob
        A			:	int										=       max_position_embeddings
        A			:	List[str]										=       type_vocab_size
        A			:	List[str]										=       initializer_range
        A			:	Dict										=       layer_norm_eps
        A			:	Optional[int]										=       position_embedding_type
        A			:	Any										=       use_cache
        A			:	Optional[int]										=       classifier_dropout






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    @property
    def       _lowerCAmelCase (	self		):
        if self.task == "multiple-choice":
            A			:	Optional[Any]										=       {0: """batch""", 1: """choice""", 2: """sequence"""}
        else:
            A			:	int										=       {0: """batch""", 1: """sequence"""}
        return OrderedDict(
            [
                ("""input_ids""", dynamic_axis),
                ("""attention_mask""", dynamic_axis),
            ]		)


