

"""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 lowerCamelCase__   (				SCREAMING_SNAKE_CASE    ):



    '''simple docstring'''




    def __init__( self ,lowerCamelCase_ ,lowerCamelCase_=1_3 ,lowerCamelCase_=7 ,lowerCamelCase_=True ,lowerCamelCase_=True ,lowerCamelCase_=True ,lowerCamelCase_=True ,lowerCamelCase_=True ,lowerCamelCase_=False ,lowerCamelCase_=False ,lowerCamelCase_=False ,lowerCamelCase_=2 ,lowerCamelCase_=9_9 ,lowerCamelCase_=0 ,lowerCamelCase_=3_2 ,lowerCamelCase_=5 ,lowerCamelCase_=4 ,lowerCamelCase_=0.1 ,lowerCamelCase_=0.1 ,lowerCamelCase_=5_1_2 ,lowerCamelCase_=1_2 ,lowerCamelCase_=2 ,lowerCamelCase_=0.02 ,lowerCamelCase_=3 ,lowerCamelCase_=4 ,lowerCamelCase_="last" ,lowerCamelCase_=None ,lowerCamelCase_=None ,)    ->      List[str]:
          A				  =		parent
          A				  =		batch_size
          A				  =		seq_length
          A				  =		is_training
          A				  =		use_input_lengths
          A				  =		use_token_type_ids
          A				  =		use_labels
          A				  =		gelu_activation
          A				  =		sinusoidal_embeddings
          A				  =		causal
          A				  =		asm
          A				  =		n_langs
          A				  =		vocab_size
          A				  =		n_special
          A				  =		hidden_size
          A				  =		num_hidden_layers
          A				  =		num_attention_heads
          A				  =		hidden_dropout_prob
          A				  =		attention_probs_dropout_prob
          A				  =		max_position_embeddings
          A				  =		type_vocab_size
          A				  =		type_sequence_label_size
          A				  =		initializer_range
          A				  =		num_labels
          A				  =		num_choices
          A				  =		summary_type
          A				  =		use_proj
          A				  =		scope




    def UpperCamelCase__		( self    )    ->      Any:
          A				  =		ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size    )
          A				  =		random_attention_mask([self.batch_size, self.seq_length]    )

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

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

          A				  =		None
          A				  =		None
          A				  =		None
          if self.use_labels:
                A				  =		ids_tensor([self.batch_size] ,self.type_sequence_label_size    )
                A				  =		ids_tensor([self.batch_size, self.seq_length] ,self.num_labels    )
                A				  =		ids_tensor([self.batch_size] ,2    ).float()
                A				  =		ids_tensor([self.batch_size] ,self.num_choices    )

          A				  =		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    )    ->      Optional[Any]:
          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_ ,)    ->      List[Any]:
          A				  =		FlaubertModel(config=lowerCamelCase_    )
          model.to(lowerCamelCase_    )
          model.eval()
          A				  =		model(lowerCamelCase_ ,lengths=lowerCamelCase_ ,langs=lowerCamelCase_    )
          A				  =		model(lowerCamelCase_ ,langs=lowerCamelCase_    )
          A				  =		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_ ,)    ->      Optional[int]:
          A				  =		FlaubertWithLMHeadModel(lowerCamelCase_    )
          model.to(lowerCamelCase_    )
          model.eval()

          A				  =		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_ ,)    ->      Union[str, Any]:
          A				  =		FlaubertForQuestionAnsweringSimple(lowerCamelCase_    )
          model.to(lowerCamelCase_    )
          model.eval()

          A				  =		model(lowerCamelCase_    )

          A				  =		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_ ,)    ->      str:
          A				  =		FlaubertForQuestionAnswering(lowerCamelCase_    )
          model.to(lowerCamelCase_    )
          model.eval()

          A				  =		model(lowerCamelCase_    )

          A				  =		model(
              lowerCamelCase_ ,start_positions=lowerCamelCase_ ,end_positions=lowerCamelCase_ ,cls_index=lowerCamelCase_ ,is_impossible=lowerCamelCase_ ,p_mask=lowerCamelCase_ ,)

          A				  =		model(
              lowerCamelCase_ ,start_positions=lowerCamelCase_ ,end_positions=lowerCamelCase_ ,cls_index=lowerCamelCase_ ,is_impossible=lowerCamelCase_ ,)

          ((A)  ,			)				  =		result_with_labels.to_tuple()

          A				  =		model(lowerCamelCase_ ,start_positions=lowerCamelCase_ ,end_positions=lowerCamelCase_    )

          ((A)  ,			)				  =		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_ ,)    ->      Union[str, Any]:
          A				  =		FlaubertForSequenceClassification(lowerCamelCase_    )
          model.to(lowerCamelCase_    )
          model.eval()

          A				  =		model(lowerCamelCase_    )
          A				  =		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_ ,)    ->      Dict:
          A				  =		self.num_labels
          A				  =		FlaubertForTokenClassification(lowerCamelCase_    )
          model.to(lowerCamelCase_    )
          model.eval()

          A				  =		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_ ,)    ->      Optional[int]:
          A				  =		self.num_choices
          A				  =		FlaubertForMultipleChoice(config=lowerCamelCase_    )
          model.to(lowerCamelCase_    )
          model.eval()
          A				  =		input_ids.unsqueeze(1    ).expand(-1 ,self.num_choices ,-1    ).contiguous()
          A				  =		token_type_ids.unsqueeze(1    ).expand(-1 ,self.num_choices ,-1    ).contiguous()
          A				  =		input_mask.unsqueeze(1    ).expand(-1 ,self.num_choices ,-1    ).contiguous()
          A				  =		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    )    ->      Any:
          A				  =		self.prepare_config_and_inputs()
          (
              (
              A
          )  ,			(
              A
          )  ,			(
              A
          )  ,			(
              A
          )  ,			(
              A
          )  ,			(
              A
          )  ,			(
              A
          )  ,			(
              A
          )  ,			(
              A
          )  ,			
          )				  =		config_and_inputs
          A				  =		{
              """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__   (				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_    )    ->      List[str]:
          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    )    ->      int:
          A				  =		super()._prepare_for_class(lowerCamelCase_ ,lowerCamelCase_ ,return_labels=lowerCamelCase_    )

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

          return inputs_dict




    def UpperCamelCase__		( self    )    ->      List[Any]:
          A				  =		FlaubertModelTester(self    )
          A				  =		ConfigTester(self ,config_class=lowerCamelCase_ ,emb_dim=3_7    )




    def UpperCamelCase__		( self    )    ->      Optional[int]:
          self.config_tester.run_common_tests()




    def UpperCamelCase__		( self    )    ->      List[Any]:
          A				  =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_model(*lowerCamelCase_    )




    def UpperCamelCase__		( self    )    ->      Optional[Any]:
          A				  =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase_    )




    def UpperCamelCase__		( self    )    ->      List[Any]:
          A				  =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_simple_qa(*lowerCamelCase_    )




    def UpperCamelCase__		( self    )    ->      List[Any]:
          A				  =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase_    )




    def UpperCamelCase__		( self    )    ->      Dict:
          A				  =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase_    )




    def UpperCamelCase__		( self    )    ->      List[str]:
          A				  =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_token_classif(*lowerCamelCase_    )




    def UpperCamelCase__		( self    )    ->      int:
          A				  =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_multiple_choice(*lowerCamelCase_    )




    @slow
    def UpperCamelCase__		( self    )    ->      str:
          for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                A				  =		FlaubertModel.from_pretrained(lowerCamelCase_    )
                self.assertIsNotNone(lowerCamelCase_    )




    @slow
    @require_torch_gpu
    def UpperCamelCase__		( self    )    ->      int:
          A  ,			A				  =		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				  =		True
                A				  =		model_class(config=lowerCamelCase_    )

                A				  =		self._prepare_for_class(lowerCamelCase_ ,lowerCamelCase_    )
                A				  =		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				  =		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 lowerCamelCase__   (				unittest.TestCase    ):



    '''simple docstring'''




    @slow
    def UpperCamelCase__		( self    )    ->      Dict:
          A				  =		FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""    )
          A				  =		torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]    )
          with torch.no_grad():
                A				  =		model(lowerCamelCase_    )[0]
          A				  =		torch.Size((1, 1_1, 7_6_8)    )
          self.assertEqual(output.shape ,lowerCamelCase_    )
          A				  =		torch.tensor(
              [[[-2.62_51, -1.42_98, -0.02_27], [-2.85_10, -1.63_87, 0.22_58], [-2.81_14, -1.18_32, -0.30_66]]]    )

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







