






'''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     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





 def __init__(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase=13      ,  __lowerCAmelCase=7      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=False      ,  __lowerCAmelCase=False      ,  __lowerCAmelCase=False      ,  __lowerCAmelCase=2      ,  __lowerCAmelCase=99      ,  __lowerCAmelCase=0      ,  __lowerCAmelCase=32      ,  __lowerCAmelCase=5      ,  __lowerCAmelCase=4      ,  __lowerCAmelCase=0.1      ,  __lowerCAmelCase=0.1      ,  __lowerCAmelCase=5_12      ,  __lowerCAmelCase=12      ,  __lowerCAmelCase=2      ,  __lowerCAmelCase=0.02      ,  __lowerCAmelCase=3      ,  __lowerCAmelCase=4      ,  __lowerCAmelCase="last"      ,  __lowerCAmelCase=None      ,  __lowerCAmelCase=None      ,  ):
       UpperCamelCase_					: int            =				parent
       UpperCamelCase_					: Any            =				batch_size
       UpperCamelCase_					: Optional[Any]            =				seq_length
       UpperCamelCase_					: Any            =				is_training
       UpperCamelCase_					: List[Any]            =				use_input_lengths
       UpperCamelCase_					: List[str]            =				use_token_type_ids
       UpperCamelCase_					: List[Any]            =				use_labels
       UpperCamelCase_					: Tuple            =				gelu_activation
       UpperCamelCase_					: int            =				sinusoidal_embeddings
       UpperCamelCase_					: Tuple            =				causal
       UpperCamelCase_					: Optional[Any]            =				asm
       UpperCamelCase_					: Dict            =				n_langs
       UpperCamelCase_					: Union[str, Any]            =				vocab_size
       UpperCamelCase_					: Optional[int]            =				n_special
       UpperCamelCase_					: int            =				hidden_size
       UpperCamelCase_					: Optional[Any]            =				num_hidden_layers
       UpperCamelCase_					: Union[str, Any]            =				num_attention_heads
       UpperCamelCase_					: Any            =				hidden_dropout_prob
       UpperCamelCase_					: Union[str, Any]            =				attention_probs_dropout_prob
       UpperCamelCase_					: int            =				max_position_embeddings
       UpperCamelCase_					: Dict            =				type_vocab_size
       UpperCamelCase_					: Any            =				type_sequence_label_size
       UpperCamelCase_					: str            =				initializer_range
       UpperCamelCase_					: int            =				num_labels
       UpperCamelCase_					: Any            =				num_choices
       UpperCamelCase_					: Union[str, Any]            =				summary_type
       UpperCamelCase_					: int            =				use_proj
       UpperCamelCase_					: List[str]            =				scope
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: int            =				ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size			)
       UpperCamelCase_					: List[Any]            =				random_attention_mask([self.batch_size, self.seq_length]			)

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

       UpperCamelCase_					: Optional[Any]            =				None
       if self.use_token_type_ids:
             UpperCamelCase_					: str            =				ids_tensor([self.batch_size, self.seq_length]      ,  self.n_langs			)

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

       UpperCamelCase_					: Any            =				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			):
       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      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: str            =				FlaubertModel(config=lowercase__			)
       model.to(lowercase__			)
       model.eval()
       UpperCamelCase_					: str            =				model(lowercase__      ,  lengths=lowercase__      ,  langs=lowercase__			)
       UpperCamelCase_					: Tuple            =				model(lowercase__      ,  langs=lowercase__			)
       UpperCamelCase_					: Tuple            =				model(lowercase__			)
       self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: Dict            =				FlaubertWithLMHeadModel(lowercase__			)
       model.to(lowercase__			)
       model.eval()

       UpperCamelCase_					: int            =				model(lowercase__      ,  token_type_ids=lowercase__      ,  labels=lowercase__			)
       self.parent.assertEqual(result.loss.shape      ,  ()			)
       self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.vocab_size)			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: Any            =				FlaubertForQuestionAnsweringSimple(lowercase__			)
       model.to(lowercase__			)
       model.eval()

       UpperCamelCase_					: Optional[Any]            =				model(lowercase__			)

       UpperCamelCase_					: Optional[int]            =				model(lowercase__      ,  start_positions=lowercase__      ,  end_positions=lowercase__			)
       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      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: Tuple            =				FlaubertForQuestionAnswering(lowercase__			)
       model.to(lowercase__			)
       model.eval()

       UpperCamelCase_					: Optional[int]            =				model(lowercase__			)

       UpperCamelCase_					: Dict            =				model(
           lowercase__      ,  start_positions=lowercase__      ,  end_positions=lowercase__      ,  cls_index=lowercase__      ,  is_impossible=lowercase__      ,  p_mask=lowercase__      ,  )

       UpperCamelCase_					: str            =				model(
           lowercase__      ,  start_positions=lowercase__      ,  end_positions=lowercase__      ,  cls_index=lowercase__      ,  is_impossible=lowercase__      ,  )

       ((UpperCamelCase_		)     ,      		)					: Tuple            =				result_with_labels.to_tuple()

       UpperCamelCase_					: Tuple            =				model(lowercase__      ,  start_positions=lowercase__      ,  end_positions=lowercase__			)

       ((UpperCamelCase_		)     ,      		)					: 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 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: str            =				FlaubertForSequenceClassification(lowercase__			)
       model.to(lowercase__			)
       model.eval()

       UpperCamelCase_					: int            =				model(lowercase__			)
       UpperCamelCase_					: str            =				model(lowercase__      ,  labels=lowercase__			)

       self.parent.assertEqual(result.loss.shape      ,  ()			)
       self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.type_sequence_label_size)			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: Optional[int]            =				self.num_labels
       UpperCamelCase_					: Optional[int]            =				FlaubertForTokenClassification(lowercase__			)
       model.to(lowercase__			)
       model.eval()

       UpperCamelCase_					: int            =				model(lowercase__      ,  attention_mask=lowercase__      ,  labels=lowercase__			)
       self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.num_labels)			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: List[Any]            =				self.num_choices
       UpperCamelCase_					: Optional[int]            =				FlaubertForMultipleChoice(config=lowercase__			)
       model.to(lowercase__			)
       model.eval()
       UpperCamelCase_					: int            =				input_ids.unsqueeze(1			).expand(-1      ,  self.num_choices      ,  -1			).contiguous()
       UpperCamelCase_					: Union[str, Any]            =				token_type_ids.unsqueeze(1			).expand(-1      ,  self.num_choices      ,  -1			).contiguous()
       UpperCamelCase_					: List[Any]            =				input_mask.unsqueeze(1			).expand(-1      ,  self.num_choices      ,  -1			).contiguous()
       UpperCamelCase_					: Union[str, Any]            =				model(
           lowercase__      ,  attention_mask=lowercase__      ,  token_type_ids=lowercase__      ,  labels=lowercase__      ,  )
       self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.num_choices)			)





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Union[str, Any]            =				self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      (
           UpperCamelCase_		
       )     ,      		
       )					: List[str]            =				config_and_inputs
       UpperCamelCase_					: List[str]            =				{
           """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     (   SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, unittest.TestCase    ):






 """simple docstring"""





 __a      :    Union[str, Any]			  =    (
     (
         FlaubertModel,
         FlaubertWithLMHeadModel,
         FlaubertForQuestionAnswering,
         FlaubertForQuestionAnsweringSimple,
         FlaubertForSequenceClassification,
         FlaubertForTokenClassification,
         FlaubertForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 __a      :    List[str]			  =    (
     {
         '''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      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase			):
       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      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase=False			):
       UpperCamelCase_					: str            =				super()._prepare_for_class(lowercase__      ,  lowercase__      ,  return_labels=lowercase__			)

       if return_labels:
             if model_class.__name__ == "FlaubertForQuestionAnswering":
                   UpperCamelCase_					: Optional[Any]            =				torch.zeros(
                       self.model_tester.batch_size      ,  dtype=torch.long      ,  device=lowercase__			)
                   UpperCamelCase_					: Tuple            =				torch.zeros(
                       self.model_tester.batch_size      ,  dtype=torch.long      ,  device=lowercase__			)

       return inputs_dict
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: str            =				FlaubertModelTester(self			)
       UpperCamelCase_					: Union[str, Any]            =				ConfigTester(self      ,  config_class=lowercase__      ,  emb_dim=37			)
 def 	_UpperCAmelCase							(				self			):
       self.config_tester.run_common_tests()
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Optional[int]            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_model(*lowercase__			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Dict            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_lm_head(*lowercase__			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: str            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_simple_qa(*lowercase__			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: str            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_qa(*lowercase__			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Union[str, Any]            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase__			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Union[str, Any]            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_token_classif(*lowercase__			)
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Optional[Any]            =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase__			)
 @slow
 def 	_UpperCAmelCase							(				self			):
       for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase_					: List[str]            =				FlaubertModel.from_pretrained(lowercase__			)
             self.assertIsNotNone(lowercase__			)





 @slow
 @require_torch_gpu
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_     ,      UpperCamelCase_					: int            =				self.model_tester.prepare_config_and_inputs_for_common()
       for model_class in self.all_model_classes:
             # FlauBertForMultipleChoice behaves incorrectly in JIT environments.
             if model_class == FlaubertForMultipleChoice:
                   return

             UpperCamelCase_					: Tuple            =				True
             UpperCamelCase_					: Union[str, Any]            =				model_class(config=lowercase__			)

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

             with tempfile.TemporaryDirectory() as tmp:
                   torch.jit.save(lowercase__      ,  os.path.join(lowercase__      ,  """traced_model.pt"""			)			)
                   UpperCamelCase_					: Optional[int]            =				torch.jit.load(os.path.join(lowercase__      ,  """traced_model.pt"""			)      ,  map_location=lowercase__			)
                   loaded(inputs_dict["""input_ids"""].to(lowercase__			)      ,  inputs_dict["""attention_mask"""].to(lowercase__			)			)






@require_torch
class      A     (   unittest.TestCase    ):






 """simple docstring"""





 @slow
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Optional[int]            =				FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""			)
       UpperCamelCase_					: Any            =				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():
             UpperCamelCase_					: Dict            =				model(lowercase__			)[0]
       UpperCamelCase_					: List[str]            =				torch.Size((1, 11, 7_68)			)
       self.assertEqual(output.shape      ,  lowercase__			)
       UpperCamelCase_					: List[Any]            =				torch.tensor(
           [[[-2.62_51, -1.42_98, -0.02_27], [-2.85_10, -1.63_87, 0.22_58], [-2.81_14, -1.18_32, -0.30_66]]]			)

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







from __future__ import annotations

from random import choice






def _lowerCamelCase  (		snake_case  ):
							return choice(snake_case  )






def _lowerCamelCase  (		snake_case     , snake_case  ):
							_lowerCAmelCase							=							random_pivot(snake_case  )

							# partition based on pivot
							# linear time
							_lowerCAmelCase							=							[e for e in lst if e < pivot]
							_lowerCAmelCase							=							[e for e in lst if e > pivot]

							# if we get lucky, pivot might be the element we want.
							# we can easily see this:
							# small (elements smaller than k)
							# + pivot (kth element)
							# + big (elements larger than k)
							if len(snake_case  ) == k - 1:
														return pivot
							# pivot is in elements bigger than k
							elif len(snake_case  ) < k - 1:
														return kth_number(snake_case     , k - len(snake_case  ) - 1  )
							# pivot is in elements smaller than k
							else:
														return kth_number(snake_case     , snake_case  )


if __name__ == "__main__":
				import doctest

				doctest.testmod()






