
'''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  __A				( __lowerCamelCase			):
 def __init__(   self			:    Optional[Any]				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    Optional[int]=13				,   lowerCamelCase			:    Optional[int]=7				,   lowerCamelCase			:    Any=True				,   lowerCamelCase			:    List[Any]=True				,   lowerCamelCase			:    Tuple=True				,   lowerCamelCase			:    Union[str, Any]=True				,   lowerCamelCase			:    str=True				,   lowerCamelCase			:    Optional[Any]=False				,   lowerCamelCase			:    List[Any]=False				,   lowerCamelCase			:    Optional[Any]=False				,   lowerCamelCase			:    Optional[int]=2				,   lowerCamelCase			:    int=99				,   lowerCamelCase			:    List[str]=0				,   lowerCamelCase			:    List[str]=32				,   lowerCamelCase			:    int=5				,   lowerCamelCase			:    Any=4				,   lowerCamelCase			:    Dict=0.1				,   lowerCamelCase			:    int=0.1				,   lowerCamelCase			:    List[str]=5_12				,   lowerCamelCase			:    Tuple=12				,   lowerCamelCase			:    str=2				,   lowerCamelCase			:    Tuple=0.02				,   lowerCamelCase			:    Any=3				,   lowerCamelCase			:    Optional[int]=4				,   lowerCamelCase			:    Union[str, Any]="last"				,   lowerCamelCase			:    Any=None				,   lowerCamelCase			:    int=None				,   ):



   """simple docstring"""





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



   """simple docstring"""





   __A		:     int												=		ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size       )
   __A		:     List[str]												=		random_attention_mask([self.batch_size, self.seq_length]       )

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

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

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

   __A		:     Tuple												=		self.get_config()

   return (
       config,
       input_ids,
       token_type_ids,
       input_lengths,
       sequence_labels,
       token_labels,
       is_impossible_labels,
       choice_labels,
       input_mask,
   )
 def 		lowercase_(   self			:    Union[str, 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 		lowercase_(   self			:    Any				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    str				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    str				,   lowerCamelCase			:    str				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    Any				,   ):



   """simple docstring"""





   __A		:     str												=		FlaubertModel(config=UpperCAmelCase_       )
   model.to(UpperCAmelCase_       )
   model.eval()
   __A		:     Union[str, Any]												=		model(UpperCAmelCase_				,   lengths=UpperCAmelCase_				,   langs=UpperCAmelCase_       )
   __A		:     Optional[Any]												=		model(UpperCAmelCase_				,   langs=UpperCAmelCase_       )
   __A		:     Any												=		model(UpperCAmelCase_       )
   self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)       )
 def 		lowercase_(   self			:    str				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    int				,   lowerCamelCase			:    int				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    Dict				,   ):



   """simple docstring"""





   __A		:     List[Any]												=		FlaubertWithLMHeadModel(UpperCAmelCase_       )
   model.to(UpperCAmelCase_       )
   model.eval()

   __A		:     Optional[int]												=		model(UpperCAmelCase_				,   token_type_ids=UpperCAmelCase_				,   labels=UpperCAmelCase_       )
   self.parent.assertEqual(result.loss.shape				,   ()       )
   self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.vocab_size)       )
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    Optional[int]				,   ):



   """simple docstring"""





   __A		:     List[Any]												=		FlaubertForQuestionAnsweringSimple(UpperCAmelCase_       )
   model.to(UpperCAmelCase_       )
   model.eval()

   __A		:     List[Any]												=		model(UpperCAmelCase_       )

   __A		:     List[str]												=		model(UpperCAmelCase_				,   start_positions=UpperCAmelCase_				,   end_positions=UpperCAmelCase_       )
   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 		lowercase_(   self			:    int				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    List[str]				,   ):



   """simple docstring"""





   __A		:     str												=		FlaubertForQuestionAnswering(UpperCAmelCase_       )
   model.to(UpperCAmelCase_       )
   model.eval()

   __A		:     Optional[int]												=		model(UpperCAmelCase_       )

   __A		:     Optional[Any]												=		model(
       UpperCAmelCase_				,   start_positions=UpperCAmelCase_				,   end_positions=UpperCAmelCase_				,   cls_index=UpperCAmelCase_				,   is_impossible=UpperCAmelCase_				,   p_mask=UpperCAmelCase_				,   )

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

   (__A	)		:     Tuple												=		result_with_labels.to_tuple()

   __A		:     int												=		model(UpperCAmelCase_				,   start_positions=UpperCAmelCase_				,   end_positions=UpperCAmelCase_       )

   (__A	)		:     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 		lowercase_(   self			:    Optional[int]				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    str				,   lowerCamelCase			:    int				,   ):



   """simple docstring"""





   __A		:     str												=		FlaubertForSequenceClassification(UpperCAmelCase_       )
   model.to(UpperCAmelCase_       )
   model.eval()

   __A		:     Tuple												=		model(UpperCAmelCase_       )
   __A		:     Any												=		model(UpperCAmelCase_				,   labels=UpperCAmelCase_       )

   self.parent.assertEqual(result.loss.shape				,   ()       )
   self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.type_sequence_label_size)       )
 def 		lowercase_(   self			:    Optional[int]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    Optional[Any]				,   ):



   """simple docstring"""





   __A		:     List[Any]												=		self.num_labels
   __A		:     Optional[Any]												=		FlaubertForTokenClassification(UpperCAmelCase_       )
   model.to(UpperCAmelCase_       )
   model.eval()

   __A		:     Union[str, Any]												=		model(UpperCAmelCase_				,   attention_mask=UpperCAmelCase_				,   labels=UpperCAmelCase_       )
   self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.num_labels)       )
 def 		lowercase_(   self			:    Tuple				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    int				,   ):



   """simple docstring"""





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






 def 		lowercase_(   self			:    Union[str, Any]       ):



   """simple docstring"""





   __A		:     List[Any]												=		self.prepare_config_and_inputs()
   (
       __A	
   )		:     str												=		config_and_inputs
   __A		:     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  __A				( __lowerCamelCase    ,						__lowerCamelCase    ,						unittest.TestCase			):
 lowerCamelCase				       =(
     (
         FlaubertModel,
         FlaubertWithLMHeadModel,
         FlaubertForQuestionAnswering,
         FlaubertForQuestionAnsweringSimple,
         FlaubertForSequenceClassification,
         FlaubertForTokenClassification,
         FlaubertForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 lowerCamelCase				       =(
     {
         '''feature-extraction''': FlaubertModel,
         '''fill-mask''': FlaubertWithLMHeadModel,
         '''question-answering''': FlaubertForQuestionAnsweringSimple,
         '''text-classification''': FlaubertForSequenceClassification,
         '''token-classification''': FlaubertForTokenClassification,
         '''zero-shot''': FlaubertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 def 		lowercase_(   self			:    Tuple				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    Union[str, 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 		lowercase_(   self			:    Optional[Any]				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    Union[str, Any]=False       ):



   """simple docstring"""





   __A		:     str												=		super()._prepare_for_class(UpperCAmelCase_				,   UpperCAmelCase_				,   return_labels=UpperCAmelCase_       )

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

   return inputs_dict
 def 		lowercase_(   self			:    Union[str, Any]       ):



   """simple docstring"""





   __A		:     str												=		FlaubertModelTester(self       )
   __A		:     int												=		ConfigTester(self				,   config_class=UpperCAmelCase_				,   emb_dim=37       )
 def 		lowercase_(   self			:    Optional[Any]       ):



   """simple docstring"""





   self.config_tester.run_common_tests()
 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   __A		:     str												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_model(*UpperCAmelCase_       )
 def 		lowercase_(   self			:    Any       ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_lm_head(*UpperCAmelCase_       )
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A		:     Dict												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_simple_qa(*UpperCAmelCase_       )
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A		:     int												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_qa(*UpperCAmelCase_       )
 def 		lowercase_(   self			:    Optional[Any]       ):



   """simple docstring"""





   __A		:     Tuple												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCAmelCase_       )
 def 		lowercase_(   self			:    Union[str, Any]       ):



   """simple docstring"""





   __A		:     Dict												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_token_classif(*UpperCAmelCase_       )
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A		:     Dict												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_multiple_choice(*UpperCAmelCase_       )
 @slow
 def 		lowercase_(   self			:    Union[str, Any]       ):



   """simple docstring"""





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






 @slow
 @require_torch_gpu
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A		:     List[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

     __A		:     str												=		True
     __A		:     List[Any]												=		model_class(config=UpperCAmelCase_       )

     __A		:     Tuple												=		self._prepare_for_class(UpperCAmelCase_				,   UpperCAmelCase_       )
     __A		:     Optional[int]												=		torch.jit.trace(
         UpperCAmelCase_				,   (inputs_dict["""input_ids"""].to("""cpu"""       ), inputs_dict["""attention_mask"""].to("""cpu"""       ))       )

     with tempfile.TemporaryDirectory() as tmp:
       torch.jit.save(UpperCAmelCase_				,   os.path.join(UpperCAmelCase_				,   """traced_model.pt"""       )       )
       __A		:     List[str]												=		torch.jit.load(os.path.join(UpperCAmelCase_				,   """traced_model.pt"""       )				,   map_location=UpperCAmelCase_       )
       loaded(inputs_dict["""input_ids"""].to(UpperCAmelCase_       )				,   inputs_dict["""attention_mask"""].to(UpperCAmelCase_       )       )







@require_torch
class  __A				( unittest.TestCase			):
 @slow
 def 		lowercase_(   self			:    List[Any]       ):



   """simple docstring"""





   __A		:     str												=		FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""       )
   __A		:     Dict												=		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():
     __A		:     str												=		model(UpperCAmelCase_       )[0]
   __A		:     Optional[int]												=		torch.Size((1, 11, 7_68)       )
   self.assertEqual(output.shape				,   UpperCAmelCase_       )
   __A		:     Optional[Any]												=		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]				,   UpperCAmelCase_				,   atol=1e-4       )       )





'''simple docstring'''
from collections.abc import Sequence







def        A_     (						__SCREAMING_SNAKE_CASE						:  Sequence[float]			,     __SCREAMING_SNAKE_CASE						:  bool = False )				->       float:





  """simple docstring"""

  if not arr:
    return 0

  __A		:     Any												=		0 if allow_empty_subarrays else float("""-inf""" )
  __A		:     List[Any]												=		0.0
  for num in arr:
    __A		:     Tuple												=		max(0 if allow_empty_subarrays else num			,     curr_sum + num )
    __A		:     Optional[int]												=		max(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE )

  return max_sum


if __name__ == "__main__":
     from doctest import testmod

     testmod()

     A__   :		Any 		=[-2, 1, -3, 4, -1, 2, 1, -5, 4]
     print(F"{max_subarray_sum(nums) = }")



