
import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel



def  UpperCamelCase					(							lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__	):
					'''simple docstring'''
					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	):
					'''simple docstring'''
					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 argparse
from collections import defaultdict

import yaml


lowercase__	:Optional[int]										=					"docs/source/en/_toctree.yml"



def  UpperCamelCase					(							lowerCAmelCase__	):
					'''simple docstring'''
					lowercase					=		defaultdict(lowerCAmelCase__	)
					for doc in model_doc:
										counts[doc["local"]] += 1
					lowercase					=		[key for key, value in counts.items() if value > 1]

					lowercase					=		[]
					for duplicate_key in duplicates:
										lowercase					=		list({doc['''title'''] for doc in model_doc if doc['''local'''] == duplicate_key}	)
										if len(lowerCAmelCase__	) > 1:
															raise ValueError(
															    f'{duplicate_key} is present several times in the documentation table of content at '
															    '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '''
															    '''others.'''	)
										# Only add this once
										new_doc.append({'''local''': duplicate_key, '''title''': titles[0]}	)

					# Add none duplicate-keys
					new_doc.extend([doc for doc in model_doc if counts[doc['''local''']] == 1]	)

					# Sort
					return sorted(lowerCAmelCase__				,    key=lambda lowerCAmelCase__	: s["title"].lower()	)



def  UpperCamelCase					(							lowerCAmelCase__=False	):
					'''simple docstring'''
					with open(lowerCAmelCase__				,    encoding='''utf-8'''	) as f:
										lowercase					=		yaml.safe_load(f.read()	)

					# Get to the API doc
					lowercase					=		0
					while content[api_idx]["title"] != "API":
										api_idx += 1
					lowercase					=		content[api_idx]['''sections''']

					# Then to the model doc
					lowercase					=		0
					while api_doc[model_idx]["title"] != "Models":
										model_idx += 1

					lowercase					=		api_doc[model_idx]['''sections''']

					lowercase					=		[(idx, section) for idx, section in enumerate(lowerCAmelCase__	) if '''sections''' in section]
					lowercase					=		False
					for idx, modality_doc in modalities_docs:
										lowercase					=		modality_doc['''sections''']
										lowercase					=		clean_model_doc_toc(lowerCAmelCase__	)

										if old_modality_doc != new_modality_doc:
															lowercase					=		True
															if overwrite:
																				lowercase					=		new_modality_doc

					if diff:
										if overwrite:
															lowercase					=		model_doc
															lowercase					=		api_doc
															with open(lowerCAmelCase__				,    '''w'''				,    encoding='''utf-8'''	) as f:
																				f.write(yaml.dump(lowerCAmelCase__				,    allow_unicode=lowerCAmelCase__	)	)
										else:
															raise ValueError(
															    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''	)


if __name__ == "__main__":
							lowercase__	:Any										=					argparse.ArgumentParser()
							parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
							lowercase__	:int										=					parser.parse_args()

							check_model_doc(args.fix_and_overwrite)




