

'''simple docstring'''



import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         FlaubertForMultipleChoice,
         FlaubertForQuestionAnswering,
         FlaubertForQuestionAnsweringSimple,
         FlaubertForSequenceClassification,
         FlaubertForTokenClassification,
         FlaubertModel,
         FlaubertWithLMHeadModel,
     )
     from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class 				_snake_case					(   a_			):

  def __init__(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=13      , _SCREAMING_SNAKE_CASE=7      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=False      , _SCREAMING_SNAKE_CASE=False      , _SCREAMING_SNAKE_CASE=False      , _SCREAMING_SNAKE_CASE=2      , _SCREAMING_SNAKE_CASE=99      , _SCREAMING_SNAKE_CASE=0      , _SCREAMING_SNAKE_CASE=32      , _SCREAMING_SNAKE_CASE=5      , _SCREAMING_SNAKE_CASE=4      , _SCREAMING_SNAKE_CASE=0.1      , _SCREAMING_SNAKE_CASE=0.1      , _SCREAMING_SNAKE_CASE=5_12      , _SCREAMING_SNAKE_CASE=12      , _SCREAMING_SNAKE_CASE=2      , _SCREAMING_SNAKE_CASE=0.02      , _SCREAMING_SNAKE_CASE=3      , _SCREAMING_SNAKE_CASE=4      , _SCREAMING_SNAKE_CASE="last"      , _SCREAMING_SNAKE_CASE=None      , _SCREAMING_SNAKE_CASE=None      , ):





      '''simple docstring'''

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

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

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

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

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

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

      lowerCAmelCase 						=							self.get_config()

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

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''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   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

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

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							FlaubertWithLMHeadModel(__lowerCAmelCase	)
      model.to(__lowerCAmelCase	)
      model.eval()

      lowerCAmelCase 						=							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   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							FlaubertForQuestionAnsweringSimple(__lowerCAmelCase	)
      model.to(__lowerCAmelCase	)
      model.eval()

      lowerCAmelCase 						=							model(__lowerCAmelCase	)

      lowerCAmelCase 						=							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   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							FlaubertForQuestionAnswering(__lowerCAmelCase	)
      model.to(__lowerCAmelCase	)
      model.eval()

      lowerCAmelCase 						=							model(__lowerCAmelCase	)

      lowerCAmelCase 						=							model(
          __lowerCAmelCase      , start_positions=__lowerCAmelCase      , end_positions=__lowerCAmelCase      , cls_index=__lowerCAmelCase      , is_impossible=__lowerCAmelCase      , p_mask=__lowerCAmelCase      , )

      lowerCAmelCase 						=							model(
          __lowerCAmelCase      , start_positions=__lowerCAmelCase      , end_positions=__lowerCAmelCase      , cls_index=__lowerCAmelCase      , is_impossible=__lowerCAmelCase      , )

      (lowerCAmelCase					) 						=							result_with_labels.to_tuple()

      lowerCAmelCase 						=							model(__lowerCAmelCase      , start_positions=__lowerCAmelCase      , end_positions=__lowerCAmelCase	)

      (lowerCAmelCase					) 						=							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   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							FlaubertForSequenceClassification(__lowerCAmelCase	)
      model.to(__lowerCAmelCase	)
      model.eval()

      lowerCAmelCase 						=							model(__lowerCAmelCase	)
      lowerCAmelCase 						=							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   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							self.num_labels
      lowerCAmelCase 						=							FlaubertForTokenClassification(__lowerCAmelCase	)
      model.to(__lowerCAmelCase	)
      model.eval()

      lowerCAmelCase 						=							model(__lowerCAmelCase      , attention_mask=__lowerCAmelCase      , labels=__lowerCAmelCase	)
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length, self.num_labels)	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							self.num_choices
      lowerCAmelCase 						=							FlaubertForMultipleChoice(config=__lowerCAmelCase	)
      model.to(__lowerCAmelCase	)
      model.eval()
      lowerCAmelCase 						=							input_ids.unsqueeze(1	).expand(-1      , self.num_choices      , -1	).contiguous()
      lowerCAmelCase 						=							token_type_ids.unsqueeze(1	).expand(-1      , self.num_choices      , -1	).contiguous()
      lowerCAmelCase 						=							input_mask.unsqueeze(1	).expand(-1      , self.num_choices      , -1	).contiguous()
      lowerCAmelCase 						=							model(
          __lowerCAmelCase      , attention_mask=__lowerCAmelCase      , token_type_ids=__lowerCAmelCase      , labels=__lowerCAmelCase      , )
      self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_choices)	)


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.prepare_config_and_inputs()
      (
          lowerCAmelCase					
      ) 						=							config_and_inputs
      lowerCAmelCase 						=							{
          '''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					(   a_     , a_     , unittest.TestCase			):
  SCREAMING_SNAKE_CASE			:       List[str]				=     (
      (
          FlaubertModel,
          FlaubertWithLMHeadModel,
          FlaubertForQuestionAnswering,
          FlaubertForQuestionAnsweringSimple,
          FlaubertForSequenceClassification,
          FlaubertForTokenClassification,
          FlaubertForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE			:       Dict				=     (
      {
          '''feature-extraction''': FlaubertModel,
          '''fill-mask''': FlaubertWithLMHeadModel,
          '''question-answering''': FlaubertForQuestionAnsweringSimple,
          '''text-classification''': FlaubertForSequenceClassification,
          '''token-classification''': FlaubertForTokenClassification,
          '''zero-shot''': FlaubertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	):





      '''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   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=False	):





      '''simple docstring'''

      lowerCAmelCase 						=							super()._prepare_for_class(__lowerCAmelCase      , __lowerCAmelCase      , return_labels=__lowerCAmelCase	)

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

      return inputs_dict

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							FlaubertModelTester(self	)
      lowerCAmelCase 						=							ConfigTester(self      , config_class=__lowerCAmelCase      , emb_dim=37	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      self.config_tester.run_common_tests()

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*__lowerCAmelCase	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*__lowerCAmelCase	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCAmelCase	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*__lowerCAmelCase	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCAmelCase	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_token_classif(*__lowerCAmelCase	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCAmelCase	)

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

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


  @slow
  @require_torch_gpu
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							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

          lowerCAmelCase 						=							True
          lowerCAmelCase 						=							model_class(config=__lowerCAmelCase	)

          lowerCAmelCase 						=							self._prepare_for_class(__lowerCAmelCase      , __lowerCAmelCase	)
          lowerCAmelCase 						=							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'	)	)
              lowerCAmelCase 						=							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			):

  @slow
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'	)
      lowerCAmelCase 						=							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():
          lowerCAmelCase 						=							model(__lowerCAmelCase	)[0]
      lowerCAmelCase 						=							torch.Size((1, 11, 7_68)	)
      self.assertEqual(output.shape      , __lowerCAmelCase	)
      lowerCAmelCase 						=							torch.tensor(
          [[[-2.6_251, -1.4_298, -0.0_227], [-2.8_510, -1.6_387, 0.2_258], [-2.8_114, -1.1_832, -0.3_066]]]	)

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













