

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_      (	_lowercase	):




	def __init__(     self						:     List[Any]					,      __A						:     Any					,      __A						:     str=13					,      __A						:     str=7					,      __A						:     Union[str, Any]=True					,      __A						:     int=True					,      __A						:     Optional[int]=False					,      __A						:     Tuple=True					,      __A						:     Union[str, Any]=99					,      __A						:     List[str]=32					,      __A						:     Tuple=5					,      __A						:     Tuple=4					,      __A						:     Union[str, Any]=37					,      __A						:     int="gelu"					,      __A						:     int=0.1					,      __A						:     Dict=0.1					,      __A						:     int=512					,      __A						:     List[str]=16					,      __A						:     Union[str, Any]=2					,      __A						:     Dict=0.0_2					,      __A						:     Dict=3					,      __A						:     str=4					,      __A						:     str=None					,      ):
		__A :			List[Any]		    =			parent
		__A :			Optional[Any]		    =			batch_size
		__A :			Tuple		    =			seq_length
		__A :			int		    =			is_training
		__A :			str		    =			use_input_mask
		__A :			List[str]		    =			use_token_type_ids
		__A :			Dict		    =			use_labels
		__A :			Optional[int]		    =			vocab_size
		__A :			Dict		    =			hidden_size
		__A :			List[Any]		    =			num_hidden_layers
		__A :			Dict		    =			num_attention_heads
		__A :			Union[str, Any]		    =			intermediate_size
		__A :			Any		    =			hidden_act
		__A :			List[Any]		    =			hidden_dropout_prob
		__A :			Optional[int]		    =			attention_probs_dropout_prob
		__A :			Tuple		    =			max_position_embeddings
		__A :			List[str]		    =			type_vocab_size
		__A :			Dict		    =			type_sequence_label_size
		__A :			List[Any]		    =			initializer_range
		__A :			int		    =			num_labels
		__A :			Dict		    =			num_choices
		__A :			Union[str, Any]		    =			scope




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		__A :			Optional[Any]		    =			ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size							)

		__A :			List[Any]		    =			None
		if self.use_input_mask:
			__A :			Union[str, Any]		    =			random_attention_mask([self.batch_size, self.seq_length]							)

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

		__A :			Any		    =			self.get_config()

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




	def        lowerCAmelCase_       (     self						:     List[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        lowerCAmelCase_       (     self						:     Optional[Any]					,      __A						:     str					,      __A						:     Optional[Any]					,      __A						:     str					,      __A						:     List[Any]					,      __A						:     Any					,      __A						:     Tuple							):
		__A :			Any		    =			DistilBertModel(config=__A							)
		model.to(__A							)
		model.eval()
		__A :			Optional[Any]		    =			model(__A					,      __A							)
		__A :			List[str]		    =			model(__A							)
		self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)							)




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




	def        lowerCAmelCase_       (     self						:     Optional[Any]					,      __A						:     Dict					,      __A						:     Any					,      __A						:     Any					,      __A						:     Optional[int]					,      __A						:     Optional[int]					,      __A						:     List[Any]							):
		__A :			List[str]		    =			DistilBertForQuestionAnswering(config=__A							)
		model.to(__A							)
		model.eval()
		__A :			int		    =			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        lowerCAmelCase_       (     self						:     int					,      __A						:     Optional[Any]					,      __A						:     List[Any]					,      __A						:     str					,      __A						:     Optional[Any]					,      __A						:     Optional[int]					,      __A						:     List[Any]							):
		__A :			Dict		    =			self.num_labels
		__A :			Optional[Any]		    =			DistilBertForSequenceClassification(__A							)
		model.to(__A							)
		model.eval()
		__A :			Optional[Any]		    =			model(__A					,      attention_mask=__A					,      labels=__A							)
		self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)							)




	def        lowerCAmelCase_       (     self						:     List[str]					,      __A						:     str					,      __A						:     Any					,      __A						:     Tuple					,      __A						:     Tuple					,      __A						:     Tuple					,      __A						:     Any							):
		__A :			Any		    =			self.num_labels
		__A :			Any		    =			DistilBertForTokenClassification(config=__A							)
		model.to(__A							)
		model.eval()

		__A :			Dict		    =			model(__A					,      attention_mask=__A					,      labels=__A							)
		self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)							)




	def        lowerCAmelCase_       (     self						:     List[str]					,      __A						:     Optional[Any]					,      __A						:     Dict					,      __A						:     List[Any]					,      __A						:     Optional[int]					,      __A						:     Tuple					,      __A						:     Any							):
		__A :			List[Any]		    =			self.num_choices
		__A :			Any		    =			DistilBertForMultipleChoice(config=__A							)
		model.to(__A							)
		model.eval()
		__A :			int		    =			input_ids.unsqueeze(1							).expand(-1					,      self.num_choices					,      -1							).contiguous()
		__A :			Tuple		    =			input_mask.unsqueeze(1							).expand(-1					,      self.num_choices					,      -1							).contiguous()
		__A :			Optional[Any]		    =			model(
		    __A					,      attention_mask=__A					,      labels=__A					,      )
		self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_choices)							)




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		__A :			Union[str, Any]		    =			self.prepare_config_and_inputs()
		((__A)    ,				(__A)    ,				(__A)    ,				(__A)    ,				(__A)    ,				(__A)) :			Any		    =			config_and_inputs
		__A :			List[str]		    =			{"""input_ids""": input_ids, """attention_mask""": input_mask}
		return config, inputs_dict






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




	def        lowerCAmelCase_       (     self						:     Optional[Any]							):
		__A :			Optional[int]		    =			DistilBertModelTester(self							)
		__A :			Dict		    =			ConfigTester(self					,      config_class=__A					,      dim=37							)




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		self.config_tester.run_common_tests()




	def        lowerCAmelCase_       (     self						:     Any							):
		__A :			Optional[int]		    =			self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_model(*__A							)




	def        lowerCAmelCase_       (     self						:     Dict							):
		__A :			Any		    =			self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_masked_lm(*__A							)




	def        lowerCAmelCase_       (     self						:     Union[str, Any]							):
		__A :			Optional[int]		    =			self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_question_answering(*__A							)




	def        lowerCAmelCase_       (     self						:     Optional[int]							):
		__A :			List[str]		    =			self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_sequence_classification(*__A							)




	def        lowerCAmelCase_       (     self						:     str							):
		__A :			List[str]		    =			self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_token_classification(*__A							)




	def        lowerCAmelCase_       (     self						:     str							):
		__A :			Any		    =			self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_distilbert_for_multiple_choice(*__A							)




	@slow
	def        lowerCAmelCase_       (     self						:     int							):
		for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			__A :			Union[str, Any]		    =			DistilBertModel.from_pretrained(__A							)
			self.assertIsNotNone(__A							)




	@slow
	@require_torch_gpu
	def        lowerCAmelCase_       (     self						:     int							):
		__A    ,				__A :			Optional[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

			__A :			List[str]		    =			True
			__A :			List[str]		    =			model_class(config=__A							)

			__A :			Any		    =			self._prepare_for_class(__A					,      __A							)
			__A :			Tuple		    =			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"""							)							)
				__A :			List[str]		    =			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      lowerCamelCase_      (	unittest.TestCase	):




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








import warnings

from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline  # noqa F401


warnings.warn(
    '''The `inpainting.py` script is outdated. Please use directly `from diffusers import'''
    ''' StableDiffusionInpaintPipeline` instead.'''
)





