
import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel




def 	UpperCamelCase__  (     SCREAMING_SNAKE_CASE_	:	BertModel		,   SCREAMING_SNAKE_CASE_	:	str		,   SCREAMING_SNAKE_CASE_	:	str       )  ->     Dict:
						_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(SCREAMING_SNAKE_CASE_       ):
												os.makedirs(SCREAMING_SNAKE_CASE_       )

						_lowercase					       =					model.state_dict()

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

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

						tf.reset_default_graph()
						with tf.Session() as session:
												for var_name in state_dict:
																		_lowercase					       =					to_tf_var_name(SCREAMING_SNAKE_CASE_       )
																		_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=SCREAMING_SNAKE_CASE_		,   name=SCREAMING_SNAKE_CASE_		,   session=SCREAMING_SNAKE_CASE_       )
																		tf.keras.backend.set_value(SCREAMING_SNAKE_CASE_		,   SCREAMING_SNAKE_CASE_       )
																		_lowercase					       =					session.run(SCREAMING_SNAKE_CASE_       )
																		print(f"""Successfully created {tf_name}: {np.allclose(SCREAMING_SNAKE_CASE_		,   SCREAMING_SNAKE_CASE_       )}"""       )

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




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

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


if __name__ == "__main__":
		main()

import datasets


A		: Optional[int]								=     '''\
@InProceedings{conneau2018xnli,
  author = "Conneau, Alexis
                 and Rinott, Ruty
                 and Lample, Guillaume
                 and Williams, Adina
                 and Bowman, Samuel R.
                 and Schwenk, Holger
                 and Stoyanov, Veselin",
  title = "XNLI: Evaluating Cross-lingual Sentence Representations",
  booktitle = "Proceedings of the 2018 Conference on Empirical Methods
               in Natural Language Processing",
  year = "2018",
  publisher = "Association for Computational Linguistics",
  location = "Brussels, Belgium",
}
'''

A		: Optional[int]								=     '''\
XNLI is a subset of a few thousand examples from MNLI which has been translated
into a 14 different languages (some low-ish resource). As with MNLI, the goal is
to predict textual entailment (does sentence A imply/contradict/neither sentence
B) and is a classification task (given two sentences, predict one of three
labels).
'''

A		: str								=     '''
Computes XNLI score which is just simple accuracy.
Args:
    predictions: Predicted labels.
    references: Ground truth labels.
Returns:
    \'accuracy\': accuracy
Examples:

    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> xnli_metric = datasets.load_metric("xnli")
    >>> results = xnli_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'accuracy\': 1.0}
'''




def 	UpperCamelCase__  (     SCREAMING_SNAKE_CASE_	:	List[str]		,   SCREAMING_SNAKE_CASE_	:	List[Any]       )  ->     str:
						return (preds == labels).mean()



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION					,			_KWARGS_DESCRIPTION    )
class    a_ (			datasets.Metric    ):






			def    UpperCamelCase_				(      self  ):
									return datasets.MetricInfo(
									    description=_DESCRIPTION				,			citation=_CITATION				,			inputs_description=_KWARGS_DESCRIPTION				,			features=datasets.Features(
									        {
									            """predictions""": datasets.Value("""int64""" if self.config_name != """sts-b""" else """float32"""  ),
									            """references""": datasets.Value("""int64""" if self.config_name != """sts-b""" else """float32"""  ),
									        }  )				,			codebase_urls=[]				,			reference_urls=[]				,			format="""numpy"""				,			)






			def    UpperCamelCase_				(      self				,			__UpperCamelCase				,			__UpperCamelCase  ):
									return {"accuracy": simple_accuracy(__UpperCamelCase				,			__UpperCamelCase  )}