
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					):






			'''simple docstring'''

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

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							int		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size )

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

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

							UpperCamelCase_:							Any		=  self.get_config()

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

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

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

			def  lowerCAmelCase__		(    self	:    Any  ,		snake_case_	:    str  ,		snake_case_	:    Any  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    str  ,		snake_case_	:    Tuple  ,		snake_case_	:    Union[str, Any] ):
							UpperCamelCase_:							List[Any]		=  self.num_labels
							UpperCamelCase_:							Tuple		=  DistilBertForTokenClassification(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()

							UpperCamelCase_:							Tuple		=  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  lowerCAmelCase__		(    self	:    List[str]  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    Any  ,		snake_case_	:    Optional[int]  ,		snake_case_	:    str  ,		snake_case_	:    Tuple  ,		snake_case_	:    Any ):
							UpperCamelCase_:							Union[str, Any]		=  self.num_choices
							UpperCamelCase_:							str		=  DistilBertForMultipleChoice(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							Optional[Any]		=  input_ids.unsqueeze(1 ).expand(-1  ,		self.num_choices  ,		-1 ).contiguous()
							UpperCamelCase_:							Optional[Any]		=  input_mask.unsqueeze(1 ).expand(-1  ,		self.num_choices  ,		-1 ).contiguous()
							UpperCamelCase_:							Union[str, Any]		=  model(
							    snake_case_  ,		attention_mask=snake_case_  ,		labels=snake_case_  ,		)
							self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.num_choices) )




			def  lowerCAmelCase__		(    self	:    Any ):
							UpperCamelCase_:							Optional[Any]		=  self.prepare_config_and_inputs()
							((UpperCamelCase_),				(UpperCamelCase_),				(UpperCamelCase_),				(UpperCamelCase_),				(UpperCamelCase_),				(UpperCamelCase_)):							Any		=  config_and_inputs
							UpperCamelCase_:							Any		=  {"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict




@require_torch
class     _UpperCamelCase			(   __snake_case       ,    __snake_case       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  List[Any]    =	(
			    (
			        DistilBertModel,
			        DistilBertForMaskedLM,
			        DistilBertForMultipleChoice,
			        DistilBertForQuestionAnswering,
			        DistilBertForSequenceClassification,
			        DistilBertForTokenClassification,
			    )
			    if is_torch_available()
			    else None
			)
			__UpperCamelCase :  Any    =	(
			    {
			        'feature-extraction': DistilBertModel,
			        'fill-mask': DistilBertForMaskedLM,
			        'question-answering': DistilBertForQuestionAnswering,
			        'text-classification': DistilBertForSequenceClassification,
			        'token-classification': DistilBertForTokenClassification,
			        'zero-shot': DistilBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			__UpperCamelCase :  int    =	True
			__UpperCamelCase :  str    =	True
			__UpperCamelCase :  int    =	True
			__UpperCamelCase :  List[str]    =	True

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							List[str]		=  DistilBertModelTester(self )
							UpperCamelCase_:							str		=  ConfigTester(self  ,		config_class=snake_case_  ,		dim=37 )

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

			def  lowerCAmelCase__		(    self	:    str ):
							UpperCamelCase_:							str		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_model(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							Tuple		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_masked_lm(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							Optional[int]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_question_answering(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							List[str]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_sequence_classification(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							UpperCamelCase_:							Any		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_token_classification(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    str ):
							UpperCamelCase_:							Optional[int]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_multiple_choice(*snake_case_ )

			@slow
			def  lowerCAmelCase__		(    self	:    Any ):
							for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCamelCase_:							Optional[Any]		=  DistilBertModel.from_pretrained(snake_case_ )
											self.assertIsNotNone(snake_case_ )




			@slow
			@require_torch_gpu
			def  lowerCAmelCase__		(    self	:    int ):
							UpperCamelCase_,				UpperCamelCase_:							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

											UpperCamelCase_:							int		=  True
											UpperCamelCase_:							List[Any]		=  model_class(config=snake_case_ )

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






			'''simple docstring'''

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








import unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow

@is_pipeline_test
@require_torch
class     _UpperCamelCase			(   unittest.TestCase					):






			'''simple docstring'''

			@require_torch
			def  lowerCAmelCase__		(    self	:    Dict ):
							UpperCamelCase_:							Any		=  pipeline(
							    task="""zero-shot-audio-classification"""  ,		model="""hf-internal-testing/tiny-clap-htsat-unfused""" )
							UpperCamelCase_:							str		=  load_dataset("""ashraq/esc50""" )
							UpperCamelCase_:							Optional[int]		=  dataset["""train"""]["""audio"""][-1]["""array"""]
							UpperCamelCase_:							Optional[int]		=  audio_classifier(snake_case_  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] )
							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[{"""score""": 0.501, """label""": """Sound of a dog"""}, {"""score""": 0.499, """label""": """Sound of vaccum cleaner"""}]  ,		)

			@unittest.skip("""No models are available in TF""" )
			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							pass

			@slow
			@require_torch
			def  lowerCAmelCase__		(    self	:    int ):
							UpperCamelCase_:							List[str]		=  pipeline(
							    task="""zero-shot-audio-classification"""  ,		model="""laion/clap-htsat-unfused"""  ,		)
							# This is an audio of a dog
							UpperCamelCase_:							Tuple		=  load_dataset("""ashraq/esc50""" )
							UpperCamelCase_:							Optional[int]		=  dataset["""train"""]["""audio"""][-1]["""array"""]
							UpperCamelCase_:							int		=  audio_classifier(snake_case_  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] )

							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[
							        {"""score""": 0.999, """label""": """Sound of a dog"""},
							        {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
							    ]  ,		)

							UpperCamelCase_:							Any		=  audio_classifier([audio] * 5  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""] )
							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[
							        [
							            {"""score""": 0.999, """label""": """Sound of a dog"""},
							            {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
							        ],
							    ]
							    * 5  ,		)
							UpperCamelCase_:							Any		=  audio_classifier(
							    [audio] * 5  ,		candidate_labels=["""Sound of a dog""", """Sound of vaccum cleaner"""]  ,		batch_size=5 )
							self.assertEqual(
							    nested_simplify(snake_case_ )  ,		[
							        [
							            {"""score""": 0.999, """label""": """Sound of a dog"""},
							            {"""score""": 0.001, """label""": """Sound of vaccum cleaner"""},
							        ],
							    ]
							    * 5  ,		)




			@unittest.skip("""No models are available in TF""" )
			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							pass






