


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				:		Tuple							,							__magic_name__				:		Optional[int]							,							__magic_name__				:		Optional[int]=1_3							,							__magic_name__				:		int=7							,							__magic_name__				:		List[str]=True							,							__magic_name__				:		Optional[Any]=True							,							__magic_name__				:		List[str]=False							,							__magic_name__				:		Union[str, Any]=True							,							__magic_name__				:		Any=9_9							,							__magic_name__				:		int=3_2							,							__magic_name__				:		Union[str, Any]=5							,							__magic_name__				:		Dict=4							,							__magic_name__				:		Tuple=3_7							,							__magic_name__				:		List[str]="gelu"							,							__magic_name__				:		Tuple=0.1							,							__magic_name__				:		Dict=0.1							,							__magic_name__				:		Optional[int]=5_1_2							,							__magic_name__				:		List[Any]=1_6							,							__magic_name__				:		int=2							,							__magic_name__				:		str=0.02							,							__magic_name__				:		int=3							,							__magic_name__				:		Dict=4							,							__magic_name__				:		int=None							,							):







						"""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 	lowerCamelCase_       (    self				:		Optional[Any]    ):







						"""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 	lowerCamelCase_       (    self				:		str    ):







						"""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 	lowerCamelCase_       (    self				:		str							,							__magic_name__				:		Dict							,							__magic_name__				:		Any							,							__magic_name__				:		List[Any]							,							__magic_name__				:		Union[str, Any]							,							__magic_name__				:		Any							,							__magic_name__				:		Optional[Any]    ):







						"""simple docstring"""







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




			def 	lowerCamelCase_       (    self				:		List[str]							,							__magic_name__				:		List[Any]							,							__magic_name__				:		Optional[int]							,							__magic_name__				:		int							,							__magic_name__				:		Union[str, Any]							,							__magic_name__				:		Tuple							,							__magic_name__				:		Union[str, Any]    ):







						"""simple docstring"""







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




			def 	lowerCamelCase_       (    self				:		Dict							,							__magic_name__				:		Dict							,							__magic_name__				:		List[str]							,							__magic_name__				:		Optional[Any]							,							__magic_name__				:		Optional[int]							,							__magic_name__				:		Optional[Any]							,							__magic_name__				:		Union[str, Any]    ):







						"""simple docstring"""







						UpperCamelCase								=       DistilBertForQuestionAnswering(config=UpperCamelCase__    )
						model.to(UpperCamelCase__    )
						model.eval()
						UpperCamelCase								=       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 	lowerCamelCase_       (    self				:		str							,							__magic_name__				:		int							,							__magic_name__				:		Union[str, Any]							,							__magic_name__				:		Dict							,							__magic_name__				:		Dict							,							__magic_name__				:		Tuple							,							__magic_name__				:		str    ):







						"""simple docstring"""







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




			def 	lowerCamelCase_       (    self				:		List[str]							,							__magic_name__				:		List[str]							,							__magic_name__				:		Dict							,							__magic_name__				:		Any							,							__magic_name__				:		Any							,							__magic_name__				:		List[str]							,							__magic_name__				:		str    ):







						"""simple docstring"""







						UpperCamelCase								=       self.num_labels
						UpperCamelCase								=       DistilBertForTokenClassification(config=UpperCamelCase__    )
						model.to(UpperCamelCase__    )
						model.eval()

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




			def 	lowerCamelCase_       (    self				:		Any							,							__magic_name__				:		List[str]							,							__magic_name__				:		List[Any]							,							__magic_name__				:		List[str]							,							__magic_name__				:		Optional[Any]							,							__magic_name__				:		Optional[Any]							,							__magic_name__				:		Union[str, Any]    ):







						"""simple docstring"""







						UpperCamelCase								=       self.num_choices
						UpperCamelCase								=       DistilBertForMultipleChoice(config=UpperCamelCase__    )
						model.to(UpperCamelCase__    )
						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(
						    UpperCamelCase__							,							attention_mask=UpperCamelCase__							,							labels=UpperCamelCase__							,							)
						self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_choices)    )





			def 	lowerCamelCase_       (    self				:		Optional[Any]    ):







						"""simple docstring"""







						UpperCamelCase								=       self.prepare_config_and_inputs()
						((UpperCamelCase)	,	(UpperCamelCase)	,	(UpperCamelCase)	,	(UpperCamelCase)	,	(UpperCamelCase)	,	(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     ):
			lowercase						       =			(
			    (
			        DistilBertModel,
			        DistilBertForMaskedLM,
			        DistilBertForMultipleChoice,
			        DistilBertForQuestionAnswering,
			        DistilBertForSequenceClassification,
			        DistilBertForTokenClassification,
			    )
			    if is_torch_available()
			    else None
			)
			lowercase						       =			(
			    {
			        """feature-extraction""": DistilBertModel,
			        """fill-mask""": DistilBertForMaskedLM,
			        """question-answering""": DistilBertForQuestionAnswering,
			        """text-classification""": DistilBertForSequenceClassification,
			        """token-classification""": DistilBertForTokenClassification,
			        """zero-shot""": DistilBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowercase						       =			True
			lowercase						       =			True
			lowercase						       =			True
			lowercase						       =			True




			def 	lowerCamelCase_       (    self				:		Optional[int]    ):







						"""simple docstring"""







						UpperCamelCase								=       DistilBertModelTester(self    )
						UpperCamelCase								=       ConfigTester(self							,							config_class=UpperCamelCase__							,							dim=3_7    )




			def 	lowerCamelCase_       (    self				:		Optional[Any]    ):







						"""simple docstring"""







						self.config_tester.run_common_tests()




			def 	lowerCamelCase_       (    self				:		Dict    ):







						"""simple docstring"""







						UpperCamelCase								=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__    )




			def 	lowerCamelCase_       (    self				:		Union[str, Any]    ):







						"""simple docstring"""







						UpperCamelCase								=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__    )




			def 	lowerCamelCase_       (    self				:		Union[str, Any]    ):







						"""simple docstring"""







						UpperCamelCase								=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__    )




			def 	lowerCamelCase_       (    self				:		Tuple    ):







						"""simple docstring"""







						UpperCamelCase								=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__    )




			def 	lowerCamelCase_       (    self				:		Optional[Any]    ):







						"""simple docstring"""







						UpperCamelCase								=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__    )




			def 	lowerCamelCase_       (    self				:		Optional[Any]    ):







						"""simple docstring"""







						UpperCamelCase								=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__    )




			@slow
			def 	lowerCamelCase_       (    self				:		Union[str, Any]    ):







						"""simple docstring"""







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





			@slow
			@require_torch_gpu
			def 	lowerCamelCase_       (    self				:		List[str]    ):







						"""simple docstring"""







						UpperCamelCase	,	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=UpperCamelCase__    )

									UpperCamelCase								=       self._prepare_for_class(UpperCamelCase__							,							UpperCamelCase__    )
									UpperCamelCase								=       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"""    )    )
												UpperCamelCase								=       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  UpperCAmelCase       (							unittest.TestCase     ):




			@slow
			def 	lowerCamelCase_       (    self				:		Dict    ):







						"""simple docstring"""







						UpperCamelCase								=       DistilBertModel.from_pretrained("""distilbert-base-uncased"""    )
						UpperCamelCase								=       torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]    )
						UpperCamelCase								=       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
						with torch.no_grad():
									UpperCamelCase								=       model(UpperCamelCase__							,							attention_mask=UpperCamelCase__    )[0]
						UpperCamelCase								=       torch.Size((1, 1_1, 7_6_8)    )
						self.assertEqual(output.shape							,							UpperCamelCase__    )
						UpperCamelCase								=       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]							,							UpperCamelCase__							,							atol=1e-4    )    )







