

'''simple docstring'''
from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel


def        _lowerCAmelCase					(						_UpperCamelCase						:     Union[str, Any]							, _UpperCamelCase						:     Optional[Any]							, _UpperCamelCase						:     Optional[Any]=None							, _UpperCamelCase						:     List[str]=None				)      ->			int:
     """simple docstring"""




     if attention_mask is None:
          _SCREAMING_SNAKE_CASE							    =tf.cast(tf.math.not_equal(_UpperCamelCase							, config.pad_token_id				)							, tf.inta				)
     return {"input_ids": input_ids, "attention_mask": attention_mask}







@require_tf
class 							A__    :
  A__		 =				OPTConfig
  A__		 =				{}
  A__		 =				'gelu'





  def __init__(		self      :	Optional[Any]							,					_a      :	List[Any]							,					_a      :	List[Any]=13							,					_a      :	Union[str, Any]=7							,					_a      :	Union[str, Any]=True							,					_a      :	Tuple=False							,					_a      :	str=99							,					_a      :	Optional[int]=16							,					_a      :	Union[str, Any]=2							,					_a      :	Dict=4							,					_a      :	Tuple=4							,					_a      :	List[str]="gelu"							,					_a      :	Dict=0.1							,					_a      :	Tuple=0.1							,					_a      :	Dict=20							,					_a      :	Tuple=2							,					_a      :	Union[str, Any]=1							,					_a      :	Any=0							,					_a      :	List[Any]=16							,					_a      :	Optional[Any]=16							,					)	->    int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =parent
       _SCREAMING_SNAKE_CASE							    =batch_size
       _SCREAMING_SNAKE_CASE							    =seq_length
       _SCREAMING_SNAKE_CASE							    =is_training
       _SCREAMING_SNAKE_CASE							    =use_labels
       _SCREAMING_SNAKE_CASE							    =vocab_size
       _SCREAMING_SNAKE_CASE							    =hidden_size
       _SCREAMING_SNAKE_CASE							    =num_hidden_layers
       _SCREAMING_SNAKE_CASE							    =num_attention_heads
       _SCREAMING_SNAKE_CASE							    =intermediate_size
       _SCREAMING_SNAKE_CASE							    =hidden_act
       _SCREAMING_SNAKE_CASE							    =hidden_dropout_prob
       _SCREAMING_SNAKE_CASE							    =attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE							    =max_position_embeddings
       _SCREAMING_SNAKE_CASE							    =eos_token_id
       _SCREAMING_SNAKE_CASE							    =pad_token_id
       _SCREAMING_SNAKE_CASE							    =bos_token_id
       _SCREAMING_SNAKE_CASE							    =embed_dim
       _SCREAMING_SNAKE_CASE							    =word_embed_proj_dim
       _SCREAMING_SNAKE_CASE							    =False





  def 						A  (		self      :	Optional[Any] )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =ids_tensor([self.batch_size, self.seq_length - 1]							,					self.vocab_size )
       _SCREAMING_SNAKE_CASE							    =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size )							,					1 )
       _SCREAMING_SNAKE_CASE							    =tf.concat([input_ids, eos_tensor]							,					axis=1 )

       _SCREAMING_SNAKE_CASE							    =self.config_cls(
           vocab_size=self.vocab_size							,					hidden_size=self.hidden_size							,					num_hidden_layers=self.num_hidden_layers							,					num_attention_heads=self.num_attention_heads							,					ffn_dim=self.intermediate_size							,					dropout=self.hidden_dropout_prob							,					attention_dropout=self.attention_probs_dropout_prob							,					max_position_embeddings=self.max_position_embeddings							,					eos_token_id=self.eos_token_id							,					bos_token_id=self.bos_token_id							,					pad_token_id=self.pad_token_id							,					embed_dim=self.embed_dim							,					word_embed_proj_dim=self.word_embed_proj_dim							,					is_encoder_decoder=_a							,					**self.config_updates							,					)
       _SCREAMING_SNAKE_CASE							    =prepare_opt_inputs_dict(_a							,					_a )
       return config, inputs_dict





  def 						A  (		self      :	Union[str, Any]							,					_a      :	Tuple							,					_a      :	Optional[Any] )	->    Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =TFOPTModel(config=_a )
       _SCREAMING_SNAKE_CASE							    =inputs_dict['input_ids']

       _SCREAMING_SNAKE_CASE							    =input_ids[:1, :]
       _SCREAMING_SNAKE_CASE							    =inputs_dict['attention_mask'][:1, :]
       _SCREAMING_SNAKE_CASE							    =1

       # first forward pass
       _SCREAMING_SNAKE_CASE							    =model(_a							,					attention_mask=_a							,					use_cache=_a )

       _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE							    =outputs.to_tuple()

       # create hypothetical next token and extent to next_input_ids
       _SCREAMING_SNAKE_CASE							    =ids_tensor((self.batch_size, 3)							,					config.vocab_size )
       _SCREAMING_SNAKE_CASE							    =tf.cast(ids_tensor((self.batch_size, 3)							,					2 )							,					tf.inta )

       # append to next input_ids and
       _SCREAMING_SNAKE_CASE							    =tf.concat([input_ids, next_tokens]							,					axis=-1 )
       _SCREAMING_SNAKE_CASE							    =tf.concat([attention_mask, next_attn_mask]							,					axis=-1 )

       _SCREAMING_SNAKE_CASE							    =model(_a							,					attention_mask=_a )[0]
       _SCREAMING_SNAKE_CASE							    =model(_a							,					attention_mask=_a							,					past_key_values=_a )[0]

       self.parent.assertEqual(next_tokens.shape[1]							,					output_from_past.shape[1] )

       # select random slice
       _SCREAMING_SNAKE_CASE							    =int(ids_tensor((1,)							,					output_from_past.shape[-1] ) )
       _SCREAMING_SNAKE_CASE							    =output_from_no_past[:, -3:, random_slice_idx]
       _SCREAMING_SNAKE_CASE							    =output_from_past[:, :, random_slice_idx]

       # test that outputs are equal for slice
       tf.debugging.assert_near(_a							,					_a							,					rtol=1e-3 )







