



"""simple docstring"""






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     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):



   def __init__(       self    :	Optional[int]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Union[str, Any]=1_3							,       __lowerCamelCase    :	Optional[Any]=7							,       __lowerCamelCase    :	List[str]=True							,       __lowerCamelCase    :	Dict=True							,       __lowerCamelCase    :	List[str]=True							,       __lowerCamelCase    :	Union[str, Any]=True							,       __lowerCamelCase    :	Optional[Any]=True							,       __lowerCamelCase    :	List[Any]=False							,       __lowerCamelCase    :	Optional[int]=False							,       __lowerCamelCase    :	int=False							,       __lowerCamelCase    :	Dict=2							,       __lowerCamelCase    :	Tuple=9_9							,       __lowerCamelCase    :	Optional[int]=0							,       __lowerCamelCase    :	Tuple=3_2							,       __lowerCamelCase    :	Tuple=5							,       __lowerCamelCase    :	Tuple=4							,       __lowerCamelCase    :	Any=0.1							,       __lowerCamelCase    :	int=0.1							,       __lowerCamelCase    :	Any=5_1_2							,       __lowerCamelCase    :	str=1_2							,       __lowerCamelCase    :	Any=2							,       __lowerCamelCase    :	Any=0.0_2							,       __lowerCamelCase    :	Optional[int]=3							,       __lowerCamelCase    :	List[str]=4							,       __lowerCamelCase    :	Optional[int]="last"							,       __lowerCamelCase    :	str=None							,       __lowerCamelCase    :	int=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    __UpperCAmelCase    (       self    :	int				):



     """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    __UpperCAmelCase    (       self    :	Dict				):



     """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    __UpperCAmelCase    (       self    :	Optional[int]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	List[str]							,       ):



     """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    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Optional[Any]							,       ):



     """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    __UpperCAmelCase    (       self    :	List[str]							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	Dict							,       ):



     """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    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	Optional[int]							,       ):



     """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    __UpperCAmelCase    (       self    :	Dict							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	str							,       ):



     """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    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	Union[str, Any]							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	List[Any]							,       ):



     """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    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	int							,       ):



     """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    __UpperCAmelCase    (       self    :	Dict				):



     """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     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,unittest.TestCase  ):
   A__		:	Optional[int]           =  (
       (
           FlaubertModel,
           FlaubertWithLMHeadModel,
           FlaubertForQuestionAnswering,
           FlaubertForQuestionAnsweringSimple,
           FlaubertForSequenceClassification,
           FlaubertForTokenClassification,
           FlaubertForMultipleChoice,
       )
       if is_torch_available()
       else ()
   )
   A__		:	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    __UpperCAmelCase    (       self    :	int							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Optional[int]				):



     """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    __UpperCAmelCase    (       self    :	Union[str, Any]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Union[str, Any]=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    __UpperCAmelCase    (       self    :	List[str]				):



     """simple docstring"""
     _snake_case   	=      FlaubertModelTester(self				)
     _snake_case   	=      ConfigTester(self							,       config_class=__lowerCamelCase							,       emb_dim=3_7				)



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     self.config_tester.run_common_tests()



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_model(*__lowerCamelCase				)



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



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_lm_head(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_qa(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCamelCase				)



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



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_token_classif(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCamelCase				)



   @slow
   def    __UpperCAmelCase    (       self    :	int				):



     """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    __UpperCAmelCase    (       self    :	List[Any]				):



     """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     UpperCAmelCase    (	unittest.TestCase  ):



   @slow
   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     _snake_case   	=      FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''				)
     _snake_case   	=      torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]				)
     with torch.no_grad():
       _snake_case   	=      model(__lowerCamelCase				)[0]
     _snake_case   	=      torch.Size((1, 1_1, 7_6_8)				)
     self.assertEqual(output.shape							,       __lowerCamelCase				)
     _snake_case   	=      torch.tensor(
         [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]				)

     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

snake_case   					=				random.Random()




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_=1.0  ,						lowerCAmelCase_=None  ,						lowerCAmelCase_=None	)						->				List[str]:
  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     UpperCAmelCase    (	unittest.TestCase  ):



   def __init__(       self    :	Tuple							,       __lowerCamelCase    :	List[str]							,       __lowerCamelCase    :	Optional[Any]=7							,       __lowerCamelCase    :	List[Any]=4_0_0							,       __lowerCamelCase    :	Any=2_0_0_0							,       __lowerCamelCase    :	Any=2_0_4_8							,       __lowerCamelCase    :	Any=1_2_8							,       __lowerCamelCase    :	Any=1							,       __lowerCamelCase    :	Optional[int]=5_1_2							,       __lowerCamelCase    :	Tuple=3_0							,       __lowerCamelCase    :	List[Any]=4_4_1_0_0							,       ):



     """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    __UpperCAmelCase    (       self    :	List[Any]				):



     """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]							,       __lowerCamelCase    :	Any=False							,       __lowerCamelCase    :	int=False				):



     """simple docstring"""
     def _flatten(__lowerCamelCase    :	List[str]				):
       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     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE,unittest.TestCase  ):
   A__		:	Tuple           =  TvltFeatureExtractor



   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     _snake_case   	=      TvltFeatureExtractionTester(self				)



   def    __UpperCAmelCase    (       self    :	Any				):



     """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    __UpperCAmelCase    (       self    :	int				):



     """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    __UpperCAmelCase    (       self    :	int				):



     """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    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     # Initialize feature_extractor
     _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_0_0							,       1_4_0_0							,       2_0_0				)]
     _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_4_1_0_0				).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_4_1_0_0				).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_4_1_0_0							,       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_0_0, 8_0_0, 8_0_0)]
     _snake_case   	=      np.asarray(__lowerCamelCase				)
     _snake_case   	=      feature_extractor(__lowerCamelCase							,       return_tensors='''np'''							,       sampling_rate=4_4_1_0_0				).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    :	Union[str, Any]							,       __lowerCamelCase    :	Optional[int]				):



     """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    __UpperCAmelCase    (       self    :	Dict				):



     """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_9_2, 1_2_8)				)

     _snake_case   	=      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]							,       __lowerCamelCase							,       atol=1E-4				)				)





