



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




	'''simple docstring'''





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

				"""simple docstring"""

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

	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict)							->			Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase :    int				=			ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size)

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

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

				__lowerCAmelCase :    str				=			self.get_config()

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

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[Any])							->			Optional[int]:

				"""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 				_SCREAMING_SNAKE_CASE  (	self:						Dict			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						Optional[int]			,     _SCREAMING_SNAKE_CASE:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						Any)							->			Tuple:

				"""simple docstring"""

				__lowerCAmelCase :    int				=			DistilBertModel(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase :    Any				=			model(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Dict				=			model(_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.seq_length, self.hidden_size))

	def 				_SCREAMING_SNAKE_CASE  (	self:						int			,     _SCREAMING_SNAKE_CASE:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						int			,     _SCREAMING_SNAKE_CASE:						Union[str, Any])							->			Dict:

				"""simple docstring"""

				__lowerCAmelCase :    int				=			DistilBertForMaskedLM(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase :    Dict				=			model(_SCREAMING_SNAKE_CASE			,     attention_mask=_SCREAMING_SNAKE_CASE			,     labels=_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size))

	def 				_SCREAMING_SNAKE_CASE  (	self:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						Any			,     _SCREAMING_SNAKE_CASE:						List[str]			,     _SCREAMING_SNAKE_CASE:						Dict			,     _SCREAMING_SNAKE_CASE:						int			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						Any)							->			List[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Any				=			DistilBertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase :    Optional[Any]				=			model(
				    _SCREAMING_SNAKE_CASE			,     attention_mask=_SCREAMING_SNAKE_CASE			,     start_positions=_SCREAMING_SNAKE_CASE			,     end_positions=_SCREAMING_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 				_SCREAMING_SNAKE_CASE  (	self:						List[str]			,     _SCREAMING_SNAKE_CASE:						Dict			,     _SCREAMING_SNAKE_CASE:						Any			,     _SCREAMING_SNAKE_CASE:						List[str]			,     _SCREAMING_SNAKE_CASE:						Any			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						str)							->			Union[str, Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Any				=			self.num_labels
				__lowerCAmelCase :    int				=			DistilBertForSequenceClassification(_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase :    Union[str, Any]				=			model(_SCREAMING_SNAKE_CASE			,     attention_mask=_SCREAMING_SNAKE_CASE			,     labels=_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels))

	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict			,     _SCREAMING_SNAKE_CASE:						Optional[Any]			,     _SCREAMING_SNAKE_CASE:						List[str]			,     _SCREAMING_SNAKE_CASE:						List[str]			,     _SCREAMING_SNAKE_CASE:						Tuple			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						List[Any])							->			Any:

				"""simple docstring"""

				__lowerCAmelCase :    Tuple				=			self.num_labels
				__lowerCAmelCase :    int				=			DistilBertForTokenClassification(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()

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

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[int]			,     _SCREAMING_SNAKE_CASE:						str			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						Union[str, Any]			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						List[Any]			,     _SCREAMING_SNAKE_CASE:						Union[str, Any])							->			List[Any]:

				"""simple docstring"""

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







	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[Any])							->			Dict:

				"""simple docstring"""

				__lowerCAmelCase :    Tuple				=			self.prepare_config_and_inputs()
				((__lowerCAmelCase)		,      (__lowerCAmelCase)		,      (__lowerCAmelCase)		,      (__lowerCAmelCase)		,      (__lowerCAmelCase)		,      (__lowerCAmelCase)) :    Any				=			config_and_inputs
				__lowerCAmelCase :    Optional[int]				=			{"input_ids": input_ids, "attention_mask": input_mask}
				return config, inputs_dict






@require_torch
class      A__  (    __SCREAMING_SNAKE_CASE    ,						__SCREAMING_SNAKE_CASE    ,						unittest.TestCase				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      (
	    (
	        DistilBertModel,
	        DistilBertForMaskedLM,
	        DistilBertForMultipleChoice,
	        DistilBertForQuestionAnswering,
	        DistilBertForSequenceClassification,
	        DistilBertForTokenClassification,
	    )
	    if is_torch_available()
	    else None
	)
	SCREAMING_SNAKE_CASE        =      (
	    {
	        '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        =      True
	SCREAMING_SNAKE_CASE        =      True
	SCREAMING_SNAKE_CASE        =      True
	SCREAMING_SNAKE_CASE        =      True

	def 				_SCREAMING_SNAKE_CASE  (	self:						List[Any])							->			List[str]:

				"""simple docstring"""

				__lowerCAmelCase :    List[Any]				=			DistilBertModelTester(self)
				__lowerCAmelCase :    Optional[int]				=			ConfigTester(self			,     config_class=_SCREAMING_SNAKE_CASE			,     dim=37)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[int])							->			Optional[int]:

				"""simple docstring"""

				self.config_tester.run_common_tests()

	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[int])							->			Any:

				"""simple docstring"""

				__lowerCAmelCase :    str				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_model(*_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			List[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    List[Any]				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_masked_lm(*_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Any				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_question_answering(*_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						int)							->			Any:

				"""simple docstring"""

				__lowerCAmelCase :    Tuple				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_sequence_classification(*_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict)							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    int				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_token_classification(*_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Tuple				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_distilbert_for_multiple_choice(*_SCREAMING_SNAKE_CASE)

	@slow
	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict)							->			int:

				"""simple docstring"""

				for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowerCAmelCase :    Optional[int]				=			DistilBertModel.from_pretrained(_SCREAMING_SNAKE_CASE)
							self.assertIsNotNone(_SCREAMING_SNAKE_CASE)







	@slow
	@require_torch_gpu
	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[Any])							->			str:

				"""simple docstring"""

				__lowerCAmelCase		,      __lowerCAmelCase :    Dict				=			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

							__lowerCAmelCase :    Dict				=			True
							__lowerCAmelCase :    Optional[int]				=			model_class(config=_SCREAMING_SNAKE_CASE)

							__lowerCAmelCase :    Any				=			self._prepare_for_class(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
							__lowerCAmelCase :    Union[str, Any]				=			torch.jit.trace(
							    _SCREAMING_SNAKE_CASE			,     (inputs_dict["input_ids"].to("cpu"), inputs_dict["attention_mask"].to("cpu")))

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






@require_torch
class      A__  (    unittest.TestCase				):




	'''simple docstring'''





	@slow
	def 				_SCREAMING_SNAKE_CASE  (	self:						int)							->			Any:

				"""simple docstring"""

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







from typing import List, Optional

from tokenizers import ByteLevelBPETokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_blenderbot_small import BlenderbotSmallTokenizer


__a:     Union[str, Any]   					=					logging.get_logger(__name__)

__a:     str   					=					{
    '''vocab_file''': '''vocab.json''',
    '''merges_file''': '''merges.txt''',
    '''tokenizer_config_file''': '''tokenizer_config.json''',
}

__a:     Dict   					=					{
    '''vocab_file''': {
        '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json'''
    },
    '''merges_file''': {
        '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt'''
    },
    '''tokenizer_config_file''': {
        '''facebook/blenderbot_small-90M''': (
            '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json'''
        )
    },
}

__a:     Union[str, Any]   					=					{
    '''facebook/blenderbot_small-90M''': 512,
}
class 		SCREAMING_SNAKE_CASE__	(  UpperCAmelCase     ):

  '''simple docstring'''







  _lowerCamelCase    =  VOCAB_FILES_NAMES
  _lowerCamelCase    =  PRETRAINED_VOCAB_FILES_MAP
  _lowerCamelCase    =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  _lowerCamelCase    =  BlenderbotSmallTokenizer


  def __init__(      self		:			List[str]       ,				lowerCamelCase		:			List[str]=None       ,				lowerCamelCase		:			str=None       ,				lowerCamelCase		:			Optional[Any]="<|endoftext|>"       ,				lowerCamelCase		:			Dict="<|endoftext|>"       ,				lowerCamelCase		:			str="<|endoftext|>"       ,				lowerCamelCase		:			str=False       ,				lowerCamelCase		:			Tuple=True       ,				**lowerCamelCase		:			List[str]       ,				)						->	List[Any]:


         """simple docstring"""
         super().__init__(
             ByteLevelBPETokenizer(
                 vocab=lowerCamelCase       ,				merges=lowerCamelCase       ,				add_prefix_space=lowerCamelCase       ,				trim_offsets=lowerCamelCase       ,				)       ,				bos_token=lowerCamelCase       ,				eos_token=lowerCamelCase       ,				unk_token=lowerCamelCase       ,				**lowerCamelCase       ,				)
         _UpperCAmelCase							=    add_prefix_space


  def  lowerCamelCase       (      self		:			Union[str, Any]       ,				lowerCamelCase		:			str       ,				lowerCamelCase		:			Union[str, Any]=None							)						->	str:


         """simple docstring"""
         _UpperCAmelCase							=    [self.bos_token_id] + token_ids_a + [self.eos_token_id]
         if token_ids_a is None:
                return output

         return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]





  def  lowerCamelCase       (      self		:			Any       ,				lowerCamelCase		:			List[int]       ,				lowerCamelCase		:			Optional[List[int]] = None							)						->	List[int]:


         """simple docstring"""
         _UpperCAmelCase							=    [self.sep_token_id]
         _UpperCAmelCase							=    [self.cls_token_id]

         if token_ids_a is None:
                return len(cls + token_ids_a + sep							) * [0]
         return len(cls + token_ids_a + sep + sep + token_ids_a + sep							) * [0]