





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



   '''simple docstring'''



   lowerCamelCase__:   Dict    						=parent
   lowerCamelCase__:   Dict    						=13
   lowerCamelCase__:   List[str]    						=7
   lowerCamelCase__:   int    						=True
   lowerCamelCase__:   Any    						=True
   lowerCamelCase__:   Tuple    						=True
   lowerCamelCase__:   str    						=True
   lowerCamelCase__:   int    						=True
   lowerCamelCase__:   List[str]    						=False
   lowerCamelCase__:   Union[str, Any]    						=False
   lowerCamelCase__:   Tuple    						=False
   lowerCamelCase__:   int    						=2
   lowerCamelCase__:   Any    						=99
   lowerCamelCase__:   Tuple    						=0
   lowerCamelCase__:   List[str]    						=32
   lowerCamelCase__:   Optional[Any]    						=2
   lowerCamelCase__:   Dict    						=4
   lowerCamelCase__:   List[str]    						=0.1
   lowerCamelCase__:   List[Any]    						=0.1
   lowerCamelCase__:   Any    						=512
   lowerCamelCase__:   List[str]    						=16
   lowerCamelCase__:   str    						=2
   lowerCamelCase__:   Optional[Any]    						=0.02
   lowerCamelCase__:   int    						=3
   lowerCamelCase__:   int    						=4
   lowerCamelCase__:   Dict    						="last"
   lowerCamelCase__:   Union[str, Any]    						=True
   lowerCamelCase__:   int    						=None
   lowerCamelCase__:   str    						=0

 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   int    						=ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size)
   lowerCamelCase__:   Optional[Any]    						=random_attention_mask([self.batch_size, self.seq_length] ,				dtype=tf.floataa)

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

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

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

   lowerCamelCase__:   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       :     Optional[int] ,				UpperCAmelCase_       :     Dict ,				UpperCAmelCase_       :     str ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Dict ,				UpperCAmelCase_       :     Optional[Any] ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Dict ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Tuple ,				) ->str:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=TFFlaubertModel(config=UpperCAmelCase_)
   lowerCamelCase__:   int    						={"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
   lowerCamelCase__:   str    						=model(UpperCAmelCase_)

   lowerCamelCase__:   Tuple    						=[input_ids, input_mask]
   lowerCamelCase__:   List[Any]    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size))

 def 							SCREAMING_SNAKE_CASE_      (self       :     Any ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     str ,				UpperCAmelCase_       :     List[Any] ,				UpperCAmelCase_       :     List[Any] ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Tuple ,				) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=TFFlaubertWithLMHeadModel(UpperCAmelCase_)

   lowerCamelCase__:   Tuple    						={"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
   lowerCamelCase__:   Tuple    						=model(UpperCAmelCase_)

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

 def 							SCREAMING_SNAKE_CASE_      (self       :     Tuple ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     Union[str, Any] ,				) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:   str    						=TFFlaubertForQuestionAnsweringSimple(UpperCAmelCase_)

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

   lowerCamelCase__:   Tuple    						=model(UpperCAmelCase_)

   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       :     Tuple ,				UpperCAmelCase_       :     Union[str, Any] ,				UpperCAmelCase_       :     Dict ,				UpperCAmelCase_       :     List[Any] ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     str ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Optional[Any] ,				) ->Any:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=TFFlaubertForSequenceClassification(UpperCAmelCase_)

   lowerCamelCase__:   Union[str, Any]    						={"input_ids": input_ids, "lengths": input_lengths}

   lowerCamelCase__:   List[str]    						=model(UpperCAmelCase_)

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

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int] ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     str ,				UpperCAmelCase_       :     List[Any] ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     Optional[int] ,				) ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=self.num_labels
   lowerCamelCase__:   Optional[Any]    						=TFFlaubertForTokenClassification(config=UpperCAmelCase_)
   lowerCamelCase__:   Union[str, Any]    						={"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
   lowerCamelCase__:   List[Any]    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels))

 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Optional[int] ,				UpperCAmelCase_       :     str ,				UpperCAmelCase_       :     Tuple ,				UpperCAmelCase_       :     Union[str, Any] ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     Any ,				) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[int]    						=self.num_choices
   lowerCamelCase__:   List[str]    						=TFFlaubertForMultipleChoice(config=UpperCAmelCase_)
   lowerCamelCase__:   str    						=tf.tile(tf.expand_dims(UpperCAmelCase_ ,				1) ,				(1, self.num_choices, 1))
   lowerCamelCase__:   Optional[int]    						=tf.tile(tf.expand_dims(UpperCAmelCase_ ,				1) ,				(1, self.num_choices, 1))
   lowerCamelCase__:   Optional[Any]    						=tf.tile(tf.expand_dims(UpperCAmelCase_ ,				1) ,				(1, self.num_choices, 1))
   lowerCamelCase__:   Optional[Any]    						={
       "input_ids": multiple_choice_inputs_ids,
       "attention_mask": multiple_choice_input_mask,
       "token_type_ids": multiple_choice_token_type_ids,
   }
   lowerCamelCase__:   Any    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_choices))







 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=self.prepare_config_and_inputs()
   (
       (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,      (
       lowerCamelCase__      
   )						,            
   ):   Any    						=config_and_inputs
   lowerCamelCase__:   Union[str, Any]    						={
       "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		(			__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 							SCREAMING_SNAKE_CASE_      (self       :     Dict ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Dict ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     List[str]) ->Dict:



   '''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 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=TFFlaubertModelTester(self)
   lowerCamelCase__:   Dict    						=ConfigTester(self ,				config_class=UpperCAmelCase_ ,				emb_dim=37)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->int:



   '''simple docstring'''



   self.config_tester.run_common_tests()

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->List[str]:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_model(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:   Dict    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_lm_head(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->int:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_qa(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->int:



   '''simple docstring'''



   lowerCamelCase__:   Any    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[int]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_for_token_classification(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_for_multiple_choice(*UpperCAmelCase_)







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



   '''simple docstring'''



   for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowerCamelCase__:   Optional[int]    						=TFFlaubertModel.from_pretrained(UpperCAmelCase_)
     self.assertIsNotNone(UpperCAmelCase_)



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







 '''simple docstring'''

 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->str:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=TFFlaubertModel.from_pretrained("jplu/tf-flaubert-small-cased")

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

   lowerCamelCase__:   Union[str, Any]    						=model(UpperCAmelCase_)[0]
   lowerCamelCase__:   Union[str, Any]    						=tf.TensorShape((1, 8, 512))
   self.assertEqual(output.shape ,				UpperCAmelCase_)
   # compare the actual values for a slice.
   lowerCamelCase__:   List[str]    						=tf.convert_to_tensor(
       [
           [
               [-1.876_8773, -1.56_6555, 0.2707_2418],
               [-1.692_0038, -0.587_3505, 1.932_9599],
               [-2.956_3985, -1.699_3835, 1.797_2052],
           ]
       ] ,				dtype=tf.floataa ,				)

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








from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__A             =       logging.get_logger(__name__)

__A             =       {
    "facebook/xlm-roberta-xl": "https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json",
    "facebook/xlm-roberta-xxl": "https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json",
    # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl
}



class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''
 lowercase_	=    "xlm-roberta-xl"

 def __init__(self       :     Tuple ,				UpperCAmelCase_       :     Union[str, Any]=250_880 ,				UpperCAmelCase_       :     Optional[int]=2_560 ,				UpperCAmelCase_       :     Union[str, Any]=36 ,				UpperCAmelCase_       :     str=32 ,				UpperCAmelCase_       :     Dict=10_240 ,				UpperCAmelCase_       :     Optional[int]="gelu" ,				UpperCAmelCase_       :     Tuple=0.1 ,				UpperCAmelCase_       :     List[Any]=0.1 ,				UpperCAmelCase_       :     List[Any]=514 ,				UpperCAmelCase_       :     List[Any]=1 ,				UpperCAmelCase_       :     Dict=0.02 ,				UpperCAmelCase_       :     Dict=1E-0_5 ,				UpperCAmelCase_       :     int=1 ,				UpperCAmelCase_       :     Dict=0 ,				UpperCAmelCase_       :     Optional[Any]=2 ,				UpperCAmelCase_       :     Union[str, Any]="absolute" ,				UpperCAmelCase_       :     str=True ,				UpperCAmelCase_       :     str=None ,				**UpperCAmelCase_       :     List[str] ,				) ->int:



   '''simple docstring'''



   super().__init__(pad_token_id=UpperCAmelCase_ ,				bos_token_id=UpperCAmelCase_ ,				eos_token_id=UpperCAmelCase_ ,				**UpperCAmelCase_)
   lowerCamelCase__:   List[str]    						=vocab_size
   lowerCamelCase__:   List[Any]    						=hidden_size
   lowerCamelCase__:   Any    						=num_hidden_layers
   lowerCamelCase__:   Optional[Any]    						=num_attention_heads
   lowerCamelCase__:   Dict    						=hidden_act
   lowerCamelCase__:   str    						=intermediate_size
   lowerCamelCase__:   List[Any]    						=hidden_dropout_prob
   lowerCamelCase__:   List[str]    						=attention_probs_dropout_prob
   lowerCamelCase__:   Union[str, Any]    						=max_position_embeddings
   lowerCamelCase__:   Optional[int]    						=type_vocab_size
   lowerCamelCase__:   Tuple    						=initializer_range
   lowerCamelCase__:   Tuple    						=layer_norm_eps
   lowerCamelCase__:   Dict    						=position_embedding_type
   lowerCamelCase__:   Optional[int]    						=use_cache
   lowerCamelCase__:   List[str]    						=classifier_dropout



class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''

 @property
 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->Mapping[str, Mapping[int, str]]:



   '''simple docstring'''



   if self.task == "multiple-choice":
     lowerCamelCase__:   Union[str, Any]    						={0: "batch", 1: "choice", 2: "sequence"}
   else:
     lowerCamelCase__:   Optional[int]    						={0: "batch", 1: "sequence"}
   return OrderedDict(
       [
           ("input_ids", dynamic_axis),
           ("attention_mask", dynamic_axis),
       ])