@require_tf
class 							A__    (       A__     ,   A__     ,   unittest.TestCase  ):
  A__		 =				(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
  A__		 =				(TFOPTForCausalLM,) if is_tf_available() else ()
  A__		 =				(
      {'feature-extraction': TFOPTModel, 'text-generation': TFOPTForCausalLM} if is_tf_available() else {}
  )
  A__		 =				False
  A__		 =				False
  A__		 =				False
  A__		 =				10





  def 						A  (		self      :	Union[str, Any] )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =TFOPTModelTester(self )
       _SCREAMING_SNAKE_CASE							    =ConfigTester(self							,					config_class=_a )





  def 						A  (		self      :	str )	->    Optional[int]:





       '''simple docstring'''






       self.config_tester.run_common_tests()





  def 						A  (		self      :	Optional[Any] )	->    str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_decoder_model_past_large_inputs(*_a )





  def 						A  (		self      :	int )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE							    =self.model_tester.prepare_config_and_inputs_for_common()

       def _get_word_embedding_weight(_a      :	Any							,					_a      :	str ):
            if hasattr(_a							,					'weight' ):
                 return embedding_layer.weight
            else:
                 # Here we build the word embeddings weights if not exists.
                 # And then we retry to get the attribute once built.
                 model.build()
                 if hasattr(_a							,					'weight' ):
                      return embedding_layer.weight
                 else:
                      return None

       for model_class in self.all_model_classes:
            for size in [config.vocab_size - 10, config.vocab_size + 10]:
                 # build the embeddings
                 _SCREAMING_SNAKE_CASE							    =model_class(config=_a )
                 _SCREAMING_SNAKE_CASE							    =_get_word_embedding_weight(_a							,					model.get_input_embeddings() )
                 _SCREAMING_SNAKE_CASE							    =_get_word_embedding_weight(_a							,					model.get_output_embeddings() )

                 # reshape the embeddings
                 model.resize_token_embeddings(_a )
                 _SCREAMING_SNAKE_CASE							    =_get_word_embedding_weight(_a							,					model.get_input_embeddings() )
                 _SCREAMING_SNAKE_CASE							    =_get_word_embedding_weight(_a							,					model.get_output_embeddings() )

                 # check that the resized embeddings size matches the desired size.
                 _SCREAMING_SNAKE_CASE							    =size if size is not None else config.vocab_size

                 self.assertEqual(new_input_embeddings.shape[0]							,					_a )

                 # check that weights remain the same after resizing
                 _SCREAMING_SNAKE_CASE							    =True
                 for pa, pa in zip(old_input_embeddings.value()							,					new_input_embeddings.value() ):
                      if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0:
                           _SCREAMING_SNAKE_CASE							    =False
                 self.assertTrue(_a )

                 if old_output_embeddings is not None and new_output_embeddings is not None:
                      self.assertEqual(new_output_embeddings.shape[0]							,					_a )

                      _SCREAMING_SNAKE_CASE							    =True
                      for pa, pa in zip(old_output_embeddings.value()							,					new_output_embeddings.value() ):
                           if tf.math.reduce_sum(tf.math.abs(pa - pa ) ) > 0:
                                _SCREAMING_SNAKE_CASE							    =False
                      self.assertTrue(_a )


def        _lowerCAmelCase					(						_UpperCamelCase						:     Optional[Any]				)      ->			int:
     """simple docstring"""




     return tf.constant(_UpperCamelCase							, dtype=tf.intaa				)







@require_tf
class 							A__    (       unittest.TestCase  ):
  A__		 =				99





  def 						A  (		self      :	Union[str, Any] )	->    int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =tf.ones((4, 1)							,					dtype=tf.intaa ) * 2
       _SCREAMING_SNAKE_CASE							    =tf.concat([ids_tensor((4, 6)							,					self.vocab_size - 3 ) + 3, eos_column_vector]							,					axis=1 )
       _SCREAMING_SNAKE_CASE							    =input_ids.shape[0]
       _SCREAMING_SNAKE_CASE							    =OPTConfig(
           vocab_size=self.vocab_size							,					hidden_size=24							,					num_hidden_layers=2							,					num_attention_heads=2							,					ffn_dim=32							,					max_position_embeddings=48							,					eos_token_id=2							,					pad_token_id=1							,					bos_token_id=0							,					)
       return config, input_ids, batch_size







@require_sentencepiece
@require_tf
class 							A__    (       unittest.TestCase  ):





  @slow
  def 						A  (		self      :	Union[str, Any] )	->    List[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =TFOPTModel.from_pretrained('facebook/opt-350m' )
       _SCREAMING_SNAKE_CASE							    =_long_tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] )
       _SCREAMING_SNAKE_CASE							    =tf.not_equal(_a							,					model.config.pad_token_id )
       with tf.GradientTape():
            _SCREAMING_SNAKE_CASE							    =model(input_ids=_a							,					attention_mask=_a ).last_hidden_state
       _SCREAMING_SNAKE_CASE							    =(1, 11, 512)
       self.assertEqual(output.shape							,					_a )
       _SCREAMING_SNAKE_CASE							    =tf.constant(
           [[-0.28_73, -1.92_18, -0.30_33], [-1.27_10, -0.13_38, -0.19_02], [0.40_95, 0.12_14, -1.31_21]] )
       self.assertTrue(np.allclose(output[:, :3, :3]							,					_a							,					atol=4e-3 ) )

       _SCREAMING_SNAKE_CASE							    =tf.function(_a							,					jit_compile=_a )
       _SCREAMING_SNAKE_CASE							    =xla_generate(_a							,					_a )[0]
       self.assertTrue(np.allclose(output[:, :3, :3]							,					_a							,					atol=4e-2 ) )







