



'''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 	UpperCAmelCase	:


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  ) ->      int:



      """simple docstring"""







      a_       		=parent
      a_       		=1_3
      a_       		=7
      a_       		=True
      a_       		=True
      a_       		=False
      a_       		=True
      a_       		=9_9
      a_       		=3_2
      a_       		=2
      a_       		=4
      a_       		=3_7
      a_       		="gelu"
      a_       		=0.1
      a_       		=0.1
      a_       		=5_1_2
      a_       		=1_6
      a_       		=2
      a_       		=0.0_2
      a_       		=3
      a_       		=4
      a_       		=None




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



      """simple docstring"""







      a_       		=ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size)

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

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

      a_       		=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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      List[Any]:



      """simple docstring"""







      a_       		=TFDistilBertModel(config=lowerCAmelCase_)
      a_       		={"input_ids": input_ids, "attention_mask": input_mask}

      a_       		=model(lowerCAmelCase_)

      a_       		=[input_ids, input_mask]

      a_       		=model(lowerCAmelCase_)

      self.parent.assertEqual(result.last_hidden_state.shape							,  (self.batch_size, self.seq_length, self.hidden_size))




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Optional[Any]:



      """simple docstring"""







      a_       		=TFDistilBertForMaskedLM(config=lowerCAmelCase_)
      a_       		={"input_ids": input_ids, "attention_mask": input_mask}
      a_       		=model(lowerCAmelCase_)
      self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.seq_length, self.vocab_size))




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Optional[int]:



      """simple docstring"""







      a_       		=TFDistilBertForQuestionAnswering(config=lowerCAmelCase_)
      a_       		={
          "input_ids": input_ids,
          "attention_mask": input_mask,
      }
      a_       		=model(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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      str:



      """simple docstring"""







      a_       		=self.num_labels
      a_       		=TFDistilBertForSequenceClassification(lowerCAmelCase_)
      a_       		={"input_ids": input_ids, "attention_mask": input_mask}
      a_       		=model(lowerCAmelCase_)
      self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_labels))




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      a_       		=self.num_choices
      a_       		=TFDistilBertForMultipleChoice(lowerCAmelCase_)
      a_       		=tf.tile(tf.expand_dims(lowerCAmelCase_							,  1)							,  (1, self.num_choices, 1))
      a_       		=tf.tile(tf.expand_dims(lowerCAmelCase_							,  1)							,  (1, self.num_choices, 1))
      a_       		={
          "input_ids": multiple_choice_inputs_ids,
          "attention_mask": multiple_choice_input_mask,
      }
      a_       		=model(lowerCAmelCase_)
      self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_choices))




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Tuple:



      """simple docstring"""







      a_       		=self.num_labels
      a_       		=TFDistilBertForTokenClassification(lowerCAmelCase_)
      a_       		={"input_ids": input_ids, "attention_mask": input_mask}
      a_       		=model(lowerCAmelCase_)
      self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.seq_length, self.num_labels))




 def       lowercase_  (   self) ->      List[Any]:



      """simple docstring"""







      a_       		=self.prepare_config_and_inputs()
      ((a_)		,						(a_)		,						(a_)		,						(a_)		,						(a_)		,						(a_))       		=config_and_inputs
      a_       		={"input_ids": input_ids, "attention_mask": input_mask}
      return config, inputs_dict







@require_tf
class 	UpperCAmelCase	(		__a					,	__a					,	unittest.TestCase):


 '''simple docstring'''




 __magic_name__					: Union[str, Any]  =      (
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 __magic_name__					: int  =      (
     {
         "feature-extraction": TFDistilBertModel,
         "fill-mask": TFDistilBertForMaskedLM,
         "question-answering": TFDistilBertForQuestionAnswering,
         "text-classification": TFDistilBertForSequenceClassification,
         "token-classification": TFDistilBertForTokenClassification,
         "zero-shot": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __magic_name__					: List[Any]  =      False
 __magic_name__					: str  =      False




 def       lowercase_  (   self) ->      Dict:



      """simple docstring"""







      a_       		=TFDistilBertModelTester(self)
      a_       		=ConfigTester(self							,  config_class=lowerCAmelCase_							,  dim=3_7)




 def       lowercase_  (   self) ->      int:



      """simple docstring"""







      self.config_tester.run_common_tests()




 def       lowercase_  (   self) ->      List[Any]:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase_)




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



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase_)




 def       lowercase_  (   self) ->      int:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase_)




 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase_)




 def       lowercase_  (   self) ->      List[Any]:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase_)




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



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase_)




 @slow
 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]):
           a_       		=TFDistilBertModel.from_pretrained(lowerCAmelCase_)
           self.assertIsNotNone(lowerCAmelCase_)







@require_tf
class 	UpperCAmelCase	(		unittest.TestCase):


 '''simple docstring'''




 @slow
 def       lowercase_  (   self) ->      Optional[int]:



      """simple docstring"""







      a_       		=TFDistilBertModel.from_pretrained("distilbert-base-uncased")
      a_       		=tf.constant([[0, 1, 2, 3, 4, 5]])
      a_       		=model(lowerCAmelCase_)[0]

      a_       		=[1, 6, 7_6_8]
      self.assertEqual(output.shape							,  lowerCAmelCase_)

      a_       		=tf.constant(
          [
              [
                  [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9],
                  [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4],
                  [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7],
              ]
          ])
      tf.debugging.assert_near(output[:, :3, :3]							,  lowerCAmelCase_							,  atol=1e-4)







'''simple docstring'''


import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

lowercase							       =			logging.getLogger()






def  UpperCAmelCase_       (		lowercase__       ):


     '''simple docstring'''







     a_       		={}
     a_       		=os.path.join(lowercase__       ,	"all_results.json"       )
     if os.path.exists(lowercase__       ):
          with open(lowercase__       ,	"r"       ) as f:
               a_       		=json.load(lowercase__       )
     else:
          raise ValueError(F"""can't find {path}"""       )
     return results


lowercase							       =			logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)







@require_torch_tpu
class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 def       lowercase_  (   self) ->      List[Any]:



      """simple docstring"""







      import xla_spawn

      a_       		=self.get_auto_remove_tmp_dir()
      a_       		=f"""
            ./examples/pytorch/text-classification/run_glue.py
            --num_cores=8
            ./examples/pytorch/text-classification/run_glue.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --do_train
            --do_eval
            --debug tpu_metrics_debug
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
            --max_seq_length=128
            """.split()

      with patch.object(lowerCAmelCase_							,  "argv"							,  lowerCAmelCase_):
           a_       		=time()
           xla_spawn.main()
           a_       		=time()

           a_       		=get_results(lowerCAmelCase_)
           self.assertGreaterEqual(result["eval_accuracy"]							,  0.7_5)

           # Assert that the script takes less than 500 seconds to make sure it doesn't hang.
           self.assertLess(end - start							,  5_0_0)




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      import xla_spawn

      a_       		="\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            ".split()
      with patch.object(lowerCAmelCase_							,  "argv"							,  lowerCAmelCase_):
           xla_spawn.main()


