






'''simple docstring'''

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							="0.12"  # assumed parallelism: 8




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






	"""simple docstring"""





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

							UpperCamelCase_				:   Union[str, Any]					    =							FlaxBertModel.from_pretrained(F"{USER}/test-model-flax" )

							UpperCamelCase_				:   Dict					    =							flatten_dict(unfreeze(model.params ) )
							UpperCamelCase_				:   int					    =							flatten_dict(unfreeze(new_model.params ) )

							for key in base_params.keys():
													UpperCamelCase_				:   Any					    =							(base_params[key] - new_params[key]).sum().item()
													self.assertLessEqual(__lowerCAmelCase			,     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(__lowerCAmelCase			,     repo_id="""test-model-flax"""			,     push_to_hub=__lowerCAmelCase			,     use_auth_token=self._token )

							UpperCamelCase_				:   List[str]					    =							FlaxBertModel.from_pretrained(F"{USER}/test-model-flax" )

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

							for key in base_params.keys():
													UpperCamelCase_				:   List[Any]					    =							(base_params[key] - new_params[key]).sum().item()
													self.assertLessEqual(__lowerCAmelCase			,     1E-3			,     msg=F"{key} not identical" )





	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   Any					    =							BertConfig(
							    vocab_size=99			,     hidden_size=32			,     num_hidden_layers=5			,     num_attention_heads=4			,     intermediate_size=37 )
							UpperCamelCase_				:   Tuple					    =							FlaxBertModel(__lowerCAmelCase )
							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_				:   str					    =							flatten_dict(unfreeze(model.params ) )
							UpperCamelCase_				:   str					    =							flatten_dict(unfreeze(new_model.params ) )

							for key in base_params.keys():
													UpperCamelCase_				:   Tuple					    =							(base_params[key] - new_params[key]).sum().item()
													self.assertLessEqual(__lowerCAmelCase			,     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(
													    __lowerCAmelCase			,     repo_id="""valid_org/test-model-flax-org"""			,     push_to_hub=__lowerCAmelCase			,     use_auth_token=self._token )

							UpperCamelCase_				:   Union[str, Any]					    =							FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org""" )

							UpperCamelCase_				:   Any					    =							flatten_dict(unfreeze(model.params ) )
							UpperCamelCase_				:   List[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(__lowerCAmelCase			,     1E-3			,     msg=F"{key} not identical" )






def 							snake_case					(     a_							: int    ,   a_							: int	)       ->				str:





						"""simple docstring"""

						UpperCamelCase_				:   List[str]					    =							True
						UpperCamelCase_				:   Optional[int]					    =							flatten_dict(modela.params	)
						UpperCamelCase_				:   Any					    =							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_				:   Any					    =							False

						return models_are_equal




@require_flax
class     A    (      unittest.TestCase						):






	"""simple docstring"""





	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   Union[str, Any]					    =							BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only""" )
							UpperCamelCase_				:   Optional[int]					    =							FlaxBertModel(__lowerCAmelCase )

							UpperCamelCase_				:   Union[str, Any]					    =							"""bert"""
							with tempfile.TemporaryDirectory() as tmp_dir:
													model.save_pretrained(os.path.join(__lowerCAmelCase			,     __lowerCAmelCase ) )

													with self.assertRaises(__lowerCAmelCase ):
																			UpperCamelCase_				:   Optional[int]					    =							FlaxBertModel.from_pretrained(__lowerCAmelCase )

													UpperCamelCase_				:   str					    =							FlaxBertModel.from_pretrained(__lowerCAmelCase			,     subfolder=__lowerCAmelCase )

							self.assertTrue(check_models_equal(__lowerCAmelCase			,     __lowerCAmelCase ) )
	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   Union[str, Any]					    =							BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only""" )
							UpperCamelCase_				:   Optional[Any]					    =							FlaxBertModel(__lowerCAmelCase )

							UpperCamelCase_				:   str					    =							"""bert"""
							with tempfile.TemporaryDirectory() as tmp_dir:
													model.save_pretrained(os.path.join(__lowerCAmelCase			,     __lowerCAmelCase )			,     max_shard_size="""10KB""" )

													with self.assertRaises(__lowerCAmelCase ):
																			UpperCamelCase_				:   Dict					    =							FlaxBertModel.from_pretrained(__lowerCAmelCase )

													UpperCamelCase_				:   Optional[int]					    =							FlaxBertModel.from_pretrained(__lowerCAmelCase			,     subfolder=__lowerCAmelCase )

							self.assertTrue(check_models_equal(__lowerCAmelCase			,     __lowerCAmelCase ) )
	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   int					    =							"""bert"""
							UpperCamelCase_				:   Optional[Any]					    =							"""hf-internal-testing/tiny-random-bert-subfolder"""

							with self.assertRaises(__lowerCAmelCase ):
													UpperCamelCase_				:   Any					    =							FlaxBertModel.from_pretrained(__lowerCAmelCase )

							UpperCamelCase_				:   Union[str, Any]					    =							FlaxBertModel.from_pretrained(__lowerCAmelCase			,     subfolder=__lowerCAmelCase )

							self.assertIsNotNone(__lowerCAmelCase )





	def   _UpperCAmelCase       (							self ):
							UpperCamelCase_				:   Union[str, Any]					    =							"""bert"""
							UpperCamelCase_				:   List[str]					    =							"""hf-internal-testing/tiny-random-bert-sharded-subfolder"""
							with self.assertRaises(__lowerCAmelCase ):
													UpperCamelCase_				:   Union[str, Any]					    =							FlaxBertModel.from_pretrained(__lowerCAmelCase )

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

							self.assertIsNotNone(__lowerCAmelCase )














'''simple docstring'''

def 							snake_case					(     a_							: int	)       ->				int:





      """simple docstring"""

      assert (
          isinstance(a_    ,   a_	) and number_of_steps > 0
      ), f"number_of_steps needs to be positive integer, your input {number_of_steps}"
      if number_of_steps == 1:
            return 1
      UpperCamelCase_    ,			UpperCamelCase_				:   str					    =							1, 1
      for _ in range(number_of_steps - 1	):
            UpperCamelCase_    ,			UpperCamelCase_				:   Tuple					    =							current + previous, current
      return current


if __name__ == "__main__":
      import doctest

      doctest.testmod()






