


'''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   __snake_case			:







    """simple docstring"""



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

    def __lowercase  (  self       :    List[str]						)  ->    Optional[int]:
      lowerCAmelCase_     : List[str]     =  ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size						)
      lowerCAmelCase_     : Optional[int]     =  random_attention_mask([self.batch_size, self.seq_length]       ,		dtype=tf.floataa						)

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

      lowerCAmelCase_     : Optional[int]     =  None
      if self.use_token_type_ids:
        lowerCAmelCase_     : List[str]     =  ids_tensor([self.batch_size, self.seq_length]       ,		self.n_langs						)

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

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

      lowerCAmelCase_     : Optional[Any]     =  [input_ids, input_mask]
      lowerCAmelCase_     : Tuple     =  model(lowerCamelCase						)
      self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, self.seq_length, self.hidden_size)						)

    def __lowercase  (  self       :    Optional[Any]       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Union[str, Any]       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    int       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Dict       ,		lowerCamelCase       :    int       ,		)  ->    Any:
      lowerCAmelCase_     : Optional[Any]     =  TFFlaubertWithLMHeadModel(lowerCamelCase						)

      lowerCAmelCase_     : Tuple     =  {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
      lowerCAmelCase_     : int     =  model(lowerCamelCase						)

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

    def __lowercase  (  self       :    Tuple       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Any       ,		lowerCamelCase       :    Union[str, Any]       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    List[str]       ,		lowerCamelCase       :    List[Any]       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    List[Any]       ,		)  ->    Dict:
      lowerCAmelCase_     : Optional[int]     =  TFFlaubertForQuestionAnsweringSimple(lowerCamelCase						)

      lowerCAmelCase_     : List[Any]     =  {"""input_ids""": input_ids, """lengths""": input_lengths}

      lowerCAmelCase_     : Any     =  model(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 __lowercase  (  self       :    Any       ,		lowerCamelCase       :    List[str]       ,		lowerCamelCase       :    List[Any]       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    Optional[int]       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    Optional[Any]       ,		lowerCamelCase       :    int       ,		lowerCamelCase       :    Optional[int]       ,		)  ->    Union[str, Any]:
      lowerCAmelCase_     : List[Any]     =  TFFlaubertForSequenceClassification(lowerCamelCase						)

      lowerCAmelCase_     : Tuple     =  {"""input_ids""": input_ids, """lengths""": input_lengths}

      lowerCAmelCase_     : List[Any]     =  model(lowerCamelCase						)

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

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

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





    def __lowercase  (  self       :    int						)  ->    List[Any]:
      lowerCAmelCase_     : Optional[Any]     =  self.prepare_config_and_inputs()
      (
          (
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				(
          lowerCAmelCase_
      ),				
      )     : Union[str, Any]     =  config_and_inputs
      lowerCAmelCase_     : str     =  {
          """input_ids""": input_ids,
          """token_type_ids""": token_type_ids,
          """langs""": token_type_ids,
          """lengths""": input_lengths,
      }
      return config, inputs_dict







@require_tf
class   __snake_case			(  _SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,unittest.TestCase):







    """simple docstring"""



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

    def __lowercase  (  self       :    Tuple       ,		lowerCamelCase       :    Tuple       ,		lowerCamelCase       :    int       ,		lowerCamelCase       :    Dict       ,		lowerCamelCase       :    Union[str, Any]       ,		lowerCamelCase       :    Optional[int]						)  ->    Union[str, 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 __lowercase  (  self       :    List[str]						)  ->    Optional[Any]:
      lowerCAmelCase_     : Any     =  TFFlaubertModelTester(self						)
      lowerCAmelCase_     : Dict     =  ConfigTester(self       ,		config_class=lowerCamelCase       ,		emb_dim=37						)

    def __lowercase  (  self       :    str						)  ->    Tuple:
      self.config_tester.run_common_tests()

    def __lowercase  (  self       :    Optional[Any]						)  ->    Optional[int]:
      lowerCAmelCase_     : List[Any]     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*lowerCamelCase						)

    def __lowercase  (  self       :    Dict						)  ->    Union[str, Any]:
      lowerCAmelCase_     : Optional[Any]     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase						)

    def __lowercase  (  self       :    Tuple						)  ->    Any:
      lowerCAmelCase_     : Dict     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase						)

    def __lowercase  (  self       :    str						)  ->    List[Any]:
      lowerCAmelCase_     : Dict     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase						)

    def __lowercase  (  self       :    Dict						)  ->    Dict:
      lowerCAmelCase_     : Optional[int]     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_token_classification(*lowerCamelCase						)

    def __lowercase  (  self       :    Optional[int]						)  ->    int:
      lowerCAmelCase_     : List[str]     =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_for_multiple_choice(*lowerCamelCase						)





    @slow
    def __lowercase  (  self       :    str						)  ->    List[Any]:
      for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCAmelCase_     : Any     =  TFFlaubertModel.from_pretrained(lowerCamelCase						)
        self.assertIsNotNone(lowerCamelCase						)







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







    """simple docstring"""



    @slow
    def __lowercase  (  self       :    int						)  ->    int:
      lowerCAmelCase_     : Dict     =  TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased"""						)

      lowerCAmelCase_     : Any     =  tf.convert_to_tensor(
          [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]]       ,		dtype=tf.intaa       ,		)  # "J'aime flaubert !"

      lowerCAmelCase_     : Union[str, Any]     =  model(lowerCamelCase						)[0]
      lowerCAmelCase_     : Any     =  tf.TensorShape((1, 8, 5_12)						)
      self.assertEqual(output.shape       ,		lowerCamelCase						)
      # compare the actual values for a slice.
      lowerCAmelCase_     : List[str]     =  tf.convert_to_tensor(
          [
              [
                  [-1.8_768_773, -1.566_555, 0.27_072_418],
                  [-1.6_920_038, -0.5_873_505, 1.9_329_599],
                  [-2.9_563_985, -1.6_993_835, 1.7_972_052],
              ]
          ]       ,		dtype=tf.floataa       ,		)

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







'''simple docstring'''






from __future__ import annotations

import numpy as np







def 							UpperCamelCase_     (       A__   :  np.ndarray   ):
  '''simple docstring'''





  lowerCAmelCase_,				lowerCAmelCase_     : List[str]     =  np.shape(A__   )
  if rows != columns:
    lowerCAmelCase_     : List[str]     =  (
        """'table' has to be of square shaped array but got a """
        f'{rows}x{columns} array:\n{table}'
    )
    raise ValueError(A__   )

  lowerCAmelCase_     : Optional[int]     =  np.zeros((rows, columns)   )
  lowerCAmelCase_     : Tuple     =  np.zeros((rows, columns)   )
  for i in range(A__   ):
    for j in range(A__   ):
      lowerCAmelCase_     : str     =  sum(lower[i][k] * upper[k][j] for k in range(A__   )   )
      if upper[j][j] == 0:
        raise ArithmeticError("""No LU decomposition exists"""   )
      lowerCAmelCase_     : Union[str, Any]     =  (table[i][j] - total) / upper[j][j]
    lowerCAmelCase_     : Dict     =  1
    for j in range(A__				,       A__   ):
      lowerCAmelCase_     : str     =  sum(lower[i][k] * upper[k][j] for k in range(A__   )   )
      lowerCAmelCase_     : List[str]     =  table[i][j] - total
  return lower, upper


if __name__ == "__main__":
  import doctest

  doctest.testmod()



