





"""simple docstring"""



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 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 def __init__(   self ,						__lowerCamelCase ,						__lowerCamelCase=13 ,						__lowerCamelCase=7 ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=False ,						__lowerCamelCase=False ,						__lowerCamelCase=False ,						__lowerCamelCase=2 ,						__lowerCamelCase=99 ,						__lowerCamelCase=0 ,						__lowerCamelCase=32 ,						__lowerCamelCase=5 ,						__lowerCamelCase=4 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=512 ,						__lowerCamelCase=12 ,						__lowerCamelCase=2 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=3 ,						__lowerCamelCase=4 ,						__lowerCamelCase="last" ,						__lowerCamelCase=None ,						__lowerCamelCase=None ,						):



     '''simple docstring'''




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

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




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

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

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

     __A   :						Any									=					None
     __A   :						Optional[Any]									=					None
     __A   :						Union[str, Any]									=					None
     if self.use_labels:
         __A   :						str									=					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[int]									=					ids_tensor([self.batch_size] ,						2  ).float()
         __A   :						List[Any]									=					ids_tensor([self.batch_size] ,						self.num_choices  )

     __A   :						str									=					self.get_config()

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

 def  UpperCamelCase__(   self  ):



     '''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  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					FlaubertModel(config=__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A   :						Optional[Any]									=					model(__lowerCamelCase ,						lengths=__lowerCamelCase ,						langs=__lowerCamelCase  )
     __A   :						Union[str, Any]									=					model(__lowerCamelCase ,						langs=__lowerCamelCase  )
     __A   :						Optional[int]									=					model(__lowerCamelCase  )
     self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						Any									=					FlaubertWithLMHeadModel(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()

     __A   :						Any									=					model(__lowerCamelCase ,						token_type_ids=__lowerCamelCase ,						labels=__lowerCamelCase  )
     self.parent.assertEqual(result.loss.shape ,						()  )
     self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.vocab_size)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						List[str]									=					FlaubertForQuestionAnsweringSimple(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()

     __A   :						Optional[int]									=					model(__lowerCamelCase  )

     __A   :						List[Any]									=					model(__lowerCamelCase ,						start_positions=__lowerCamelCase ,						end_positions=__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  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						List[str]									=					FlaubertForQuestionAnswering(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()

     __A   :						List[str]									=					model(__lowerCamelCase  )

     __A   :						Optional[int]									=					model(
         __lowerCamelCase ,						start_positions=__lowerCamelCase ,						end_positions=__lowerCamelCase ,						cls_index=__lowerCamelCase ,						is_impossible=__lowerCamelCase ,						p_mask=__lowerCamelCase ,						)

     __A   :						int									=					model(
         __lowerCamelCase ,						start_positions=__lowerCamelCase ,						end_positions=__lowerCamelCase ,						cls_index=__lowerCamelCase ,						is_impossible=__lowerCamelCase ,						)

     ((__A)			,	)   :						Optional[int]									=					result_with_labels.to_tuple()

     __A   :						Tuple									=					model(__lowerCamelCase ,						start_positions=__lowerCamelCase ,						end_positions=__lowerCamelCase  )

     ((__A)			,	)   :						List[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  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						int									=					FlaubertForSequenceClassification(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()

     __A   :						List[str]									=					model(__lowerCamelCase  )
     __A   :						Optional[int]									=					model(__lowerCamelCase ,						labels=__lowerCamelCase  )

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

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						List[Any]									=					self.num_labels
     __A   :						Union[str, Any]									=					FlaubertForTokenClassification(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()

     __A   :						Optional[int]									=					model(__lowerCamelCase ,						attention_mask=__lowerCamelCase ,						labels=__lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.num_labels)  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						):



     '''simple docstring'''




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







 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




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






@require_torch
class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase					):





 """simple docstring"""


 _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  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase  ):



     '''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  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase=False  ):



     '''simple docstring'''




     __A   :						List[Any]									=					super()._prepare_for_class(__lowerCamelCase ,						__lowerCamelCase ,						return_labels=__lowerCamelCase  )

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

     return inputs_dict

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[Any]									=					FlaubertModelTester(self  )
     __A   :						Dict									=					ConfigTester(self ,						config_class=__lowerCamelCase ,						emb_dim=37  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Tuple									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_model(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_lm_head(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[str]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[Any]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_qa(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						str									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[Any]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_token_classif(*__lowerCamelCase  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						List[str]									=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCamelCase  )

 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A   :						Optional[int]									=					FlaubertModel.from_pretrained(__lowerCamelCase  )
         self.assertIsNotNone(__lowerCamelCase  )







 @slow
 @require_torch_gpu
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A			,	__A   :						str									=					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   :						str									=					True
         __A   :						Dict									=					model_class(config=__lowerCamelCase  )

         __A   :						Optional[Any]									=					self._prepare_for_class(__lowerCamelCase ,						__lowerCamelCase  )
         __A   :						str									=					torch.jit.trace(
             __lowerCamelCase ,						(inputs_dict['''input_ids'''].to('''cpu'''  ), inputs_dict['''attention_mask'''].to('''cpu'''  ))  )

         with tempfile.TemporaryDirectory() as tmp:
             torch.jit.save(__lowerCamelCase ,						os.path.join(__lowerCamelCase ,						'''traced_model.pt'''  )  )
             __A   :						Optional[int]									=					torch.jit.load(os.path.join(__lowerCamelCase ,						'''traced_model.pt'''  ) ,						map_location=__lowerCamelCase  )
             loaded(inputs_dict['''input_ids'''].to(__lowerCamelCase  ) ,						inputs_dict['''attention_mask'''].to(__lowerCamelCase  )  )






@require_torch
class 		__snake_case (		unittest.TestCase					):





 """simple docstring"""


 @slow
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Dict									=					FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''  )
     __A   :						Optional[int]									=					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]  )
     with torch.no_grad():
         __A   :						Any									=					model(__lowerCamelCase  )[0]
     __A   :						Union[str, Any]									=					torch.Size((1, 11, 768)  )
     self.assertEqual(output.shape ,						__lowerCamelCase  )
     __A   :						Optional[int]									=					torch.tensor(
         [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]  )

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











"""simple docstring"""



from typing import TYPE_CHECKING

from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


a_			=      {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]}

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_			=      ["""GPTNeoXTokenizerFast"""]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_			=      [
           """GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """GPTNeoXForCausalLM""",
           """GPTNeoXForQuestionAnswering""",
           """GPTNeoXForSequenceClassification""",
           """GPTNeoXForTokenClassification""",
           """GPTNeoXLayer""",
           """GPTNeoXModel""",
           """GPTNeoXPreTrainedModel""",
       ]


if TYPE_CHECKING:
       from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_gpt_neox import (
                  GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST,
                  GPTNeoXForCausalLM,
                  GPTNeoXForQuestionAnswering,
                  GPTNeoXForSequenceClassification,
                  GPTNeoXForTokenClassification,
                  GPTNeoXLayer,
                  GPTNeoXModel,
                  GPTNeoXPreTrainedModel,
              )


else:
       import sys

       a_			=      _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




