





"""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      __SCREAMING_SNAKE_CASE    :







  '''simple docstring'''


  def __init__(		self   :       str					,   __a   :       Optional[int]					,   )		->				str:
        _UpperCamelCase      :	List[str]						=       parent
        _UpperCamelCase      :	List[str]						=       13
        _UpperCamelCase      :	List[Any]						=       7
        _UpperCamelCase      :	List[Any]						=       True
        _UpperCamelCase      :	List[Any]						=       True
        _UpperCamelCase      :	List[Any]						=       True
        _UpperCamelCase      :	int						=       True
        _UpperCamelCase      :	Optional[Any]						=       True
        _UpperCamelCase      :	Dict						=       False
        _UpperCamelCase      :	List[str]						=       False
        _UpperCamelCase      :	Dict						=       False
        _UpperCamelCase      :	Dict						=       2
        _UpperCamelCase      :	Any						=       99
        _UpperCamelCase      :	Optional[Any]						=       0
        _UpperCamelCase      :	Union[str, Any]						=       32
        _UpperCamelCase      :	int						=       2
        _UpperCamelCase      :	List[str]						=       4
        _UpperCamelCase      :	Dict						=       0.1
        _UpperCamelCase      :	Any						=       0.1
        _UpperCamelCase      :	Tuple						=       512
        _UpperCamelCase      :	Optional[Any]						=       16
        _UpperCamelCase      :	Union[str, Any]						=       2
        _UpperCamelCase      :	str						=       0.02
        _UpperCamelCase      :	List[Any]						=       3
        _UpperCamelCase      :	Union[str, Any]						=       4
        _UpperCamelCase      :	Optional[int]						=       "last"
        _UpperCamelCase      :	int						=       True
        _UpperCamelCase      :	Optional[Any]						=       None
        _UpperCamelCase      :	Optional[int]						=       0

  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple	)		->				List[Any]:
        _UpperCamelCase      :	List[Any]						=       ids_tensor([self.batch_size, self.seq_length]					,   self.vocab_size	)
        _UpperCamelCase      :	str						=       random_attention_mask([self.batch_size, self.seq_length]					,   dtype=tf.floataa	)

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

        _UpperCamelCase      :	str						=       None
        if self.use_token_type_ids:
              _UpperCamelCase      :	Union[str, Any]						=       ids_tensor([self.batch_size, self.seq_length]					,   self.n_langs	)

        _UpperCamelCase      :	int						=       None
        _UpperCamelCase      :	List[Any]						=       None
        _UpperCamelCase      :	Tuple						=       None
        if self.use_labels:
              _UpperCamelCase      :	Dict						=       ids_tensor([self.batch_size]					,   self.type_sequence_label_size	)
              _UpperCamelCase      :	List[str]						=       ids_tensor([self.batch_size, self.seq_length]					,   self.num_labels	)
              _UpperCamelCase      :	Tuple						=       ids_tensor([self.batch_size]					,   2					,   dtype=tf.floataa	)
              _UpperCamelCase      :	List[Any]						=       ids_tensor([self.batch_size]					,   self.num_choices	)

        _UpperCamelCase      :	Dict						=       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    __SCREAMING_SNAKE_CASE				(		self   :       int					,   __a   :       str					,   __a   :       Tuple					,   __a   :       Optional[Any]					,   __a   :       Dict					,   __a   :       Union[str, Any]					,   __a   :       Any					,   __a   :       Union[str, Any]					,   __a   :       int					,   __a   :       Optional[int]					,   )		->				Optional[int]:
        _UpperCamelCase      :	Tuple						=       TFFlaubertModel(config=__a	)
        _UpperCamelCase      :	Dict						=       {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
        _UpperCamelCase      :	Optional[int]						=       model(__a	)

        _UpperCamelCase      :	Optional[Any]						=       [input_ids, input_mask]
        _UpperCamelCase      :	str						=       model(__a	)
        self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size)	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       str					,   __a   :       str					,   __a   :       Tuple					,   __a   :       List[str]					,   __a   :       List[Any]					,   __a   :       Optional[Any]					,   __a   :       Dict					,   __a   :       Optional[int]					,   __a   :       Optional[Any]					,   __a   :       Union[str, Any]					,   )		->				int:
        _UpperCamelCase      :	str						=       TFFlaubertWithLMHeadModel(__a	)

        _UpperCamelCase      :	Optional[int]						=       {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
        _UpperCamelCase      :	Union[str, Any]						=       model(__a	)

        self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.vocab_size)	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       str					,   __a   :       Optional[int]					,   __a   :       Tuple					,   __a   :       int					,   __a   :       Tuple					,   __a   :       Optional[Any]					,   __a   :       List[str]					,   __a   :       List[Any]					,   __a   :       Optional[int]					,   __a   :       int					,   )		->				List[str]:
        _UpperCamelCase      :	Dict						=       TFFlaubertForQuestionAnsweringSimple(__a	)

        _UpperCamelCase      :	int						=       {"input_ids": input_ids, "lengths": input_lengths}

        _UpperCamelCase      :	List[str]						=       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    __SCREAMING_SNAKE_CASE				(		self   :       List[Any]					,   __a   :       int					,   __a   :       Tuple					,   __a   :       List[Any]					,   __a   :       List[str]					,   __a   :       str					,   __a   :       int					,   __a   :       str					,   __a   :       Tuple					,   __a   :       str					,   )		->				Dict:
        _UpperCamelCase      :	Any						=       TFFlaubertForSequenceClassification(__a	)

        _UpperCamelCase      :	Optional[int]						=       {"input_ids": input_ids, "lengths": input_lengths}

        _UpperCamelCase      :	Union[str, Any]						=       model(__a	)

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

  def    __SCREAMING_SNAKE_CASE				(		self   :       Union[str, Any]					,   __a   :       Tuple					,   __a   :       Dict					,   __a   :       Any					,   __a   :       List[str]					,   __a   :       Union[str, Any]					,   __a   :       int					,   __a   :       Optional[Any]					,   __a   :       Dict					,   __a   :       Tuple					,   )		->				Optional[Any]:
        _UpperCamelCase      :	Dict						=       self.num_labels
        _UpperCamelCase      :	int						=       TFFlaubertForTokenClassification(config=__a	)
        _UpperCamelCase      :	Optional[Any]						=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        _UpperCamelCase      :	Dict						=       model(__a	)
        self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.num_labels)	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Union[str, Any]					,   __a   :       Optional[int]					,   __a   :       Optional[Any]					,   __a   :       Tuple					,   __a   :       Tuple					,   __a   :       Any					,   __a   :       Union[str, Any]					,   __a   :       int					,   __a   :       Tuple					,   __a   :       str					,   )		->				List[str]:
        _UpperCamelCase      :	int						=       self.num_choices
        _UpperCamelCase      :	str						=       TFFlaubertForMultipleChoice(config=__a	)
        _UpperCamelCase      :	List[str]						=       tf.tile(tf.expand_dims(__a					,   1	)					,   (1, self.num_choices, 1)	)
        _UpperCamelCase      :	Any						=       tf.tile(tf.expand_dims(__a					,   1	)					,   (1, self.num_choices, 1)	)
        _UpperCamelCase      :	List[str]						=       tf.tile(tf.expand_dims(__a					,   1	)					,   (1, self.num_choices, 1)	)
        _UpperCamelCase      :	List[str]						=       {
            "input_ids": multiple_choice_inputs_ids,
            "attention_mask": multiple_choice_input_mask,
            "token_type_ids": multiple_choice_token_type_ids,
        }
        _UpperCamelCase      :	Tuple						=       model(__a	)
        self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_choices)	)


  def    __SCREAMING_SNAKE_CASE				(		self   :       Any	)		->				Dict:
        _UpperCamelCase      :	Optional[Any]						=       self.prepare_config_and_inputs()
        (
            (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  (
            _UpperCamelCase					
        ),  					
        )      :	int						=       config_and_inputs
        _UpperCamelCase      :	Dict						=       {
            "input_ids": input_ids,
            "token_type_ids": token_type_ids,
            "langs": token_type_ids,
            "lengths": input_lengths,
        }
        return config, inputs_dict





@require_tf
class      __SCREAMING_SNAKE_CASE    (						_UpperCamelCase		,  _UpperCamelCase		,  unittest.TestCase   ):







  '''simple docstring'''


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

  def    __SCREAMING_SNAKE_CASE				(		self   :       int					,   __a   :       List[str]					,   __a   :       Dict					,   __a   :       Optional[int]					,   __a   :       Union[str, Any]					,   __a   :       List[Any]	)		->				Optional[Any]:







        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    __SCREAMING_SNAKE_CASE				(		self   :       Optional[Any]	)		->				int:
        _UpperCamelCase      :	List[Any]						=       TFFlaubertModelTester(self	)
        _UpperCamelCase      :	Any						=       ConfigTester(self					,   config_class=__a					,   emb_dim=37	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]	)		->				Any:
        self.config_tester.run_common_tests()

  def    __SCREAMING_SNAKE_CASE				(		self   :       Dict	)		->				Union[str, Any]:
        _UpperCamelCase      :	int						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_model(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       str	)		->				Optional[int]:
        _UpperCamelCase      :	List[str]						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_lm_head(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple	)		->				Dict:
        _UpperCamelCase      :	Tuple						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_qa(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[int]	)		->				Any:
        _UpperCamelCase      :	Optional[int]						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_sequence_classif(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[Any]	)		->				Optional[int]:
        _UpperCamelCase      :	str						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_for_token_classification(*__a	)

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[Any]	)		->				Union[str, Any]:
        _UpperCamelCase      :	List[str]						=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_flaubert_for_multiple_choice(*__a	)


  @slow
  def    __SCREAMING_SNAKE_CASE				(		self   :       int	)		->				Optional[Any]:







        for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _UpperCamelCase      :	Union[str, Any]						=       TFFlaubertModel.from_pretrained(__a	)
              self.assertIsNotNone(__a	)





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







  '''simple docstring'''


  @slow
  def    __SCREAMING_SNAKE_CASE				(		self   :       Any	)		->				List[str]:
        _UpperCamelCase      :	int						=       TFFlaubertModel.from_pretrained("jplu/tf-flaubert-small-cased"	)

        _UpperCamelCase      :	Optional[Any]						=       tf.convert_to_tensor(
            [[0, 158, 735, 2592, 1424, 6727, 82, 1]]					,   dtype=tf.intaa					,   )  # "J'aime flaubert !"

        _UpperCamelCase      :	Optional[Any]						=       model(__a	)[0]
        _UpperCamelCase      :	Optional[int]						=       tf.TensorShape((1, 8, 512)	)
        self.assertEqual(output.shape					,   __a	)
        # compare the actual values for a slice.
        _UpperCamelCase      :	Union[str, Any]						=       tf.convert_to_tensor(
            [
                [
                    [-1.8_76_87_73, -1.56_65_55, 0.27_07_24_18],
                    [-1.6_92_00_38, -0.5_87_35_05, 1.9_32_95_99],
                    [-2.9_56_39_85, -1.6_99_38_35, 1.7_97_20_52],
                ]
            ]					,   dtype=tf.floataa					,   )

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











"""simple docstring"""


import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append(".")




def 					lowercase__    (					lowercase_			)					->		int:




      """simple docstring"""

      _UpperCamelCase      :	Any						=       test_file.split(os.path.sep			)
      if components[0:2] != ["tests", "models"]:
            raise ValueError(
                "`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got "
                F'''{test_file} instead.'''			)
      _UpperCamelCase      :	str						=       components[-1]
      if not test_fn.endswith("py"			):
            raise ValueError(F'''`test_file` should be a python file. Got {test_fn} instead.'''			)
      if not test_fn.startswith("test_modeling_"			):
            raise ValueError(
                F'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.'''			)

      _UpperCamelCase      :	Dict						=       components[:-1] + [test_fn.replace(".py"		,""			)]
      _UpperCamelCase      :	List[str]						=       ".".join(lowercase_			)

      return test_module_path




def 					lowercase__    (					lowercase_			)					->		List[Any]:




      """simple docstring"""

      _UpperCamelCase      :	Optional[Any]						=       get_module_path(lowercase_			)
      _UpperCamelCase      :	str						=       importlib.import_module(lowercase_			)

      return test_module




def 					lowercase__    (					lowercase_			)					->		Optional[Any]:




      """simple docstring"""

      _UpperCamelCase      :	Union[str, Any]						=       []
      _UpperCamelCase      :	List[Any]						=       get_test_module(lowercase_			)
      for attr in dir(lowercase_			):
            if attr.endswith("ModelTester"			):
                  tester_classes.append(getattr(lowercase_		,lowercase_			)			)

    # sort with class names
      return sorted(lowercase_		,key=lambda lowercase_			: x.__name__			)




def 					lowercase__    (					lowercase_			)					->		Tuple:




      """simple docstring"""

      _UpperCamelCase      :	Optional[Any]						=       []
      _UpperCamelCase      :	Any						=       get_test_module(lowercase_			)
      for attr in dir(lowercase_			):
            _UpperCamelCase      :	int						=       getattr(lowercase_		,lowercase_			)
            # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
            # `all_model_classes` is not empty (which also excludes other special classes).
            _UpperCamelCase      :	Optional[Any]						=       getattr(lowercase_		,"all_model_classes"		,[]			)
            if len(lowercase_			) > 0:
                  test_classes.append(lowercase_			)

    # sort with class names
      return sorted(lowercase_		,key=lambda lowercase_			: x.__name__			)




def 					lowercase__    (					lowercase_			)					->		Any:




      """simple docstring"""

      _UpperCamelCase      :	Dict						=       get_test_classes(lowercase_			)
      _UpperCamelCase      :	int						=       set()
      for test_class in test_classes:
            model_classes.update(test_class.all_model_classes			)

      # sort with class names
      return sorted(lowercase_		,key=lambda lowercase_			: x.__name__			)




def 					lowercase__    (					lowercase_			)					->		Optional[Any]:




      """simple docstring"""

      _UpperCamelCase      :	List[str]						=       test_class()
      if hasattr(lowercase_		,"setUp"			):
            test.setUp()

      _UpperCamelCase      :	Tuple						=       None
      if hasattr(lowercase_		,"model_tester"			):
            # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
            if test.model_tester is not None:
                  _UpperCamelCase      :	Tuple						=       test.model_tester.__class__

      return model_tester




def 					lowercase__    (					lowercase_		,lowercase_			)					->		Union[str, Any]:




      """simple docstring"""

      _UpperCamelCase      :	str						=       get_test_classes(lowercase_			)

      _UpperCamelCase      :	Dict						=       []
      for test_class in test_classes:
            if model_class in test_class.all_model_classes:
                  target_test_classes.append(lowercase_			)

    # sort with class names
      return sorted(lowercase_		,key=lambda lowercase_			: x.__name__			)




def 					lowercase__    (					lowercase_		,lowercase_			)					->		Dict:




      """simple docstring"""

      _UpperCamelCase      :	Any						=       get_test_classes_for_model(lowercase_		,lowercase_			)

      _UpperCamelCase      :	List[Any]						=       []
      for test_class in test_classes:
            _UpperCamelCase      :	List[Any]						=       get_model_tester_from_test_class(lowercase_			)
            if tester_class is not None:
                  tester_classes.append(lowercase_			)

    # sort with class names
      return sorted(lowercase_		,key=lambda lowercase_			: x.__name__			)




def 					lowercase__    (					lowercase_			)					->		Union[str, Any]:




      """simple docstring"""

      _UpperCamelCase      :	Any						=       get_test_classes(lowercase_			)
      _UpperCamelCase      :	Tuple						=       {test_class: get_model_tester_from_test_class(lowercase_			) for test_class in test_classes}
      return test_tester_mapping




def 					lowercase__    (					lowercase_			)					->		Union[str, Any]:




      """simple docstring"""

      _UpperCamelCase      :	List[Any]						=       get_model_classes(lowercase_			)
      _UpperCamelCase      :	Optional[int]						=       {
          model_class: get_test_classes_for_model(lowercase_		,lowercase_			) for model_class in model_classes
      }
      return model_test_mapping




def 					lowercase__    (					lowercase_			)					->		Union[str, Any]:




      """simple docstring"""

      _UpperCamelCase      :	Optional[Any]						=       get_model_classes(lowercase_			)
      _UpperCamelCase      :	Tuple						=       {
          model_class: get_tester_classes_for_model(lowercase_		,lowercase_			) for model_class in model_classes
      }
      return model_to_tester_mapping




def 					lowercase__    (					lowercase_			)					->		Optional[int]:




      """simple docstring"""

      if isinstance(lowercase_		,lowercase_			):
            return o
      elif isinstance(lowercase_		,lowercase_			):
            return o.__name__
      elif isinstance(lowercase_		,(list, tuple)			):
            return [to_json(lowercase_			) for x in o]
      elif isinstance(lowercase_		,lowercase_			):
            return {to_json(lowercase_			): to_json(lowercase_			) for k, v in o.items()}
      else:
            return o
