





import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel


def       UpperCamelCase__    (		lowerCAmelCase__       ,lowerCAmelCase__       ,lowerCAmelCase__ ):
				lowercase							=     ("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

				lowercase							=     (
				    ("""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(__lowerCamelCase ):
								os.makedirs(__lowerCamelCase )

				lowercase							=     model.state_dict()

				def to_tf_var_name(lowerCAmelCase__ ):
								for patt, repl in iter(__lowerCamelCase ):
												lowercase							=     name.replace(__lowerCamelCase       ,__lowerCamelCase )
								return f"""bert/{name}"""

				def create_tf_var(lowerCAmelCase__       ,lowerCAmelCase__       ,lowerCAmelCase__ ):
								lowercase							=     tf.dtypes.as_dtype(tensor.dtype )
								lowercase							=     tf.get_variable(dtype=__lowerCamelCase       ,shape=tensor.shape       ,name=__lowerCamelCase       ,initializer=tf.zeros_initializer() )
								session.run(tf.variables_initializer([tf_var] ) )
								session.run(__lowerCamelCase )
								return tf_var

				tf.reset_default_graph()
				with tf.Session() as session:
								for var_name in state_dict:
												lowercase							=     to_tf_var_name(__lowerCamelCase )
												lowercase							=     state_dict[var_name].numpy()
												if any(x in var_name for x in tensors_to_transpose ):
																lowercase							=     torch_tensor.T
												lowercase							=     create_tf_var(tensor=__lowerCamelCase       ,name=__lowerCamelCase       ,session=__lowerCamelCase )
												tf.keras.backend.set_value(__lowerCamelCase       ,__lowerCamelCase )
												lowercase							=     session.run(__lowerCamelCase )
												print(f"""Successfully created {tf_name}: {np.allclose(__lowerCamelCase       ,__lowerCamelCase )}""" )

								lowercase							=     tf.train.Saver(tf.trainable_variables() )
								saver.save(__lowerCamelCase       ,os.path.join(__lowerCamelCase       ,model_name.replace("""-"""       ,"""_""" ) + """.ckpt""" ) )


def       UpperCamelCase__    (		lowerCAmelCase__=None ):
				lowercase							=     argparse.ArgumentParser()
				parser.add_argument("""--model_name"""       ,type=__lowerCamelCase       ,required=__lowerCamelCase       ,help="""model name e.g. bert-base-uncased""" )
				parser.add_argument(
				    """--cache_dir"""       ,type=__lowerCamelCase       ,default=__lowerCamelCase       ,required=__lowerCamelCase       ,help="""Directory containing pytorch model""" )
				parser.add_argument("""--pytorch_model_path"""       ,type=__lowerCamelCase       ,required=__lowerCamelCase       ,help="""/path/to/<pytorch-model-name>.bin""" )
				parser.add_argument("""--tf_cache_dir"""       ,type=__lowerCamelCase       ,required=__lowerCamelCase       ,help="""Directory in which to save tensorflow model""" )
				lowercase							=     parser.parse_args(__lowerCamelCase )

				lowercase							=     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=__lowerCamelCase       ,ckpt_dir=args.tf_cache_dir       ,model_name=args.model_name )


if __name__ == "__main__":
					main()







import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases






def        __lowerCAmelCase		(	__lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     Any						)	->    Dict:
					assert isinstance(__lowerCamelCase					, __lowerCamelCase						)
					assert dataset.num_rows == 4
					assert dataset.num_columns == 1
					assert dataset.column_names == ["text"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype






@pytest.mark.parametrize("""keep_in_memory"""					, [False, True]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     Tuple					, __lowerCamelCase						:     str					, __lowerCamelCase						:     Optional[Any]						)	->    List[Any]:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase					, keep_in_memory=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize(
    """features"""					, [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]					, )
def        __lowerCAmelCase		(	__lowerCamelCase						:     Tuple					, __lowerCamelCase						:     Any					, __lowerCamelCase						:     Tuple						)	->    List[Any]:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =features.copy() if features else default_expected_features
					__lowerCAmelCase						 =(
					    Features({feature: Value(__lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
					)
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, features=__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize("""split"""					, [None, NamedSplit("""train"""						), """train""", """test"""]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     Union[str, Any]					, __lowerCamelCase						:     List[str]					, __lowerCamelCase						:     Union[str, Any]						)	->    Any:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase					, split=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)
					assert dataset.split == split if split else "train"






@pytest.mark.parametrize("""path_type"""					, [str, list]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     int					, __lowerCamelCase						:     List[str]						)	->    Any:
					if issubclass(__lowerCamelCase					, __lowerCamelCase						):
										__lowerCAmelCase						 =text_path
					elif issubclass(__lowerCamelCase					, __lowerCamelCase						):
										__lowerCAmelCase						 =[text_path]
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)






def        __lowerCAmelCase		(	__lowerCamelCase						:     Tuple					, __lowerCamelCase						:     List[str]					, __lowerCamelCase						:     str=("train",)						)	->    Dict:
					assert isinstance(__lowerCamelCase					, __lowerCamelCase						)
					for split in splits:
										__lowerCAmelCase						 =dataset_dict[split]
										assert dataset.num_rows == 4
										assert dataset.num_columns == 1
										assert dataset.column_names == ["text"]
										for feature, expected_dtype in expected_features.items():
															assert dataset.features[feature].dtype == expected_dtype






@pytest.mark.parametrize("""keep_in_memory"""					, [False, True]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[int]					, __lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     List[Any]						)	->    Dict:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										__lowerCAmelCase						 =TextDatasetReader({"""train""": text_path}					, cache_dir=__lowerCamelCase					, keep_in_memory=__lowerCamelCase						).read()
					_check_text_datasetdict(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize(
    """features"""					, [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]					, )
def        __lowerCAmelCase		(	__lowerCamelCase						:     Any					, __lowerCamelCase						:     Optional[int]					, __lowerCamelCase						:     Union[str, Any]						)	->    Tuple:
					__lowerCAmelCase						 =tmp_path / """cache"""
					# CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =features.copy() if features else default_expected_features
					__lowerCAmelCase						 =(
					    Features({feature: Value(__lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
					)
					__lowerCAmelCase						 =TextDatasetReader({"""train""": text_path}					, features=__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_datasetdict(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize("""split"""					, [None, NamedSplit("""train"""						), """train""", """test"""]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     int					, __lowerCamelCase						:     List[str]					, __lowerCamelCase						:     Any						)	->    int:
					if split:
										__lowerCAmelCase						 ={split: text_path}
					else:
										__lowerCAmelCase						 ="""train"""
										__lowerCAmelCase						 ={"""train""": text_path, """test""": text_path}
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_datasetdict(__lowerCamelCase					, __lowerCamelCase					, splits=list(path.keys()						)						)
					assert all(dataset[split].split == split for split in path.keys()						)




