






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




      """simple docstring"""




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


       '''simple docstring'''

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






      def 				A__      ( self			:		Optional[int]		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size		)
       lowercase					:   str				  =random_attention_mask([self.batch_size, self.seq_length]		)

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

       lowercase					:   Dict				  =None
       if self.use_token_type_ids:
        lowercase					:   List[Any]				  =ids_tensor([self.batch_size, self.seq_length]     ,      self.n_langs		)

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

       lowercase					:   List[str]				  =self.get_config()

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






      def 				A__      ( self			:		Union[str, Any]		)  ->  Optional[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 				A__      ( self			:		Tuple     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		int     ,      )  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   List[str]				  =FlaubertModel(config=UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase					:   Optional[Any]				  =model(UpperCAmelCase     ,      lengths=UpperCAmelCase     ,      langs=UpperCAmelCase		)
       lowercase					:   Any				  =model(UpperCAmelCase     ,      langs=UpperCAmelCase		)
       lowercase					:   Tuple				  =model(UpperCAmelCase		)
       self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)		)






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


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =FlaubertWithLMHeadModel(UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()

       lowercase					:   Any				  =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 				A__      ( self			:		Dict     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Union[str, Any]     ,      )  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   List[Any]				  =FlaubertForQuestionAnsweringSimple(UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()

       lowercase					:   List[Any]				  =model(UpperCAmelCase		)

       lowercase					:   Tuple				  =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 				A__      ( self			:		Optional[int]     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		int     ,      )  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =FlaubertForQuestionAnswering(UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()

       lowercase					:   Any				  =model(UpperCAmelCase		)

       lowercase					:   Optional[Any]				  =model(
           UpperCAmelCase     ,      start_positions=UpperCAmelCase     ,      end_positions=UpperCAmelCase     ,      cls_index=UpperCAmelCase     ,      is_impossible=UpperCAmelCase     ,      p_mask=UpperCAmelCase     ,      )

       lowercase					:   Dict				  =model(
           UpperCAmelCase     ,      start_positions=UpperCAmelCase     ,      end_positions=UpperCAmelCase     ,      cls_index=UpperCAmelCase     ,      is_impossible=UpperCAmelCase     ,      )

       (lowercase		)					:   Optional[int]				  =result_with_labels.to_tuple()

       lowercase					:   str				  =model(UpperCAmelCase     ,      start_positions=UpperCAmelCase     ,      end_positions=UpperCAmelCase		)

       (lowercase		)					:   Optional[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 				A__      ( self			:		str     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Union[str, Any]     ,      )  ->  int:


       '''simple docstring'''

       lowercase					:   Dict				  =FlaubertForSequenceClassification(UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()

       lowercase					:   Optional[Any]				  =model(UpperCAmelCase		)
       lowercase					:   Optional[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 				A__      ( self			:		Optional[int]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		List[str]     ,      )  ->  Dict:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =self.num_labels
       lowercase					:   Optional[int]				  =FlaubertForTokenClassification(UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()

       lowercase					:   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 				A__      ( self			:		Any     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Dict     ,      )  ->  List[Any]:


       '''simple docstring'''

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






      def 				A__      ( self			:		List[str]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =self.prepare_config_and_inputs()
       (
           lowercase		
       )					:   Tuple				  =config_and_inputs
       lowercase					:   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 UpperCAmelCase_					(    __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 				A__      ( self			:		Union[str, Any]     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Optional[Any]		)  ->  int:


       '''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 				A__      ( self			:		Optional[int]     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		str=False		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   Tuple				  =super()._prepare_for_class(UpperCAmelCase     ,      UpperCAmelCase     ,      return_labels=UpperCAmelCase		)

       if return_labels:
        if model_class.__name__ == "FlaubertForQuestionAnswering":
         lowercase					:   str				  =torch.zeros(
             self.model_tester.batch_size     ,      dtype=torch.long     ,      device=UpperCAmelCase		)
         lowercase					:   Dict				  =torch.zeros(
             self.model_tester.batch_size     ,      dtype=torch.long     ,      device=UpperCAmelCase		)

       return inputs_dict






      def 				A__      ( self			:		Dict		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =FlaubertModelTester(self		)
       lowercase					:   Optional[int]				  =ConfigTester(self     ,      config_class=UpperCAmelCase     ,      emb_dim=37		)






      def 				A__      ( self			:		List[Any]		)  ->  Optional[Any]:


       '''simple docstring'''

       self.config_tester.run_common_tests()






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


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_model(*UpperCAmelCase		)






      def 				A__      ( self			:		Any		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   List[str]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_lm_head(*UpperCAmelCase		)






      def 				A__      ( self			:		Any		)  ->  int:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_simple_qa(*UpperCAmelCase		)






      def 				A__      ( self			:		Tuple		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   int				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_qa(*UpperCAmelCase		)






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


       '''simple docstring'''

       lowercase					:   Optional[int]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCAmelCase		)






      def 				A__      ( self			:		List[Any]		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   Tuple				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_token_classif(*UpperCAmelCase		)






      def 				A__      ( self			:		List[Any]		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   int				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_multiple_choice(*UpperCAmelCase		)






      @slow
      def 				A__      ( self			:		List[str]		)  ->  List[Any]:


       '''simple docstring'''

       for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase					:   List[Any]				  =FlaubertModel.from_pretrained(UpperCAmelCase		)
        self.assertIsNotNone(UpperCAmelCase		)






      @slow
      @require_torch_gpu
      def 				A__      ( self			:		Union[str, Any]		)  ->  str:


       '''simple docstring'''

       lowercase					:   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

        lowercase					:   Optional[Any]				  =True
        lowercase					:   Any				  =model_class(config=UpperCAmelCase		)

        lowercase					:   Tuple				  =self._prepare_for_class(UpperCAmelCase     ,      UpperCAmelCase		)
        lowercase					:   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'''		)		)
         lowercase					:   Union[str, Any]				  =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 UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      @slow
      def 				A__      ( self			:		List[Any]		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   List[str]				  =FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''		)
       lowercase					:   Any				  =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]		)
       with torch.no_grad():
        lowercase					:   Union[str, Any]				  =model(UpperCAmelCase		)[0]
       lowercase					:   int				  =torch.Size((1, 11, 768)		)
       self.assertEqual(output.shape     ,      UpperCAmelCase		)
       lowercase					:   Union[str, Any]				  =torch.tensor(
           [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]		)

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











'''simple docstring'''



import re


def 							lowercase_				(    __A :       str			)       ->      bool:



 """simple docstring"""




 lowercase					:   Any				  =re.compile(R'''^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$'''			)
 if match := re.search(__A					,      __A			):
  return match.string == phone
 return False


if __name__ == "__main__":
      print(indian_phone_validator('+918827897895'))



