





import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel





def 	lowerCAmelCase_					(			__a      ,       __a      ,       __a						)	->						int:







  """simple docstring"""





  lowerCamelCase__: Tuple         =("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

  lowerCamelCase__: Optional[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						)

  lowerCamelCase__: str         =model.state_dict()

  def to_tf_var_name(__a						):
    for patt, repl in iter(__a						):
      lowerCamelCase__: Tuple         =name.replace(__a      ,       __a						)
    return F"""bert/{name}"""

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

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





def 	lowerCAmelCase_					(			__a=None						)	->						List[str]:







  """simple docstring"""





  lowerCamelCase__: Optional[int]         =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"						)
  lowerCamelCase__: Any         =parser.parse_args(__a						)

  lowerCamelCase__: int         =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 ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__			=    logging.get_logger(__name__)

UpperCAmelCase__			=    {
    """google/realm-cc-news-pretrained-embedder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-encoder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-scorer""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-openqa""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"""
    ),
    """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json""",
    """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json""",
    """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json""",
    """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json""",
    # See all REALM models at https://huggingface.co/models?filter=realm
}


class 							a					(		lowerCAmelCase_ ):
    _snake_case  :	List[Any]  				=				'realm'


    def __init__(       self						:	List[Any] ,					__lowerCAmelCase						:	List[str]=3_0522 ,					__lowerCAmelCase						:	Tuple=768 ,					__lowerCAmelCase						:	List[Any]=128 ,					__lowerCAmelCase						:	Tuple=12 ,					__lowerCAmelCase						:	List[str]=12 ,					__lowerCAmelCase						:	Union[str, Any]=8 ,					__lowerCAmelCase						:	List[str]=3072 ,					__lowerCAmelCase						:	Union[str, Any]="gelu_new" ,					__lowerCAmelCase						:	str=0.1 ,					__lowerCAmelCase						:	Tuple=0.1 ,					__lowerCAmelCase						:	List[str]=512 ,					__lowerCAmelCase						:	Dict=2 ,					__lowerCAmelCase						:	List[Any]=0.02 ,					__lowerCAmelCase						:	Optional[int]=1e-1_2 ,					__lowerCAmelCase						:	Any=256 ,					__lowerCAmelCase						:	Union[str, Any]=10 ,					__lowerCAmelCase						:	Dict=1e-3 ,					__lowerCAmelCase						:	str=5 ,					__lowerCAmelCase						:	str=320 ,					__lowerCAmelCase						:	Tuple=1335_3718 ,					__lowerCAmelCase						:	Optional[Any]=5000 ,					__lowerCAmelCase						:	int=1 ,					__lowerCAmelCase						:	Dict=0 ,					__lowerCAmelCase						:	Any=2 ,					**__lowerCAmelCase						:	Optional[int] ,					):
      super().__init__(pad_token_id=__lowerCAmelCase ,					bos_token_id=__lowerCAmelCase ,					eos_token_id=__lowerCAmelCase ,					**__lowerCAmelCase       )

      # Common config
      _UpperCAmelCase    			=   vocab_size
      _UpperCAmelCase    			=   max_position_embeddings
      _UpperCAmelCase    			=   hidden_size
      _UpperCAmelCase    			=   retriever_proj_size
      _UpperCAmelCase    			=   num_hidden_layers
      _UpperCAmelCase    			=   num_attention_heads
      _UpperCAmelCase    			=   num_candidates
      _UpperCAmelCase    			=   intermediate_size
      _UpperCAmelCase    			=   hidden_act
      _UpperCAmelCase    			=   hidden_dropout_prob
      _UpperCAmelCase    			=   attention_probs_dropout_prob
      _UpperCAmelCase    			=   initializer_range
      _UpperCAmelCase    			=   type_vocab_size
      _UpperCAmelCase    			=   layer_norm_eps

      # Reader config
      _UpperCAmelCase    			=   span_hidden_size
      _UpperCAmelCase    			=   max_span_width
      _UpperCAmelCase    			=   reader_layer_norm_eps
      _UpperCAmelCase    			=   reader_beam_size
      _UpperCAmelCase    			=   reader_seq_len

      # Retrieval config
      _UpperCAmelCase    			=   num_block_records
      _UpperCAmelCase    			=   searcher_beam_size
