






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





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase=1_3  ,      _lowerCamelCase=7  ,      _lowerCamelCase=True  ,      _lowerCamelCase=True  ,      _lowerCamelCase=False  ,      _lowerCamelCase=True  ,      _lowerCamelCase=9_9  ,      _lowerCamelCase=3_2  ,      _lowerCamelCase=5  ,      _lowerCamelCase=4  ,      _lowerCamelCase=3_7  ,      _lowerCamelCase="gelu"  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=5_1_2  ,      _lowerCamelCase=1_6  ,      _lowerCamelCase=2  ,      _lowerCamelCase=0.0_2  ,      _lowerCamelCase=3  ,      _lowerCamelCase=4  ,      _lowerCamelCase=None  ,      ):
         UpperCamelCase_: Optional[int]   				=    parent
         UpperCamelCase_: List[Any]   				=    batch_size
         UpperCamelCase_: Optional[Any]   				=    seq_length
         UpperCamelCase_: Optional[Any]   				=    is_training
         UpperCamelCase_: Any   				=    use_input_mask
         UpperCamelCase_: Tuple   				=    use_token_type_ids
         UpperCamelCase_: Union[str, Any]   				=    use_labels
         UpperCamelCase_: List[Any]   				=    vocab_size
         UpperCamelCase_: str   				=    hidden_size
         UpperCamelCase_: Union[str, Any]   				=    num_hidden_layers
         UpperCamelCase_: int   				=    num_attention_heads
         UpperCamelCase_: str   				=    intermediate_size
         UpperCamelCase_: Optional[int]   				=    hidden_act
         UpperCamelCase_: Optional[int]   				=    hidden_dropout_prob
         UpperCamelCase_: Any   				=    attention_probs_dropout_prob
         UpperCamelCase_: Dict   				=    max_position_embeddings
         UpperCamelCase_: List[str]   				=    type_vocab_size
         UpperCamelCase_: Optional[int]   				=    type_sequence_label_size
         UpperCamelCase_: Union[str, Any]   				=    initializer_range
         UpperCamelCase_: Optional[int]   				=    num_labels
         UpperCamelCase_: List[Any]   				=    num_choices
         UpperCamelCase_: str   				=    scope





       def       _a   ( self							):
         UpperCamelCase_: List[str]   				=    ids_tensor([self.batch_size, self.seq_length]  ,      self.vocab_size							)

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

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

         UpperCamelCase_: Union[str, Any]   				=    self.get_config()

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





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





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Optional[int]   				=    DistilBertForMaskedLM(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: List[Any]   				=    model(_lowerCamelCase  ,      attention_mask=_lowerCamelCase  ,      labels=_lowerCamelCase							)
         self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.vocab_size)							)





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





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Dict   				=    self.num_labels
         UpperCamelCase_: Optional[Any]   				=    DistilBertForTokenClassification(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()

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





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





       def       _a   ( self							):
         UpperCamelCase_: List[str]   				=    self.prepare_config_and_inputs()
         ((UpperCamelCase_)				,(UpperCamelCase_)				,(UpperCamelCase_)				,(UpperCamelCase_)				,(UpperCamelCase_)				,(UpperCamelCase_)): Tuple   				=    config_and_inputs
         UpperCamelCase_: Union[str, Any]   				=    {'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict







@require_torch
class     _lowerCAmelCase(	UpperCAmelCase_							,     UpperCAmelCase_							,     unittest.TestCase ):





       """simple docstring"""







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





       def       _a   ( self							):
         UpperCamelCase_: Union[str, Any]   				=    DistilBertModelTester(self							)
         UpperCamelCase_: int   				=    ConfigTester(self  ,      config_class=_lowerCamelCase  ,      dim=3_7							)





       def       _a   ( self							):
         self.config_tester.run_common_tests()





       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_model(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: str   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: Optional[int]   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: Optional[int]   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: int   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: Any   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCamelCase							)





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





       @slow
       @require_torch_gpu
       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_: 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[int]   				=    True
           UpperCamelCase_: Union[str, Any]   				=    model_class(config=_lowerCamelCase							)

           UpperCamelCase_: Optional[int]   				=    self._prepare_for_class(_lowerCamelCase  ,      _lowerCamelCase							)
           UpperCamelCase_: Optional[Any]   				=    torch.jit.trace(
               _lowerCamelCase  ,      (inputs_dict['input_ids'].to('cpu'							), inputs_dict['attention_mask'].to('cpu'							))							)

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







@require_torch
class     _lowerCAmelCase(	unittest.TestCase ):





       """simple docstring"""







       @slow
       def       _a   ( self							):
         UpperCamelCase_: Optional[int]   				=    DistilBertModel.from_pretrained('distilbert-base-uncased'							)
         UpperCamelCase_: int   				=    torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]							)
         UpperCamelCase_: str   				=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
         with torch.no_grad():
           UpperCamelCase_: Union[str, Any]   				=    model(_lowerCamelCase  ,      attention_mask=_lowerCamelCase							)[0]
         UpperCamelCase_: Dict   				=    torch.Size((1, 1_1, 7_6_8)							)
         self.assertEqual(output.shape  ,      _lowerCamelCase							)
         UpperCamelCase_: str   				=    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]  ,      _lowerCamelCase  ,      atol=1e-4							)							)







def   snake_case (UpperCAmelCase__  )	->    int:
  assert isinstance(UpperCAmelCase__     ,   UpperCAmelCase__  ), F'''The input value of [n={number}] is not an integer'''

  if number == 1:
    return 2
  elif number < 1:
    UpperCamelCase_: List[Any]   				=    F'''The input value of [n={number}] has to be > 0'''
    raise ValueError(UpperCAmelCase__  )
  else:
    UpperCamelCase_: str   				=    sylvester(number - 1  )
    UpperCamelCase_: str   				=    num - 1
    UpperCamelCase_: Any   				=    num
    return lower * upper + 1


if __name__ == "__main__":
   print(F'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')