





'''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_       (    _lowerCAmelCase							):





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





           """simple docstring"""




           lowercase_      :Optional[int]         =			parent
           lowercase_      :Tuple         =			batch_size
           lowercase_      :Optional[Any]         =			seq_length
           lowercase_      :List[Any]         =			is_training
           lowercase_      :List[str]         =			use_input_mask
           lowercase_      :Tuple         =			use_token_type_ids
           lowercase_      :List[str]         =			use_labels
           lowercase_      :int         =			vocab_size
           lowercase_      :List[Any]         =			hidden_size
           lowercase_      :str         =			num_hidden_layers
           lowercase_      :Optional[int]         =			num_attention_heads
           lowercase_      :Union[str, Any]         =			intermediate_size
           lowercase_      :Any         =			hidden_act
           lowercase_      :str         =			hidden_dropout_prob
           lowercase_      :Dict         =			attention_probs_dropout_prob
           lowercase_      :List[str]         =			max_position_embeddings
           lowercase_      :str         =			type_vocab_size
           lowercase_      :Optional[Any]         =			type_sequence_label_size
           lowercase_      :Dict         =			initializer_range
           lowercase_      :Dict         =			num_labels
           lowercase_      :int         =			num_choices
           lowercase_      :Union[str, Any]         =			scope





     def 							lowercase__				(			self		: Any						):





           """simple docstring"""




           lowercase_      :Any         =			ids_tensor([self.batch_size, self.seq_length]     ,				self.vocab_size						)

           lowercase_      :Optional[int]         =			None
           if self.use_input_mask:
                 lowercase_      :Optional[Any]         =			random_attention_mask([self.batch_size, self.seq_length]						)

           lowercase_      :Optional[int]         =			None
           lowercase_      :Union[str, Any]         =			None
           lowercase_      :Dict         =			None
           if self.use_labels:
                 lowercase_      :Optional[Any]         =			ids_tensor([self.batch_size]     ,				self.type_sequence_label_size						)
                 lowercase_      :Optional[int]         =			ids_tensor([self.batch_size, self.seq_length]     ,				self.num_labels						)
                 lowercase_      :List[Any]         =			ids_tensor([self.batch_size]     ,				self.num_choices						)

           lowercase_      :Tuple         =			self.get_config()

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





     def 							lowercase__				(			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 							lowercase__				(			self		: Optional[Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: List[str]     ,				lowercase		: List[str]     ,				lowercase		: str     ,				lowercase		: Any     ,				lowercase		: Tuple						):





           """simple docstring"""




           lowercase_      :Optional[Any]         =			DistilBertModel(config=lowercase						)
           model.to(lowercase						)
           model.eval()
           lowercase_      :str         =			model(lowercase     ,				lowercase						)
           lowercase_      :int         =			model(lowercase						)
           self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)						)





     def 							lowercase__				(			self		: List[Any]     ,				lowercase		: Any     ,				lowercase		: str     ,				lowercase		: Optional[Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: Optional[Any]     ,				lowercase		: str						):





           """simple docstring"""




           lowercase_      :int         =			DistilBertForMaskedLM(config=lowercase						)
           model.to(lowercase						)
           model.eval()
           lowercase_      :str         =			model(lowercase     ,				attention_mask=lowercase     ,				labels=lowercase						)
           self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.seq_length, self.vocab_size)						)





     def 							lowercase__				(			self		: Optional[int]     ,				lowercase		: Union[str, Any]     ,				lowercase		: List[Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: Optional[int]     ,				lowercase		: Dict     ,				lowercase		: Dict						):





           """simple docstring"""




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





           """simple docstring"""




           lowercase_      :Optional[Any]         =			self.num_labels
           lowercase_      :Any         =			DistilBertForSequenceClassification(lowercase						)
           model.to(lowercase						)
           model.eval()
           lowercase_      :List[Any]         =			model(lowercase     ,				attention_mask=lowercase     ,				labels=lowercase						)
           self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.num_labels)						)





     def 							lowercase__				(			self		: Optional[Any]     ,				lowercase		: Optional[int]     ,				lowercase		: int     ,				lowercase		: Optional[int]     ,				lowercase		: Dict     ,				lowercase		: Any     ,				lowercase		: Optional[Any]						):





           """simple docstring"""




           lowercase_      :Tuple         =			self.num_labels
           lowercase_      :int         =			DistilBertForTokenClassification(config=lowercase						)
           model.to(lowercase						)
           model.eval()

           lowercase_      :List[str]         =			model(lowercase     ,				attention_mask=lowercase     ,				labels=lowercase						)
           self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.seq_length, self.num_labels)						)





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: Tuple     ,				lowercase		: List[Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: Any     ,				lowercase		: int     ,				lowercase		: Union[str, Any]						):





           """simple docstring"""




           lowercase_      :Any         =			self.num_choices
           lowercase_      :List[str]         =			DistilBertForMultipleChoice(config=lowercase						)
           model.to(lowercase						)
           model.eval()
           lowercase_      :Optional[int]         =			input_ids.unsqueeze(1						).expand(-1     ,				self.num_choices     ,				-1						).contiguous()
           lowercase_      :Any         =			input_mask.unsqueeze(1						).expand(-1     ,				self.num_choices     ,				-1						).contiguous()
           lowercase_      :Union[str, Any]         =			model(
               lowercase     ,				attention_mask=lowercase     ,				labels=lowercase     ,				)
           self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.num_choices)						)







     def 							lowercase__				(			self		: Tuple						):





           """simple docstring"""




           lowercase_      :Tuple         =			self.prepare_config_and_inputs()
           (lowercase_)      :str         =			config_and_inputs
           lowercase_      :str         =			{"input_ids": input_ids, "attention_mask": input_mask}
           return config, inputs_dict







@require_torch
class 				a_       (    _lowerCAmelCase     ,						_lowerCAmelCase     ,						unittest.TestCase							):
     __A								=      (
         (
             DistilBertModel,
             DistilBertForMaskedLM,
             DistilBertForMultipleChoice,
             DistilBertForQuestionAnswering,
             DistilBertForSequenceClassification,
             DistilBertForTokenClassification,
         )
         if is_torch_available()
         else None
     )
     __A								=      (
         {
             "feature-extraction": DistilBertModel,
             "fill-mask": DistilBertForMaskedLM,
             "question-answering": DistilBertForQuestionAnswering,
             "text-classification": DistilBertForSequenceClassification,
             "token-classification": DistilBertForTokenClassification,
             "zero-shot": DistilBertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __A								=      True
     __A								=      True
     __A								=      True
     __A								=      True





     def 							lowercase__				(			self		: List[str]						):





           """simple docstring"""




           lowercase_      :Dict         =			DistilBertModelTester(self						)
           lowercase_      :int         =			ConfigTester(self     ,				config_class=lowercase     ,				dim=37						)





     def 							lowercase__				(			self		: List[Any]						):





           """simple docstring"""




           self.config_tester.run_common_tests()





     def 							lowercase__				(			self		: Optional[Any]						):





           """simple docstring"""




           lowercase_      :List[str]         =			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_model(*lowercase						)





     def 							lowercase__				(			self		: List[Any]						):





           """simple docstring"""




           lowercase_      :List[Any]         =			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase						)





     def 							lowercase__				(			self		: List[Any]						):





           """simple docstring"""




           lowercase_      :int         =			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase						)





     def 							lowercase__				(			self		: List[Any]						):





           """simple docstring"""




           lowercase_      :Tuple         =			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase						)





     def 							lowercase__				(			self		: Tuple						):





           """simple docstring"""




           lowercase_      :List[Any]         =			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase						)





     def 							lowercase__				(			self		: List[str]						):





           """simple docstring"""




           lowercase_      :Any         =			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase						)





     @slow
     def 							lowercase__				(			self		: str						):





           """simple docstring"""




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







     @slow
     @require_torch_gpu
     def 							lowercase__				(			self		: List[str]						):





           """simple docstring"""




           lowercase_      :Optional[int]         =			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

                 lowercase_      :Dict         =			True
                 lowercase_      :str         =			model_class(config=lowercase						)

                 lowercase_      :List[str]         =			self._prepare_for_class(lowercase     ,				lowercase						)
                 lowercase_      :Optional[int]         =			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"						)						)
                       lowercase_      :int         =			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 							lowercase__				(			self		: Tuple						):





           """simple docstring"""




           lowercase_      :List[str]         =			DistilBertModel.from_pretrained("distilbert-base-uncased"						)
           lowercase_      :Optional[int]         =			torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]						)
           lowercase_      :List[Any]         =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
           with torch.no_grad():
                 lowercase_      :Dict         =			model(lowercase     ,				attention_mask=lowercase						)[0]
           lowercase_      :Dict         =			torch.Size((1, 11, 768)						)
           self.assertEqual(output.shape     ,				lowercase						)
           lowercase_      :List[str]         =			torch.tensor(
               [[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]]						)

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









'''simple docstring'''


from abc import ABC, abstractmethod
from argparse import ArgumentParser
class 				a_       (    _lowerCAmelCase							):





     @staticmethod
     @abstractmethod
     def 							lowercase__				(			lowercase		: ArgumentParser						):





        """simple docstring"""




        raise NotImplementedError()







     @abstractmethod
     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        raise NotImplementedError()


