


"""simple docstring"""







import os
import tempfile
import unittest

from transformers import DistilBertConfig, 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 (
         DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
         DistilBertForMaskedLM,
         DistilBertForMultipleChoice,
         DistilBertForQuestionAnswering,
         DistilBertForSequenceClassification,
         DistilBertForTokenClassification,
         DistilBertModel,
     )


class  SCREAMING_SNAKE_CASE__							(						_UpperCamelCase							):
       """simple docstring"""


       def __init__(		self						,	snake_case__						,	snake_case__=13						,	snake_case__=7						,	snake_case__=True						,	snake_case__=True						,	snake_case__=False						,	snake_case__=True						,	snake_case__=99						,	snake_case__=32						,	snake_case__=5						,	snake_case__=4						,	snake_case__=37						,	snake_case__="gelu"						,	snake_case__=0.1						,	snake_case__=0.1						,	snake_case__=512						,	snake_case__=16						,	snake_case__=2						,	snake_case__=0.02						,	snake_case__=3						,	snake_case__=4						,	snake_case__=None						,	):






         """simple docstring"""





         lowerCAmelCase	:	str								=				parent
         lowerCAmelCase	:	str								=				batch_size
         lowerCAmelCase	:	int								=				seq_length
         lowerCAmelCase	:	Union[str, Any]								=				is_training
         lowerCAmelCase	:	Optional[Any]								=				use_input_mask
         lowerCAmelCase	:	List[Any]								=				use_token_type_ids
         lowerCAmelCase	:	Optional[int]								=				use_labels
         lowerCAmelCase	:	List[str]								=				vocab_size
         lowerCAmelCase	:	int								=				hidden_size
         lowerCAmelCase	:	Optional[Any]								=				num_hidden_layers
         lowerCAmelCase	:	Tuple								=				num_attention_heads
         lowerCAmelCase	:	List[str]								=				intermediate_size
         lowerCAmelCase	:	Any								=				hidden_act
         lowerCAmelCase	:	str								=				hidden_dropout_prob
         lowerCAmelCase	:	Dict								=				attention_probs_dropout_prob
         lowerCAmelCase	:	List[Any]								=				max_position_embeddings
         lowerCAmelCase	:	Any								=				type_vocab_size
         lowerCAmelCase	:	List[str]								=				type_sequence_label_size
         lowerCAmelCase	:	Optional[int]								=				initializer_range
         lowerCAmelCase	:	List[Any]								=				num_labels
         lowerCAmelCase	:	Dict								=				num_choices
         lowerCAmelCase	:	Optional[Any]								=				scope

       def 			lowercase__				(		self		):






         """simple docstring"""





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

         lowerCAmelCase	:	int								=				None
         if self.use_input_mask:
           lowerCAmelCase	:	int								=				random_attention_mask([self.batch_size, self.seq_length]		)

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

         lowerCAmelCase	:	str								=				self.get_config()

         return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

       def 			lowercase__				(		self		):






         """simple docstring"""





         return DistilBertConfig(
             vocab_size=self.vocab_size						,	dim=self.hidden_size						,	n_layers=self.num_hidden_layers						,	n_heads=self.num_attention_heads						,	hidden_dim=self.intermediate_size						,	hidden_act=self.hidden_act						,	dropout=self.hidden_dropout_prob						,	attention_dropout=self.attention_probs_dropout_prob						,	max_position_embeddings=self.max_position_embeddings						,	initializer_range=self.initializer_range						,	)

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	Optional[int]								=				DistilBertModel(config=__a		)
         model.to(__a		)
         model.eval()
         lowerCAmelCase	:	Optional[Any]								=				model(__a						,	__a		)
         lowerCAmelCase	:	int								=				model(__a		)
         self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)		)

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	List[str]								=				DistilBertForMaskedLM(config=__a		)
         model.to(__a		)
         model.eval()
         lowerCAmelCase	:	List[Any]								=				model(__a						,	attention_mask=__a						,	labels=__a		)
         self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)		)

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	Union[str, Any]								=				DistilBertForQuestionAnswering(config=__a		)
         model.to(__a		)
         model.eval()
         lowerCAmelCase	:	Tuple								=				model(
             __a						,	attention_mask=__a						,	start_positions=__a						,	end_positions=__a		)
         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						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	str								=				self.num_labels
         lowerCAmelCase	:	Optional[int]								=				DistilBertForSequenceClassification(__a		)
         model.to(__a		)
         model.eval()
         lowerCAmelCase	:	Tuple								=				model(__a						,	attention_mask=__a						,	labels=__a		)
         self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)		)

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	Union[str, Any]								=				self.num_labels
         lowerCAmelCase	:	Dict								=				DistilBertForTokenClassification(config=__a		)
         model.to(__a		)
         model.eval()

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

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__						,	snake_case__		):






         """simple docstring"""





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







       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Union[str, Any]								=				self.prepare_config_and_inputs()
         (lowerCAmelCase)	:	List[str]								=				config_and_inputs
         lowerCAmelCase	:	Any								=				{"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict


@require_torch
class  SCREAMING_SNAKE_CASE__							(						_UpperCamelCase  ,       _UpperCamelCase  ,       unittest.TestCase							):
       """simple docstring"""


       a    :							Union[str, Any]  =(
           (
               DistilBertModel,
               DistilBertForMaskedLM,
               DistilBertForMultipleChoice,
               DistilBertForQuestionAnswering,
               DistilBertForSequenceClassification,
               DistilBertForTokenClassification,
           )
           if is_torch_available()
           else None
       )
       a    :							List[Any]  =(
           {
               "feature-extraction": DistilBertModel,
               "fill-mask": DistilBertForMaskedLM,
               "question-answering": DistilBertForQuestionAnswering,
               "text-classification": DistilBertForSequenceClassification,
               "token-classification": DistilBertForTokenClassification,
               "zero-shot": DistilBertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       a    :							Optional[Any]  =True
       a    :							List[str]  =True
       a    :							Dict  =True
       a    :							Dict  =True

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	int								=				DistilBertModelTester(self		)
         lowerCAmelCase	:	int								=				ConfigTester(self						,	config_class=__a						,	dim=37		)

       def 			lowercase__				(		self		):






         """simple docstring"""





         self.config_tester.run_common_tests()

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Union[str, Any]								=				self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_model(*__a		)

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Tuple								=				self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_masked_lm(*__a		)

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Optional[int]								=				self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_question_answering(*__a		)

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Union[str, Any]								=				self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_sequence_classification(*__a		)

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	List[Any]								=				self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_token_classification(*__a		)

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Dict								=				self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_multiple_choice(*__a		)

       @slow
       def 			lowercase__				(		self		):






         """simple docstring"""





         for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowerCAmelCase	:	Tuple								=				DistilBertModel.from_pretrained(__a		)
           self.assertIsNotNone(__a		)







       @slow
       @require_torch_gpu
       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Union[str, Any]								=				self.model_tester.prepare_config_and_inputs_for_common()
         for model_class in self.all_model_classes:
           # BertForMultipleChoice behaves incorrectly in JIT environments.
           if model_class == DistilBertForMultipleChoice:
             return

           lowerCAmelCase	:	Union[str, Any]								=				True
           lowerCAmelCase	:	str								=				model_class(config=__a		)

           lowerCAmelCase	:	Optional[int]								=				self._prepare_for_class(__a						,	__a		)
           lowerCAmelCase	:	Union[str, Any]								=				torch.jit.trace(
               __a						,	(inputs_dict["input_ids"].to("cpu"		), inputs_dict["attention_mask"].to("cpu"		))		)

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


@require_torch
class  SCREAMING_SNAKE_CASE__							(						unittest.TestCase							):
       """simple docstring"""


       @slow
       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	Optional[Any]								=				DistilBertModel.from_pretrained("distilbert-base-uncased"		)
         lowerCAmelCase	:	Tuple								=				torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
         lowerCAmelCase	:	str								=				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
         with torch.no_grad():
           lowerCAmelCase	:	str								=				model(__a						,	attention_mask=__a		)[0]
         lowerCAmelCase	:	Any								=				torch.Size((1, 11, 768)		)
         self.assertEqual(output.shape						,	__a		)
         lowerCAmelCase	:	int								=				torch.tensor(
             [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]		)

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












"""simple docstring"""


import re




def        lowercase__     (       lowercase_      )       ->		bool:




      """simple docstring"""

      _UpperCamelCase   :						List[Any]    =							re.compile(
          r"^(?:0|94|\+94|0{2}94)" r"7(0|1|2|4|5|6|7|8)" r"(-| |)" r"\d{7}$"      )

      return bool(re.search(lowercase_		,lowercase_      )      )


if __name__ == "__main__":
  lowerCamelCase__						= "0094702343221"

  print(is_sri_lankan_phone_number(phone))
