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







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_=1_3				,		snake_case_=7				,		snake_case_=True				,		snake_case_=True				,		snake_case_=False				,		snake_case_=True				,		snake_case_=9_9				,		snake_case_=3_2				,		snake_case_=5				,		snake_case_=4				,		snake_case_=3_7				,		snake_case_="gelu"				,		snake_case_=0.1				,		snake_case_=0.1				,		snake_case_=5_1_2				,		snake_case_=1_6				,		snake_case_=2				,		snake_case_=0.02				,		snake_case_=3				,		snake_case_=4				,		snake_case_=None				,		):




       '''simple docstring'''

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				ids_tensor([self.batch_size, self.seq_length]				,		self.vocab_size						)

       UpperCAmelCase_							:       List[str]       =				None
       if self.use_input_mask:
            UpperCAmelCase_							:       List[Any]       =				random_attention_mask([self.batch_size, self.seq_length]						)

       UpperCAmelCase_							:       Union[str, Any]       =				None
       UpperCAmelCase_							:       Tuple       =				None
       UpperCAmelCase_							:       Union[str, Any]       =				None
       if self.use_labels:
            UpperCAmelCase_							:       Any       =				ids_tensor([self.batch_size]				,		self.type_sequence_label_size						)
            UpperCAmelCase_							:       Optional[Any]       =				ids_tensor([self.batch_size, self.seq_length]				,		self.num_labels						)
            UpperCAmelCase_							:       Union[str, Any]       =				ids_tensor([self.batch_size]				,		self.num_choices						)

       UpperCAmelCase_							:       Dict       =				self.get_config()

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


  def _UpperCamelCase					(			self						):




       '''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 _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				DistilBertModel(config=snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       Tuple       =				model(snake_case_				,		snake_case_						)
       UpperCAmelCase_							:       Union[str, Any]       =				model(snake_case_						)
       self.parent.assertEqual(result.last_hidden_state.shape				,		(self.batch_size, self.seq_length, self.hidden_size)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				DistilBertForMaskedLM(config=snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       List[Any]       =				model(snake_case_				,		attention_mask=snake_case_				,		labels=snake_case_						)
       self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.seq_length, self.vocab_size)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				DistilBertForQuestionAnswering(config=snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       List[Any]       =				model(
           snake_case_				,		attention_mask=snake_case_				,		start_positions=snake_case_				,		end_positions=snake_case_						)
       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				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				self.num_labels
       UpperCAmelCase_							:       int       =				DistilBertForSequenceClassification(snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       str       =				model(snake_case_				,		attention_mask=snake_case_				,		labels=snake_case_						)
       self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.num_labels)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				self.num_labels
       UpperCAmelCase_							:       Optional[int]       =				DistilBertForTokenClassification(config=snake_case_						)
       model.to(snake_case_						)
       model.eval()

       UpperCAmelCase_							:       Optional[int]       =				model(snake_case_				,		attention_mask=snake_case_				,		labels=snake_case_						)
       self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.seq_length, self.num_labels)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				self.num_choices
       UpperCAmelCase_							:       str       =				DistilBertForMultipleChoice(config=snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       Dict       =				input_ids.unsqueeze(1						).expand(-1				,		self.num_choices				,		-1						).contiguous()
       UpperCAmelCase_							:       Tuple       =				input_mask.unsqueeze(1						).expand(-1				,		self.num_choices				,		-1						).contiguous()
       UpperCAmelCase_							:       Optional[int]       =				model(
           snake_case_				,		attention_mask=snake_case_				,		labels=snake_case_				,		)
       self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.num_choices)						)






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				self.prepare_config_and_inputs()
       ((UpperCAmelCase_)       ,      (UpperCAmelCase_)       ,      (UpperCAmelCase_)       ,      (UpperCAmelCase_)       ,      (UpperCAmelCase_)       ,      (UpperCAmelCase_))							:       Optional[int]       =				config_and_inputs
       UpperCAmelCase_							:       int       =				{'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict







@require_torch
class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_						,     lowerCamelCase_						,     unittest.TestCase	):







  '''simple docstring'''






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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				DistilBertModelTester(self						)
       UpperCAmelCase_							:       int       =				ConfigTester(self				,		config_class=snake_case_				,		dim=3_7						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       self.config_tester.run_common_tests()


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Any       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[int]       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*snake_case_						)


  @slow
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_							:       Any       =				DistilBertModel.from_pretrained(snake_case_						)
            self.assertIsNotNone(snake_case_						)






  @slow
  @require_torch_gpu
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       Optional[Any]       =				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_							:       Tuple       =				True
            UpperCAmelCase_							:       Tuple       =				model_class(config=snake_case_						)

            UpperCAmelCase_							:       Tuple       =				self._prepare_for_class(snake_case_				,		snake_case_						)
            UpperCAmelCase_							:       Any       =				torch.jit.trace(
                snake_case_				,		(inputs_dict['input_ids'].to('cpu'						), inputs_dict['attention_mask'].to('cpu'						))						)

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







@require_torch
class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  @slow
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				DistilBertModel.from_pretrained('distilbert-base-uncased'						)
       UpperCAmelCase_							:       List[str]       =				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]]						)
       UpperCAmelCase_							:       int       =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
       with torch.no_grad():
            UpperCAmelCase_							:       Optional[Any]       =				model(snake_case_				,		attention_mask=snake_case_						)[0]
       UpperCAmelCase_							:       Any       =				torch.Size((1, 1_1, 7_6_8)						)
       self.assertEqual(output.shape				,		snake_case_						)
       UpperCAmelCase_							:       int       =				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]				,		snake_case_				,		atol=1E-4						)						)







'''simple docstring'''


import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)






class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				['a', 'b', 'c']

       # Defaults to last layer if both are None
       UpperCAmelCase_       ,      UpperCAmelCase_							:       List[str]       =				get_aligned_output_features_output_indices(snake_case_				,		snake_case_				,		snake_case_						)
       self.assertEqual(snake_case_				,		['c']						)
       self.assertEqual(snake_case_				,		[2]						)

       # Out indices set to match out features
       UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				get_aligned_output_features_output_indices(['a', 'c']				,		snake_case_				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[0, 2]						)

       # Out features set to match out indices
       UpperCAmelCase_       ,      UpperCAmelCase_							:       List[Any]       =				get_aligned_output_features_output_indices(snake_case_				,		[0, 2]				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[0, 2]						)

       # Out features selected from negative indices
       UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				get_aligned_output_features_output_indices(snake_case_				,		[-3, -1]				,		snake_case_						)
       self.assertEqual(snake_case_				,		['a', 'c']						)
       self.assertEqual(snake_case_				,		[-3, -1]						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 1)				,		snake_case_						)

       # Out features must be a list
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(('a', 'b')				,		(0, 1)				,		['a', 'b']						)

       # Out features must be a subset of stage names
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 1)				,		['a']						)

       # Out indices must be a list or tuple
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(snake_case_				,		0				,		['a', 'b']						)

       # Out indices must be a subset of stage names
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(snake_case_				,		(0, 1)				,		['a']						)

       # Out features and out indices must be the same length
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0,)				,		['a', 'b', 'c']						)

       # Out features should match out indices
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['a', 'b']				,		(0, 2)				,		['a', 'b', 'c']						)

       # Out features and out indices should be in order
       with self.assertRaises(snake_case_						):
            verify_out_features_out_indices(['b', 'a']				,		(0, 1)				,		['a', 'b']						)

       # Check passes with valid inputs
       verify_out_features_out_indices(['a', 'b', 'd']				,		(0, 1, -1)				,		['a', 'b', 'c', 'd']						)






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				BackboneMixin()

       UpperCAmelCase_							:       Any       =				['a', 'b', 'c']
       UpperCAmelCase_							:       str       =				['a', 'c']
       UpperCAmelCase_							:       str       =				[0, 2]

       # Check that the output features and indices are set correctly
       self.assertEqual(backbone.out_features				,		['a', 'c']						)
       self.assertEqual(backbone.out_indices				,		[0, 2]						)

       # Check out features and indices are updated correctly
       UpperCAmelCase_							:       str       =				['a', 'b']
       self.assertEqual(backbone.out_features				,		['a', 'b']						)
       self.assertEqual(backbone.out_indices				,		[0, 1]						)

       UpperCAmelCase_							:       Optional[int]       =				[-3, -1]
       self.assertEqual(backbone.out_features				,		['a', 'c']						)
       self.assertEqual(backbone.out_indices				,		[-3, -1]						)






