


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




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

										"""simple docstring"""




										UpperCAmelCase								=      parent
										UpperCAmelCase								=      batch_size
										UpperCAmelCase								=      seq_length
										UpperCAmelCase								=      is_training
										UpperCAmelCase								=      use_input_mask
										UpperCAmelCase								=      use_token_type_ids
										UpperCAmelCase								=      use_labels
										UpperCAmelCase								=      vocab_size
										UpperCAmelCase								=      hidden_size
										UpperCAmelCase								=      num_hidden_layers
										UpperCAmelCase								=      num_attention_heads
										UpperCAmelCase								=      intermediate_size
										UpperCAmelCase								=      hidden_act
										UpperCAmelCase								=      hidden_dropout_prob
										UpperCAmelCase								=      attention_probs_dropout_prob
										UpperCAmelCase								=      max_position_embeddings
										UpperCAmelCase								=      type_vocab_size
										UpperCAmelCase								=      type_sequence_label_size
										UpperCAmelCase								=      initializer_range
										UpperCAmelCase								=      num_labels
										UpperCAmelCase								=      num_choices
										UpperCAmelCase								=      scope




				def 	a__(						self     :					int )->	Dict:

										"""simple docstring"""




										UpperCAmelCase								=      ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size )

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

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

										UpperCAmelCase								=      self.get_config()

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




				def 	a__(						self     :					Any )->	Any:

										"""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 	a__(						self     :					int ,       lowerCAmelCase     :					Optional[Any] ,       lowerCAmelCase     :					Tuple ,       lowerCAmelCase     :					List[str] ,       lowerCAmelCase     :					Tuple ,       lowerCAmelCase     :					Any ,       lowerCAmelCase     :					Dict )->	Optional[int]:

										"""simple docstring"""




										UpperCAmelCase								=      DistilBertModel(config=lowerCAmelCase )
										model.to(lowerCAmelCase )
										model.eval()
										UpperCAmelCase								=      model(lowerCAmelCase ,       lowerCAmelCase )
										UpperCAmelCase								=      model(lowerCAmelCase )
										self.parent.assertEqual(result.last_hidden_state.shape ,       (self.batch_size, self.seq_length, self.hidden_size) )




				def 	a__(						self     :					int ,       lowerCAmelCase     :					Optional[Any] ,       lowerCAmelCase     :					Tuple ,       lowerCAmelCase     :					Optional[Any] ,       lowerCAmelCase     :					List[str] ,       lowerCAmelCase     :					Optional[Any] ,       lowerCAmelCase     :					Optional[Any] )->	Dict:

										"""simple docstring"""




										UpperCAmelCase								=      DistilBertForMaskedLM(config=lowerCAmelCase )
										model.to(lowerCAmelCase )
										model.eval()
										UpperCAmelCase								=      model(lowerCAmelCase ,       attention_mask=lowerCAmelCase ,       labels=lowerCAmelCase )
										self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length, self.vocab_size) )




				def 	a__(						self     :					Tuple ,       lowerCAmelCase     :					List[str] ,       lowerCAmelCase     :					Any ,       lowerCAmelCase     :					List[str] ,       lowerCAmelCase     :					Dict ,       lowerCAmelCase     :					str ,       lowerCAmelCase     :					str )->	Any:

										"""simple docstring"""




										UpperCAmelCase								=      DistilBertForQuestionAnswering(config=lowerCAmelCase )
										model.to(lowerCAmelCase )
										model.eval()
										UpperCAmelCase								=      model(
										    lowerCAmelCase ,       attention_mask=lowerCAmelCase ,       start_positions=lowerCAmelCase ,       end_positions=lowerCAmelCase )
										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 	a__(						self     :					Union[str, Any] ,       lowerCAmelCase     :					str ,       lowerCAmelCase     :					Dict ,       lowerCAmelCase     :					Dict ,       lowerCAmelCase     :					Union[str, Any] ,       lowerCAmelCase     :					Optional[int] ,       lowerCAmelCase     :					Dict )->	Dict:

										"""simple docstring"""




										UpperCAmelCase								=      self.num_labels
										UpperCAmelCase								=      DistilBertForSequenceClassification(lowerCAmelCase )
										model.to(lowerCAmelCase )
										model.eval()
										UpperCAmelCase								=      model(lowerCAmelCase ,       attention_mask=lowerCAmelCase ,       labels=lowerCAmelCase )
										self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels) )




				def 	a__(						self     :					int ,       lowerCAmelCase     :					Union[str, Any] ,       lowerCAmelCase     :					Optional[Any] ,       lowerCAmelCase     :					int ,       lowerCAmelCase     :					Optional[int] ,       lowerCAmelCase     :					Optional[int] ,       lowerCAmelCase     :					str )->	str:

										"""simple docstring"""




										UpperCAmelCase								=      self.num_labels
										UpperCAmelCase								=      DistilBertForTokenClassification(config=lowerCAmelCase )
										model.to(lowerCAmelCase )
										model.eval()

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




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

										"""simple docstring"""




										UpperCAmelCase								=      self.num_choices
										UpperCAmelCase								=      DistilBertForMultipleChoice(config=lowerCAmelCase )
										model.to(lowerCAmelCase )
										model.eval()
										UpperCAmelCase								=      input_ids.unsqueeze(1 ).expand(-1 ,       self.num_choices ,       -1 ).contiguous()
										UpperCAmelCase								=      input_mask.unsqueeze(1 ).expand(-1 ,       self.num_choices ,       -1 ).contiguous()
										UpperCAmelCase								=      model(
										    lowerCAmelCase ,       attention_mask=lowerCAmelCase ,       labels=lowerCAmelCase ,       )
										self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_choices) )





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

										"""simple docstring"""




										UpperCAmelCase								=      self.prepare_config_and_inputs()
										(UpperCAmelCase)								=      config_and_inputs
										UpperCAmelCase								=      {"input_ids": input_ids, "attention_mask": input_mask}
										return config, inputs_dict







@require_torch
class 			UpperCamelCase__(  __snake_case		,							__snake_case		,							unittest.TestCase			):
				__magic_name__							: str    						=			(
				    (
				        DistilBertModel,
				        DistilBertForMaskedLM,
				        DistilBertForMultipleChoice,
				        DistilBertForQuestionAnswering,
				        DistilBertForSequenceClassification,
				        DistilBertForTokenClassification,
				    )
				    if is_torch_available()
				    else None
				)
				__magic_name__							: Optional[Any]    						=			(
				    {
				        "feature-extraction": DistilBertModel,
				        "fill-mask": DistilBertForMaskedLM,
				        "question-answering": DistilBertForQuestionAnswering,
				        "text-classification": DistilBertForSequenceClassification,
				        "token-classification": DistilBertForTokenClassification,
				        "zero-shot": DistilBertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				__magic_name__							: Optional[Any]    						=			True
				__magic_name__							: Tuple    						=			True
				__magic_name__							: Union[str, Any]    						=			True
				__magic_name__							: int    						=			True




				def 	a__(						self     :					Dict )->	List[Any]:

										"""simple docstring"""




										UpperCAmelCase								=      DistilBertModelTester(self )
										UpperCAmelCase								=      ConfigTester(self ,       config_class=lowerCAmelCase ,       dim=37 )




				def 	a__(						self     :					int )->	List[str]:

										"""simple docstring"""




										self.config_tester.run_common_tests()




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

										"""simple docstring"""




										UpperCAmelCase								=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase )




				def 	a__(						self     :					Any )->	int:

										"""simple docstring"""




										UpperCAmelCase								=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase )




				def 	a__(						self     :					Dict )->	List[Any]:

										"""simple docstring"""




										UpperCAmelCase								=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase )




				def 	a__(						self     :					str )->	Tuple:

										"""simple docstring"""




										UpperCAmelCase								=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase )




				def 	a__(						self     :					Any )->	List[Any]:

										"""simple docstring"""




										UpperCAmelCase								=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase )




				def 	a__(						self     :					Optional[int] )->	Optional[int]:

										"""simple docstring"""




										UpperCAmelCase								=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase )




				@slow
				def 	a__(						self     :					Optional[Any] )->	Dict:

										"""simple docstring"""




										for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																UpperCAmelCase								=      DistilBertModel.from_pretrained(lowerCAmelCase )
																self.assertIsNotNone(lowerCAmelCase )





				@slow
				@require_torch_gpu
				def 	a__(						self     :					Optional[int] )->	Optional[int]:

										"""simple docstring"""




										UpperCAmelCase								=      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								=      True
																UpperCAmelCase								=      model_class(config=lowerCAmelCase )

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

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







@require_torch
class 			UpperCamelCase__(  unittest.TestCase			):




				@slow
				def 	a__(						self     :					int )->	str:

										"""simple docstring"""




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







from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase_						:     Optional[int]	       =							logging.get_logger(__name__)




class   __lowercase						(						__snake_case       ):
	_A       =   "timm_backbone"






	def __init__(self				: Any      ,	snake_case				: List[Any]=None      ,	snake_case				: int=3      ,	snake_case				: Dict=True      ,	snake_case				: Union[str, Any]=True      ,	snake_case				: Any=None      ,	**snake_case				: int      ,	) ->			Optional[int]:
							super().__init__(**snake_case    )
							_lowercase       :	Dict           =   backbone
							_lowercase       :	Optional[Any]           =   num_channels
							_lowercase       :	Union[str, Any]           =   features_only
							_lowercase       :	Tuple           =   use_pretrained_backbone
							_lowercase       :	List[str]           =   True
							_lowercase       :	Tuple           =   out_indices if out_indices is not None else (-1,)




