





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

	_lowerCamelCase   				=			'0.12'  # assumed parallelism: 8




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


							@classmethod
							def      _snake_case  (							cls    :Dict		)   ->   Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      TOKEN
									HfFolder.save_token(__A		)


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



									"""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    :List[Any]		)   ->   Tuple:



									"""simple docstring"""



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

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

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

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

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

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

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


							def      _snake_case  (							self    :Optional[Any]		)   ->   str:



									"""simple docstring"""



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

									SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org"""		)

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

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

									SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org"""		)

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

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






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							str					,   UpperCamelCase__:							Optional[Any]    ):
		SCREAMING_SNAKE_CASE__			=      True
		SCREAMING_SNAKE_CASE__			=      flatten_dict(modela.params    )
		SCREAMING_SNAKE_CASE__			=      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:
						SCREAMING_SNAKE_CASE__			=      False

		return models_are_equal




@require_flax
class   UpperCamelCase_       (     unittest.TestCase    ):


							def      _snake_case  (							self    :List[str]		)   ->   Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""		)
									SCREAMING_SNAKE_CASE__			=      FlaxBertModel(__A		)

									SCREAMING_SNAKE_CASE__			=      """bert"""
									with tempfile.TemporaryDirectory() as tmp_dir:
											model.save_pretrained(os.path.join(__A							,				__A		)		)

											with self.assertRaises(__A		):
													SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained(__A		)

											SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained(__A							,				subfolder=__A		)

									self.assertTrue(check_models_equal(__A							,				__A		)		)


							def      _snake_case  (							self    :Union[str, Any]		)   ->   Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""		)
									SCREAMING_SNAKE_CASE__			=      FlaxBertModel(__A		)

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

											with self.assertRaises(__A		):
													SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained(__A		)

											SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained(__A							,				subfolder=__A		)

									self.assertTrue(check_models_equal(__A							,				__A		)		)


							def      _snake_case  (							self    :List[Any]		)   ->   str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      """bert"""
									SCREAMING_SNAKE_CASE__			=      """hf-internal-testing/tiny-random-bert-subfolder"""

									with self.assertRaises(__A		):
											SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained(__A		)

									SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained(__A							,				subfolder=__A		)

									self.assertIsNotNone(__A		)


							def      _snake_case  (							self    :List[str]		)   ->   Union[str, Any]:



									"""simple docstring"""



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

									SCREAMING_SNAKE_CASE__			=      FlaxBertModel.from_pretrained(__A							,				subfolder=__A		)

									self.assertIsNotNone(__A		)






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


_lowerCamelCase   				=			{
    'configuration_layoutlmv3': [
        'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'LayoutLMv3Config',
        'LayoutLMv3OnnxConfig',
    ],
    'processing_layoutlmv3': ['LayoutLMv3Processor'],
    'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			['LayoutLMv3TokenizerFast']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			[
	    'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'LayoutLMv3ForQuestionAnswering',
	    'LayoutLMv3ForSequenceClassification',
	    'LayoutLMv3ForTokenClassification',
	    'LayoutLMv3Model',
	    'LayoutLMv3PreTrainedModel',
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			[
	    'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'TFLayoutLMv3ForQuestionAnswering',
	    'TFLayoutLMv3ForSequenceClassification',
	    'TFLayoutLMv3ForTokenClassification',
	    'TFLayoutLMv3Model',
	    'TFLayoutLMv3PreTrainedModel',
	]

try:
	if not is_vision_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			['LayoutLMv3FeatureExtractor']
	_lowerCamelCase   				=			['LayoutLMv3ImageProcessor']


if TYPE_CHECKING:
	from .configuration_layoutlmva import (
	    LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    LayoutLMvaConfig,
	    LayoutLMvaOnnxConfig,
	)
	from .processing_layoutlmva import LayoutLMvaProcessor
	from .tokenization_layoutlmva import LayoutLMvaTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_layoutlmva import (
		    LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
		    LayoutLMvaForQuestionAnswering,
		    LayoutLMvaForSequenceClassification,
		    LayoutLMvaForTokenClassification,
		    LayoutLMvaModel,
		    LayoutLMvaPreTrainedModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_layoutlmva import (
		    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFLayoutLMvaForQuestionAnswering,
		    TFLayoutLMvaForSequenceClassification,
		    TFLayoutLMvaForTokenClassification,
		    TFLayoutLMvaModel,
		    TFLayoutLMvaPreTrainedModel,
		)

	try:
		if not is_vision_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
		from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
	import sys

	_lowerCamelCase   				=			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)