

import tempfile
import unittest

import numpy as np
from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax


if is_flax_available():
					import os

					from flax.core.frozen_dict import unfreeze
					from flax.traverse_util import flatten_dict

					from transformers import FlaxBertModel

					_UpperCAmelCase						:  int									=			"""0.12"""  # assumed parallelism: 8







@require_flax
@is_staging_test
class 					lowercase							(			unittest.TestCase     ):




				@classmethod
				def        a				(   cls			):
											snake_case_     							=       TOKEN
											HfFolder.save_token(snake_case			)




				@classmethod
				def        a				(   cls			):
											try:
																		delete_repo(token=cls._token ,  repo_id='test-model-flax'			)
											except HTTPError:
																		pass

											try:
																		delete_repo(token=cls._token ,  repo_id='valid_org/test-model-flax-org'			)
											except HTTPError:
																		pass




				def        a				(   self			):
											snake_case_     							=       BertConfig(
											    vocab_size=99 ,  hidden_size=32 ,  num_hidden_layers=5 ,  num_attention_heads=4 ,  intermediate_size=37			)
											snake_case_     							=       FlaxBertModel(snake_case			)
											model.push_to_hub('test-model-flax' ,  use_auth_token=self._token			)

											snake_case_     							=       FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax'''			)

											snake_case_     							=       flatten_dict(unfreeze(model.params			)			)
											snake_case_     							=       flatten_dict(unfreeze(new_model.params			)			)

											for key in base_params.keys():
																		snake_case_     							=       (base_params[key] - new_params[key]).sum().item()
																		self.assertLessEqual(snake_case ,  1e-3 ,  msg=F'''{key} not identical'''			)

											# Reset repo
											delete_repo(token=self._token ,  repo_id='test-model-flax'			)

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																		model.save_pretrained(snake_case ,  repo_id='test-model-flax' ,  push_to_hub=snake_case ,  use_auth_token=self._token			)

											snake_case_     							=       FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax'''			)

											snake_case_     							=       flatten_dict(unfreeze(model.params			)			)
											snake_case_     							=       flatten_dict(unfreeze(new_model.params			)			)

											for key in base_params.keys():
																		snake_case_     							=       (base_params[key] - new_params[key]).sum().item()
																		self.assertLessEqual(snake_case ,  1e-3 ,  msg=F'''{key} not identical'''			)




				def        a				(   self			):
											snake_case_     							=       BertConfig(
											    vocab_size=99 ,  hidden_size=32 ,  num_hidden_layers=5 ,  num_attention_heads=4 ,  intermediate_size=37			)
											snake_case_     							=       FlaxBertModel(snake_case			)
											model.push_to_hub('valid_org/test-model-flax-org' ,  use_auth_token=self._token			)

											snake_case_     							=       FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'			)

											snake_case_     							=       flatten_dict(unfreeze(model.params			)			)
											snake_case_     							=       flatten_dict(unfreeze(new_model.params			)			)

											for key in base_params.keys():
																		snake_case_     							=       (base_params[key] - new_params[key]).sum().item()
																		self.assertLessEqual(snake_case ,  1e-3 ,  msg=F'''{key} not identical'''			)

											# Reset repo
											delete_repo(token=self._token ,  repo_id='valid_org/test-model-flax-org'			)

											# Push to hub via save_pretrained
											with tempfile.TemporaryDirectory() as tmp_dir:
																		model.save_pretrained(
																		    snake_case ,  repo_id='valid_org/test-model-flax-org' ,  push_to_hub=snake_case ,  use_auth_token=self._token			)

											snake_case_     							=       FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'			)

											snake_case_     							=       flatten_dict(unfreeze(model.params			)			)
											snake_case_     							=       flatten_dict(unfreeze(new_model.params			)			)

											for key in base_params.keys():
																		snake_case_     							=       (base_params[key] - new_params[key]).sum().item()
																		self.assertLessEqual(snake_case ,  1e-3 ,  msg=F'''{key} not identical'''			)





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       True
							snake_case_     							=       flatten_dict(modela.params )
							snake_case_     							=       flatten_dict(modela.params )
							for key in flat_params_a.keys():
														if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4:
																					snake_case_     							=       False

							return models_are_equal







@require_flax
class 					lowercase							(			unittest.TestCase     ):




				def        a				(   self			):
											snake_case_     							=       BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'			)
											snake_case_     							=       FlaxBertModel(snake_case			)

											snake_case_     							=       'bert'
											with tempfile.TemporaryDirectory() as tmp_dir:
																		model.save_pretrained(os.path.join(snake_case ,  snake_case			)			)

																		with self.assertRaises(snake_case			):
																									snake_case_     							=       FlaxBertModel.from_pretrained(snake_case			)

																		snake_case_     							=       FlaxBertModel.from_pretrained(snake_case ,  subfolder=snake_case			)

											self.assertTrue(check_models_equal(snake_case ,  snake_case			)			)




				def        a				(   self			):
											snake_case_     							=       BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'			)
											snake_case_     							=       FlaxBertModel(snake_case			)

											snake_case_     							=       'bert'
											with tempfile.TemporaryDirectory() as tmp_dir:
																		model.save_pretrained(os.path.join(snake_case ,  snake_case			) ,  max_shard_size='10KB'			)

																		with self.assertRaises(snake_case			):
																									snake_case_     							=       FlaxBertModel.from_pretrained(snake_case			)

																		snake_case_     							=       FlaxBertModel.from_pretrained(snake_case ,  subfolder=snake_case			)

											self.assertTrue(check_models_equal(snake_case ,  snake_case			)			)




				def        a				(   self			):
											snake_case_     							=       'bert'
											snake_case_     							=       'hf-internal-testing/tiny-random-bert-subfolder'

											with self.assertRaises(snake_case			):
																		snake_case_     							=       FlaxBertModel.from_pretrained(snake_case			)

											snake_case_     							=       FlaxBertModel.from_pretrained(snake_case ,  subfolder=snake_case			)

											self.assertIsNotNone(snake_case			)




				def        a				(   self			):
											snake_case_     							=       'bert'
											snake_case_     							=       'hf-internal-testing/tiny-random-bert-sharded-subfolder'
											with self.assertRaises(snake_case			):
																		snake_case_     							=       FlaxBertModel.from_pretrained(snake_case			)

											snake_case_     							=       FlaxBertModel.from_pretrained(snake_case ,  subfolder=snake_case			)

											self.assertIsNotNone(snake_case			)







import argparse

import torch

from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert
from transformers.utils import logging


logging.set_verbosity_info()





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       BertConfig.from_json_file(UpperCamelCase__ )
							print(F'''Building PyTorch model from configuration: {config}''' )
							snake_case_     							=       BertForPreTraining(UpperCamelCase__ )

							# Load weights from tf checkpoint
							load_tf_weights_in_bert(UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ )

							# Save pytorch-model
							print(F'''Save PyTorch model to {pytorch_dump_path}''' )
							torch.save(model.state_dict()  ,				UpperCamelCase__ )


if __name__ == "__main__":
					_UpperCAmelCase						:  Union[str, Any]									=			argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
					)
					parser.add_argument(
					    """--bert_config_file""",
					    default=None,
					    type=str,
					    required=True,
					    help=(
					        """The config json file corresponding to the pre-trained BERT model. \n"""
					        """This specifies the model architecture."""
					    ),
					)
					parser.add_argument(
					    """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
					)
					_UpperCAmelCase						:  Any									=			parser.parse_args()
					convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)




