



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 	lowerCamelCase__	(       _A):



 """simple docstring"""

 def __init__(    self					:					List[str]			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					Union[str, Any]=13			,						__lowerCAmelCase					:					str=7			,						__lowerCAmelCase					:					int=True			,						__lowerCAmelCase					:					Optional[int]=True			,						__lowerCAmelCase					:					Dict=False			,						__lowerCAmelCase					:					Tuple=True			,						__lowerCAmelCase					:					Union[str, Any]=99			,						__lowerCAmelCase					:					Union[str, Any]=32			,						__lowerCAmelCase					:					Any=5			,						__lowerCAmelCase					:					List[Any]=4			,						__lowerCAmelCase					:					Union[str, Any]=37			,						__lowerCAmelCase					:					int="gelu"			,						__lowerCAmelCase					:					Optional[int]=0.1			,						__lowerCAmelCase					:					str=0.1			,						__lowerCAmelCase					:					Optional[Any]=5_12			,						__lowerCAmelCase					:					Optional[int]=16			,						__lowerCAmelCase					:					str=2			,						__lowerCAmelCase					:					Union[str, Any]=0.02			,						__lowerCAmelCase					:					int=3			,						__lowerCAmelCase					:					Any=4			,						__lowerCAmelCase					:					Optional[int]=None			,						)   ->	Union[str, Any]:
    _A      =       parent
    _A      =       batch_size
    _A      =       seq_length
    _A      =       is_training
    _A      =       use_input_mask
    _A      =       use_token_type_ids
    _A      =       use_labels
    _A      =       vocab_size
    _A      =       hidden_size
    _A      =       num_hidden_layers
    _A      =       num_attention_heads
    _A      =       intermediate_size
    _A      =       hidden_act
    _A      =       hidden_dropout_prob
    _A      =       attention_probs_dropout_prob
    _A      =       max_position_embeddings
    _A      =       type_vocab_size
    _A      =       type_sequence_label_size
    _A      =       initializer_range
    _A      =       num_labels
    _A      =       num_choices
    _A      =       scope

 def     snake_case_      (    self					:					Tuple      )   ->	Union[str, Any]:
    _A      =       ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size      )

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

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

    _A      =       self.get_config()

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

 def     snake_case_      (    self					:					Any      )   ->	Union[str, Any]:
    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     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Union[str, Any]      )   ->	Dict:
    _A      =       DistilBertModel(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(__lowerCAmelCase			,						__lowerCAmelCase      )
    _A      =       model(__lowerCAmelCase      )
    self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)      )

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					Any      )   ->	Any:
    _A      =       DistilBertForMaskedLM(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						labels=__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.vocab_size)      )

 def     snake_case_      (    self					:					int			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					List[Any]      )   ->	List[Any]:
    _A      =       DistilBertForQuestionAnswering(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(
        __lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						start_positions=__lowerCAmelCase			,						end_positions=__lowerCAmelCase      )
    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     snake_case_      (    self					:					Tuple			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Any      )   ->	Dict:
    _A      =       self.num_labels
    _A      =       DistilBertForSequenceClassification(__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       model(__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						labels=__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)      )

 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					List[str]      )   ->	Tuple:
    _A      =       self.num_labels
    _A      =       DistilBertForTokenClassification(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()

    _A      =       model(__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						labels=__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.num_labels)      )

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					List[str]      )   ->	Union[str, Any]:
    _A      =       self.num_choices
    _A      =       DistilBertForMultipleChoice(config=__lowerCAmelCase      )
    model.to(__lowerCAmelCase      )
    model.eval()
    _A      =       input_ids.unsqueeze(1      ).expand(-1			,						self.num_choices			,						-1      ).contiguous()
    _A      =       input_mask.unsqueeze(1      ).expand(-1			,						self.num_choices			,						-1      ).contiguous()
    _A      =       model(
        __lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						labels=__lowerCAmelCase			,						)
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_choices)      )

 def     snake_case_      (    self					:					Optional[int]      )   ->	Union[str, Any]:
    _A      =       self.prepare_config_and_inputs()
    ((_A)  ,    (_A)  ,    (_A)  ,    (_A)  ,    (_A)  ,    (_A))      =       config_and_inputs
    _A      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
    return config, inputs_dict


