






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

  '''simple docstring'''







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


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


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


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

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

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

         _UpperCAmelCase							=    self.get_config()

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


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


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


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


         """simple docstring"""
         _UpperCAmelCase							=    DistilBertForMaskedLM(config=lowerCamelCase							)
         model.to(lowerCamelCase							)
         model.eval()
         _UpperCAmelCase							=    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		:			Union[str, Any]       ,				lowerCamelCase		:			Any       ,				lowerCamelCase		:			List[str]       ,				lowerCamelCase		:			Dict       ,				lowerCamelCase		:			int       ,				lowerCamelCase		:			List[Any]       ,				lowerCamelCase		:			Any							)						->	List[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    DistilBertForQuestionAnswering(config=lowerCamelCase							)
         model.to(lowerCamelCase							)
         model.eval()
         _UpperCAmelCase							=    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		:			List[str]       ,				lowerCamelCase		:			Dict       ,				lowerCamelCase		:			Any       ,				lowerCamelCase		:			List[str]       ,				lowerCamelCase		:			Any       ,				lowerCamelCase		:			List[Any]       ,				lowerCamelCase		:			str							)						->	Union[str, Any]:


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


  def  lowerCamelCase       (      self		:			Dict       ,				lowerCamelCase		:			Optional[Any]       ,				lowerCamelCase		:			List[str]       ,				lowerCamelCase		:			List[str]       ,				lowerCamelCase		:			Tuple       ,				lowerCamelCase		:			List[Any]       ,				lowerCamelCase		:			List[Any]							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase							=    self.num_labels
         _UpperCAmelCase							=    DistilBertForTokenClassification(config=lowerCamelCase							)
         model.to(lowerCamelCase							)
         model.eval()

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


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





  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	Dict:


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



@require_torch
class 		SCREAMING_SNAKE_CASE__	(  UpperCAmelCase					,      UpperCAmelCase					,      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		:			List[Any]							)						->	List[str]:


         """simple docstring"""
         _UpperCAmelCase							=    DistilBertModelTester(self							)
         _UpperCAmelCase							=    ConfigTester(self       ,				config_class=lowerCamelCase       ,				dim=37							)


  def  lowerCamelCase       (      self		:			Optional[int]							)						->	Optional[int]:


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


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


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_model(*lowerCamelCase							)


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


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Tuple							)						->	Optional[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCamelCase							)


  def  lowerCamelCase       (      self		:			int							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Dict							)						->	Optional[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Tuple							)						->	Optional[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCamelCase							)


  @slow
  def  lowerCamelCase       (      self		:			Dict							)						->	int:


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





  @slow
  @require_torch_gpu
  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	str:


         """simple docstring"""
         _UpperCAmelCase	,			_UpperCAmelCase							=    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

                _UpperCAmelCase							=    True
                _UpperCAmelCase							=    model_class(config=lowerCamelCase							)

                _UpperCAmelCase							=    self._prepare_for_class(lowerCamelCase       ,				lowerCamelCase							)
                _UpperCAmelCase							=    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"""							)							)
                       _UpperCAmelCase							=    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 		SCREAMING_SNAKE_CASE__	(  unittest.TestCase     ):

  '''simple docstring'''







  @slow
  def  lowerCamelCase       (      self		:			int							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase							=    DistilBertModel.from_pretrained("""distilbert-base-uncased"""							)
         _UpperCAmelCase							=    torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]							)
         _UpperCAmelCase							=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
         with torch.no_grad():
                _UpperCAmelCase							=    model(lowerCamelCase       ,				attention_mask=lowerCamelCase							)[0]
         _UpperCAmelCase							=    torch.Size((1, 11, 768)							)
         self.assertEqual(output.shape       ,				lowerCamelCase							)
         _UpperCAmelCase							=    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 os
import sys
import unittest


__a:     Optional[int]   					=					os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, '''utils'''))

import get_test_info  # noqa: E402
from get_test_info import (  # noqa: E402
    get_model_to_test_mapping,
    get_model_to_tester_mapping,
    get_test_to_tester_mapping,
)


__a:     Dict   					=					os.path.join('''tests''', '''models''', '''bert''', '''test_modeling_bert.py''')
__a:     Union[str, Any]   					=					os.path.join('''tests''', '''models''', '''blip''', '''test_modeling_blip.py''')
class 		SCREAMING_SNAKE_CASE__	(  unittest.TestCase     ):

  '''simple docstring'''







  def  lowerCamelCase       (      self		:			str							)						->	Dict:


         """simple docstring"""
         _UpperCAmelCase							=    get_test_to_tester_mapping(lowerCamelCase							)
         _UpperCAmelCase							=    get_test_to_tester_mapping(lowerCamelCase							)

         _UpperCAmelCase							=    {"""BertModelTest""": """BertModelTester"""}

         _UpperCAmelCase							=    {
             """BlipModelTest""": """BlipModelTester""",
             """BlipTextImageModelTest""": """BlipTextImageModelsModelTester""",
             """BlipTextModelTest""": """BlipTextModelTester""",
             """BlipTextRetrievalModelTest""": """BlipTextRetrievalModelTester""",
             """BlipVQAModelTest""": """BlipVQAModelTester""",
             """BlipVisionModelTest""": """BlipVisionModelTester""",
         }

         self.assertEqual(get_test_info.to_json(lowerCamelCase							)       ,				lowerCamelCase							)
         self.assertEqual(get_test_info.to_json(lowerCamelCase							)       ,				lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Optional[int]							)						->	str:


         """simple docstring"""
         _UpperCAmelCase							=    get_model_to_test_mapping(lowerCamelCase							)
         _UpperCAmelCase							=    get_model_to_test_mapping(lowerCamelCase							)

         _UpperCAmelCase							=    {
             """BertForMaskedLM""": ["""BertModelTest"""],
             """BertForMultipleChoice""": ["""BertModelTest"""],
             """BertForNextSentencePrediction""": ["""BertModelTest"""],
             """BertForPreTraining""": ["""BertModelTest"""],
             """BertForQuestionAnswering""": ["""BertModelTest"""],
             """BertForSequenceClassification""": ["""BertModelTest"""],
             """BertForTokenClassification""": ["""BertModelTest"""],
             """BertLMHeadModel""": ["""BertModelTest"""],
             """BertModel""": ["""BertModelTest"""],
         }

         _UpperCAmelCase							=    {
             """BlipForConditionalGeneration""": ["""BlipTextImageModelTest"""],
             """BlipForImageTextRetrieval""": ["""BlipTextRetrievalModelTest"""],
             """BlipForQuestionAnswering""": ["""BlipVQAModelTest"""],
             """BlipModel""": ["""BlipModelTest"""],
             """BlipTextModel""": ["""BlipTextModelTest"""],
             """BlipVisionModel""": ["""BlipVisionModelTest"""],
         }

         self.assertEqual(get_test_info.to_json(lowerCamelCase							)       ,				lowerCamelCase							)
         self.assertEqual(get_test_info.to_json(lowerCamelCase							)       ,				lowerCamelCase							)





  def  lowerCamelCase       (      self		:			int							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase							=    get_model_to_tester_mapping(lowerCamelCase							)
         _UpperCAmelCase							=    get_model_to_tester_mapping(lowerCamelCase							)

         _UpperCAmelCase							=    {
             """BertForMaskedLM""": ["""BertModelTester"""],
             """BertForMultipleChoice""": ["""BertModelTester"""],
             """BertForNextSentencePrediction""": ["""BertModelTester"""],
             """BertForPreTraining""": ["""BertModelTester"""],
             """BertForQuestionAnswering""": ["""BertModelTester"""],
             """BertForSequenceClassification""": ["""BertModelTester"""],
             """BertForTokenClassification""": ["""BertModelTester"""],
             """BertLMHeadModel""": ["""BertModelTester"""],
             """BertModel""": ["""BertModelTester"""],
         }

         _UpperCAmelCase							=    {
             """BlipForConditionalGeneration""": ["""BlipTextImageModelsModelTester"""],
             """BlipForImageTextRetrieval""": ["""BlipTextRetrievalModelTester"""],
             """BlipForQuestionAnswering""": ["""BlipVQAModelTester"""],
             """BlipModel""": ["""BlipModelTester"""],
             """BlipTextModel""": ["""BlipTextModelTester"""],
             """BlipVisionModel""": ["""BlipVisionModelTester"""],
         }

         self.assertEqual(get_test_info.to_json(lowerCamelCase							)       ,				lowerCamelCase							)
         self.assertEqual(get_test_info.to_json(lowerCamelCase							)       ,				lowerCamelCase							)