




import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment
@require_huggingface_suite
class       a__   ( unittest.TestCase				):






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	inspect.getfile(accelerate.test_utils      )
											__lowerCAmelCase           =	os.path.sep.join(
											    mod_file.split(os.path.sep      )[:-1] + ["scripts", "external_deps", "test_metrics.py"]      )

											from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

											__lowerCAmelCase           =	test_metrics






						@require_cpu
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											debug_launcher(self.test_metrics.main		,      num_processes=1      )






						@require_cpu
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											debug_launcher(self.test_metrics.main      )






						@require_single_gpu
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											self.test_metrics.main()






						@require_multi_gpu
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											print(f"""Found {torch.cuda.device_count()} devices."""      )
											__lowerCAmelCase           =	["torchrun", f"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path]
											with patch_environment(omp_num_threads=1      ):
																execute_subprocess_async(_A		,      env=os.environ.copy()      )










from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
					from transformers import (
					    TFAutoModel,
					    TFAutoModelForCausalLM,
					    TFAutoModelForMaskedLM,
					    TFAutoModelForPreTraining,
					    TFAutoModelForQuestionAnswering,
					    TFAutoModelForSeqaSeqLM,
					    TFAutoModelForSequenceClassification,
					    TFAutoModelForTableQuestionAnswering,
					    TFAutoModelForTokenClassification,
					    TFAutoModelWithLMHead,
					    TFBertForMaskedLM,
					    TFBertForPreTraining,
					    TFBertForQuestionAnswering,
					    TFBertForSequenceClassification,
					    TFBertModel,
					    TFFunnelBaseModel,
					    TFFunnelModel,
					    TFGPTaLMHeadModel,
					    TFRobertaForMaskedLM,
					    TFTaForConditionalGeneration,
					    TFTapasForQuestionAnswering,
					)
					from transformers.models.auto.modeling_tf_auto import (
					    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
					    TF_MODEL_FOR_MASKED_LM_MAPPING,
					    TF_MODEL_FOR_PRETRAINING_MAPPING,
					    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
					    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
					    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
					    TF_MODEL_MAPPING,
					)
					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
					from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST
class       a__   ( snake_case__				):
						_a							:   Optional[int] =   """new-model"""


if is_tf_available():
					class       a__   ( snake_case__				):
											_a							:   Dict =   NewModelConfig
@require_tf
class       a__   ( unittest.TestCase				):






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"bert-base-cased"
											__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
											self.assertIsNotNone(_A      )
											self.assertIsInstance(_A		,      _A      )

											__lowerCAmelCase           =	TFAutoModel.from_pretrained(_A      )
											self.assertIsNotNone(_A      )
											self.assertIsInstance(_A		,      _A      )






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"bert-base-cased"
											__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
											self.assertIsNotNone(_A      )
											self.assertIsInstance(_A		,      _A      )

											__lowerCAmelCase           =	TFAutoModelForPreTraining.from_pretrained(_A      )
											self.assertIsNotNone(_A      )
											self.assertIsInstance(_A		,      _A      )






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )

																__lowerCAmelCase           =	TFAutoModelForCausalLM.from_pretrained(_A      )
																__lowerCAmelCase       ,							__lowerCAmelCase           =	TFAutoModelForCausalLM.from_pretrained(_A		,      output_loading_info=_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )

																__lowerCAmelCase           =	TFAutoModelWithLMHead.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )

																__lowerCAmelCase           =	TFAutoModelForMaskedLM.from_pretrained(_A      )
																__lowerCAmelCase       ,							__lowerCAmelCase           =	TFAutoModelForMaskedLM.from_pretrained(_A		,      output_loading_info=_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )

																__lowerCAmelCase           =	TFAutoModelForSeqaSeqLM.from_pretrained(_A      )
																__lowerCAmelCase       ,							__lowerCAmelCase           =	TFAutoModelForSeqaSeqLM.from_pretrained(_A		,      output_loading_info=_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in ["bert-base-uncased"]:
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )

																__lowerCAmelCase           =	TFAutoModelForSequenceClassification.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )






						@slow
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in ["bert-base-uncased"]:
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )

																__lowerCAmelCase           =	TFAutoModelForQuestionAnswering.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )






						@slow
						@require_tensorflow_probability
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
																__lowerCAmelCase           =	AutoConfig.from_pretrained(_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )

																__lowerCAmelCase           =	TFAutoModelForTableQuestionAnswering.from_pretrained(_A      )
																__lowerCAmelCase       ,							__lowerCAmelCase           =	TFAutoModelForTableQuestionAnswering.from_pretrained(
																    _A		,      output_loading_info=_A      )
																self.assertIsNotNone(_A      )
																self.assertIsInstance(_A		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFAutoModelWithLMHead.from_pretrained(_A      )
											self.assertIsInstance(_A		,      _A      )
											self.assertEqual(model.num_parameters()		,      1_4_4_1_0      )
											self.assertEqual(model.num_parameters(only_trainable=_A      )		,      1_4_4_1_0      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFAutoModelWithLMHead.from_pretrained(_A      )
											self.assertIsInstance(_A		,      _A      )
											self.assertEqual(model.num_parameters()		,      1_4_4_1_0      )
											self.assertEqual(model.num_parameters(only_trainable=_A      )		,      1_4_4_1_0      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"      )
											self.assertIsInstance(_A		,      _A      )

											__lowerCAmelCase           =	copy.deepcopy(model.config      )
											__lowerCAmelCase           =	["FunnelBaseModel"]
											__lowerCAmelCase           =	TFAutoModel.from_config(_A      )
											self.assertIsInstance(_A		,      _A      )

											with tempfile.TemporaryDirectory() as tmp_dir:
																model.save_pretrained(_A      )
																__lowerCAmelCase           =	TFAutoModel.from_pretrained(_A      )
																self.assertIsInstance(_A		,      _A      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											try:
																AutoConfig.register("new-model"		,      _A      )

																__lowerCAmelCase           =	[
																    TFAutoModel,
																    TFAutoModelForCausalLM,
																    TFAutoModelForMaskedLM,
																    TFAutoModelForPreTraining,
																    TFAutoModelForQuestionAnswering,
																    TFAutoModelForSequenceClassification,
																    TFAutoModelForTokenClassification,
																]

																for auto_class in auto_classes:
																					with self.subTest(auto_class.__name__      ):
																										# Wrong config class will raise an error
																										with self.assertRaises(_A      ):
																															auto_class.register(_A		,      _A      )
																										auto_class.register(_A		,      _A      )
																										# Trying to register something existing in the Transformers library will raise an error
																										with self.assertRaises(_A      ):
																															auto_class.register(_A		,      _A      )

																										# Now that the config is registered, it can be used as any other config with the auto-API
																										__lowerCAmelCase           =	BertModelTester(self      ).get_config()
																										__lowerCAmelCase           =	NewModelConfig(**tiny_config.to_dict()      )
																										__lowerCAmelCase           =	auto_class.from_config(_A      )
																										self.assertIsInstance(_A		,      _A      )

																										with tempfile.TemporaryDirectory() as tmp_dir:
																															model.save_pretrained(_A      )
																															__lowerCAmelCase           =	auto_class.from_pretrained(_A      )
																															self.assertIsInstance(_A		,      _A      )

											finally:
																if "new-model" in CONFIG_MAPPING._extra_content:
																					del CONFIG_MAPPING._extra_content["new-model"]
																for mapping in (
																    TF_MODEL_MAPPING,
																    TF_MODEL_FOR_PRETRAINING_MAPPING,
																    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
																    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
																    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
																    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
																    TF_MODEL_FOR_MASKED_LM_MAPPING,
																):
																					if NewModelConfig in mapping._extra_content:
																										del mapping._extra_content[NewModelConfig]






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											with self.assertRaisesRegex(
											    _A		,      "bert-base is not a local folder and is not a valid model identifier"      ):
																__lowerCAmelCase           =	TFAutoModel.from_pretrained("bert-base"      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											with self.assertRaisesRegex(
											    _A		,      R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"      ):
																__lowerCAmelCase           =	TFAutoModel.from_pretrained(_A		,      revision="aaaaaa"      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											with self.assertRaisesRegex(
											    _A		,      "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"		,      ):
																__lowerCAmelCase           =	TFAutoModel.from_pretrained("hf-internal-testing/config-no-model"      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											with self.assertRaisesRegex(_A		,      "Use `from_pt=True` to load this model"      ):
																__lowerCAmelCase           =	TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"      )
											with RequestCounter() as counter:
																__lowerCAmelCase           =	TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"      )
																self.assertEqual(counter.get_request_count		,      0      )
																self.assertEqual(counter.head_request_count		,      1      )
																self.assertEqual(counter.other_request_count		,      0      )

											# With a sharded checkpoint
											__lowerCAmelCase           =	TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"      )
											with RequestCounter() as counter:
																__lowerCAmelCase           =	TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"      )
																self.assertEqual(counter.get_request_count		,      0      )
																self.assertEqual(counter.head_request_count		,      1      )
																self.assertEqual(counter.other_request_count		,      0      )




