



'''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 __A			(  A						):



 '''simple docstring'''







 def __init__(self ,   A ,   A=13 ,   A=7 ,   A=True ,   A=True ,   A=True ,   A=True ,   A=True ,   A=False ,   A=False ,   A=False ,   A=2 ,   A=99 ,   A=0 ,   A=32 ,   A=5 ,   A=4 ,   A=0.1 ,   A=0.1 ,   A=512 ,   A=12 ,   A=2 ,   A=0.02 ,   A=3 ,   A=4 ,   A="last" ,   A=None ,   A=None ,   )    ->     Dict:


      """simple docstring"""



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



 def 					a__  (self       )    ->     Tuple:


      """simple docstring"""



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

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

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

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

      _a      =  self.get_config()

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



 def 					a__  (self       )    ->     int:


      """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 					a__  (self ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   )    ->     Dict:


      """simple docstring"""



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



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


      """simple docstring"""



      _a      =  FlaubertWithLMHeadModel(A       )
      model.to(A       )
      model.eval()

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



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


      """simple docstring"""



      _a      =  FlaubertForQuestionAnsweringSimple(A       )
      model.to(A       )
      model.eval()

      _a      =  model(A       )

      _a      =  model(A ,   start_positions=A ,   end_positions=A       )
      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 					a__  (self ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   )    ->     Dict:


      """simple docstring"""



      _a      =  FlaubertForQuestionAnswering(A       )
      model.to(A       )
      model.eval()

      _a      =  model(A       )

      _a      =  model(
          A ,   start_positions=A ,   end_positions=A ,   cls_index=A ,   is_impossible=A ,   p_mask=A ,   )

      _a      =  model(
          A ,   start_positions=A ,   end_positions=A ,   cls_index=A ,   is_impossible=A ,   )

      ((_a)  ,    )      =  result_with_labels.to_tuple()

      _a      =  model(A ,   start_positions=A ,   end_positions=A       )

      ((_a)  ,    )      =  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 					a__  (self ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   )    ->     Optional[Any]:


      """simple docstring"""



      _a      =  FlaubertForSequenceClassification(A       )
      model.to(A       )
      model.eval()

      _a      =  model(A       )
      _a      =  model(A ,   labels=A       )

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



 def 					a__  (self ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   A ,   )    ->     str:


      """simple docstring"""



      _a      =  self.num_labels
      _a      =  FlaubertForTokenClassification(A       )
      model.to(A       )
      model.eval()

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



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


      """simple docstring"""



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



 def 					a__  (self       )    ->     int:


      """simple docstring"""



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






@require_torch
class __A			(  A   ,   A   ,   unittest.TestCase						):



 '''simple docstring'''







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



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


      """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 					a__  (self ,   A ,   A ,   A=False       )    ->     Tuple:


      """simple docstring"""



      _a      =  super()._prepare_for_class(A ,   A ,   return_labels=A       )

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

      return inputs_dict



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


      """simple docstring"""



      _a      =  FlaubertModelTester(self       )
      _a      =  ConfigTester(self ,   config_class=A ,   emb_dim=37       )



 def 					a__  (self       )    ->     Optional[int]:


      """simple docstring"""



      self.config_tester.run_common_tests()



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


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*A       )



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


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*A       )



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


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_simple_qa(*A       )



 def 					a__  (self       )    ->     int:


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*A       )



 def 					a__  (self       )    ->     str:


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*A       )



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


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_token_classif(*A       )



 def 					a__  (self       )    ->     Any:


      """simple docstring"""



      _a      =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_multiple_choice(*A       )



 @slow
 def 					a__  (self       )    ->     Dict:


      """simple docstring"""



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



 @slow
 @require_torch_gpu
 def 					a__  (self       )    ->     Union[str, Any]:


      """simple docstring"""



      _a  ,    _a      =  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

           _a      =  True
           _a      =  model_class(config=A       )

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

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






@require_torch
class __A			(  unittest.TestCase						):



 '''simple docstring'''







 @slow
 def 					a__  (self       )    ->     List[str]:


      """simple docstring"""



      _a      =  FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''       )
      _a      =  torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]       )
      with torch.no_grad():
           _a      =  model(A       )[0]
      _a      =  torch.Size((1, 11, 768)       )
      self.assertEqual(output.shape ,   A       )
      _a      =  torch.tensor(
          [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]       )

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








'''simple docstring'''





from typing import List

import datasets
from datasets.tasks import AudioClassification

from ..folder_based_builder import folder_based_builder


lowercase_		       =  datasets.utils.logging.get_logger(__name__)






class __A			(  folder_based_builder.FolderBasedBuilderConfig						):



 '''simple docstring'''







 __lowerCamelCase    :       bool          =       None
 __lowerCamelCase    :       bool          =       None






class __A			(  folder_based_builder.FolderBasedBuilder						):



 '''simple docstring'''







 __lowerCamelCase    :       int          =       datasets.Audio()
 __lowerCamelCase    :       str          =       'audio'
 __lowerCamelCase    :       Optional[Any]          =       AudioFolderConfig
 __lowerCamelCase    :       List[str]  # definition at the bottom of the script
 __lowerCamelCase    :       Union[str, Any]          =       AudioClassification(audio_column='audio'   ,   label_column='label'						)





lowercase_		       =  [
    ".aiff",
    ".au",
    ".avr",
    ".caf",
    ".flac",
    ".htk",
    ".svx",
    ".mat4",
    ".mat5",
    ".mpc2k",
    ".ogg",
    ".paf",
    ".pvf",
    ".raw",
    ".rf64",
    ".sd2",
    ".sds",
    ".ircam",
    ".voc",
    ".w64",
    ".wav",
    ".nist",
    ".wavex",
    ".wve",
    ".xi",
    ".mp3",
    ".opus",
]
lowercase_		       =  AUDIO_EXTENSIONS



