



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   _A   ( _lowercase	):


      '''simple docstring'''





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






       '''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     _snake_case	(	self	:							Optional[int]	):






       '''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     _snake_case	(	self	:							int	):






       '''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     _snake_case	(	self	:							Optional[int]							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Any							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							int							,      lowerCamelCase	:							List[str]							,      lowerCamelCase	:							str							,      lowerCamelCase	:							Optional[Any]							,      ):






       '''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     _snake_case	(	self	:							Any							,      lowerCamelCase	:							Dict							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							List[Any]							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Optional[Any]							,      lowerCamelCase	:							List[str]							,      ):






       '''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     _snake_case	(	self	:							int							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							List[str]							,      lowerCamelCase	:							int							,      lowerCamelCase	:							str							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							str							,      lowerCamelCase	:							Any							,      ):






       '''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     _snake_case	(	self	:							Dict							,      lowerCamelCase	:							str							,      lowerCamelCase	:							Dict							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Optional[Any]							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Tuple							,      ):






       '''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     _snake_case	(	self	:							List[str]							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							Optional[Any]							,      lowerCamelCase	:							Any							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							Any							,      lowerCamelCase	:							List[str]							,      lowerCamelCase	:							Dict							,      lowerCamelCase	:							Optional[int]							,      ):






       '''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     _snake_case	(	self	:							int							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							List[str]							,      lowerCamelCase	:							Dict							,      lowerCamelCase	:							Optional[Any]							,      lowerCamelCase	:							str							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							str							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							Optional[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     _snake_case	(	self	:							str							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							Optional[Any]							,      lowerCamelCase	:							Dict							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							List[Any]							,      lowerCamelCase	:							Any							,      lowerCamelCase	:							Union[str, Any]							,      lowerCamelCase	:							List[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     _snake_case	(	self	:							List[str]	):






       '''simple docstring'''



       __lowercase		       =			self.prepare_config_and_inputs()
       (
           (
           __lowercase    
       )       ,    (
           __lowercase    
       )       ,    (
           __lowercase    
       )       ,    (
           __lowercase    
       )       ,    (
           __lowercase    
       )       ,    (
           __lowercase    
       )       ,    (
           __lowercase    
       )       ,    (
           __lowercase    
       )       ,    (
           __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   _A   ( _lowercase  ,						_lowercase  ,						unittest.TestCase	):


      '''simple docstring'''





      _snake_case			:     List[str]      							=      (
          (
              FlaubertModel,
              FlaubertWithLMHeadModel,
              FlaubertForQuestionAnswering,
              FlaubertForQuestionAnsweringSimple,
              FlaubertForSequenceClassification,
              FlaubertForTokenClassification,
              FlaubertForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      _snake_case			:     Any      							=      (
          {
              """feature-extraction""": FlaubertModel,
              """fill-mask""": FlaubertWithLMHeadModel,
              """question-answering""": FlaubertForQuestionAnsweringSimple,
              """text-classification""": FlaubertForSequenceClassification,
              """token-classification""": FlaubertForTokenClassification,
              """zero-shot""": FlaubertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )



      def     _snake_case	(	self	:							List[str]							,      lowerCamelCase	:							str							,      lowerCamelCase	:							str							,      lowerCamelCase	:							str							,      lowerCamelCase	:							int							,      lowerCamelCase	:							Dict	):






       '''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     _snake_case	(	self	:							Union[str, Any]							,      lowerCamelCase	:							Tuple							,      lowerCamelCase	:							Optional[int]							,      lowerCamelCase	:							List[Any]=False	):






       '''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     _snake_case	(	self	:							List[str]	):






       '''simple docstring'''



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



      def     _snake_case	(	self	:							Any	):






       '''simple docstring'''



       self.config_tester.run_common_tests()



      def     _snake_case	(	self	:							str	):






       '''simple docstring'''



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



      def     _snake_case	(	self	:							List[Any]	):






       '''simple docstring'''



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



      def     _snake_case	(	self	:							List[Any]	):






       '''simple docstring'''



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



      def     _snake_case	(	self	:							Optional[int]	):






       '''simple docstring'''



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



      def     _snake_case	(	self	:							int	):






       '''simple docstring'''



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



      def     _snake_case	(	self	:							int	):






       '''simple docstring'''



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



      def     _snake_case	(	self	:							Any	):






       '''simple docstring'''



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



      @slow
      def     _snake_case	(	self	:							int	):






       '''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     _snake_case	(	self	:							Optional[Any]	):






       '''simple docstring'''



       __lowercase       ,    __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   _A   ( unittest.TestCase	):


      '''simple docstring'''





      @slow
      def     _snake_case	(	self	:							Union[str, Any]	):






       '''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	)	)








def    snake_case_				(    _SCREAMING_SNAKE_CASE   ):

	# bit count represents no. of bits in the gray code
	if bit_count < 0:
		raise ValueError("The given input must be positive"   )

	# get the generated string sequence
	__lowercase		       =			gray_code_sequence_string(_SCREAMING_SNAKE_CASE   )
	#
	# convert them to integers
	for i in range(len(_SCREAMING_SNAKE_CASE   )   ):
		__lowercase		       =			int(sequence[i]  ,	2   )

	return sequence



def    snake_case_				(    _SCREAMING_SNAKE_CASE   ):

	# The approach is a recursive one
	# Base case achieved when either n = 0 or n=1
	if bit_count == 0:
		return ["0"]

	if bit_count == 1:
		return ["0", "1"]

	__lowercase		       =			1 << bit_count  # defines the length of the sequence
	# 1<< n is equivalent to 2^n

	# recursive answer will generate answer for n-1 bits
	__lowercase		       =			gray_code_sequence_string(bit_count - 1   )

	__lowercase		       =			[]

	# append 0 to first half of the smaller sequence generated
	for i in range(seq_len // 2   ):
		__lowercase		       =			"0" + smaller_sequence[i]
		sequence.append(_SCREAMING_SNAKE_CASE   )

	# append 1 to second half ... start from the end of the list
	for i in reversed(range(seq_len // 2   )   ):
		__lowercase		       =			"1" + smaller_sequence[i]
		sequence.append(_SCREAMING_SNAKE_CASE   )

	return sequence


if __name__ == "__main__":
	import doctest

	doctest.testmod()



