




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



 '''simple docstring'''


 def __init__(    self :  int						,							A_ :  List[Any]						,							A_ :  Tuple=13						,							A_ :  Any=7						,							A_ :  str=True						,							A_ :  Tuple=True						,							A_ :  List[Any]=False						,							A_ :  Optional[int]=True						,							A_ :  Optional[Any]=99						,							A_ :  Any=32						,							A_ :  Optional[int]=5						,							A_ :  Union[str, Any]=4						,							A_ :  List[Any]=37						,							A_ :  Tuple="gelu"						,							A_ :  int=0.1						,							A_ :  str=0.1						,							A_ :  List[Any]=512						,							A_ :  List[str]=16						,							A_ :  List[Any]=2						,							A_ :  Optional[int]=0.02						,							A_ :  Optional[Any]=3						,							A_ :  List[Any]=4						,							A_ :  Tuple=None						,							)       ->     int:

       """simple docstring"""






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

 def     a__			(    self :  Union[str, Any]  )       ->     Optional[int]:

       """simple docstring"""






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

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

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

       lowerCamelCase_							=   self.get_config()

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

 def     a__			(    self :  List[Any]  )       ->     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 :  Optional[Any]						,							A_ :  List[Any]						,							A_ :  Dict						,							A_ :  Tuple						,							A_ :  Dict						,							A_ :  Tuple						,							A_ :  int  )       ->     str:

       """simple docstring"""






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

 def     a__			(    self :  List[Any]						,							A_ :  List[str]						,							A_ :  Dict						,							A_ :  Any						,							A_ :  Any						,							A_ :  Optional[Any]						,							A_ :  str  )       ->     Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_							=   DistilBertForMaskedLM(config=A_  )
       model.to(A_  )
       model.eval()
       lowerCamelCase_							=   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 :  Any						,							A_ :  Optional[int]						,							A_ :  Union[str, Any]						,							A_ :  str						,							A_ :  str						,							A_ :  Union[str, Any]						,							A_ :  Dict  )       ->     List[Any]:

       """simple docstring"""






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

       """simple docstring"""






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

 def     a__			(    self :  str						,							A_ :  int						,							A_ :  Union[str, Any]						,							A_ :  List[Any]						,							A_ :  List[Any]						,							A_ :  Union[str, Any]						,							A_ :  List[str]  )       ->     Tuple:

       """simple docstring"""






       lowerCamelCase_							=   self.num_labels
       lowerCamelCase_							=   DistilBertForTokenClassification(config=A_  )
       model.to(A_  )
       model.eval()

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

       """simple docstring"""






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





 def     a__			(    self :  Optional[Any]  )       ->     Union[str, Any]:

       """simple docstring"""






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







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



 '''simple docstring'''


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

 def     a__			(    self :  str  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   DistilBertModelTester(self  )
       lowerCamelCase_							=   ConfigTester(self						,							config_class=A_						,							dim=37  )

 def     a__			(    self :  Dict  )       ->     Dict:

       """simple docstring"""






       self.config_tester.run_common_tests()

 def     a__			(    self :  Dict  )       ->     Optional[int]:

       """simple docstring"""






       lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*A_  )

 def     a__			(    self :  List[Any]  )       ->     Tuple:

       """simple docstring"""






       lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*A_  )

 def     a__			(    self :  List[Any]  )       ->     Optional[int]:

       """simple docstring"""






       lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*A_  )

 def     a__			(    self :  Union[str, Any]  )       ->     Optional[int]:

       """simple docstring"""






       lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*A_  )

 def     a__			(    self :  int  )       ->     Tuple:

       """simple docstring"""






       lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*A_  )

 def     a__			(    self :  Dict  )       ->     str:

       """simple docstring"""






       lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*A_  )

 @slow
 def     a__			(    self :  Any  )       ->     Tuple:

       """simple docstring"""






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





 @slow
 @require_torch_gpu
 def     a__			(    self :  Tuple  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_					,    lowerCamelCase_							=   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_							=   True
             lowerCamelCase_							=   model_class(config=A_  )

             lowerCamelCase_							=   self._prepare_for_class(A_						,							A_  )
             lowerCamelCase_							=   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_							=   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   A(						unittest.TestCase       ):



 '''simple docstring'''


 @slow
 def     a__			(    self :  int  )       ->     Optional[Any]:

       """simple docstring"""






       lowerCamelCase_							=   DistilBertModel.from_pretrained('distilbert-base-uncased'  )
       lowerCamelCase_							=   torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]  )
       lowerCamelCase_							=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
       with torch.no_grad():
             lowerCamelCase_							=   model(A_						,							attention_mask=A_  )[0]
       lowerCamelCase_							=   torch.Size((1, 11, 768)  )
       self.assertEqual(output.shape						,							A_  )
       lowerCamelCase_							=   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  )  )









def       _SCREAMING_SNAKE_CASE      (							lowercase     :	Tuple     ,							lowercase     :	Dict     ,							lowercase     :	List[str]     ,							lowercase     :	Dict     ,							lowercase     :	Dict     ,							lowercase     :	List[str]					):


      '''simple docstring'''



      if index == r:
            for j in range(lowercase					):
                  print(data[j]     ,							end=' '					)
            print(' '					)
            return
      #  When no more elements are there to put in data[]
      if i >= n:
            return
      # current is included, put next at next location
      lowerCamelCase_							=   arr[i]
      combination_util(lowercase     ,							lowercase     ,							lowercase     ,							index + 1     ,							lowercase     ,							i + 1					)
      # current is excluded, replace it with
      # next (Note that i+1 is passed, but
      # index is not changed)
      combination_util(lowercase     ,							lowercase     ,							lowercase     ,							lowercase     ,							lowercase     ,							i + 1					)
      # The main function that prints all combinations
      # of size r in arr[] of size n. This function
      # mainly uses combinationUtil()

def       _SCREAMING_SNAKE_CASE      (							lowercase     :	int     ,							lowercase     :	Any     ,							lowercase     :	Tuple					):


      '''simple docstring'''



      lowerCamelCase_							=   [0] * r
      # Print all combination using temporary array 'data[]'
      combination_util(lowercase     ,							lowercase     ,							lowercase     ,							0     ,							lowercase     ,							0					)


if __name__ == "__main__":
 # Driver code to check the function above
 lowerCamelCase    :		int			   =       [10, 20, 30, 40, 50]
 print_combination(arr, len(arr), 3)
 # This code is contributed by Ambuj sahu



