


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






 """simple docstring"""




 def __init__(	self ,			__magic_name__ ,			__magic_name__=13 ,			__magic_name__=7 ,			__magic_name__=True ,			__magic_name__=True ,			__magic_name__=False ,			__magic_name__=True ,			__magic_name__=99 ,			__magic_name__=32 ,			__magic_name__=5 ,			__magic_name__=4 ,			__magic_name__=37 ,			__magic_name__="gelu" ,			__magic_name__=0.1 ,			__magic_name__=0.1 ,			__magic_name__=5_12 ,			__magic_name__=16 ,			__magic_name__=2 ,			__magic_name__=0.0_2 ,			__magic_name__=3 ,			__magic_name__=4 ,			__magic_name__=None ,			)   ->							int:
      _a      =							parent
      _a      =							batch_size
      _a      =							seq_length
      _a      =							is_training
      _a      =							use_input_mask
      _a      =							use_token_type_ids
      _a      =							use_labels
      _a      =							vocab_size
      _a      =							hidden_size
      _a      =							num_hidden_layers
      _a      =							num_attention_heads
      _a      =							intermediate_size
      _a      =							hidden_act
      _a      =							hidden_dropout_prob
      _a      =							attention_probs_dropout_prob
      _a      =							max_position_embeddings
      _a      =							type_vocab_size
      _a      =							type_sequence_label_size
      _a      =							initializer_range
      _a      =							num_labels
      _a      =							num_choices
      _a      =							scope
 def 							__UpperCAmelCase						(	self  )   ->							Optional[int]:
      _a      =							ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size  )

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

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

      _a      =							self.get_config()

      return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 							__UpperCAmelCase						(	self  )   ->							Optional[int]:
      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 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__  )   ->							str:
      _a      =							DistilBertModel(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a      =							model(__magic_name__ ,			__magic_name__  )
      _a      =							model(__magic_name__  )
      self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)  )
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__  )   ->							Union[str, Any]:
      _a      =							DistilBertForMaskedLM(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a      =							model(__magic_name__ ,			attention_mask=__magic_name__ ,			labels=__magic_name__  )
      self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.vocab_size)  )
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__  )   ->							List[Any]:
      _a      =							DistilBertForQuestionAnswering(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a      =							model(
          __magic_name__ ,			attention_mask=__magic_name__ ,			start_positions=__magic_name__ ,			end_positions=__magic_name__  )
      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 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__  )   ->							Tuple:
      _a      =							self.num_labels
      _a      =							DistilBertForSequenceClassification(__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a      =							model(__magic_name__ ,			attention_mask=__magic_name__ ,			labels=__magic_name__  )
      self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_labels)  )
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__  )   ->							Tuple:
      _a      =							self.num_labels
      _a      =							DistilBertForTokenClassification(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()

      _a      =							model(__magic_name__ ,			attention_mask=__magic_name__ ,			labels=__magic_name__  )
      self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.num_labels)  )
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__  )   ->							Union[str, Any]:
      _a      =							self.num_choices
      _a      =							DistilBertForMultipleChoice(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a      =							input_ids.unsqueeze(1  ).expand(-1 ,			self.num_choices ,			-1  ).contiguous()
      _a      =							input_mask.unsqueeze(1  ).expand(-1 ,			self.num_choices ,			-1  ).contiguous()
      _a      =							model(
          __magic_name__ ,			attention_mask=__magic_name__ ,			labels=__magic_name__ ,			)
      self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_choices)  )




 def 							__UpperCAmelCase						(	self  )   ->							Union[str, Any]:
      _a      =							self.prepare_config_and_inputs()
      ((_a) ,				(_a) ,				(_a) ,				(_a) ,				(_a) ,				(_a))      =							config_and_inputs
      _a      =							{'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"""




 _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 							__UpperCAmelCase						(	self  )   ->							Any:
      _a      =							DistilBertModelTester(self  )
      _a      =							ConfigTester(self ,			config_class=__magic_name__ ,			dim=37  )
 def 							__UpperCAmelCase						(	self  )   ->							Dict:
      self.config_tester.run_common_tests()
 def 							__UpperCAmelCase						(	self  )   ->							Optional[int]:
      _a      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*__magic_name__  )
 def 							__UpperCAmelCase						(	self  )   ->							Tuple:
      _a      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*__magic_name__  )
 def 							__UpperCAmelCase						(	self  )   ->							Optional[int]:
      _a      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*__magic_name__  )
 def 							__UpperCAmelCase						(	self  )   ->							Optional[int]:
      _a      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*__magic_name__  )
 def 							__UpperCAmelCase						(	self  )   ->							Tuple:
      _a      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*__magic_name__  )
 def 							__UpperCAmelCase						(	self  )   ->							str:
      _a      =							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*__magic_name__  )
 @slow
 def 							__UpperCAmelCase						(	self  )   ->							Tuple:
      for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _a      =							DistilBertModel.from_pretrained(__magic_name__  )
           self.assertIsNotNone(__magic_name__  )




 @slow
 @require_torch_gpu
 def 							__UpperCAmelCase						(	self  )   ->							Any:
      _a ,				_a      =							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

           _a      =							True
           _a      =							model_class(config=__magic_name__  )

           _a      =							self._prepare_for_class(__magic_name__ ,			__magic_name__  )
           _a      =							torch.jit.trace(
               __magic_name__ ,			(inputs_dict['input_ids'].to('cpu'  ), inputs_dict['attention_mask'].to('cpu'  ))  )

           with tempfile.TemporaryDirectory() as tmp:
                torch.jit.save(__magic_name__ ,			os.path.join(__magic_name__ ,			'traced_model.pt'  )  )
                _a      =							torch.jit.load(os.path.join(__magic_name__ ,			'traced_model.pt'  ) ,			map_location=__magic_name__  )
                loaded(inputs_dict['input_ids'].to(__magic_name__  ) ,			inputs_dict['attention_mask'].to(__magic_name__  )  )





@require_torch
class 	a       (       unittest.TestCase    ):






 """simple docstring"""




 @slow
 def 							__UpperCAmelCase						(	self  )   ->							Optional[Any]:
      _a      =							DistilBertModel.from_pretrained('distilbert-base-uncased'  )
      _a      =							torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]  )
      _a      =							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
      with torch.no_grad():
           _a      =							model(__magic_name__ ,			attention_mask=__magic_name__  )[0]
      _a      =							torch.Size((1, 11, 7_68)  )
      self.assertEqual(output.shape ,			__magic_name__  )
      _a      =							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] ,			__magic_name__ ,			atol=1e-4  )  )





'''simple docstring'''






import warnings

from ...utils import logging
from .image_processing_clip import CLIPImageProcessor


a_   :      Optional[Any]				    =      logging.get_logger(__name__)




class 	a       (       _SCREAMING_SNAKE_CASE    ):
 def __init__(	self ,			*__magic_name__ ,			**__magic_name__  )   ->							None:
      warnings.warn(
          'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
          ' use CLIPImageProcessor instead.' ,			__magic_name__ ,			)
      super().__init__(*__magic_name__ ,			**__magic_name__  )

