



"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
     from transformers import (
         AutoConfig,
         BertConfig,
         GPTaConfig,
         TaConfig,
         TFAutoModel,
         TFAutoModelForCausalLM,
         TFAutoModelForMaskedLM,
         TFAutoModelForPreTraining,
         TFAutoModelForQuestionAnswering,
         TFAutoModelForSeqaSeqLM,
         TFAutoModelForSequenceClassification,
         TFAutoModelWithLMHead,
         TFBertForMaskedLM,
         TFBertForPreTraining,
         TFBertForQuestionAnswering,
         TFBertForSequenceClassification,
         TFBertModel,
         TFGPTaLMHeadModel,
         TFRobertaForMaskedLM,
         TFTaForConditionalGeneration,
     )
     from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
     from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
     from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
     from transformers import (
         AutoModel,
         AutoModelForCausalLM,
         AutoModelForMaskedLM,
         AutoModelForPreTraining,
         AutoModelForQuestionAnswering,
         AutoModelForSeqaSeqLM,
         AutoModelForSequenceClassification,
         AutoModelWithLMHead,
         BertForMaskedLM,
         BertForPreTraining,
         BertForQuestionAnswering,
         BertForSequenceClassification,
         BertModel,
         GPTaLMHeadModel,
         RobertaForMaskedLM,
         TaForConditionalGeneration,
     )







@is_pt_tf_cross_test
class       UpperCamelCase__   (	unittest.TestCase  ):




      """simple docstring"""




      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      Tuple       ):
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        for model_name in ["bert-base-uncased"]:
          lowerCAmelCase_   :				Optional[Any]								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Union[str, Any]								= TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				List[Any]								= AutoModel.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      Union[str, Any]       ):
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        for model_name in ["bert-base-uncased"]:
          lowerCAmelCase_   :				List[Any]								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Dict								= TFAutoModelForPreTraining.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Union[str, Any]								= AutoModelForPreTraining.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      Union[str, Any]       ):
        for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase_   :				int								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Optional[Any]								= TFAutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          lowerCAmelCase_						,lowerCAmelCase_   :				str								= TFAutoModelForCausalLM.from_pretrained(
              SCREAMING_SNAKE_CASE_    ,			output_loading_info=SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Optional[Any]								= AutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          lowerCAmelCase_						,lowerCAmelCase_   :				Optional[Any]								= AutoModelForCausalLM.from_pretrained(
              SCREAMING_SNAKE_CASE_    ,			output_loading_info=SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]       ):
        for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase_   :				List[str]								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Dict								= TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				List[str]								= AutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      str       ):
        for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase_   :				List[str]								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				str								= TFAutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          lowerCAmelCase_						,lowerCAmelCase_   :				Any								= TFAutoModelForMaskedLM.from_pretrained(
              SCREAMING_SNAKE_CASE_    ,			output_loading_info=SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Union[str, Any]								= AutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          lowerCAmelCase_						,lowerCAmelCase_   :				List[Any]								= AutoModelForMaskedLM.from_pretrained(
              SCREAMING_SNAKE_CASE_    ,			output_loading_info=SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      Union[str, Any]       ):
        for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase_   :				Optional[Any]								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Dict								= TFAutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          lowerCAmelCase_						,lowerCAmelCase_   :				Tuple								= TFAutoModelForSeqaSeqLM.from_pretrained(
              SCREAMING_SNAKE_CASE_    ,			output_loading_info=SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Union[str, Any]								= AutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          lowerCAmelCase_						,lowerCAmelCase_   :				Optional[int]								= AutoModelForSeqaSeqLM.from_pretrained(
              SCREAMING_SNAKE_CASE_    ,			output_loading_info=SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      str       ):
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        for model_name in ["bert-base-uncased"]:
          lowerCAmelCase_   :				List[Any]								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				List[str]								= TFAutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Union[str, Any]								= AutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      @slow
      def      SCREAMING_SNAKE_CASE__ (      self				:      Tuple       ):
        # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        for model_name in ["bert-base-uncased"]:
          lowerCAmelCase_   :				str								= AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Any								= TFAutoModelForQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )

          lowerCAmelCase_   :				Optional[int]								= AutoModelForQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
          self.assertIsNotNone(SCREAMING_SNAKE_CASE_       )
          self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )



      def      SCREAMING_SNAKE_CASE__ (      self				:      List[str]       ):
        lowerCAmelCase_   :				Optional[Any]								= TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
        self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )
        self.assertEqual(model.num_parameters()    ,			1_4_4_1_0       )
        self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE_       )    ,			1_4_4_1_0       )

        lowerCAmelCase_   :				Tuple								= AutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
        self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )
        self.assertEqual(model.num_parameters()    ,			1_4_4_1_0       )
        self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE_       )    ,			1_4_4_1_0       )





      def      SCREAMING_SNAKE_CASE__ (      self				:      str       ):
        lowerCAmelCase_   :				List[Any]								= TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_pt=SCREAMING_SNAKE_CASE_       )
        self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )
        self.assertEqual(model.num_parameters()    ,			1_4_4_1_0       )
        self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE_       )    ,			1_4_4_1_0       )

        lowerCAmelCase_   :				List[Any]								= AutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE_    ,			from_tf=SCREAMING_SNAKE_CASE_       )
        self.assertIsInstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )
        self.assertEqual(model.num_parameters()    ,			1_4_4_1_0       )
        self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE_       )    ,			1_4_4_1_0       )










"""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__		:				List[str]          =	logging.getLogger()

def 				UpperCamelCase_    (    lowerCAmelCase__      :				str						)						->   Dict:

  """simple docstring"""




  lowerCAmelCase_   :				Dict								= {}
  lowerCAmelCase_   :				Union[str, Any]								= os.path.join(lowerCAmelCase__    ,   'all_results.json'						)
  if os.path.exists(lowerCAmelCase__						):
    with open(lowerCAmelCase__    ,   'r'						) as f:
      lowerCAmelCase_   :				Any								= json.load(lowerCAmelCase__						)
  else:
    raise ValueError(f"can't find {path}"						)
  return results


lowercase__		:				Optional[int]          =	logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)







@require_torch_tpu
class       UpperCamelCase__   (	lowercase_  ):




      """simple docstring"""




      def      SCREAMING_SNAKE_CASE__ (      self				:      Any       ):
        import xla_spawn

        lowerCAmelCase_   :				Union[str, Any]								= self.get_auto_remove_tmp_dir()
        lowerCAmelCase_   :				Optional[Any]								= F"\n            ./examples/pytorch/text-classification/run_glue.py\n            --num_cores=8\n            ./examples/pytorch/text-classification/run_glue.py\n            --model_name_or_path distilbert-base-uncased\n            --output_dir {tmp_dir}\n            --overwrite_output_dir\n            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n            --do_train\n            --do_eval\n            --debug tpu_metrics_debug\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --learning_rate=1e-4\n            --max_steps=10\n            --warmup_steps=2\n            --seed=42\n            --max_seq_length=128\n            ".split()

        with patch.object(SCREAMING_SNAKE_CASE_    ,			'argv'    ,			SCREAMING_SNAKE_CASE_       ):
          lowerCAmelCase_   :				Optional[Any]								= time()
          xla_spawn.main()
          lowerCAmelCase_   :				Any								= time()

          lowerCAmelCase_   :				Dict								= get_results(SCREAMING_SNAKE_CASE_       )
          self.assertGreaterEqual(result['eval_accuracy']    ,			0.75       )

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





      def      SCREAMING_SNAKE_CASE__ (      self				:      List[str]       ):
        import xla_spawn

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





