





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


      def __init__(self       :      int				,			a__       :      Optional[int]				,			a__       :      str=13				,			a__       :      Tuple=7				,			a__       :      Optional[int]=True				,			a__       :      Any=True				,			a__       :      Optional[Any]=False				,			a__       :      Optional[int]=True				,			a__       :      Optional[Any]=99				,			a__       :      List[Any]=32				,			a__       :      Optional[int]=5				,			a__       :      Optional[int]=4				,			a__       :      Dict=37				,			a__       :      Optional[Any]="gelu"				,			a__       :      Optional[int]=0.1				,			a__       :      int=0.1				,			a__       :      List[Any]=512				,			a__       :      Dict=16				,			a__       :      List[str]=2				,			a__       :      Dict=0.0_2				,			a__       :      Dict=3				,			a__       :      Dict=4				,			a__       :      Optional[Any]=None				,			):





         """simple docstring"""



         __snake_case							       =   parent
         __snake_case							       =   batch_size
         __snake_case							       =   seq_length
         __snake_case							       =   is_training
         __snake_case							       =   use_input_mask
         __snake_case							       =   use_token_type_ids
         __snake_case							       =   use_labels
         __snake_case							       =   vocab_size
         __snake_case							       =   hidden_size
         __snake_case							       =   num_hidden_layers
         __snake_case							       =   num_attention_heads
         __snake_case							       =   intermediate_size
         __snake_case							       =   hidden_act
         __snake_case							       =   hidden_dropout_prob
         __snake_case							       =   attention_probs_dropout_prob
         __snake_case							       =   max_position_embeddings
         __snake_case							       =   type_vocab_size
         __snake_case							       =   type_sequence_label_size
         __snake_case							       =   initializer_range
         __snake_case							       =   num_labels
         __snake_case							       =   num_choices
         __snake_case							       =   scope


      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



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

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

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

         __snake_case							       =   self.get_config()

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


      def    a (self       :      Optional[int]      ):





         """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       :      str				,			a__       :      Optional[int]				,			a__       :      Optional[int]				,			a__       :      Union[str, Any]				,			a__       :      Dict				,			a__       :      Optional[Any]				,			a__       :      List[Any]      ):





         """simple docstring"""



         __snake_case							       =   DistilBertModel(config=a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(a__				,			a__      )
         __snake_case							       =   model(a__      )
         self.parent.assertEqual(result.last_hidden_state.shape				,			(self.batch_size, self.seq_length, self.hidden_size)      )


      def    a (self       :      List[str]				,			a__       :      Any				,			a__       :      Union[str, Any]				,			a__       :      List[str]				,			a__       :      Optional[Any]				,			a__       :      Dict				,			a__       :      Union[str, Any]      ):





         """simple docstring"""



         __snake_case							       =   DistilBertForMaskedLM(config=a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(a__				,			attention_mask=a__				,			labels=a__      )
         self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.seq_length, self.vocab_size)      )


      def    a (self       :      str				,			a__       :      Union[str, Any]				,			a__       :      Any				,			a__       :      Any				,			a__       :      List[str]				,			a__       :      Tuple				,			a__       :      Optional[int]      ):





         """simple docstring"""



         __snake_case							       =   DistilBertForQuestionAnswering(config=a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   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    a (self       :      Dict				,			a__       :      Any				,			a__       :      List[Any]				,			a__       :      Tuple				,			a__       :      Tuple				,			a__       :      Tuple				,			a__       :      Tuple      ):





         """simple docstring"""



         __snake_case							       =   self.num_labels
         __snake_case							       =   DistilBertForSequenceClassification(a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(a__				,			attention_mask=a__				,			labels=a__      )
         self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.num_labels)      )


      def    a (self       :      Tuple				,			a__       :      Any				,			a__       :      List[str]				,			a__       :      Optional[int]				,			a__       :      List[str]				,			a__       :      List[Any]				,			a__       :      Tuple      ):





         """simple docstring"""



         __snake_case							       =   self.num_labels
         __snake_case							       =   DistilBertForTokenClassification(config=a__      )
         model.to(a__      )
         model.eval()

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


      def    a (self       :      List[str]				,			a__       :      str				,			a__       :      str				,			a__       :      Union[str, Any]				,			a__       :      int				,			a__       :      Dict				,			a__       :      int      ):





         """simple docstring"""



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


      def    a (self       :      Optional[int]      ):





         """simple docstring"""



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






@require_torch
class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase						,      _UpperCAmelCase						,      unittest.TestCase  ):
      A_						:				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_						:				Any							=    True
      A_						:				int							=    True
      A_						:				Dict							=    True
      A_						:				Dict							=    True


      def    a (self       :      Any      ):





         """simple docstring"""



         __snake_case							       =   DistilBertModelTester(self      )
         __snake_case							       =   ConfigTester(self				,			config_class=a__				,			dim=37      )


      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



         self.config_tester.run_common_tests()


      def    a (self       :      int      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_model(*a__      )


      def    a (self       :      Union[str, Any]      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_masked_lm(*a__      )


      def    a (self       :      int      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_question_answering(*a__      )


      def    a (self       :      Any      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_sequence_classification(*a__      )


      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_token_classification(*a__      )


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_multiple_choice(*a__      )


      @slow
      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



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


      @slow
      @require_torch_gpu
      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



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

            __snake_case							       =   True
            __snake_case							       =   model_class(config=a__      )

            __snake_case							       =   self._prepare_for_class(a__				,			a__      )
            __snake_case							       =   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'''      )      )
               __snake_case							       =   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  ):


      @slow
      def    a (self       :      str      ):





         """simple docstring"""



         __snake_case							       =   DistilBertModel.from_pretrained('''distilbert-base-uncased'''      )
         __snake_case							       =   torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
         __snake_case							       =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
         with torch.no_grad():
            __snake_case							       =   model(a__				,			attention_mask=a__      )[0]
         __snake_case							       =   torch.Size((1, 11, 768)      )
         self.assertEqual(output.shape				,			a__      )
         __snake_case							       =   torch.tensor(
             [[[-0.1_6_3_9, 0.3_2_9_9, 0.1_6_4_8], [-0.1_7_4_6, 0.3_2_8_9, 0.1_7_1_0], [-0.1_8_8_4, 0.3_3_5_7, 0.1_8_1_0]]]      )

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








from math import loga






def lowerCamelCase__    (							snake_case_	:  int       )      ->				int:
   if a < 0:
      raise ValueError('''Input value must be a positive integer'''       )
   elif isinstance(snake_case_       ,      snake_case_       ):
      raise TypeError('''Input value must be a \'int\' type'''       )
   return 0 if (a == 0) else int(loga(a & -a       )       )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

