





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







					'''simple docstring'''




					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=512	,			_UpperCAmelCase=16	,			_UpperCAmelCase=2	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=None	,			):
									__a :						Union[str, Any]			       =  parent
									__a :						Union[str, Any]			       =  batch_size
									__a :						Optional[int]			       =  seq_length
									__a :						Any			       =  is_training
									__a :						List[Any]			       =  use_input_mask
									__a :						int			       =  use_token_type_ids
									__a :						List[Any]			       =  use_labels
									__a :						Optional[int]			       =  vocab_size
									__a :						Any			       =  hidden_size
									__a :						Optional[Any]			       =  num_hidden_layers
									__a :						str			       =  num_attention_heads
									__a :						Optional[int]			       =  intermediate_size
									__a :						List[str]			       =  hidden_act
									__a :						Tuple			       =  hidden_dropout_prob
									__a :						int			       =  attention_probs_dropout_prob
									__a :						Union[str, Any]			       =  max_position_embeddings
									__a :						Dict			       =  type_vocab_size
									__a :						int			       =  type_sequence_label_size
									__a :						List[str]			       =  initializer_range
									__a :						Optional[Any]			       =  num_labels
									__a :						int			       =  num_choices
									__a :						Any			       =  scope







					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  ids_tensor([self.batch_size, self.seq_length]	,			self.vocab_size     )

									__a :						int			       =  None
									if self.use_input_mask:
													__a :						Union[str, Any]			       =  random_attention_mask([self.batch_size, self.seq_length]     )

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

									__a :						Dict			       =  self.get_config()

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







					def  _lowerCamelCase		(						self     ):
									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	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Dict			       =  DistilBertModel(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						List[Any]			       =  model(_UpperCAmelCase	,			_UpperCAmelCase     )
									__a :						Any			       =  model(_UpperCAmelCase     )
									self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						str			       =  DistilBertForMaskedLM(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						str			       =  model(_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			labels=_UpperCAmelCase     )
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.vocab_size)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Optional[int]			       =  DistilBertForQuestionAnswering(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						Optional[int]			       =  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  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Any			       =  self.num_labels
									__a :						Union[str, Any]			       =  DistilBertForSequenceClassification(_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						Optional[Any]			       =  model(_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			labels=_UpperCAmelCase     )
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.num_labels)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Any			       =  self.num_labels
									__a :						Dict			       =  DistilBertForTokenClassification(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()

									__a :						Union[str, Any]			       =  model(_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			labels=_UpperCAmelCase     )
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.num_labels)     )







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







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  self.prepare_config_and_inputs()
									((__a)       ,						(__a)       ,						(__a)       ,						(__a)       ,						(__a)       ,						(__a)) :						str			       =  config_and_inputs
									__a :						Optional[Any]			       =  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_torch
class 				__lowercase			(			_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     ):
									__a :						str			       =  DistilBertModelTester(self     )
									__a :						Optional[int]			       =  ConfigTester(self	,			config_class=_UpperCAmelCase	,			dim=37     )







					def  _lowerCamelCase		(						self     ):
									self.config_tester.run_common_tests()







					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_model(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_masked_lm(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[Any]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_question_answering(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_sequence_classification(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_token_classification(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_multiple_choice(*_UpperCAmelCase     )







					@slow
					def  _lowerCamelCase		(						self     ):
									for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a :						Dict			       =  DistilBertModel.from_pretrained(_UpperCAmelCase     )
													self.assertIsNotNone(_UpperCAmelCase     )







					@slow
					@require_torch_gpu
					def  _lowerCamelCase		(						self     ):
									__a       ,						__a :						Dict			       =  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 :						Optional[int]			       =  True
													__a :						Union[str, Any]			       =  model_class(config=_UpperCAmelCase     )

													__a :						List[str]			       =  self._prepare_for_class(_UpperCAmelCase	,			_UpperCAmelCase     )
													__a :						Optional[int]			       =  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'''     )     )
																	__a :						List[str]			       =  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 				__lowercase			(			unittest.TestCase							):







					'''simple docstring'''




					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						Dict			       =  DistilBertModel.from_pretrained('''distilbert-base-uncased'''     )
									__a :						Any			       =  torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]     )
									__a :						Tuple			       =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
									with torch.no_grad():
													__a :						str			       =  model(_UpperCAmelCase	,			attention_mask=_UpperCAmelCase     )[0]
									__a :						Tuple			       =  torch.Size((1, 11, 768)     )
									self.assertEqual(output.shape	,			_UpperCAmelCase     )
									__a :						Optional[Any]			       =  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]	,			_UpperCAmelCase	,			atol=1e-4     )     )






"""simple docstring"""






import copy
import re




class 				__lowercase			:







					'''simple docstring'''




					__lowerCAmelCase  =  '''hp'''
					__lowerCAmelCase  =  {}
					__lowerCAmelCase  =  None







					@classmethod
					def  _lowerCamelCase		(						cls	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Optional[int]			       =  prefix
									__a :						List[str]			       =  defaults
									cls.build_naming_info()







					@staticmethod
					def  _lowerCamelCase		(						_UpperCAmelCase	,			_UpperCAmelCase     ):
									if len(_UpperCAmelCase     ) == 0:
													return ""
									__a :						Optional[int]			       =  None
									if any(char.isdigit() for char in word     ):
													raise Exception(f"""Parameters should not contain numbers: '{word}' contains a number"""     )
									if word in info["short_word"]:
													return info["short_word"][word]
									for prefix_len in range(1	,			len(_UpperCAmelCase     ) + 1     ):
													__a :						str			       =  word[:prefix_len]
													if prefix in info["reverse_short_word"]:
																	continue
													else:
																	__a :						Tuple			       =  prefix
																	break

									if short_word is None:
													# Paranoid fallback
													def int_to_alphabetic(_UpperCAmelCase     ):
																	__a :						List[str]			       =  ''''''
																	while integer != 0:
																					__a :						Union[str, Any]			       =  chr(ord('''A'''     ) + integer % 10     ) + s
																					integer //= 10
																	return s

													__a :						Optional[int]			       =  0
													while True:
																	__a :						List[str]			       =  word + '''#''' + int_to_alphabetic(_UpperCAmelCase     )
																	if sword in info["reverse_short_word"]:
																					continue
																	else:
																					__a :						Optional[Any]			       =  sword
																					break

									__a :						List[str]			       =  short_word
									__a :						Dict			       =  word
									return short_word







					@staticmethod
					def  _lowerCamelCase		(						_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						str			       =  param_name.split('''_'''     )

									__a :						str			       =  [TrialShortNamer.shortname_for_word(_UpperCAmelCase	,			_UpperCAmelCase     ) for word in words]

									# We try to create a separatorless short name, but if there is a collision we have to fallback
									# to a separated short name
									__a :						Dict			       =  ['''''', '''_''']

									for separator in separators:
													__a :						Union[str, Any]			       =  separator.join(_UpperCAmelCase     )
													if shortname not in info["reverse_short_param"]:
																	__a :						List[str]			       =  shortname
																	__a :						Union[str, Any]			       =  param_name
																	return shortname

									return param_name







					@staticmethod
					def  _lowerCamelCase		(						_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						int			       =  TrialShortNamer.shortname_for_key(_UpperCAmelCase	,			_UpperCAmelCase     )
									__a :						Optional[Any]			       =  short_name
									__a :						Optional[Any]			       =  param_name







					@classmethod
					def  _lowerCamelCase		(						cls     ):
									if cls.NAMING_INFO is not None:
													return

									__a :						Optional[Any]			       =  {
									    '''short_word''': {},
									    '''reverse_short_word''': {},
									    '''short_param''': {},
									    '''reverse_short_param''': {},
									}

									__a :						List[str]			       =  list(cls.DEFAULTS.keys()     )

									for k in field_keys:
													cls.add_new_param_name(_UpperCAmelCase	,			_UpperCAmelCase     )

									__a :						Optional[Any]			       =  info







					@classmethod
					def  _lowerCamelCase		(						cls	,			_UpperCAmelCase     ):
									cls.build_naming_info()
									assert cls.PREFIX is not None
									__a :						str			       =  [copy.copy(cls.PREFIX     )]

									for k, v in params.items():
													if k not in cls.DEFAULTS:
																	raise Exception(f"""You should provide a default value for the param name {k} with value {v}"""     )
													if v == cls.DEFAULTS[k]:
																	# The default value is not added to the name
																	continue

													__a :						Union[str, Any]			       =  cls.NAMING_INFO['''short_param'''][k]

													if isinstance(_UpperCAmelCase	,			_UpperCAmelCase     ):
																	__a :						str			       =  1 if v else 0

													__a :						Tuple			       =  '''''' if isinstance(_UpperCAmelCase	,			(int, float)     ) else '''-'''
													__a :						Dict			       =  f"""{key}{sep}{v}"""
													name.append(_UpperCAmelCase     )

									return "_".join(_UpperCAmelCase     )







					@classmethod
					def  _lowerCamelCase		(						cls	,			_UpperCAmelCase     ):
									__a :						List[Any]			       =  repr[len(cls.PREFIX     ) + 1 :]
									if repr == "":
													__a :						Tuple			       =  []
									else:
													__a :						str			       =  repr.split('''_'''     )

									__a :						Optional[Any]			       =  {}

									for value in values:
													if "-" in value:
																	__a       ,						__a :						List[Any]			       =  value.split('''-'''     )
													else:
																	__a :						int			       =  re.sub('''[0-9.]'''	,			''''''	,			_UpperCAmelCase     )
																	__a :						Union[str, Any]			       =  float(re.sub('''[^0-9.]'''	,			''''''	,			_UpperCAmelCase     )     )

													__a :						Dict			       =  cls.NAMING_INFO['''reverse_short_param'''][p_k]

													__a :						Union[str, Any]			       =  p_v

									for k in cls.DEFAULTS:
													if k not in parameters:
																	__a :						Optional[int]			       =  cls.DEFAULTS[k]

									return parameters