




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







 '''simple docstring'''





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




   '''simple docstring'''



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


 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   _snake_case							:	List[Any]			=						ids_tensor([self.batch_size, self.seq_length],   self.vocab_size	)

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

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

   _snake_case							:	Optional[Any]			=						self.get_config()

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


 def 				UpperCamelCase_     (       self:	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 				UpperCamelCase_     (       self:	Tuple,   a_:	Optional[Any],   a_:	List[Any],   a_:	int,   a_:	Optional[Any],   a_:	Any,   a_:	Tuple	):




   '''simple docstring'''



   _snake_case							:	Dict			=						DistilBertModel(config=UpperCamelCase_	)
   model.to(UpperCamelCase_	)
   model.eval()
   _snake_case							:	Optional[Any]			=						model(UpperCamelCase_,   UpperCamelCase_	)
   _snake_case							:	Optional[Any]			=						model(UpperCamelCase_	)
   self.parent.assertEqual(result.last_hidden_state.shape,   (self.batch_size, self.seq_length, self.hidden_size)	)


 def 				UpperCamelCase_     (       self:	Any,   a_:	Optional[Any],   a_:	List[Any],   a_:	str,   a_:	Optional[int],   a_:	Any,   a_:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	Tuple			=						DistilBertForMaskedLM(config=UpperCamelCase_	)
   model.to(UpperCamelCase_	)
   model.eval()
   _snake_case							:	List[str]			=						model(UpperCamelCase_,   attention_mask=UpperCamelCase_,   labels=UpperCamelCase_	)
   self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.vocab_size)	)


 def 				UpperCamelCase_     (       self:	List[Any],   a_:	Optional[int],   a_:	Dict,   a_:	Any,   a_:	Any,   a_:	Optional[Any],   a_:	str	):




   '''simple docstring'''



   _snake_case							:	int			=						DistilBertForQuestionAnswering(config=UpperCamelCase_	)
   model.to(UpperCamelCase_	)
   model.eval()
   _snake_case							:	Tuple			=						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 				UpperCamelCase_     (       self:	Optional[Any],   a_:	Union[str, Any],   a_:	Tuple,   a_:	List[Any],   a_:	Any,   a_:	Tuple,   a_:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	int			=						self.num_labels
   _snake_case							:	Optional[int]			=						DistilBertForSequenceClassification(UpperCamelCase_	)
   model.to(UpperCamelCase_	)
   model.eval()
   _snake_case							:	str			=						model(UpperCamelCase_,   attention_mask=UpperCamelCase_,   labels=UpperCamelCase_	)
   self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.num_labels)	)


 def 				UpperCamelCase_     (       self:	str,   a_:	Optional[int],   a_:	Union[str, Any],   a_:	Optional[int],   a_:	Dict,   a_:	Union[str, Any],   a_:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	List[Any]			=						self.num_labels
   _snake_case							:	Optional[Any]			=						DistilBertForTokenClassification(config=UpperCamelCase_	)
   model.to(UpperCamelCase_	)
   model.eval()

   _snake_case							:	int			=						model(UpperCamelCase_,   attention_mask=UpperCamelCase_,   labels=UpperCamelCase_	)
   self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.num_labels)	)


 def 				UpperCamelCase_     (       self:	List[str],   a_:	Optional[Any],   a_:	str,   a_:	Optional[int],   a_:	Union[str, Any],   a_:	List[str],   a_:	Optional[Any]	):




   '''simple docstring'''



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


 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						self.prepare_config_and_inputs()
   ((_snake_case)	,      (_snake_case)	,      (_snake_case)	,      (_snake_case)	,      (_snake_case)	,      (_snake_case))							:	Union[str, Any]			=						config_and_inputs
   _snake_case							:	str			=						{"""input_ids""": input_ids, """attention_mask""": input_mask}
   return config, inputs_dict







@require_torch
class       lowercase(  __lowerCamelCase ,    __lowerCamelCase ,    unittest.TestCase				):







 '''simple docstring'''





 lowercase__				      =      (
     (
         DistilBertModel,
         DistilBertForMaskedLM,
         DistilBertForMultipleChoice,
         DistilBertForQuestionAnswering,
         DistilBertForSequenceClassification,
         DistilBertForTokenClassification,
     )
     if is_torch_available()
     else None
 )
 lowercase__				      =      (
     {
         'feature-extraction': DistilBertModel,
         'fill-mask': DistilBertForMaskedLM,
         'question-answering': DistilBertForQuestionAnswering,
         'text-classification': DistilBertForSequenceClassification,
         'token-classification': DistilBertForTokenClassification,
         'zero-shot': DistilBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowercase__				      =      True
 lowercase__				      =      True
 lowercase__				      =      True
 lowercase__				      =      True


 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						DistilBertModelTester(self	)
   _snake_case							:	Any			=						ConfigTester(self,   config_class=UpperCamelCase_,   dim=37	)


 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   self.config_tester.run_common_tests()


 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	Optional[int]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_model(*UpperCamelCase_	)


 def 				UpperCamelCase_     (       self:	Tuple	):




   '''simple docstring'''



   _snake_case							:	Tuple			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase_	)


 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase_	)


 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase_	)


 def 				UpperCamelCase_     (       self:	Any	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase_	)


 def 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   _snake_case							:	int			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase_	)


 @slow
 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:	List[str]			=						DistilBertModel.from_pretrained(UpperCamelCase_	)
     self.assertIsNotNone(UpperCamelCase_	)


 @slow
 @require_torch_gpu
 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   _snake_case	,      _snake_case							:	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

     _snake_case							:	List[Any]			=						True
     _snake_case							:	Optional[Any]			=						model_class(config=UpperCamelCase_	)

     _snake_case							:	Union[str, Any]			=						self._prepare_for_class(UpperCamelCase_,   UpperCamelCase_	)
     _snake_case							:	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"""	)	)
       _snake_case							:	Union[str, Any]			=						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       lowercase(  unittest.TestCase				):







 '''simple docstring'''





 @slow
 def 				UpperCamelCase_     (       self:	List[Any]	):




   '''simple docstring'''



   _snake_case							:	Optional[int]			=						DistilBertModel.from_pretrained("""distilbert-base-uncased"""	)
   _snake_case							:	Dict			=						torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
   _snake_case							:	Tuple			=						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
   with torch.no_grad():
     _snake_case							:	Optional[int]			=						model(UpperCamelCase_,   attention_mask=UpperCamelCase_	)[0]
   _snake_case							:	Optional[Any]			=						torch.Size((1, 11, 768)	)
   self.assertEqual(output.shape,   UpperCamelCase_	)
   _snake_case							:	str			=						torch.tensor(
       [[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]]	)

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








"""simple docstring"""


def       UpperCAmelCase__ (snake_case__							:      int					):
  """simple docstring"""

  if not isinstance(snake_case__	,     snake_case__					) or number < 0:
    raise ValueError("""Input must be a non-negative integer"""					)

  _snake_case							:	Dict			=						0
  while number:
    # This way we arrive at next set bit (next 1) instead of looping
    # through each bit and checking for 1s hence the
    # loop won't run 32 times it will only run the number of `1` times
    number &= number - 1
    count += 1
  return count


if __name__ == "__main__":
     import doctest

     doctest.testmod()