@require_tf
@slow
class 							A__    (       unittest.TestCase  ):





  def 						A  (		self      :	List[Any] )	->    Tuple:





       '''simple docstring'''






       super().setUp()
       _SCREAMING_SNAKE_CASE							    ='facebook/opt-350m'





  def 						A  (		self      :	str )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =TFOPTForCausalLM.from_pretrained(self.path_model )
       _SCREAMING_SNAKE_CASE							    =GPTaTokenizer.from_pretrained(self.path_model )

       _SCREAMING_SNAKE_CASE							    =[
           'Today is a beautiful day and I want to',
           'In the city of',
           'Paris is the capital of France and',
           'Computers and mobile phones have taken',
       ]
       # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
       _SCREAMING_SNAKE_CASE							    =tokenizer(_a							,					return_tensors='tf'							,					padding=_a							,					add_special_tokens=_a )
       _SCREAMING_SNAKE_CASE							    =tf.math.reduce_mean(model(inputs.input_ids							,					attention_mask=inputs.attention_mask )[0]							,					axis=-1 )
       _SCREAMING_SNAKE_CASE							    =tf.constant(
           [
               [1.38_51, -13.89_23, -10.52_29, -10.75_33, -0.23_09, -10.23_84, -0.53_65, -9.09_47, -5.16_70],
               [-4.70_73, -10.62_76, -3.94_15, -21.52_42, -0.28_22, -0.28_22, -0.28_22, -0.28_22, -0.28_22],
               [0.62_47, -3.42_29, -8.91_79, -1.42_97, -14.16_50, 1.41_46, -9.02_18, -0.27_03, -0.27_03],
               [6.47_83, -1.99_13, -10.79_26, -2.33_36, 1.50_92, -0.99_74, -6.82_13, 1.34_77, 1.34_77],
           ] )
       self.assertTrue(np.allclose(_a							,					_a							,					atol=1e-4 ) )

       _SCREAMING_SNAKE_CASE							    =tf.function(_a							,					jit_compile=_a )
       _SCREAMING_SNAKE_CASE							    =tf.math.reduce_mean(xla_generate(inputs.input_ids							,					attention_mask=inputs.attention_mask )[0]							,					axis=-1 )
       self.assertTrue(np.allclose(_a							,					_a							,					atol=1e-4 ) )







