




'''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 			_a  (	__a ):
       def __init__(	self						:	List[str]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	int=13   ,				lowercase						:	Dict=7   ,				lowercase						:	Any=True   ,				lowercase						:	int=True   ,				lowercase						:	Optional[int]=False   ,				lowercase						:	List[str]=True   ,				lowercase						:	str=99   ,				lowercase						:	int=32   ,				lowercase						:	Tuple=5   ,				lowercase						:	List[str]=4   ,				lowercase						:	Optional[Any]=37   ,				lowercase						:	Optional[int]="gelu"   ,				lowercase						:	Optional[int]=0.1   ,				lowercase						:	str=0.1   ,				lowercase						:	Union[str, Any]=512   ,				lowercase						:	List[str]=16   ,				lowercase						:	Optional[Any]=2   ,				lowercase						:	str=0.02   ,				lowercase						:	str=3   ,				lowercase						:	List[Any]=4   ,				lowercase						:	Union[str, Any]=None   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		parent
           UpperCAmelCase			       =		batch_size
           UpperCAmelCase			       =		seq_length
           UpperCAmelCase			       =		is_training
           UpperCAmelCase			       =		use_input_mask
           UpperCAmelCase			       =		use_token_type_ids
           UpperCAmelCase			       =		use_labels
           UpperCAmelCase			       =		vocab_size
           UpperCAmelCase			       =		hidden_size
           UpperCAmelCase			       =		num_hidden_layers
           UpperCAmelCase			       =		num_attention_heads
           UpperCAmelCase			       =		intermediate_size
           UpperCAmelCase			       =		hidden_act
           UpperCAmelCase			       =		hidden_dropout_prob
           UpperCAmelCase			       =		attention_probs_dropout_prob
           UpperCAmelCase			       =		max_position_embeddings
           UpperCAmelCase			       =		type_vocab_size
           UpperCAmelCase			       =		type_sequence_label_size
           UpperCAmelCase			       =		initializer_range
           UpperCAmelCase			       =		num_labels
           UpperCAmelCase			       =		num_choices
           UpperCAmelCase			       =		scope
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

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

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

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

           UpperCAmelCase			       =		self.get_config()

           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
       def     A      (	self						:	List[str]				):



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



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertModel(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase   ,				lowercase				)
           UpperCAmelCase			       =		model(lowercase				)
           self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)				)
       def     A      (	self						:	Any   ,				lowercase						:	Dict   ,				lowercase						:	List[str]   ,				lowercase						:	List[Any]   ,				lowercase						:	Tuple   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	int				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertForMaskedLM(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase   ,				labels=lowercase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)				)
       def     A      (	self						:	Tuple   ,				lowercase						:	int   ,				lowercase						:	Tuple   ,				lowercase						:	Optional[Any]   ,				lowercase						:	str   ,				lowercase						:	List[Any]   ,				lowercase						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertForQuestionAnswering(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(
               lowercase   ,				attention_mask=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     A      (	self						:	Tuple   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	List[str]   ,				lowercase						:	Optional[int]   ,				lowercase						:	Optional[int]   ,				lowercase						:	int				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_labels
           UpperCAmelCase			       =		DistilBertForSequenceClassification(lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase   ,				labels=lowercase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)				)
       def     A      (	self						:	List[str]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Tuple   ,				lowercase						:	Any   ,				lowercase						:	str   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_labels
           UpperCAmelCase			       =		DistilBertForTokenClassification(config=lowercase				)
           model.to(lowercase				)
           model.eval()

           UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase   ,				labels=lowercase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.num_labels)				)
       def     A      (	self						:	Any   ,				lowercase						:	Tuple   ,				lowercase						:	List[str]   ,				lowercase						:	Dict   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Any   ,				lowercase						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_choices
           UpperCAmelCase			       =		DistilBertForMultipleChoice(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		input_ids.unsqueeze(1				).expand(-1   ,				self.num_choices   ,				-1				).contiguous()
           UpperCAmelCase			       =		input_mask.unsqueeze(1				).expand(-1   ,				self.num_choices   ,				-1				).contiguous()
           UpperCAmelCase			       =		model(
               lowercase   ,				attention_mask=lowercase   ,				labels=lowercase   ,				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices)				)







       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.prepare_config_and_inputs()
           ((UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase))			       =		config_and_inputs
           UpperCAmelCase			       =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict






@require_torch
class 			_a  (	__a							,							__a							,							unittest.TestCase ):
       __a				:     int   				=					(
           (
               DistilBertModel,
               DistilBertForMaskedLM,
               DistilBertForMultipleChoice,
               DistilBertForQuestionAnswering,
               DistilBertForSequenceClassification,
               DistilBertForTokenClassification,
           )
           if is_torch_available()
           else None
       )
       __a				:     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				:     List[str]   				=					True
       __a				:     Optional[int]   				=					True
       __a				:     Optional[Any]   				=					True
       __a				:     Union[str, Any]   				=					True
       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertModelTester(self				)
           UpperCAmelCase			       =		ConfigTester(self   ,				config_class=lowercase   ,				dim=37				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

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



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_model(*lowercase				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase				)
       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase				)
       @slow
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase			       =		DistilBertModel.from_pretrained(lowercase				)
               self.assertIsNotNone(lowercase				)







       @slow
       @require_torch_gpu
       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		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

               UpperCAmelCase			       =		True
               UpperCAmelCase			       =		model_class(config=lowercase				)

               UpperCAmelCase			       =		self._prepare_for_class(lowercase   ,				lowercase				)
               UpperCAmelCase			       =		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			       =		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 ):
       @slow
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertModel.from_pretrained('''distilbert-base-uncased'''				)
           UpperCAmelCase			       =		torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
           UpperCAmelCase			       =		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
           with torch.no_grad():
               UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase				)[0]
           UpperCAmelCase			       =		torch.Size((1, 11, 768)				)
           self.assertEqual(output.shape   ,				lowercase				)
           UpperCAmelCase			       =		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]   ,				lowercase   ,				atol=1E-4				)				)










'''simple docstring'''







def 						snake_case_							(_a	:							str    ,   _a	:							str     ):
    UpperCAmelCase			       =		len(_a     ) + 1
    UpperCAmelCase			       =		len(_a     ) + 1

    # dp is a 2d matrix where dp[i][j] denotes whether prefix string of
    # length i of input_string matches with prefix string of length j of
    # given pattern.
    # "dp" stands for dynamic programming.
    UpperCAmelCase			       =		[[0 for i in range(_a     )] for j in range(_a     )]

    # since string of zero length match pattern of zero length
    UpperCAmelCase			       =		1

    # since pattern of zero length will never match with string of non-zero length
    for i in range(1    ,   _a     ):
        UpperCAmelCase			       =		0

    # since string of zero length will match with pattern where there
    # is at least one * alternatively
    for j in range(1    ,   _a     ):
        UpperCAmelCase			       =		dp[0][j - 2] if pattern[j - 1] == '''*''' else 0

    # now using bottom-up approach to find for all remaining lengths
    for i in range(1    ,   _a     ):
        for j in range(1    ,   _a     ):
            if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".":
                UpperCAmelCase			       =		dp[i - 1][j - 1]

            elif pattern[j - 1] == "*":
                if dp[i][j - 2] == 1:
                    UpperCAmelCase			       =		1
                elif pattern[j - 2] in (input_string[i - 1], "."):
                    UpperCAmelCase			       =		dp[i - 1][j]
                else:
                    UpperCAmelCase			       =		0
            else:
                UpperCAmelCase			       =		0

    return bool(dp[-1][-1]     )


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   # inputing the strings
   # input_string = input("input a string :")
   # pattern = input("input a pattern :")

   A     ='aab'
   A     ='c*a*b'

   # using function to check whether given string matches the given pattern
   if match_pattern(input_string, pattern):
      print(f"""{input_string} matches the given pattern {pattern}""")
   else:
      print(f"""{input_string} does not match with the given pattern {pattern}""")




