





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       (							__lowerCamelCase			):





       '''simple docstring'''







       def __init__(  self :    str							,				A :    Optional[Any]							,				A :    Tuple=13							,				A :    str=7							,				A :    Optional[int]=True							,				A :    int=True							,				A :    Dict=True							,				A :    int=True							,				A :    List[str]=True							,				A :    Tuple=False							,				A :    str=False							,				A :    Optional[Any]=False							,				A :    int=2							,				A :    List[str]=99							,				A :    Tuple=0							,				A :    Optional[Any]=32							,				A :    Optional[Any]=5							,				A :    List[Any]=4							,				A :    List[Any]=0.1							,				A :    List[Any]=0.1							,				A :    List[Any]=512							,				A :    int=12							,				A :    Union[str, Any]=2							,				A :    Any=0.02							,				A :    Union[str, Any]=3							,				A :    Dict=4							,				A :    Dict="last"							,				A :    Union[str, Any]=None							,				A :    Optional[Any]=None							,				):
         __snake_case:						List[str]   =						parent
         __snake_case:						Any   =						batch_size
         __snake_case:						Optional[Any]   =						seq_length
         __snake_case:						Dict   =						is_training
         __snake_case:						Any   =						use_input_lengths
         __snake_case:						Union[str, Any]   =						use_token_type_ids
         __snake_case:						List[Any]   =						use_labels
         __snake_case:						Optional[Any]   =						gelu_activation
         __snake_case:						int   =						sinusoidal_embeddings
         __snake_case:						int   =						causal
         __snake_case:						str   =						asm
         __snake_case:						List[str]   =						n_langs
         __snake_case:						Any   =						vocab_size
         __snake_case:						Tuple   =						n_special
         __snake_case:						Dict   =						hidden_size
         __snake_case:						Any   =						num_hidden_layers
         __snake_case:						str   =						num_attention_heads
         __snake_case:						Optional[int]   =						hidden_dropout_prob
         __snake_case:						List[str]   =						attention_probs_dropout_prob
         __snake_case:						int   =						max_position_embeddings
         __snake_case:						List[Any]   =						type_vocab_size
         __snake_case:						Tuple   =						type_sequence_label_size
         __snake_case:						Tuple   =						initializer_range
         __snake_case:						List[str]   =						num_labels
         __snake_case:						List[str]   =						num_choices
         __snake_case:						List[str]   =						summary_type
         __snake_case:						List[str]   =						use_proj
         __snake_case:						Optional[int]   =						scope




       def 							UpperCAmelCase__ (  self :    Dict		):
         __snake_case:						Optional[Any]   =						ids_tensor([self.batch_size, self.seq_length]							,				self.vocab_size		)
         __snake_case:						List[str]   =						random_attention_mask([self.batch_size, self.seq_length]		)

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

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

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

         __snake_case:						List[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 							UpperCAmelCase__ (  self :    int		):
         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 :    List[str]							,				A :    List[Any]							,				A :    Optional[int]							,				A :    Optional[Any]							,				A :    str							,				A :    Dict							,				A :    Optional[Any]							,				A :    Optional[int]							,				A :    Any							,				A :    int							,				):
         __snake_case:						Optional[Any]   =						FlaubertModel(config=A		)
         model.to(A		)
         model.eval()
         __snake_case:						Tuple   =						model(A							,				lengths=A							,				langs=A		)
         __snake_case:						Optional[Any]   =						model(A							,				langs=A		)
         __snake_case:						Dict   =						model(A		)
         self.parent.assertEqual(result.last_hidden_state.shape							,				(self.batch_size, self.seq_length, self.hidden_size)		)




       def 							UpperCAmelCase__ (  self :    List[Any]							,				A :    int							,				A :    Any							,				A :    Tuple							,				A :    List[str]							,				A :    Optional[int]							,				A :    Dict							,				A :    List[str]							,				A :    Any							,				A :    Optional[int]							,				):
         __snake_case:						Dict   =						FlaubertWithLMHeadModel(A		)
         model.to(A		)
         model.eval()

         __snake_case:						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 							UpperCAmelCase__ (  self :    List[str]							,				A :    int							,				A :    str							,				A :    List[str]							,				A :    Optional[Any]							,				A :    Dict							,				A :    Tuple							,				A :    List[str]							,				A :    Dict							,				A :    List[Any]							,				):
         __snake_case:						Union[str, Any]   =						FlaubertForQuestionAnsweringSimple(A		)
         model.to(A		)
         model.eval()

         __snake_case:						Tuple   =						model(A		)

         __snake_case:						str   =						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 							UpperCAmelCase__ (  self :    Dict							,				A :    str							,				A :    List[str]							,				A :    List[str]							,				A :    List[str]							,				A :    List[str]							,				A :    int							,				A :    Tuple							,				A :    str							,				A :    List[Any]							,				):
         __snake_case:						Union[str, Any]   =						FlaubertForQuestionAnswering(A		)
         model.to(A		)
         model.eval()

         __snake_case:						Optional[Any]   =						model(A		)

         __snake_case:						List[Any]   =						model(
             A							,				start_positions=A							,				end_positions=A							,				cls_index=A							,				is_impossible=A							,				p_mask=A							,				)

         __snake_case:						Optional[Any]   =						model(
             A							,				start_positions=A							,				end_positions=A							,				cls_index=A							,				is_impossible=A							,				)

         ((__snake_case)  ,   ):						Union[str, Any]   =						result_with_labels.to_tuple()

         __snake_case:						Any   =						model(A							,				start_positions=A							,				end_positions=A		)

         ((__snake_case)  ,   ):						Tuple   =						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 :    Tuple							,				A :    Dict							,				A :    Tuple							,				A :    Union[str, Any]							,				A :    Tuple							,				A :    Any							,				A :    Optional[Any]							,				A :    List[Any]							,				A :    List[Any]							,				A :    Dict							,				):
         __snake_case:						int   =						FlaubertForSequenceClassification(A		)
         model.to(A		)
         model.eval()

         __snake_case:						Any   =						model(A		)
         __snake_case:						str   =						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 							UpperCAmelCase__ (  self :    List[Any]							,				A :    Tuple							,				A :    List[str]							,				A :    Union[str, Any]							,				A :    str							,				A :    Optional[Any]							,				A :    List[Any]							,				A :    Dict							,				A :    Optional[Any]							,				A :    Optional[int]							,				):
         __snake_case:						Any   =						self.num_labels
         __snake_case:						Tuple   =						FlaubertForTokenClassification(A		)
         model.to(A		)
         model.eval()

         __snake_case:						str   =						model(A							,				attention_mask=A							,				labels=A		)
         self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.seq_length, self.num_labels)		)




       def 							UpperCAmelCase__ (  self :    Union[str, Any]							,				A :    int							,				A :    List[str]							,				A :    Optional[Any]							,				A :    Tuple							,				A :    Optional[int]							,				A :    Union[str, Any]							,				A :    Dict							,				A :    Tuple							,				A :    List[str]							,				):
         __snake_case:						Any   =						self.num_choices
         __snake_case:						Tuple   =						FlaubertForMultipleChoice(config=A		)
         model.to(A		)
         model.eval()
         __snake_case:						Dict   =						input_ids.unsqueeze(1		).expand(-1							,				self.num_choices							,				-1		).contiguous()
         __snake_case:						Dict   =						token_type_ids.unsqueeze(1		).expand(-1							,				self.num_choices							,				-1		).contiguous()
         __snake_case:						Any   =						input_mask.unsqueeze(1		).expand(-1							,				self.num_choices							,				-1		).contiguous()
         __snake_case:						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 							UpperCAmelCase__ (  self :    str		):
         __snake_case:						List[Any]   =						self.prepare_config_and_inputs()
         (
             (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   (
             __snake_case
         )  ,   
         ):						Union[str, Any]   =						config_and_inputs
         __snake_case:						Any   =						{
             """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       (							__lowerCamelCase   ,			__lowerCamelCase   ,			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 :    str							,				A :    Union[str, Any]							,				A :    List[str]							,				A :    List[Any]							,				A :    str							,				A :    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 							UpperCAmelCase__ (  self :    List[str]							,				A :    Optional[Any]							,				A :    int							,				A :    Any=False		):
         __snake_case:						List[Any]   =						super()._prepare_for_class(A							,				A							,				return_labels=A		)

         if return_labels:
           if model_class.__name__ == "FlaubertForQuestionAnswering":
             __snake_case:						Tuple   =						torch.zeros(
                 self.model_tester.batch_size							,				dtype=torch.long							,				device=A		)
             __snake_case:						str   =						torch.zeros(
                 self.model_tester.batch_size							,				dtype=torch.long							,				device=A		)

         return inputs_dict




       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						Optional[int]   =						FlaubertModelTester(self		)
         __snake_case:						Any   =						ConfigTester(self							,				config_class=A							,				emb_dim=37		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         self.config_tester.run_common_tests()




       def 							UpperCAmelCase__ (  self :    str		):
         __snake_case:						List[Any]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_model(*A		)




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						int   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_lm_head(*A		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						Union[str, Any]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_simple_qa(*A		)




       def 							UpperCAmelCase__ (  self :    str		):
         __snake_case:						Dict   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_qa(*A		)




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Any   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_sequence_classif(*A		)




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						int   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_token_classif(*A		)




       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						int   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_multiple_choice(*A		)




       @slow
       def 							UpperCAmelCase__ (  self :    List[str]		):
         for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __snake_case:						Any   =						FlaubertModel.from_pretrained(A		)
           self.assertIsNotNone(A		)






       @slow
       @require_torch_gpu
       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case  ,   __snake_case:						Dict   =						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

           __snake_case:						Any   =						True
           __snake_case:						str   =						model_class(config=A		)

           __snake_case:						List[str]   =						self._prepare_for_class(A							,				A		)
           __snake_case:						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"""		)		)
             __snake_case:						List[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     __snake_case       (							unittest.TestCase			):





       '''simple docstring'''







       @slow
       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						int   =						FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""		)
         __snake_case:						List[str]   =						torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
         with torch.no_grad():
           __snake_case:						int   =						model(A		)[0]
         __snake_case:						Union[str, Any]   =						torch.Size((1, 11, 768)		)
         self.assertEqual(output.shape							,				A		)
         __snake_case:						Optional[int]   =						torch.tensor(
             [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]		)

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













import warnings

from ...utils import logging
from .image_processing_perceiver import PerceiverImageProcessor


__UpperCAmelCase       :   Tuple				       =							logging.get_logger(__name__)







class     __snake_case       (							__lowerCamelCase			):





       '''simple docstring'''







       def __init__(  self :    Union[str, Any]							,				*A :    Union[str, Any]							,				**A :    Optional[int]		):
         warnings.warn(
             """The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers."""
             """ Please use PerceiverImageProcessor instead."""							,				A							,				)
         super().__init__(*A							,				**A		)