@require_torch
class 	lowerCamelCase__	(       _A							,  _A							,  unittest.TestCase):



 """simple docstring"""

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

 def     snake_case_      (    self					:					List[str]      )   ->	Tuple:
    _A      =       DistilBertModelTester(self      )
    _A      =       ConfigTester(self			,						config_class=__lowerCAmelCase			,						dim=37      )

 def     snake_case_      (    self					:					List[str]      )   ->	str:
    self.config_tester.run_common_tests()

 def     snake_case_      (    self					:					Any      )   ->	str:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_model(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Optional[Any]      )   ->	List[str]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_masked_lm(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Dict      )   ->	int:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_question_answering(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Optional[Any]      )   ->	Union[str, Any]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_sequence_classification(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Dict      )   ->	Optional[int]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_token_classification(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Union[str, Any]      )   ->	Any:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_multiple_choice(*__lowerCAmelCase      )

 @slow
 def     snake_case_      (    self					:					Optional[int]      )   ->	Dict:
    for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _A      =       DistilBertModel.from_pretrained(__lowerCAmelCase      )
       self.assertIsNotNone(__lowerCAmelCase      )

 @slow
 @require_torch_gpu
 def     snake_case_      (    self					:					Any      )   ->	str:
    _A  ,    _A      =       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

       _A      =       True
       _A      =       model_class(config=__lowerCAmelCase      )

       _A      =       self._prepare_for_class(__lowerCAmelCase			,						__lowerCAmelCase      )
       _A      =       torch.jit.trace(
           __lowerCAmelCase			,						(inputs_dict['''input_ids'''].to('''cpu'''      ), inputs_dict['''attention_mask'''].to('''cpu'''      ))      )

       with tempfile.TemporaryDirectory() as tmp:
          torch.jit.save(__lowerCAmelCase			,						os.path.join(__lowerCAmelCase			,						'''traced_model.pt'''      )      )
          _A      =       torch.jit.load(os.path.join(__lowerCAmelCase			,						'''traced_model.pt'''      )			,						map_location=__lowerCAmelCase      )
          loaded(inputs_dict['''input_ids'''].to(__lowerCAmelCase      )			,						inputs_dict['''attention_mask'''].to(__lowerCAmelCase      )      )


@require_torch
class 	lowerCamelCase__	(       unittest.TestCase):



 """simple docstring"""

 @slow
 def     snake_case_      (    self					:					Optional[Any]      )   ->	int:
    _A      =       DistilBertModel.from_pretrained('''distilbert-base-uncased'''      )
    _A      =       torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]      )
    _A      =       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
    with torch.no_grad():
       _A      =       model(__lowerCAmelCase			,						attention_mask=__lowerCAmelCase      )[0]
    _A      =       torch.Size((1, 11, 7_68)      )
    self.assertEqual(output.shape			,						__lowerCAmelCase      )
    _A      =       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]			,						__lowerCAmelCase			,						atol=1E-4      )      )





import math





def      lowerCamelCase_					(							lowerCamelCase__       ):

				if 1 < number < 4:
								# 2 and 3 are primes
								return True
				elif number < 2 or number % 2 == 0 or number % 3 == 0:
								# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
								return False

				# All primes number are in format of 6k +/- 1
				for i in range(5      ,					int(math.sqrt(lowerCamelCase__       ) + 1       )      ,					6       ):
								if number % i == 0 or number % (i + 2) == 0:
												return False
				return True





def      lowerCamelCase_					(							lowerCamelCase__ = 1_0_0_0_1       ):

				try:
								lowerCamelCase_         =     int(lowerCamelCase__       )
				except (TypeError, ValueError):
								raise TypeError("Parameter nth must be int or castable to int."       ) from None
				if nth <= 0:
								raise ValueError("Parameter nth must be greater than or equal to one."       )
				lowerCamelCase_         =     []
				lowerCamelCase_         =     2
				while len(lowerCamelCase__       ) < nth:
								if is_prime(lowerCamelCase__       ):
												primes.append(lowerCamelCase__       )
												num += 1
								else:
												num += 1
				return primes[len(lowerCamelCase__       ) - 1]


if __name__ == "__main__":
			print(F"""{solution() = }""")


