import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    FlaubertForMultipleChoice,
					    FlaubertForQuestionAnswering,
					    FlaubertForQuestionAnsweringSimple,
					    FlaubertForSequenceClassification,
					    FlaubertForTokenClassification,
					    FlaubertModel,
					    FlaubertWithLMHeadModel,
					)
					from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class 					lowerCAmelCase_      (     lowerCAmelCase   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase						,      lowerCAmelCase=13						,      lowerCAmelCase=7						,      lowerCAmelCase=True						,      lowerCAmelCase=True						,      lowerCAmelCase=True						,      lowerCAmelCase=True						,      lowerCAmelCase=True						,      lowerCAmelCase=False						,      lowerCAmelCase=False						,      lowerCAmelCase=False						,      lowerCAmelCase=2						,      lowerCAmelCase=99						,      lowerCAmelCase=0						,      lowerCAmelCase=32						,      lowerCAmelCase=5						,      lowerCAmelCase=4						,      lowerCAmelCase=0.1						,      lowerCAmelCase=0.1						,      lowerCAmelCase=5_12						,      lowerCAmelCase=12						,      lowerCAmelCase=2						,      lowerCAmelCase=0.02						,      lowerCAmelCase=3						,      lowerCAmelCase=4						,      lowerCAmelCase="last"						,      lowerCAmelCase=None						,      lowerCAmelCase=None						,      ):






										"""simple docstring"""

										snake_case        =     parent
										snake_case        =     batch_size
										snake_case        =     seq_length
										snake_case        =     is_training
										snake_case        =     use_input_lengths
										snake_case        =     use_token_type_ids
										snake_case        =     use_labels
										snake_case        =     gelu_activation
										snake_case        =     sinusoidal_embeddings
										snake_case        =     causal
										snake_case        =     asm
										snake_case        =     n_langs
										snake_case        =     vocab_size
										snake_case        =     n_special
										snake_case        =     hidden_size
										snake_case        =     num_hidden_layers
										snake_case        =     num_attention_heads
										snake_case        =     hidden_dropout_prob
										snake_case        =     attention_probs_dropout_prob
										snake_case        =     max_position_embeddings
										snake_case        =     type_vocab_size
										snake_case        =     type_sequence_label_size
										snake_case        =     initializer_range
										snake_case        =     num_labels
										snake_case        =     num_choices
										snake_case        =     summary_type
										snake_case        =     use_proj
										snake_case        =     scope

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     ids_tensor([self.batch_size, self.seq_length]						,      self.vocab_size      )
										snake_case        =     random_attention_mask([self.batch_size, self.seq_length]      )

										snake_case        =     None
										if self.use_input_lengths:
																snake_case        =     (
																    ids_tensor([self.batch_size]						,      vocab_size=2      ) + self.seq_length - 2
																)  # small variation of seq_length

										snake_case        =     None
										if self.use_token_type_ids:
																snake_case        =     ids_tensor([self.batch_size, self.seq_length]						,      self.n_langs      )

										snake_case        =     None
										snake_case        =     None
										snake_case        =     None
										if self.use_labels:
																snake_case        =     ids_tensor([self.batch_size]						,      self.type_sequence_label_size      )
																snake_case        =     ids_tensor([self.batch_size, self.seq_length]						,      self.num_labels      )
																snake_case        =     ids_tensor([self.batch_size]						,      2      ).float()
																snake_case        =     ids_tensor([self.batch_size]						,      self.num_choices      )

										snake_case        =     self.get_config()

										return (
										    config,
										    input_ids,
										    token_type_ids,
										    input_lengths,
										    sequence_labels,
										    token_labels,
										    is_impossible_labels,
										    choice_labels,
										    input_mask,
										)

				def 						snake_case	(			self      ):






										"""simple docstring"""

										return FlaubertConfig(
										    vocab_size=self.vocab_size						,      n_special=self.n_special						,      emb_dim=self.hidden_size						,      n_layers=self.num_hidden_layers						,      n_heads=self.num_attention_heads						,      dropout=self.hidden_dropout_prob						,      attention_dropout=self.attention_probs_dropout_prob						,      gelu_activation=self.gelu_activation						,      sinusoidal_embeddings=self.sinusoidal_embeddings						,      asm=self.asm						,      causal=self.causal						,      n_langs=self.n_langs						,      max_position_embeddings=self.max_position_embeddings						,      initializer_range=self.initializer_range						,      summary_type=self.summary_type						,      use_proj=self.use_proj						,      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      ):






										"""simple docstring"""

										snake_case        =     FlaubertModel(config=lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()
										snake_case        =     model(lowerCAmelCase						,      lengths=lowerCAmelCase						,      langs=lowerCAmelCase      )
										snake_case        =     model(lowerCAmelCase						,      langs=lowerCAmelCase      )
										snake_case        =     model(lowerCAmelCase      )
										self.parent.assertEqual(result.last_hidden_state.shape						,      (self.batch_size, self.seq_length, self.hidden_size)      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      ):






										"""simple docstring"""

										snake_case        =     FlaubertWithLMHeadModel(lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()

										snake_case        =     model(lowerCAmelCase						,      token_type_ids=lowerCAmelCase						,      labels=lowerCAmelCase      )
										self.parent.assertEqual(result.loss.shape						,      ()      )
										self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.vocab_size)      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      ):






										"""simple docstring"""

										snake_case        =     FlaubertForQuestionAnsweringSimple(lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()

										snake_case        =     model(lowerCAmelCase      )

										snake_case        =     model(lowerCAmelCase						,      start_positions=lowerCAmelCase						,      end_positions=lowerCAmelCase      )
										self.parent.assertEqual(result.start_logits.shape						,      (self.batch_size, self.seq_length)      )
										self.parent.assertEqual(result.end_logits.shape						,      (self.batch_size, self.seq_length)      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      ):






										"""simple docstring"""

										snake_case        =     FlaubertForQuestionAnswering(lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()

										snake_case        =     model(lowerCAmelCase      )

										snake_case        =     model(
										    lowerCAmelCase						,      start_positions=lowerCAmelCase						,      end_positions=lowerCAmelCase						,      cls_index=lowerCAmelCase						,      is_impossible=lowerCAmelCase						,      p_mask=lowerCAmelCase						,      )

										snake_case        =     model(
										    lowerCAmelCase						,      start_positions=lowerCAmelCase						,      end_positions=lowerCAmelCase						,      cls_index=lowerCAmelCase						,      is_impossible=lowerCAmelCase						,      )

										((snake_case)   ,)        =     result_with_labels.to_tuple()

										snake_case        =     model(lowerCAmelCase						,      start_positions=lowerCAmelCase						,      end_positions=lowerCAmelCase      )

										((snake_case)   ,)        =     result_with_labels.to_tuple()

										self.parent.assertEqual(result_with_labels.loss.shape						,      ()      )
										self.parent.assertEqual(result.start_top_log_probs.shape						,      (self.batch_size, model.config.start_n_top)      )
										self.parent.assertEqual(result.start_top_index.shape						,      (self.batch_size, model.config.start_n_top)      )
										self.parent.assertEqual(
										    result.end_top_log_probs.shape						,      (self.batch_size, model.config.start_n_top * model.config.end_n_top)      )
										self.parent.assertEqual(
										    result.end_top_index.shape						,      (self.batch_size, model.config.start_n_top * model.config.end_n_top)      )
										self.parent.assertEqual(result.cls_logits.shape						,      (self.batch_size,)      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      ):






										"""simple docstring"""

										snake_case        =     FlaubertForSequenceClassification(lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()

										snake_case        =     model(lowerCAmelCase      )
										snake_case        =     model(lowerCAmelCase						,      labels=lowerCAmelCase      )

										self.parent.assertEqual(result.loss.shape						,      ()      )
										self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.type_sequence_label_size)      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      ):






										"""simple docstring"""

										snake_case        =     self.num_labels
										snake_case        =     FlaubertForTokenClassification(lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()

										snake_case        =     model(lowerCAmelCase						,      attention_mask=lowerCAmelCase						,      labels=lowerCAmelCase      )
										self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.num_labels)      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      ):






										"""simple docstring"""

										snake_case        =     self.num_choices
										snake_case        =     FlaubertForMultipleChoice(config=lowerCAmelCase      )
										model.to(lowerCAmelCase      )
										model.eval()
										snake_case        =     input_ids.unsqueeze(1      ).expand(-1						,      self.num_choices						,      -1      ).contiguous()
										snake_case        =     token_type_ids.unsqueeze(1      ).expand(-1						,      self.num_choices						,      -1      ).contiguous()
										snake_case        =     input_mask.unsqueeze(1      ).expand(-1						,      self.num_choices						,      -1      ).contiguous()
										snake_case        =     model(
										    lowerCAmelCase						,      attention_mask=lowerCAmelCase						,      token_type_ids=lowerCAmelCase						,      labels=lowerCAmelCase						,      )
										self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.num_choices)      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.prepare_config_and_inputs()
										(
										    (
										    snake_case
										)   ,(
										    snake_case
										)   ,(
										    snake_case
										)   ,(
										    snake_case
										)   ,(
										    snake_case
										)   ,(
										    snake_case
										)   ,(
										    snake_case
										)   ,(
										    snake_case
										)   ,(
										    snake_case
										)   ,
										)        =     config_and_inputs
										snake_case        =     {
										    'input_ids': input_ids,
										    'token_type_ids': token_type_ids,
										    'lengths': input_lengths,
										    'attention_mask': input_mask,
										}
										return config, inputs_dict





