


import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel



def UpperCamelCase_				(				__a			,				__a			,				__a      )	->   Optional[Any]:
      a__	:  Union[str, Any]     =   ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

      a__	:  Union[str, Any]     =   (
          ("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      )

      a__	:  Any     =   model.state_dict()

      def to_tf_var_name(__a      ):
            for patt, repl in iter(__a      ):
                  a__	:  Tuple     =   name.replace(__a			,				__a      )
            return f'''bert/{name}'''

      def create_tf_var(__a			,				__a			,				__a      ):
            a__	:  Tuple     =   tf.dtypes.as_dtype(tensor.dtype      )
            a__	:  Dict     =   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:
                  a__	:  int     =   to_tf_var_name(__a      )
                  a__	:  Union[str, Any]     =   state_dict[var_name].numpy()
                  if any(x in var_name for x in tensors_to_transpose      ):
                        a__	:  int     =   torch_tensor.T
                  a__	:  Optional[Any]     =   create_tf_var(tensor=__a			,				name=__a			,				session=__a      )
                  tf.keras.backend.set_value(__a			,				__a      )
                  a__	:  int     =   session.run(__a      )
                  print(f'''Successfully created {tf_name}: {np.allclose(__a			,				__a      )}'''      )

            a__	:  Any     =   tf.train.Saver(tf.trainable_variables()      )
            saver.save(__a			,				os.path.join(__a			,				model_name.replace("-"			,				"_"      ) + ".ckpt"      )      )



def UpperCamelCase_				(				__a=None      )	->   int:
      a__	:  Dict     =   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"      )
      a__	:  Optional[Any]     =   parser.parse_args(__a      )

      a__	:  Tuple     =   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()









'''simple docstring'''
from typing import TYPE_CHECKING

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


UpperCamelCase__			=      {
    'configuration_funnel': ['FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FunnelConfig'],
    'convert_funnel_original_tf_checkpoint_to_pytorch': [],
    'tokenization_funnel': ['FunnelTokenizer'],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      ['FunnelTokenizerFast']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST',
     'FunnelBaseModel',
     'FunnelForMaskedLM',
     'FunnelForMultipleChoice',
     'FunnelForPreTraining',
     'FunnelForQuestionAnswering',
     'FunnelForSequenceClassification',
     'FunnelForTokenClassification',
     'FunnelModel',
     'FunnelPreTrainedModel',
     'load_tf_weights_in_funnel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFFunnelBaseModel',
     'TFFunnelForMaskedLM',
     'TFFunnelForMultipleChoice',
     'TFFunnelForPreTraining',
     'TFFunnelForQuestionAnswering',
     'TFFunnelForSequenceClassification',
     'TFFunnelForTokenClassification',
     'TFFunnelModel',
     'TFFunnelPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
 from .tokenization_funnel import FunnelTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_funnel_fast import FunnelTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_funnel import (
      FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
      FunnelBaseModel,
      FunnelForMaskedLM,
      FunnelForMultipleChoice,
      FunnelForPreTraining,
      FunnelForQuestionAnswering,
      FunnelForSequenceClassification,
      FunnelForTokenClassification,
      FunnelModel,
      FunnelPreTrainedModel,
      load_tf_weights_in_funnel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_funnel import (
      TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFFunnelBaseModel,
      TFFunnelForMaskedLM,
      TFFunnelForMultipleChoice,
      TFFunnelForPreTraining,
      TFFunnelForQuestionAnswering,
      TFFunnelForSequenceClassification,
      TFFunnelForTokenClassification,
      TFFunnelModel,
      TFFunnelPreTrainedModel,
  )

else:
 import sys

 UpperCamelCase__			=      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




