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__						(   __lowerCamelCase):


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





												'''simple docstring'''





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


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size						)

												SCREAMING_SNAKE_CASE							:    Tuple							=							None
												if self.use_input_mask:
																		SCREAMING_SNAKE_CASE							:    str							=							random_attention_mask([self.batch_size, self.seq_length]						)

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

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.get_config()

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


						def        __A       (       self	:				List[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	:				Dict    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							DistilBertModel(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    str							=							model(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							model(UpperCamelCase__						)
												self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)						)


						def        __A       (       self	:				Any    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							DistilBertForMaskedLM(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)						)


						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							DistilBertForQuestionAnswering(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Dict							=							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        __A       (       self	:				str    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.num_labels
												SCREAMING_SNAKE_CASE							:    str							=							DistilBertForSequenceClassification(UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Tuple							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)						)


						def        __A       (       self	:				Any    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							self.num_labels
												SCREAMING_SNAKE_CASE							:    List[str]							=							DistilBertForTokenClassification(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()

												SCREAMING_SNAKE_CASE							:    str							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.num_labels)						)


						def        __A       (       self	:				Optional[Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				List[Any]						):





												'''simple docstring'''





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



						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





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






@require_torch
class 					lowercase__						(   __lowerCamelCase	,							__lowerCamelCase	,							unittest.TestCase):
						UpperCamelCase_     =		(
						    (
						        DistilBertModel,
						        DistilBertForMaskedLM,
						        DistilBertForMultipleChoice,
						        DistilBertForQuestionAnswering,
						        DistilBertForSequenceClassification,
						        DistilBertForTokenClassification,
						    )
						    if is_torch_available()
						    else None
						)
						UpperCamelCase_     =		(
						    {
						        """feature-extraction""": DistilBertModel,
						        """fill-mask""": DistilBertForMaskedLM,
						        """question-answering""": DistilBertForQuestionAnswering,
						        """text-classification""": DistilBertForSequenceClassification,
						        """token-classification""": DistilBertForTokenClassification,
						        """zero-shot""": DistilBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCamelCase_     =		True
						UpperCamelCase_     =		True
						UpperCamelCase_     =		True
						UpperCamelCase_     =		True


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							DistilBertModelTester(self						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							ConfigTester(self    ,		config_class=UpperCamelCase__    ,		dim=37						)


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__						)


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__						)


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__						)


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__						)


						@slow
						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE							:    Tuple							=							DistilBertModel.from_pretrained(UpperCamelCase__						)
																		self.assertIsNotNone(UpperCamelCase__						)



						@slow
						@require_torch_gpu
						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    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

																		SCREAMING_SNAKE_CASE							:    Tuple							=							True
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							model_class(config=UpperCamelCase__						)

																		SCREAMING_SNAKE_CASE							:    List[Any]							=							self._prepare_for_class(UpperCamelCase__    ,		UpperCamelCase__						)
																		SCREAMING_SNAKE_CASE							:    Dict							=							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'''						)						)
																								SCREAMING_SNAKE_CASE							:    int							=							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):


						@slow
						def        __A       (       self	:				str						):





												'''simple docstring'''





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












from . import __version__

# Backward compatibility imports, to make sure all those objects can be found in file_utils
from .utils import (
    CLOUDFRONT_DISTRIB_PREFIX,
    CONFIG_NAME,
    DISABLE_TELEMETRY,
    DUMMY_INPUTS,
    DUMMY_MASK,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    FEATURE_EXTRACTOR_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_PREFIX,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    MODEL_CARD_NAME,
    MULTIPLE_CHOICE_DUMMY_INPUTS,
    PYTORCH_PRETRAINED_BERT_CACHE,
    PYTORCH_TRANSFORMERS_CACHE,
    S3_BUCKET_PREFIX,
    SENTENCEPIECE_UNDERLINE,
    SPIECE_UNDERLINE,
    TF2_WEIGHTS_NAME,
    TF_WEIGHTS_NAME,
    TORCH_FX_REQUIRED_VERSION,
    TRANSFORMERS_CACHE,
    TRANSFORMERS_DYNAMIC_MODULE_NAME,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    WEIGHTS_INDEX_NAME,
    WEIGHTS_NAME,
    ContextManagers,
    DummyObject,
    EntryNotFoundError,
    ExplicitEnum,
    ModelOutput,
    PaddingStrategy,
    PushToHubMixin,
    RepositoryNotFoundError,
    RevisionNotFoundError,
    TensorType,
    _LazyModule,
    add_code_sample_docstrings,
    add_end_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    cached_property,
    copy_func,
    default_cache_path,
    define_sagemaker_information,
    get_cached_models,
    get_file_from_repo,
    get_full_repo_name,
    get_torch_version,
    has_file,
    http_user_agent,
    is_apex_available,
    is_bsa_available,
    is_coloredlogs_available,
    is_datasets_available,
    is_detectrona_available,
    is_faiss_available,
    is_flax_available,
    is_ftfy_available,
    is_in_notebook,
    is_ipex_available,
    is_librosa_available,
    is_offline_mode,
    is_onnx_available,
    is_pandas_available,
    is_phonemizer_available,
    is_protobuf_available,
    is_psutil_available,
    is_pyanvml_available,
    is_pyctcdecode_available,
    is_pytesseract_available,
    is_pytorch_quantization_available,
    is_rjieba_available,
    is_sagemaker_dp_enabled,
    is_sagemaker_mp_enabled,
    is_scipy_available,
    is_sentencepiece_available,
    is_seqio_available,
    is_sklearn_available,
    is_soundfile_availble,
    is_spacy_available,
    is_speech_available,
    is_tensor,
    is_tensorflow_probability_available,
    is_tfaonnx_available,
    is_tf_available,
    is_timm_available,
    is_tokenizers_available,
    is_torch_available,
    is_torch_bfaa_available,
    is_torch_cuda_available,
    is_torch_fx_available,
    is_torch_fx_proxy,
    is_torch_mps_available,
    is_torch_tfaa_available,
    is_torch_tpu_available,
    is_torchaudio_available,
    is_training_run_on_sagemaker,
    is_vision_available,
    replace_return_docstrings,
    requires_backends,
    to_numpy,
    to_py_obj,
    torch_only_method,
)






