





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     SCREAMING_SNAKE_CASE       (unittest.TestCase				):


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Any	)->       Any:





     """simple docstring"""







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

       lowercase__           =  TFAutoModel.from_pretrained(a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModel.from_pretrained(a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    int	)->       Dict:





     """simple docstring"""







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

       lowercase__           =  TFAutoModelForPreTraining.from_pretrained(a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModelForPreTraining.from_pretrained(a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


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





     """simple docstring"""







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

       lowercase__           =  TFAutoModelForCausalLM.from_pretrained(a      ,					from_pt=a	)
       lowercase__      ,   lowercase__           =  TFAutoModelForCausalLM.from_pretrained(
           a      ,					output_loading_info=a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModelForCausalLM.from_pretrained(a      ,					from_tf=a	)
       lowercase__      ,   lowercase__           =  AutoModelForCausalLM.from_pretrained(
           a      ,					output_loading_info=a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Dict	)->       Optional[Any]:





     """simple docstring"""







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

       lowercase__           =  TFAutoModelWithLMHead.from_pretrained(a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModelWithLMHead.from_pretrained(a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


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





     """simple docstring"""







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

       lowercase__           =  TFAutoModelForMaskedLM.from_pretrained(a      ,					from_pt=a	)
       lowercase__      ,   lowercase__           =  TFAutoModelForMaskedLM.from_pretrained(
           a      ,					output_loading_info=a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModelForMaskedLM.from_pretrained(a      ,					from_tf=a	)
       lowercase__      ,   lowercase__           =  AutoModelForMaskedLM.from_pretrained(
           a      ,					output_loading_info=a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[Any]	)->       Union[str, Any]:





     """simple docstring"""







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

       lowercase__           =  TFAutoModelForSeqaSeqLM.from_pretrained(a      ,					from_pt=a	)
       lowercase__      ,   lowercase__           =  TFAutoModelForSeqaSeqLM.from_pretrained(
           a      ,					output_loading_info=a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModelForSeqaSeqLM.from_pretrained(a      ,					from_tf=a	)
       lowercase__      ,   lowercase__           =  AutoModelForSeqaSeqLM.from_pretrained(
           a      ,					output_loading_info=a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


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





     """simple docstring"""







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

       lowercase__           =  TFAutoModelForSequenceClassification.from_pretrained(a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModelForSequenceClassification.from_pretrained(a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Union[str, Any]	)->       str:





     """simple docstring"""







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

       lowercase__           =  TFAutoModelForQuestionAnswering.from_pretrained(a      ,					from_pt=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)

       lowercase__           =  AutoModelForQuestionAnswering.from_pretrained(a      ,					from_tf=a	)
       self.assertIsNotNone(a	)
       self.assertIsInstance(a      ,					a	)


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





     """simple docstring"""







     lowercase__           =  TFAutoModelWithLMHead.from_pretrained(a      ,					from_pt=a	)
     self.assertIsInstance(a      ,					a	)
     self.assertEqual(model.num_parameters()      ,					14_410	)
     self.assertEqual(model.num_parameters(only_trainable=a	)      ,					14_410	)

     lowercase__           =  AutoModelWithLMHead.from_pretrained(a      ,					from_tf=a	)
     self.assertIsInstance(a      ,					a	)
     self.assertEqual(model.num_parameters()      ,					14_410	)
     self.assertEqual(model.num_parameters(only_trainable=a	)      ,					14_410	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Tuple	)->       int:





     """simple docstring"""







     lowercase__           =  TFAutoModelWithLMHead.from_pretrained(a      ,					from_pt=a	)
     self.assertIsInstance(a      ,					a	)
     self.assertEqual(model.num_parameters()      ,					14_410	)
     self.assertEqual(model.num_parameters(only_trainable=a	)      ,					14_410	)

     lowercase__           =  AutoModelWithLMHead.from_pretrained(a      ,					from_tf=a	)
     self.assertIsInstance(a      ,					a	)
     self.assertEqual(model.num_parameters()      ,					14_410	)
     self.assertEqual(model.num_parameters(only_trainable=a	)      ,					14_410	)









import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
      from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       List[str]:
  if is_torch_version('<'		,						'2.0.0'		) or not hasattr(_SCREAMING_SNAKE_CASE		,						'_dynamo'		):
    return False
  return isinstance(_SCREAMING_SNAKE_CASE		,						torch._dynamo.eval_frame.OptimizedModule		)




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE = True		)    ->       Dict:
  lowercase__           =  (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

  lowercase__           =  is_compiled_module(_SCREAMING_SNAKE_CASE		)
  if is_compiled:
    lowercase__           =  model
    lowercase__           =  model._orig_mod

  if is_deepspeed_available():
    options += (DeepSpeedEngine,)

  while isinstance(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		):
    lowercase__           =  model.module

  if not keep_fpaa_wrapper:
    lowercase__           =  getattr(_SCREAMING_SNAKE_CASE		,						'forward'		)
    lowercase__           =  model.__dict__.pop('_original_forward'		,						_SCREAMING_SNAKE_CASE		)
    if original_forward is not None:
      while hasattr(_SCREAMING_SNAKE_CASE		,						'__wrapped__'		):
        lowercase__           =  forward.__wrapped__
        if forward == original_forward:
          break
      lowercase__           =  forward
    if getattr(_SCREAMING_SNAKE_CASE		,						'_converted_to_transformer_engine'		,						_SCREAMING_SNAKE_CASE		):
      convert_model(_SCREAMING_SNAKE_CASE		,						to_transformer_engine=_SCREAMING_SNAKE_CASE		)

  if is_compiled:
    lowercase__           =  model
    lowercase__           =  compiled_model

  return model




def       __UpperCamelCase  ()    ->       Tuple:
  PartialState().wait_for_everyone()




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)    ->       List[Any]:
  if PartialState().distributed_type == DistributedType.TPU:
    xm.save(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)
  elif PartialState().local_process_index == 0:
    torch.save(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)




@contextmanager
def       __UpperCamelCase  (**_SCREAMING_SNAKE_CASE		)    ->       Optional[Any]:
  for key, value in kwargs.items():
    lowercase__           =  str(_SCREAMING_SNAKE_CASE		)

  yield

  for key in kwargs:
    if key.upper() in os.environ:
      del os.environ[key.upper()]




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       List[str]:
  if not hasattr(_SCREAMING_SNAKE_CASE		,						'__qualname__'		) and not hasattr(_SCREAMING_SNAKE_CASE		,						'__name__'		):
    lowercase__           =  getattr(_SCREAMING_SNAKE_CASE		,						'__class__'		,						_SCREAMING_SNAKE_CASE		)
  if hasattr(_SCREAMING_SNAKE_CASE		,						'__qualname__'		):
    return obj.__qualname__
  if hasattr(_SCREAMING_SNAKE_CASE		,						'__name__'		):
    return obj.__name__
  return str(_SCREAMING_SNAKE_CASE		)




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)    ->       Dict:
  for key, value in source.items():
    if isinstance(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		):
      lowercase__           =  destination.setdefault(_SCREAMING_SNAKE_CASE		,						{}		)
      merge_dicts(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)
    else:
      lowercase__           =  value

  return destination




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE = None		)    ->       bool:
  if port is None:
    lowercase__           =  29500
  with socket.socket(socket.AF_INET		,						socket.SOCK_STREAM		) as s:
    return s.connect_ex(('localhost', port)		) == 0


