






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  lowerCAmelCase_       (				snake_case__					):




							def __init__(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=13 ,							SCREAMING_SNAKE_CASE_     :   Any=7 ,							SCREAMING_SNAKE_CASE_     :   Any=True ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=True ,							SCREAMING_SNAKE_CASE_     :   List[Any]=False ,							SCREAMING_SNAKE_CASE_     :   Tuple=True ,							SCREAMING_SNAKE_CASE_     :   str=99 ,							SCREAMING_SNAKE_CASE_     :   Any=32 ,							SCREAMING_SNAKE_CASE_     :   Tuple=5 ,							SCREAMING_SNAKE_CASE_     :   int=4 ,							SCREAMING_SNAKE_CASE_     :   List[str]=37 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]="gelu" ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=0.1 ,							SCREAMING_SNAKE_CASE_     :   Any=0.1 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=512 ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=16 ,							SCREAMING_SNAKE_CASE_     :   Dict=2 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=0.02 ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]=3 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=4 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=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     :   List[str]	):
											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     :   Any	):
											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     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Tuple	):
											lowerCAmelCase__      			=						DistilBertModel(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.hidden_size)	)




							def 						__snake_case					(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   List[str]	):
											lowerCAmelCase__      			=						DistilBertForMaskedLM(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							labels=SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)	)




							def 						__snake_case					(   self     :   str ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   List[Any]	):
											lowerCAmelCase__      			=						DistilBertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(
											    SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							start_positions=SCREAMING_SNAKE_CASE_ ,							end_positions=SCREAMING_SNAKE_CASE_	)
											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     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Dict	):
											lowerCAmelCase__      			=						self.num_labels
											lowerCAmelCase__      			=						DistilBertForSequenceClassification(SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							labels=SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)	)




							def 						__snake_case					(   self     :   str ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   List[str]	):
											lowerCAmelCase__      			=						self.num_labels
											lowerCAmelCase__      			=						DistilBertForTokenClassification(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()

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




							def 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											lowerCAmelCase__      			=						self.num_choices
											lowerCAmelCase__      			=						DistilBertForMultipleChoice(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											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(
											    SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							labels=SCREAMING_SNAKE_CASE_ ,							)
											self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_choices)	)




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											((lowerCAmelCase__)			,		(lowerCAmelCase__)			,		(lowerCAmelCase__)			,		(lowerCAmelCase__)			,		(lowerCAmelCase__)			,		(lowerCAmelCase__))      			=						config_and_inputs
											lowerCAmelCase__      			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
											return config, inputs_dict



@require_torch
class  lowerCAmelCase_       (				snake_case__     ,    snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Any		      =    (
							    (
							        DistilBertModel,
							        DistilBertForMaskedLM,
							        DistilBertForMultipleChoice,
							        DistilBertForQuestionAnswering,
							        DistilBertForSequenceClassification,
							        DistilBertForTokenClassification,
							    )
							    if is_torch_available()
							    else None
							)
							UpperCamelCase_		:Optional[int]		      =    (
							    {
							        'feature-extraction': DistilBertModel,
							        'fill-mask': DistilBertForMaskedLM,
							        'question-answering': DistilBertForQuestionAnswering,
							        'text-classification': DistilBertForSequenceClassification,
							        'token-classification': DistilBertForTokenClassification,
							        'zero-shot': DistilBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							UpperCamelCase_		:Dict		      =    True
							UpperCamelCase_		:List[str]		      =    True
							UpperCamelCase_		:str		      =    True
							UpperCamelCase_		:Dict		      =    True




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						DistilBertModelTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=SCREAMING_SNAKE_CASE_ ,							dim=37	)




							def 						__snake_case					(   self     :   List[str]	):
											self.config_tester.run_common_tests()




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_model(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_masked_lm(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_question_answering(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_sequence_classification(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_token_classification(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_multiple_choice(*SCREAMING_SNAKE_CASE_	)




							@slow
							def 						__snake_case					(   self     :   Optional[int]	):
											for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCAmelCase__      			=						DistilBertModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
															self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)




							@slow
							@require_torch_gpu
							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__			,		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=SCREAMING_SNAKE_CASE_	)

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

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



@require_torch
class  lowerCAmelCase_       (				unittest.TestCase					):




							@slow
							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__      			=						DistilBertModel.from_pretrained('''distilbert-base-uncased'''	)
											lowerCAmelCase__      			=						torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
											lowerCAmelCase__      			=						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
											with torch.no_grad():
															lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_	)[0]
											lowerCAmelCase__      			=						torch.Size((1, 11, 768)	)
											self.assertEqual(output.shape ,							SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						torch.tensor(
											    [[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]]	)

											self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,							SCREAMING_SNAKE_CASE_ ,							atol=1e-4	)	)













from typing import TYPE_CHECKING

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


_UpperCAmelCase			:		Optional[Any]   				=    {
    "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"],
    "feature_extraction_mctct": ["MCTCTFeatureExtractor"],
    "processing_mctct": ["MCTCTProcessor"],
}


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_UpperCAmelCase			:		str   				=    [
			    "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "MCTCTForCTC",
			    "MCTCTModel",
			    "MCTCTPreTrainedModel",
			]


if TYPE_CHECKING:
			from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig
			from .feature_extraction_mctct import MCTCTFeatureExtractor
			from .processing_mctct import MCTCTProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel

else:
			import sys

			_UpperCAmelCase			:		List[str]   				=    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





