

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



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



   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase	:							Optional[int]						=			ids_tensor([self.batch_size, self.seq_length],		self.vocab_size						)
     UpperCAmelCase	:							List[str]						=			random_attention_mask([self.batch_size, self.seq_length]						)

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

     UpperCAmelCase	:							Union[str, Any]						=			None
     if self.use_token_type_ids:
       UpperCAmelCase	:							int						=			ids_tensor([self.batch_size, self.seq_length],		self.n_langs						)

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

     UpperCAmelCase	:							Dict						=			self.get_config()

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



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



   def 	__magic_name__   (							self							:					str,		__A							:					Optional[int],		__A							:					Optional[Any],		__A							:					Optional[int],		__A							:					Dict,		__A							:					int,		__A							:					List[Any],		__A							:					List[Any],		__A							:					List[Any],		__A							:					Optional[int],		):
     UpperCAmelCase	:							Tuple						=			FlaubertWithLMHeadModel(__A						)
     model.to(__A						)
     model.eval()

     UpperCAmelCase	:							Optional[Any]						=			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 	__magic_name__   (							self							:					int,		__A							:					Optional[Any],		__A							:					Optional[int],		__A							:					str,		__A							:					Tuple,		__A							:					str,		__A							:					Tuple,		__A							:					List[Any],		__A							:					List[Any],		__A							:					Optional[Any],		):
     UpperCAmelCase	:							int						=			FlaubertForQuestionAnsweringSimple(__A						)
     model.to(__A						)
     model.eval()

     UpperCAmelCase	:							Optional[int]						=			model(__A						)

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

     UpperCAmelCase	:							Dict						=			model(__A						)

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

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

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

     UpperCAmelCase	:							str						=			model(__A,		start_positions=__A,		end_positions=__A						)

     ((UpperCAmelCase)			,						)	:							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 	__magic_name__   (							self							:					int,		__A							:					Optional[Any],		__A							:					Tuple,		__A							:					Union[str, Any],		__A							:					Any,		__A							:					List[Any],		__A							:					List[str],		__A							:					int,		__A							:					str,		__A							:					int,		):
     UpperCAmelCase	:							Union[str, Any]						=			FlaubertForSequenceClassification(__A						)
     model.to(__A						)
     model.eval()

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

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



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





   def 	__magic_name__   (							self							:					Any						):
     UpperCAmelCase	:							Optional[Any]						=			self.prepare_config_and_inputs()
     (
         (
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						
     )	:							List[Any]						=			config_and_inputs
     UpperCAmelCase	:							Optional[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  __UpperCAmelCase					(	lowerCamelCase__      ,  lowerCamelCase__      ,  unittest.TestCase						):
   UpperCamelCase       					=					(
       (
           FlaubertModel,
           FlaubertWithLMHeadModel,
           FlaubertForQuestionAnswering,
           FlaubertForQuestionAnsweringSimple,
           FlaubertForSequenceClassification,
           FlaubertForTokenClassification,
           FlaubertForMultipleChoice,
       )
       if is_torch_available()
       else ()
   )
   UpperCamelCase       					=					(
       {
           """feature-extraction""": FlaubertModel,
           """fill-mask""": FlaubertWithLMHeadModel,
           """question-answering""": FlaubertForQuestionAnsweringSimple,
           """text-classification""": FlaubertForSequenceClassification,
           """token-classification""": FlaubertForTokenClassification,
           """zero-shot""": FlaubertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )



   def 	__magic_name__   (							self							:					Dict,		__A							:					Union[str, Any],		__A							:					Tuple,		__A							:					Union[str, Any],		__A							:					Optional[Any],		__A							:					List[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 	__magic_name__   (							self							:					Union[str, Any],		__A							:					Any,		__A							:					Any,		__A							:					List[Any]=False						):
     UpperCAmelCase	:							Optional[int]						=			super()._prepare_for_class(__A,		__A,		return_labels=__A						)

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

     return inputs_dict



   def 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							str						=			FlaubertModelTester(self						)
     UpperCAmelCase	:							Optional[Any]						=			ConfigTester(self,		config_class=__A,		emb_dim=3_7						)



   def 	__magic_name__   (							self							:					int						):
     self.config_tester.run_common_tests()



   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase	:							Optional[Any]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_model(*__A						)



   def 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							int						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_lm_head(*__A						)



   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_simple_qa(*__A						)



   def 	__magic_name__   (							self							:					Tuple						):
     UpperCAmelCase	:							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_qa(*__A						)



   def 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							Dict						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_sequence_classif(*__A						)



   def 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_token_classif(*__A						)



   def 	__magic_name__   (							self							:					Optional[Any]						):
     UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_multiple_choice(*__A						)



   @slow
   def 	__magic_name__   (							self							:					str						):
     for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							Union[str, Any]						=			FlaubertModel.from_pretrained(__A						)
       self.assertIsNotNone(__A						)





   @slow
   @require_torch_gpu
   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase			,						UpperCAmelCase	:							Union[str, 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

       UpperCAmelCase	:							Optional[Any]						=			True
       UpperCAmelCase	:							Optional[Any]						=			model_class(config=__A						)

       UpperCAmelCase	:							Optional[int]						=			self._prepare_for_class(__A,		__A						)
       UpperCAmelCase	:							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'''						)						)
         UpperCAmelCase	:							Optional[int]						=			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  __UpperCAmelCase					(	unittest.TestCase						):



   @slow
   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase	:							str						=			FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''						)
     UpperCAmelCase	:							Optional[int]						=			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():
       UpperCAmelCase	:							Any						=			model(__A						)[0]
     UpperCAmelCase	:							int						=			torch.Size((1, 1_1, 7_6_8)						)
     self.assertEqual(output.shape,		__A						)
     UpperCAmelCase	:							List[str]						=			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],		__A,		atol=1E-4						)						)








def       a__					(		UpperCAmelCase							:      int    ,			UpperCAmelCase							:      int      )  ->						str:
  if a < 0 or b < 0:
    raise ValueError('''the value of both inputs must be positive'''      )
  UpperCAmelCase	:							List[Any]						=			str(bin(UpperCAmelCase      )      )[2:]  # remove the leading "0b"
  UpperCAmelCase	:							List[str]						=			str(bin(UpperCAmelCase      )      )[2:]
  UpperCAmelCase	:							Optional[Any]						=			max(len(UpperCAmelCase      )    ,			len(UpperCAmelCase      )      )
  return "0b" + "".join(
      str(int('''1''' in (char_a, char_b)      )      )
      for char_a, char_b in zip(a_binary.zfill(UpperCAmelCase      )    ,			b_binary.zfill(UpperCAmelCase      )      )      )


if __name__ == "__main__":
     import doctest

     doctest.testmod()