"""simple docstring"""


from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
  from PIL import Image

  from ..image_utils import load_image

if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

UpperCAmelCase													=logging.get_logger(__name__)





@add_end_docstrings(SCREAMING_SNAKE_CASE    )
class lowerCamelCase__   (				SCREAMING_SNAKE_CASE    ):



    '''simple docstring'''




    def __init__( self ,*lowerCamelCase_ ,**lowerCamelCase_    )    ->      str:
          super().__init__(*lowerCamelCase_ ,**lowerCamelCase_    )
          self.check_model_type(lowerCamelCase_    )




    def UpperCamelCase__		( self ,lowerCamelCase_=None ,lowerCamelCase_=None ,lowerCamelCase_=None ,**lowerCamelCase_    )    ->      List[Any]:
          A  ,			A				  =		{}, {}
          if padding is not None:
                A				  =		padding
          if truncation is not None:
                A				  =		truncation
          if top_k is not None:
                A				  =		top_k
          return preprocess_params, {}, postprocess_params




    def __call__( self ,lowerCamelCase_ ,lowerCamelCase_ = None ,**lowerCamelCase_    )    ->      Dict:
          if isinstance(lowerCamelCase_ ,(Image.Image, str)    ) and isinstance(lowerCamelCase_ ,lowerCamelCase_    ):
                A				  =		{"""image""": image, """question""": question}
          else:
                A				  =		image
          A				  =		super().__call__(lowerCamelCase_ ,**lowerCamelCase_    )
          return results




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_=False ,lowerCamelCase_=False    )    ->      List[Any]:
          A				  =		load_image(inputs["""image"""]    )
          A				  =		self.tokenizer(
              inputs["""question"""] ,return_tensors=self.framework ,padding=lowerCamelCase_ ,truncation=lowerCamelCase_    )
          A				  =		self.image_processor(images=lowerCamelCase_ ,return_tensors=self.framework    )
          model_inputs.update(lowerCamelCase_    )
          return model_inputs




    def UpperCamelCase__		( self ,lowerCamelCase_    )    ->      Union[str, Any]:
          A				  =		self.model(**lowerCamelCase_    )
          return model_outputs




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_=5    )    ->      int:
          if top_k > self.model.config.num_labels:
                A				  =		self.model.config.num_labels

          if self.framework == "pt":
                A				  =		model_outputs.logits.sigmoid()[0]
                A  ,			A				  =		probs.topk(lowerCamelCase_    )
          else:
                raise ValueError(f'Unsupported framework: {self.framework}'    )

          A				  =		scores.tolist()
          A				  =		ids.tolist()
          return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase_ ,lowerCamelCase_    )]




