






import copy
import os
from typing import Union

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


UpperCamelCase			    =     logging.get_logger(__name__)

UpperCamelCase			    =     {
    """Salesforce/blip-vqa-base""": """https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json""",
    """Salesforce/blip-vqa-capfit-large""": (
        """https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json"""
    ),
    """Salesforce/blip-image-captioning-base""": (
        """https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json"""
    ),
    """Salesforce/blip-image-captioning-large""": (
        """https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json"""
    ),
    """Salesforce/blip-itm-base-coco""": """https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json""",
    """Salesforce/blip-itm-large-coco""": """https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json""",
    """Salesforce/blip-itm-base-flikr""": """https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json""",
    """Salesforce/blip-itm-large-flikr""": (
        """https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json"""
    ),
}



class 				UpperCamelCase__		(_UpperCAmelCase   ):
			__snake_case    :				Any									=							"blip_text_model"







			def __init__(     self:  Dict				,     UpperCAmelCase_:  Optional[int]=30_524				,     UpperCAmelCase_:  int=768				,     UpperCAmelCase_:  Union[str, Any]=768				,     UpperCAmelCase_:  Dict=3_072				,     UpperCAmelCase_:  Optional[int]=768				,     UpperCAmelCase_:  List[Any]=12				,     UpperCAmelCase_:  Dict=8				,     UpperCAmelCase_:  Tuple=512				,     UpperCAmelCase_:  Optional[Any]="gelu"				,     UpperCAmelCase_:  Any=1E-12				,     UpperCAmelCase_:  Any=0.0				,     UpperCAmelCase_:  Union[str, Any]=0.0				,     UpperCAmelCase_:  List[str]=0.02				,     UpperCAmelCase_:  int=30_522				,     UpperCAmelCase_:  Optional[Any]=2				,     UpperCAmelCase_:  int=0				,     UpperCAmelCase_:  Optional[Any]=102				,     UpperCAmelCase_:  Tuple=True				,     UpperCAmelCase_:  Dict=True				,     **UpperCAmelCase_:  Union[str, Any]				,     ):







									'''simple docstring'''




									super().__init__(
									    pad_token_id=UpperCAmelCase_				,     bos_token_id=UpperCAmelCase_				,     eos_token_id=UpperCAmelCase_				,     sep_token_id=UpperCAmelCase_				,     **UpperCAmelCase_				,     )

									_SCREAMING_SNAKE_CASE     				=  vocab_size
									_SCREAMING_SNAKE_CASE     				=  hidden_size
									_SCREAMING_SNAKE_CASE     				=  encoder_hidden_size
									_SCREAMING_SNAKE_CASE     				=  intermediate_size
									_SCREAMING_SNAKE_CASE     				=  projection_dim
									_SCREAMING_SNAKE_CASE     				=  hidden_dropout_prob
									_SCREAMING_SNAKE_CASE     				=  num_hidden_layers
									_SCREAMING_SNAKE_CASE     				=  num_attention_heads
									_SCREAMING_SNAKE_CASE     				=  max_position_embeddings
									_SCREAMING_SNAKE_CASE     				=  layer_norm_eps
									_SCREAMING_SNAKE_CASE     				=  hidden_act
									_SCREAMING_SNAKE_CASE     				=  initializer_range
									_SCREAMING_SNAKE_CASE     				=  attention_probs_dropout_prob
									_SCREAMING_SNAKE_CASE     				=  is_decoder
									_SCREAMING_SNAKE_CASE     				=  use_cache







			@classmethod
			def 							UpperCamelCase				(     cls:  List[Any]				,     UpperCAmelCase_:  Union[str, os.PathLike]				,     **UpperCAmelCase_:  str					):







									'''simple docstring'''




									cls._set_token_in_kwargs(UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE				,					_SCREAMING_SNAKE_CASE     				=  cls.get_config_dict(UpperCAmelCase_				,     **UpperCAmelCase_					)

									# get the text config dict if we are loading from BlipConfig
									if config_dict.get("""model_type"""					) == "blip":
															_SCREAMING_SNAKE_CASE     				=  config_dict["""text_config"""]

									if "model_type" in config_dict and hasattr(cls				,     """model_type"""					) and config_dict["model_type"] != cls.model_type:
															logger.warning(
															    F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
															    F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'					)

									return cls.from_dict(UpperCAmelCase_				,     **UpperCAmelCase_					)



class 				UpperCamelCase__		(_UpperCAmelCase   ):
			__snake_case    :				str									=							"blip_vision_model"







			def __init__(     self:  List[str]				,     UpperCAmelCase_:  int=768				,     UpperCAmelCase_:  List[Any]=3_072				,     UpperCAmelCase_:  Union[str, Any]=512				,     UpperCAmelCase_:  str=12				,     UpperCAmelCase_:  Any=12				,     UpperCAmelCase_:  int=384				,     UpperCAmelCase_:  Dict=16				,     UpperCAmelCase_:  Dict="gelu"				,     UpperCAmelCase_:  Dict=1E-5				,     UpperCAmelCase_:  Optional[int]=0.0				,     UpperCAmelCase_:  Optional[int]=1E-10				,     **UpperCAmelCase_:  int				,     ):







									'''simple docstring'''




									super().__init__(**UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  hidden_size
									_SCREAMING_SNAKE_CASE     				=  intermediate_size
									_SCREAMING_SNAKE_CASE     				=  projection_dim
									_SCREAMING_SNAKE_CASE     				=  num_hidden_layers
									_SCREAMING_SNAKE_CASE     				=  num_attention_heads
									_SCREAMING_SNAKE_CASE     				=  patch_size
									_SCREAMING_SNAKE_CASE     				=  image_size
									_SCREAMING_SNAKE_CASE     				=  initializer_range
									_SCREAMING_SNAKE_CASE     				=  attention_dropout
									_SCREAMING_SNAKE_CASE     				=  layer_norm_eps
									_SCREAMING_SNAKE_CASE     				=  hidden_act







			@classmethod
			def 							UpperCamelCase				(     cls:  Dict				,     UpperCAmelCase_:  Union[str, os.PathLike]				,     **UpperCAmelCase_:  Tuple					):







									'''simple docstring'''




									cls._set_token_in_kwargs(UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE				,					_SCREAMING_SNAKE_CASE     				=  cls.get_config_dict(UpperCAmelCase_				,     **UpperCAmelCase_					)

									# get the vision config dict if we are loading from BlipConfig
									if config_dict.get("""model_type"""					) == "blip":
															_SCREAMING_SNAKE_CASE     				=  config_dict["""vision_config"""]

									if "model_type" in config_dict and hasattr(cls				,     """model_type"""					) and config_dict["model_type"] != cls.model_type:
															logger.warning(
															    F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
															    F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'					)

									return cls.from_dict(UpperCAmelCase_				,     **UpperCAmelCase_					)



class 				UpperCamelCase__		(_UpperCAmelCase   ):
			__snake_case    :				Optional[int]									=							"blip"
			__snake_case    :				List[Any]									=							True







			def __init__(     self:  Tuple				,     UpperCAmelCase_:  List[str]=None				,     UpperCAmelCase_:  List[str]=None				,     UpperCAmelCase_:  Any=512				,     UpperCAmelCase_:  int=2.65_92				,     UpperCAmelCase_:  List[str]=256				,     **UpperCAmelCase_:  Optional[Any]				,     ):







									'''simple docstring'''




									super().__init__(**UpperCAmelCase_					)

									if text_config is None:
															_SCREAMING_SNAKE_CASE     				=  {}
															logger.info("""`text_config` is `None`. Initializing the `BlipTextConfig` with default values."""					)

									if vision_config is None:
															_SCREAMING_SNAKE_CASE     				=  {}
															logger.info("""`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values."""					)

									_SCREAMING_SNAKE_CASE     				=  BlipTextConfig(**UpperCAmelCase_					)
									_SCREAMING_SNAKE_CASE     				=  BlipVisionConfig(**UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  self.vision_config.hidden_size

									_SCREAMING_SNAKE_CASE     				=  projection_dim
									_SCREAMING_SNAKE_CASE     				=  logit_scale_init_value
									_SCREAMING_SNAKE_CASE     				=  1.0
									_SCREAMING_SNAKE_CASE     				=  0.02
									_SCREAMING_SNAKE_CASE     				=  image_text_hidden_size







			@classmethod
			def 							UpperCamelCase				(     cls:  str				,     UpperCAmelCase_:  BlipTextConfig				,     UpperCAmelCase_:  BlipVisionConfig				,     **UpperCAmelCase_:  Any					):







									'''simple docstring'''




									return cls(text_config=text_config.to_dict()				,     vision_config=vision_config.to_dict()				,     **UpperCAmelCase_					)







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  copy.deepcopy(self.__dict__					)
									_SCREAMING_SNAKE_CASE     				=  self.text_config.to_dict()
									_SCREAMING_SNAKE_CASE     				=  self.vision_config.to_dict()
									_SCREAMING_SNAKE_CASE     				=  self.__class__.model_type
									return output









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







			@classmethod
			def 							UpperCamelCase				(     cls:  Any					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  TOKEN
									HfFolder.save_token(UpperCAmelCase_					)







			@classmethod
			def 							UpperCamelCase				(     cls:  Union[str, 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 							UpperCamelCase				(     self:  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(UpperCAmelCase_					)
									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(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					)

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







			def 							UpperCamelCase				(     self:  int					):







									'''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(UpperCAmelCase_					)
									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(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					)

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

def 	__lowerCamelCase      (		snake_case__	,snake_case__		)							->  Tuple:






						"""simple docstring"""





						_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 							UpperCamelCase				(     self:  List[Any]					):







									'''simple docstring'''




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

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

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

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

									self.assertTrue(check_models_equal(UpperCAmelCase_				,     UpperCAmelCase_					)					)







			def 							UpperCamelCase				(     self:  List[str]					):







									'''simple docstring'''




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

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

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

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

									self.assertTrue(check_models_equal(UpperCAmelCase_				,     UpperCAmelCase_					)					)







			def 							UpperCamelCase				(     self:  List[Any]					):







									'''simple docstring'''




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

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

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

									self.assertIsNotNone(UpperCAmelCase_					)







			def 							UpperCamelCase				(     self:  Optional[Any]					):







									'''simple docstring'''




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

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

									self.assertIsNotNone(UpperCAmelCase_					)

