


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 					lowerCAmelCase_ (       _UpperCAmelCase):




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






         """simple docstring"""






         a__ :List[str]						  =    parent
         a__ :Any						  =    batch_size
         a__ :Union[str, Any]						  =    seq_length
         a__ :str						  =    is_training
         a__ :Optional[Any]						  =    use_input_lengths
         a__ :str						  =    use_token_type_ids
         a__ :List[Any]						  =    use_labels
         a__ :Any						  =    gelu_activation
         a__ :Optional[Any]						  =    sinusoidal_embeddings
         a__ :Dict						  =    causal
         a__ :List[Any]						  =    asm
         a__ :Optional[Any]						  =    n_langs
         a__ :str						  =    vocab_size
         a__ :List[Any]						  =    n_special
         a__ :Union[str, Any]						  =    hidden_size
         a__ :Tuple						  =    num_hidden_layers
         a__ :str						  =    num_attention_heads
         a__ :List[str]						  =    hidden_dropout_prob
         a__ :int						  =    attention_probs_dropout_prob
         a__ :int						  =    max_position_embeddings
         a__ :str						  =    type_vocab_size
         a__ :List[Any]						  =    type_sequence_label_size
         a__ :Any						  =    initializer_range
         a__ :int						  =    num_labels
         a__ :List[Any]						  =    num_choices
         a__ :List[Any]						  =    summary_type
         a__ :List[str]						  =    use_proj
         a__ :List[str]						  =    scope




      def  _snake_case					(	self						:   str						)      ->Tuple:






         """simple docstring"""






         a__ :Optional[Any]						  =    ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size						)
         a__ :Tuple						  =    random_attention_mask([self.batch_size, self.seq_length]						)

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

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

         a__ :Dict						  =    None
         a__ :Union[str, Any]						  =    None
         a__ :Optional[int]						  =    None
         if self.use_labels:
            a__ :Optional[int]						  =    ids_tensor([self.batch_size]   ,     self.type_sequence_label_size						)
            a__ :Any						  =    ids_tensor([self.batch_size, self.seq_length]   ,     self.num_labels						)
            a__ :Optional[Any]						  =    ids_tensor([self.batch_size]   ,     2						).float()
            a__ :Dict						  =    ids_tensor([self.batch_size]   ,     self.num_choices						)

         a__ :Union[str, Any]						  =    self.get_config()

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




      def  _snake_case					(	self						:   Union[str, Any]						)      ->Optional[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  _snake_case					(	self						:   Optional[Any]   ,     __A						:   Tuple   ,     __A						:   int   ,     __A						:   Tuple   ,     __A						:   Optional[int]   ,     __A						:   Any   ,     __A						:   Dict   ,     __A						:   Union[str, Any]   ,     __A						:   Union[str, Any]   ,     __A						:   Tuple   ,     )      ->int:






         """simple docstring"""






         a__ :List[Any]						  =    FlaubertModel(config=A_						)
         model.to(A_						)
         model.eval()
         a__ :Tuple						  =    model(A_   ,     lengths=A_   ,     langs=A_						)
         a__ :List[str]						  =    model(A_   ,     langs=A_						)
         a__ :List[str]						  =    model(A_						)
         self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size)						)




      def  _snake_case					(	self						:   Dict   ,     __A						:   Optional[Any]   ,     __A						:   Optional[Any]   ,     __A						:   Optional[Any]   ,     __A						:   Dict   ,     __A						:   Optional[Any]   ,     __A						:   Optional[int]   ,     __A						:   Tuple   ,     __A						:   List[Any]   ,     __A						:   Dict   ,     )      ->List[Any]:






         """simple docstring"""






         a__ :int						  =    FlaubertWithLMHeadModel(A_						)
         model.to(A_						)
         model.eval()

         a__ :Optional[int]						  =    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  _snake_case					(	self						:   Optional[Any]   ,     __A						:   Optional[Any]   ,     __A						:   Union[str, Any]   ,     __A						:   int   ,     __A						:   Optional[Any]   ,     __A						:   List[str]   ,     __A						:   List[str]   ,     __A						:   Optional[Any]   ,     __A						:   Optional[int]   ,     __A						:   List[str]   ,     )      ->Optional[Any]:






         """simple docstring"""






         a__ :Union[str, Any]						  =    FlaubertForQuestionAnsweringSimple(A_						)
         model.to(A_						)
         model.eval()

         a__ :int						  =    model(A_						)

         a__ :Tuple						  =    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  _snake_case					(	self						:   Dict   ,     __A						:   int   ,     __A						:   Optional[int]   ,     __A						:   Optional[int]   ,     __A						:   Optional[Any]   ,     __A						:   Optional[int]   ,     __A						:   Union[str, Any]   ,     __A						:   Optional[int]   ,     __A						:   Any   ,     __A						:   Union[str, Any]   ,     )      ->Tuple:






         """simple docstring"""






         a__ :Tuple						  =    FlaubertForQuestionAnswering(A_						)
         model.to(A_						)
         model.eval()

         a__ :List[Any]						  =    model(A_						)

         a__ :Dict						  =    model(
             A_   ,     start_positions=A_   ,     end_positions=A_   ,     cls_index=A_   ,     is_impossible=A_   ,     p_mask=A_   ,     )

         a__ :List[str]						  =    model(
             A_   ,     start_positions=A_   ,     end_positions=A_   ,     cls_index=A_   ,     is_impossible=A_   ,     )

         ((a__	)  ,  	) :List[str]						  =    result_with_labels.to_tuple()

         a__ :int						  =    model(A_   ,     start_positions=A_   ,     end_positions=A_						)

         ((a__	)  ,  	) :Optional[Any]						  =    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  _snake_case					(	self						:   List[str]   ,     __A						:   Dict   ,     __A						:   List[Any]   ,     __A						:   List[Any]   ,     __A						:   Any   ,     __A						:   Optional[Any]   ,     __A						:   Dict   ,     __A						:   Dict   ,     __A						:   int   ,     __A						:   Union[str, Any]   ,     )      ->Union[str, Any]:






         """simple docstring"""






         a__ :Tuple						  =    FlaubertForSequenceClassification(A_						)
         model.to(A_						)
         model.eval()

         a__ :List[Any]						  =    model(A_						)
         a__ :List[Any]						  =    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  _snake_case					(	self						:   List[Any]   ,     __A						:   Optional[Any]   ,     __A						:   str   ,     __A						:   int   ,     __A						:   Dict   ,     __A						:   str   ,     __A						:   Optional[int]   ,     __A						:   int   ,     __A						:   List[Any]   ,     __A						:   List[str]   ,     )      ->Dict:






         """simple docstring"""






         a__ :Union[str, Any]						  =    self.num_labels
         a__ :Optional[Any]						  =    FlaubertForTokenClassification(A_						)
         model.to(A_						)
         model.eval()

         a__ :Optional[Any]						  =    model(A_   ,     attention_mask=A_   ,     labels=A_						)
         self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.num_labels)						)




      def  _snake_case					(	self						:   Optional[int]   ,     __A						:   List[str]   ,     __A						:   Tuple   ,     __A						:   Optional[Any]   ,     __A						:   int   ,     __A						:   str   ,     __A						:   Tuple   ,     __A						:   Union[str, Any]   ,     __A						:   Optional[Any]   ,     __A						:   List[Any]   ,     )      ->Any:






         """simple docstring"""






         a__ :str						  =    self.num_choices
         a__ :Optional[Any]						  =    FlaubertForMultipleChoice(config=A_						)
         model.to(A_						)
         model.eval()
         a__ :Optional[Any]						  =    input_ids.unsqueeze(1						).expand(-1   ,     self.num_choices   ,     -1						).contiguous()
         a__ :Optional[Any]						  =    token_type_ids.unsqueeze(1						).expand(-1   ,     self.num_choices   ,     -1						).contiguous()
         a__ :Any						  =    input_mask.unsqueeze(1						).expand(-1   ,     self.num_choices   ,     -1						).contiguous()
         a__ :List[Any]						  =    model(
             A_   ,     attention_mask=A_   ,     token_type_ids=A_   ,     labels=A_   ,     )
         self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_choices)						)






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






         """simple docstring"""






         a__ :Optional[int]						  =    self.prepare_config_and_inputs()
         (
             (
             a__	
         )  ,  (
             a__	
         )  ,  (
             a__	
         )  ,  (
             a__	
         )  ,  (
             a__	
         )  ,  (
             a__	
         )  ,  (
             a__	
         )  ,  (
             a__	
         )  ,  (
             a__	
         )  ,  	
         ) :Any						  =    config_and_inputs
         a__ :List[str]						  =    {
             "input_ids": input_ids,
             "token_type_ids": token_type_ids,
             "lengths": input_lengths,
             "attention_mask": input_mask,
         }
         return config, inputs_dict