@require_tf
@slow
class 							A__    (       unittest.TestCase  ):





  @property
  def 						A  (		self      :	List[str] )	->    List[Any]:





       '''simple docstring'''






       return [
           "Today is a beautiful day and I want",
           "In the city of",
           "Paris is the capital of France and",
           "Computers and mobile phones have taken",
       ]





  def 						A  (		self      :	Tuple )	->    Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    ='facebook/opt-125m'

       _SCREAMING_SNAKE_CASE							    =[
           'Today is a beautiful day and I want to',
           'In the city of New York, the city',
           'Paris is the capital of France and the capital',
           'Computers and mobile phones have taken over the',
       ]

       _SCREAMING_SNAKE_CASE							    =[]
       _SCREAMING_SNAKE_CASE							    =GPTaTokenizer.from_pretrained(_a )
       _SCREAMING_SNAKE_CASE							    =TFOPTForCausalLM.from_pretrained(_a )

       for prompt in self.prompts:
            _SCREAMING_SNAKE_CASE							    =tokenizer(_a							,					return_tensors='tf' ).input_ids

            _SCREAMING_SNAKE_CASE							    =model.generate(_a							,					max_length=10 )

            _SCREAMING_SNAKE_CASE							    =tokenizer.batch_decode(_a							,					skip_special_tokens=_a )
            predicted_outputs += generated_string

       self.assertListEqual(_a							,					_a )





  def 						A  (		self      :	Dict )	->    Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    ='facebook/opt-350m'

       _SCREAMING_SNAKE_CASE							    =GPTaTokenizer.from_pretrained(_a )
       _SCREAMING_SNAKE_CASE							    =TFOPTForCausalLM.from_pretrained(_a )

       _SCREAMING_SNAKE_CASE							    ='left'

       # use different length sentences to test batching
       _SCREAMING_SNAKE_CASE							    =[
           'Hello, my dog is a little',
           'Today, I',
       ]

       _SCREAMING_SNAKE_CASE							    =tokenizer(_a							,					return_tensors='tf'							,					padding=_a )
       _SCREAMING_SNAKE_CASE							    =inputs['input_ids']

       _SCREAMING_SNAKE_CASE							    =model.generate(input_ids=_a							,					attention_mask=inputs['attention_mask'] )

       _SCREAMING_SNAKE_CASE							    =tokenizer(sentences[0]							,					return_tensors='tf' ).input_ids
       _SCREAMING_SNAKE_CASE							    =model.generate(input_ids=_a )

       _SCREAMING_SNAKE_CASE							    =inputs_non_padded.shape[-1] - tf.math.reduce_sum(
           tf.cast(inputs['attention_mask'][-1]							,					tf.intaa ) )
       _SCREAMING_SNAKE_CASE							    =tokenizer(sentences[1]							,					return_tensors='tf' ).input_ids
       _SCREAMING_SNAKE_CASE							    =model.generate(input_ids=_a							,					max_length=model.config.max_length - num_paddings )

       _SCREAMING_SNAKE_CASE							    =tokenizer.batch_decode(_a							,					skip_special_tokens=_a )
       _SCREAMING_SNAKE_CASE							    =tokenizer.decode(output_non_padded[0]							,					skip_special_tokens=_a )
       _SCREAMING_SNAKE_CASE							    =tokenizer.decode(output_padded[0]							,					skip_special_tokens=_a )

       _SCREAMING_SNAKE_CASE							    =[
           'Hello, my dog is a little bit of a dork.\nI\'m a little bit',
           'Today, I was in the middle of a conversation with a friend about the',
       ]
       self.assertListEqual(_a							,					_a )
       self.assertListEqual(_a							,					[non_padded_sentence, padded_sentence] )





  def 						A  (		self      :	Any )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    ='facebook/opt-350m'

       _SCREAMING_SNAKE_CASE							    =[
           'Today is a beautiful day and I want to',
           'In the city of San Francisco, the city',
           'Paris is the capital of France and the capital',
           'Computers and mobile phones have taken over the',
       ]

       _SCREAMING_SNAKE_CASE							    =[]
       _SCREAMING_SNAKE_CASE							    =GPTaTokenizer.from_pretrained(_a )
       _SCREAMING_SNAKE_CASE							    =TFOPTForCausalLM.from_pretrained(_a )

       for prompt in self.prompts:
            _SCREAMING_SNAKE_CASE							    =tokenizer(_a							,					return_tensors='tf' ).input_ids

            _SCREAMING_SNAKE_CASE							    =model.generate(_a							,					max_length=10 )

            _SCREAMING_SNAKE_CASE							    =tokenizer.batch_decode(_a							,					skip_special_tokens=_a )
            predicted_outputs += generated_string

       self.assertListEqual(_a							,					_a )






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

