


"""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 			lowerCamelCase__						(   UpperCAmelCase_			):







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







						def __a						(							self   :    Dict		):
								A					       = ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size		)

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

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

								A					       = self.get_config()

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







						def __a						(							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 __a						(							self   :    Dict			,			_lowercase   :    Optional[int]			,			_lowercase   :    Dict			,			_lowercase   :    Optional[int]			,			_lowercase   :    Union[str, Any]			,			_lowercase   :    Union[str, Any]			,			_lowercase   :    Optional[int]		):
								A					       = DistilBertModel(config=_lowercase		)
								model.to(_lowercase		)
								model.eval()
								A					       = model(_lowercase			,			_lowercase		)
								A					       = model(_lowercase		)
								self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size)		)







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







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







						def __a						(							self   :    Optional[int]			,			_lowercase   :    List[Any]			,			_lowercase   :    Dict			,			_lowercase   :    Optional[Any]			,			_lowercase   :    int			,			_lowercase   :    Dict			,			_lowercase   :    List[str]		):
								A					       = self.num_labels
								A					       = DistilBertForTokenClassification(config=_lowercase		)
								model.to(_lowercase		)
								model.eval()

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







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







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



@require_torch
class 			lowerCamelCase__						(   UpperCAmelCase_							,    UpperCAmelCase_							,    unittest.TestCase			):
						lowerCAmelCase       =     (
						    (
						        DistilBertModel,
						        DistilBertForMaskedLM,
						        DistilBertForMultipleChoice,
						        DistilBertForQuestionAnswering,
						        DistilBertForSequenceClassification,
						        DistilBertForTokenClassification,
						    )
						    if is_torch_available()
						    else None
						)
						lowerCAmelCase       =     (
						    {
						        """feature-extraction""": DistilBertModel,
						        """fill-mask""": DistilBertForMaskedLM,
						        """question-answering""": DistilBertForQuestionAnswering,
						        """text-classification""": DistilBertForSequenceClassification,
						        """token-classification""": DistilBertForTokenClassification,
						        """zero-shot""": DistilBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						lowerCAmelCase       =     True
						lowerCAmelCase       =     True
						lowerCAmelCase       =     True
						lowerCAmelCase       =     True







						def __a						(							self   :    Dict		):
								A					       = DistilBertModelTester(self		)
								A					       = ConfigTester(self			,			config_class=_lowercase			,			dim=37		)







						def __a						(							self   :    int		):
								self.config_tester.run_common_tests()







						def __a						(							self   :    List[Any]		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_model(*_lowercase		)







						def __a						(							self   :    str		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowercase		)







						def __a						(							self   :    Any		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_question_answering(*_lowercase		)







						def __a						(							self   :    int		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowercase		)







						def __a						(							self   :    str		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_token_classification(*_lowercase		)







						def __a						(							self   :    str		):
								A					       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowercase		)







						@slow
						def __a						(							self   :    Tuple		):
								for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										A					       = DistilBertModel.from_pretrained(_lowercase		)
										self.assertIsNotNone(_lowercase		)







						@slow
						@require_torch_gpu
						def __a						(							self   :    Optional[int]		):
								A       , A					       = 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					       = True
										A					       = model_class(config=_lowercase		)

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

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



@require_torch
class 			lowerCamelCase__						(   unittest.TestCase			):







						@slow
						def __a						(							self   :    int		):
								A					       = DistilBertModel.from_pretrained('distilbert-base-uncased'		)
								A					       = torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
								A					       = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
								with torch.no_grad():
										A					       = model(_lowercase			,			attention_mask=_lowercase		)[0]
								A					       = torch.Size((1, 11, 768)		)
								self.assertEqual(output.shape			,			_lowercase		)
								A					       = 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]			,			_lowercase			,			atol=1e-4		)		)









"""simple docstring"""


import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible



class 			lowerCamelCase__						(   unittest.TestCase			):







						def __a						(							self   :    Dict		):
								A					       = [
								    'safety_checker/pytorch_model.bin',
								    'safety_checker/model.safetensors',
								    'vae/diffusion_pytorch_model.bin',
								    'vae/diffusion_pytorch_model.safetensors',
								    'text_encoder/pytorch_model.bin',
								    'text_encoder/model.safetensors',
								    'unet/diffusion_pytorch_model.bin',
								    'unet/diffusion_pytorch_model.safetensors',
								]
								self.assertTrue(is_safetensors_compatible(_lowercase		)		)







						def __a						(							self   :    Union[str, Any]		):
								A					       = [
								    'unet/diffusion_pytorch_model.bin',
								    'unet/diffusion_pytorch_model.safetensors',
								]
								self.assertTrue(is_safetensors_compatible(_lowercase		)		)







						def __a						(							self   :    int		):
								A					       = [
								    'safety_checker/pytorch_model.bin',
								    'safety_checker/model.safetensors',
								    'vae/diffusion_pytorch_model.bin',
								    'vae/diffusion_pytorch_model.safetensors',
								    'text_encoder/pytorch_model.bin',
								    'text_encoder/model.safetensors',
								    'unet/diffusion_pytorch_model.bin',
								    # Removed: 'unet/diffusion_pytorch_model.safetensors',
								]
								self.assertFalse(is_safetensors_compatible(_lowercase		)		)







						def __a						(							self   :    Dict		):
								A					       = [
								    'text_encoder/pytorch_model.bin',
								    'text_encoder/model.safetensors',
								]
								self.assertTrue(is_safetensors_compatible(_lowercase		)		)







						def __a						(							self   :    Union[str, Any]		):
								A					       = [
								    'safety_checker/pytorch_model.bin',
								    'safety_checker/model.safetensors',
								    'vae/diffusion_pytorch_model.bin',
								    'vae/diffusion_pytorch_model.safetensors',
								    'text_encoder/pytorch_model.bin',
								    # Removed: 'text_encoder/model.safetensors',
								    'unet/diffusion_pytorch_model.bin',
								    'unet/diffusion_pytorch_model.safetensors',
								]
								self.assertFalse(is_safetensors_compatible(_lowercase		)		)







						def __a						(							self   :    Any		):
								A					       = [
								    'safety_checker/pytorch_model.fp16.bin',
								    'safety_checker/model.fp16.safetensors',
								    'vae/diffusion_pytorch_model.fp16.bin',
								    'vae/diffusion_pytorch_model.fp16.safetensors',
								    'text_encoder/pytorch_model.fp16.bin',
								    'text_encoder/model.fp16.safetensors',
								    'unet/diffusion_pytorch_model.fp16.bin',
								    'unet/diffusion_pytorch_model.fp16.safetensors',
								]
								A					       = 'fp16'
								self.assertTrue(is_safetensors_compatible(_lowercase			,			variant=_lowercase		)		)







						def __a						(							self   :    Union[str, Any]		):
								A					       = [
								    'unet/diffusion_pytorch_model.fp16.bin',
								    'unet/diffusion_pytorch_model.fp16.safetensors',
								]
								A					       = 'fp16'
								self.assertTrue(is_safetensors_compatible(_lowercase			,			variant=_lowercase		)		)







						def __a						(							self   :    Optional[Any]		):
								# pass variant but use the non-variant filenames
								A					       = [
								    'unet/diffusion_pytorch_model.bin',
								    'unet/diffusion_pytorch_model.safetensors',
								]
								A					       = 'fp16'
								self.assertTrue(is_safetensors_compatible(_lowercase			,			variant=_lowercase		)		)







						def __a						(							self   :    Dict		):
								A					       = [
								    'safety_checker/pytorch_model.fp16.bin',
								    'safety_checker/model.fp16.safetensors',
								    'vae/diffusion_pytorch_model.fp16.bin',
								    'vae/diffusion_pytorch_model.fp16.safetensors',
								    'text_encoder/pytorch_model.fp16.bin',
								    'text_encoder/model.fp16.safetensors',
								    'unet/diffusion_pytorch_model.fp16.bin',
								    # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
								]
								A					       = 'fp16'
								self.assertFalse(is_safetensors_compatible(_lowercase			,			variant=_lowercase		)		)







						def __a						(							self   :    Dict		):
								A					       = [
								    'text_encoder/pytorch_model.fp16.bin',
								    'text_encoder/model.fp16.safetensors',
								]
								A					       = 'fp16'
								self.assertTrue(is_safetensors_compatible(_lowercase			,			variant=_lowercase		)		)







						def __a						(							self   :    Any		):
								# pass variant but use the non-variant filenames
								A					       = [
								    'text_encoder/pytorch_model.bin',
								    'text_encoder/model.safetensors',
								]
								A					       = 'fp16'
								self.assertTrue(is_safetensors_compatible(_lowercase			,			variant=_lowercase		)		)







						def __a						(							self   :    List[str]		):
								A					       = [
								    'safety_checker/pytorch_model.fp16.bin',
								    'safety_checker/model.fp16.safetensors',
								    'vae/diffusion_pytorch_model.fp16.bin',
								    'vae/diffusion_pytorch_model.fp16.safetensors',
								    'text_encoder/pytorch_model.fp16.bin',
								    # 'text_encoder/model.fp16.safetensors',
								    'unet/diffusion_pytorch_model.fp16.bin',
								    'unet/diffusion_pytorch_model.fp16.safetensors',
								]
								A					       = 'fp16'
								self.assertFalse(is_safetensors_compatible(_lowercase			,			variant=_lowercase		)		)





