



'''simple docstring'''





from __future__ import annotations

import unittest

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

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


if is_tf_available():
   import numpy as np
   import tensorflow as tf

   from transformers import (
       TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       FlaubertConfig,
       TFFlaubertForMultipleChoice,
       TFFlaubertForQuestionAnsweringSimple,
       TFFlaubertForSequenceClassification,
       TFFlaubertForTokenClassification,
       TFFlaubertModel,
       TFFlaubertWithLMHeadModel,
   )






class __A    :



 '''simple docstring'''







 def __init__(self				,     A				,     )					->  List[str]:


      """simple docstring"""



      _a											=						parent
      _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											=						2
      _a											=						4
      _a											=						0.1
      _a											=						0.1
      _a											=						512
      _a											=						16
      _a											=						2
      _a											=						0.02
      _a											=						3
      _a											=						4
      _a											=						'''last'''
      _a											=						True
      _a											=						None
      _a											=						0



 def       a__						(self )					->  Dict:


      """simple docstring"""



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

      _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				,     dtype=tf.floataa )
           _a											=						ids_tensor([self.batch_size]				,     self.num_choices )

      _a											=						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				,     bos_token_id=self.bos_token_id				,     )

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



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


      """simple docstring"""



      _a											=						TFFlaubertModel(config=A )
      _a											=						{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
      _a											=						model(A )

      _a											=						[input_ids, input_mask]
      _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				,     )					->  List[str]:


      """simple docstring"""



      _a											=						TFFlaubertWithLMHeadModel(A )

      _a											=						{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
      _a											=						model(A )

      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				,     )					->  Tuple:


      """simple docstring"""



      _a											=						TFFlaubertForQuestionAnsweringSimple(A )

      _a											=						{'''input_ids''': input_ids, '''lengths''': input_lengths}

      _a											=						model(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				,     )					->  Any:


      """simple docstring"""



      _a											=						TFFlaubertForSequenceClassification(A )

      _a											=						{'''input_ids''': input_ids, '''lengths''': input_lengths}

      _a											=						model(A )

      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				,     )					->  Optional[Any]:


      """simple docstring"""



      _a											=						self.num_labels
      _a											=						TFFlaubertForTokenClassification(config=A )
      _a											=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
      _a											=						model(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				,     )					->  Optional[int]:


      """simple docstring"""



      _a											=						self.num_choices
      _a											=						TFFlaubertForMultipleChoice(config=A )
      _a											=						tf.tile(tf.expand_dims(A				,     1 )				,     (1, self.num_choices, 1) )
      _a											=						tf.tile(tf.expand_dims(A				,     1 )				,     (1, self.num_choices, 1) )
      _a											=						tf.tile(tf.expand_dims(A				,     1 )				,     (1, self.num_choices, 1) )
      _a											=						{
          '''input_ids''': multiple_choice_inputs_ids,
          '''attention_mask''': multiple_choice_input_mask,
          '''token_type_ids''': multiple_choice_token_type_ids,
      }
      _a											=						model(A )
      self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices) )



 def       a__						(self )					->  Tuple:


      """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,
          '''langs''': token_type_ids,
          '''lengths''': input_lengths,
      }
      return config, inputs_dict






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



 '''simple docstring'''







 __lowerCamelCase					: int    = (
     (
         TFFlaubertModel,
         TFFlaubertWithLMHeadModel,
         TFFlaubertForSequenceClassification,
         TFFlaubertForQuestionAnsweringSimple,
         TFFlaubertForTokenClassification,
         TFFlaubertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 __lowerCamelCase					: Any    = (
     (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Check other models whether language generation is also applicable
 __lowerCamelCase					: Union[str, Any]    = (
     {
         'feature-extraction': TFFlaubertModel,
         'fill-mask': TFFlaubertWithLMHeadModel,
         'question-answering': TFFlaubertForQuestionAnsweringSimple,
         'text-classification': TFFlaubertForSequenceClassification,
         'token-classification': TFFlaubertForTokenClassification,
         'zero-shot': TFFlaubertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __lowerCamelCase					: Union[str, Any]    = False
 __lowerCamelCase					: Union[str, Any]    = False



 def       a__						(self				,     A				,     A				,     A				,     A				,     A )					->  List[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 )					->  Tuple:


      """simple docstring"""



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



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


      """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 )					->  Optional[Any]:


      """simple docstring"""



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



 def       a__						(self )					->  Dict:


      """simple docstring"""



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



 def       a__						(self )					->  Any:


      """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_for_token_classification(*A )



 def       a__						(self )					->  Tuple:


      """simple docstring"""



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



 @slow
 def       a__						(self )					->  Dict:


      """simple docstring"""



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






@require_tf
@require_sentencepiece
@require_tokenizers
class __A    (						unittest.TestCase		):



 '''simple docstring'''







 @slow
 def       a__						(self )					->  Any:


      """simple docstring"""



      _a											=						TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased''' )

      _a											=						tf.convert_to_tensor(
          [[0, 158, 735, 2_592, 1_424, 6_727, 82, 1]]				,     dtype=tf.intaa				,     )  # "J'aime flaubert !"

      _a											=						model(A )[0]
      _a											=						tf.TensorShape((1, 8, 512) )
      self.assertEqual(output.shape				,     A )
      # compare the actual values for a slice.
      _a											=						tf.convert_to_tensor(
          [
              [
                  [-1.8768773, -1.566555, 0.27072418],
                  [-1.6920038, -0.5873505, 1.9329599],
                  [-2.9563985, -1.6993835, 1.7972052],
              ]
          ]				,     dtype=tf.floataa				,     )

      self.assertTrue(np.allclose(output[:, :3, :3].numpy()				,     expected_slice.numpy()				,     atol=1E-4 ) )








'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import MobileNetVaImageProcessor






class __A    (						unittest.TestCase		):



 '''simple docstring'''







 def __init__(self				,     A				,     A=7				,     A=3				,     A=18				,     A=30				,     A=400				,     A=True				,     A=None				,     A=True				,     A=None				,     )					->  Any:


      """simple docstring"""



      _a											=						size if size is not None else {'''shortest_edge''': 20}
      _a											=						crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
      _a											=						parent
      _a											=						batch_size
      _a											=						num_channels
      _a											=						image_size
      _a											=						min_resolution
      _a											=						max_resolution
      _a											=						do_resize
      _a											=						size
      _a											=						do_center_crop
      _a											=						crop_size



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


      """simple docstring"""



      return {
          "do_resize": self.do_resize,
          "size": self.size,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
      }






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



 '''simple docstring'''







 __lowerCamelCase					: Tuple    = MobileNetVaImageProcessor if is_vision_available() else None



 def       a__						(self )					->  int:


      """simple docstring"""



      _a											=						MobileNetVaImageProcessingTester(self )



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


      """simple docstring"""



      return self.image_processor_tester.prepare_image_processor_dict()



 def       a__						(self )					->  str:


      """simple docstring"""



      _a											=						self.image_processing_class(**self.image_processor_dict )
      self.assertTrue(hasattr(A				,     '''do_resize''' ) )
      self.assertTrue(hasattr(A				,     '''size''' ) )
      self.assertTrue(hasattr(A				,     '''do_center_crop''' ) )
      self.assertTrue(hasattr(A				,     '''crop_size''' ) )



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


      """simple docstring"""



      _a											=						self.image_processing_class.from_dict(self.image_processor_dict )
      self.assertEqual(image_processor.size				,     {'''shortest_edge''': 20} )
      self.assertEqual(image_processor.crop_size				,     {'''height''': 18, '''width''': 18} )

      _a											=						self.image_processing_class.from_dict(self.image_processor_dict				,     size=42				,     crop_size=84 )
      self.assertEqual(image_processor.size				,     {'''shortest_edge''': 42} )
      self.assertEqual(image_processor.crop_size				,     {'''height''': 84, '''width''': 84} )



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


      """simple docstring"""



      pass



 def       a__						(self )					->  Any:


      """simple docstring"""



      _a											=						self.image_processing_class(**self.image_processor_dict )
      # create random PIL images
      _a											=						prepare_image_inputs(self.image_processor_tester				,     equal_resolution=A )
      for image in image_inputs:
           self.assertIsInstance(A				,     Image.Image )

      # Test not batched input
      _a											=						image_processing(image_inputs[0]				,     return_tensors='''pt''' ).pixel_values
      self.assertEqual(
          encoded_images.shape				,     (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          )				,     )

      # Test batched
      _a											=						image_processing(A				,     return_tensors='''pt''' ).pixel_values
      self.assertEqual(
          encoded_images.shape				,     (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          )				,     )



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


      """simple docstring"""



      _a											=						self.image_processing_class(**self.image_processor_dict )
      # create random numpy tensors
      _a											=						prepare_image_inputs(self.image_processor_tester				,     equal_resolution=A				,     numpify=A )
      for image in image_inputs:
           self.assertIsInstance(A				,     np.ndarray )

      # Test not batched input
      _a											=						image_processing(image_inputs[0]				,     return_tensors='''pt''' ).pixel_values
      self.assertEqual(
          encoded_images.shape				,     (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          )				,     )

      # Test batched
      _a											=						image_processing(A				,     return_tensors='''pt''' ).pixel_values
      self.assertEqual(
          encoded_images.shape				,     (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          )				,     )



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


      """simple docstring"""



      _a											=						self.image_processing_class(**self.image_processor_dict )
      # create random PyTorch tensors
      _a											=						prepare_image_inputs(self.image_processor_tester				,     equal_resolution=A				,     torchify=A )
      for image in image_inputs:
           self.assertIsInstance(A				,     torch.Tensor )

      # Test not batched input
      _a											=						image_processing(image_inputs[0]				,     return_tensors='''pt''' ).pixel_values
      self.assertEqual(
          encoded_images.shape				,     (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          )				,     )

      # Test batched
      _a											=						image_processing(A				,     return_tensors='''pt''' ).pixel_values
      self.assertEqual(
          encoded_images.shape				,     (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['''height'''],
              self.image_processor_tester.crop_size['''width'''],
          )				,     )



