


import argparse
import json

from tqdm import tqdm



def 							UpperCamelCase_( )       ->		str:
    _lowercase			:				List[Any] 						=						argparse.ArgumentParser()

    # Required parameters
    parser.add_argument(
        '--src_path'				,					type=lowerCamelCase_				,					default='biencoder-nq-dev.json'				,					help='Path to raw DPR training data'				,					)
    parser.add_argument(
        '--evaluation_set'				,					type=lowerCamelCase_				,					help='where to store parsed evaluation_set file'				,					)
    parser.add_argument(
        '--gold_data_path'				,					type=lowerCamelCase_				,					help='where to store parsed gold_data_path file'				,					)
    _lowercase			:				str 						=						parser.parse_args()

    with open(args.src_path				,					'r'					) as src_file, open(args.evaluation_set				,					'w'					) as eval_file, open(
        args.gold_data_path				,					'w'					) as gold_file:
        _lowercase			:				int 						=						json.load(lowerCamelCase_					)
        for dpr_record in tqdm(lowerCamelCase_					):
            _lowercase			:				Optional[Any] 						=						dpr_record['question']
            _lowercase			:				int 						=						[context['title'] for context in dpr_record['positive_ctxs']]
            eval_file.write(question + '\n'					)
            gold_file.write('\t'.join(lowerCamelCase_					) + '\n'					)


if __name__ == "__main__":
 main()



'''simple docstring'''


import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel





def    lowerCAmelCase       (		UpperCamelCase__					: BertModel     ,       UpperCamelCase__					: str     ,       UpperCamelCase__					: str							):


	"""simple docstring"""
	__UpperCAmelCase      					=      ('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''')

	__UpperCAmelCase      					=      (
	    ('''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(UpperCamelCase__							):
		os.makedirs(UpperCamelCase__							)

	__UpperCAmelCase      					=      model.state_dict()

	def to_tf_var_name(UpperCamelCase__					: str							):
		for patt, repl in iter(UpperCamelCase__							):
			__UpperCAmelCase      					=      name.replace(UpperCamelCase__     ,       UpperCamelCase__							)
		return f"""bert/{name}"""

	def create_tf_var(UpperCamelCase__					: np.ndarray     ,       UpperCamelCase__					: str     ,       UpperCamelCase__					: tf.Session							):
		__UpperCAmelCase      					=      tf.dtypes.as_dtype(tensor.dtype							)
		__UpperCAmelCase      					=      tf.get_variable(dtype=UpperCamelCase__     ,       shape=tensor.shape     ,       name=UpperCamelCase__     ,       initializer=tf.zeros_initializer()							)
		session.run(tf.variables_initializer([tf_var]							)							)
		session.run(UpperCamelCase__							)
		return tf_var

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

		__UpperCAmelCase      					=      tf.train.Saver(tf.trainable_variables()							)
		saver.save(UpperCamelCase__     ,       os.path.join(UpperCamelCase__     ,       model_name.replace('''-'''     ,       '''_'''							) + '''.ckpt'''							)							)





def    lowerCAmelCase       (		UpperCamelCase__					: List[str]=None							):


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

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


if __name__ == "__main__":
					main()
