
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

							lowerCamelCase_						: Dict         =  """0.12"""  # assumed parallelism: 8

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






			'''simple docstring'''

			@classmethod
			def       lowerCAmelCase__	(       cls :       str      ):
							UpperCamelCase_:							Any	=						TOKEN
							HfFolder.save_token(snake_case_      )

			@classmethod
			def       lowerCAmelCase__	(       cls :       List[Any]      ):
							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       lowerCAmelCase__	(       self :       Any      ):
							UpperCamelCase_:							Union[str, Any]	=						BertConfig(
							    vocab_size=99						,	hidden_size=32						,	num_hidden_layers=5						,	num_attention_heads=4						,	intermediate_size=37      )
							UpperCamelCase_:							Dict	=						FlaxBertModel(snake_case_      )
							model.push_to_hub("""test-model-flax"""						,	use_auth_token=self._token      )

							UpperCamelCase_:							Dict	=						FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax'''      )

							UpperCamelCase_:							List[str]	=						flatten_dict(unfreeze(model.params      )      )
							UpperCamelCase_:							Any	=						flatten_dict(unfreeze(new_model.params      )      )

							for key in base_params.keys():
											UpperCamelCase_:							Tuple	=						(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      )

							UpperCamelCase_:							Tuple	=						FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax'''      )

							UpperCamelCase_:							int	=						flatten_dict(unfreeze(model.params      )      )
							UpperCamelCase_:							List[str]	=						flatten_dict(unfreeze(new_model.params      )      )

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




			def       lowerCAmelCase__	(       self :       Tuple      ):
							UpperCamelCase_:							Union[str, Any]	=						BertConfig(
							    vocab_size=99						,	hidden_size=32						,	num_hidden_layers=5						,	num_attention_heads=4						,	intermediate_size=37      )
							UpperCamelCase_:							Dict	=						FlaxBertModel(snake_case_      )
							model.push_to_hub("""valid_org/test-model-flax-org"""						,	use_auth_token=self._token      )

							UpperCamelCase_:							Any	=						FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org"""      )

							UpperCamelCase_:							List[Any]	=						flatten_dict(unfreeze(model.params      )      )
							UpperCamelCase_:							Any	=						flatten_dict(unfreeze(new_model.params      )      )

							for key in base_params.keys():
											UpperCamelCase_:							List[str]	=						(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      )

							UpperCamelCase_:							str	=						FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org"""      )

							UpperCamelCase_:							List[str]	=						flatten_dict(unfreeze(model.params      )      )
							UpperCamelCase_:							Tuple	=						flatten_dict(unfreeze(new_model.params      )      )

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







def        A__					(						lowerCamelCase     ,    lowerCamelCase						)  ->	Optional[int]:
				UpperCamelCase_:							str	=						True
				UpperCamelCase_:							Optional[Any]	=						flatten_dict(modela.params						)
				UpperCamelCase_:							int	=						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:
												UpperCamelCase_:							Union[str, Any]	=						False

				return models_are_equal





