
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			:     List[Any]           =   'hf-internal-testing/tiny-random-bert'
__lowerCAmelCase			:     str           =   os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
__lowerCAmelCase			:     int           =   '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'




class        UpperCAmelCase_			( unittest.TestCase     ):


  '''simple docstring'''







  def 							_lowercase							( self			: int    )			->	str:







    """simple docstring"""






    __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 							_lowercase							( self			: Dict    )			->	Any:







    """simple docstring"""






    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 							_lowercase							( self			: Optional[int]    )			->	Tuple:







    """simple docstring"""






    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 							_lowercase							( self			: Union[str, Any]    )			->	Any:







    """simple docstring"""






    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 							_lowercase							( self			: str    )			->	str:







    """simple docstring"""






    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 							_lowercase							( self			: Optional[Any]    )			->	Optional[Any]:







    """simple docstring"""






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





"""simple docstring"""



from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers.models.distilbert.modeling_tf_distilbert import (
      TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFDistilBertForMaskedLM,
      TFDistilBertForMultipleChoice,
      TFDistilBertForQuestionAnswering,
      TFDistilBertForSequenceClassification,
      TFDistilBertForTokenClassification,
      TFDistilBertModel,
  )







class _lowerCAmelCase				:







   def __init__(	self						,	UpperCamelCase__						,	)	->		str:

       '''simple docstring'''




       snake_case    :      List[str]      					=    parent
       snake_case    :      int      					=    13
       snake_case    :      Optional[int]      					=    7
       snake_case    :      Tuple      					=    True
       snake_case    :      Optional[Any]      					=    True
       snake_case    :      Optional[Any]      					=    False
       snake_case    :      Optional[Any]      					=    True
       snake_case    :      List[Any]      					=    99
       snake_case    :      Union[str, Any]      					=    32
       snake_case    :      Union[str, Any]      					=    2
       snake_case    :      Union[str, Any]      					=    4
       snake_case    :      Optional[Any]      					=    37
       snake_case    :      str      					=    "gelu"
       snake_case    :      int      					=    0.1
       snake_case    :      Any      					=    0.1
       snake_case    :      Any      					=    512
       snake_case    :      Union[str, Any]      					=    16
       snake_case    :      List[str]      					=    2
       snake_case    :      int      					=    0.02
       snake_case    :      str      					=    3
       snake_case    :      Tuple      					=    4
       snake_case    :      Optional[Any]      					=    None







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       snake_case    :      List[str]      					=    ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size						)

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

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

       snake_case    :      str      					=    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						,	)

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







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

       '''simple docstring'''




       snake_case    :      Optional[Any]      					=    TFDistilBertModel(config=UpperCamelCase__						)
       snake_case    :      Union[str, Any]      					=    {"input_ids": input_ids, "attention_mask": input_mask}

       snake_case    :      Union[str, Any]      					=    model(UpperCamelCase__						)

       snake_case    :      Tuple      					=    [input_ids, input_mask]

       snake_case    :      Optional[Any]      					=    model(UpperCamelCase__						)

       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__						)	->		Any:

       '''simple docstring'''




       snake_case    :      Tuple      					=    TFDistilBertForMaskedLM(config=UpperCamelCase__						)
       snake_case    :      Union[str, Any]      					=    {"input_ids": input_ids, "attention_mask": input_mask}
       snake_case    :      List[str]      					=    model(UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		str:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    TFDistilBertForQuestionAnswering(config=UpperCamelCase__						)
       snake_case    :      Optional[Any]      					=    {
           "input_ids": input_ids,
           "attention_mask": input_mask,
       }
       snake_case    :      str      					=    model(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  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		str:

       '''simple docstring'''




       snake_case    :      int      					=    self.num_labels
       snake_case    :      List[str]      					=    TFDistilBertForSequenceClassification(UpperCamelCase__						)
       snake_case    :      List[str]      					=    {"input_ids": input_ids, "attention_mask": input_mask}
       snake_case    :      List[str]      					=    model(UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		Any:

       '''simple docstring'''




       snake_case    :      Tuple      					=    self.num_choices
       snake_case    :      Any      					=    TFDistilBertForMultipleChoice(UpperCamelCase__						)
       snake_case    :      List[str]      					=    tf.tile(tf.expand_dims(UpperCamelCase__						,	1						)						,	(1, self.num_choices, 1)						)
       snake_case    :      Any      					=    tf.tile(tf.expand_dims(UpperCamelCase__						,	1						)						,	(1, self.num_choices, 1)						)
       snake_case    :      Optional[Any]      					=    {
           "input_ids": multiple_choice_inputs_ids,
           "attention_mask": multiple_choice_input_mask,
       }
       snake_case    :      int      					=    model(UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_choices)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		Dict:

       '''simple docstring'''




       snake_case    :      str      					=    self.num_labels
       snake_case    :      Tuple      					=    TFDistilBertForTokenClassification(UpperCamelCase__						)
       snake_case    :      Union[str, Any]      					=    {"input_ids": input_ids, "attention_mask": input_mask}
       snake_case    :      List[Any]      					=    model(UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.num_labels)						)







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

       '''simple docstring'''




       snake_case    :      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    :      Union[str, Any]      					=    {"input_ids": input_ids, "attention_mask": input_mask}
       return config, inputs_dict







@require_tf
class _lowerCAmelCase				(      snake_case_    ,							snake_case_    ,							unittest.TestCase	):
   __UpperCAmelCase :       List[str]							  =  (
       (
           TFDistilBertModel,
           TFDistilBertForMaskedLM,
           TFDistilBertForQuestionAnswering,
           TFDistilBertForSequenceClassification,
           TFDistilBertForTokenClassification,
           TFDistilBertForMultipleChoice,
       )
       if is_tf_available()
       else None
   )
   __UpperCAmelCase :       Optional[int]							  =  (
       {
           '''feature-extraction''': TFDistilBertModel,
           '''fill-mask''': TFDistilBertForMaskedLM,
           '''question-answering''': TFDistilBertForQuestionAnswering,
           '''text-classification''': TFDistilBertForSequenceClassification,
           '''token-classification''': TFDistilBertForTokenClassification,
           '''zero-shot''': TFDistilBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   __UpperCAmelCase :       Optional[int]							  =  False
   __UpperCAmelCase :       Optional[Any]							  =  False







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       snake_case    :      int      					=    TFDistilBertModelTester(self						)
       snake_case    :      str      					=    ConfigTester(self						,	config_class=UpperCamelCase__						,	dim=37						)







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




       self.config_tester.run_common_tests()







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      Union[str, Any]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Dict:

       '''simple docstring'''




       snake_case    :      int      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case    :      List[Any]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      str      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case    :      int      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case    :      Any      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__						)







   @slow
   def  lowerCamelCase		(	self						)	->		List[str]:

       '''simple docstring'''




       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]						):
           snake_case    :      Optional[int]      					=    TFDistilBertModel.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)







@require_tf
class _lowerCAmelCase				(      unittest.TestCase	):







   @slow
   def  lowerCamelCase		(	self						)	->		Dict:

       '''simple docstring'''




       snake_case    :      str      					=    TFDistilBertModel.from_pretrained("distilbert-base-uncased"						)
       snake_case    :      Optional[int]      					=    tf.constant([[0, 1, 2, 3, 4, 5]]						)
       snake_case    :      Any      					=    model(UpperCamelCase__						)[0]

       snake_case    :      Dict      					=    [1, 6, 768]
       self.assertEqual(output.shape						,	UpperCamelCase__						)

       snake_case    :      str      					=    tf.constant(
           [
               [
                   [0.19261885, -0.13732955, 0.4119799],
                   [0.22150156, -0.07422661, 0.39037204],
                   [0.22756018, -0.0896414, 0.3701467],
               ]
           ]						)
       tf.debugging.assert_near(output[:, :3, :3]						,	UpperCamelCase__						,	atol=1e-4						)





