



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





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

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





		def 							__A				(   self )				->   Tuple:

					'''simple docstring'''
					__UpperCAmelCase      :					str   =   ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size )

					__UpperCAmelCase      :					str   =   None
					if self.use_input_mask:
								__UpperCAmelCase      :					Dict   =   random_attention_mask([self.batch_size, self.seq_length] )

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

					__UpperCAmelCase      :					Optional[int]   =   self.get_config()

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





		def 							__A				(   self )				->   str:

					'''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 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase )				->   Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase      :					Tuple   =   DistilBertModel(config=UpperCAmelCase__ )
					model.to(UpperCAmelCase__ )
					model.eval()
					__UpperCAmelCase      :					List[str]   =   model(UpperCAmelCase__       ,				UpperCAmelCase__ )
					__UpperCAmelCase      :					List[str]   =   model(UpperCAmelCase__ )
					self.parent.assertEqual(result.last_hidden_state.shape       ,				(self.batch_size, self.seq_length, self.hidden_size) )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase )				->   Any:

					'''simple docstring'''
					__UpperCAmelCase      :					Dict   =   DistilBertForMaskedLM(config=UpperCAmelCase__ )
					model.to(UpperCAmelCase__ )
					model.eval()
					__UpperCAmelCase      :					int   =   model(UpperCAmelCase__       ,				attention_mask=UpperCAmelCase__       ,				labels=UpperCAmelCase__ )
					self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.vocab_size) )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase )				->   List[Any]:

					'''simple docstring'''
					__UpperCAmelCase      :					Tuple   =   DistilBertForQuestionAnswering(config=UpperCAmelCase__ )
					model.to(UpperCAmelCase__ )
					model.eval()
					__UpperCAmelCase      :					Dict   =   model(
					    UpperCAmelCase__       ,				attention_mask=UpperCAmelCase__       ,				start_positions=UpperCAmelCase__       ,				end_positions=UpperCAmelCase__ )
					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       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase )				->   str:

					'''simple docstring'''
					__UpperCAmelCase      :					Union[str, Any]   =   self.num_labels
					__UpperCAmelCase      :					Tuple   =   DistilBertForSequenceClassification(UpperCAmelCase__ )
					model.to(UpperCAmelCase__ )
					model.eval()
					__UpperCAmelCase      :					Union[str, Any]   =   model(UpperCAmelCase__       ,				attention_mask=UpperCAmelCase__       ,				labels=UpperCAmelCase__ )
					self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_labels) )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase )				->   Dict:

					'''simple docstring'''
					__UpperCAmelCase      :					int   =   self.num_labels
					__UpperCAmelCase      :					str   =   DistilBertForTokenClassification(config=UpperCAmelCase__ )
					model.to(UpperCAmelCase__ )
					model.eval()

					__UpperCAmelCase      :					Any   =   model(UpperCAmelCase__       ,				attention_mask=UpperCAmelCase__       ,				labels=UpperCAmelCase__ )
					self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.num_labels) )





		def 							__A				(   self       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase       ,				__UpperCAmelCase )				->   Optional[int]:

					'''simple docstring'''
					__UpperCAmelCase      :					Union[str, Any]   =   self.num_choices
					__UpperCAmelCase      :					Dict   =   DistilBertForMultipleChoice(config=UpperCAmelCase__ )
					model.to(UpperCAmelCase__ )
					model.eval()
					__UpperCAmelCase      :					List[str]   =   input_ids.unsqueeze(1 ).expand(-1       ,				self.num_choices       ,				-1 ).contiguous()
					__UpperCAmelCase      :					Optional[int]   =   input_mask.unsqueeze(1 ).expand(-1       ,				self.num_choices       ,				-1 ).contiguous()
					__UpperCAmelCase      :					Optional[int]   =   model(
					    UpperCAmelCase__       ,				attention_mask=UpperCAmelCase__       ,				labels=UpperCAmelCase__       ,				)
					self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_choices) )





		def 							__A				(   self )				->   Union[str, Any]:

					'''simple docstring'''
					__UpperCAmelCase      :					Union[str, Any]   =   self.prepare_config_and_inputs()
					((__UpperCAmelCase) ,     (__UpperCAmelCase) ,     (__UpperCAmelCase) ,     (__UpperCAmelCase) ,     (__UpperCAmelCase) ,     (__UpperCAmelCase))      :					List[str]   =   config_and_inputs
					__UpperCAmelCase      :					str   =   {"""input_ids""": input_ids, """attention_mask""": input_mask}
					return config, inputs_dict



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





		def 							__A				(   self )				->   Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase      :					List[str]   =   DistilBertModelTester(self )
					__UpperCAmelCase      :					List[Any]   =   ConfigTester(self       ,				config_class=UpperCAmelCase__       ,				dim=37 )





		def 							__A				(   self )				->   Optional[Any]:

					'''simple docstring'''
					self.config_tester.run_common_tests()





		def 							__A				(   self )				->   str:

					'''simple docstring'''
					__UpperCAmelCase      :					Any   =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase__ )





		def 							__A				(   self )				->   Any:

					'''simple docstring'''
					__UpperCAmelCase      :					Any   =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase__ )





		def 							__A				(   self )				->   str:

					'''simple docstring'''
					__UpperCAmelCase      :					Any   =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase__ )





		def 							__A				(   self )				->   Tuple:

					'''simple docstring'''
					__UpperCAmelCase      :					Dict   =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase__ )





		def 							__A				(   self )				->   Any:

					'''simple docstring'''
					__UpperCAmelCase      :					str   =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase__ )





		def 							__A				(   self )				->   List[str]:

					'''simple docstring'''
					__UpperCAmelCase      :					Optional[Any]   =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase__ )





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

					'''simple docstring'''
					for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__UpperCAmelCase      :					List[str]   =   DistilBertModel.from_pretrained(UpperCAmelCase__ )
								self.assertIsNotNone(UpperCAmelCase__ )





		@slow
		@require_torch_gpu
		def 							__A				(   self )				->   Any:

					'''simple docstring'''
					__UpperCAmelCase ,     __UpperCAmelCase      :					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

								__UpperCAmelCase      :					Optional[Any]   =   True
								__UpperCAmelCase      :					Optional[Any]   =   model_class(config=UpperCAmelCase__ )

								__UpperCAmelCase      :					Any   =   self._prepare_for_class(UpperCAmelCase__       ,				UpperCAmelCase__ )
								__UpperCAmelCase      :					Optional[Any]   =   torch.jit.trace(
								    UpperCAmelCase__       ,				(inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) )

								with tempfile.TemporaryDirectory() as tmp:
											torch.jit.save(UpperCAmelCase__       ,				os.path.join(UpperCAmelCase__       ,				"""traced_model.pt""" ) )
											__UpperCAmelCase      :					List[str]   =   torch.jit.load(os.path.join(UpperCAmelCase__       ,				"""traced_model.pt""" )       ,				map_location=UpperCAmelCase__ )
											loaded(inputs_dict["""input_ids"""].to(UpperCAmelCase__ )       ,				inputs_dict["""attention_mask"""].to(UpperCAmelCase__ ) )



@require_torch
class _A     (			unittest.TestCase			):





		@slow
		def 							__A				(   self )				->   Union[str, Any]:

					'''simple docstring'''
					__UpperCAmelCase      :					Tuple   =   DistilBertModel.from_pretrained("""distilbert-base-uncased""" )
					__UpperCAmelCase      :					Tuple   =   torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] )
					__UpperCAmelCase      :					List[Any]   =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
					with torch.no_grad():
								__UpperCAmelCase      :					List[str]   =   model(UpperCAmelCase__       ,				attention_mask=UpperCAmelCase__ )[0]
					__UpperCAmelCase      :					Tuple   =   torch.Size((1, 11, 768) )
					self.assertEqual(output.shape       ,				UpperCAmelCase__ )
					__UpperCAmelCase      :					int   =   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]       ,				UpperCAmelCase__       ,				atol=1E-4 ) )







"""simple docstring"""





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin




class 					_lowerCAmelCase  (	unittest.TestCase     ,lowercase   ):







 """simple docstring"""







 def 					_lowercase (			self							:			List[Any]	):
    __lowercase								=						load_tool("text-classification"	)
    self.tool.setup()
    __lowercase								=						load_tool("text-classification",					remote=UpperCAmelCase__	)


 def 					_lowercase (			self							:			str	):
    __lowercase								=						self.tool("That's quite cool",					["positive", "negative"]	)
    self.assertEqual(UpperCAmelCase__,					"positive"	)


 def 					_lowercase (			self							:			str	):
    __lowercase								=						self.remote_tool("That's quite cool",					["positive", "negative"]	)
    self.assertEqual(UpperCAmelCase__,					"positive"	)


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						self.tool(text="That's quite cool",					labels=["positive", "negative"]	)
    self.assertEqual(UpperCAmelCase__,					"positive"	)



 def 					_lowercase (			self							:			Tuple	):
    __lowercase								=						self.remote_tool(text="That's quite cool",					labels=["positive", "negative"]	)
    self.assertEqual(UpperCAmelCase__,					"positive"	)




