



"""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 			A__   (  __SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





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

				"""simple docstring"""

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

	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any])				->       List[str]:

				"""simple docstring"""

				__lowerCAmelCase      :  str												=       ids_tensor([self.batch_size, self.seq_length]       ,			self.vocab_size)

				__lowerCAmelCase      :  int												=       None
				if self.use_input_mask:
							__lowerCAmelCase      :  Optional[Any]												=       random_attention_mask([self.batch_size, self.seq_length])

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

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

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

	def 							_SCREAMING_SNAKE_CASE   (     self:		Dict)				->       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 							_SCREAMING_SNAKE_CASE   (     self:		int       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		Tuple       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		List[str])				->       Dict:

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       DistilBertModel(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase      :  Optional[int]												=       model(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Optional[int]												=       model(_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(result.last_hidden_state.shape       ,			(self.batch_size, self.seq_length, self.hidden_size))

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

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       DistilBertForMaskedLM(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase      :  Optional[Any]												=       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 							_SCREAMING_SNAKE_CASE   (     self:		Tuple       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		str       ,			_SCREAMING_SNAKE_CASE:		Optional[Any]       ,			_SCREAMING_SNAKE_CASE:		int)				->       List[str]:

				"""simple docstring"""

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

				"""simple docstring"""

				__lowerCAmelCase      :  List[str]												=       self.num_labels
				__lowerCAmelCase      :  Optional[Any]												=       DistilBertForSequenceClassification(_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase      :  Optional[Any]												=       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 							_SCREAMING_SNAKE_CASE   (     self:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		str       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		Tuple       ,			_SCREAMING_SNAKE_CASE:		Dict       ,			_SCREAMING_SNAKE_CASE:		int)				->       Dict:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[Any]												=       self.num_labels
				__lowerCAmelCase      :  Any												=       DistilBertForTokenClassification(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()

				__lowerCAmelCase      :  str												=       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 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any]       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		Any       ,			_SCREAMING_SNAKE_CASE:		Optional[Any]       ,			_SCREAMING_SNAKE_CASE:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		str       ,			_SCREAMING_SNAKE_CASE:		Optional[Any])				->       int:

				"""simple docstring"""

				__lowerCAmelCase      :  int												=       self.num_choices
				__lowerCAmelCase      :  List[str]												=       DistilBertForMultipleChoice(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase      :  Union[str, Any]												=       input_ids.unsqueeze(1).expand(-1       ,			self.num_choices       ,			-1).contiguous()
				__lowerCAmelCase      :  Optional[Any]												=       input_mask.unsqueeze(1).expand(-1       ,			self.num_choices       ,			-1).contiguous()
				__lowerCAmelCase      :  int												=       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 							_SCREAMING_SNAKE_CASE   (     self:		List[Any])				->       Union[str, Any]:

				"""simple docstring"""

				__lowerCAmelCase      :  Dict												=       self.prepare_config_and_inputs()
				((__lowerCAmelCase)						,	(__lowerCAmelCase)						,	(__lowerCAmelCase)						,	(__lowerCAmelCase)						,	(__lowerCAmelCase)						,	(__lowerCAmelCase))      :  int												=       config_and_inputs
				__lowerCAmelCase      :  Any												=       {"input_ids": input_ids, "attention_mask": input_mask}
				return config, inputs_dict






@require_torch
class 			A__   (  __SCREAMING_SNAKE_CASE  ,		__SCREAMING_SNAKE_CASE  ,		unittest.TestCase							):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE          =	(
	    (
	        DistilBertModel,
	        DistilBertForMaskedLM,
	        DistilBertForMultipleChoice,
	        DistilBertForQuestionAnswering,
	        DistilBertForSequenceClassification,
	        DistilBertForTokenClassification,
	    )
	    if is_torch_available()
	    else None
	)
	SCREAMING_SNAKE_CASE          =	(
	    {
	        'feature-extraction': DistilBertModel,
	        'fill-mask': DistilBertForMaskedLM,
	        'question-answering': DistilBertForQuestionAnswering,
	        'text-classification': DistilBertForSequenceClassification,
	        'token-classification': DistilBertForTokenClassification,
	        'zero-shot': DistilBertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	SCREAMING_SNAKE_CASE          =	True
	SCREAMING_SNAKE_CASE          =	True
	SCREAMING_SNAKE_CASE          =	True
	SCREAMING_SNAKE_CASE          =	True

	def 							_SCREAMING_SNAKE_CASE   (     self:		Tuple)				->       Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase      :  List[str]												=       DistilBertModelTester(self)
				__lowerCAmelCase      :  str												=       ConfigTester(self       ,			config_class=_SCREAMING_SNAKE_CASE       ,			dim=37)

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any])				->       int:

				"""simple docstring"""

				self.config_tester.run_common_tests()

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

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_model(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		str)				->       Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase      :  Union[str, Any]												=       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_masked_lm(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       int:

				"""simple docstring"""

				__lowerCAmelCase      :  List[str]												=       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_question_answering(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		Tuple)				->       Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_sequence_classification(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		Dict)				->       int:

				"""simple docstring"""

				__lowerCAmelCase      :  List[Any]												=       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_token_classification(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		str)				->       Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_multiple_choice(*_SCREAMING_SNAKE_CASE)

	@slow
	def 							_SCREAMING_SNAKE_CASE   (     self:		Tuple)				->       Tuple:

				"""simple docstring"""

				for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowerCAmelCase      :  Dict												=       DistilBertModel.from_pretrained(_SCREAMING_SNAKE_CASE)
							self.assertIsNotNone(_SCREAMING_SNAKE_CASE)







	@slow
	@require_torch_gpu
	def 							_SCREAMING_SNAKE_CASE   (     self:		Tuple)				->       Tuple:

				"""simple docstring"""

				__lowerCAmelCase						,	__lowerCAmelCase      :  List[str]												=       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      :  Dict												=       True
							__lowerCAmelCase      :  int												=       model_class(config=_SCREAMING_SNAKE_CASE)

							__lowerCAmelCase      :  int												=       self._prepare_for_class(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase      :  int												=       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      :  Any												=       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 			A__   (  unittest.TestCase							):




	'''simple docstring'''





	@slow
	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any])				->       int:

				"""simple docstring"""

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




"""simple docstring"""






import re
from pathlib import Path
from unittest import TestCase

import pytest



@pytest.mark.integration
class 			A__   (  __SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		str)				->       Dict:

				"""simple docstring"""

				with open(_SCREAMING_SNAKE_CASE       ,			encoding="utf-8") as input_file:
							__lowerCAmelCase      :  List[str]												=       re.compile(r"(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)")
							__lowerCAmelCase      :  List[Any]												=       input_file.read()
							__lowerCAmelCase      :  Any												=       regexp.search(_SCREAMING_SNAKE_CASE)

				return match

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		str)				->       Optional[Any]:

				"""simple docstring"""

				with open(_SCREAMING_SNAKE_CASE       ,			encoding="utf-8") as input_file:
							__lowerCAmelCase      :  Any												=       re.compile(r"#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()"       ,			re.DOTALL)
							__lowerCAmelCase      :  Optional[int]												=       input_file.read()
							# use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
							__lowerCAmelCase      :  int												=       regexp.finditer(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  Union[str, Any]												=       [match for match in matches if match is not None and match.group(1) is not None]
				return matches[0] if matches else None

	def 							_SCREAMING_SNAKE_CASE   (     self:		str)				->       List[Any]:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[Any]												=       Path("./datasets")
				__lowerCAmelCase      :  Optional[int]												=       list(dataset_paths.absolute().glob("**/*.py"))

				for dataset in dataset_files:
							if self._no_encoding_on_file_open(str(_SCREAMING_SNAKE_CASE)):
										raise AssertionError(F"""open(...) must use utf-8 encoding in {dataset}""")







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

				"""simple docstring"""

				__lowerCAmelCase      :  Dict												=       Path("./datasets")
				__lowerCAmelCase      :  Union[str, Any]												=       list(dataset_paths.absolute().glob("**/*.py"))

				for dataset in dataset_files:
							if self._no_print_statements(str(_SCREAMING_SNAKE_CASE)):
										raise AssertionError(F"""print statement found in {dataset}. Use datasets.logger/logging instead.""")