__A        =    random.Random()







def 			lowerCamelCase_		(       UpperCamelCase__				:      Optional[int] ,     UpperCamelCase__				:      List[str]=1.0 ,     UpperCamelCase__				:      Union[str, Any]=None ,     UpperCamelCase__				:      Optional[int]=None      )     ->       Optional[Any]:





				"""simple docstring"""


				if rng is None:
								__lowerCamelCase		       =    global_rng

				__lowerCamelCase		       =    []
				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     __lowerCAmelCase (       unittest.TestCase ):

			"""simple docstring"""







			def __init__(      self			,						lowerCamelCase__			,						lowerCamelCase__=7			,						lowerCamelCase__=400			,						lowerCamelCase__=2_000			,						lowerCamelCase__=10			,						lowerCamelCase__=160			,						lowerCamelCase__=8			,						lowerCamelCase__=0.0			,						lowerCamelCase__=4_000			,						lowerCamelCase__=False			,						lowerCamelCase__=True			,						)    ->      List[str]:



							'''simple docstring'''
							__lowerCamelCase		       =    parent
							__lowerCamelCase		       =    batch_size
							__lowerCamelCase		       =    min_seq_length
							__lowerCamelCase		       =    max_seq_length
							__lowerCamelCase		       =    (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
							__lowerCamelCase		       =    padding_value
							__lowerCamelCase		       =    sampling_rate
							__lowerCamelCase		       =    return_attention_mask
							__lowerCamelCase		       =    do_normalize
							__lowerCamelCase		       =    feature_size
							__lowerCamelCase		       =    chunk_length
							__lowerCamelCase		       =    hop_length


			def       lowercase_    (      self							)    ->      Any:



							'''simple docstring'''
							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       lowercase_    (      self			,						lowerCamelCase__=False			,						lowerCamelCase__=False							)    ->      Optional[int]:



							'''simple docstring'''
							def _flatten(lowerCamelCase__							):
											return list(itertools.chain(*lowerCamelCase__							)							)

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







@require_torch
@require_torchaudio
class     __lowerCAmelCase (       __magic_name__			,				unittest.TestCase ):

			"""simple docstring"""







			snake_case_   		=       WhisperFeatureExtractor if is_speech_available() else None


			def       lowercase_    (      self							)    ->      Any:



							'''simple docstring'''
							__lowerCamelCase		       =    WhisperFeatureExtractionTester(self							)


			def       lowercase_    (      self							)    ->      List[str]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.feature_extraction_class(**self.feat_extract_dict							)

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

							__lowerCamelCase		       =    feat_extract_first.to_dict()
							__lowerCamelCase		       =    feat_extract_second.to_dict()
							__lowerCamelCase		       =    feat_extract_first.mel_filters
							__lowerCamelCase		       =    feat_extract_second.mel_filters
							self.assertTrue(np.allclose(lowerCamelCase__			,						lowerCamelCase__							)							)
							self.assertEqual(lowerCamelCase__			,						lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Dict:



							'''simple docstring'''
							__lowerCamelCase		       =    self.feature_extraction_class(**self.feat_extract_dict							)

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

							__lowerCamelCase		       =    feat_extract_first.to_dict()
							__lowerCamelCase		       =    feat_extract_second.to_dict()
							__lowerCamelCase		       =    feat_extract_first.mel_filters
							__lowerCamelCase		       =    feat_extract_second.mel_filters
							self.assertTrue(np.allclose(lowerCamelCase__			,						lowerCamelCase__							)							)
							self.assertEqual(lowerCamelCase__			,						lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Dict:



							'''simple docstring'''
							# Tests that all call wrap to encode_plus and batch_encode_plus
							__lowerCamelCase		       =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
							# create three inputs of length 800, 1000, and 1200
							__lowerCamelCase		       =    [floats_list((1, x)							)[0] for x in range(800			,						1_400			,						200							)]
							__lowerCamelCase		       =    [np.asarray(lowerCamelCase__							) for speech_input in speech_inputs]

							# Test feature size
							__lowerCamelCase		       =    feature_extractor(lowerCamelCase__			,						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
							__lowerCamelCase		       =    feature_extractor(speech_inputs[0]			,						return_tensors='np'							).input_features
							__lowerCamelCase		       =    feature_extractor(np_speech_inputs[0]			,						return_tensors='np'							).input_features
							self.assertTrue(np.allclose(lowerCamelCase__			,						lowerCamelCase__			,						atol=1e-3							)							)

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

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

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

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

							__lowerCamelCase		       =    feature_extractor(lowerCamelCase__			,						return_tensors='np'							).input_features
							__lowerCamelCase		       =    feature_extractor(lowerCamelCase__			,						return_tensors='np'							).input_features
							for enc_seq_a, enc_seq_a in zip(lowerCamelCase__			,						lowerCamelCase__							):
											self.assertTrue(np.allclose(lowerCamelCase__			,						lowerCamelCase__			,						atol=1e-3							)							)


			def       lowercase_    (      self							)    ->      List[str]:



							'''simple docstring'''
							import torch

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

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


			def       lowercase_    (      self			,						lowerCamelCase__							)    ->      List[str]:



							'''simple docstring'''
							__lowerCamelCase		       =    load_dataset('hf-internal-testing/librispeech_asr_dummy'			,						'clean'			,						split='validation'							)
							# automatic decoding with librispeech
							__lowerCamelCase		       =    ds.sort('id'							).select(range(lowerCamelCase__							)							)[:num_samples]['audio']

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


			def       lowercase_    (      self							)    ->      Tuple:



							'''simple docstring'''
							# fmt: off
							__lowerCamelCase		       =    torch.tensor(
							    [
							        0.11_93, -0.09_46, -0.10_98, -0.01_96, 0.02_25, -0.06_90, -0.17_36, 0.09_51,
							        0.09_71, -0.08_17, -0.07_02, 0.01_62, 0.02_60, 0.00_17, -0.01_92, -0.16_78,
							        0.07_09, -0.18_67, -0.06_55, -0.02_74, -0.02_34, -0.18_84, -0.05_16, -0.05_54,
							        -0.02_74, -0.14_25, -0.14_23, 0.08_37, 0.03_77, -0.08_54
							    ]							)
							# fmt: on

							__lowerCamelCase		       =    self._load_datasamples(1							)
							__lowerCamelCase		       =    WhisperFeatureExtractor()
							__lowerCamelCase		       =    feature_extractor(lowerCamelCase__			,						return_tensors='pt'							).input_features
							self.assertEqual(input_features.shape			,						(1, 80, 3_000)							)
							self.assertTrue(torch.allclose(input_features[0, 0, :30]			,						lowerCamelCase__			,						atol=1e-4							)							)






			def       lowercase_    (      self							)    ->      List[Any]:



							'''simple docstring'''
							__lowerCamelCase		       =    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
							__lowerCamelCase		       =    self._load_datasamples(1							)[0]
							__lowerCamelCase		       =    ((audio - audio.min()) / (audio.max() - audio.min())) * 65_535  # Rescale to [0, 65535] to show issue
							__lowerCamelCase		       =    feat_extract.zero_mean_unit_var_norm([audio]			,						attention_mask=lowerCamelCase__							)[0]

							self.assertTrue(np.all(np.mean(lowerCamelCase__							) < 1e-3							)							)
							self.assertTrue(np.all(np.abs(np.var(lowerCamelCase__							) - 1							) < 1e-3							)							)