@require_torch
class 					lowerCAmelCase_ (       _UpperCAmelCase			,_UpperCAmelCase			,unittest.TestCase):
      lowerCamelCase_         =     (
          (
              FlaubertModel,
              FlaubertWithLMHeadModel,
              FlaubertForQuestionAnswering,
              FlaubertForQuestionAnsweringSimple,
              FlaubertForSequenceClassification,
              FlaubertForTokenClassification,
              FlaubertForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      lowerCamelCase_         =     (
          {
              '''feature-extraction''': FlaubertModel,
              '''fill-mask''': FlaubertWithLMHeadModel,
              '''question-answering''': FlaubertForQuestionAnsweringSimple,
              '''text-classification''': FlaubertForSequenceClassification,
              '''token-classification''': FlaubertForTokenClassification,
              '''zero-shot''': FlaubertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )




      def  _snake_case					(	self						:   Optional[int]   ,     __A						:   Optional[Any]   ,     __A						:   Any   ,     __A						:   List[Any]   ,     __A						:   str   ,     __A						:   List[str]						)      ->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  _snake_case					(	self						:   str   ,     __A						:   List[str]   ,     __A						:   Any   ,     __A						:   Optional[int]=False						)      ->Optional[Any]:






         """simple docstring"""






         a__ :Any						  =    super()._prepare_for_class(A_   ,     A_   ,     return_labels=A_						)

         if return_labels:
            if model_class.__name__ == "FlaubertForQuestionAnswering":
               a__ :Union[str, Any]						  =    torch.zeros(
                   self.model_tester.batch_size   ,     dtype=torch.long   ,     device=A_						)
               a__ :Union[str, Any]						  =    torch.zeros(
                   self.model_tester.batch_size   ,     dtype=torch.long   ,     device=A_						)

         return inputs_dict




      def  _snake_case					(	self						:   str						)      ->Optional[int]:






         """simple docstring"""






         a__ :Dict						  =    FlaubertModelTester(self						)
         a__ :Any						  =    ConfigTester(self   ,     config_class=A_   ,     emb_dim=37						)




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






         """simple docstring"""






         self.config_tester.run_common_tests()




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






         """simple docstring"""






         a__ :Tuple						  =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_model(*A_						)




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






         """simple docstring"""






         a__ :Optional[Any]						  =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_lm_head(*A_						)




      def  _snake_case					(	self						:   str						)      ->int:






         """simple docstring"""






         a__ :Union[str, Any]						  =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_simple_qa(*A_						)




      def  _snake_case					(	self						:   int						)      ->str:






         """simple docstring"""






         a__ :str						  =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_qa(*A_						)




      def  _snake_case					(	self						:   Any						)      ->Any:






         """simple docstring"""






         a__ :List[str]						  =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_sequence_classif(*A_						)




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






         """simple docstring"""






         a__ :int						  =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_token_classif(*A_						)




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






         """simple docstring"""






         a__ :Tuple						  =    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_multiple_choice(*A_						)




      @slow
      def  _snake_case					(	self						:   Any						)      ->Tuple:






         """simple docstring"""






         for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            a__ :Dict						  =    FlaubertModel.from_pretrained(A_						)
            self.assertIsNotNone(A_						)






      @slow
      @require_torch_gpu
      def  _snake_case					(	self						:   int						)      ->Any:






         """simple docstring"""






         a__  ,  a__ :List[Any]						  =    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__ :List[Any]						  =    True
            a__ :int						  =    model_class(config=A_						)

            a__ :Union[str, Any]						  =    self._prepare_for_class(A_   ,     A_						)
            a__ :Union[str, Any]						  =    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__ :Union[str, Any]						  =    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 					lowerCAmelCase_ (       unittest.TestCase):




      @slow
      def  _snake_case					(	self						:   List[Any]						)      ->Optional[Any]:






         """simple docstring"""






         a__ :str						  =    FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"						)
         a__ :List[Any]						  =    torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]						)
         with torch.no_grad():
            a__ :Tuple						  =    model(A_						)[0]
         a__ :Tuple						  =    torch.Size((1, 11, 768)						)
         self.assertEqual(output.shape   ,     A_						)
         a__ :Union[str, Any]						  =    torch.tensor(
             [[[-2.6_251, -1.4_298, -0.0_227], [-2.8_510, -1.6_387, 0.2_258], [-2.8_114, -1.1_832, -0.3_066]]]						)

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






def 				lowerCamelCase__	(      a	:   int	,  a	:   int				)    ->	Any:





			"""simple docstring"""
			# "extended trapezoidal rule"
			# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
			a__ :Optional[int]						  =    (boundary[1] - boundary[0]) / steps
			a__ :str						  =    boundary[0]
			a__ :Optional[int]						  =    boundary[1]
			a__ :str						  =    make_points(a	,  a	,  a				)
			a__ :Any						  =    0.0
			y += (h / 2.0) * f(a				)
			for i in x_i:
						# print(i)
						y += h * f(a				)
			y += (h / 2.0) * f(a				)
			return y


def 				lowerCamelCase__	(      a	:   Any	,  a	:   str	,  a	:   Dict				)    ->	int:





			"""simple docstring"""
			a__ :Union[str, Any]						  =    a + h
			while x < (b - h):
						yield x
						a__ :str						  =    x + h


def 				lowerCamelCase__	(      a	:   Optional[int]				)    ->	List[Any]:  # enter your function here





			"""simple docstring"""
			a__ :Tuple						  =    (x - 0) * (x - 0)
			return y


def 				lowerCamelCase__	(      )    ->	List[str]:





			"""simple docstring"""
			a__ :Optional[Any]						  =    0.0  # Lower bound of integration
			a__ :Optional[Any]						  =    1.0  # Upper bound of integration
			a__ :List[str]						  =    1_0.0  # define number of steps or resolution
			a__ :int						  =    [a, b]  # define boundary of integration
			a__ :str						  =    method_a(a	,  a				)
			print(F'''y = {y}'''				)


if __name__ == "__main__":
		main()


