

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



   @slow
   def 	__magic_name__   (							self							:					int						):
     # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     for model_name in ["bert-base-uncased"]:
       UpperCAmelCase	:							Tuple						=			AutoConfig.from_pretrained(__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							Optional[int]						=			TFAutoModel.from_pretrained(__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							Union[str, Any]						=			AutoModel.from_pretrained(__A,		from_tf=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)



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

       UpperCAmelCase	:							Any						=			TFAutoModelForPreTraining.from_pretrained(__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							int						=			AutoModelForPreTraining.from_pretrained(__A,		from_tf=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)



   @slow
   def 	__magic_name__   (							self							:					List[Any]						):
     for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							Any						=			AutoConfig.from_pretrained(__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							Any						=			TFAutoModelForCausalLM.from_pretrained(__A,		from_pt=__A						)
       UpperCAmelCase			,						UpperCAmelCase	:							Tuple						=			TFAutoModelForCausalLM.from_pretrained(
           __A,		output_loading_info=__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							Tuple						=			AutoModelForCausalLM.from_pretrained(__A,		from_tf=__A						)
       UpperCAmelCase			,						UpperCAmelCase	:							Optional[Any]						=			AutoModelForCausalLM.from_pretrained(
           __A,		output_loading_info=__A,		from_tf=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)



   @slow
   def 	__magic_name__   (							self							:					str						):
     for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							str						=			AutoConfig.from_pretrained(__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							str						=			TFAutoModelWithLMHead.from_pretrained(__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							Optional[int]						=			AutoModelWithLMHead.from_pretrained(__A,		from_tf=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)



   @slow
   def 	__magic_name__   (							self							:					str						):
     for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							Dict						=			AutoConfig.from_pretrained(__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							int						=			TFAutoModelForMaskedLM.from_pretrained(__A,		from_pt=__A						)
       UpperCAmelCase			,						UpperCAmelCase	:							str						=			TFAutoModelForMaskedLM.from_pretrained(
           __A,		output_loading_info=__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							List[str]						=			AutoModelForMaskedLM.from_pretrained(__A,		from_tf=__A						)
       UpperCAmelCase			,						UpperCAmelCase	:							Optional[int]						=			AutoModelForMaskedLM.from_pretrained(
           __A,		output_loading_info=__A,		from_tf=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)



   @slow
   def 	__magic_name__   (							self							:					Dict						):
     for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							str						=			AutoConfig.from_pretrained(__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							Dict						=			TFAutoModelForSeqaSeqLM.from_pretrained(__A,		from_pt=__A						)
       UpperCAmelCase			,						UpperCAmelCase	:							Any						=			TFAutoModelForSeqaSeqLM.from_pretrained(
           __A,		output_loading_info=__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							Optional[Any]						=			AutoModelForSeqaSeqLM.from_pretrained(__A,		from_tf=__A						)
       UpperCAmelCase			,						UpperCAmelCase	:							List[str]						=			AutoModelForSeqaSeqLM.from_pretrained(
           __A,		output_loading_info=__A,		from_tf=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)



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

       UpperCAmelCase	:							Tuple						=			TFAutoModelForSequenceClassification.from_pretrained(__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

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



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

       UpperCAmelCase	:							Tuple						=			TFAutoModelForQuestionAnswering.from_pretrained(__A,		from_pt=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)

       UpperCAmelCase	:							str						=			AutoModelForQuestionAnswering.from_pretrained(__A,		from_tf=__A						)
       self.assertIsNotNone(__A						)
       self.assertIsInstance(__A,		__A						)



   def 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							List[Any]						=			TFAutoModelWithLMHead.from_pretrained(__A,		from_pt=__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						)

     UpperCAmelCase	:							Optional[Any]						=			AutoModelWithLMHead.from_pretrained(__A,		from_tf=__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 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							int						=			TFAutoModelWithLMHead.from_pretrained(__A,		from_pt=__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						)

     UpperCAmelCase	:							List[str]						=			AutoModelWithLMHead.from_pretrained(__A,		from_tf=__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						)








import logging
import os

from .state import PartialState





class  __UpperCAmelCase					(	logging.LoggerAdapter						):



   @staticmethod
   def 	__magic_name__   (							__A							:					str						):
     UpperCAmelCase	:							Dict						=			PartialState()
     return not main_process_only or (main_process_only and state.is_main_process)





   def 	__magic_name__   (							self							:					Union[str, Any],		__A							:					Union[str, Any],		__A							:					Union[str, Any],		*__A							:					Optional[int],		**__A							:					Tuple						):
     if PartialState._shared_state == {}:
       raise RuntimeError(
           '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.'''						)
     UpperCAmelCase	:							List[str]						=			kwargs.pop('''main_process_only''',		__A						)
     UpperCAmelCase	:							int						=			kwargs.pop('''in_order''',		__A						)

     if self.isEnabledFor(__A						):
       if self._should_log(__A						):
         UpperCAmelCase			,						UpperCAmelCase	:							Dict						=			self.process(__A,		__A						)
         self.logger.log(__A,		__A,		*__A,		**__A						)

       elif in_order:
         UpperCAmelCase	:							Dict						=			PartialState()
         for i in range(state.num_processes						):
           if i == state.process_index:
             UpperCAmelCase			,						UpperCAmelCase	:							Optional[int]						=			self.process(__A,		__A						)
             self.logger.log(__A,		__A,		*__A,		**__A						)
           state.wait_for_everyone()






def       a__					(		UpperCAmelCase							:      str    ,			UpperCAmelCase							:      str = None      )  ->						Dict:
  if log_level is None:
    UpperCAmelCase	:							Union[str, Any]						=			os.environ.get('''ACCELERATE_LOG_LEVEL'''    ,			UpperCAmelCase      )
  UpperCAmelCase	:							Tuple						=			logging.getLogger(UpperCAmelCase      )
  if log_level is not None:
    logger.setLevel(log_level.upper()      )
    logger.root.setLevel(log_level.upper()      )
  return MultiProcessAdapter(UpperCAmelCase    ,			{}      )





