





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_   (							__lowercase						):







	'''simple docstring'''




	def __init__(						self							:     str					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     str=13					,      __lowerCamelCase							:     Dict=7					,      __lowerCamelCase							:     str=True					,      __lowerCamelCase							:     Dict=True					,      __lowerCamelCase							:     List[Any]=True					,      __lowerCamelCase							:     Any=True					,      __lowerCamelCase							:     Optional[Any]=True					,      __lowerCamelCase							:     Union[str, Any]=False					,      __lowerCamelCase							:     List[str]=False					,      __lowerCamelCase							:     List[str]=False					,      __lowerCamelCase							:     Tuple=2					,      __lowerCamelCase							:     Dict=99					,      __lowerCamelCase							:     List[Any]=0					,      __lowerCamelCase							:     Dict=32					,      __lowerCamelCase							:     str=5					,      __lowerCamelCase							:     str=4					,      __lowerCamelCase							:     Dict=0.1					,      __lowerCamelCase							:     Union[str, Any]=0.1					,      __lowerCamelCase							:     Union[str, Any]=512					,      __lowerCamelCase							:     Optional[int]=12					,      __lowerCamelCase							:     Dict=2					,      __lowerCamelCase							:     Optional[int]=0.02					,      __lowerCamelCase							:     Optional[Any]=3					,      __lowerCamelCase							:     int=4					,      __lowerCamelCase							:     int="last"					,      __lowerCamelCase							:     List[Any]=None					,      __lowerCamelCase							:     List[Any]=None					,      )       -> Dict:

			'''simple docstring'''




			__lowercase											=     parent
			__lowercase											=     batch_size
			__lowercase											=     seq_length
			__lowercase											=     is_training
			__lowercase											=     use_input_lengths
			__lowercase											=     use_token_type_ids
			__lowercase											=     use_labels
			__lowercase											=     gelu_activation
			__lowercase											=     sinusoidal_embeddings
			__lowercase											=     causal
			__lowercase											=     asm
			__lowercase											=     n_langs
			__lowercase											=     vocab_size
			__lowercase											=     n_special
			__lowercase											=     hidden_size
			__lowercase											=     num_hidden_layers
			__lowercase											=     num_attention_heads
			__lowercase											=     hidden_dropout_prob
			__lowercase											=     attention_probs_dropout_prob
			__lowercase											=     max_position_embeddings
			__lowercase											=     type_vocab_size
			__lowercase											=     type_sequence_label_size
			__lowercase											=     initializer_range
			__lowercase											=     num_labels
			__lowercase											=     num_choices
			__lowercase											=     summary_type
			__lowercase											=     use_proj
			__lowercase											=     scope




	def    UpperCAmelCase			(						self							:     Union[str, Any]   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size   )
			__lowercase											=     random_attention_mask([self.batch_size, self.seq_length]   )

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

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

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

			__lowercase											=     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							:     Any   )       -> 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							:     Optional[int]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     str					,      )       -> List[str]:

			'''simple docstring'''




			__lowercase											=     FlaubertModel(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      lengths=__lowerCamelCase					,      langs=__lowerCamelCase   )
			__lowercase											=     model(__lowerCamelCase					,      langs=__lowerCamelCase   )
			__lowercase											=     model(__lowerCamelCase   )
			self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)   )




	def    UpperCAmelCase			(						self							:     str					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Optional[int]					,      )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     FlaubertWithLMHeadModel(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			__lowercase											=     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							:     Optional[int]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Union[str, Any]					,      )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     FlaubertForQuestionAnsweringSimple(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			__lowercase											=     model(__lowerCamelCase   )

			__lowercase											=     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							:     Tuple					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Dict					,      )       -> Dict:

			'''simple docstring'''




			__lowercase											=     FlaubertForQuestionAnswering(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			__lowercase											=     model(__lowerCamelCase   )

			__lowercase											=     model(
			    __lowerCamelCase					,      start_positions=__lowerCamelCase					,      end_positions=__lowerCamelCase					,      cls_index=__lowerCamelCase					,      is_impossible=__lowerCamelCase					,      p_mask=__lowerCamelCase					,      )

			__lowercase											=     model(
			    __lowerCamelCase					,      start_positions=__lowerCamelCase					,      end_positions=__lowerCamelCase					,      cls_index=__lowerCamelCase					,      is_impossible=__lowerCamelCase					,      )

			(__lowercase		)											=     result_with_labels.to_tuple()

			__lowercase											=     model(__lowerCamelCase					,      start_positions=__lowerCamelCase					,      end_positions=__lowerCamelCase   )

			(__lowercase		)											=     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							:     Union[str, Any]					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     Dict					,      )       -> Optional[Any]:

			'''simple docstring'''




			__lowercase											=     FlaubertForSequenceClassification(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			__lowercase											=     model(__lowerCamelCase   )
			__lowercase											=     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							:     Tuple					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     List[str]					,      )       -> int:

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     FlaubertForTokenClassification(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			__lowercase											=     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							:     Optional[Any]					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Union[str, Any]					,      )       -> Union[str, Any]:

			'''simple docstring'''




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

			'''simple docstring'''




			__lowercase											=     self.prepare_config_and_inputs()
			(
			    __lowercase		
			)											=     config_and_inputs
			__lowercase											=     {
			    "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_   (							__lowercase     ,     __lowercase     ,     unittest.TestCase						):







	'''simple docstring'''
	__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    UpperCAmelCase			(						self							:     Optional[int]					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Dict   )       -> str:

			'''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							:     Dict					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Optional[int]=False   )       -> Union[str, Any]:

			'''simple docstring'''




			__lowercase											=     super()._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase					,      return_labels=__lowerCamelCase   )

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

			return inputs_dict




	def    UpperCAmelCase			(						self							:     str   )       -> Optional[Any]:

			'''simple docstring'''




			__lowercase											=     FlaubertModelTester(self   )
			__lowercase											=     ConfigTester(self					,      config_class=__lowerCamelCase					,      emb_dim=37   )




	def    UpperCAmelCase			(						self							:     List[str]   )       -> Union[str, Any]:

			'''simple docstring'''




			self.config_tester.run_common_tests()




	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> Optional[Any]:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_flaubert_model(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Tuple   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_flaubert_lm_head(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Union[str, Any]   )       -> Any:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     List[Any]   )       -> str:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_flaubert_qa(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Dict   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     str   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_flaubert_token_classif(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     int   )       -> int:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCamelCase   )




	@slow
	def    UpperCAmelCase			(						self							:     Dict   )       -> Optional[Any]:

			'''simple docstring'''




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






	@slow
	@require_torch_gpu
	def    UpperCAmelCase			(						self							:     int   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     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											=     True
					__lowercase											=     model_class(config=__lowerCamelCase   )

					__lowercase											=     self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase   )
					__lowercase											=     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											=     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							:     Any   )       -> List[str]:

			'''simple docstring'''




			__lowercase											=     FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'   )
			__lowercase											=     torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]   )
			with torch.no_grad():
					__lowercase											=     model(__lowerCamelCase   )[0]
			__lowercase											=     torch.Size((1, 11, 768)   )
			self.assertEqual(output.shape					,      __lowerCamelCase   )
			__lowercase											=     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]					,      __lowerCamelCase					,      atol=1E-4   )   )






import math




def       _SCREAMING_SNAKE_CASE   (						_lowerCamelCase					:     int)   ->       str:






							'''simple docstring'''





							__UpperCamelCase      :  Union[str, Any]       =   0
							__UpperCamelCase      :  int       =   0
							while num > 0:
														__UpperCamelCase      :  List[Any]       =   num % 8
														__UpperCamelCase      :  Tuple       =   octal + (remainder * math.floor(math.pow(10		,    _lowerCamelCase)))
														counter += 1
														__UpperCamelCase      :  Optional[Any]       =   math.floor(num / 8)  # basically /= 8 without remainder if any
														# This formatting removes trailing '.0' from `octal`.
							return F'0o{int(_lowerCamelCase)}'




def       _SCREAMING_SNAKE_CASE   (						)   ->       None:






							'''simple docstring'''





							print("\n2 in octal is:")
							print(decimal_to_octal(2))  # = 2
							print("\n8 in octal is:")
							print(decimal_to_octal(8))  # = 10
							print("\n65 in octal is:")
							print(decimal_to_octal(65))  # = 101
							print("\n216 in octal is:")
							print(decimal_to_octal(216))  # = 330
							print("\n512 in octal is:")
							print(decimal_to_octal(512))  # = 1000
							print("\n")


if __name__ == "__main__":
		main()