


import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_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_torch_available():
				import torch

if is_datasets_available():
				from datasets import load_dataset

A      = random.Random()





def __UpperCAmelCase       (		__A ,						__A=1.0 ,						__A=None ,						__A=None			)						->					str:



							'''simple docstring'''





							if rng is None:
														UpperCAmelCase__										=     global_rng

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

							return values


class     lowercase__  (					unittest.TestCase		):






		def __init__(					self      :					str		,						_lowercase      :					Optional[Any]		,						_lowercase      :					Tuple=7		,						_lowercase      :					str=4_00		,						_lowercase      :					Tuple=20_00		,						_lowercase      :					List[Any]=20_48		,						_lowercase      :					int=1_28		,						_lowercase      :					int=1		,						_lowercase      :					List[Any]=5_12		,						_lowercase      :					Optional[Any]=30		,						_lowercase      :					Tuple=4_41_00		,						):




									"""simple docstring"""







									UpperCAmelCase__										=     parent
									UpperCAmelCase__										=     batch_size
									UpperCAmelCase__										=     min_seq_length
									UpperCAmelCase__										=     max_seq_length
									UpperCAmelCase__										=     (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
									UpperCAmelCase__										=     spectrogram_length
									UpperCAmelCase__										=     feature_size
									UpperCAmelCase__										=     num_audio_channels
									UpperCAmelCase__										=     hop_length
									UpperCAmelCase__										=     chunk_length
									UpperCAmelCase__										=     sampling_rate






		def 			_UpperCAmelCase  (					self      :					int ):




									"""simple docstring"""







									return {
									    "spectrogram_length": self.spectrogram_length,
									    "feature_size": self.feature_size,
									    "num_audio_channels": self.num_audio_channels,
									    "hop_length": self.hop_length,
									    "chunk_length": self.chunk_length,
									    "sampling_rate": self.sampling_rate,
									}






		def 			_UpperCAmelCase  (					self      :					Union[str, Any]		,						_lowercase      :					Tuple=False		,						_lowercase      :					Tuple=False ):




									"""simple docstring"""







									def _flatten(_lowercase      :					str ):
																return list(itertools.chain(*_lowercase ) )

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




@require_torch
@require_torchaudio
class     lowercase__  (					__SCREAMING_SNAKE_CASE					,							unittest.TestCase		):
		A__=  TvltFeatureExtractor






		def 			_UpperCAmelCase  (					self      :					Union[str, Any] ):




									"""simple docstring"""







									UpperCAmelCase__										=     TvltFeatureExtractionTester(self )






		def 			_UpperCAmelCase  (					self      :					List[Any] ):




									"""simple docstring"""







									UpperCAmelCase__										=     self.feature_extraction_class(**self.feat_extract_dict )
									self.assertTrue(hasattr(_lowercase		,						"spectrogram_length" ) )
									self.assertTrue(hasattr(_lowercase		,						"feature_size" ) )
									self.assertTrue(hasattr(_lowercase		,						"num_audio_channels" ) )
									self.assertTrue(hasattr(_lowercase		,						"hop_length" ) )
									self.assertTrue(hasattr(_lowercase		,						"chunk_length" ) )
									self.assertTrue(hasattr(_lowercase		,						"sampling_rate" ) )






		def 			_UpperCAmelCase  (					self      :					Optional[int] ):




									"""simple docstring"""







									UpperCAmelCase__										=     self.feature_extraction_class(**self.feat_extract_dict )

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

									UpperCAmelCase__										=     feat_extract_first.to_dict()
									UpperCAmelCase__										=     feat_extract_second.to_dict()
									UpperCAmelCase__										=     dict_first.pop("mel_filters" )
									UpperCAmelCase__										=     dict_second.pop("mel_filters" )
									self.assertTrue(np.allclose(_lowercase		,						_lowercase ) )
									self.assertEqual(_lowercase		,						_lowercase )






		def 			_UpperCAmelCase  (					self      :					List[Any] ):




									"""simple docstring"""







									UpperCAmelCase__										=     self.feature_extraction_class(**self.feat_extract_dict )

									with tempfile.TemporaryDirectory() as tmpdirname:
																UpperCAmelCase__										=     os.path.join(_lowercase		,						"feat_extract.json" )
																feat_extract_first.to_json_file(_lowercase )
																UpperCAmelCase__										=     self.feature_extraction_class.from_json_file(_lowercase )

									UpperCAmelCase__										=     feat_extract_first.to_dict()
									UpperCAmelCase__										=     feat_extract_second.to_dict()
									UpperCAmelCase__										=     dict_first.pop("mel_filters" )
									UpperCAmelCase__										=     dict_second.pop("mel_filters" )
									self.assertTrue(np.allclose(_lowercase		,						_lowercase ) )
									self.assertEqual(_lowercase		,						_lowercase )






		def 			_UpperCAmelCase  (					self      :					List[str] ):




									"""simple docstring"""







									UpperCAmelCase__										=     self.feature_extraction_class(**self.feat_extract_dict )

									# create three inputs of length 800, 1000, and 1200
									UpperCAmelCase__										=     [floats_list((1, x) )[0] for x in range(8_00		,						14_00		,						2_00 )]
									UpperCAmelCase__										=     [np.asarray(_lowercase ) for speech_input in speech_inputs]

									# Test not batched input
									UpperCAmelCase__										=     feature_extractor(np_speech_inputs[0]		,						return_tensors="np"		,						sampling_rate=4_41_00 ).audio_values

									self.assertTrue(encoded_audios.ndim == 4 )
									self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
									self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
									self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )

									# Test batched
									UpperCAmelCase__										=     feature_extractor(_lowercase		,						return_tensors="np"		,						sampling_rate=4_41_00 ).audio_values

									self.assertTrue(encoded_audios.ndim == 4 )
									self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
									self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
									self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )

									# Test audio masking
									UpperCAmelCase__										=     feature_extractor(
									    _lowercase		,						return_tensors="np"		,						sampling_rate=4_41_00		,						mask_audio=_lowercase ).audio_values

									self.assertTrue(encoded_audios.ndim == 4 )
									self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
									self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
									self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )

									# Test 2-D numpy arrays are batched.
									UpperCAmelCase__										=     [floats_list((1, x) )[0] for x in (8_00, 8_00, 8_00)]
									UpperCAmelCase__										=     np.asarray(_lowercase )
									UpperCAmelCase__										=     feature_extractor(_lowercase		,						return_tensors="np"		,						sampling_rate=4_41_00 ).audio_values
									self.assertTrue(encoded_audios.ndim == 4 )
									self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size )
									self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length )
									self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels )






		def 			_UpperCAmelCase  (					self      :					Optional[Any]		,						_lowercase      :					Dict ):




									"""simple docstring"""







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

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






		def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




									"""simple docstring"""







									UpperCAmelCase__										=     self._load_datasamples(1 )
									UpperCAmelCase__										=     TvltFeatureExtractor()
									UpperCAmelCase__										=     feature_extractor(_lowercase		,						return_tensors="pt" ).audio_values

									self.assertEquals(audio_values.shape		,						(1, 1, 1_92, 1_28) )

									UpperCAmelCase__										=     torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]] )
									self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]		,						_lowercase		,						atol=1E-4 ) )









