




'''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 		lowercase_   (			A       ):


   """simple docstring"""

   def __init__(   self :     Optional[int]  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     List[Any]=1_3  ,				__lowerCamelCase :     int=7  ,				__lowerCamelCase :     int=True  ,				__lowerCamelCase :     Dict=True  ,				__lowerCamelCase :     Union[str, Any]=False  ,				__lowerCamelCase :     List[str]=True  ,				__lowerCamelCase :     int=9_9  ,				__lowerCamelCase :     Optional[Any]=3_2  ,				__lowerCamelCase :     List[Any]=5  ,				__lowerCamelCase :     int=4  ,				__lowerCamelCase :     Optional[int]=3_7  ,				__lowerCamelCase :     Any="gelu"  ,				__lowerCamelCase :     Optional[int]=0.1  ,				__lowerCamelCase :     List[Any]=0.1  ,				__lowerCamelCase :     str=5_1_2  ,				__lowerCamelCase :     Optional[Any]=1_6  ,				__lowerCamelCase :     int=2  ,				__lowerCamelCase :     Union[str, Any]=0.0_2  ,				__lowerCamelCase :     str=3  ,				__lowerCamelCase :     List[str]=4  ,				__lowerCamelCase :     str=None  ,				):

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



   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				ids_tensor([self.batch_size, self.seq_length]  ,				self.vocab_size   )

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

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

      _SCREAMING_SNAKE_CASE	=				self.get_config()

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



   def 							lowerCAmelCase_      (   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 							lowerCAmelCase_      (   self :     str  ,				__lowerCamelCase :     Union[str, Any]  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     Union[str, Any]  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				DistilBertModel(config=__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase   )
      self.parent.assertEqual(result.last_hidden_state.shape  ,				(self.batch_size, self.seq_length, self.hidden_size)   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]  ,				__lowerCamelCase :     int  ,				__lowerCamelCase :     Optional[int]  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				DistilBertForMaskedLM(config=__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				attention_mask=__lowerCamelCase  ,				labels=__lowerCamelCase   )
      self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.vocab_size)   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]  ,				__lowerCamelCase :     Dict  ,				__lowerCamelCase :     Optional[int]  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     Optional[int]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				DistilBertForQuestionAnswering(config=__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()
      _SCREAMING_SNAKE_CASE	=				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 							lowerCAmelCase_      (   self :     Optional[Any]  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     Dict  ,				__lowerCamelCase :     Optional[Any]  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     Dict  ,				__lowerCamelCase :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.num_labels
      _SCREAMING_SNAKE_CASE	=				DistilBertForSequenceClassification(__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				attention_mask=__lowerCamelCase  ,				labels=__lowerCamelCase   )
      self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.num_labels)   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]  ,				__lowerCamelCase :     int  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     Union[str, Any]  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     Tuple   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.num_labels
      _SCREAMING_SNAKE_CASE	=				DistilBertForTokenClassification(config=__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()

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



   def 							lowerCAmelCase_      (   self :     Union[str, Any]  ,				__lowerCamelCase :     int  ,				__lowerCamelCase :     Dict  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.num_choices
      _SCREAMING_SNAKE_CASE	=				DistilBertForMultipleChoice(config=__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()
      _SCREAMING_SNAKE_CASE	=				input_ids.unsqueeze(1   ).expand(-1  ,				self.num_choices  ,				-1   ).contiguous()
      _SCREAMING_SNAKE_CASE	=				input_mask.unsqueeze(1   ).expand(-1  ,				self.num_choices  ,				-1   ).contiguous()
      _SCREAMING_SNAKE_CASE	=				model(
          __lowerCamelCase  ,				attention_mask=__lowerCamelCase  ,				labels=__lowerCamelCase  ,				)
      self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.num_choices)   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.prepare_config_and_inputs()
      ((_SCREAMING_SNAKE_CASE),	(_SCREAMING_SNAKE_CASE),	(_SCREAMING_SNAKE_CASE),	(_SCREAMING_SNAKE_CASE),	(_SCREAMING_SNAKE_CASE),	(_SCREAMING_SNAKE_CASE))	=				config_and_inputs
      _SCREAMING_SNAKE_CASE	=				{"input_ids": input_ids, "attention_mask": input_mask}
      return config, inputs_dict







@require_torch
class 		lowercase_   (			A					,  A					,  unittest.TestCase       ):


   """simple docstring"""

   lowerCamelCase_      =						(
       (
           DistilBertModel,
           DistilBertForMaskedLM,
           DistilBertForMultipleChoice,
           DistilBertForQuestionAnswering,
           DistilBertForSequenceClassification,
           DistilBertForTokenClassification,
       )
       if is_torch_available()
       else None
   )
   lowerCamelCase_      =						(
       {
           '''feature-extraction''': DistilBertModel,
           '''fill-mask''': DistilBertForMaskedLM,
           '''question-answering''': DistilBertForQuestionAnswering,
           '''text-classification''': DistilBertForSequenceClassification,
           '''token-classification''': DistilBertForTokenClassification,
           '''zero-shot''': DistilBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   lowerCamelCase_      =						True
   lowerCamelCase_      =						True
   lowerCamelCase_      =						True
   lowerCamelCase_      =						True



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				DistilBertModelTester(self   )
      _SCREAMING_SNAKE_CASE	=				ConfigTester(self  ,				config_class=__lowerCamelCase  ,				dim=3_7   )



   def 							lowerCAmelCase_      (   self :     List[str]   ):

      """simple docstring"""
      self.config_tester.run_common_tests()



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*__lowerCamelCase   )



   @slow
   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _SCREAMING_SNAKE_CASE	=				DistilBertModel.from_pretrained(__lowerCamelCase   )
         self.assertIsNotNone(__lowerCamelCase   )



   @slow
   @require_torch_gpu
   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE,	_SCREAMING_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

         _SCREAMING_SNAKE_CASE	=				True
         _SCREAMING_SNAKE_CASE	=				model_class(config=__lowerCamelCase   )

         _SCREAMING_SNAKE_CASE	=				self._prepare_for_class(__lowerCamelCase  ,				__lowerCamelCase   )
         _SCREAMING_SNAKE_CASE	=				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"   )   )
            _SCREAMING_SNAKE_CASE	=				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 		lowercase_   (			unittest.TestCase       ):


   """simple docstring"""

   @slow
   def 							lowerCAmelCase_      (   self :     Optional[int]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				DistilBertModel.from_pretrained("distilbert-base-uncased"   )
      _SCREAMING_SNAKE_CASE	=				torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]   )
      _SCREAMING_SNAKE_CASE	=				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
      with torch.no_grad():
         _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				attention_mask=__lowerCamelCase   )[0]
      _SCREAMING_SNAKE_CASE	=				torch.Size((1, 1_1, 7_6_8)   )
      self.assertEqual(output.shape  ,				__lowerCamelCase   )
      _SCREAMING_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]  ,				__lowerCamelCase  ,				atol=1e-4   )   )







'''simple docstring'''



lowerCamelCase_			=						'Tobias Carryer'

from time import time







class 		lowercase_   :


   """simple docstring"""

   def __init__(   self :     Tuple  ,				__lowerCamelCase :     int  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     Optional[int]  ,				__lowerCamelCase :     Dict=int(time()   )   ):  # noqa: B008

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				multiplier
      _SCREAMING_SNAKE_CASE	=				increment
      _SCREAMING_SNAKE_CASE	=				modulo
      _SCREAMING_SNAKE_CASE	=				seed



   def 							lowerCAmelCase_      (   self :     List[str]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				(self.multiplier * self.seed + self.increment) % self.modulo
      return self.seed


if __name__ == "__main__":
 # Show the LCG in action.
 lowerCamelCase_			=						LinearCongruentialGenerator(1_66_45_25, 10_13_90_42_23, 2 << 31)
 while True:
  print(lcg.next_number())

