

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







     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.0_2 ,						UpperCamelCase_=3 ,						UpperCamelCase_=4 ,						UpperCamelCase_=None ,						):
      __magic_name__     =      parent
      __magic_name__     =      batch_size
      __magic_name__     =      seq_length
      __magic_name__     =      is_training
      __magic_name__     =      use_input_mask
      __magic_name__     =      use_token_type_ids
      __magic_name__     =      use_labels
      __magic_name__     =      vocab_size
      __magic_name__     =      hidden_size
      __magic_name__     =      num_hidden_layers
      __magic_name__     =      num_attention_heads
      __magic_name__     =      intermediate_size
      __magic_name__     =      hidden_act
      __magic_name__     =      hidden_dropout_prob
      __magic_name__     =      attention_probs_dropout_prob
      __magic_name__     =      max_position_embeddings
      __magic_name__     =      type_vocab_size
      __magic_name__     =      type_sequence_label_size
      __magic_name__     =      initializer_range
      __magic_name__     =      num_labels
      __magic_name__     =      num_choices
      __magic_name__     =      scope







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size       )

      __magic_name__     =      None
      if self.use_input_mask:
       __magic_name__     =      random_attention_mask([self.batch_size, self.seq_length]       )

      __magic_name__     =      None
      __magic_name__     =      None
      __magic_name__     =      None
      if self.use_labels:
       __magic_name__     =      ids_tensor([self.batch_size] ,						self.type_sequence_label_size       )
       __magic_name__     =      ids_tensor([self.batch_size, self.seq_length] ,						self.num_labels       )
       __magic_name__     =      ids_tensor([self.batch_size] ,						self.num_choices       )

      __magic_name__     =      self.get_config()

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







     def 	lowerCAmelCase__    (      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 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      DistilBertModel(config=__SCREAMING_SNAKE_CASE       )
      model.to(__SCREAMING_SNAKE_CASE       )
      model.eval()
      __magic_name__     =      model(__SCREAMING_SNAKE_CASE ,						__SCREAMING_SNAKE_CASE       )
      __magic_name__     =      model(__SCREAMING_SNAKE_CASE       )
      self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      DistilBertForMaskedLM(config=__SCREAMING_SNAKE_CASE       )
      model.to(__SCREAMING_SNAKE_CASE       )
      model.eval()
      __magic_name__     =      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 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      DistilBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE       )
      model.to(__SCREAMING_SNAKE_CASE       )
      model.eval()
      __magic_name__     =      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 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      self.num_labels
      __magic_name__     =      DistilBertForSequenceClassification(__SCREAMING_SNAKE_CASE       )
      model.to(__SCREAMING_SNAKE_CASE       )
      model.eval()
      __magic_name__     =      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 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      self.num_labels
      __magic_name__     =      DistilBertForTokenClassification(config=__SCREAMING_SNAKE_CASE       )
      model.to(__SCREAMING_SNAKE_CASE       )
      model.eval()

      __magic_name__     =      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 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      self.num_choices
      __magic_name__     =      DistilBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE       )
      model.to(__SCREAMING_SNAKE_CASE       )
      model.eval()
      __magic_name__     =      input_ids.unsqueeze(1       ).expand(-1 ,						self.num_choices ,						-1       ).contiguous()
      __magic_name__     =      input_mask.unsqueeze(1       ).expand(-1 ,						self.num_choices ,						-1       ).contiguous()
      __magic_name__     =      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 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.prepare_config_and_inputs()
      ((__magic_name__)   ,						(__magic_name__)   ,						(__magic_name__)   ,						(__magic_name__)   ,						(__magic_name__)   ,						(__magic_name__))     =      config_and_inputs
      __magic_name__     =      {'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict


@require_torch
class        _lowercase					(     __UpperCAmelCase   , __UpperCAmelCase   , unittest.TestCase     ):
     _lowerCamelCase     =       (
         (
             DistilBertModel,
             DistilBertForMaskedLM,
             DistilBertForMultipleChoice,
             DistilBertForQuestionAnswering,
             DistilBertForSequenceClassification,
             DistilBertForTokenClassification,
         )
         if is_torch_available()
         else None
     )
     _lowerCamelCase     =       (
         {
             '''feature-extraction''': DistilBertModel,
             '''fill-mask''': DistilBertForMaskedLM,
             '''question-answering''': DistilBertForQuestionAnswering,
             '''text-classification''': DistilBertForSequenceClassification,
             '''token-classification''': DistilBertForTokenClassification,
             '''zero-shot''': DistilBertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     _lowerCamelCase     =       True
     _lowerCamelCase     =       True
     _lowerCamelCase     =       True
     _lowerCamelCase     =       True







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      DistilBertModelTester(self       )
      __magic_name__     =      ConfigTester(self ,						config_class=__SCREAMING_SNAKE_CASE ,						dim=37       )







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







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*__SCREAMING_SNAKE_CASE       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*__SCREAMING_SNAKE_CASE       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*__SCREAMING_SNAKE_CASE       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*__SCREAMING_SNAKE_CASE       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*__SCREAMING_SNAKE_CASE       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*__SCREAMING_SNAKE_CASE       )







     @slow
     def 	lowerCAmelCase__    (      self       ):

      for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __magic_name__     =      DistilBertModel.from_pretrained(__SCREAMING_SNAKE_CASE       )
       self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )







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

       __magic_name__     =      True
       __magic_name__     =      model_class(config=__SCREAMING_SNAKE_CASE       )

       __magic_name__     =      self._prepare_for_class(__SCREAMING_SNAKE_CASE ,						__SCREAMING_SNAKE_CASE       )
       __magic_name__     =      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'''       )       )
        __magic_name__     =      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        _lowercase					(     unittest.TestCase     ):







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




"""simple docstring"""






import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


__lowerCamelCase										=		"hf-internal-testing/tiny-random-bert"
__lowerCamelCase										=		os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
__lowerCamelCase										=		"9b8c223d42b2188cb49d29af482996f9d0f3e5a6"
class        _lowercase					(     unittest.TestCase     ):







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      cached_file(UpperCamelCase_ ,						UpperCamelCase_       )
      # Should have downloaded the file in here
      self.assertTrue(os.path.isdir(UpperCamelCase_       )       )
      # Cache should contain at least those three subfolders:
      for subfolder in ["blobs", "refs", "snapshots"]:
       self.assertTrue(os.path.isdir(os.path.join(UpperCamelCase_ ,						UpperCamelCase_       )       )       )
      with open(os.path.join(UpperCamelCase_ ,						'''refs''' ,						'''main'''       )       ) as f:
       __magic_name__     =      f.read()
      self.assertEqual(UpperCamelCase_ ,						os.path.join(UpperCamelCase_ ,						'''snapshots''' ,						UpperCamelCase_ ,						UpperCamelCase_       )       )
      self.assertTrue(os.path.isfile(UpperCamelCase_       )       )

      # File is cached at the same place the second time.
      __magic_name__     =      cached_file(UpperCamelCase_ ,						UpperCamelCase_       )
      self.assertEqual(UpperCamelCase_ ,						UpperCamelCase_       )

      # Using a specific revision to test the full commit hash.
      __magic_name__     =      cached_file(UpperCamelCase_ ,						UpperCamelCase_ ,						revision='''9b8c223'''       )
      self.assertEqual(UpperCamelCase_ ,						os.path.join(UpperCamelCase_ ,						'''snapshots''' ,						UpperCamelCase_ ,						UpperCamelCase_       )       )







     def 	lowerCAmelCase__    (      self       ):
      with self.assertRaisesRegex(UpperCamelCase_ ,						'''is not a valid model identifier'''       ):
       __magic_name__     =      cached_file('''tiny-random-bert''' ,						UpperCamelCase_       )

      with self.assertRaisesRegex(UpperCamelCase_ ,						'''is not a valid git identifier'''       ):
       __magic_name__     =      cached_file(UpperCamelCase_ ,						UpperCamelCase_ ,						revision='''aaaa'''       )

      with self.assertRaisesRegex(UpperCamelCase_ ,						'''does not appear to have a file named'''       ):
       __magic_name__     =      cached_file(UpperCamelCase_ ,						'''conf'''       )







     def 	lowerCAmelCase__    (      self       ):
      with self.assertRaisesRegex(UpperCamelCase_ ,						'''does not appear to have a file named'''       ):
       __magic_name__     =      cached_file(UpperCamelCase_ ,						'''conf'''       )

      with open(os.path.join(UpperCamelCase_ ,						'''refs''' ,						'''main'''       )       ) as f:
       __magic_name__     =      f.read()
      self.assertTrue(os.path.isfile(os.path.join(UpperCamelCase_ ,						'''.no_exist''' ,						UpperCamelCase_ ,						'''conf'''       )       )       )

      __magic_name__     =      cached_file(UpperCamelCase_ ,						'''conf''' ,						_raise_exceptions_for_missing_entries=UpperCamelCase_       )
      self.assertIsNone(UpperCamelCase_       )

      __magic_name__     =      cached_file(UpperCamelCase_ ,						'''conf''' ,						local_files_only=UpperCamelCase_ ,						_raise_exceptions_for_missing_entries=UpperCamelCase_       )
      self.assertIsNone(UpperCamelCase_       )

      __magic_name__     =      mock.Mock()
      __magic_name__     =      500
      __magic_name__     =      {}
      __magic_name__     =      HTTPError
      __magic_name__     =      {}

      # Under the mock environment we get a 500 error when trying to reach the tokenizer.
      with mock.patch('''requests.Session.request''' ,						return_value=UpperCamelCase_       ) as mock_head:
       __magic_name__     =      cached_file(UpperCamelCase_ ,						'''conf''' ,						_raise_exceptions_for_connection_errors=UpperCamelCase_       )
       self.assertIsNone(UpperCamelCase_       )
       # This check we did call the fake head request
       mock_head.assert_called()







     def 	lowerCAmelCase__    (      self       ):
      self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,						UpperCamelCase_       )       )
      self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,						UpperCamelCase_       )       )
      self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' ,						UpperCamelCase_       )       )







     def 	lowerCAmelCase__    (      self       ):
      # `get_file_from_repo` returns None if the file does not exist
      self.assertIsNone(get_file_from_repo('''bert-base-cased''' ,						'''ahah.txt'''       )       )

      # The function raises if the repository does not exist.
      with self.assertRaisesRegex(UpperCamelCase_ ,						'''is not a valid model identifier'''       ):
       get_file_from_repo('''bert-base-case''' ,						UpperCamelCase_       )

      # The function raises if the revision does not exist.
      with self.assertRaisesRegex(UpperCamelCase_ ,						'''is not a valid git identifier'''       ):
       get_file_from_repo('''bert-base-cased''' ,						UpperCamelCase_ ,						revision='''ahaha'''       )

      __magic_name__     =      get_file_from_repo('''bert-base-cased''' ,						UpperCamelCase_       )
      # The name is the cached name which is not very easy to test, so instead we load the content.
      __magic_name__     =      json.loads(open(UpperCamelCase_ ,						'''r'''       ).read()       )
      self.assertEqual(config['''hidden_size'''] ,						768       )







     def 	lowerCAmelCase__    (      self       ):
      with tempfile.TemporaryDirectory() as tmp_dir:
       __magic_name__     =      Path(UpperCamelCase_       ) / '''a.txt'''
       filename.touch()
       self.assertEqual(get_file_from_repo(UpperCamelCase_ ,						'''a.txt'''       ) ,						str(UpperCamelCase_       )       )

       self.assertIsNone(get_file_from_repo(UpperCamelCase_ ,						'''b.txt'''       )       )

