
"""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

    a		:       List[Any]     						=       '0.12'  # assumed parallelism: 8







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



     @classmethod
     def 			lowercase__  (							cls   )		->     int:







         """simple docstring"""



         a__		=			TOKEN
         HfFolder.save_token(__SCREAMING_SNAKE_CASE   )



     @classmethod
     def 			lowercase__  (							cls   )		->     List[str]:







         """simple docstring"""



         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 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__		=			BertConfig(
             vocab_size=9_9     ,				hidden_size=3_2     ,				num_hidden_layers=5     ,				num_attention_heads=4     ,				intermediate_size=3_7   )
         a__		=			FlaxBertModel(__SCREAMING_SNAKE_CASE   )
         model.push_to_hub('test-model-flax'     ,				use_auth_token=self._token   )

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

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

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

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

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

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




     def 			lowercase__  (							self   )		->     List[Any]:







         """simple docstring"""



         a__		=			BertConfig(
             vocab_size=9_9     ,				hidden_size=3_2     ,				num_hidden_layers=5     ,				num_attention_heads=4     ,				intermediate_size=3_7   )
         a__		=			FlaxBertModel(__SCREAMING_SNAKE_CASE   )
         model.push_to_hub('valid_org/test-model-flax-org'     ,				use_auth_token=self._token   )

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

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

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

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

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

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




def     __magic_name__		(					UpperCamelCase     :		List[Any]							,		UpperCamelCase     :		Dict     )     ->						Optional[int]:
    a__		=			True
    a__		=			flatten_dict(modela.params     )
    a__		=			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:
            a__		=			False

    return models_are_equal







@require_flax
class     lowercase(unittest.TestCase       ):



     def 			lowercase__  (							self   )		->     Tuple:







         """simple docstring"""



         a__		=			BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'   )
         a__		=			FlaxBertModel(__SCREAMING_SNAKE_CASE   )

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

             with self.assertRaises(__SCREAMING_SNAKE_CASE   ):
                 a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE   )

             a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE     ,				subfolder=__SCREAMING_SNAKE_CASE   )

         self.assertTrue(check_models_equal(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )   )



     def 			lowercase__  (							self   )		->     Optional[Any]:







         """simple docstring"""



         a__		=			BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'   )
         a__		=			FlaxBertModel(__SCREAMING_SNAKE_CASE   )

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

             with self.assertRaises(__SCREAMING_SNAKE_CASE   ):
                 a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE   )

             a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE     ,				subfolder=__SCREAMING_SNAKE_CASE   )

         self.assertTrue(check_models_equal(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )   )



     def 			lowercase__  (							self   )		->     Optional[Any]:







         """simple docstring"""



         a__		=			'bert'
         a__		=			'hf-internal-testing/tiny-random-bert-subfolder'

         with self.assertRaises(__SCREAMING_SNAKE_CASE   ):
             a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE   )

         a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE     ,				subfolder=__SCREAMING_SNAKE_CASE   )

         self.assertIsNotNone(__SCREAMING_SNAKE_CASE   )




     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__		=			'bert'
         a__		=			'hf-internal-testing/tiny-random-bert-sharded-subfolder'
         with self.assertRaises(__SCREAMING_SNAKE_CASE   ):
             a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE   )

         a__		=			FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE     ,				subfolder=__SCREAMING_SNAKE_CASE   )

         self.assertIsNotNone(__SCREAMING_SNAKE_CASE   )




"""simple docstring"""




from math import ceil



def     __magic_name__		(					UpperCamelCase     :		int = 1001     )     ->						int:
    a__		=			1

    for i in range(1							,		int(ceil(n / 2.0     )     )     ):
        a__		=			2 * i + 1
        a__		=			2 * i
        a__		=			total + 4 * odd**2 - 6 * even

    return total


if __name__ == "__main__":
    import sys

    if len(sys.argv) == 1:
        print(solution())
    else:
        try:
            a		:       Any     						=       int(sys.argv[1])
            print(solution(n))
        except ValueError:
            print('Invalid entry - please enter a number')