@require_flax
class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			def       lowerCAmelCase__	(       self :       Tuple      ):
							UpperCamelCase_:							Optional[int]	=						BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""      )
							UpperCamelCase_:							int	=						FlaxBertModel(snake_case_      )

							UpperCamelCase_:							Any	=						"""bert"""
							with tempfile.TemporaryDirectory() as tmp_dir:
											model.save_pretrained(os.path.join(snake_case_						,	snake_case_      )      )

											with self.assertRaises(snake_case_      ):
															UpperCamelCase_:							List[Any]	=						FlaxBertModel.from_pretrained(snake_case_      )

											UpperCamelCase_:							Any	=						FlaxBertModel.from_pretrained(snake_case_						,	subfolder=snake_case_      )

							self.assertTrue(check_models_equal(snake_case_						,	snake_case_      )      )

			def       lowerCAmelCase__	(       self :       Tuple      ):
							UpperCamelCase_:							int	=						BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""      )
							UpperCamelCase_:							Tuple	=						FlaxBertModel(snake_case_      )

							UpperCamelCase_:							Tuple	=						"""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_      ):
															UpperCamelCase_:							Optional[int]	=						FlaxBertModel.from_pretrained(snake_case_      )

											UpperCamelCase_:							List[str]	=						FlaxBertModel.from_pretrained(snake_case_						,	subfolder=snake_case_      )

							self.assertTrue(check_models_equal(snake_case_						,	snake_case_      )      )

			def       lowerCAmelCase__	(       self :       int      ):
							UpperCamelCase_:							List[str]	=						"""bert"""
							UpperCamelCase_:							Optional[int]	=						"""hf-internal-testing/tiny-random-bert-subfolder"""

							with self.assertRaises(snake_case_      ):
											UpperCamelCase_:							int	=						FlaxBertModel.from_pretrained(snake_case_      )

							UpperCamelCase_:							List[str]	=						FlaxBertModel.from_pretrained(snake_case_						,	subfolder=snake_case_      )

							self.assertIsNotNone(snake_case_      )




			def       lowerCAmelCase__	(       self :       List[str]      ):
							UpperCamelCase_:							Optional[Any]	=						"""bert"""
							UpperCamelCase_:							str	=						"""hf-internal-testing/tiny-random-bert-sharded-subfolder"""
							with self.assertRaises(snake_case_      ):
											UpperCamelCase_:							Optional[int]	=						FlaxBertModel.from_pretrained(snake_case_      )

							UpperCamelCase_:							int	=						FlaxBertModel.from_pretrained(snake_case_						,	subfolder=snake_case_      )

							self.assertIsNotNone(snake_case_      )








class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       List[str]						,	snake_case_ :       int						,	snake_case_ :       Optional[Any]=None						,	snake_case_ :       List[str]=None      ):
							UpperCamelCase_:							List[Any]	=						data
							UpperCamelCase_:							List[Any]	=						previous
							UpperCamelCase_:							Tuple	=						next_node

			def __str__(       self :       Dict      ):
							return f'''{self.data}'''

			def       lowerCAmelCase__	(       self :       List[str]      ):
							return self.data

			def       lowerCAmelCase__	(       self :       Any      ):
							return self.next




			def       lowerCAmelCase__	(       self :       List[str]      ):
							return self.previous




class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       Optional[Any]						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						head

			def __iter__(       self :       Union[str, Any]      ):
							return self




			def       lowerCAmelCase__	(       self :       Union[str, Any]      ):
							if not self.current:
											raise StopIteration
							else:
											UpperCamelCase_:							Dict	=						self.current.get_data()
											UpperCamelCase_:							Tuple	=						self.current.get_next()
											return value




class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       int      ):
							UpperCamelCase_:							Optional[int]	=						None  # First node in list
							UpperCamelCase_:							Dict	=						None  # Last node in list

			def __str__(       self :       Tuple      ):
							UpperCamelCase_:							int	=						self.head
							UpperCamelCase_:							Tuple	=						[]
							while current is not None:
											nodes.append(current.get_data()      )
											UpperCamelCase_:							List[str]	=						current.get_next()
							return " ".join(str(snake_case_      ) for node in nodes      )

			def __contains__(       self :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Optional[Any]	=						self.head
							while current:
											if current.get_data() == value:
															return True
											UpperCamelCase_:							Any	=						current.get_next()
							return False

			def __iter__(       self :       Any      ):
							return LinkedListIterator(self.head      )

			def       lowerCAmelCase__	(       self :       Tuple      ):
							if self.head:
											return self.head.get_data()
							return None

			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							if self.tail:
											return self.tail.get_data()
							return None

			def       lowerCAmelCase__	(       self :       Optional[int]						,	snake_case_ :       Node      ):
							if self.head is None:
											UpperCamelCase_:							Tuple	=						node
											UpperCamelCase_:							Optional[int]	=						node
							else:
											self.insert_before_node(self.head						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]						,	snake_case_ :       Node      ):
							if self.head is None:
											self.set_head(snake_case_      )
							else:
											self.insert_after_node(self.tail						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       int      ):
							UpperCamelCase_:							Any	=						Node(snake_case_      )
							if self.head is None:
											self.set_head(snake_case_      )
							else:
											self.set_tail(snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]						,	snake_case_ :       Node						,	snake_case_ :       Node      ):
							UpperCamelCase_:							str	=						node
							UpperCamelCase_:							int	=						node.previous

							if node.get_previous() is None:
											UpperCamelCase_:							int	=						node_to_insert
							else:
											UpperCamelCase_:							Dict	=						node_to_insert

							UpperCamelCase_:							int	=						node_to_insert

			def       lowerCAmelCase__	(       self :       Dict						,	snake_case_ :       Node						,	snake_case_ :       Node      ):
							UpperCamelCase_:							Tuple	=						node
							UpperCamelCase_:							Dict	=						node.next

							if node.get_next() is None:
											UpperCamelCase_:							Union[str, Any]	=						node_to_insert
							else:
											UpperCamelCase_:							str	=						node_to_insert

							UpperCamelCase_:							int	=						node_to_insert

			def       lowerCAmelCase__	(       self :       Tuple						,	snake_case_ :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						1
							UpperCamelCase_:							List[str]	=						Node(snake_case_      )
							UpperCamelCase_:							Optional[Any]	=						self.head
							while node:
											if current_position == position:
															self.insert_before_node(snake_case_						,	snake_case_      )
															return
											current_position += 1
											UpperCamelCase_:							Dict	=						node.next
							self.insert_after_node(self.tail						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						self.head
							while node:
											if node.get_data() == item:
															return node
											UpperCamelCase_:							List[Any]	=						node.get_next()
							raise Exception("""Node not found"""      )

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       List[str]      ):
							if (node := self.get_node(snake_case_      )) is not None:
											if node == self.head:
															UpperCamelCase_:							Optional[int]	=						self.head.get_next()

											if node == self.tail:
															UpperCamelCase_:							Union[str, Any]	=						self.tail.get_previous()

											self.remove_node_pointers(snake_case_      )

			@staticmethod
			def       lowerCAmelCase__	(       snake_case_ :       Node      ):
							if node.get_next():
											UpperCamelCase_:							str	=						node.previous

							if node.get_previous():
											UpperCamelCase_:							int	=						node.next

							UpperCamelCase_:							List[str]	=						None
							UpperCamelCase_:							int	=						None




			def       lowerCAmelCase__	(       self :       str      ):
							return self.head is None







def        A__					(						)  ->	None:
				pass


if __name__ == "__main__":
							import doctest

							doctest.testmod()






