




'''simple docstring'''


import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
				from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
				from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
				from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


_snake_case   :   int     							=	DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

_snake_case   :   Optional[int]     							=	'main'
# Default branch name
_snake_case   :   List[Any]     							=	'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
_snake_case   :   Any     							=	'aaaaaaa'
# This commit does not exist, so we should 404.

_snake_case   :   Dict     							=	'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
_snake_case   :   List[str]     							=	'4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'
@contextlib.contextmanager
def 	snake_case_					():





							'''simple docstring'''


							print('''Welcome!'''   )
							yield
							print('''Bye!'''   )
@contextlib.contextmanager
def 	snake_case_					():





							'''simple docstring'''


							print('''Bonjour!'''   )
							yield
							print('''Au revoir!'''   )

class     A					(	unittest.TestCase					):







							def      __lowerCAmelCase    (						self :						Any      )    ->							Union[str, Any]:


														"""simple docstring"""


														assert transformers.__spec__ is not None
														assert importlib.util.find_spec('''transformers'''      ) is not None



class     A					(	unittest.TestCase					):







							@unittest.mock.patch('''sys.stdout'''							,    new_callable=io.StringIO      )
							def      __lowerCAmelCase    (						self :						Tuple							,    lowerCAmelCase_ :						List[Any]      )    ->							Optional[int]:


														"""simple docstring"""


														with ContextManagers([]      ):
																					print('''Transformers are awesome!'''      )
														# The print statement adds a new line at the end of the output
														self.assertEqual(mock_stdout.getvalue()							,    '''Transformers are awesome!\n'''      )







							@unittest.mock.patch('''sys.stdout'''							,    new_callable=io.StringIO      )
							def      __lowerCAmelCase    (						self :						Any							,    lowerCAmelCase_ :						int      )    ->							Dict:


														"""simple docstring"""


														with ContextManagers([context_en()]      ):
																					print('''Transformers are awesome!'''      )
														# The output should be wrapped with an English welcome and goodbye
														self.assertEqual(mock_stdout.getvalue()							,    '''Welcome!\nTransformers are awesome!\nBye!\n'''      )







							@unittest.mock.patch('''sys.stdout'''							,    new_callable=io.StringIO      )
							def      __lowerCAmelCase    (						self :						List[Any]							,    lowerCAmelCase_ :						str      )    ->							Union[str, Any]:


														"""simple docstring"""


														with ContextManagers([context_fr(), context_en()]      ):
																					print('''Transformers are awesome!'''      )
														# The output should be wrapped with an English and French welcome and goodbye
														self.assertEqual(mock_stdout.getvalue()							,    '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n'''      )







							@require_torch
							def      __lowerCAmelCase    (						self :						Any      )    ->							str:


														"""simple docstring"""


														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''labels''']      )
														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''labels''', '''next_sentence_label''']      )
														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''start_positions''', '''end_positions''']      )

														class     A					(	_a					):
																					pass



														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''labels''']      )







							@require_tf
							def      __lowerCAmelCase    (						self :						Optional[int]      )    ->							Dict:


														"""simple docstring"""


														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''labels''']      )
														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''labels''', '''next_sentence_label''']      )
														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''start_positions''', '''end_positions''']      )

														class     A					(	_a					):
																					pass



														self.assertEqual(find_labels(lowerCAmelCase_      )							,    ['''labels''']      )







							@require_flax
							def      __lowerCAmelCase    (						self :						Any      )    ->							List[str]:


														"""simple docstring"""


														self.assertEqual(find_labels(lowerCAmelCase_      )							,    []      )
														self.assertEqual(find_labels(lowerCAmelCase_      )							,    []      )
														self.assertEqual(find_labels(lowerCAmelCase_      )							,    []      )

														class     A					(	_a					):
																					pass



														self.assertEqual(find_labels(lowerCAmelCase_      )							,    []      )







'''simple docstring'''


import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel
def 	snake_case_					(UpperCamelCase :						BertModel				,		UpperCamelCase :						str				,		UpperCamelCase :						str   ):





							'''simple docstring'''


							_a			  =       ('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''')

							_a			  =       (
							    ('''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   )

							_a			  =       model.state_dict()

							def to_tf_var_name(UpperCamelCase :						str   ):
														for patt, repl in iter(UpperCamelCase   ):
																					_a			  =       name.replace(UpperCamelCase				,		UpperCamelCase   )
														return f'bert/{name}'

							def create_tf_var(UpperCamelCase :						np.ndarray				,		UpperCamelCase :						str				,		UpperCamelCase :						tf.Session   ):
														_a			  =       tf.dtypes.as_dtype(tensor.dtype   )
														_a			  =       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:
																					_a			  =       to_tf_var_name(UpperCamelCase   )
																					_a			  =       state_dict[var_name].numpy()
																					if any(x in var_name for x in tensors_to_transpose   ):
																												_a			  =       torch_tensor.T
																					_a			  =       create_tf_var(tensor=UpperCamelCase				,		name=UpperCamelCase				,		session=UpperCamelCase   )
																					tf.keras.backend.set_value(UpperCamelCase				,		UpperCamelCase   )
																					_a			  =       session.run(UpperCamelCase   )
																					print(f'Successfully created {tf_name}: {np.allclose(UpperCamelCase				,		UpperCamelCase   )}'   )

														_a			  =       tf.train.Saver(tf.trainable_variables()   )
														saver.save(UpperCamelCase				,		os.path.join(UpperCamelCase				,		model_name.replace('''-'''				,		'''_'''   ) + '''.ckpt'''   )   )
def 	snake_case_					(UpperCamelCase :						Tuple=None   ):





							'''simple docstring'''


							_a			  =       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'''   )
							_a			  =       parser.parse_args(UpperCamelCase   )

							_a			  =       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()

