


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(		_a     ):
       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=5_12,   lowerCamelCase=12,   lowerCamelCase=2,   lowerCamelCase=0.0_2,   lowerCamelCase=3,   lowerCamelCase=4,   lowerCamelCase="last",   lowerCamelCase=None,   lowerCamelCase=None,   ) ->     Optional[int]:




           """simple docstring"""

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




           """simple docstring"""

           _lowercase :							Union[str, Any]   	=	ids_tensor([self.batch_size, self.seq_length],   self.vocab_size)
           _lowercase :							Dict   	=	random_attention_mask([self.batch_size, self.seq_length])

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

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

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

           _lowercase :							List[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) ->     List[Any]:




           """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,   ) ->     str:




           """simple docstring"""

           _lowercase :							str   	=	FlaubertModel(config=lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()
           _lowercase :							Dict   	=	model(lowerCamelCase,   lengths=lowerCamelCase,   langs=lowerCamelCase)
           _lowercase :							Union[str, Any]   	=	model(lowerCamelCase,   langs=lowerCamelCase)
           _lowercase :							Union[str, Any]   	=	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,   ) ->     Any:




           """simple docstring"""

           _lowercase :							str   	=	FlaubertWithLMHeadModel(lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()

           _lowercase :							Optional[int]   	=	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]:




           """simple docstring"""

           _lowercase :							Dict   	=	FlaubertForQuestionAnsweringSimple(lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()

           _lowercase :							Optional[int]   	=	model(lowerCamelCase)

           _lowercase :							Optional[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,   ) ->     Dict:




           """simple docstring"""

           _lowercase :							Tuple   	=	FlaubertForQuestionAnswering(lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()

           _lowercase :							Any   	=	model(lowerCamelCase)

           _lowercase :							List[str]   	=	model(
               lowerCamelCase,   start_positions=lowerCamelCase,   end_positions=lowerCamelCase,   cls_index=lowerCamelCase,   is_impossible=lowerCamelCase,   p_mask=lowerCamelCase,   )

           _lowercase :							Any   	=	model(
               lowerCamelCase,   start_positions=lowerCamelCase,   end_positions=lowerCamelCase,   cls_index=lowerCamelCase,   is_impossible=lowerCamelCase,   )

           ((_lowercase)	,       ) :							Tuple   	=	result_with_labels.to_tuple()

           _lowercase :							List[str]   	=	model(lowerCamelCase,   start_positions=lowerCamelCase,   end_positions=lowerCamelCase)

           ((_lowercase)	,       ) :							Optional[int]   	=	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,   ) ->     Any:




           """simple docstring"""

           _lowercase :							Dict   	=	FlaubertForSequenceClassification(lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()

           _lowercase :							List[str]   	=	model(lowerCamelCase)
           _lowercase :							Tuple   	=	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,   ) ->     List[Any]:




           """simple docstring"""

           _lowercase :							Union[str, Any]   	=	self.num_labels
           _lowercase :							Dict   	=	FlaubertForTokenClassification(lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()

           _lowercase :							Optional[Any]   	=	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,   ) ->     str:




           """simple docstring"""

           _lowercase :							Dict   	=	self.num_choices
           _lowercase :							List[Any]   	=	FlaubertForMultipleChoice(config=lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()
           _lowercase :							int   	=	input_ids.unsqueeze(1).expand(-1,   self.num_choices,   -1).contiguous()
           _lowercase :							int   	=	token_type_ids.unsqueeze(1).expand(-1,   self.num_choices,   -1).contiguous()
           _lowercase :							Optional[Any]   	=	input_mask.unsqueeze(1).expand(-1,   self.num_choices,   -1).contiguous()
           _lowercase :							Tuple   	=	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) ->     Tuple:




           """simple docstring"""

           _lowercase :							Tuple   	=	self.prepare_config_and_inputs()
           (
               (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       (
               _lowercase
           )	,       
           ) :							List[str]   	=	config_and_inputs
           _lowercase :							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(		_a,				_a,				unittest.TestCase     ):
       lowercase_				:			Union[str, Any]									=    (
           (
               FlaubertModel,
               FlaubertWithLMHeadModel,
               FlaubertForQuestionAnswering,
               FlaubertForQuestionAnsweringSimple,
               FlaubertForSequenceClassification,
               FlaubertForTokenClassification,
               FlaubertForMultipleChoice,
           )
           if is_torch_available()
           else ()
       )
       lowercase_				:			Tuple									=    (
           {
               """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) ->     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     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase=False) ->     Optional[int]:




           """simple docstring"""

           _lowercase :							Any   	=	super()._prepare_for_class(lowerCamelCase,   lowerCamelCase,   return_labels=lowerCamelCase)

           if return_labels:
               if model_class.__name__ == "FlaubertForQuestionAnswering":
                   _lowercase :							List[Any]   	=	torch.zeros(
                       self.model_tester.batch_size,   dtype=torch.long,   device=lowerCamelCase)
                   _lowercase :							Optional[int]   	=	torch.zeros(
                       self.model_tester.batch_size,   dtype=torch.long,   device=lowerCamelCase)

           return inputs_dict
       def     UpperCamelCase  (      self) ->     Tuple:




           """simple docstring"""

           _lowercase :							List[Any]   	=	FlaubertModelTester(self)
           _lowercase :							str   	=	ConfigTester(self,   config_class=lowerCamelCase,   emb_dim=37)
       def     UpperCamelCase  (      self) ->     Optional[int]:




           """simple docstring"""

           self.config_tester.run_common_tests()
       def     UpperCamelCase  (      self) ->     Union[str, Any]:




           """simple docstring"""

           _lowercase :							Optional[Any]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_flaubert_model(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Tuple:




           """simple docstring"""

           _lowercase :							Dict   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Tuple:




           """simple docstring"""

           _lowercase :							Tuple   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_flaubert_simple_qa(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Any:




           """simple docstring"""

           _lowercase :							Tuple   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Dict:




           """simple docstring"""

           _lowercase :							Dict   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Any:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_flaubert_token_classif(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Any:




           """simple docstring"""

           _lowercase :							Union[str, Any]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_flaubert_multiple_choice(*lowerCamelCase)
       @slow
       def     UpperCamelCase  (      self) ->     Union[str, Any]:




           """simple docstring"""

           for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _lowercase :							int   	=	FlaubertModel.from_pretrained(lowerCamelCase)
               self.assertIsNotNone(lowerCamelCase)




       @slow
       @require_torch_gpu
       def     UpperCamelCase  (      self) ->     Optional[int]:




           """simple docstring"""

           _lowercase	,       _lowercase :							int   	=	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

               _lowercase :							Optional[int]   	=	True
               _lowercase :							Dict   	=	model_class(config=lowerCamelCase)

               _lowercase :							Union[str, Any]   	=	self._prepare_for_class(lowerCamelCase,   lowerCamelCase)
               _lowercase :							Any   	=	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'))
                   _lowercase :							str   	=	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     ):
       @slow
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	FlaubertModel.from_pretrained('flaubert/flaubert_base_cased')
           _lowercase :							List[Any]   	=	torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]])
           with torch.no_grad():
               _lowercase :							Union[str, Any]   	=	model(lowerCamelCase)[0]
           _lowercase :							Optional[Any]   	=	torch.Size((1, 11, 7_68))
           self.assertEqual(output.shape,   lowerCamelCase)
           _lowercase :							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))






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class _lowerCamelCase(		_a     ):
       lowercase_				:			int									=    ["""image_processor""", """tokenizer"""]
       lowercase_				:			List[str]									=    """CLIPImageProcessor"""
       lowercase_				:			Union[str, Any]									=    ("""CLIPTokenizer""", """CLIPTokenizerFast""")
       def __init__(      self,   lowerCamelCase=None,   lowerCamelCase=None,   **lowerCamelCase) ->     Dict:




           """simple docstring"""

           _lowercase :							List[Any]   	=	None
           if "feature_extractor" in kwargs:
               warnings.warn(
                   'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                   ' instead.',   lowerCamelCase,   )
               _lowercase :							Dict   	=	kwargs.pop('feature_extractor')

           _lowercase :							int   	=	image_processor if image_processor is not None else feature_extractor
           if image_processor is None:
               raise ValueError('You need to specify an `image_processor`.')
           if tokenizer is None:
               raise ValueError('You need to specify a `tokenizer`.')

           super().__init__(lowerCamelCase,   lowerCamelCase)
       def __call__(      self,   lowerCamelCase=None,   lowerCamelCase=None,   lowerCamelCase=None,   **lowerCamelCase) ->     Tuple:




           """simple docstring"""

           if text is None and images is None:
               raise ValueError('You have to specify either text or images. Both cannot be none.')

           if text is not None:
               _lowercase :							Optional[Any]   	=	self.tokenizer(lowerCamelCase,   return_tensors=lowerCamelCase,   **lowerCamelCase)

           if images is not None:
               _lowercase :							List[Any]   	=	self.image_processor(lowerCamelCase,   return_tensors=lowerCamelCase,   **lowerCamelCase)

           if text is not None and images is not None:
               _lowercase :							int   	=	image_features.pixel_values
               return encoding
           elif text is not None:
               return encoding
           else:
               return BatchEncoding(data=dict(**lowerCamelCase),   tensor_type=lowerCamelCase)
       def     UpperCamelCase  (      self,   *lowerCamelCase,   **lowerCamelCase) ->     Union[str, Any]:




           """simple docstring"""

           return self.tokenizer.batch_decode(*lowerCamelCase,   **lowerCamelCase)
       def     UpperCamelCase  (      self,   *lowerCamelCase,   **lowerCamelCase) ->     int:




           """simple docstring"""

           return self.tokenizer.decode(*lowerCamelCase,   **lowerCamelCase)
       @property
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           _lowercase :							int   	=	self.tokenizer.model_input_names
           _lowercase :							Dict   	=	self.image_processor.model_input_names
           return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))
       @property
       def     UpperCamelCase  (      self) ->     List[str]:




           """simple docstring"""

           warnings.warn(
               '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.',   lowerCamelCase,   )
           return self.image_processor_class




       @property
       def     UpperCamelCase  (      self) ->     Optional[Any]:




           """simple docstring"""

           warnings.warn(
               '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.',   lowerCamelCase,   )
           return self.image_processor


