


'''simple docstring'''
from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
      import torch

lowerCAmelCase  =				logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
      from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):

      def 				lowerCamelCase_   (   cls				:					Tuple		)				->  Dict:







           """simple docstring"""
           return cls

def 				lowerCamelCase_   (   __UpperCamelCase				:					Union[Tuple, List]       ,						__UpperCamelCase				:					Optional[Union[List["torch.Generator"], "torch.Generator"]] = None       ,						__UpperCamelCase				:					Optional["torch.device"] = None       ,						__UpperCamelCase				:					Optional["torch.dtype"] = None       ,						__UpperCamelCase				:					Optional["torch.layout"] = None       ,						)				->  Tuple:







     """simple docstring"""
     _A				=       device
     _A				=       shape[0]

     _A				=       layout or torch.strided
     _A				=       device or torch.device('cpu'		)

     if generator is not None:
          _A				=       generator.device.type if not isinstance(__UpperCamelCase       ,						__UpperCamelCase		) else generator[0].device.type
          if gen_device_type != device.type and gen_device_type == "cpu":
               _A				=       'cpu'
               if device != "mps":
                    logger.info(
                        F'The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'
                        F' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'
                        F' slighly speed up this function by passing a generator that was created on the {device} device.'		)
          elif gen_device_type != device.type and gen_device_type == "cuda":
               raise ValueError(F'Cannot generate a {device} tensor from a generator of type {gen_device_type}.'		)

     if isinstance(__UpperCamelCase       ,						__UpperCamelCase		):
          _A				=       (1,) + shape[1:]
          _A				=       [
              torch.randn(__UpperCamelCase       ,						generator=generator[i]       ,						device=__UpperCamelCase       ,						dtype=__UpperCamelCase       ,						layout=__UpperCamelCase		)
              for i in range(__UpperCamelCase		)
          ]
          _A				=       torch.cat(__UpperCamelCase       ,						dim=0		).to(__UpperCamelCase		)
     else:
          _A				=       torch.randn(__UpperCamelCase       ,						generator=__UpperCamelCase       ,						device=__UpperCamelCase       ,						dtype=__UpperCamelCase       ,						layout=__UpperCamelCase		).to(__UpperCamelCase		)

     return latents

def 				lowerCamelCase_   (   __UpperCamelCase				:					Any		)				->  Optional[int]:







     """simple docstring"""
     if is_torch_version('<'       ,						'2.0.0'		) or not hasattr(__UpperCamelCase       ,						'_dynamo'		):
          return False
     return isinstance(__UpperCamelCase       ,						torch._dynamo.eval_frame.OptimizedModule		)









'''simple docstring'''
import argparse

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


logging.set_verbosity_info()

def 				lowerCamelCase_   (   __UpperCamelCase				:					Tuple       ,						__UpperCamelCase				:					Any       ,						__UpperCamelCase				:					Dict       ,						__UpperCamelCase				:					List[str]       ,						__UpperCamelCase				:					str		)				->  List[str]:







     """simple docstring"""
     # Initialise PyTorch model
     _A				=       BertConfig.from_pretrained(
         'bert-large-cased'       ,						vocab_size=__UpperCamelCase       ,						max_position_embeddings=5_1_2       ,						is_decoder=__UpperCamelCase       ,						add_cross_attention=__UpperCamelCase       ,						)
     _A				=       bert_config.to_dict()
     del bert_config_dict["type_vocab_size"]
     _A				=       BertGenerationConfig(**__UpperCamelCase		)
     if is_encoder:
          _A				=       BertGenerationEncoder(__UpperCamelCase		)
     else:
          _A				=       BertGenerationDecoder(__UpperCamelCase		)
     print(F'Building PyTorch model from configuration: {config}'		)

     # Load weights from tf checkpoint
     load_tf_weights_in_bert_generation(
         __UpperCamelCase       ,						__UpperCamelCase       ,						model_class='bert'       ,						is_encoder_named_decoder=__UpperCamelCase       ,						is_encoder=__UpperCamelCase       ,						)

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


if __name__ == "__main__":
    lowerCAmelCase  =				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=50_358, type=int, help="""Vocab size of model""")
    lowerCAmelCase  =				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,
    )





