




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



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





								'''simple docstring'''




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



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase    :    List[Any]										=      ids_tensor([self.batch_size, self.seq_length]		,	self.vocab_size      )

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

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

								_lowerCAmelCase    :    Optional[int]										=      self.get_config()

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



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]      )  ->    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       SCREAMING_SNAKE_CASE       (      self      :		Union[str, Any]		,	_UpperCAmelCase      :		Any		,	_UpperCAmelCase      :		str		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		List[Any]		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		Any      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      DistilBertModel(config=__a      )
								model.to(__a      )
								model.eval()
								_lowerCAmelCase    :    Dict										=      model(__a		,	__a      )
								_lowerCAmelCase    :    str										=      model(__a      )
								self.parent.assertEqual(result.last_hidden_state.shape		,	(self.batch_size, self.seq_length, self.hidden_size)      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Tuple		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		Optional[Any]		,	_UpperCAmelCase      :		List[Any]		,	_UpperCAmelCase      :		List[str]		,	_UpperCAmelCase      :		Optional[Any]		,	_UpperCAmelCase      :		Any      )  ->    int:





								'''simple docstring'''




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



				def       SCREAMING_SNAKE_CASE       (      self      :		Optional[int]		,	_UpperCAmelCase      :		Tuple		,	_UpperCAmelCase      :		Tuple		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		Dict		,	_UpperCAmelCase      :		Tuple		,	_UpperCAmelCase      :		Optional[Any]      )  ->    Optional[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Union[str, Any]										=      DistilBertForQuestionAnswering(config=__a      )
								model.to(__a      )
								model.eval()
								_lowerCAmelCase    :    Union[str, Any]										=      model(
								    __a		,	attention_mask=__a		,	start_positions=__a		,	end_positions=__a      )
								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       SCREAMING_SNAKE_CASE       (      self      :		Union[str, Any]		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		Any		,	_UpperCAmelCase      :		Optional[int]		,	_UpperCAmelCase      :		Optional[int]		,	_UpperCAmelCase      :		int		,	_UpperCAmelCase      :		List[str]      )  ->    Optional[int]:





								'''simple docstring'''




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



				def       SCREAMING_SNAKE_CASE       (      self      :		Union[str, Any]		,	_UpperCAmelCase      :		Optional[int]		,	_UpperCAmelCase      :		Any		,	_UpperCAmelCase      :		str		,	_UpperCAmelCase      :		Optional[Any]		,	_UpperCAmelCase      :		Tuple		,	_UpperCAmelCase      :		Dict      )  ->    str:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[int]										=      self.num_labels
								_lowerCAmelCase    :    Dict										=      DistilBertForTokenClassification(config=__a      )
								model.to(__a      )
								model.eval()

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



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





								'''simple docstring'''




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



				def       SCREAMING_SNAKE_CASE       (      self      :		Union[str, Any]      )  ->    Union[str, Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    Any										=      self.prepare_config_and_inputs()
								(_lowerCAmelCase)    :    List[str]										=      config_and_inputs
								_lowerCAmelCase    :    Tuple										=      {"input_ids": input_ids, "attention_mask": input_mask}
								return config, inputs_dict






@require_torch
class        __snake_case  (_UpperCamelCase						,	_UpperCamelCase						,	unittest.TestCase		):
				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       SCREAMING_SNAKE_CASE       (      self      :		Any      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase    :    Tuple										=      DistilBertModelTester(self      )
								_lowerCAmelCase    :    Optional[Any]										=      ConfigTester(self		,	config_class=__a		,	dim=37      )



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





								'''simple docstring'''




								self.config_tester.run_common_tests()



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase    :    str										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_model(*__a      )



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





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[Any]										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_masked_lm(*__a      )



				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    List[Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    List[Any]										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_question_answering(*__a      )



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    List[str]:





								'''simple docstring'''




								_lowerCAmelCase    :    List[str]										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_sequence_classification(*__a      )



				def       SCREAMING_SNAKE_CASE       (      self      :		List[str]      )  ->    Any:





								'''simple docstring'''




								_lowerCAmelCase    :    Dict										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_token_classification(*__a      )



				def       SCREAMING_SNAKE_CASE       (      self      :		Tuple      )  ->    Dict:





								'''simple docstring'''




								_lowerCAmelCase    :    Optional[Any]										=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_multiple_choice(*__a      )



				@slow
				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    str:





								'''simple docstring'''




								for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_lowerCAmelCase    :    Optional[int]										=      DistilBertModel.from_pretrained(__a      )
												self.assertIsNotNone(__a      )



				@slow
				@require_torch_gpu
				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    Tuple:





								'''simple docstring'''




								_lowerCAmelCase    :    str										=      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    :    Tuple										=      True
												_lowerCAmelCase    :    Tuple										=      model_class(config=__a      )

												_lowerCAmelCase    :    int										=      self._prepare_for_class(__a		,	__a      )
												_lowerCAmelCase    :    str										=      torch.jit.trace(
												    __a		,	(inputs_dict["""input_ids"""].to("""cpu"""      ), inputs_dict["""attention_mask"""].to("""cpu"""      ))      )

												with tempfile.TemporaryDirectory() as tmp:
																torch.jit.save(__a		,	os.path.join(__a		,	"""traced_model.pt"""      )      )
																_lowerCAmelCase    :    Optional[int]										=      torch.jit.load(os.path.join(__a		,	"""traced_model.pt"""      )		,	map_location=__a      )
																loaded(inputs_dict["""input_ids"""].to(__a      )		,	inputs_dict["""attention_mask"""].to(__a      )      )






@require_torch
class        __snake_case  (unittest.TestCase		):



				@slow
				def       SCREAMING_SNAKE_CASE       (      self      :		str      )  ->    Union[str, Any]:





								'''simple docstring'''




								_lowerCAmelCase    :    List[Any]										=      DistilBertModel.from_pretrained("""distilbert-base-uncased"""      )
								_lowerCAmelCase    :    int										=      torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
								_lowerCAmelCase    :    Any										=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
								with torch.no_grad():
												_lowerCAmelCase    :    Any										=      model(__a		,	attention_mask=__a      )[0]
								_lowerCAmelCase    :    str										=      torch.Size((1, 11, 768)      )
								self.assertEqual(output.shape		,	__a      )
								_lowerCAmelCase    :    Optional[int]										=      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]		,	__a		,	atol=1E-4      )      )







def 							_UpperCAmelCase  (UpperCamelCase_ : str       ):
				'''simple docstring'''
				_lowerCAmelCase    :    Dict										=      [0] * len(UpperCamelCase_       )

				for i in range(1      ,    len(UpperCamelCase_       )       ):
								# use last results for better performance - dynamic programming
								_lowerCAmelCase    :    Union[str, Any]										=      prefix_result[i - 1]
								while j > 0 and input_string[i] != input_string[j]:
												_lowerCAmelCase    :    str										=      prefix_result[j - 1]

								if input_string[i] == input_string[j]:
												j += 1
								_lowerCAmelCase    :    Optional[int]										=      j

				return prefix_result




def 							_UpperCAmelCase  (UpperCamelCase_ : str       ):
				'''simple docstring'''
				return max(prefix_function(UpperCamelCase_       )       )


if __name__ == "__main__":
	import doctest

	doctest.testmod()

