

'''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 	lowerCAmelCase  (   unittest.TestCase ):







       @classmethod
       def       snake_case (				cls      :			Dict   ):







            """simple docstring"""







            __lowercase      		=TOKEN
            HfFolder.save_token(__lowercase   )







       @classmethod
       def       snake_case (				cls      :			Dict   ):







            """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       snake_case (				self      :			Any   ):







            """simple docstring"""







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

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

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

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

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

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

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







       def       snake_case (				self      :			int   ):







            """simple docstring"""







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

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

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

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

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

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

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






def 					__UpperCamelCase				(						lowercase__    :	Optional[Any],       lowercase__    :	str			):






     '''simple docstring'''





     __lowercase      		=True
     __lowercase      		=flatten_dict(modela.params			)
     __lowercase      		=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:
               __lowercase      		=False

     return models_are_equal







@require_flax
class 	lowerCAmelCase  (   unittest.TestCase ):







       def       snake_case (				self      :			str   ):







            """simple docstring"""







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

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

                 with self.assertRaises(__lowercase   ):
                      __lowercase      		=FlaxBertModel.from_pretrained(__lowercase   )

                 __lowercase      		=FlaxBertModel.from_pretrained(__lowercase    ,   subfolder=__lowercase   )

            self.assertTrue(check_models_equal(__lowercase    ,   __lowercase   )   )







       def       snake_case (				self      :			Any   ):







            """simple docstring"""







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

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

                 with self.assertRaises(__lowercase   ):
                      __lowercase      		=FlaxBertModel.from_pretrained(__lowercase   )

                 __lowercase      		=FlaxBertModel.from_pretrained(__lowercase    ,   subfolder=__lowercase   )

            self.assertTrue(check_models_equal(__lowercase    ,   __lowercase   )   )







       def       snake_case (				self      :			Optional[int]   ):







            """simple docstring"""







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

            with self.assertRaises(__lowercase   ):
                 __lowercase      		=FlaxBertModel.from_pretrained(__lowercase   )

            __lowercase      		=FlaxBertModel.from_pretrained(__lowercase    ,   subfolder=__lowercase   )

            self.assertIsNotNone(__lowercase   )







       def       snake_case (				self      :			Optional[int]   ):







            """simple docstring"""







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

            __lowercase      		=FlaxBertModel.from_pretrained(__lowercase    ,   subfolder=__lowercase   )

            self.assertIsNotNone(__lowercase   )





'''simple docstring'''

# Function to print upper half of diamond (pyramid)

def 					__UpperCamelCase				(						lowercase__    :	Optional[Any]			):






     '''simple docstring'''





     for i in range(0,       lowercase__			):
          for _ in range(0,       n - i - 1			):  # printing spaces
               print(' ',       end=''			)
          for _ in range(0,       i + 1			):  # printing stars
               print('* ',       end=''			)
          print()

def 					__UpperCamelCase				(						lowercase__    :	Optional[Any]			):






     '''simple docstring'''





     for i in range(lowercase__,       0,       -1			):
          for _ in range(lowercase__,       0,       -1			):  # printing stars
               print('* ',       end=''			)
          print()
          for _ in range(n - i + 1,       0,       -1			):  # printing spaces
               print(' ',       end=''			)

def 					__UpperCamelCase				(						lowercase__    :	Any			):






     '''simple docstring'''





     if n <= 0:
          print('       ...       ....        nothing printing :('			)
          return
     floyd(lowercase__			)  # upper half
     reverse_floyd(lowercase__			)  # lower half


if __name__ == "__main__":
       print(r'''| /\ | |- |  |-  |--| |\  /| |-''')
       print(r'''|/  \| |- |_ |_  |__| | \/ | |_''')
       UpperCAmelCase					 =	1
       while K:
              UpperCAmelCase					 =	int(input('''enter the number and , and see the magic : '''))
              print()
              pretty_print(user_number)
              UpperCAmelCase					 =	int(input('''press 0 to exit... and 1 to continue...'''))

       print('''Good Bye...''')


