





"""simple docstring"""
import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


lowerCAmelCase:	int							      =logging.get_logger(__name__)

if is_tf_available():
   import tensorflow as tf
@dataclass
class lowerCamelCase__  (    __UpperCamelCase				):
   __UpperCAmelCase        =					"""tf"""
   __UpperCAmelCase        =					field(
       default=__UpperCamelCase  ,				metadata={"""help""": """Name of TPU"""}  ,				)

   __UpperCAmelCase        =					field(
       default=__UpperCamelCase  ,				metadata={"""help""": """Zone of TPU"""}  ,				)

   __UpperCAmelCase        =					field(
       default=__UpperCamelCase  ,				metadata={"""help""": """Name of Cloud TPU-enabled project"""}  ,				)

   __UpperCAmelCase        =					field(
       default=1.0  ,				metadata={"""help""": """Power for the Polynomial decay LR scheduler."""}  ,				)

   __UpperCAmelCase        =					field(default=__UpperCamelCase  ,				metadata={"""help""": """Whether to activate the XLA compilation or not"""}				)



   @cached_property
   def    _UpperCAmelCase	(       self	) ->    Tuple["tf.distribute.Strategy", int]:







      """simple docstring"""
      requires_backends(self      ,   ["""tf"""]	)
      logger.info("""Tensorflow: setting up strategy"""	)

      lowercase    :			List[str]  =       tf.config.list_physical_devices("""GPU"""	)

      # Set to float16 at first
      if self.fpaa:
         tf.keras.mixed_precision.set_global_policy("""mixed_float16"""	)

      if self.no_cuda:
         lowercase    :			str  =       tf.distribute.OneDeviceStrategy(device="""/cpu:0"""	)
      else:
         try:
            if self.tpu_name:
               lowercase    :			Union[str, Any]  =       tf.distribute.cluster_resolver.TPUClusterResolver(
                   self.tpu_name      ,   zone=self.tpu_zone      ,   project=self.gcp_project	)
            else:
               lowercase    :			Optional[Any]  =       tf.distribute.cluster_resolver.TPUClusterResolver()
         except ValueError:
            if self.tpu_name:
               raise RuntimeError(f'''Couldn\'t connect to TPU {self.tpu_name}!'''	)
            else:
               lowercase    :			Tuple  =       None

         if tpu:
            # Set to bfloat16 in case of TPU
            if self.fpaa:
               tf.keras.mixed_precision.set_global_policy("""mixed_bfloat16"""	)

            tf.config.experimental_connect_to_cluster(snake_case	)
            tf.tpu.experimental.initialize_tpu_system(snake_case	)

            lowercase    :			Optional[int]  =       tf.distribute.TPUStrategy(snake_case	)

         elif len(snake_case	) == 0:
            lowercase    :			Optional[int]  =       tf.distribute.OneDeviceStrategy(device="""/cpu:0"""	)
         elif len(snake_case	) == 1:
            lowercase    :			str  =       tf.distribute.OneDeviceStrategy(device="""/gpu:0"""	)
         elif len(snake_case	) > 1:
            # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
            lowercase    :			Tuple  =       tf.distribute.MirroredStrategy()
         else:
            raise ValueError("""Cannot find the proper strategy, please check your environment properties."""	)

      return strategy



   @property
   def    _UpperCAmelCase	(       self	) ->    "tf.distribute.Strategy":







      """simple docstring"""
      requires_backends(self      ,   ["""tf"""]	)
      return self._setup_strategy



   @property
   def    _UpperCAmelCase	(       self	) ->    int:







      """simple docstring"""
      requires_backends(self      ,   ["""tf"""]	)
      return self._setup_strategy.num_replicas_in_sync



   @property
   def    _UpperCAmelCase	(       self	) ->    int:







      """simple docstring"""
      return False  # TF Logging is handled by Keras not the Trainer



   @property
   def    _UpperCAmelCase	(       self	) ->    int:







      """simple docstring"""
      if self.per_gpu_train_batch_size:
         logger.warning(
             """Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future """
             """version. Using `--per_device_train_batch_size` is preferred."""	)
      lowercase    :			List[Any]  =       self.per_gpu_train_batch_size or self.per_device_train_batch_size
      return per_device_batch_size * self.n_replicas



   @property
   def    _UpperCAmelCase	(       self	) ->    int:







      """simple docstring"""
      if self.per_gpu_eval_batch_size:
         logger.warning(
             """Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future """
             """version. Using `--per_device_eval_batch_size` is preferred."""	)
      lowercase    :			int  =       self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
      return per_device_batch_size * self.n_replicas





   @property
   def    _UpperCAmelCase	(       self	) ->    int:







      """simple docstring"""
      requires_backends(self      ,   ["""tf"""]	)
      warnings.warn(
          """The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead."""      ,   snake_case      ,   )
      return self._setup_strategy.num_replicas_in_sync









"""simple docstring"""
import argparse

from . import (
    BertConfig,
    BertGenerationConfig,
    BertGenerationDecoder,
    BertGenerationEncoder,
    load_tf_weights_in_bert_generation,
    logging,
)


logging.set_verbosity_info()




def 			__snake_case						(      __A ,__A ,__A ,__A ,__A    )		->				Optional[int]:
   # Initialise PyTorch model
   lowercase    :			Optional[Any]  =       BertConfig.from_pretrained(
       """bert-large-cased""" ,vocab_size=__A ,max_position_embeddings=512 ,is_decoder=__A ,add_cross_attention=__A ,)
   lowercase    :			List[str]  =       bert_config.to_dict()
   del bert_config_dict["type_vocab_size"]
   lowercase    :			Tuple  =       BertGenerationConfig(**__A    )
   if is_encoder:
      lowercase    :			Dict  =       BertGenerationEncoder(__A    )
   else:
      lowercase    :			Dict  =       BertGenerationDecoder(__A    )
   print(F'''Building PyTorch model from configuration: {config}'''    )

   # Load weights from tf checkpoint
   load_tf_weights_in_bert_generation(
       __A ,__A ,model_class="""bert""" ,is_encoder_named_decoder=__A ,is_encoder=__A ,)

   # Save pytorch-model
   print(F'''Save PyTorch model and config to {pytorch_dump_path}'''    )
   model.save_pretrained(__A    )


if __name__ == "__main__":
   lowerCAmelCase:	Dict							      =argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       "--tf_hub_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
   )
   parser.add_argument(
       "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
   )
   parser.add_argument(
       "--is_encoder_named_decoder",
       action="store_true",
       help="If decoder has to be renamed to encoder in PyTorch model.",
   )
   parser.add_argument("--is_encoder", action="store_true", help="If model is an encoder.")
   parser.add_argument("--vocab_size", default=50358, type=int, help="Vocab size of model")
   lowerCAmelCase:	Optional[int]							      =parser.parse_args()
   convert_tf_checkpoint_to_pytorch(
       args.tf_hub_path,
       args.pytorch_dump_path,
       args.is_encoder_named_decoder,
       args.vocab_size,
       is_encoder=args.is_encoder,
   )


