

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 				lowercase       ( lowercase_    ):




				def __init__(      self   ,     snake_case   ,     snake_case=13   ,     snake_case=7   ,     snake_case=True   ,     snake_case=True   ,     snake_case=False   ,     snake_case=True   ,     snake_case=99   ,     snake_case=32   ,     snake_case=5   ,     snake_case=4   ,     snake_case=37   ,     snake_case="gelu"   ,     snake_case=0.1   ,     snake_case=0.1   ,     snake_case=512   ,     snake_case=16   ,     snake_case=2   ,     snake_case=0.02   ,     snake_case=3   ,     snake_case=4   ,     snake_case=None   ,     ):
											snake_case_           =					parent
											snake_case_           =					batch_size
											snake_case_           =					seq_length
											snake_case_           =					is_training
											snake_case_           =					use_input_mask
											snake_case_           =					use_token_type_ids
											snake_case_           =					use_labels
											snake_case_           =					vocab_size
											snake_case_           =					hidden_size
											snake_case_           =					num_hidden_layers
											snake_case_           =					num_attention_heads
											snake_case_           =					intermediate_size
											snake_case_           =					hidden_act
											snake_case_           =					hidden_dropout_prob
											snake_case_           =					attention_probs_dropout_prob
											snake_case_           =					max_position_embeddings
											snake_case_           =					type_vocab_size
											snake_case_           =					type_sequence_label_size
											snake_case_           =					initializer_range
											snake_case_           =					num_labels
											snake_case_           =					num_choices
											snake_case_           =					scope




				def 					a							(      self ):
											snake_case_           =					ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size )

											snake_case_           =					None
											if self.use_input_mask:
																		snake_case_           =					random_attention_mask([self.batch_size, self.seq_length] )

											snake_case_           =					None
											snake_case_           =					None
											snake_case_           =					None
											if self.use_labels:
																		snake_case_           =					ids_tensor([self.batch_size]   ,     self.type_sequence_label_size )
																		snake_case_           =					ids_tensor([self.batch_size, self.seq_length]   ,     self.num_labels )
																		snake_case_           =					ids_tensor([self.batch_size]   ,     self.num_choices )

											snake_case_           =					self.get_config()

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




				def 					a							(      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 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					DistilBertModel(config=snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					model(snake_case   ,     snake_case )
											snake_case_           =					model(snake_case )
											self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size) )




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					DistilBertForMaskedLM(config=snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					model(snake_case   ,     attention_mask=snake_case   ,     labels=snake_case )
											self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.vocab_size) )




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					DistilBertForQuestionAnswering(config=snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					model(
											    snake_case   ,     attention_mask=snake_case   ,     start_positions=snake_case   ,     end_positions=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 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					self.num_labels
											snake_case_           =					DistilBertForSequenceClassification(snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					model(snake_case   ,     attention_mask=snake_case   ,     labels=snake_case )
											self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels) )




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					self.num_labels
											snake_case_           =					DistilBertForTokenClassification(config=snake_case )
											model.to(snake_case )
											model.eval()

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




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					self.num_choices
											snake_case_           =					DistilBertForMultipleChoice(config=snake_case )
											model.to(snake_case )
											model.eval()
											snake_case_           =					input_ids.unsqueeze(1 ).expand(-1   ,     self.num_choices   ,     -1 ).contiguous()
											snake_case_           =					input_mask.unsqueeze(1 ).expand(-1   ,     self.num_choices   ,     -1 ).contiguous()
											snake_case_           =					model(
											    snake_case   ,     attention_mask=snake_case   ,     labels=snake_case   ,     )
											self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_choices) )




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







@require_torch
class 				lowercase       ( lowercase_			,						lowercase_			,						unittest.TestCase    ):
				__SCREAMING_SNAKE_CASE      :				str     =    (
				    (
				        DistilBertModel,
				        DistilBertForMaskedLM,
				        DistilBertForMultipleChoice,
				        DistilBertForQuestionAnswering,
				        DistilBertForSequenceClassification,
				        DistilBertForTokenClassification,
				    )
				    if is_torch_available()
				    else None
				)
				__SCREAMING_SNAKE_CASE      :				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 {}
				)
				__SCREAMING_SNAKE_CASE      :				List[str]     =    True
				__SCREAMING_SNAKE_CASE      :				Dict     =    True
				__SCREAMING_SNAKE_CASE      :				Optional[Any]     =    True
				__SCREAMING_SNAKE_CASE      :				Any     =    True




				def 					a							(      self ):
											snake_case_           =					DistilBertModelTester(self )
											snake_case_           =					ConfigTester(self   ,     config_class=snake_case   ,     dim=37 )




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




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_model(*snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_masked_lm(*snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_question_answering(*snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_sequence_classification(*snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_token_classification(*snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_multiple_choice(*snake_case )




				@slow
				def 					a							(      self ):
											for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		snake_case_           =					DistilBertModel.from_pretrained(snake_case )
																		self.assertIsNotNone(snake_case )




				@slow
				@require_torch_gpu
				def 					a							(      self ):
											snake_case_       ,					snake_case_           =					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

																		snake_case_           =					True
																		snake_case_           =					model_class(config=snake_case )

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

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







@require_torch
class 				lowercase       ( unittest.TestCase    ):




				@slow
				def 					a							(      self ):
											snake_case_           =					DistilBertModel.from_pretrained('distilbert-base-uncased' )
											snake_case_           =					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] )
											snake_case_           =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
											with torch.no_grad():
																		snake_case_           =					model(snake_case   ,     attention_mask=snake_case )[0]
											snake_case_           =					torch.Size((1, 11, 768) )
											self.assertEqual(output.shape   ,     snake_case )
											snake_case_           =					torch.tensor(
											    [[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]] )

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







# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works
from ...utils import deprecate
from ..controlnet.multicontrolnet import MultiControlNetModel  # noqa: F401
from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline  # noqa: F401


deprecate(
    """stable diffusion controlnet""",
    """0.22.0""",
    """Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.""",
    standard_warn=False,
    stacklevel=3,
)




