






"""simple docstring"""
import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel

def 							snake_case (  A__      ,A__      ,A__		):
			UpperCAmelCase_	:					List[Any]     	=   ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

			UpperCAmelCase_	:					int     	=   (
			    ("layer.", "layer_"),
			    ("word_embeddings.weight", "word_embeddings"),
			    ("position_embeddings.weight", "position_embeddings"),
			    ("token_type_embeddings.weight", "token_type_embeddings"),
			    (".", "/"),
			    ("LayerNorm/weight", "LayerNorm/gamma"),
			    ("LayerNorm/bias", "LayerNorm/beta"),
			    ("weight", "kernel"),
			)

			if not os.path.isdir(A__		):
						os.makedirs(A__		)

			UpperCAmelCase_	:					Optional[int]     	=   model.state_dict()

			def to_tf_var_name(A__		):
						for patt, repl in iter(A__		):
									UpperCAmelCase_	:					Optional[int]     	=   name.replace(A__      ,A__		)
						return F"""bert/{name}"""

			def create_tf_var(A__      ,A__      ,A__		):
						UpperCAmelCase_	:					int     	=   tf.dtypes.as_dtype(tensor.dtype		)
						UpperCAmelCase_	:					List[Any]     	=   tf.get_variable(dtype=A__      ,shape=tensor.shape      ,name=A__      ,initializer=tf.zeros_initializer()		)
						session.run(tf.variables_initializer([tf_var]		)		)
						session.run(A__		)
						return tf_var

			tf.reset_default_graph()
			with tf.Session() as session:
						for var_name in state_dict:
									UpperCAmelCase_	:					Tuple     	=   to_tf_var_name(A__		)
									UpperCAmelCase_	:					Optional[int]     	=   state_dict[var_name].numpy()
									if any(x in var_name for x in tensors_to_transpose		):
												UpperCAmelCase_	:					List[str]     	=   torch_tensor.T
									UpperCAmelCase_	:					List[str]     	=   create_tf_var(tensor=A__      ,name=A__      ,session=A__		)
									tf.keras.backend.set_value(A__      ,A__		)
									UpperCAmelCase_	:					Optional[Any]     	=   session.run(A__		)
									print(F"""Successfully created {tf_name}: {np.allclose(A__      ,A__		)}"""		)

						UpperCAmelCase_	:					Tuple     	=   tf.train.Saver(tf.trainable_variables()		)
						saver.save(A__      ,os.path.join(A__      ,model_name.replace("-"      ,"_"		) + ".ckpt"		)		)

def 							snake_case (  A__=None		):
			UpperCAmelCase_	:					Optional[Any]     	=   argparse.ArgumentParser()
			parser.add_argument("--model_name"      ,type=A__      ,required=A__      ,help="model name e.g. bert-base-uncased"		)
			parser.add_argument(
			    "--cache_dir"      ,type=A__      ,default=A__      ,required=A__      ,help="Directory containing pytorch model"		)
			parser.add_argument("--pytorch_model_path"      ,type=A__      ,required=A__      ,help="/path/to/<pytorch-model-name>.bin"		)
			parser.add_argument("--tf_cache_dir"      ,type=A__      ,required=A__      ,help="Directory in which to save tensorflow model"		)
			UpperCAmelCase_	:					str     	=   parser.parse_args(A__		)

			UpperCAmelCase_	:					Optional[Any]     	=   BertModel.from_pretrained(
			    pretrained_model_name_or_path=args.model_name      ,state_dict=torch.load(args.pytorch_model_path		)      ,cache_dir=args.cache_dir      ,)

			convert_pytorch_checkpoint_to_tf(model=A__      ,ckpt_dir=args.tf_cache_dir      ,model_name=args.model_name		)


if __name__ == "__main__":
			main()









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_lowerCamelCase      :						Any							=  {"""configuration_xlnet""": ["""XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLNetConfig"""]}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCamelCase      :						int							=  ["""XLNetTokenizer"""]

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCamelCase      :						int							=  ["""XLNetTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCamelCase      :						Union[str, Any]							=  [
      """XLNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """XLNetForMultipleChoice""",
      """XLNetForQuestionAnswering""",
      """XLNetForQuestionAnsweringSimple""",
      """XLNetForSequenceClassification""",
      """XLNetForTokenClassification""",
      """XLNetLMHeadModel""",
      """XLNetModel""",
      """XLNetPreTrainedModel""",
      """load_tf_weights_in_xlnet""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCamelCase      :						Dict							=  [
      """TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFXLNetForMultipleChoice""",
      """TFXLNetForQuestionAnsweringSimple""",
      """TFXLNetForSequenceClassification""",
      """TFXLNetForTokenClassification""",
      """TFXLNetLMHeadModel""",
      """TFXLNetMainLayer""",
      """TFXLNetModel""",
      """TFXLNetPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet import XLNetTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet_fast import XLNetTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlnet import (
        XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLNetForMultipleChoice,
        XLNetForQuestionAnswering,
        XLNetForQuestionAnsweringSimple,
        XLNetForSequenceClassification,
        XLNetForTokenClassification,
        XLNetLMHeadModel,
        XLNetModel,
        XLNetPreTrainedModel,
        load_tf_weights_in_xlnet,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlnet import (
        TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLNetForMultipleChoice,
        TFXLNetForQuestionAnsweringSimple,
        TFXLNetForSequenceClassification,
        TFXLNetForTokenClassification,
        TFXLNetLMHeadModel,
        TFXLNetMainLayer,
        TFXLNetModel,
        TFXLNetPreTrainedModel,
    )

else:
  import sys

  _lowerCamelCase      :						int							=  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


