





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       __A     (						a__    ):


				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   , ):
								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 _snake_case       (		self							):
								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 _snake_case       (		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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								lowerCamelCase	 =DistilBertModel(config=_A							)
								model.to(_A							)
								model.eval()
								lowerCamelCase	 =model(_A   , _A							)
								lowerCamelCase	 =model(_A							)
								self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size)							)


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								lowerCamelCase	 =DistilBertForMaskedLM(config=_A							)
								model.to(_A							)
								model.eval()
								lowerCamelCase	 =model(_A   , attention_mask=_A   , labels=_A							)
								self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.vocab_size)							)


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								lowerCamelCase	 =DistilBertForQuestionAnswering(config=_A							)
								model.to(_A							)
								model.eval()
								lowerCamelCase	 =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 _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								lowerCamelCase	 =self.num_labels
								lowerCamelCase	 =DistilBertForSequenceClassification(_A							)
								model.to(_A							)
								model.eval()
								lowerCamelCase	 =model(_A   , attention_mask=_A   , labels=_A							)
								self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_labels)							)


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								lowerCamelCase	 =self.num_labels
								lowerCamelCase	 =DistilBertForTokenClassification(config=_A							)
								model.to(_A							)
								model.eval()

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


				def _snake_case       (		self   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_   , UpperCAmelCase_							):
								lowerCamelCase	 =self.num_choices
								lowerCamelCase	 =DistilBertForMultipleChoice(config=_A							)
								model.to(_A							)
								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(
								    _A   , attention_mask=_A   , labels=_A   , )
								self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_choices)							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.prepare_config_and_inputs()
								(lowerCamelCase)	 =config_and_inputs
								lowerCamelCase	 ={'input_ids': input_ids, 'attention_mask': input_mask}
								return config, inputs_dict




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


				def _snake_case       (		self							):
								lowerCamelCase	 =DistilBertModelTester(self							)
								lowerCamelCase	 =ConfigTester(self   , config_class=_A   , dim=37							)


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


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_model(*_A							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_masked_lm(*_A							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_question_answering(*_A							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_sequence_classification(*_A							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_token_classification(*_A							)


				def _snake_case       (		self							):
								lowerCamelCase	 =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_multiple_choice(*_A							)


				@slow
				def _snake_case       (		self							):

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


				@slow
				@require_torch_gpu
				def _snake_case       (		self							):
								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=_A							)

												lowerCamelCase	 =self._prepare_for_class(_A   , _A							)
												lowerCamelCase	 =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	 =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       __A     (						unittest.TestCase    ):


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












from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCAmelCase__						:			List[Any]									={'''configuration_yolos''': ['''YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''YolosConfig''', '''YolosOnnxConfig''']}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__						:			Dict									=['''YolosFeatureExtractor''']
							UpperCAmelCase__						:			List[Any]									=['''YolosImageProcessor''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__						:			Optional[Any]									=[
							    '''YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''YolosForObjectDetection''',
							    '''YolosModel''',
							    '''YolosPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_yolos import YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP, YolosConfig, YolosOnnxConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_yolos import YolosFeatureExtractor
														from .image_processing_yolos import YolosImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_yolos import (
														    YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST,
														    YolosForObjectDetection,
														    YolosModel,
														    YolosPreTrainedModel,
														)


else:
							import sys

							UpperCAmelCase__						:			Optional[Any]									=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