import sys

SCREAMING_SNAKE_CASE__   :      Optional[Any]				=		(
    """73167176531330624919225119674426574742355349194934"""
    """96983520312774506326239578318016984801869478851843"""
    """85861560789112949495459501737958331952853208805511"""
    """12540698747158523863050715693290963295227443043557"""
    """66896648950445244523161731856403098711121722383113"""
    """62229893423380308135336276614282806444486645238749"""
    """30358907296290491560440772390713810515859307960866"""
    """70172427121883998797908792274921901699720888093776"""
    """65727333001053367881220235421809751254540594752243"""
    """52584907711670556013604839586446706324415722155397"""
    """53697817977846174064955149290862569321978468622482"""
    """83972241375657056057490261407972968652414535100474"""
    """82166370484403199890008895243450658541227588666881"""
    """16427171479924442928230863465674813919123162824586"""
    """17866458359124566529476545682848912883142607690042"""
    """24219022671055626321111109370544217506941658960408"""
    """07198403850962455444362981230987879927244284909188"""
    """84580156166097919133875499200524063689912560717606"""
    """05886116467109405077541002256983155200055935729725"""
    """71636269561882670428252483600823257530420752963450"""
)

def   __lowercase       (			snake_case = N			):







				"""simple docstring"""






				__magic_name__		:Optional[int]   =   -sys.maxsize - 1
				for i in range(len(snake_case			) - 1_2			):
								__magic_name__		:List[Any]   =   1
								for j in range(1_3			):
												product *= int(n[i + j]			)
								if product > largest_product:
												__magic_name__		:str   =   product
				return largest_product


if __name__ == "__main__":
					print(f"{solution() = }")