import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs


class     lowercase__  (					__SCREAMING_SNAKE_CASE		):






  def __init__(					self      :					Dict		,						_lowercase      :					int		,						_lowercase      :					List[Any]=None		,						_lowercase      :					str=True		,						_lowercase      :					str=None		,						**_lowercase      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     parent
         UpperCAmelCase__										=     config_class
         UpperCAmelCase__										=     has_text_modality
         UpperCAmelCase__										=     kwargs
         UpperCAmelCase__										=     common_properties






  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







         UpperCAmelCase__										=     self.config_class(**self.inputs_dict )
         UpperCAmelCase__										=     (
             ["hidden_size", "num_attention_heads", "num_hidden_layers"]
             if self.common_properties is None
             else self.common_properties
         )

         # Add common fields for text models
         if self.has_text_modality:
                common_properties.extend(["vocab_size"] )

         # Test that config has the common properties as getters
         for prop in common_properties:
                self.parent.assertTrue(hasattr(_lowercase		,						_lowercase )		,						msg=F"""`{prop}` does not exist""" )

         # Test that config has the common properties as setter
         for idx, name in enumerate(_lowercase ):
                try:
                       setattr(_lowercase		,						_lowercase		,						_lowercase )
                       self.parent.assertEqual(
                           getattr(_lowercase		,						_lowercase )		,						_lowercase		,						msg=F"""`{name} value {idx} expected, but was {getattr(_lowercase		,						_lowercase )}""" )
                except NotImplementedError:
                       # Some models might not be able to implement setters for common_properties
                       # In that case, a NotImplementedError is raised
                       pass

        # Test if config class can be called with Config(prop_name=..)
         for idx, name in enumerate(_lowercase ):
                try:
                       UpperCAmelCase__										=     self.config_class(**{name: idx} )
                       self.parent.assertEqual(
                           getattr(_lowercase		,						_lowercase )		,						_lowercase		,						msg=F"""`{name} value {idx} expected, but was {getattr(_lowercase		,						_lowercase )}""" )
                except NotImplementedError:
                       # Some models might not be able to implement setters for common_properties
                       # In that case, a NotImplementedError is raised
                       pass






  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     self.config_class(**self.inputs_dict )
         UpperCAmelCase__										=     json.loads(config.to_json_string() )
         for key, value in self.inputs_dict.items():
                self.parent.assertEqual(obj[key]		,						_lowercase )






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.config_class(**self.inputs_dict )

         with tempfile.TemporaryDirectory() as tmpdirname:
                UpperCAmelCase__										=     os.path.join(_lowercase		,						"config.json" )
                config_first.to_json_file(_lowercase )
                UpperCAmelCase__										=     self.config_class.from_json_file(_lowercase )

         self.parent.assertEqual(config_second.to_dict()		,						config_first.to_dict() )






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.config_class(**self.inputs_dict )

         with tempfile.TemporaryDirectory() as tmpdirname:
                config_first.save_pretrained(_lowercase )
                UpperCAmelCase__										=     self.config_class.from_pretrained(_lowercase )

         self.parent.assertEqual(config_second.to_dict()		,						config_first.to_dict() )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.config_class(**self.inputs_dict )

         UpperCAmelCase__										=     "test"
         with tempfile.TemporaryDirectory() as tmpdirname:
                UpperCAmelCase__										=     os.path.join(_lowercase		,						_lowercase )
                config_first.save_pretrained(_lowercase )
                UpperCAmelCase__										=     self.config_class.from_pretrained(_lowercase		,						subfolder=_lowercase )

         self.parent.assertEqual(config_second.to_dict()		,						config_first.to_dict() )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.config_class(**self.inputs_dict		,						num_labels=5 )
         self.parent.assertEqual(len(config.idalabel )		,						5 )
         self.parent.assertEqual(len(config.labelaid )		,						5 )

         UpperCAmelCase__										=     3
         self.parent.assertEqual(len(config.idalabel )		,						3 )
         self.parent.assertEqual(len(config.labelaid )		,						3 )






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         if self.config_class.is_composition:
                return
         UpperCAmelCase__										=     self.config_class()
         self.parent.assertIsNotNone(_lowercase )






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         UpperCAmelCase__										=     copy.deepcopy(_lowercase )
         UpperCAmelCase__										=     self.config_class(**_lowercase )
         UpperCAmelCase__										=     []
         for key, value in config_common_kwargs.items():
                if key == "torch_dtype":
                       if not is_torch_available():
                              continue
                       else:
                              import torch

                              if config.torch_dtype != torch.floataa:
                                     wrong_values.append(("torch_dtype", config.torch_dtype, torch.floataa) )
                elif getattr(_lowercase		,						_lowercase ) != value:
                       wrong_values.append((key, getattr(_lowercase		,						_lowercase ), value) )

         if len(_lowercase ) > 0:
                UpperCAmelCase__										=     "\n".join([F"""- {v[0]}: got {v[1]} instead of {v[2]}""" for v in wrong_values] )
                raise ValueError(F"""The following keys were not properly set in the config:\n{errors}""" )






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         self.create_and_test_config_common_properties()
         self.create_and_test_config_to_json_string()
         self.create_and_test_config_to_json_file()
         self.create_and_test_config_from_and_save_pretrained()
         self.create_and_test_config_from_and_save_pretrained_subfolder()
         self.create_and_test_config_with_num_labels()
         self.check_config_can_be_init_without_params()
         self.check_config_arguments_init()





