





"""simple docstring"""




import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402







class     __lowerCAmelCase	(   unittest.TestCase):







      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        a__     : List[Any]  =0







      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




        a__     : str  =AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"							)
        self.assertIsInstance(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)







      def        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




        with tempfile.TemporaryDirectory() as tmpdirname:
          a__     : Tuple  =Path(__SCREAMING_SNAKE_CASE							) / "preprocessor_config.json"
          a__     : str  =Path(__SCREAMING_SNAKE_CASE							) / "config.json"
          json.dump(
              {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)		,				)
          json.dump({"model_type": "clip"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)							)

          a__     : Optional[int]  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE							)
          self.assertIsInstance(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)







      def        _lowercase  (   self							)       ->						Dict:



        '''simple docstring'''




        with tempfile.TemporaryDirectory() as tmpdirname:
          a__     : Dict  =Path(__SCREAMING_SNAKE_CASE							) / "preprocessor_config.json"
          a__     : int  =Path(__SCREAMING_SNAKE_CASE							) / "config.json"
          json.dump(
              {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)		,				)
          json.dump({"model_type": "clip"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)							)

          a__     : Optional[int]  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE							)
          self.assertIsInstance(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)







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



        '''simple docstring'''




        with tempfile.TemporaryDirectory() as tmpdirname:
          a__     : Union[str, Any]  =CLIPConfig()

          # Create a dummy config file with image_proceesor_type
          a__     : List[str]  =Path(__SCREAMING_SNAKE_CASE							) / "preprocessor_config.json"
          a__     : Optional[Any]  =Path(__SCREAMING_SNAKE_CASE							) / "config.json"
          json.dump(
              {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)		,				)
          json.dump({"model_type": "clip"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)							)

          # remove image_processor_type to make sure config.json alone is enough to load image processor locally
          a__     : Tuple  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE							).to_dict()

          config_dict.pop("image_processor_type"							)
          a__     : str  =CLIPImageProcessor(**__SCREAMING_SNAKE_CASE							)

          # save in new folder
          model_config.save_pretrained(__SCREAMING_SNAKE_CASE							)
          config.save_pretrained(__SCREAMING_SNAKE_CASE							)

          a__     : List[str]  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE							)

          # make sure private variable is not incorrectly saved
          a__     : List[str]  =json.loads(config.to_json_string()							)
          self.assertTrue("_processor_class" not in dict_as_saved							)

        self.assertIsInstance(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)







      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        with tempfile.TemporaryDirectory() as tmpdirname:
          a__     : List[Any]  =Path(__SCREAMING_SNAKE_CASE							) / "preprocessor_config.json"
          json.dump(
              {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)		,				)

          a__     : Union[str, Any]  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE							)
          self.assertIsInstance(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)







      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        with self.assertRaisesRegex(
            __SCREAMING_SNAKE_CASE		,				"clip-base is not a local folder and is not a valid model identifier"							):
          a__     : Optional[Any]  =AutoImageProcessor.from_pretrained("clip-base"							)







      def        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




        with self.assertRaisesRegex(
            __SCREAMING_SNAKE_CASE		,				r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"							):
          a__     : Optional[int]  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE		,				revision="aaaaaa"							)







      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




        with self.assertRaisesRegex(
            __SCREAMING_SNAKE_CASE		,				"hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."		,				):
          a__     : Optional[int]  =AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model"							)







      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        with self.assertRaises(__SCREAMING_SNAKE_CASE							):
          a__     : List[Any]  =AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"							)
        # If remote code is disabled, we can't load this config.
        with self.assertRaises(__SCREAMING_SNAKE_CASE							):
          a__     : Optional[int]  =AutoImageProcessor.from_pretrained(
              "hf-internal-testing/test_dynamic_image_processor"		,				trust_remote_code=__SCREAMING_SNAKE_CASE							)

        a__     : Optional[int]  =AutoImageProcessor.from_pretrained(
            "hf-internal-testing/test_dynamic_image_processor"		,				trust_remote_code=__SCREAMING_SNAKE_CASE							)
        self.assertEqual(image_processor.__class__.__name__		,				"NewImageProcessor"							)

        # Test image processor can be reloaded.
        with tempfile.TemporaryDirectory() as tmp_dir:
          image_processor.save_pretrained(__SCREAMING_SNAKE_CASE							)
          a__     : Any  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE		,				trust_remote_code=__SCREAMING_SNAKE_CASE							)
        self.assertEqual(reloaded_image_processor.__class__.__name__		,				"NewImageProcessor"							)







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



        '''simple docstring'''




        try:
          AutoConfig.register("custom"		,				__SCREAMING_SNAKE_CASE							)
          AutoImageProcessor.register(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)
          # Trying to register something existing in the Transformers library will raise an error
          with self.assertRaises(__SCREAMING_SNAKE_CASE							):
            AutoImageProcessor.register(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)

          with tempfile.TemporaryDirectory() as tmpdirname:
            a__     : Optional[Any]  =Path(__SCREAMING_SNAKE_CASE							) / "preprocessor_config.json"
            a__     : Dict  =Path(__SCREAMING_SNAKE_CASE							) / "config.json"
            json.dump(
                {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)		,				)
            json.dump({"model_type": "clip"}		,				open(__SCREAMING_SNAKE_CASE		,				"w"							)							)

            a__     : Dict  =CustomImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE							)

          # Now that the config is registered, it can be used as any other config with the auto-API
          with tempfile.TemporaryDirectory() as tmp_dir:
            image_processor.save_pretrained(__SCREAMING_SNAKE_CASE							)
            a__     : int  =AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE							)
            self.assertIsInstance(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)

        finally:
          if "custom" in CONFIG_MAPPING._extra_content:
            del CONFIG_MAPPING._extra_content["custom"]
          if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
            del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]







      def        _lowercase  (   self							)       ->						Dict:



        '''simple docstring'''




        class     __lowerCAmelCase	(   _A):
              _lowercase    : int            =      True

        try:
          AutoConfig.register("custom"		,				__SCREAMING_SNAKE_CASE							)
          AutoImageProcessor.register(__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE							)
          # If remote code is not set, the default is to use local
          a__     : Tuple  =AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"							)
          self.assertEqual(image_processor.__class__.__name__		,				"NewImageProcessor"							)
          self.assertTrue(image_processor.is_local							)

          # If remote code is disabled, we load the local one.
          a__     : Optional[Any]  =AutoImageProcessor.from_pretrained(
              "hf-internal-testing/test_dynamic_image_processor"		,				trust_remote_code=__SCREAMING_SNAKE_CASE							)
          self.assertEqual(image_processor.__class__.__name__		,				"NewImageProcessor"							)
          self.assertTrue(image_processor.is_local							)

          # If remote is enabled, we load from the Hub
          a__     : str  =AutoImageProcessor.from_pretrained(
              "hf-internal-testing/test_dynamic_image_processor"		,				trust_remote_code=__SCREAMING_SNAKE_CASE							)
          self.assertEqual(image_processor.__class__.__name__		,				"NewImageProcessor"							)
          self.assertTrue(not hasattr(__SCREAMING_SNAKE_CASE		,				"is_local"							)							)

        finally:
          if "custom" in CONFIG_MAPPING._extra_content:
            del CONFIG_MAPPING._extra_content["custom"]
          if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
            del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]













