
'''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								=			"0.12"  # assumed parallelism: 8





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



			@classmethod
			def A__       (    cls				)	->						Any:




									'''simple docstring'''



									lowercase__							       =				TOKEN
									HfFolder.save_token(_SCREAMING_SNAKE_CASE				)



			@classmethod
			def A__       (    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 A__       (    self				)	->						List[str]:




									'''simple docstring'''



									lowercase__							       =				BertConfig(
									    vocab_size=99				,   hidden_size=32				,   num_hidden_layers=5				,   num_attention_heads=4				,   intermediate_size=37				)
									lowercase__							       =				FlaxBertModel(_SCREAMING_SNAKE_CASE				)
									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(_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				)

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



			def A__       (    self				)	->						List[str]:




									'''simple docstring'''



									lowercase__							       =				BertConfig(
									    vocab_size=99				,   hidden_size=32				,   num_hidden_layers=5				,   num_attention_heads=4				,   intermediate_size=37				)
									lowercase__							       =				FlaxBertModel(_SCREAMING_SNAKE_CASE				)
									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(_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				)

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





def        _A						(							lowercase__						,						lowercase__		):
						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 	A							(				unittest.TestCase ):



			def A__       (    self				)	->						Any:




									'''simple docstring'''



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

									lowercase__							       =				"""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				):
																					lowercase__							       =				FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE				)

															lowercase__							       =				FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE				,   subfolder=_SCREAMING_SNAKE_CASE				)

									self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE				,   _SCREAMING_SNAKE_CASE				)				)



			def A__       (    self				)	->						Any:




									'''simple docstring'''



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

									lowercase__							       =				"""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				):
																					lowercase__							       =				FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE				)

															lowercase__							       =				FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE				,   subfolder=_SCREAMING_SNAKE_CASE				)

									self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE				,   _SCREAMING_SNAKE_CASE				)				)



			def A__       (    self				)	->						List[str]:




									'''simple docstring'''



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

									with self.assertRaises(_SCREAMING_SNAKE_CASE				):
															lowercase__							       =				FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE				)

									lowercase__							       =				FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE				,   subfolder=_SCREAMING_SNAKE_CASE				)

									self.assertIsNotNone(_SCREAMING_SNAKE_CASE				)



			def A__       (    self				)	->						Any:




									'''simple docstring'''



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

									lowercase__							       =				FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE				,   subfolder=_SCREAMING_SNAKE_CASE				)

									self.assertIsNotNone(_SCREAMING_SNAKE_CASE				)











"""simple docstring"""
import copy
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


A:   List[str]								=		logging.get_logger(__name__)

A:   Dict								=		{
    "microsoft/conditional-detr-resnet-50": (
        "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json"
    ),
}



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):
   __lowerCAmelCase  :   Any						=						'conditional_detr'
   __lowerCAmelCase  :   Union[str, Any]						=						['past_key_values']
   __lowerCAmelCase  :   int						=						{
       'hidden_size': 'd_model',
       'num_attention_heads': 'encoder_attention_heads',
   }

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=None      ,     _SCREAMING_SNAKE_CASE=3      ,     _SCREAMING_SNAKE_CASE=300      ,     _SCREAMING_SNAKE_CASE=6      ,     _SCREAMING_SNAKE_CASE=2048      ,     _SCREAMING_SNAKE_CASE=8      ,     _SCREAMING_SNAKE_CASE=6      ,     _SCREAMING_SNAKE_CASE=2048      ,     _SCREAMING_SNAKE_CASE=8      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE="relu"      ,     _SCREAMING_SNAKE_CASE=256      ,     _SCREAMING_SNAKE_CASE=0.1      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.02      ,     _SCREAMING_SNAKE_CASE=1.0      ,     _SCREAMING_SNAKE_CASE=False      ,     _SCREAMING_SNAKE_CASE="sine"      ,     _SCREAMING_SNAKE_CASE="resnet50"      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=False      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=5      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=1      ,     _SCREAMING_SNAKE_CASE=1      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=5      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=0.25      ,     **_SCREAMING_SNAKE_CASE      ,     )  ->      Tuple:





    '''simple docstring'''




    if backbone_config is not None and use_timm_backbone:
     raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""						)

    if not use_timm_backbone:
     if backbone_config is None:
      logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""						)
      UpperCAmelCase	:					Tuple	=	CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]						)
     elif isinstance(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						):
      UpperCAmelCase	:					str	=	backbone_config.get("""model_type"""						)
      UpperCAmelCase	:					int	=	CONFIG_MAPPING[backbone_model_type]
      UpperCAmelCase	:					Union[str, Any]	=	config_class.from_dict(_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Union[str, Any]	=	use_timm_backbone
    UpperCAmelCase	:					Optional[int]	=	backbone_config
    UpperCAmelCase	:					List[str]	=	num_channels
    UpperCAmelCase	:					Any	=	num_queries
    UpperCAmelCase	:					Union[str, Any]	=	d_model
    UpperCAmelCase	:					List[str]	=	encoder_ffn_dim
    UpperCAmelCase	:					Optional[int]	=	encoder_layers
    UpperCAmelCase	:					Union[str, Any]	=	encoder_attention_heads
    UpperCAmelCase	:					Optional[Any]	=	decoder_ffn_dim
    UpperCAmelCase	:					Any	=	decoder_layers
    UpperCAmelCase	:					Optional[int]	=	decoder_attention_heads
    UpperCAmelCase	:					Optional[int]	=	dropout
    UpperCAmelCase	:					Dict	=	attention_dropout
    UpperCAmelCase	:					Dict	=	activation_dropout
    UpperCAmelCase	:					Any	=	activation_function
    UpperCAmelCase	:					Any	=	init_std
    UpperCAmelCase	:					Tuple	=	init_xavier_std
    UpperCAmelCase	:					Optional[int]	=	encoder_layerdrop
    UpperCAmelCase	:					Any	=	decoder_layerdrop
    UpperCAmelCase	:					Any	=	encoder_layers
    UpperCAmelCase	:					Optional[Any]	=	auxiliary_loss
    UpperCAmelCase	:					List[Any]	=	position_embedding_type
    UpperCAmelCase	:					Union[str, Any]	=	backbone
    UpperCAmelCase	:					List[Any]	=	use_pretrained_backbone
    UpperCAmelCase	:					Dict	=	dilation
    # Hungarian matcher
    UpperCAmelCase	:					Optional[int]	=	class_cost
    UpperCAmelCase	:					List[str]	=	bbox_cost
    UpperCAmelCase	:					List[str]	=	giou_cost
    # Loss coefficients
    UpperCAmelCase	:					List[Any]	=	mask_loss_coefficient
    UpperCAmelCase	:					List[str]	=	dice_loss_coefficient
    UpperCAmelCase	:					Optional[int]	=	cls_loss_coefficient
    UpperCAmelCase	:					Union[str, Any]	=	bbox_loss_coefficient
    UpperCAmelCase	:					Union[str, Any]	=	giou_loss_coefficient
    UpperCAmelCase	:					int	=	focal_alpha
    super().__init__(is_encoder_decoder=_SCREAMING_SNAKE_CASE      ,     **_SCREAMING_SNAKE_CASE						)

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    return self.encoder_attention_heads

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    return self.d_model




   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	copy.deepcopy(self.__dict__						)
    if self.backbone_config is not None:
     UpperCAmelCase	:					Union[str, Any]	=	self.backbone_config.to_dict()
    UpperCAmelCase	:					Dict	=	self.__class__.model_type
    return output



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):
   __lowerCAmelCase  :   Any						=						version.parse('1.11' )

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Mapping[str, Mapping[int, str]]:





    '''simple docstring'''




    return OrderedDict(
        [
            ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
            ("""pixel_mask""", {0: """batch"""}),
        ]						)

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      float:





    '''simple docstring'''




    return 1E-5




   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    return 12