@require_torch
class 					lowerCAmelCase_      (     lowerCAmelCase       ,      lowerCAmelCase       ,      unittest.TestCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      List[Any]     					=  (
				    (
				        FlaubertModel,
				        FlaubertWithLMHeadModel,
				        FlaubertForQuestionAnswering,
				        FlaubertForQuestionAnsweringSimple,
				        FlaubertForSequenceClassification,
				        FlaubertForTokenClassification,
				        FlaubertForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				_lowerCAmelCase       :      Any     					=  (
				    {
				        """feature-extraction""": FlaubertModel,
				        """fill-mask""": FlaubertWithLMHeadModel,
				        """question-answering""": FlaubertForQuestionAnsweringSimple,
				        """text-classification""": FlaubertForSequenceClassification,
				        """token-classification""": FlaubertForTokenClassification,
				        """zero-shot""": FlaubertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

										if (
										    pipeline_test_casse_name == "QAPipelineTests"
										    and tokenizer_name is not None
										    and not tokenizer_name.endswith('Fast'      )
										):
																# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																return True

										return False

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase=False      ):






										"""simple docstring"""

										snake_case        =     super()._prepare_for_class(lowerCAmelCase						,      lowerCAmelCase						,      return_labels=lowerCAmelCase      )

										if return_labels:
																if model_class.__name__ == "FlaubertForQuestionAnswering":
																						snake_case        =     torch.zeros(
																						    self.model_tester.batch_size						,      dtype=torch.long						,      device=lowerCAmelCase      )
																						snake_case        =     torch.zeros(
																						    self.model_tester.batch_size						,      dtype=torch.long						,      device=lowerCAmelCase      )

										return inputs_dict

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     FlaubertModelTester(self      )
										snake_case        =     ConfigTester(self						,      config_class=lowerCAmelCase						,      emb_dim=37      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										self.config_tester.run_common_tests()

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_flaubert_model(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_flaubert_lm_head(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_flaubert_simple_qa(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_flaubert_qa(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_flaubert_token_classif(*lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_flaubert_multiple_choice(*lowerCAmelCase      )

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

										for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case        =     FlaubertModel.from_pretrained(lowerCAmelCase      )
																self.assertIsNotNone(lowerCAmelCase      )

				@slow
				@require_torch_gpu
				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case   ,snake_case        =     self.model_tester.prepare_config_and_inputs_for_common()
										for model_class in self.all_model_classes:
																# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
																if model_class == FlaubertForMultipleChoice:
																						return

																snake_case        =     True
																snake_case        =     model_class(config=lowerCAmelCase      )

																snake_case        =     self._prepare_for_class(lowerCAmelCase						,      lowerCAmelCase      )
																snake_case        =     torch.jit.trace(
																    lowerCAmelCase						,      (inputs_dict['input_ids'].to('cpu'      ), inputs_dict['attention_mask'].to('cpu'      ))      )

																with tempfile.TemporaryDirectory() as tmp:
																						torch.jit.save(lowerCAmelCase						,      os.path.join(lowerCAmelCase						,      'traced_model.pt'      )      )
																						snake_case        =     torch.jit.load(os.path.join(lowerCAmelCase						,      'traced_model.pt'      )						,      map_location=lowerCAmelCase      )
																						loaded(inputs_dict['input_ids'].to(lowerCAmelCase      )						,      inputs_dict['attention_mask'].to(lowerCAmelCase      )      )





@require_torch
class 					lowerCAmelCase_      (     unittest.TestCase   ):



				"""simple docstring"""

				@slow
				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'      )
										snake_case        =     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]      )
										with torch.no_grad():
																snake_case        =     model(lowerCAmelCase      )[0]
										snake_case        =     torch.Size((1, 11, 7_68)      )
										self.assertEqual(output.shape						,      lowerCAmelCase      )
										snake_case        =     torch.tensor(
										    [[[-2.62_51, -1.42_98, -0.02_27], [-2.85_10, -1.63_87, 0.22_58], [-2.81_14, -1.18_32, -0.30_66]]]      )

										self.assertTrue(torch.allclose(output[:, :3, :3]						,      lowerCAmelCase						,      atol=1E-4      )      )



"""simple docstring"""
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

SCREAMING_SNAKE_CASE__   =					random.Random()





def 				lowerCAmelCase__      (     _UpperCamelCase    :			List[Any]	,     _UpperCamelCase    :			Optional[Any]=1.0	,     _UpperCamelCase    :			Optional[int]=None	,     _UpperCamelCase    :			Optional[int]=None   )   ->			Optional[int]:





						"""simple docstring"""






						if rng is None:
												snake_case        =     global_rng

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

						return values



class 					lowerCAmelCase_      (     unittest.TestCase   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase						,      lowerCAmelCase=7						,      lowerCAmelCase=4_00						,      lowerCAmelCase=20_00						,      lowerCAmelCase=20_48						,      lowerCAmelCase=1_28						,      lowerCAmelCase=1						,      lowerCAmelCase=5_12						,      lowerCAmelCase=30						,      lowerCAmelCase=4_41_00						,      ):






										"""simple docstring"""

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

				def 						snake_case	(			self      ):






										"""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 						snake_case	(			self						,      lowerCAmelCase=False						,      lowerCAmelCase=False      ):






										"""simple docstring"""

										def _flatten(lowerCAmelCase      ):
																return list(itertools.chain(*lowerCAmelCase      )      )

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





@require_torch
@require_torchaudio
class 					lowerCAmelCase_      (     lowerCAmelCase       ,      unittest.TestCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      Union[str, Any]     					=  TvltFeatureExtractor

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     TvltFeatureExtractionTester(self      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self.feature_extraction_class(**self.feat_extract_dict      )
										self.assertTrue(hasattr(lowerCAmelCase						,      'spectrogram_length'      )      )
										self.assertTrue(hasattr(lowerCAmelCase						,      'feature_size'      )      )
										self.assertTrue(hasattr(lowerCAmelCase						,      'num_audio_channels'      )      )
										self.assertTrue(hasattr(lowerCAmelCase						,      'hop_length'      )      )
										self.assertTrue(hasattr(lowerCAmelCase						,      'chunk_length'      )      )
										self.assertTrue(hasattr(lowerCAmelCase						,      'sampling_rate'      )      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

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

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

										snake_case        =     feat_extract_first.to_dict()
										snake_case        =     feat_extract_second.to_dict()
										snake_case        =     dict_first.pop('mel_filters'      )
										snake_case        =     dict_second.pop('mel_filters'      )
										self.assertTrue(np.allclose(lowerCAmelCase						,      lowerCAmelCase      )      )
										self.assertEqual(lowerCAmelCase						,      lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

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

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

										snake_case        =     feat_extract_first.to_dict()
										snake_case        =     feat_extract_second.to_dict()
										snake_case        =     dict_first.pop('mel_filters'      )
										snake_case        =     dict_second.pop('mel_filters'      )
										self.assertTrue(np.allclose(lowerCAmelCase						,      lowerCAmelCase      )      )
										self.assertEqual(lowerCAmelCase						,      lowerCAmelCase      )

				def 						snake_case	(			self      ):






										"""simple docstring"""

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

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

										# Test not batched input
										snake_case        =     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
										snake_case        =     feature_extractor(lowerCAmelCase						,      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
										snake_case        =     feature_extractor(
										    lowerCAmelCase						,      return_tensors='np'						,      sampling_rate=4_41_00						,      mask_audio=lowerCAmelCase      ).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.
										snake_case        =     [floats_list((1, x)      )[0] for x in (8_00, 8_00, 8_00)]
										snake_case        =     np.asarray(lowerCAmelCase      )
										snake_case        =     feature_extractor(lowerCAmelCase						,      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 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

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

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

				def 						snake_case	(			self      ):






										"""simple docstring"""

										snake_case        =     self._load_datasamples(1      )
										snake_case        =     TvltFeatureExtractor()
										snake_case        =     feature_extractor(lowerCAmelCase						,      return_tensors='pt'      ).audio_values

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

										snake_case        =     torch.tensor([[-0.30_32, -0.27_08], [-0.44_34, -0.40_07]]      )
										self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]						,      lowerCAmelCase						,      atol=1E-4      )      )