import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase     :     List[Any]							=	logging.get_logger(__name__)

UpperCAmelCase     :     Tuple							=	{
    """Salesforce/instruct-blip-flan-t5""": """https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json""",
}







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip_vision_model"""







      def __init__(   self		,				lowerCAmelCase__=1_4_0_8		,				lowerCAmelCase__=6_1_4_4		,				lowerCAmelCase__=3_9		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=2_2_4		,				lowerCAmelCase__=1_4		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=1E-6		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=1E-10		,				lowerCAmelCase__=True		,				**lowerCAmelCase__		,				)       ->						Optional[Any]:



        '''simple docstring'''




        super().__init__(**lowerCAmelCase__							)

        a__     : Tuple  =hidden_size
        a__     : Any  =intermediate_size
        a__     : Union[str, Any]  =num_hidden_layers
        a__     : Optional[Any]  =num_attention_heads
        a__     : List[str]  =patch_size
        a__     : int  =image_size
        a__     : Tuple  =initializer_range
        a__     : Any  =attention_dropout
        a__     : List[Any]  =layer_norm_eps
        a__     : Optional[Any]  =hidden_act
        a__     : Optional[Any]  =qkv_bias







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				**lowerCAmelCase__							)       ->						"PretrainedConfig":



        '''simple docstring'''




        cls._set_token_in_kwargs(lowerCAmelCase__							)

        a__ ,							a__     : Optional[Any]  =cls.get_config_dict(lowerCAmelCase__		,				**lowerCAmelCase__							)

        # get the vision config dict if we are loading from InstructBlipConfig
        if config_dict.get("model_type"							) == "instructblip":
          a__     : Any  =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(lowerCAmelCase__		,				**lowerCAmelCase__							)







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip_qformer"""







      def __init__(   self		,				lowerCAmelCase__=3_0_5_2_2		,				lowerCAmelCase__=7_6_8		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=3_0_7_2		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=5_1_2		,				lowerCAmelCase__=0.02		,				lowerCAmelCase__=1E-12		,				lowerCAmelCase__=0		,				lowerCAmelCase__="absolute"		,				lowerCAmelCase__=2		,				lowerCAmelCase__=1_4_0_8		,				**lowerCAmelCase__		,				)       ->						Dict:



        '''simple docstring'''




        super().__init__(pad_token_id=lowerCAmelCase__		,				**lowerCAmelCase__							)

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







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				**lowerCAmelCase__							)       ->						"PretrainedConfig":



        '''simple docstring'''




        cls._set_token_in_kwargs(lowerCAmelCase__							)

        a__ ,							a__     : str  =cls.get_config_dict(lowerCAmelCase__		,				**lowerCAmelCase__							)

        # get the qformer config dict if we are loading from InstructBlipConfig
        if config_dict.get("model_type"							) == "instructblip":
          a__     : Optional[int]  =config_dict["qformer_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(lowerCAmelCase__		,				**lowerCAmelCase__							)







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip"""
      _lowercase    : List[Any]            =      True







      def __init__(   self		,				lowerCAmelCase__=None		,				lowerCAmelCase__=None		,				lowerCAmelCase__=None		,				lowerCAmelCase__=3_2		,				**lowerCAmelCase__							)       ->						str:



        '''simple docstring'''




        super().__init__(**lowerCAmelCase__							)

        if vision_config is None:
          a__     : List[Any]  ={}
          logger.info("vision_config is None. initializing the InstructBlipVisionConfig with default values."							)

        if qformer_config is None:
          a__     : Tuple  ={}
          logger.info("qformer_config is None. Initializing the InstructBlipQFormerConfig with default values."							)

        if text_config is None:
          a__     : Dict  ={}
          logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`)."							)

        a__     : Dict  =InstructBlipVisionConfig(**lowerCAmelCase__							)
        a__     : Union[str, Any]  =InstructBlipQFormerConfig(**lowerCAmelCase__							)
        a__     : Tuple  =text_config["model_type"] if "model_type" in text_config else "opt"
        a__     : List[str]  =CONFIG_MAPPING[text_model_type](**lowerCAmelCase__							)

        a__     : Union[str, Any]  =self.text_config.tie_word_embeddings
        a__     : Optional[Any]  =self.text_config.is_encoder_decoder

        a__     : str  =num_query_tokens
        a__     : List[Any]  =self.vision_config.hidden_size
        a__     : str  =self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
        a__     : List[Any]  =1.0
        a__     : List[str]  =0.02







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				**lowerCAmelCase__		,				)       ->						int:



        '''simple docstring'''




        return cls(
            vision_config=vision_config.to_dict()		,				qformer_config=qformer_config.to_dict()		,				text_config=text_config.to_dict()		,				**lowerCAmelCase__		,				)







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



        '''simple docstring'''




        a__     : int  =copy.deepcopy(self.__dict__							)
        a__     : int  =self.vision_config.to_dict()
        a__     : str  =self.qformer_config.to_dict()
        a__     : str  =self.text_config.to_dict()
        a__     : List[str]  =self.__class__.model_type
        return output






