






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






						def __init__(		self       ,  _UpperCamelCase       ,  _UpperCamelCase=13       ,  _UpperCamelCase=7       ,  _UpperCamelCase=True       ,  _UpperCamelCase=True       ,  _UpperCamelCase=False       ,  _UpperCamelCase=True       ,  _UpperCamelCase=99       ,  _UpperCamelCase=32       ,  _UpperCamelCase=5       ,  _UpperCamelCase=4       ,  _UpperCamelCase=37       ,  _UpperCamelCase="gelu"       ,  _UpperCamelCase=0.1       ,  _UpperCamelCase=0.1       ,  _UpperCamelCase=5_12       ,  _UpperCamelCase=16       ,  _UpperCamelCase=2       ,  _UpperCamelCase=0.02       ,  _UpperCamelCase=3       ,  _UpperCamelCase=4       ,  _UpperCamelCase=None       ,  ):






												"""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  UpperCamelCase__ (		self				):






												"""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  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       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												lowerCAmelCase__        =  DistilBertForMaskedLM(config=_UpperCamelCase				)
												model.to(_UpperCamelCase				)
												model.eval()
												lowerCAmelCase__        =  model(_UpperCamelCase       ,  attention_mask=_UpperCamelCase       ,  labels=_UpperCamelCase				)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												lowerCAmelCase__        =  DistilBertForQuestionAnswering(config=_UpperCamelCase				)
												model.to(_UpperCamelCase				)
												model.eval()
												lowerCAmelCase__        =  model(
												    _UpperCamelCase       ,  attention_mask=_UpperCamelCase       ,  start_positions=_UpperCamelCase       ,  end_positions=_UpperCamelCase				)
												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       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.num_labels
												lowerCAmelCase__        =  DistilBertForTokenClassification(config=_UpperCamelCase				)
												model.to(_UpperCamelCase				)
												model.eval()

												lowerCAmelCase__        =  model(_UpperCamelCase       ,  attention_mask=_UpperCamelCase       ,  labels=_UpperCamelCase				)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels)				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.num_choices
												lowerCAmelCase__        =  DistilBertForMultipleChoice(config=_UpperCamelCase				)
												model.to(_UpperCamelCase				)
												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(
												    _UpperCamelCase       ,  attention_mask=_UpperCamelCase       ,  labels=_UpperCamelCase       ,  )
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_choices)				)







						def  UpperCamelCase__ (		self				):






												"""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       __SCREAMING_SNAKE_CASE   (				__lowercase						,   __lowercase						,   unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:    Optional[int]						=   (
						    (
						        DistilBertModel,
						        DistilBertForMaskedLM,
						        DistilBertForMultipleChoice,
						        DistilBertForQuestionAnswering,
						        DistilBertForSequenceClassification,
						        DistilBertForTokenClassification,
						    )
						    if is_torch_available()
						    else None
						)
						_SCREAMING_SNAKE_CASE		:    int						=   (
						    {
						        '''feature-extraction''': DistilBertModel,
						        '''fill-mask''': DistilBertForMaskedLM,
						        '''question-answering''': DistilBertForQuestionAnswering,
						        '''text-classification''': DistilBertForSequenceClassification,
						        '''token-classification''': DistilBertForTokenClassification,
						        '''zero-shot''': DistilBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						_SCREAMING_SNAKE_CASE		:    Optional[Any]						=   True
						_SCREAMING_SNAKE_CASE		:    Optional[Any]						=   True
						_SCREAMING_SNAKE_CASE		:    Optional[int]						=   True
						_SCREAMING_SNAKE_CASE		:    Tuple						=   True






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  DistilBertModelTester(self				)
												lowerCAmelCase__        =  ConfigTester(self       ,  config_class=_UpperCamelCase       ,  dim=37				)






						def  UpperCamelCase__ (		self				):






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






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_model(*_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_masked_lm(*_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_question_answering(*_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_sequence_classification(*_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_token_classification(*_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_multiple_choice(*_UpperCamelCase				)






						@slow
						def  UpperCamelCase__ (		self				):






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







						@slow
						@require_torch_gpu
						def  UpperCamelCase__ (		self				):






												"""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=_UpperCamelCase				)

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

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




@require_torch
class       __SCREAMING_SNAKE_CASE   (				unittest.TestCase):






						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  DistilBertModel.from_pretrained('distilbert-base-uncased'				)
												lowerCAmelCase__        =  torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]				)
												lowerCAmelCase__        =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
												with torch.no_grad():
																		lowerCAmelCase__        =  model(_UpperCamelCase       ,  attention_mask=_UpperCamelCase				)[0]
												lowerCAmelCase__        =  torch.Size((1, 11, 7_68)				)
												self.assertEqual(output.shape       ,  _UpperCamelCase				)
												lowerCAmelCase__        =  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]       ,  _UpperCamelCase       ,  atol=1E-4				)				)













def 		_UpperCamelCase				(      UpperCamelCase_    :   str						)					->				str:

						"""simple docstring"""



						return " ".join(input_str.split()[::-1]						)


if __name__ == "__main__":
					import doctest

					doctest.testmod()





