


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

						snake_case							:	Any					      =				'''0.12'''  # assumed parallelism: 8







@require_flax
@is_staging_test
class 	snake_case_       (unittest.TestCase				):
					@classmethod
					def 			lowerCamelCase__(				cls       :List[Any]				) ->   List[Any]:
												a__					=					TOKEN
												HfFolder.save_token(__snake_case				)
					@classmethod
					def 			lowerCamelCase__(				cls       :Dict				) ->   Dict:
												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				) ->   Optional[Any]:
												a__					=					BertConfig(
												    vocab_size=99      ,hidden_size=32      ,num_hidden_layers=5      ,num_attention_heads=4      ,intermediate_size=37				)
												a__					=					FlaxBertModel(__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(__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				)

												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(__snake_case      ,1E-3      ,msg=F'{key} not identical'				)



					def 			lowerCamelCase__(				self       :int				) ->   List[str]:
												a__					=					BertConfig(
												    vocab_size=99      ,hidden_size=32      ,num_hidden_layers=5      ,num_attention_heads=4      ,intermediate_size=37				)
												a__					=					FlaxBertModel(__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(__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				)

												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(__snake_case      ,1E-3      ,msg=F'{key} not identical'				)






def 							__lowercase       (			__lowerCAmelCase							:    Tuple ,   __lowerCAmelCase							:    Union[str, Any]       ):






							"""simple docstring"""




							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 	snake_case_       (unittest.TestCase				):
					def 			lowerCamelCase__(				self       :Optional[Any]				) ->   int:
												a__					=					BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'				)
												a__					=					FlaxBertModel(__snake_case				)

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

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

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

												self.assertTrue(check_models_equal(__snake_case      ,__snake_case				)				)
					def 			lowerCamelCase__(				self       :Any				) ->   int:
												a__					=					BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'				)
												a__					=					FlaxBertModel(__snake_case				)

												a__					=					'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				):
																										a__					=					FlaxBertModel.from_pretrained(__snake_case				)

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

												self.assertTrue(check_models_equal(__snake_case      ,__snake_case				)				)
					def 			lowerCamelCase__(				self       :Optional[int]				) ->   Optional[Any]:
												a__					=					'bert'
												a__					=					'hf-internal-testing/tiny-random-bert-subfolder'

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

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

												self.assertIsNotNone(__snake_case				)



					def 			lowerCamelCase__(				self       :Any				) ->   List[str]:
												a__					=					'bert'
												a__					=					'hf-internal-testing/tiny-random-bert-sharded-subfolder'
												with self.assertRaises(__snake_case				):
																			a__					=					FlaxBertModel.from_pretrained(__snake_case				)

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

												self.assertIsNotNone(__snake_case				)








from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case							:	Optional[int]					      =				logging.get_logger(__name__)

snake_case							:	Union[str, Any]					      =				{
    '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''',
    '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-vqa-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json'''
    ),
    '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''',
    '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-vcr-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json'''
    ),
    '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''',
    '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-nlvr2-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json'''
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}







class 	snake_case_       (lowerCamelCase_				):
					UpperCAmelCase__ :   Dict   				=      '''visual_bert'''
					def __init__(				self       :Optional[int]      ,__snake_case       :Any=3_05_22      ,__snake_case       :str=7_68      ,__snake_case       :Any=5_12      ,__snake_case       :Any=12      ,__snake_case       :int=12      ,__snake_case       :str=30_72      ,__snake_case       :int="gelu"      ,__snake_case       :Optional[int]=0.1      ,__snake_case       :str=0.1      ,__snake_case       :Union[str, Any]=5_12      ,__snake_case       :Tuple=2      ,__snake_case       :Union[str, Any]=0.02      ,__snake_case       :Optional[Any]=1E-12      ,__snake_case       :Optional[Any]=False      ,__snake_case       :int=True      ,__snake_case       :Any=1      ,__snake_case       :Optional[int]=0      ,__snake_case       :Tuple=2      ,**__snake_case       :Any      ,) ->   Union[str, Any]:
												super().__init__(pad_token_id=__snake_case      ,bos_token_id=__snake_case      ,eos_token_id=__snake_case      ,**__snake_case				)

												a__					=					vocab_size
												a__					=					max_position_embeddings
												a__					=					hidden_size
												a__					=					visual_embedding_dim
												a__					=					num_hidden_layers
												a__					=					num_attention_heads
												a__					=					intermediate_size
												a__					=					hidden_act
												a__					=					hidden_dropout_prob
												a__					=					attention_probs_dropout_prob
												a__					=					initializer_range
												a__					=					type_vocab_size
												a__					=					layer_norm_eps
												a__					=					bypass_transformer
												a__					=					special_visual_initialize