'''simple docstring'''







import pytest

import datasets


# Import fixture modules as plugins
__lowerCamelCase									=     ['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec''']
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      Optional[int]:
       # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit")
       for item in items:
              if any(marker in item.keywords for marker in ["""integration""", """unit"""]					):
                     continue
              item.add_marker(pytest.mark.unit					)
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      Tuple:
       config.addinivalue_line("""markers""",							"""torchaudio_latest: mark test to run with torchaudio>=0.12"""					)
@pytest.fixture(autouse=UpperCAmelCase__					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      int:
       # test_hf_cache_home = tmp_path_factory.mktemp("cache")  # TODO: why a cache dir per test function does not work?
       A_						       =   tmp_path_factory.getbasetemp() / """cache"""
       A_						       =   test_hf_cache_home / """datasets"""
       A_						       =   test_hf_cache_home / """metrics"""
       A_						       =   test_hf_cache_home / """modules"""
       monkeypatch.setattr("""datasets.config.HF_DATASETS_CACHE""",							str(UpperCAmelCase__					)					)
       monkeypatch.setattr("""datasets.config.HF_METRICS_CACHE""",							str(UpperCAmelCase__					)					)
       monkeypatch.setattr("""datasets.config.HF_MODULES_CACHE""",							str(UpperCAmelCase__					)					)
       A_						       =   test_hf_datasets_cache / """downloads"""
       monkeypatch.setattr("""datasets.config.DOWNLOADED_DATASETS_PATH""",							str(UpperCAmelCase__					)					)
       A_						       =   test_hf_datasets_cache / """downloads""" / """extracted"""
       monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH""",							str(UpperCAmelCase__					)					)
@pytest.fixture(autouse=UpperCAmelCase__,							scope="""session"""					)
def    UpperCAmelCase__     (   )      ->      str:
       datasets.disable_progress_bar()
@pytest.fixture(autouse=UpperCAmelCase__					)
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      Tuple:
       # don't take tests into account when counting downloads
       monkeypatch.setattr("""datasets.config.HF_UPDATE_DOWNLOAD_COUNTS""",							UpperCAmelCase__					)







@pytest.fixture
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      Any:
       # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0
       # To be removed once SQLAlchemy 2.0 supported
       monkeypatch.setattr("""sqlalchemy.util.deprecations.SILENCE_UBER_WARNING""",							UpperCAmelCase__					)
