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



      @classmethod
      def  __snake_case							(   cls      ):
          UpperCAmelCase__	:      Optional[Any]     							=					TOKEN
          HfFolder.save_token(UpperCamelCase_      )



      @classmethod
      def  __snake_case							(   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  __snake_case							(   self      ):
          UpperCAmelCase__	:      Optional[int]     							=					BertConfig(
              vocab_size=99     ,	hidden_size=32     ,	num_hidden_layers=5     ,	num_attention_heads=4     ,	intermediate_size=37      )
          UpperCAmelCase__	:      List[str]     							=					FlaxBertModel(UpperCamelCase_      )
          model.push_to_hub('test-model-flax'     ,	use_auth_token=self._token      )

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

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

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

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

          UpperCAmelCase__	:      Any     							=					flatten_dict(unfreeze(model.params      )      )
          UpperCAmelCase__	:      Optional[int]     							=					flatten_dict(unfreeze(new_model.params      )      )

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



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

          UpperCAmelCase__	:      Optional[int]     							=					FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'      )

          UpperCAmelCase__	:      Tuple     							=					flatten_dict(unfreeze(model.params      )      )
          UpperCAmelCase__	:      Union[str, Any]     							=					flatten_dict(unfreeze(new_model.params      )      )

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

          UpperCAmelCase__	:      Any     							=					FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'      )

          UpperCAmelCase__	:      Optional[int]     							=					flatten_dict(unfreeze(model.params      )      )
          UpperCAmelCase__	:      int     							=					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(UpperCamelCase_     ,	1E-3     ,	msg=F'''{key} not identical'''      )




def 					lowerCamelCase   (     _snake_case    ,_snake_case ):
    UpperCAmelCase__	:      List[str]     							=					True
    UpperCAmelCase__	:      Optional[int]     							=					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__	:      str     							=					False

    return models_are_equal





@require_flax
class 				a			(					unittest.TestCase      ):



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

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

              with self.assertRaises(UpperCamelCase_      ):
                  UpperCAmelCase__	:      Optional[Any]     							=					FlaxBertModel.from_pretrained(UpperCamelCase_      )

              UpperCAmelCase__	:      Optional[Any]     							=					FlaxBertModel.from_pretrained(UpperCamelCase_     ,	subfolder=UpperCamelCase_      )

          self.assertTrue(check_models_equal(UpperCamelCase_     ,	UpperCamelCase_      )      )



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Any     							=					BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'      )
          UpperCAmelCase__	:      Optional[Any]     							=					FlaxBertModel(UpperCamelCase_      )

          UpperCAmelCase__	:      Union[str, Any]     							=					'bert'
          with tempfile.TemporaryDirectory() as tmp_dir:
              model.save_pretrained(os.path.join(UpperCamelCase_     ,	UpperCamelCase_      )     ,	max_shard_size='10KB'      )

              with self.assertRaises(UpperCamelCase_      ):
                  UpperCAmelCase__	:      Optional[Any]     							=					FlaxBertModel.from_pretrained(UpperCamelCase_      )

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

          self.assertTrue(check_models_equal(UpperCamelCase_     ,	UpperCamelCase_      )      )



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      int     							=					'bert'
          UpperCAmelCase__	:      Dict     							=					'hf-internal-testing/tiny-random-bert-subfolder'

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

          UpperCAmelCase__	:      Dict     							=					FlaxBertModel.from_pretrained(UpperCamelCase_     ,	subfolder=UpperCamelCase_      )

          self.assertIsNotNone(UpperCamelCase_      )



      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      str     							=					'bert'
          UpperCAmelCase__	:      Optional[int]     							=					'hf-internal-testing/tiny-random-bert-sharded-subfolder'
          with self.assertRaises(UpperCamelCase_      ):
              UpperCAmelCase__	:      Tuple     							=					FlaxBertModel.from_pretrained(UpperCamelCase_      )

          UpperCAmelCase__	:      List[Any]     							=					FlaxBertModel.from_pretrained(UpperCamelCase_     ,	subfolder=UpperCamelCase_      )

          self.assertIsNotNone(UpperCamelCase_      )



"""simple docstring"""



from ...configuration_utils import PretrainedConfig





class 				a			(					lowercase      ):
      UpperCamelCase							:   Union[str, Any]           =     """bert-generation"""



      def __init__(   self     ,	UpperCamelCase_=50_358     ,	UpperCamelCase_=1_024     ,	UpperCamelCase_=24     ,	UpperCamelCase_=16     ,	UpperCamelCase_=4_096     ,	UpperCamelCase_="gelu"     ,	UpperCamelCase_=0.1     ,	UpperCamelCase_=0.1     ,	UpperCamelCase_=512     ,	UpperCamelCase_=0.02     ,	UpperCamelCase_=1E-12     ,	UpperCamelCase_=0     ,	UpperCamelCase_=2     ,	UpperCamelCase_=1     ,	UpperCamelCase_="absolute"     ,	UpperCamelCase_=True     ,	**UpperCamelCase_     ,	):
          super().__init__(pad_token_id=UpperCamelCase_     ,	bos_token_id=UpperCamelCase_     ,	eos_token_id=UpperCamelCase_     ,	**UpperCamelCase_      )

          UpperCAmelCase__	:      Optional[Any]     							=					vocab_size
          UpperCAmelCase__	:      Union[str, Any]     							=					hidden_size
          UpperCAmelCase__	:      Optional[Any]     							=					num_hidden_layers
          UpperCAmelCase__	:      Dict     							=					num_attention_heads
          UpperCAmelCase__	:      Dict     							=					hidden_act
          UpperCAmelCase__	:      Optional[Any]     							=					intermediate_size
          UpperCAmelCase__	:      Optional[int]     							=					hidden_dropout_prob
          UpperCAmelCase__	:      Tuple     							=					attention_probs_dropout_prob
          UpperCAmelCase__	:      List[Any]     							=					max_position_embeddings
          UpperCAmelCase__	:      Optional[int]     							=					initializer_range
          UpperCAmelCase__	:      Optional[int]     							=					layer_norm_eps
          UpperCAmelCase__	:      Union[str, Any]     							=					position_embedding_type
          UpperCAmelCase__	:      Dict     							=					use_cache

